]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/internal/osinfo: report Node.js version
authorDmitri Shuralyov <dmitshur@golang.org>
Thu, 5 Jan 2023 01:54:34 +0000 (20:54 -0500)
committerGopher Robot <gobot@golang.org>
Thu, 19 Jan 2023 22:43:17 +0000 (22:43 +0000)
Seeing the Node.js version that was used during a particular test run
should be helpful during the upcoming migration from Node.js 14 to 18.
Add minimal support for that.

For golang/go#57614.

Change-Id: Id55ba25a7ee4a803788316d4a646cd4b6f4297e9
Reviewed-on: https://go-review.googlesource.com/c/go/+/460655
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Dmitri Shuralyov <dmitshur@golang.org>
Reviewed-by: Michael Pratt <mpratt@google.com>
Reviewed-by: Richard Musiol <neelance@gmail.com>
Reviewed-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
Auto-Submit: Dmitri Shuralyov <dmitshur@golang.org>

src/cmd/internal/osinfo/os_js.go

index 882580d652796974067ab8178ec59f278ede59d1..f4f03aa312402ec68c9f48bbb7d7332a8fb36b0c 100644 (file)
@@ -8,14 +8,31 @@ package osinfo
 
 import (
        "fmt"
+       "syscall/js"
 )
 
 // Version returns the OS version name/number.
 func Version() (string, error) {
-       // Version detection on wasm varies depending on the underlying runtime
+       // Version detection on Wasm varies depending on the underlying runtime
        // (browser, node, etc), nor is there a standard via something like
-       // WASI (see https://go.dev/issue/31105). We could attempt multiple
-       // combinations, but for now we leave this unimplemented for
-       // simplicity.
-       return "", fmt.Errorf("unimplemented")
+       // WASI (see https://go.dev/issue/31105). For now, attempt a few simple
+       // combinations for the convenience of reading logs at build.golang.org
+       // and local development. It's not a goal to recognize all environments.
+       if v, ok := node(); ok {
+               return "Node.js " + v, nil
+       }
+       return "", fmt.Errorf("unrecognized environment")
+}
+
+func node() (version string, ok bool) {
+       // Try the https://nodejs.org/api/process.html#processversion API.
+       p := js.Global().Get("process")
+       if p.IsUndefined() {
+               return "", false
+       }
+       v := p.Get("version")
+       if v.IsUndefined() {
+               return "", false
+       }
+       return v.String(), true
 }