From 91b820e939cc5354522620dcbc9e8682d4b1304c Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Sun, 27 Sep 2020 21:24:44 -0700 Subject: [PATCH] esm: use "node:" namespace for builtins MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/35387 Reviewed-By: Michaƫl Zasso Reviewed-By: Bradley Farias Reviewed-By: Jan Krems Reviewed-By: Matteo Collina --- doc/api/esm.md | 2 +- lib/internal/modules/esm/get_format.js | 2 +- lib/internal/modules/esm/resolve.js | 4 ++-- lib/internal/modules/esm/translators.js | 8 ++++---- test/es-module/test-esm-dynamic-import.js | 4 ++-- .../es-module-loaders/builtin-named-exports-loader.mjs | 6 +++--- test/fixtures/es-module-loaders/example-loader.mjs | 4 ++-- .../es-module-loaders/loader-unknown-builtin-module.mjs | 4 ++-- .../es-module-loaders/not-found-assert-loader.mjs | 2 +- test/parallel/test-loaders-unknown-builtin-module.mjs | 2 +- 10 files changed, 19 insertions(+), 19 deletions(-) diff --git a/doc/api/esm.md b/doc/api/esm.md index a27a20bb1fcf73..db77d246287ce0 100644 --- a/doc/api/esm.md +++ b/doc/api/esm.md @@ -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_. diff --git a/lib/internal/modules/esm/get_format.js b/lib/internal/modules/esm/get_format.js index 616b2cf52309ea..16e2ad5e2d5c3e 100644 --- a/lib/internal/modules/esm/get_format.js +++ b/lib/internal/modules/esm/get_format.js @@ -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); diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js index 12a9b14a577ce9..21459cf3d1e753 100644 --- a/lib/internal/modules/esm/resolve.js +++ b/lib/internal/modules/esm/resolve.js @@ -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:')) { diff --git a/lib/internal/modules/esm/translators.js b/lib/internal/modules/esm/translators.js index 5e4de5d5af0f39..efbb5d92d7e26d 100644 --- a/lib/internal/modules/esm/translators.js +++ b/lib/internal/modules/esm/translators.js @@ -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(); diff --git a/test/es-module/test-esm-dynamic-import.js b/test/es-module/test-esm-dynamic-import.js index 0cb56dff2c04c5..6f8757da1b914e 100644 --- a/test/es-module/test-esm-dynamic-import.js +++ b/test/es-module/test-esm-dynamic-import.js @@ -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'); diff --git a/test/fixtures/es-module-loaders/builtin-named-exports-loader.mjs b/test/fixtures/es-module-loaders/builtin-named-exports-loader.mjs index f476c676cdea5b..f06e9e90ff97c3 100644 --- a/test/fixtures/es-module-loaders/builtin-named-exports-loader.mjs +++ b/test/fixtures/es-module-loaders/builtin-named-exports-loader.mjs @@ -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); diff --git a/test/fixtures/es-module-loaders/example-loader.mjs b/test/fixtures/es-module-loaders/example-loader.mjs index 1ed18bda51070d..be4808738035f9 100644 --- a/test/fixtures/es-module-loaders/example-loader.mjs +++ b/test/fixtures/es-module-loaders/example-loader.mjs @@ -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' }; diff --git a/test/fixtures/es-module-loaders/loader-unknown-builtin-module.mjs b/test/fixtures/es-module-loaders/loader-unknown-builtin-module.mjs index e976343e47e9bc..4ef089fd6eb3fd 100644 --- a/test/fixtures/es-module-loaders/loader-unknown-builtin-module.mjs +++ b/test/fixtures/es-module-loaders/loader-unknown-builtin-module.mjs @@ -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' }; diff --git a/test/fixtures/es-module-loaders/not-found-assert-loader.mjs b/test/fixtures/es-module-loaders/not-found-assert-loader.mjs index 2130bad5f52698..9a2cd735a2fd66 100644 --- a/test/fixtures/es-module-loaders/not-found-assert-loader.mjs +++ b/test/fixtures/es-module-loaders/not-found-assert-loader.mjs @@ -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`); diff --git a/test/parallel/test-loaders-unknown-builtin-module.mjs b/test/parallel/test-loaders-unknown-builtin-module.mjs index 464dbeb22a9b31..85181a2b73b54c 100644 --- a/test/parallel/test-loaders-unknown-builtin-module.mjs +++ b/test/parallel/test-loaders-unknown-builtin-module.mjs @@ -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({