Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion lib/internal/bootstrap/realm.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ const schemelessBlockList = new SafeSet([
'quic',
'test',
'test/reporters',
'vfs',
]);
// Modules that will only be enabled at run time.
const experimentalModuleList = new SafeSet([
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-module-builtin-experimental.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const experimentalBuiltins = [
['bench/reporters', '--experimental-bench', true],
['dtls', '--experimental-dtls', common.hasDtls],
['quic', '--experimental-quic', common.hasQuic],
['vfs', '--experimental-vfs', true],
].filter(([, , available]) => available);

for (const [id, flag] of experimentalBuiltins) {
Expand All @@ -25,6 +24,7 @@ for (const [id, flag] of experimentalBuiltins) {
}

const schemelessExperimentalBuiltins = [
['vfs', '--experimental-vfs'],
['stream/iter', '--experimental-stream-iter'],
['zlib/iter', '--experimental-stream-iter'],
];
Expand Down
70 changes: 44 additions & 26 deletions test/parallel/test-vfs-flag.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,45 @@
'use strict';

// node:vfs is gated behind --experimental-vfs. Without the flag the
// module is not exposed; bare `vfs` (without the node: scheme) is also
// blocked.
// Both vfs and node:vfs are gated behind --experimental-vfs.

require('../common');
const { spawnSyncAndAssert } = require('../common/child_process');

// Without the flag, requiring node:vfs throws ERR_UNKNOWN_BUILTIN_MODULE.
{
spawnSyncAndAssert(process.execPath, [
'-e', 'require("node:vfs")',
], { status: 1, stderr: /ERR_UNKNOWN_BUILTIN_MODULE/ });
}
// Without the flag, or when explicitly disabled, neither specifier is exposed.
for (const flags of [[], ['--no-experimental-vfs']]) {
for (const [id, requireError, importError] of [
['vfs', 'MODULE_NOT_FOUND', 'ERR_MODULE_NOT_FOUND'],
['node:vfs', 'ERR_UNKNOWN_BUILTIN_MODULE', 'ERR_UNKNOWN_BUILTIN_MODULE'],
]) {
spawnSyncAndAssert(process.execPath, [
...flags,
'-e',
`const assert = require('node:assert');
const { isBuiltin } = require('node:module');
assert.strictEqual(isBuiltin('${id}'), false);
assert.strictEqual(process.getBuiltinModule('${id}'), undefined);
assert.throws(() => require('${id}'), { code: '${requireError}' });`,
], { status: 0 });

// Without the flag, importing node:vfs throws ERR_UNKNOWN_BUILTIN_MODULE.
{
spawnSyncAndAssert(process.execPath, [
'--input-type=module',
'-e', 'import("node:vfs").catch((e) => { console.error(e.code); process.exit(1); });',
], {
status: 1,
stderr: /ERR_UNKNOWN_BUILTIN_MODULE/,
});
spawnSyncAndAssert(process.execPath, [
...flags,
'--input-type=module',
'-e',
`import assert from 'node:assert';
await assert.rejects(import('${id}'), { code: '${importError}' });`,
], { status: 0 });
}
}

// With the flag, node:vfs loads and works.
{
// With the flag, both specifiers resolve to the same CommonJS exports.
for (const id of ['vfs', 'node:vfs']) {
const script =
'const v = require("node:vfs");' +
'const assert = require("node:assert");' +
`const v = require('${id}');` +
'assert.strictEqual(v, require("node:vfs"));' +
`assert.strictEqual(require.resolve('${id}'), '${id}');` +
`assert.strictEqual(require('node:module').isBuiltin('${id}'), true);` +
`assert.strictEqual(process.getBuiltinModule('${id}'), v);` +
'const x = v.create();' +
'x.writeFileSync("/x", "hi");' +
'console.log(x.readFileSync("/x", "utf8"));';
Expand All @@ -38,22 +49,29 @@ const { spawnSyncAndAssert } = require('../common/child_process');
});
}

// Bare `vfs` (no node: scheme) is always blocked.
// Static and dynamic ESM imports share the same module and named exports.
{
spawnSyncAndAssert(process.execPath, [
'--experimental-vfs',
'-e', "require('vfs')",
], { status: 1, stderr: /Cannot find module 'vfs'/ });
'--input-type=module',
'-e',
`import assert from 'node:assert';
import vfs, { create } from 'vfs';
import nodeVfs, { create as nodeCreate } from 'node:vfs';
assert.strictEqual(vfs, nodeVfs);
assert.strictEqual(create, nodeCreate);
assert.strictEqual(await import('vfs'), await import('node:vfs'));`,
], { status: 0 });
}

// Module.builtinModules reflects whether --experimental-vfs is active.
// Module.builtinModules lists the bare name when --experimental-vfs is active.
for (const [flag, expected] of [
['--experimental-vfs', 'true\n'],
['--no-experimental-vfs', 'false\n'],
]) {
spawnSyncAndAssert(process.execPath, [
flag,
'-p',
'require("node:module").builtinModules.includes("node:vfs")',
'require("node:module").builtinModules.includes("vfs")',
], { stdout: expected, stderr: '' });
}
Loading