mirror of
https://github.com/nodejs/node.git
synced 2026-11-25 23:01:06 -05:00
esm: use "node:" namespace for builtins
Backport-PR-URL: https://github.com/nodejs/node/pull/35757 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:
1 parent
1f34230373
commit
0f757bc2df
9 files changed
+16
-16
No files matched your search
+1
-1
@@ -1007,7 +1007,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_.
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -750,13 +750,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:')) {
|
||||
|
||||
@@ -261,11 +261,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();
|
||||
|
||||
@@ -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');
|
||||
|
||||
+2
-2
@@ -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'
|
||||
};
|
||||
|
||||
@@ -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'
|
||||
};
|
||||
|
||||
@@ -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`);
|
||||
|
||||
@@ -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({
|
||||
|
||||
Reference in new issue
Block a user