GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — module: centralize builtin exposure policies by sjungwon03 · Pull Request #66292 · nodejs/node

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.

Sign up for GitHub

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

Merged
nodejs-github-bot merged 1 commit into nodejs:main from sjungwon03:refactor/module-builtin-policy
Oct 2, 2026

Uh oh!

There was an error while loading. Please reload this page.

Merged

module: centralize builtin exposure policies #66292

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
1 commit
Select commit
59a764c
module: centralize builtin exposure policies
sjungwon03 Sep 24, 2026
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

76 changes: 47 additions & 29 deletions 76 lib/internal/bootstrap/realm.js
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -120,34 +120,6 @@ const legacyWrapperList = new SafeSet([
'util',
]);

// The code below assumes that the two lists must not contain any modules
// beginning with "internal/".
// Modules that can only be imported via the node: scheme.
const schemelessBlockList = new SafeSet([
'bench',
'bench/reporters',
'dtls',
'ffi',
'sea',
'sqlite',
'quic',
'test',
'test/reporters',
'vfs',
]);
// Modules that will only be enabled at run time.
const experimentalModuleList = new SafeSet([
'bench',
'bench/reporters',
'dtls',
'ffi',
'quic',
'sqlite',
'stream/iter',
'vfs',
'zlib/iter',
]);

// Set up process.binding() and process._linkedBinding().
{
const bindingObj = { __proto__: null };
Expand Down Expand Up @@ -220,10 +192,41 @@ const getOwn = (target, property, receiver) => {
undefined;
};

// Public builtin exposure policies. Each entry is [id, schemeOnly, option].
// A null option means that the builtin is not gated by a runtime option.
// Do not include internal modules. Runtime option definitions and code-cache
// categories are owned by their respective native subsystems.
const builtinModulePolicies = [
['bench', true, '--experimental-bench'],
['bench/reporters', true, '--experimental-bench'],
['dtls', true, '--experimental-dtls'],
['ffi', true, '--experimental-ffi'],
['sea', true, null],
['sqlite', true, '--experimental-sqlite'],
['quic', true, '--experimental-quic'],
['stream/iter', false, '--experimental-stream-iter'],
['test', true, null],
['test/reporters', true, null],
['vfs', true, '--experimental-vfs'],
['zlib/iter', false, '--experimental-stream-iter'],
];

const schemelessBlockList = new SafeSet();
const optionGatedBuiltinOptions = new SafeMap();
for (let i = 0; i < builtinModulePolicies.length; i++) {
const { 0: id, 1: schemeOnly, 2: option } = builtinModulePolicies[i];
if (schemeOnly) {
schemelessBlockList.add(id);
}
if (option !== null) {
optionGatedBuiltinOptions.set(id, option);
}
}

const publicBuiltinIds = builtinIds
.filter((id) =>
!StringPrototypeStartsWith(id, 'internal/') &&
!experimentalModuleList.has(id),
!optionGatedBuiltinOptions.has(id),
);
// Do not expose the loaders to user land even with --expose-internals.
const internalBuiltinIds = builtinIds
Expand Down Expand Up @@ -284,6 +287,21 @@ class BuiltinModule {
}
}

// Called after runtime options have been initialized, before user modules.
static allowOptionGatedBuiltins(getOptionValue) {
for (const { 0: id, 1: option } of optionGatedBuiltinOptions) {
if (getOptionValue(option)) {
BuiltinModule.allowRequireByUsers(id);
}
}
}

// Return a copy so internal tests cannot mutate the loader's policy.
static getBuiltinModulePolicies() {
return ArrayPrototypeMap(builtinModulePolicies,
(policy) => ArrayPrototypeSlice(policy));
}

static setRealmAllowRequireByUsers(ids) {
canBeRequiredByUsersList =
new SafeSet(ArrayPrototypeFilter(ids, (id) => ArrayPrototypeIncludes(publicBuiltinIds, id)));
Expand Down
72 changes: 3 additions & 69 deletions 72 lib/internal/process/pre_execution.js
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,7 @@ function prepareExecution(options) {
setupNetworkInspection();
setupNavigator();
setupWarningHandler();
setupBench();
setupFFI();
setupSQLite();
setupStreamIter();
setupDTLS();
setupVfs();
setupQuic();
setupOptionGatedBuiltins();
setupWebStorage();
removeWebWorkersIfDisabled();
setupEventsource();
Expand Down Expand Up @@ -461,32 +455,9 @@ function setupNavigator() {
defineReplaceableLazyAttribute(globalThis, 'internal/navigator', ['navigator'], false);
}

function setupBench() {
if (!getOptionValue('--experimental-bench')) {
return;
}

const { BuiltinModule } = require('internal/bootstrap/realm');
BuiltinModule.allowRequireByUsers('bench');
BuiltinModule.allowRequireByUsers('bench/reporters');
}

function setupFFI() {
if (!getOptionValue('--experimental-ffi')) {
return;
}

const { BuiltinModule } = require('internal/bootstrap/realm');
BuiltinModule.allowRequireByUsers('ffi');
}

function setupSQLite() {
if (getOptionValue('--no-experimental-sqlite')) {
return;
}

function setupOptionGatedBuiltins() {
const { BuiltinModule } = require('internal/bootstrap/realm');
BuiltinModule.allowRequireByUsers('sqlite');
BuiltinModule.allowOptionGatedBuiltins(getOptionValue);
}

function initializeConfigFileSupport() {
Expand All @@ -495,43 +466,6 @@ function initializeConfigFileSupport() {
}
}

function setupStreamIter() {
if (!getOptionValue('--experimental-stream-iter')) {
return;
}

const { BuiltinModule } = require('internal/bootstrap/realm');
BuiltinModule.allowRequireByUsers('stream/iter');
BuiltinModule.allowRequireByUsers('zlib/iter');
}

function setupDTLS() {
if (!getOptionValue('--experimental-dtls')) {
return;
}

const { BuiltinModule } = require('internal/bootstrap/realm');
BuiltinModule.allowRequireByUsers('dtls');
}

function setupQuic() {
if (!getOptionValue('--experimental-quic')) {
return;
}

const { BuiltinModule } = require('internal/bootstrap/realm');
BuiltinModule.allowRequireByUsers('quic');
}

function setupVfs() {
if (!getOptionValue('--experimental-vfs')) {
return;
}

const { BuiltinModule } = require('internal/bootstrap/realm');
BuiltinModule.allowRequireByUsers('vfs');
}

function setupWebStorage() {
if (getEmbedderOptions().noBrowserGlobals ||
!getOptionValue('--experimental-webstorage')) {
Expand Down
8 changes: 7 additions & 1 deletion 8 lib/internal/test/binding.js
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,10 @@ if (module.isPreloading) {
globalThis.primordials = primordials;
}

module.exports = { internalBinding: filteredInternalBinding, primordials };
module.exports = {
internalBinding: filteredInternalBinding,
primordials,
getBuiltinModulePolicies() {
return require('internal/bootstrap/realm').BuiltinModule.getBuiltinModulePolicies();
},
};
171 changes: 171 additions & 0 deletions 171 test/parallel/test-module-builtin-policies.js
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
// 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 policies = JSON.parse(process.argv[3]);
const enabled = process.argv[4] === 'true';
Promise.all(policies.map((policy) => 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 moduleAvailability = new Map([
['dtls', common.hasDtls],
['ffi', common.hasFFI],
['quic', common.hasQuic],
['sqlite', common.hasSQLite],
]);

// Validate every declared policy before testing its behavior.
{
const seenIds = new Set();
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) continue;
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);
}

// Test defaults, command-line flags, NODE_OPTIONS, and their precedence in
// fresh processes.
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);
}
}
}

// Test option-gated builtins in Workers without changing the parent state.
{
const policiesByOption = new Map();
for (const policy of policies) {
const [id, , option] = policy;
if (option === null || moduleAvailability.get(id) === false) continue;

const optionPolicies = policiesByOption.get(option);
if (optionPolicies === undefined) {
policiesByOption.set(option, [policy]);
} else {
optionPolicies.push(policy);
}
}

// Test each option once, together with all builtins it controls.
for (const [option, optionPolicies] of policiesByOption) {
const parentState = optionPolicies.map(([id]) => [
id,
isBuiltin(`node:${id}`),
]);
const disabledOption = option.replace('--', '--no-');

for (const enabled of [true, false]) {
const worker = new Worker(__filename, {
argv: ['child', JSON.stringify(optionPolicies), String(enabled)],
execArgv: [enabled ? option : disabledOption],
env: { ...process.env, NODE_OPTIONS: '' },
});

worker.on('exit', common.mustCall((code) => {
assert.strictEqual(code, 0);
for (const [id, parentEnabled] of parentState) {
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',
});
}
}
}
Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.