Skip to content
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

esm: use "node:" internal namespace for builtins #35387

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion doc/api/esm.md
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,7 @@ The resolver can throw the following errors:
> 1. If _selfUrl_ is not **undefined**, return _selfUrl_.
> 1. If _packageSubpath_ is _"."_ and _packageName_ is a Node.js builtin
> module, then
> 1. Return the string _"nodejs:"_ concatenated with _packageSpecifier_.
> 1. Return the string _"node:"_ concatenated with _packageSpecifier_.
> 1. While _parentURL_ is not the file system root,
> 1. Let _packageURL_ be the URL resolution of _"node_modules/"_
> concatenated with _packageSpecifier_, relative to _parentURL_.
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/modules/esm/get_format.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ if (experimentalJsonModules)
extensionFormatMap['.json'] = legacyExtensionFormatMap['.json'] = 'json';

function defaultGetFormat(url, context, defaultGetFormatUnused) {
if (StringPrototypeStartsWith(url, 'nodejs:')) {
if (StringPrototypeStartsWith(url, 'node:')) {
return { format: 'builtin' };
}
const parsed = new URL(url);
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/modules/esm/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -776,13 +776,13 @@ function defaultResolve(specifier, context = {}, defaultResolveUnused) {
};
}
} catch {}
if (parsed && parsed.protocol === 'nodejs:')
if (parsed && parsed.protocol === 'node:')
return { url: specifier };
if (parsed && parsed.protocol !== 'file:' && parsed.protocol !== 'data:')
throw new ERR_UNSUPPORTED_ESM_URL_SCHEME(parsed);
if (NativeModule.canBeRequiredByUsers(specifier)) {
return {
url: 'nodejs:' + specifier
url: 'node:' + specifier
};
}
if (parentURL && StringPrototypeStartsWith(parentURL, 'data:')) {
Expand Down
8 changes: 4 additions & 4 deletions lib/internal/modules/esm/translators.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,11 @@ translators.set('commonjs', function commonjsStrategy(url, isMain) {
// through normal resolution
translators.set('builtin', async function builtinStrategy(url) {
debug(`Translating BuiltinModule ${url}`);
// Slice 'nodejs:' scheme
const id = url.slice(7);
// Slice 'node:' scheme
const id = url.slice(5);
guybedford marked this conversation as resolved.
Show resolved Hide resolved
const module = loadNativeModule(id, url, true);
if (!url.startsWith('nodejs:') || !module) {
throw new ERR_UNKNOWN_BUILTIN_MODULE(id);
if (!url.startsWith('node:') || !module) {
guybedford marked this conversation as resolved.
Show resolved Hide resolved
throw new ERR_UNKNOWN_BUILTIN_MODULE(url);
}
debug(`Loading BuiltinModule ${url}`);
return module.getESMFacade();
Expand Down
4 changes: 2 additions & 2 deletions test/es-module/test-esm-dynamic-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ function expectFsNamespace(result) {
expectFsNamespace(import('fs'));
expectFsNamespace(eval('import("fs")'));
expectFsNamespace(eval('import("fs")'));
expectFsNamespace(import('nodejs:fs'));
expectFsNamespace(import('node:fs'));

expectModuleError(import('nodejs:unknown'),
expectModuleError(import('node:unknown'),
'ERR_UNKNOWN_BUILTIN_MODULE');
expectModuleError(import('./not-an-existing-module.mjs'),
'ERR_MODULE_NOT_FOUND');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function getGlobalPreloadCode() {

export function resolve(specifier, context, defaultResolve) {
const def = defaultResolve(specifier, context);
if (def.url.startsWith('nodejs:')) {
if (def.url.startsWith('node:')) {
return {
url: `custom-${def.url}`,
};
Expand All @@ -24,7 +24,7 @@ export function resolve(specifier, context, defaultResolve) {
}

export function getSource(url, context, defaultGetSource) {
if (url.startsWith('custom-nodejs:')) {
if (url.startsWith('custom-node:')) {
const urlObj = new URL(url);
return {
source: generateBuiltinModule(urlObj.pathname),
Expand All @@ -35,7 +35,7 @@ export function getSource(url, context, defaultGetSource) {
}

export function getFormat(url, context, defaultGetFormat) {
if (url.startsWith('custom-nodejs:')) {
if (url.startsWith('custom-node:')) {
return { format: 'module' };
}
return defaultGetFormat(url, context, defaultGetFormat);
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/es-module-loaders/example-loader.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ baseURL.pathname = process.cwd() + '/';
export function resolve(specifier, { parentURL = baseURL }, defaultResolve) {
if (builtinModules.includes(specifier)) {
return {
url: 'nodejs:' + specifier
url: 'node:' + specifier
};
}
if (/^\.{1,2}[/]/.test(specifier) !== true && !specifier.startsWith('file:')) {
Expand All @@ -27,7 +27,7 @@ export function resolve(specifier, { parentURL = baseURL }, defaultResolve) {
}

export function getFormat(url, context, defaultGetFormat) {
if (url.startsWith('nodejs:') && builtinModules.includes(url.slice(7))) {
if (url.startsWith('node:') && builtinModules.includes(url.slice(5))) {
return {
format: 'builtin'
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
export async function resolve(specifier, { parentURL }, defaultResolve) {
if (specifier === 'unknown-builtin-module') {
return {
url: 'nodejs:unknown-builtin-module'
url: 'node:unknown-builtin-module'
};
}
return defaultResolve(specifier, {parentURL}, defaultResolve);
}

export async function getFormat(url, context, defaultGetFormat) {
if (url === 'nodejs:unknown-builtin-module') {
if (url === 'node:unknown-builtin-module') {
return {
format: 'builtin'
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export async function resolve(specifier, { parentURL }, defaultResolve) {
catch (e) {
assert.strictEqual(e.code, 'ERR_MODULE_NOT_FOUND');
return {
url: 'nodejs:fs'
url: 'node:fs'
};
}
assert.fail(`Module resolution for ${specifier} should be throw ERR_MODULE_NOT_FOUND`);
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-loaders-unknown-builtin-module.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { expectsError, mustCall } from '../common/index.mjs';
import assert from 'assert';

const unknownBuiltinModule = 'unknown-builtin-module';
const unknownBuiltinModule = 'node:unknown-builtin-module';

import(unknownBuiltinModule)
.then(assert.fail, expectsError({
Expand Down