Skip to content

Commit 117686e

Browse files
authored
Unify Node detection code (#24354)
This was duplicated in quite a few places and slightly different in some places. This change reduces that size of the code a little and reduces the number of places we specify it to just two. This change also fixes the `instance.test_environment` test which was failing due to broken nodejs detection in shell.js (it was checking for `require` which doesn't exist in ES module mode). Split out from #24344
1 parent be873b8 commit 117686e

File tree

94 files changed

+108
-96
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+108
-96
lines changed

.circleci/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,7 @@ jobs:
652652
wasmfs.test_freetype
653653
minimal0.test_utf
654654
instance.test_async_hello_v8
655+
instance.test_environment
655656
"
656657
test-wasm2js1:
657658
environment:

src/parseTools.mjs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,6 +1093,10 @@ function ENVIRONMENT_IS_WORKER_THREAD() {
10931093
return '(' + envs.join('||') + ')';
10941094
}
10951095

1096+
function nodeDetectionCode() {
1097+
return "typeof process == 'object' && process.versions?.node && process.type != 'renderer'";
1098+
}
1099+
10961100
addToCompileTimeContext({
10971101
ATEXITS,
10981102
ATPRERUNS,
@@ -1153,6 +1157,7 @@ addToCompileTimeContext({
11531157
makeSetValue,
11541158
makeThrow,
11551159
modifyJSFunction,
1160+
nodeDetectionCode,
11561161
receiveI64ParamAsI53,
11571162
receiveI64ParamAsI53Unchecked,
11581163
receivedSymbol,

src/proxyClient.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#endif
1717

1818
#if ENVIRONMENT_MAY_BE_NODE
19-
var ENVIRONMENT_IS_NODE = typeof process == 'object' && typeof process.versions == 'object' && typeof process.versions.node == 'string' && process.type != 'renderer';
19+
var ENVIRONMENT_IS_NODE = {{{ nodeDetectionCode() }}};
2020
if (ENVIRONMENT_IS_NODE) {
2121
var NodeWorker = require('worker_threads').Worker;
2222
global.Worker = function(url, options) {

src/shell.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ var ENVIRONMENT_IS_WEB = typeof window == 'object';
8484
var ENVIRONMENT_IS_WORKER = typeof WorkerGlobalScope != 'undefined';
8585
// N.b. Electron.js environment is simultaneously a NODE-environment, but
8686
// also a web environment.
87-
var ENVIRONMENT_IS_NODE = typeof process == 'object' && typeof process.versions == 'object' && typeof process.versions.node == 'string' && process.type != 'renderer';
87+
var ENVIRONMENT_IS_NODE = {{{ nodeDetectionCode() }}};
8888
#if AUDIO_WORKLET
8989
var ENVIRONMENT_IS_SHELL = !ENVIRONMENT_IS_WEB && !ENVIRONMENT_IS_NODE && !ENVIRONMENT_IS_WORKER && !ENVIRONMENT_IS_AUDIO_WORKLET;
9090
#else
@@ -193,7 +193,8 @@ var readAsync, readBinary;
193193
#if ENVIRONMENT_MAY_BE_NODE
194194
if (ENVIRONMENT_IS_NODE) {
195195
#if ENVIRONMENT && ASSERTIONS
196-
if (typeof process == 'undefined' || !process.release || process.release.name !== 'node') throw new Error('not compiled for this environment (did you build to HTML and try to run it not on the web, or set ENVIRONMENT to something - like node - and run it someplace else - like on the web?)');
196+
const isNode = {{{ nodeDetectionCode() }}};
197+
if (!isNode) throw new Error('not compiled for this environment (did you build to HTML and try to run it not on the web, or set ENVIRONMENT to something - like node - and run it someplace else - like on the web?)');
197198
#endif
198199

199200
#if ASSERTIONS
@@ -276,7 +277,8 @@ if (ENVIRONMENT_IS_NODE) {
276277
if (ENVIRONMENT_IS_SHELL) {
277278

278279
#if ENVIRONMENT && ASSERTIONS
279-
if ((typeof process == 'object' && typeof require === 'function') || typeof window == 'object' || typeof WorkerGlobalScope != 'undefined') throw new Error('not compiled for this environment (did you build to HTML and try to run it not on the web, or set ENVIRONMENT to something - like node - and run it someplace else - like on the web?)');
280+
const isNode = {{{ nodeDetectionCode() }}};
281+
if (isNode || typeof window == 'object' || typeof WorkerGlobalScope != 'undefined') throw new Error('not compiled for this environment (did you build to HTML and try to run it not on the web, or set ENVIRONMENT to something - like node - and run it someplace else - like on the web?)');
280282
#endif
281283

282284
#if ENVIRONMENT_MAY_BE_SHELL

src/shell_minimal.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ var readyPromise = new Promise((resolve, reject) => {
4444
#endif
4545

4646
#if ENVIRONMENT_MAY_BE_NODE
47-
var ENVIRONMENT_IS_NODE = typeof process == 'object' && process.type != 'renderer';
47+
var ENVIRONMENT_IS_NODE = {{{ nodeDetectionCode() }}};
4848
#endif
4949

5050
#if ENVIRONMENT_MAY_BE_SHELL

test/core/test_esm_integration.expected.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export { default, _foo, _main, err, stringToNewUTF8 } from './hello_world.suppor
66
// When run as the main module under node, create the module directly. This will
77
// execute any startup code along with main (if it exists).
88
import init from './hello_world.support.mjs';
9-
const isNode = typeof process == 'object' && typeof process.versions == 'object' && typeof process.versions.node == 'string' && process.type != 'renderer';
9+
const isNode = globalThis.process?.versions?.node && globalThis.process?.type != 'renderer';
1010
if (isNode) {
1111
const url = await import('url');
1212
const isMainModule = url.pathToFileURL(process.argv[1]).href === import.meta.url;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8208
1+
8202
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
19920
1+
19869
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8196
1+
8191
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
19898
1+
19847

0 commit comments

Comments
 (0)