diff options
author | Richard Musiol <mail@richard-musiol.de> | 2018-10-25 23:11:10 +0200 |
---|---|---|
committer | Richard Musiol <neelance@gmail.com> | 2018-10-25 22:13:48 +0000 |
commit | 9627180f0f1f016307f4987cec6594baf90d64ae (patch) | |
tree | d7827ed3642f6cb3be58a28774d28644099596db /misc/wasm | |
parent | dd789550a74817e88466cdb583ae86c4c1426380 (diff) | |
download | go-9627180f0f1f016307f4987cec6594baf90d64ae.tar.gz go-9627180f0f1f016307f4987cec6594baf90d64ae.zip |
misc/wasm: improve detection of Node.js
This commit adds a check of "process.title" to detect Node.js.
The web app bundler Parcel sets "process" to an empty object. This
incorrectly got detected as Node.js, even though the script was
running in a browser.
Fixes #28364.
Change-Id: Iecac7f8fc3cc4ac7ddb42dd43c5385681a3282de
Reviewed-on: https://go-review.googlesource.com/c/144658
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Diffstat (limited to 'misc/wasm')
-rw-r--r-- | misc/wasm/wasm_exec.js | 20 |
1 files changed, 11 insertions, 9 deletions
diff --git a/misc/wasm/wasm_exec.js b/misc/wasm/wasm_exec.js index 78eb306253..e47663783e 100644 --- a/misc/wasm/wasm_exec.js +++ b/misc/wasm/wasm_exec.js @@ -3,8 +3,18 @@ // license that can be found in the LICENSE file. (() => { + if (typeof global !== "undefined") { + // global already exists + } else if (typeof window !== "undefined") { + window.global = window; + } else if (typeof self !== "undefined") { + self.global = self; + } else { + throw new Error("cannot export Go (neither global, window nor self is defined)"); + } + // Map web browser API and Node.js API to a single common API (preferring web standards over Node.js API). - const isNodeJS = typeof process !== "undefined"; + const isNodeJS = global.process && global.process.title === "node"; if (isNodeJS) { global.require = require; global.fs = require("fs"); @@ -27,14 +37,6 @@ global.TextEncoder = util.TextEncoder; global.TextDecoder = util.TextDecoder; } else { - if (typeof window !== "undefined") { - window.global = window; - } else if (typeof self !== "undefined") { - self.global = self; - } else { - throw new Error("cannot export Go (neither window nor self is defined)"); - } - let outputBuf = ""; global.fs = { constants: { O_WRONLY: -1, O_RDWR: -1, O_CREAT: -1, O_TRUNC: -1, O_APPEND: -1, O_EXCL: -1 }, // unused |