Skip to content

Commit

Permalink
esm: use "node:" namespace for builtins
Browse files Browse the repository at this point in the history
PR-URL: nodejs#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 aduh95 committed Oct 27, 2020
1 parent 18c9216 commit 3e18962
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion doc/api/esm.md
Original file line number Diff line number Diff line change
Expand Up @@ -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_.
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 @@ -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:')) {
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 @@ -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();
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
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

0 comments on commit 3e18962

Please sign in to comment.