From 5d2ae05131aa23a5d28103b24ad58f9d7b5ae9c1 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Sat, 14 Dec 2019 11:14:02 -0500 Subject: [PATCH] wasi: require CLI flag to require() wasi module This commit ensures that the WASI module cannot be require()'ed without a CLI flag while the module is still experimental. This fixes a regression from https://github.com/nodejs/node/pull/30778. PR-URL: https://github.com/nodejs/node/pull/30963 Reviewed-By: Anna Henningsen Reviewed-By: Richard Lau Reviewed-By: Ruben Bridgewater Reviewed-By: James M Snell Reviewed-By: Rich Trott Reviewed-By: David Carlier --- lib/internal/bootstrap/pre_execution.js | 9 ++++----- src/node_native_module.cc | 1 + test/wasi/test-wasi-require-flag.js | 9 +++++++++ 3 files changed, 14 insertions(+), 5 deletions(-) create mode 100644 test/wasi/test-wasi-require-flag.js diff --git a/lib/internal/bootstrap/pre_execution.js b/lib/internal/bootstrap/pre_execution.js index 81002a3e3c49f6..67fcfc8fbf172f 100644 --- a/lib/internal/bootstrap/pre_execution.js +++ b/lib/internal/bootstrap/pre_execution.js @@ -402,11 +402,10 @@ function initializePolicy() { } function initializeWASI() { - if (getOptionValue('--experimental-wasi-unstable-preview0')) { - const { NativeModule } = require('internal/bootstrap/loaders'); - const mod = NativeModule.map.get('wasi'); - mod.canBeRequiredByUsers = true; - } + const { NativeModule } = require('internal/bootstrap/loaders'); + const mod = NativeModule.map.get('wasi'); + mod.canBeRequiredByUsers = + getOptionValue('--experimental-wasi-unstable-preview0'); } function initializeCJSLoader() { diff --git a/src/node_native_module.cc b/src/node_native_module.cc index 814adb620dcf8a..43c13ea30a0dc5 100644 --- a/src/node_native_module.cc +++ b/src/node_native_module.cc @@ -98,6 +98,7 @@ void NativeModuleLoader::InitializeModuleCategories() { #endif // !HAVE_OPENSSL "sys", // Deprecated. + "wasi", // Experimental. "internal/test/binding", "internal/v8_prof_polyfill", "internal/v8_prof_processor", diff --git a/test/wasi/test-wasi-require-flag.js b/test/wasi/test-wasi-require-flag.js new file mode 100644 index 00000000000000..0a52ea952d2785 --- /dev/null +++ b/test/wasi/test-wasi-require-flag.js @@ -0,0 +1,9 @@ +'use strict'; +// This test verifies that the WASI module cannot be require()'ed without a +// CLI flag while it is still experimental. +require('../common'); +const assert = require('assert'); + +assert.throws(() => { + require('wasi'); +}, /^Error: Cannot find module 'wasi'/);