|
| 1 | +// Flags: --expose-internals |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +// Run before loading common, whose async-hooks checks need --expose-internals. |
| 5 | +// Child processes and Workers exercise only the public loaders. |
| 6 | +if (process.argv[2] === 'child') { |
| 7 | + const policies = JSON.parse(process.argv[3]); |
| 8 | + const enabled = process.argv[4] === 'true'; |
| 9 | + Promise.all(policies.map((policy) => checkPolicy(policy, enabled))) |
| 10 | + .catch((err) => { |
| 11 | + console.error(err); |
| 12 | + process.exitCode = 1; |
| 13 | + }); |
| 14 | + return; |
| 15 | +} |
| 16 | + |
| 17 | +const common = require('../common'); |
| 18 | +const assert = require('assert'); |
| 19 | +const { isBuiltin } = require('module'); |
| 20 | +const { Worker } = require('worker_threads'); |
| 21 | +const { spawnSyncAndAssert } = require('../common/child_process'); |
| 22 | +const { |
| 23 | + internalBinding, |
| 24 | + getBuiltinModulePolicies, |
| 25 | +} = require('internal/test/binding'); |
| 26 | +const { getCLIOptionsInfo } = require('internal/options'); |
| 27 | + |
| 28 | +const policies = getBuiltinModulePolicies(); |
| 29 | +const { builtinIds } = internalBinding('builtins'); |
| 30 | +const { options } = getCLIOptionsInfo(); |
| 31 | +const moduleAvailability = new Map([ |
| 32 | + ['dtls', common.hasDtls], |
| 33 | + ['ffi', common.hasFFI], |
| 34 | + ['quic', common.hasQuic], |
| 35 | + ['sqlite', common.hasSQLite], |
| 36 | +]); |
| 37 | + |
| 38 | +// Validate every declared policy before testing its behavior. |
| 39 | +{ |
| 40 | + const seenIds = new Set(); |
| 41 | + for (const policy of policies) { |
| 42 | + assert(Array.isArray(policy)); |
| 43 | + assert.strictEqual(policy.length, 3); |
| 44 | + const [id, schemeOnly, option] = policy; |
| 45 | + assert.strictEqual(typeof id, 'string'); |
| 46 | + assert(!id.startsWith('internal/'), id); |
| 47 | + assert(builtinIds.includes(id), `Unknown builtin: ${id}`); |
| 48 | + assert(!seenIds.has(id), `Duplicate policy: ${id}`); |
| 49 | + seenIds.add(id); |
| 50 | + assert.strictEqual(typeof schemeOnly, 'boolean', id); |
| 51 | + |
| 52 | + if (option === null) continue; |
| 53 | + assert.match(option, /^--experimental-[a-z-]+$/); |
| 54 | + |
| 55 | + // FFI has no CLI option in builds without FFI support. |
| 56 | + const missingFFIOption = id === 'ffi' && !common.hasFFI && |
| 57 | + option === '--experimental-ffi'; |
| 58 | + assert(options.has(option) || missingFFIOption, |
| 59 | + `Unknown builtin option: ${option}`); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// Callers must not be able to change the loader's policy through this API. |
| 64 | +{ |
| 65 | + const copy = getBuiltinModulePolicies(); |
| 66 | + copy[0][0] = 'changed'; |
| 67 | + copy.pop(); |
| 68 | + assert.deepStrictEqual(getBuiltinModulePolicies(), policies); |
| 69 | +} |
| 70 | + |
| 71 | +// Test defaults, command-line flags, NODE_OPTIONS, and their precedence in |
| 72 | +// fresh processes. |
| 73 | +for (const policy of policies) { |
| 74 | + const [id, , option] = policy; |
| 75 | + if (moduleAvailability.get(id) === false) continue; |
| 76 | + |
| 77 | + runPolicyTest(policy, [], option === null || options.get(option).defaultIsTrue); |
| 78 | + if (option !== null) { |
| 79 | + const disabledOption = option.replace('--', '--no-'); |
| 80 | + runPolicyTest(policy, [option], true); |
| 81 | + runPolicyTest(policy, [disabledOption], false); |
| 82 | + |
| 83 | + if (!process.config.variables.node_without_node_options) { |
| 84 | + runPolicyTest(policy, [], true, option); |
| 85 | + runPolicyTest(policy, [], false, disabledOption); |
| 86 | + // Command-line options take precedence over NODE_OPTIONS. |
| 87 | + runPolicyTest(policy, [option], true, disabledOption); |
| 88 | + runPolicyTest(policy, [disabledOption], false, option); |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +// Test option-gated builtins in Workers without changing the parent state. |
| 94 | +{ |
| 95 | + const policiesByOption = new Map(); |
| 96 | + for (const policy of policies) { |
| 97 | + const [id, , option] = policy; |
| 98 | + if (option === null || moduleAvailability.get(id) === false) continue; |
| 99 | + |
| 100 | + const optionPolicies = policiesByOption.get(option); |
| 101 | + if (optionPolicies === undefined) { |
| 102 | + policiesByOption.set(option, [policy]); |
| 103 | + } else { |
| 104 | + optionPolicies.push(policy); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + // Test each option once, together with all builtins it controls. |
| 109 | + for (const [option, optionPolicies] of policiesByOption) { |
| 110 | + const parentState = optionPolicies.map(([id]) => [ |
| 111 | + id, |
| 112 | + isBuiltin(`node:${id}`), |
| 113 | + ]); |
| 114 | + const disabledOption = option.replace('--', '--no-'); |
| 115 | + |
| 116 | + for (const enabled of [true, false]) { |
| 117 | + const worker = new Worker(__filename, { |
| 118 | + argv: ['child', JSON.stringify(optionPolicies), String(enabled)], |
| 119 | + execArgv: [enabled ? option : disabledOption], |
| 120 | + env: { ...process.env, NODE_OPTIONS: '' }, |
| 121 | + }); |
| 122 | + |
| 123 | + worker.on('exit', common.mustCall((code) => { |
| 124 | + assert.strictEqual(code, 0); |
| 125 | + for (const [id, parentEnabled] of parentState) { |
| 126 | + assert.strictEqual(isBuiltin(`node:${id}`), parentEnabled, id); |
| 127 | + } |
| 128 | + })); |
| 129 | + } |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +// Run in a fresh process so each case initializes the loader with its flags. |
| 134 | +function runPolicyTest(policy, flags, enabled, nodeOptions = '') { |
| 135 | + spawnSyncAndAssert(process.execPath, [ |
| 136 | + ...flags, |
| 137 | + __filename, |
| 138 | + 'child', |
| 139 | + JSON.stringify([policy]), |
| 140 | + String(enabled), |
| 141 | + ], { env: { ...process.env, NODE_OPTIONS: nodeOptions } }, { status: 0 }); |
| 142 | +} |
| 143 | + |
| 144 | +async function checkPolicy([id, schemeOnly], enabled) { |
| 145 | + const assert = require('assert'); |
| 146 | + const { builtinModules, isBuiltin } = require('module'); |
| 147 | + const prefixed = `node:${id}`; |
| 148 | + assert.strictEqual(builtinModules.includes(id), enabled && !schemeOnly, id); |
| 149 | + assert.strictEqual(builtinModules.includes(prefixed), |
| 150 | + enabled && schemeOnly, prefixed); |
| 151 | + |
| 152 | + for (const specifier of [id, prefixed]) { |
| 153 | + const supported = enabled && (!schemeOnly || specifier === prefixed); |
| 154 | + assert.strictEqual(isBuiltin(specifier), supported, specifier); |
| 155 | + if (supported) { |
| 156 | + const exports = require(specifier); |
| 157 | + assert.strictEqual(process.getBuiltinModule(specifier), exports); |
| 158 | + assert.strictEqual((await import(specifier)).default, exports); |
| 159 | + } else { |
| 160 | + assert.strictEqual(process.getBuiltinModule(specifier), undefined); |
| 161 | + assert.throws(() => require(specifier), { |
| 162 | + code: specifier === prefixed ? |
| 163 | + 'ERR_UNKNOWN_BUILTIN_MODULE' : 'MODULE_NOT_FOUND', |
| 164 | + }); |
| 165 | + await assert.rejects(import(specifier), { |
| 166 | + code: specifier === prefixed ? |
| 167 | + 'ERR_UNKNOWN_BUILTIN_MODULE' : 'ERR_MODULE_NOT_FOUND', |
| 168 | + }); |
| 169 | + } |
| 170 | + } |
| 171 | +} |
0 commit comments