github.com/bir3/gocompiler@v0.9.2202/src/cmd/internal/osinfo/os_js.go (about)

     1  // Copyright 2022 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build js
     6  
     7  package osinfo
     8  
     9  import (
    10  	"fmt"
    11  	"syscall/js"
    12  )
    13  
    14  // Version returns the OS version name/number.
    15  func Version() (string, error) {
    16  	// Version detection on Wasm varies depending on the underlying runtime
    17  	// (browser, node, etc), nor is there a standard via something like
    18  	// WASI (see https://go.dev/issue/31105). For now, attempt a few simple
    19  	// combinations for the convenience of reading logs at build.golang.org
    20  	// and local development. It's not a goal to recognize all environments.
    21  	if v, ok := node(); ok {
    22  		return "Node.js " + v, nil
    23  	}
    24  	return "", fmt.Errorf("unrecognized environment")
    25  }
    26  
    27  func node() (version string, ok bool) {
    28  	// Try the https://nodejs.org/api/process.html#processversion API.
    29  	p := js.Global().Get("process")
    30  	if p.IsUndefined() {
    31  		return "", false
    32  	}
    33  	v := p.Get("version")
    34  	if v.IsUndefined() {
    35  		return "", false
    36  	}
    37  	return v.String(), true
    38  }