esm: use "node:" namespace for builtins

PR-URL: https://github.com/nodejs/node/pull/35387
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Bradley Farias <bradley.meck@gmail.com>
Reviewed-By: Jan Krems <jan.krems@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
Guy Bedford
2020-09-27 21:24:44 -07:00
parent 606df7c4e7
commit ee9e3e75aa
10 changed files with 19 additions and 19 deletions

View File

@@ -1003,7 +1003,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_.

View File

@@ -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);

View File

@@ -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:')) {

View File

@@ -223,11 +223,11 @@ function cjsPreparseModuleExports(filename) {
// 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);
const module = loadNativeModule(id, url, true);
if (!url.startsWith('nodejs:') || !module) {
throw new ERR_UNKNOWN_BUILTIN_MODULE(id);
if (!url.startsWith('node:') || !module) {
throw new ERR_UNKNOWN_BUILTIN_MODULE(url);
}
debug(`Loading BuiltinModule ${url}`);
return module.getESMFacade();

View File

@@ -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');

View File

@@ -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}`,
};
@@ -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),
@@ -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);

View File

@@ -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:')) {
@@ -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'
};

View File

@@ -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'
};

View File

@@ -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`);

View File

@@ -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({