-
-
Notifications
You must be signed in to change notification settings - Fork 38k
module: centralize builtin exposure policies #66292
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sjungwon03
wants to merge
1
commit into
nodejs:main
Choose a base branch
from
sjungwon03:refactor/module-builtin-policy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+200
−99
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| // Flags: --expose-internals | ||
| 'use strict'; | ||
|
|
||
| // Run before loading common, whose async-hooks checks need --expose-internals. | ||
| // Child processes and Workers exercise only the public loaders. | ||
| if (process.argv[2] === 'child') { | ||
| const policy = JSON.parse(process.argv[3]); | ||
| const enabled = process.argv[4] === 'true'; | ||
| checkPolicy(policy, enabled).catch((err) => { | ||
| console.error(err); | ||
| process.exitCode = 1; | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| const common = require('../common'); | ||
| const assert = require('assert'); | ||
| const { isBuiltin } = require('module'); | ||
| const { Worker } = require('worker_threads'); | ||
| const { spawnSyncAndAssert } = require('../common/child_process'); | ||
| const { | ||
| internalBinding, | ||
| getBuiltinModulePolicies, | ||
| } = require('internal/test/binding'); | ||
| const { getCLIOptionsInfo } = require('internal/options'); | ||
|
|
||
| const policies = getBuiltinModulePolicies(); | ||
| const { builtinIds } = internalBinding('builtins'); | ||
| const { options } = getCLIOptionsInfo(); | ||
| const seenIds = new Set(); | ||
| const moduleAvailability = new Map([ | ||
| ['dtls', common.hasDtls], | ||
| ['ffi', common.hasFFI], | ||
| ['quic', common.hasQuic], | ||
| ['sqlite', common.hasSQLite], | ||
| ]); | ||
|
|
||
| for (const policy of policies) { | ||
| assert(Array.isArray(policy)); | ||
| assert.strictEqual(policy.length, 3); | ||
| const [id, schemeOnly, option] = policy; | ||
| assert.strictEqual(typeof id, 'string'); | ||
| assert(!id.startsWith('internal/'), id); | ||
| assert(builtinIds.includes(id), `Unknown builtin: ${id}`); | ||
| assert(!seenIds.has(id), `Duplicate policy: ${id}`); | ||
| seenIds.add(id); | ||
| assert.strictEqual(typeof schemeOnly, 'boolean', id); | ||
|
|
||
| if (option !== null) { | ||
| assert.match(option, /^--experimental-[a-z-]+$/); | ||
| // FFI has no CLI option in builds without FFI support. | ||
| const missingFFIOption = id === 'ffi' && !common.hasFFI && | ||
| option === '--experimental-ffi'; | ||
| assert(options.has(option) || missingFFIOption, | ||
| `Unknown builtin option: ${option}`); | ||
| } | ||
| } | ||
|
|
||
| // Callers must not be able to change the loader's policy through this API. | ||
| const copy = getBuiltinModulePolicies(); | ||
| copy[0][0] = 'changed'; | ||
| copy.pop(); | ||
| assert.deepStrictEqual(getBuiltinModulePolicies(), policies); | ||
|
|
||
| // Cover shared flags, both scheme rules, and an option enabled by default. | ||
| const workerPolicyIds = new Set([ | ||
| 'bench', 'bench/reporters', 'stream/iter', 'zlib/iter', 'sqlite', | ||
| ]); | ||
|
|
||
| for (const policy of policies) { | ||
| const [id, , option] = policy; | ||
| if (moduleAvailability.get(id) === false) continue; | ||
|
|
||
| runPolicyTest(policy, [], option === null || options.get(option).defaultIsTrue); | ||
| if (option !== null) { | ||
| const disabledOption = option.replace('--', '--no-'); | ||
| runPolicyTest(policy, [option], true); | ||
| runPolicyTest(policy, [disabledOption], false); | ||
|
|
||
| if (!process.config.variables.node_without_node_options) { | ||
| runPolicyTest(policy, [], true, option); | ||
| runPolicyTest(policy, [], false, disabledOption); | ||
| // Command-line options take precedence over NODE_OPTIONS. | ||
| runPolicyTest(policy, [option], true, disabledOption); | ||
| runPolicyTest(policy, [disabledOption], false, option); | ||
| } | ||
|
|
||
| if (workerPolicyIds.has(id)) { | ||
| const parentEnabled = isBuiltin(`node:${id}`); | ||
| for (const enabled of [true, false]) { | ||
| const worker = new Worker(__filename, { | ||
| argv: ['child', JSON.stringify(policy), String(enabled)], | ||
| execArgv: [enabled ? option : disabledOption], | ||
| env: { ...process.env, NODE_OPTIONS: '' }, | ||
| }); | ||
| worker.on('exit', common.mustCall((code) => { | ||
| assert.strictEqual(code, 0); | ||
| assert.strictEqual(isBuiltin(`node:${id}`), parentEnabled, id); | ||
| })); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Run in a fresh process so each case initializes the loader with its flags. | ||
| function runPolicyTest(policy, flags, enabled, nodeOptions = '') { | ||
| spawnSyncAndAssert(process.execPath, [ | ||
| ...flags, | ||
| __filename, | ||
| 'child', | ||
| JSON.stringify(policy), | ||
| String(enabled), | ||
| ], { env: { ...process.env, NODE_OPTIONS: nodeOptions } }, { status: 0 }); | ||
| } | ||
|
|
||
| async function checkPolicy([id, schemeOnly], enabled) { | ||
| const assert = require('assert'); | ||
| const { builtinModules, isBuiltin } = require('module'); | ||
| const prefixed = `node:${id}`; | ||
| assert.strictEqual(builtinModules.includes(id), enabled && !schemeOnly, id); | ||
| assert.strictEqual(builtinModules.includes(prefixed), | ||
| enabled && schemeOnly, prefixed); | ||
|
|
||
| for (const specifier of [id, prefixed]) { | ||
| const supported = enabled && (!schemeOnly || specifier === prefixed); | ||
| assert.strictEqual(isBuiltin(specifier), supported, specifier); | ||
| if (supported) { | ||
| const exports = require(specifier); | ||
| assert.strictEqual(process.getBuiltinModule(specifier), exports); | ||
| assert.strictEqual((await import(specifier)).default, exports); | ||
| } else { | ||
| assert.strictEqual(process.getBuiltinModule(specifier), undefined); | ||
| assert.throws(() => require(specifier), { | ||
| code: specifier === prefixed ? | ||
| 'ERR_UNKNOWN_BUILTIN_MODULE' : 'MODULE_NOT_FOUND', | ||
| }); | ||
| await assert.rejects(import(specifier), { | ||
| code: specifier === prefixed ? | ||
| 'ERR_UNKNOWN_BUILTIN_MODULE' : 'ERR_MODULE_NOT_FOUND', | ||
| }); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This list could become stale as policies change. For example, if these modules no longer need experimental flags while other modules still do, the test could pass without the coverage described above.