Skip to content

Commit a957d7d

Browse files
authored
Add runtime check for MIN_NODE_VERSION (#18845)
See emscripten-core/emsdk#1189
1 parent ca19311 commit a957d7d

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

src/parseTools.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,3 +995,10 @@ function preJS() {
995995
}
996996
return result;
997997
}
998+
999+
function formattedMinNodeVersion() {
1000+
var major = MIN_NODE_VERSION / 10000
1001+
var minor = (MIN_NODE_VERSION / 100) % 100
1002+
var rev = MIN_NODE_VERSION % 100
1003+
return `v${major}.${minor}.${rev}`;
1004+
}

src/shell.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,17 @@ if (ENVIRONMENT_IS_NODE) {
177177
#if ENVIRONMENT && ASSERTIONS
178178
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?)');
179179
#endif
180+
181+
#if ASSERTIONS
182+
var nodeVersion = process.versions.node;
183+
var numericVersion = nodeVersion.split('.').slice(0, 3);
184+
numericVersion = (numericVersion[0] * 10000) + (numericVersion[1] * 100) + numericVersion[2] * 1;
185+
var minVersion = {{{ MIN_NODE_VERSION }}};
186+
if (numericVersion < {{{ MIN_NODE_VERSION }}}) {
187+
throw new Error('This emscripten-generated code requires node {{{ formattedMinNodeVersion() }}} (detected v' + nodeVersion + ')');
188+
}
189+
#endif
190+
180191
// `require()` is no-op in an ESM module, use `createRequire()` to construct
181192
// the require()` function. This is only necessary for multi-environment
182193
// builds, `-sENVIRONMENT=node` emits a static import declaration instead.

test/test_other.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13026,3 +13026,10 @@ def test_parseTools_legacy(self):
1302613026
self.set_setting('DEFAULT_LIBRARY_FUNCS_TO_INCLUDE', 'foo')
1302713027
self.do_runf(test_file('hello_world.c'), '4\nhello, world!',
1302813028
emcc_args=['--post-js=post.js', '--js-library=lib.js'])
13029+
13030+
def test_min_node_version(self):
13031+
node_version = shared.check_node_version()
13032+
node_version = '.'.join(str(x) for x in node_version)
13033+
self.set_setting('MIN_NODE_VERSION', 210000)
13034+
expected = 'This emscripten-generated code requires node v21.0.0 (detected v%s)' % node_version
13035+
self.do_runf(test_file('hello_world.c'), expected, assert_returncode=NON_ZERO)

0 commit comments

Comments
 (0)