Skip to content

Commit

Permalink
esm: use "node:" namespace for builtins
Browse files Browse the repository at this point in the history
PR-URL: #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>
  • Loading branch information
guybedford authored and danielleadams committed Oct 6, 2020
1 parent af360ac commit 91b820e
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 19 deletions.
2 changes: 1 addition & 1 deletion doc/api/esm.md
Expand Up @@ -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_.
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/modules/esm/get_format.js
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
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
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions test/es-module/test-esm-dynamic-import.js
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
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
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
@@ -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
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
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

0 comments on commit 91b820e

Please sign in to comment.