Skip to content

Commit

Permalink
Add runtime check for MIN_NODE_VERSION (#18845)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbc100 authored Feb 24, 2023
1 parent ca19311 commit a957d7d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/parseTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -995,3 +995,10 @@ function preJS() {
}
return result;
}

function formattedMinNodeVersion() {
var major = MIN_NODE_VERSION / 10000
var minor = (MIN_NODE_VERSION / 100) % 100
var rev = MIN_NODE_VERSION % 100
return `v${major}.${minor}.${rev}`;
}
11 changes: 11 additions & 0 deletions src/shell.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,17 @@ if (ENVIRONMENT_IS_NODE) {
#if ENVIRONMENT && ASSERTIONS
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?)');
#endif

#if ASSERTIONS
var nodeVersion = process.versions.node;
var numericVersion = nodeVersion.split('.').slice(0, 3);
numericVersion = (numericVersion[0] * 10000) + (numericVersion[1] * 100) + numericVersion[2] * 1;
var minVersion = {{{ MIN_NODE_VERSION }}};
if (numericVersion < {{{ MIN_NODE_VERSION }}}) {
throw new Error('This emscripten-generated code requires node {{{ formattedMinNodeVersion() }}} (detected v' + nodeVersion + ')');
}
#endif

// `require()` is no-op in an ESM module, use `createRequire()` to construct
// the require()` function. This is only necessary for multi-environment
// builds, `-sENVIRONMENT=node` emits a static import declaration instead.
Expand Down
7 changes: 7 additions & 0 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -13026,3 +13026,10 @@ def test_parseTools_legacy(self):
self.set_setting('DEFAULT_LIBRARY_FUNCS_TO_INCLUDE', 'foo')
self.do_runf(test_file('hello_world.c'), '4\nhello, world!',
emcc_args=['--post-js=post.js', '--js-library=lib.js'])

def test_min_node_version(self):
node_version = shared.check_node_version()
node_version = '.'.join(str(x) for x in node_version)
self.set_setting('MIN_NODE_VERSION', 210000)
expected = 'This emscripten-generated code requires node v21.0.0 (detected v%s)' % node_version
self.do_runf(test_file('hello_world.c'), expected, assert_returncode=NON_ZERO)

0 comments on commit a957d7d

Please sign in to comment.