| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent d657ae6 commit 9f3a015
23 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -595,6 +595,15 @@ added: v7.10.0 | |||
| 595 | 595 | ||
| 596 | 596 | This option is a no-op. It is kept for compatibility. | |
| 597 | 597 | ||
| 598 | + ### `--no-addons` | ||
| 599 | + <!-- YAML | ||
| 600 | + added: REPLACEME | ||
| 601 | + --> | ||
| 602 | + | ||
| 603 | + Disable the `node-addons` exports condition as well as disable loading | ||
| 604 | + native addons. When `--no-addons` is specified, calling `process.dlopen` or | ||
| 605 | + requiring a native C++ addon will fail and throw an exception. | ||
| 606 | + | ||
| 598 | 607 | ### `--no-deprecation` | |
| 599 | 608 | <!-- YAML | |
| 600 | 609 | added: v0.8.0 | |
@@ -1418,6 +1427,7 @@ Node.js options that are allowed are: | |||
| 1418 | 1427 | * `--inspect` | |
| 1419 | 1428 | * `--max-http-header-size` | |
| 1420 | 1429 | * `--napi-modules` | |
| 1430 | + * `--no-addons` | ||
| 1421 | 1431 | * `--no-deprecation` | |
| 1422 | 1432 | * `--no-experimental-repl-await` | |
| 1423 | 1433 | * `--no-force-async-hooks-checks` | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1019,6 +1019,14 @@ added: v16.4.0 | |||
| 1019 | 1019 | ||
| 1020 | 1020 | The [debugger][] timed out waiting for the required host/port to be free. | |
| 1021 | 1021 | ||
| 1022 | + <a id="ERR_DLOPEN_DISABLED"></a> | ||
| 1023 | + ### `ERR_DLOPEN_DISABLED` | ||
| 1024 | + <!-- YAML | ||
| 1025 | + added: REPLACEME | ||
| 1026 | + --> | ||
| 1027 | + | ||
| 1028 | + Loading native addons has been disabled using [`--no-addons`][]. | ||
| 1029 | + | ||
| 1022 | 1030 | <a id="ERR_DLOPEN_FAILED"></a> | |
| 1023 | 1031 | ### `ERR_DLOPEN_FAILED` | |
| 1024 | 1032 | <!-- YAML | |
@@ -2871,6 +2879,7 @@ The native call from `process.cpuUsage` could not be processed. | |||
| 2871 | 2879 | [`'uncaughtException'`]: process.md#process_event_uncaughtexception | |
| 2872 | 2880 | [`--disable-proto=throw`]: cli.md#cli_disable_proto_mode | |
| 2873 | 2881 | [`--force-fips`]: cli.md#cli_force_fips | |
| 2882 | + [`--no-addons`]: cli.md#cli_no_addons | ||
| 2874 | 2883 | [`Class: assert.AssertionError`]: assert.md#assert_class_assert_assertionerror | |
| 2875 | 2884 | [`ERR_INVALID_ARG_TYPE`]: #ERR_INVALID_ARG_TYPE | |
| 2876 | 2885 | [`ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST`]: #ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -537,6 +537,11 @@ Node.js implements the following conditions: | |||
| 537 | 537 | * `"node"` - matches for any Node.js environment. Can be a CommonJS or ES | |
| 538 | 538 | module file. _This condition should always come after `"import"` or | |
| 539 | 539 | `"require"`._ | |
| 540 | + * `"node-addons"` - similar to `"node"` and matches for any Node.js environment. | ||
| 541 | + This condition can be used to provide an entry point which uses native C++ | ||
| 542 | + addons as opposed to an entry point which is more universal and doesn't rely | ||
| 543 | + on native addons. This condition can be disabled via the | ||
| 544 | + [`--no-addons` flag][]. | ||
| 540 | 545 | * `"default"` - the generic fallback that always matches. Can be a CommonJS | |
| 541 | 546 | or ES module file. _This condition should always come last._ | |
| 542 | 547 | ||
@@ -615,17 +620,23 @@ node --conditions=development main.js | |||
| 615 | 620 | ``` | |
| 616 | 621 | ||
| 617 | 622 | which would then resolve the `"development"` condition in package imports and | |
| 618 | - exports, while resolving the existing `"node"`, `"default"`, `"import"`, and | ||
| 619 | - `"require"` conditions as appropriate. | ||
| 623 | + exports, while resolving the existing `"node"`, `"node-addons"`, `"default"`, | ||
| 624 | + `"import"`, and `"require"` conditions as appropriate. | ||
| 620 | 625 | ||
| 621 | 626 | Any number of custom conditions can be set with repeat flags. | |
| 622 | 627 | ||
| 623 | 628 | ### Conditions Definitions | |
| 624 | 629 | ||
| 625 | - The `"import"`, `"require"`, `"node"` and `"default"` conditions are defined | ||
| 626 | - and implemented in Node.js core, | ||
| 630 | + The `"import"` , `"require"` , `"node"` , `"node-addons"` and `"default"` | ||
| 631 | + conditions are defined and implemented in Node.js core, | ||
| 627 | 632 | [as specified above](#packages_conditional_exports). | |
| 628 | 633 | ||
| 634 | + The `"node-addons"` condition can be used to provide an entry point which | ||
| 635 | + uses native C++ addons. However, this condition can be disabled via the | ||
| 636 | + [`--no-addons` flag][]. When using `"node-addons"`, it's recommended to treat | ||
| 637 | + `"default"` as an enhancement that provides a more universal entry point, e.g. | ||
| 638 | + using WebAssembly instead of a native addon. | ||
| 639 | + | ||
| 629 | 640 | Other condition strings are unknown to Node.js and thus ignored by default. | |
| 630 | 641 | Runtimes or tools other than Node.js can use them at their discretion. | |
| 631 | 642 | ||
@@ -1249,6 +1260,7 @@ This field defines [subpath imports][] for the current package. | |||
| 1249 | 1260 | [`"name"`]: #packages_name | |
| 1250 | 1261 | [`"packageManager"`]: #packages_packagemanager | |
| 1251 | 1262 | [`"type"`]: #packages_type | |
| 1263 | + [`--no-addons` flag]: cli.md#cli_no_addons | ||
| 1252 | 1264 | [`ERR_PACKAGE_PATH_NOT_EXPORTED`]: errors.md#errors_err_package_path_not_exported | |
| 1253 | 1265 | [`esm`]: https://github.com/standard-things/esm#readme | |
| 1254 | 1266 | [`package.json`]: #packages_node_js_package_json_field_definitions | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -277,6 +277,11 @@ Silence deprecation warnings. | |||
| 277 | 277 | Disable runtime checks for `async_hooks`. | |
| 278 | 278 | These will still be enabled dynamically when `async_hooks` is enabled. | |
| 279 | 279 | . | |
| 280 | + .It Fl -no-addons | ||
| 281 | + Disable the `node-addons` exports condition as well as disable loading native | ||
| 282 | + addons. When `--no-addons` is specified, calling `process.dlopen` or requiring | ||
| 283 | + a native C++ addon will fail and throw an exception. | ||
| 284 | + . | ||
| 280 | 285 | .It Fl -no-warnings | |
| 281 | 286 | Silence all process warnings (including deprecations). | |
| 282 | 287 | . | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -30,8 +30,16 @@ let debug = require('internal/util/debuglog').debuglog('module', (fn) => { | |||
| 30 | 30 | debug = fn; | |
| 31 | 31 | }); | |
| 32 | 32 | ||
| 33 | + const noAddons = getOptionValue('--no-addons'); | ||
| 34 | + const addonConditions = noAddons ? [] : ['node-addons']; | ||
| 35 | + | ||
| 33 | 36 | // TODO: Use this set when resolving pkg#exports conditions in loader.js. | |
| 34 | - const cjsConditions = new SafeSet(['require', 'node', ...userConditions]); | ||
| 37 | + const cjsConditions = new SafeSet([ | ||
| 38 | + 'require', | ||
| 39 | + 'node', | ||
| 40 | + ...addonConditions, | ||
| 41 | + ...userConditions, | ||
| 42 | + ]); | ||
| 35 | 43 | ||
| 36 | 44 | function loadNativeModule(filename, request) { | |
| 37 | 45 | const mod = NativeModule.map.get(filename); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -58,7 +58,16 @@ const { Module: CJSModule } = require('internal/modules/cjs/loader'); | |||
| 58 | 58 | ||
| 59 | 59 | const packageJsonReader = require('internal/modules/package_json_reader'); | |
| 60 | 60 | const userConditions = getOptionValue('--conditions'); | |
| 61 | - const DEFAULT_CONDITIONS = ObjectFreeze(['node', 'import', ...userConditions]); | ||
| 61 | + const noAddons = getOptionValue('--no-addons'); | ||
| 62 | + const addonConditions = noAddons ? [] : ['node-addons']; | ||
| 63 | + | ||
| 64 | + const DEFAULT_CONDITIONS = ObjectFreeze([ | ||
| 65 | + 'node', | ||
| 66 | + 'import', | ||
| 67 | + ...addonConditions, | ||
| 68 | + ...userConditions, | ||
| 69 | + ]); | ||
| 70 | + | ||
| 62 | 71 | const DEFAULT_CONDITIONS_SET = new SafeSet(DEFAULT_CONDITIONS); | |
| 63 | 72 | ||
| 64 | 73 | /** | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -861,6 +861,11 @@ inline bool Environment::is_main_thread() const { | |||
| 861 | 861 | return worker_context() == nullptr; | |
| 862 | 862 | } | |
| 863 | 863 | ||
| 864 | + inline bool Environment::no_native_addons() const { | ||
| 865 | + return (flags_ & EnvironmentFlags::kNoNativeAddons) || | ||
| 866 | + !options_->allow_native_addons; | ||
| 867 | + } | ||
| 868 | + | ||
| 864 | 869 | inline bool Environment::should_not_register_esm_loader() const { | |
| 865 | 870 | return flags_ & EnvironmentFlags::kNoRegisterESMLoader; | |
| 866 | 871 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1197,6 +1197,7 @@ class Environment : public MemoryRetainer { | |||
| 1197 | 1197 | inline void set_has_serialized_options(bool has_serialized_options); | |
| 1198 | 1198 | ||
| 1199 | 1199 | inline bool is_main_thread() const; | |
| 1200 | + inline bool no_native_addons() const; | ||
| 1200 | 1201 | inline bool should_not_register_esm_loader() const; | |
| 1201 | 1202 | inline bool owns_process_state() const; | |
| 1202 | 1203 | inline bool owns_inspector() const; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -406,7 +406,13 @@ enum Flags : uint64_t { | |||
| 406 | 406 | // Set this flag to force hiding console windows when spawning child | |
| 407 | 407 | // processes. This is usually used when embedding Node.js in GUI programs on | |
| 408 | 408 | // Windows. | |
| 409 | - kHideConsoleWindows = 1 << 5 | ||
| 409 | + kHideConsoleWindows = 1 << 5, | ||
| 410 | + // Set this flag to disable loading native addons via `process.dlopen`. | ||
| 411 | + // This environment flag is especially important for worker threads | ||
| 412 | + // so that a worker thread can't load a native addon even if `execArgv` | ||
| 413 | + // is overwritten and `--no-addons` is not specified but was specified | ||
| 414 | + // for this Environment instance. | ||
| 415 | + kNoNativeAddons = 1 << 6 | ||
| 410 | 416 | }; | |
| 411 | 417 | } // namespace EnvironmentFlags | |
| 412 | 418 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -415,6 +415,12 @@ inline napi_addon_register_func GetNapiInitializerCallback(DLib* dlib) { | |||
| 415 | 415 | // cache that's a plain C list or hash table that's shared across contexts? | |
| 416 | 416 | void DLOpen(const FunctionCallbackInfo<Value>& args) { | |
| 417 | 417 | Environment* env = Environment::GetCurrent(args); | |
| 418 | + | ||
| 419 | + if (env->no_native_addons()) { | ||
| 420 | + return THROW_ERR_DLOPEN_DISABLED( | ||
| 421 | + env, "Cannot load native addon because loading addons is disabled."); | ||
| 422 | + } | ||
| 423 | + | ||
| 418 | 424 | auto context = env->context(); | |
| 419 | 425 | ||
| 420 | 426 | CHECK_NULL(thread_local_modpending); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments