From b884fab5952139da653a148b95fe5136b7432d1f Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Fri, 20 May 2022 17:52:16 -0700 Subject: [PATCH 1/8] esm: refactor responseURL handling --- lib/internal/modules/cjs/loader.js | 6 +- lib/internal/modules/esm/fetch_module.js | 11 --- lib/internal/modules/esm/get_source.js | 60 ------------ .../modules/esm/initialize_import_meta.js | 4 +- lib/internal/modules/esm/load.js | 64 +++++++++++- lib/internal/modules/esm/loader.js | 98 +++++++------------ lib/internal/modules/esm/module_job.js | 7 +- lib/internal/modules/esm/translators.js | 8 +- test/parallel/test-bootstrap-modules.js | 1 - 9 files changed, 104 insertions(+), 155 deletions(-) delete mode 100644 lib/internal/modules/esm/get_source.js diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index de919e7406b4e7..d4ca8c4dde4412 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -1023,8 +1023,7 @@ function wrapSafe(filename, content, cjsModuleInstance) { displayErrors: true, importModuleDynamically: async (specifier, _, importAssertions) => { const loader = asyncESM.esmLoader; - return loader.import(specifier, - loader.getBaseURL(normalizeReferrerURL(filename)), + return loader.import(specifier, normalizeReferrerURL(filename), importAssertions); }, }); @@ -1040,8 +1039,7 @@ function wrapSafe(filename, content, cjsModuleInstance) { filename, importModuleDynamically(specifier, _, importAssertions) { const loader = asyncESM.esmLoader; - return loader.import(specifier, - loader.getBaseURL(normalizeReferrerURL(filename)), + return loader.import(specifier, normalizeReferrerURL(filename), importAssertions); }, }); diff --git a/lib/internal/modules/esm/fetch_module.js b/lib/internal/modules/esm/fetch_module.js index eeca8fc5e8edcc..c65587e34c488f 100644 --- a/lib/internal/modules/esm/fetch_module.js +++ b/lib/internal/modules/esm/fetch_module.js @@ -238,17 +238,6 @@ function fetchModule(parsed, { parentURL }) { return fetchWithRedirects(parsed); } -/** - * Checks if the given canonical URL exists in the fetch cache - * - * @param {string} key - * @returns {boolean} - */ -function inFetchCache(key) { - return cacheForGET.has(key); -} - module.exports = { fetchModule, - inFetchCache, }; diff --git a/lib/internal/modules/esm/get_source.js b/lib/internal/modules/esm/get_source.js deleted file mode 100644 index ab2a9888f76fe7..00000000000000 --- a/lib/internal/modules/esm/get_source.js +++ /dev/null @@ -1,60 +0,0 @@ -'use strict'; - -const { - ArrayPrototypeConcat, - RegExpPrototypeExec, - decodeURIComponent, -} = primordials; -const { getOptionValue } = require('internal/options'); -const { fetchModule } = require('internal/modules/esm/fetch_module'); - -// Do not eagerly grab .manifest, it may be in TDZ -const policy = getOptionValue('--experimental-policy') ? - require('internal/process/policy') : - null; -const experimentalNetworkImports = - getOptionValue('--experimental-network-imports'); - -const { Buffer: { from: BufferFrom } } = require('buffer'); - -const fs = require('internal/fs/promises').exports; -const { URL } = require('internal/url'); -const { - ERR_INVALID_URL, - ERR_UNSUPPORTED_ESM_URL_SCHEME, -} = require('internal/errors').codes; -const readFileAsync = fs.readFile; - -const DATA_URL_PATTERN = /^[^/]+\/[^,;]+(?:[^,]*?)(;base64)?,([\s\S]*)$/; - -async function defaultGetSource(url, context, defaultGetSource) { - const parsed = new URL(url); - let source; - if (parsed.protocol === 'file:') { - source = await readFileAsync(parsed); - } else if (parsed.protocol === 'data:') { - const match = RegExpPrototypeExec(DATA_URL_PATTERN, parsed.pathname); - if (!match) { - throw new ERR_INVALID_URL(url); - } - const { 1: base64, 2: body } = match; - source = BufferFrom(decodeURIComponent(body), base64 ? 'base64' : 'utf8'); - } else if (experimentalNetworkImports && ( - parsed.protocol === 'https:' || - parsed.protocol === 'http:' - )) { - const res = await fetchModule(parsed, context); - source = await res.body; - } else { - throw new ERR_UNSUPPORTED_ESM_URL_SCHEME(parsed, ArrayPrototypeConcat([ - 'file', - 'data', - experimentalNetworkImports ? ['https', 'http'] : [], - ])); - } - if (policy?.manifest) { - policy.manifest.assertIntegrity(parsed, source); - } - return source; -} -exports.defaultGetSource = defaultGetSource; diff --git a/lib/internal/modules/esm/initialize_import_meta.js b/lib/internal/modules/esm/initialize_import_meta.js index f1daabbb6425aa..d6be06f23e1493 100644 --- a/lib/internal/modules/esm/initialize_import_meta.js +++ b/lib/internal/modules/esm/initialize_import_meta.js @@ -26,15 +26,13 @@ function createImportMetaResolve(defaultParentUrl) { * @param {{url: string}} context */ function initializeImportMeta(meta, context) { - let url = context.url; + const { url } = context; // Alphabetical if (experimentalImportMetaResolve) { meta.resolve = createImportMetaResolve(url); } - url = asyncESM.esmLoader.getBaseURL(url); - meta.url = url; } diff --git a/lib/internal/modules/esm/load.js b/lib/internal/modules/esm/load.js index 6defb598a2abf7..5464f6d7075dbe 100644 --- a/lib/internal/modules/esm/load.js +++ b/lib/internal/modules/esm/load.js @@ -1,8 +1,66 @@ 'use strict'; +const { + ArrayPrototypePush, + RegExpPrototypeExec, + decodeURIComponent, +} = primordials; + const { defaultGetFormat } = require('internal/modules/esm/get_format'); -const { defaultGetSource } = require('internal/modules/esm/get_source'); const { validateAssertions } = require('internal/modules/esm/assert'); +const { getOptionValue } = require('internal/options'); +const { fetchModule } = require('internal/modules/esm/fetch_module'); + +// Do not eagerly grab .manifest, it may be in TDZ +const policy = getOptionValue('--experimental-policy') ? + require('internal/process/policy') : + null; +const experimentalNetworkImports = + getOptionValue('--experimental-network-imports'); + +const { Buffer: { from: BufferFrom } } = require('buffer'); + +const { readFile: readFileAsync } = require('internal/fs/promises').exports; +const { URL } = require('internal/url'); +const { + ERR_INVALID_URL, + ERR_UNSUPPORTED_ESM_URL_SCHEME, +} = require('internal/errors').codes; + +const DATA_URL_PATTERN = /^[^/]+\/[^,;]+(?:[^,]*?)(;base64)?,([\s\S]*)$/; + +async function getSource(url, context) { + const parsed = new URL(url); + let responseURL = url; + let source; + if (parsed.protocol === 'file:') { + source = await readFileAsync(parsed); + } else if (parsed.protocol === 'data:') { + const match = RegExpPrototypeExec(DATA_URL_PATTERN, parsed.pathname); + if (!match) { + throw new ERR_INVALID_URL(url); + } + const { 1: base64, 2: body } = match; + source = BufferFrom(decodeURIComponent(body), base64 ? 'base64' : 'utf8'); + } else if (experimentalNetworkImports && ( + parsed.protocol === 'https:' || + parsed.protocol === 'http:' + )) { + const res = await fetchModule(parsed, context); + source = await res.body; + responseURL = res.resolvedHREF; + } else { + const supportedSchemes = ['file', 'data']; + if (experimentalNetworkImports) + ArrayPrototypePush(supportedSchemes, 'http', 'https'); + throw new ERR_UNSUPPORTED_ESM_URL_SCHEME(parsed, supportedSchemes); + } + if (policy?.manifest) { + policy.manifest.assertIntegrity(parsed, source); + } + return { responseURL, source }; +} + /** * Node.js default load hook. @@ -11,6 +69,7 @@ const { validateAssertions } = require('internal/modules/esm/assert'); * @returns {object} */ async function defaultLoad(url, context) { + let responseURL = url; const { importAssertions } = context; let { format, @@ -29,11 +88,12 @@ async function defaultLoad(url, context) { ) { source = null; } else if (source == null) { - source = await defaultGetSource(url, context); + ({ responseURL, source } = await getSource(url, context)); } return { format, + responseURL, source, }; } diff --git a/lib/internal/modules/esm/loader.js b/lib/internal/modules/esm/loader.js index c867595bccc9e2..d05b5f20548e7d 100644 --- a/lib/internal/modules/esm/loader.js +++ b/lib/internal/modules/esm/loader.js @@ -17,14 +17,12 @@ const { RegExpPrototypeExec, SafeArrayIterator, SafeWeakMap, - StringPrototypeStartsWith, globalThis, } = primordials; const { MessageChannel } = require('internal/worker/io'); const { ERR_LOADER_CHAIN_INCOMPLETE, - ERR_INTERNAL_ASSERTION, ERR_INVALID_ARG_TYPE, ERR_INVALID_ARG_VALUE, ERR_INVALID_RETURN_PROPERTY_VALUE, @@ -55,11 +53,6 @@ const { defaultLoad } = require('internal/modules/esm/load'); const { translators } = require( 'internal/modules/esm/translators'); const { getOptionValue } = require('internal/options'); -const { - fetchModule, - inFetchCache, -} = require('internal/modules/esm/fetch_module'); - /** * @typedef {object} ExportedHooks @@ -306,9 +299,7 @@ class ESMLoader { const module = new ModuleWrap(url, undefined, source, 0, 0); callbackMap.set(module, { importModuleDynamically: (specifier, { url }, importAssertions) => { - return this.import(specifier, - this.getBaseURL(url), - importAssertions); + return this.import(specifier, url, importAssertions); } }); @@ -324,55 +315,6 @@ class ESMLoader { }; } - /** - * Returns the url to use for the resolution of a given cache key url - * These are not guaranteed to be the same. - * - * In WHATWG HTTP spec for ESM the cache key is the non-I/O bound - * synchronous resolution using only string operations - * ~= resolveImportMap(new URL(specifier, importerHREF)) - * - * The url used for subsequent resolution is the response URL after - * all redirects have been resolved. - * - * https://example.com/foo redirecting to https://example.com/bar - * would have a cache key of https://example.com/foo and baseURL - * of https://example.com/bar - * - * ! MUST BE SYNCHRONOUS for import.meta initialization - * ! MUST BE CALLED AFTER receiving the url body due to I/O - * @param {URL['href']} url - * @returns {string|Promise} - */ - getBaseURL(url) { - if (getOptionValue('--experimental-network-imports') && ( - StringPrototypeStartsWith(url, 'http:') || - StringPrototypeStartsWith(url, 'https:') - )) { - // When using network-imports, the request & response have already settled - // so they are in fetchModule's cache, in which case, fetchModule returns - // immediately and synchronously - // Unless a custom loader bypassed the fetch cache, in which case we just - // use the original url - if (inFetchCache(url)) { - const module = fetchModule(new URL(url), { parentURL: url }); - if (typeof module?.resolvedHREF === 'string') { - return module.resolvedHREF; - } - // Internal error - throw new ERR_INTERNAL_ASSERTION( - `Base url for module ${url} not loaded.` - ); - } else { - // A custom loader was used instead of network-imports. - // Adding support for a response URL resolve return in custom loaders is - // pending. - return url; - } - } - return url; - } - /** * Get a (possibly still pending) module job from the cache, * or create one and return its Promise. @@ -431,6 +373,7 @@ class ESMLoader { const moduleProvider = async (url, isMain) => { const { format: finalFormat, + responseURL, source, } = await this.load(url, { format, @@ -440,10 +383,10 @@ class ESMLoader { const translator = translators.get(finalFormat); if (!translator) { - throw new ERR_UNKNOWN_MODULE_FORMAT(finalFormat, url); + throw new ERR_UNKNOWN_MODULE_FORMAT(finalFormat, responseURL); } - return FunctionPrototypeCall(translator, this, url, source, isMain); + return FunctionPrototypeCall(translator, this, responseURL, source, isMain); }; const inspectBrk = ( @@ -523,7 +466,7 @@ class ESMLoader { * hooks starts at the top and each call to `nextLoad()` moves down 1 step * until it reaches the bottom or short-circuits. * - * @param {URL['href']} url The URL/path of the module to be loaded + * @param {string} url The URL/path of the module to be loaded * @param {object} context Metadata about the module * @returns {{ format: ModuleFormat, source: ModuleSource }} */ @@ -607,6 +550,36 @@ class ESMLoader { format, source, } = loaded; + let responseURL = loaded.responseURL; + + if (responseURL === undefined) { + responseURL = url; + } + + if (typeof responseURL !== 'string') { + throw new ERR_INVALID_RETURN_PROPERTY_VALUE( + 'undefined or a string', + hookErrIdentifier, + 'responseURL', + responseURL, + ); + } + + let responseURLObj; + try { + responseURLObj = new URL(responseURL); + } catch { + // Continue regardless of error. + } + + if (responseURLObj.href !== responseURL) { + throw new ERR_INVALID_RETURN_PROPERTY_VALUE( + 'a value URL string', + hookErrIdentifier, + 'responseURL', + responseURL, + ); + } if (format == null) { const dataUrl = RegExpPrototypeExec( @@ -644,6 +617,7 @@ class ESMLoader { return { format, + responseURL, source, }; } diff --git a/lib/internal/modules/esm/module_job.js b/lib/internal/modules/esm/module_job.js index d654958ad14cfd..fd1c6166330e76 100644 --- a/lib/internal/modules/esm/module_job.js +++ b/lib/internal/modules/esm/module_job.js @@ -76,12 +76,7 @@ class ModuleJob { // these `link` callbacks depending on each other. const dependencyJobs = []; const promises = this.module.link(async (specifier, assertions) => { - const base = await this.loader.getBaseURL(url); - const baseURL = typeof base === 'string' ? - base : - base.resolvedHREF; - - const jobPromise = this.loader.getModuleJob(specifier, baseURL, assertions); + const jobPromise = this.loader.getModuleJob(specifier, url, assertions); ArrayPrototypePush(dependencyJobs, jobPromise); const job = await jobPromise; return job.modulePromise; diff --git a/lib/internal/modules/esm/translators.js b/lib/internal/modules/esm/translators.js index d7f4c7edec63d3..bcd1775bac898e 100644 --- a/lib/internal/modules/esm/translators.js +++ b/lib/internal/modules/esm/translators.js @@ -103,9 +103,7 @@ function errPath(url) { } async function importModuleDynamically(specifier, { url }, assertions) { - return asyncESM.esmLoader.import(specifier, - asyncESM.esmLoader.getBaseURL(url), - assertions); + return asyncESM.esmLoader.import(specifier, url, assertions); } // Strategy for loading a standard JavaScript module. @@ -116,9 +114,7 @@ translators.set('module', async function moduleStrategy(url, source, isMain) { debug(`Translating StandardModule ${url}`); const module = new ModuleWrap(url, undefined, source, 0, 0); moduleWrap.callbackMap.set(module, { - initializeImportMeta: (meta, wrap) => this.importMetaInitialize(meta, { - url: wrap.url - }), + initializeImportMeta: (meta, wrap) => this.importMetaInitialize(meta, { url }), importModuleDynamically, }); return module; diff --git a/test/parallel/test-bootstrap-modules.js b/test/parallel/test-bootstrap-modules.js index 9b9dc5c18590cd..703fbc6f10af3a 100644 --- a/test/parallel/test-bootstrap-modules.js +++ b/test/parallel/test-bootstrap-modules.js @@ -78,7 +78,6 @@ const expectedModules = new Set([ 'NativeModule internal/modules/esm/fetch_module', 'NativeModule internal/modules/esm/formats', 'NativeModule internal/modules/esm/get_format', - 'NativeModule internal/modules/esm/get_source', 'NativeModule internal/modules/esm/handle_process_exit', 'NativeModule internal/modules/esm/initialize_import_meta', 'NativeModule internal/modules/esm/load', From 580b8b7586fa45345ca5e3fe363ddbf6e3c0b48c Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Fri, 27 May 2022 13:58:54 -0700 Subject: [PATCH 2/8] fix: adjust responseURL error --- lib/internal/modules/esm/loader.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/internal/modules/esm/loader.js b/lib/internal/modules/esm/loader.js index d05b5f20548e7d..fac41c0577eb5b 100644 --- a/lib/internal/modules/esm/loader.js +++ b/lib/internal/modules/esm/loader.js @@ -572,9 +572,9 @@ class ESMLoader { // Continue regardless of error. } - if (responseURLObj.href !== responseURL) { + if (responseURLObj?.href !== responseURL) { throw new ERR_INVALID_RETURN_PROPERTY_VALUE( - 'a value URL string', + 'a fully resolved URL string', hookErrIdentifier, 'responseURL', responseURL, From bab3d29a2eed62908fabb7b08bc96ef59d8fdb47 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Sun, 29 May 2022 14:20:21 -0700 Subject: [PATCH 3/8] use responseUrl over responseURL --- lib/internal/modules/esm/load.js | 12 ++++++------ lib/internal/modules/esm/loader.js | 30 +++++++++++++++--------------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/lib/internal/modules/esm/load.js b/lib/internal/modules/esm/load.js index 5464f6d7075dbe..c5af5b515dc8d0 100644 --- a/lib/internal/modules/esm/load.js +++ b/lib/internal/modules/esm/load.js @@ -31,7 +31,7 @@ const DATA_URL_PATTERN = /^[^/]+\/[^,;]+(?:[^,]*?)(;base64)?,([\s\S]*)$/; async function getSource(url, context) { const parsed = new URL(url); - let responseURL = url; + let responseUrl = url; let source; if (parsed.protocol === 'file:') { source = await readFileAsync(parsed); @@ -48,7 +48,7 @@ async function getSource(url, context) { )) { const res = await fetchModule(parsed, context); source = await res.body; - responseURL = res.resolvedHREF; + responseUrl = res.resolvedHREF; } else { const supportedSchemes = ['file', 'data']; if (experimentalNetworkImports) @@ -58,7 +58,7 @@ async function getSource(url, context) { if (policy?.manifest) { policy.manifest.assertIntegrity(parsed, source); } - return { responseURL, source }; + return { responseUrl, source }; } @@ -69,7 +69,7 @@ async function getSource(url, context) { * @returns {object} */ async function defaultLoad(url, context) { - let responseURL = url; + let responseUrl = url; const { importAssertions } = context; let { format, @@ -88,12 +88,12 @@ async function defaultLoad(url, context) { ) { source = null; } else if (source == null) { - ({ responseURL, source } = await getSource(url, context)); + ({ responseUrl, source } = await getSource(url, context)); } return { format, - responseURL, + responseUrl, source, }; } diff --git a/lib/internal/modules/esm/loader.js b/lib/internal/modules/esm/loader.js index fac41c0577eb5b..72a9073f215e0d 100644 --- a/lib/internal/modules/esm/loader.js +++ b/lib/internal/modules/esm/loader.js @@ -373,7 +373,7 @@ class ESMLoader { const moduleProvider = async (url, isMain) => { const { format: finalFormat, - responseURL, + responseUrl, source, } = await this.load(url, { format, @@ -383,10 +383,10 @@ class ESMLoader { const translator = translators.get(finalFormat); if (!translator) { - throw new ERR_UNKNOWN_MODULE_FORMAT(finalFormat, responseURL); + throw new ERR_UNKNOWN_MODULE_FORMAT(finalFormat, responseUrl); } - return FunctionPrototypeCall(translator, this, responseURL, source, isMain); + return FunctionPrototypeCall(translator, this, responseUrl, source, isMain); }; const inspectBrk = ( @@ -550,34 +550,34 @@ class ESMLoader { format, source, } = loaded; - let responseURL = loaded.responseURL; + let responseUrl = loaded.responseUrl; - if (responseURL === undefined) { - responseURL = url; + if (responseUrl === undefined) { + responseUrl = url; } - if (typeof responseURL !== 'string') { + if (typeof responseUrl !== 'string') { throw new ERR_INVALID_RETURN_PROPERTY_VALUE( 'undefined or a string', hookErrIdentifier, - 'responseURL', - responseURL, + 'responseUrl', + responseUrl, ); } - let responseURLObj; + let responseUrlObj; try { - responseURLObj = new URL(responseURL); + responseUrlObj = new URL(responseUrl); } catch { // Continue regardless of error. } - if (responseURLObj?.href !== responseURL) { + if (responseUrlObj?.href !== responseUrl) { throw new ERR_INVALID_RETURN_PROPERTY_VALUE( 'a fully resolved URL string', hookErrIdentifier, - 'responseURL', - responseURL, + 'responseUrl', + responseUrl, ); } @@ -617,7 +617,7 @@ class ESMLoader { return { format, - responseURL, + responseUrl, source, }; } From 4cbf3e1df89cc97a2af8e44b15cce3f0a48085bc Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Sun, 29 May 2022 14:22:23 -0700 Subject: [PATCH 4/8] url type --- lib/internal/modules/esm/loader.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/modules/esm/loader.js b/lib/internal/modules/esm/loader.js index 72a9073f215e0d..4f21804fd63bea 100644 --- a/lib/internal/modules/esm/loader.js +++ b/lib/internal/modules/esm/loader.js @@ -466,7 +466,7 @@ class ESMLoader { * hooks starts at the top and each call to `nextLoad()` moves down 1 step * until it reaches the bottom or short-circuits. * - * @param {string} url The URL/path of the module to be loaded + * @param {URL['href']} url The URL/path of the module to be loaded * @param {object} context Metadata about the module * @returns {{ format: ModuleFormat, source: ModuleSource }} */ From 8f4b62e13ef5cac645377dee5e2548caa6bc93d6 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Sun, 29 May 2022 14:28:49 -0700 Subject: [PATCH 5/8] pr feedback --- lib/internal/modules/esm/load.js | 3 ++- lib/internal/modules/esm/loader.js | 21 +++++++-------------- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/lib/internal/modules/esm/load.js b/lib/internal/modules/esm/load.js index c5af5b515dc8d0..1235792d876f45 100644 --- a/lib/internal/modules/esm/load.js +++ b/lib/internal/modules/esm/load.js @@ -51,8 +51,9 @@ async function getSource(url, context) { responseUrl = res.resolvedHREF; } else { const supportedSchemes = ['file', 'data']; - if (experimentalNetworkImports) + if (experimentalNetworkImports) { ArrayPrototypePush(supportedSchemes, 'http', 'https'); + } throw new ERR_UNSUPPORTED_ESM_URL_SCHEME(parsed, supportedSchemes); } if (policy?.manifest) { diff --git a/lib/internal/modules/esm/loader.js b/lib/internal/modules/esm/loader.js index 4f21804fd63bea..6bf6b93fd4e4f3 100644 --- a/lib/internal/modules/esm/loader.js +++ b/lib/internal/modules/esm/loader.js @@ -556,25 +556,18 @@ class ESMLoader { responseUrl = url; } - if (typeof responseUrl !== 'string') { - throw new ERR_INVALID_RETURN_PROPERTY_VALUE( - 'undefined or a string', - hookErrIdentifier, - 'responseUrl', - responseUrl, - ); - } - let responseUrlObj; - try { - responseUrlObj = new URL(responseUrl); - } catch { - // Continue regardless of error. + if (typeof responseUrl === 'string') { + try { + responseUrlObj = new URL(responseUrl); + } catch { + // responseUrlObj not defined will throw in next branch. + } } if (responseUrlObj?.href !== responseUrl) { throw new ERR_INVALID_RETURN_PROPERTY_VALUE( - 'a fully resolved URL string', + 'undefined or a fully resolved URL string', hookErrIdentifier, 'responseUrl', responseUrl, From 0cb156bbd591fe7d5e3c5d66f300f1faf933800f Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Sun, 29 May 2022 16:58:53 -0700 Subject: [PATCH 6/8] revert to responseURL --- lib/internal/modules/esm/load.js | 12 ++++++------ lib/internal/modules/esm/loader.js | 28 ++++++++++++++-------------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/lib/internal/modules/esm/load.js b/lib/internal/modules/esm/load.js index 1235792d876f45..86b31830457240 100644 --- a/lib/internal/modules/esm/load.js +++ b/lib/internal/modules/esm/load.js @@ -31,7 +31,7 @@ const DATA_URL_PATTERN = /^[^/]+\/[^,;]+(?:[^,]*?)(;base64)?,([\s\S]*)$/; async function getSource(url, context) { const parsed = new URL(url); - let responseUrl = url; + let responseURL = url; let source; if (parsed.protocol === 'file:') { source = await readFileAsync(parsed); @@ -48,7 +48,7 @@ async function getSource(url, context) { )) { const res = await fetchModule(parsed, context); source = await res.body; - responseUrl = res.resolvedHREF; + responseURL = res.resolvedHREF; } else { const supportedSchemes = ['file', 'data']; if (experimentalNetworkImports) { @@ -59,7 +59,7 @@ async function getSource(url, context) { if (policy?.manifest) { policy.manifest.assertIntegrity(parsed, source); } - return { responseUrl, source }; + return { responseURL, source }; } @@ -70,7 +70,7 @@ async function getSource(url, context) { * @returns {object} */ async function defaultLoad(url, context) { - let responseUrl = url; + let responseURL = url; const { importAssertions } = context; let { format, @@ -89,12 +89,12 @@ async function defaultLoad(url, context) { ) { source = null; } else if (source == null) { - ({ responseUrl, source } = await getSource(url, context)); + ({ responseURL, source } = await getSource(url, context)); } return { format, - responseUrl, + responseURL, source, }; } diff --git a/lib/internal/modules/esm/loader.js b/lib/internal/modules/esm/loader.js index 6bf6b93fd4e4f3..c16f9122f1d0e6 100644 --- a/lib/internal/modules/esm/loader.js +++ b/lib/internal/modules/esm/loader.js @@ -373,7 +373,7 @@ class ESMLoader { const moduleProvider = async (url, isMain) => { const { format: finalFormat, - responseUrl, + responseURL, source, } = await this.load(url, { format, @@ -383,10 +383,10 @@ class ESMLoader { const translator = translators.get(finalFormat); if (!translator) { - throw new ERR_UNKNOWN_MODULE_FORMAT(finalFormat, responseUrl); + throw new ERR_UNKNOWN_MODULE_FORMAT(finalFormat, responseURL); } - return FunctionPrototypeCall(translator, this, responseUrl, source, isMain); + return FunctionPrototypeCall(translator, this, responseURL, source, isMain); }; const inspectBrk = ( @@ -550,27 +550,27 @@ class ESMLoader { format, source, } = loaded; - let responseUrl = loaded.responseUrl; + let responseURL = loaded.responseURL; - if (responseUrl === undefined) { - responseUrl = url; + if (responseURL === undefined) { + responseURL = url; } - let responseUrlObj; - if (typeof responseUrl === 'string') { + let responseURLObj; + if (typeof responseURL === 'string') { try { - responseUrlObj = new URL(responseUrl); + responseURLObj = new URL(responseURL); } catch { - // responseUrlObj not defined will throw in next branch. + // responseURLObj not defined will throw in next branch. } } - if (responseUrlObj?.href !== responseUrl) { + if (responseURLObj?.href !== responseURL) { throw new ERR_INVALID_RETURN_PROPERTY_VALUE( 'undefined or a fully resolved URL string', hookErrIdentifier, - 'responseUrl', - responseUrl, + 'responseURL', + responseURL, ); } @@ -610,7 +610,7 @@ class ESMLoader { return { format, - responseUrl, + responseURL, source, }; } From a2923a90480ff05353317c829359dfb751da45b9 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Wed, 1 Jun 2022 14:31:25 -0700 Subject: [PATCH 7/8] parentURL, responseURL -> parentUrl, responseUrl --- doc/api/esm.md | 20 +++--- lib/internal/modules/esm/fetch_module.js | 4 +- lib/internal/modules/esm/get_format.js | 8 +-- lib/internal/modules/esm/load.js | 12 ++-- lib/internal/modules/esm/loader.js | 66 +++++++++---------- lib/internal/modules/esm/resolve.js | 48 +++++++------- lib/repl.js | 12 ++-- test/es-module/test-esm-loader-hooks.mjs | 8 +-- .../es-module-loaders/example-loader.mjs | 6 +- .../es-module-loaders/http-loader.mjs | 6 +- .../loader-invalid-format.mjs | 6 +- .../es-module-loaders/loader-invalid-url.mjs | 6 +- .../es-module-loaders/loader-shared-dep.mjs | 4 +- .../es-module-loaders/loader-with-dep.mjs | 4 +- .../missing-dynamic-instantiate-hook.mjs | 4 +- .../es-module-loaders/mock-loader.mjs | 2 +- .../not-found-assert-loader.mjs | 6 +- 17 files changed, 111 insertions(+), 111 deletions(-) diff --git a/doc/api/esm.md b/doc/api/esm.md index abf0486fa415d1..2228e6b19194c3 100644 --- a/doc/api/esm.md +++ b/doc/api/esm.md @@ -749,7 +749,7 @@ changes: * `context` {Object} * `conditions` {string\[]} Export conditions of the relevant `package.json` * `importAssertions` {Object} - * `parentURL` {string|undefined} The module importing this one, or undefined + * `parentUrl` {string|undefined} The module importing this one, or undefined if this is the Node.js entry point * `nextResolve` {Function} The subsequent `resolve` hook in the chain, or the Node.js default `resolve` hook after the last user-supplied `resolve` hook @@ -790,15 +790,15 @@ Node.js module specifier resolution behavior_ when calling `defaultResolve`, the ```js export async function resolve(specifier, context, nextResolve) { - const { parentURL = null } = context; + const { parentUrl = null } = context; if (Math.random() > 0.5) { // Some condition. // For some or all specifiers, do some custom logic for resolving. // Always return an object of the form {url: }. return { shortCircuit: true, - url: parentURL ? - new URL(specifier, parentURL).href : + url: parentUrl ? + new URL(specifier, parentUrl).href : new URL(specifier).href, }; } @@ -1007,7 +1007,7 @@ and there is no security. import { get } from 'node:https'; export function resolve(specifier, context, nextResolve) { - const { parentURL = null } = context; + const { parentUrl = null } = context; // Normally Node.js would error on specifiers starting with 'https://', so // this hook intercepts them and converts them into absolute URLs to be @@ -1017,10 +1017,10 @@ export function resolve(specifier, context, nextResolve) { shortCircuit: true, url: specifier }; - } else if (parentURL && parentURL.startsWith('https://')) { + } else if (parentUrl && parentUrl.startsWith('https://')) { return { shortCircuit: true, - url: new URL(specifier, parentURL).href, + url: new URL(specifier, parentUrl).href, }; } @@ -1083,20 +1083,20 @@ import { cwd } from 'node:process'; import { fileURLToPath, pathToFileURL } from 'node:url'; import CoffeeScript from 'coffeescript'; -const baseURL = pathToFileURL(`${cwd()}/`).href; +const baseUrl = pathToFileURL(`${cwd()}/`).href; // CoffeeScript files end in .coffee, .litcoffee or .coffee.md. const extensionsRegex = /\.coffee$|\.litcoffee$|\.coffee\.md$/; export async function resolve(specifier, context, nextResolve) { if (extensionsRegex.test(specifier)) { - const { parentURL = baseURL } = context; + const { parentUrl = baseUrl } = context; // Node.js normally errors on unknown file extensions, so return a URL for // specifiers ending in the CoffeeScript file extensions. return { shortCircuit: true, - url: new URL(specifier, parentURL).href + url: new URL(specifier, parentUrl).href }; } diff --git a/lib/internal/modules/esm/fetch_module.js b/lib/internal/modules/esm/fetch_module.js index c65587e34c488f..dec22b682c1773 100644 --- a/lib/internal/modules/esm/fetch_module.js +++ b/lib/internal/modules/esm/fetch_module.js @@ -217,7 +217,7 @@ async function isLocalAddress(hostname) { * @param {ESModuleContext} context * @returns {ReturnType} */ -function fetchModule(parsed, { parentURL }) { +function fetchModule(parsed, { parentUrl }) { const { href } = parsed; const existing = cacheForGET.get(href); if (existing) { @@ -228,7 +228,7 @@ function fetchModule(parsed, { parentURL }) { if (is !== true) { throw new ERR_NETWORK_IMPORT_DISALLOWED( href, - parentURL, + parentUrl, 'http can only be used to load local resources (use https instead).' ); } diff --git a/lib/internal/modules/esm/get_format.js b/lib/internal/modules/esm/get_format.js index 950a769227c03f..1aa6befdedb0eb 100644 --- a/lib/internal/modules/esm/get_format.js +++ b/lib/internal/modules/esm/get_format.js @@ -48,7 +48,7 @@ function getDataProtocolModuleFormat(parsed) { /** * @param {URL} url - * @param {{parentURL: string}} context + * @param {{parentUrl: string}} context * @param {boolean} ignoreErrors * @returns {string} */ @@ -85,7 +85,7 @@ function getFileProtocolModuleFormat(url, context, ignoreErrors) { /** * @param {URL} url - * @param {{parentURL: string}} context + * @param {{parentUrl: string}} context * @returns {Promise | undefined} only works when enabled */ function getHttpProtocolModuleFormat(url, context) { @@ -101,7 +101,7 @@ function getHttpProtocolModuleFormat(url, context) { /** * @param {URL | URL['href']} url - * @param {{parentURL: string}} context + * @param {{parentUrl: string}} context * @returns {Promise | string | undefined} only works when enabled */ function defaultGetFormatWithoutErrors(url, context) { @@ -113,7 +113,7 @@ function defaultGetFormatWithoutErrors(url, context) { /** * @param {URL | URL['href']} url - * @param {{parentURL: string}} context + * @param {{parentUrl: string}} context * @returns {Promise | string | undefined} only works when enabled */ function defaultGetFormat(url, context) { diff --git a/lib/internal/modules/esm/load.js b/lib/internal/modules/esm/load.js index 86b31830457240..1235792d876f45 100644 --- a/lib/internal/modules/esm/load.js +++ b/lib/internal/modules/esm/load.js @@ -31,7 +31,7 @@ const DATA_URL_PATTERN = /^[^/]+\/[^,;]+(?:[^,]*?)(;base64)?,([\s\S]*)$/; async function getSource(url, context) { const parsed = new URL(url); - let responseURL = url; + let responseUrl = url; let source; if (parsed.protocol === 'file:') { source = await readFileAsync(parsed); @@ -48,7 +48,7 @@ async function getSource(url, context) { )) { const res = await fetchModule(parsed, context); source = await res.body; - responseURL = res.resolvedHREF; + responseUrl = res.resolvedHREF; } else { const supportedSchemes = ['file', 'data']; if (experimentalNetworkImports) { @@ -59,7 +59,7 @@ async function getSource(url, context) { if (policy?.manifest) { policy.manifest.assertIntegrity(parsed, source); } - return { responseURL, source }; + return { responseUrl, source }; } @@ -70,7 +70,7 @@ async function getSource(url, context) { * @returns {object} */ async function defaultLoad(url, context) { - let responseURL = url; + let responseUrl = url; const { importAssertions } = context; let { format, @@ -89,12 +89,12 @@ async function defaultLoad(url, context) { ) { source = null; } else if (source == null) { - ({ responseURL, source } = await getSource(url, context)); + ({ responseUrl, source } = await getSource(url, context)); } return { format, - responseURL, + responseUrl, source, }; } diff --git a/lib/internal/modules/esm/loader.js b/lib/internal/modules/esm/loader.js index c16f9122f1d0e6..96f15927bed9e2 100644 --- a/lib/internal/modules/esm/loader.js +++ b/lib/internal/modules/esm/loader.js @@ -321,14 +321,14 @@ class ESMLoader { * @param {string} specifier The string after `from` in an `import` statement, * or the first parameter of an `import()` * expression - * @param {string | undefined} parentURL The URL of the module importing this + * @param {string | undefined} parentUrl The URL of the module importing this * one, unless this is the Node.js entry * point. * @param {Record} importAssertions Validations for the * module import. * @returns {Promise} The (possibly pending) module job */ - async getModuleJob(specifier, parentURL, importAssertions) { + async getModuleJob(specifier, parentUrl, importAssertions) { let importAssertionsForResolve; // By default, `this.#loaders` contains just the Node default load hook @@ -342,7 +342,7 @@ class ESMLoader { } const { format, url } = - await this.resolve(specifier, parentURL, importAssertionsForResolve); + await this.resolve(specifier, parentUrl, importAssertionsForResolve); let job = this.moduleMap.get(url, importAssertions.type); @@ -352,7 +352,7 @@ class ESMLoader { } if (job === undefined) { - job = this.#createModuleJob(url, importAssertions, parentURL, format); + job = this.#createModuleJob(url, importAssertions, parentUrl, format); } return job; @@ -363,17 +363,17 @@ class ESMLoader { * @param {string} url The absolute URL that was resolved for this module * @param {Record} importAssertions Validations for the * module import. - * @param {string} [parentURL] The absolute URL of the module importing this + * @param {string} [parentUrl] The absolute URL of the module importing this * one, unless this is the Node.js entry point * @param {string} [format] The format hint possibly returned by the * `resolve` hook * @returns {Promise} The (possibly pending) module job */ - #createModuleJob(url, importAssertions, parentURL, format) { + #createModuleJob(url, importAssertions, parentUrl, format) { const moduleProvider = async (url, isMain) => { const { format: finalFormat, - responseURL, + responseUrl, source, } = await this.load(url, { format, @@ -383,14 +383,14 @@ class ESMLoader { const translator = translators.get(finalFormat); if (!translator) { - throw new ERR_UNKNOWN_MODULE_FORMAT(finalFormat, responseURL); + throw new ERR_UNKNOWN_MODULE_FORMAT(finalFormat, responseUrl); } - return FunctionPrototypeCall(translator, this, responseURL, source, isMain); + return FunctionPrototypeCall(translator, this, responseUrl, source, isMain); }; const inspectBrk = ( - parentURL === undefined && + parentUrl === undefined && getOptionValue('--inspect-brk') ); @@ -399,7 +399,7 @@ class ESMLoader { url, importAssertions, moduleProvider, - parentURL === undefined, + parentUrl === undefined, inspectBrk ); @@ -416,14 +416,14 @@ class ESMLoader { * loader module. * * @param {string | string[]} specifiers Path(s) to the module. - * @param {string} parentURL Path of the parent importing the module. + * @param {string} parentUrl Path of the parent importing the module. * @param {Record} importAssertions Validations for the * module import. * @returns {Promise} * A collection of module export(s) or a list of collections of module * export(s). */ - async import(specifiers, parentURL, importAssertions) { + async import(specifiers, parentUrl, importAssertions) { // For loaders, `import` is passed multiple things to process, it returns a // list pairing the url and exports collected. This is especially useful for // error messaging, to identity from where an export came. But, in most @@ -439,7 +439,7 @@ class ESMLoader { const jobs = new Array(count); for (let i = 0; i < count; i++) { - jobs[i] = this.getModuleJob(specifiers[i], parentURL, importAssertions) + jobs[i] = this.getModuleJob(specifiers[i], parentUrl, importAssertions) .then((job) => job.run()) .then(({ module }) => module.getNamespace()); } @@ -550,27 +550,27 @@ class ESMLoader { format, source, } = loaded; - let responseURL = loaded.responseURL; + let responseUrl = loaded.responseUrl; - if (responseURL === undefined) { - responseURL = url; + if (responseUrl === undefined) { + responseUrl = url; } - let responseURLObj; - if (typeof responseURL === 'string') { + let responseURL; + if (typeof responseUrl === 'string') { try { - responseURLObj = new URL(responseURL); + responseURL = new URL(responseUrl); } catch { - // responseURLObj not defined will throw in next branch. + // responseURL not defined will throw in next branch. } } - if (responseURLObj?.href !== responseURL) { + if (responseURL?.href !== responseUrl) { throw new ERR_INVALID_RETURN_PROPERTY_VALUE( 'undefined or a fully resolved URL string', hookErrIdentifier, - 'responseURL', - responseURL, + 'responseUrl', + responseUrl, ); } @@ -610,7 +610,7 @@ class ESMLoader { return { format, - responseURL, + responseUrl, source, }; } @@ -713,27 +713,27 @@ class ESMLoader { * * @param {string} originalSpecifier The specified URL path of the module to * be resolved. - * @param {string} [parentURL] The URL path of the module's parent. + * @param {string} [parentUrl] The URL path of the module's parent. * @param {ImportAssertions} [importAssertions] Assertions from the import * statement or expression. * @returns {{ format: string, url: URL['href'] }} */ async resolve( originalSpecifier, - parentURL, + parentUrl, importAssertions = ObjectCreate(null) ) { - const isMain = parentURL === undefined; + const isMain = parentUrl === undefined; if ( !isMain && - typeof parentURL !== 'string' && - !isURLInstance(parentURL) + typeof parentUrl !== 'string' && + !isURLInstance(parentUrl) ) { throw new ERR_INVALID_ARG_TYPE( - 'parentURL', + 'parentUrl', ['string', 'URL'], - parentURL, + parentUrl, ); } const resolvers = this.#resolvers; @@ -749,7 +749,7 @@ class ESMLoader { const context = { conditions: DEFAULT_CONDITIONS, importAssertions, - parentURL, + parentUrl, }; const nextResolve = async (suppliedSpecifier, ctx = context) => { diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js index 8767109d7c4177..43c3a81757769d 100644 --- a/lib/internal/modules/esm/resolve.js +++ b/lib/internal/modules/esm/resolve.js @@ -969,12 +969,12 @@ function moduleResolve(specifier, base, conditions, preserveSymlinks) { /** * Try to resolve an import as a CommonJS module * @param {string} specifier - * @param {string} parentURL + * @param {string} parentUrl * @returns {boolean|string} */ -function resolveAsCommonJS(specifier, parentURL) { +function resolveAsCommonJS(specifier, parentUrl) { try { - const parent = fileURLToPath(parentURL); + const parent = fileURLToPath(parentUrl); const tmpModule = new CJSModule(parent, null); tmpModule.paths = CJSModule._nodeModulePaths(parent); @@ -1013,11 +1013,11 @@ function resolveAsCommonJS(specifier, parentURL) { } // TODO(@JakobJingleheimer): de-dupe `specifier` & `parsed` -function checkIfDisallowedImport(specifier, parsed, parsedParentURL) { - if (parsedParentURL) { +function checkIfDisallowedImport(specifier, parsed, parentURL) { + if (parentURL) { if ( - parsedParentURL.protocol === 'http:' || - parsedParentURL.protocol === 'https:' + parentURL.protocol === 'http:' || + parentURL.protocol === 'https:' ) { if (shouldBeTreatedAsRelativeOrAbsolutePath(specifier)) { // data: and blob: disallowed due to allowing file: access via @@ -1028,7 +1028,7 @@ function checkIfDisallowedImport(specifier, parsed, parsedParentURL) { ) { throw new ERR_NETWORK_IMPORT_DISALLOWED( specifier, - parsedParentURL, + parentURL, 'remote imports cannot import from a local location.' ); } @@ -1039,14 +1039,14 @@ function checkIfDisallowedImport(specifier, parsed, parsedParentURL) { NativeModule.canBeRequiredWithoutScheme(specifier)) { throw new ERR_NETWORK_IMPORT_DISALLOWED( specifier, - parsedParentURL, + parentURL, 'remote imports cannot import from a local location.' ); } throw new ERR_NETWORK_IMPORT_DISALLOWED( specifier, - parsedParentURL, + parentURL, 'only relative and absolute specifiers are supported.' ); } @@ -1082,9 +1082,9 @@ function throwIfUnsupportedURLScheme(parsed, experimentalNetworkImports) { } async function defaultResolve(specifier, context = {}, defaultResolveUnused) { - let { parentURL, conditions } = context; - if (parentURL && policy?.manifest) { - const redirects = policy.manifest.getDependencyMapper(parentURL); + let { parentUrl, conditions } = context; + if (parentUrl && policy?.manifest) { + const redirects = policy.manifest.getDependencyMapper(parentUrl); if (redirects) { const { resolve, reaction } = redirects; const destination = resolve(specifier, new SafeSet(conditions)); @@ -1100,7 +1100,7 @@ async function defaultResolve(specifier, context = {}, defaultResolveUnused) { // Network requests can extract data by doing things like putting // secrets in query params reaction(new ERR_MANIFEST_DEPENDENCY_MISSING( - parentURL, + parentUrl, specifier, ArrayPrototypeJoin([...conditions], ', ')) ); @@ -1108,10 +1108,10 @@ async function defaultResolve(specifier, context = {}, defaultResolveUnused) { } } - let parsedParentURL; - if (parentURL) { + let parsedparentURL; + if (parentUrl) { try { - parsedParentURL = new URL(parentURL); + parsedparentURL = new URL(parentUrl); } catch { // Ignore exception } @@ -1120,7 +1120,7 @@ async function defaultResolve(specifier, context = {}, defaultResolveUnused) { let parsed; try { if (shouldBeTreatedAsRelativeOrAbsolutePath(specifier)) { - parsed = new URL(specifier, parsedParentURL); + parsed = new URL(specifier, parsedparentURL); } else { parsed = new URL(specifier); } @@ -1145,7 +1145,7 @@ async function defaultResolve(specifier, context = {}, defaultResolveUnused) { const maybeReturn = checkIfDisallowedImport( specifier, parsed, - parsedParentURL, + parsedparentURL, ); if (maybeReturn) return maybeReturn; @@ -1155,9 +1155,9 @@ async function defaultResolve(specifier, context = {}, defaultResolveUnused) { throwIfUnsupportedURLScheme(parsed, experimentalNetworkImports); - const isMain = parentURL === undefined; + const isMain = parentUrl === undefined; if (isMain) { - parentURL = pathToFileURL(`${process.cwd()}/`).href; + parentUrl = pathToFileURL(`${process.cwd()}/`).href; // This is the initial entry point to the program, and --input-type has // been passed as an option; but --input-type can only be used with @@ -1173,7 +1173,7 @@ async function defaultResolve(specifier, context = {}, defaultResolveUnused) { try { url = moduleResolve( specifier, - parentURL, + parentUrl, conditions, isMain ? preserveSymlinksMain : preserveSymlinks ); @@ -1185,7 +1185,7 @@ async function defaultResolve(specifier, context = {}, defaultResolveUnused) { if (StringPrototypeStartsWith(specifier, 'file://')) { specifier = fileURLToPath(specifier); } - const found = resolveAsCommonJS(specifier, parentURL); + const found = resolveAsCommonJS(specifier, parentUrl); if (found) { // Modify the stack and message string to include the hint const lines = StringPrototypeSplit(error.stack, '\n'); @@ -1235,7 +1235,7 @@ if (policy) { // This is a preflight check to avoid data exfiltration by query params etc. policy.manifest.mightAllow(ret.url, () => new ERR_MANIFEST_DEPENDENCY_MISSING( - context.parentURL, + context.parentUrl, specifier, context.conditions ) diff --git a/lib/repl.js b/lib/repl.js index af9b607b022372..39c5413d7641bf 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -443,12 +443,12 @@ function REPLServer(prompt, } catch (e) { let recoverableError = false; if (e.name === 'SyntaxError') { - let parentURL; + let parentUrl; try { const { pathToFileURL } = require('url'); // Adding `/repl` prevents dynamic imports from loading relative // to the parent of `process.cwd()`. - parentURL = pathToFileURL(path.join(process.cwd(), 'repl')).href; + parentUrl = pathToFileURL(path.join(process.cwd(), 'repl')).href; } catch { // Continue regardless of error. } @@ -461,7 +461,7 @@ function REPLServer(prompt, filename: file, displayErrors: true, importModuleDynamically: (specifier, _, importAssertions) => { - return asyncESM.esmLoader.import(specifier, parentURL, + return asyncESM.esmLoader.import(specifier, parentUrl, importAssertions); } }); @@ -484,12 +484,12 @@ function REPLServer(prompt, return cb(null); if (err === null) { - let parentURL; + let parentUrl; try { const { pathToFileURL } = require('url'); // Adding `/repl` prevents dynamic imports from loading relative // to the parent of `process.cwd()`. - parentURL = pathToFileURL(path.join(process.cwd(), 'repl')).href; + parentUrl = pathToFileURL(path.join(process.cwd(), 'repl')).href; } catch { // Continue regardless of error. } @@ -505,7 +505,7 @@ function REPLServer(prompt, filename: file, displayErrors: true, importModuleDynamically: (specifier, _, importAssertions) => { - return asyncESM.esmLoader.import(specifier, parentURL, + return asyncESM.esmLoader.import(specifier, parentUrl, importAssertions); } }); diff --git a/test/es-module/test-esm-loader-hooks.mjs b/test/es-module/test-esm-loader-hooks.mjs index d314a4d9aa0a5e..b3ead9e5ce0be2 100644 --- a/test/es-module/test-esm-loader-hooks.mjs +++ b/test/es-module/test-esm-loader-hooks.mjs @@ -16,7 +16,7 @@ const { ESMLoader } = esmLoaderModule; Object.create(null), { type: 'json' }, ); - const parentURL = 'file:///entrypoint.js'; + const parentUrl = 'file:///entrypoint.js'; const resolvedURL = 'file:///foo/bar.js'; const suggestedFormat = 'test'; @@ -26,11 +26,11 @@ const { ESMLoader } = esmLoaderModule; assert.deepStrictEqual(Object.keys(context), [ 'conditions', 'importAssertions', - 'parentURL', + 'parentUrl', ]); assert.ok(Array.isArray(context.conditions)); assert.deepStrictEqual(context.importAssertions, importAssertions); - assert.strictEqual(context.parentURL, parentURL); + assert.strictEqual(context.parentUrl, parentUrl); assert.strictEqual(typeof defaultResolve, 'function'); return { @@ -76,7 +76,7 @@ const { ESMLoader } = esmLoaderModule; // Manually trigger hooks (since ESMLoader is not actually running) const job = await esmLoader.getModuleJob( originalSpecifier, - parentURL, + parentUrl, importAssertions, ); await job.modulePromise; diff --git a/test/fixtures/es-module-loaders/example-loader.mjs b/test/fixtures/es-module-loaders/example-loader.mjs index f87054c8b70502..cad8c1699e1f79 100644 --- a/test/fixtures/es-module-loaders/example-loader.mjs +++ b/test/fixtures/es-module-loaders/example-loader.mjs @@ -8,7 +8,7 @@ const JS_EXTENSIONS = new Set(['.js', '.mjs']); const baseURL = new URL('file://'); baseURL.pathname = process.cwd() + '/'; -export function resolve(specifier, { parentURL = baseURL }, defaultResolve) { +export function resolve(specifier, { parentUrl = baseURL }, defaultResolve) { if (builtinModules.includes(specifier)) { return { shortCircuit: true, @@ -17,11 +17,11 @@ export function resolve(specifier, { parentURL = baseURL }, defaultResolve) { } if (/^\.{1,2}[/]/.test(specifier) !== true && !specifier.startsWith('file:')) { // For node_modules support: - // return defaultResolve(specifier, {parentURL}, defaultResolve); + // return defaultResolve(specifier, {parentUrl}, defaultResolve); throw new Error( `imports must be URLs or begin with './', or '../'; '${specifier}' does not`); } - const resolved = new URL(specifier, parentURL); + const resolved = new URL(specifier, parentUrl); return { shortCircuit: true, url: resolved.href diff --git a/test/fixtures/es-module-loaders/http-loader.mjs b/test/fixtures/es-module-loaders/http-loader.mjs index f0add5d5b419f8..8432748bd53124 100644 --- a/test/fixtures/es-module-loaders/http-loader.mjs +++ b/test/fixtures/es-module-loaders/http-loader.mjs @@ -1,17 +1,17 @@ import { get } from 'http'; export function resolve(specifier, context, nextResolve) { - const { parentURL = null } = context; + const { parentUrl = null } = context; if (specifier.startsWith('http://')) { return { shortCircuit: true, url: specifier, }; - } else if (parentURL?.startsWith('http://')) { + } else if (parentUrl?.startsWith('http://')) { return { shortCircuit: true, - url: new URL(specifier, parentURL).href, + url: new URL(specifier, parentUrl).href, }; } diff --git a/test/fixtures/es-module-loaders/loader-invalid-format.mjs b/test/fixtures/es-module-loaders/loader-invalid-format.mjs index 438d50dacba433..1b341921090e2a 100644 --- a/test/fixtures/es-module-loaders/loader-invalid-format.mjs +++ b/test/fixtures/es-module-loaders/loader-invalid-format.mjs @@ -1,11 +1,11 @@ -export async function resolve(specifier, { parentURL, importAssertions }, defaultResolve) { - if (parentURL && specifier === '../fixtures/es-modules/test-esm-ok.mjs') { +export async function resolve(specifier, { parentUrl, importAssertions }, defaultResolve) { + if (parentUrl && specifier === '../fixtures/es-modules/test-esm-ok.mjs') { return { shortCircuit: true, url: 'file:///asdf', }; } - return defaultResolve(specifier, {parentURL, importAssertions}, defaultResolve); + return defaultResolve(specifier, {parentUrl, importAssertions}, defaultResolve); } export async function load(url, context, next) { diff --git a/test/fixtures/es-module-loaders/loader-invalid-url.mjs b/test/fixtures/es-module-loaders/loader-invalid-url.mjs index 87d1a6a564b461..3236aaef1e46e9 100644 --- a/test/fixtures/es-module-loaders/loader-invalid-url.mjs +++ b/test/fixtures/es-module-loaders/loader-invalid-url.mjs @@ -1,10 +1,10 @@ -export async function resolve(specifier, { parentURL, importAssertions }, defaultResolve) { - if (parentURL && specifier === '../fixtures/es-modules/test-esm-ok.mjs') { +export async function resolve(specifier, { parentUrl, importAssertions }, defaultResolve) { + if (parentUrl && specifier === '../fixtures/es-modules/test-esm-ok.mjs') { return { shortCircuit: true, url: specifier, importAssertions, }; } - return defaultResolve(specifier, {parentURL, importAssertions}, defaultResolve); + return defaultResolve(specifier, {parentUrl, importAssertions}, defaultResolve); } diff --git a/test/fixtures/es-module-loaders/loader-shared-dep.mjs b/test/fixtures/es-module-loaders/loader-shared-dep.mjs index 387575794c00dc..4ddb6126e82a96 100644 --- a/test/fixtures/es-module-loaders/loader-shared-dep.mjs +++ b/test/fixtures/es-module-loaders/loader-shared-dep.mjs @@ -5,7 +5,7 @@ import { createRequire } from '../../common/index.mjs'; const require = createRequire(import.meta.url); const dep = require('./loader-dep.js'); -export function resolve(specifier, { parentURL, importAssertions }, defaultResolve) { +export function resolve(specifier, { parentUrl, importAssertions }, defaultResolve) { assert.strictEqual(dep.format, 'module'); - return defaultResolve(specifier, { parentURL, importAssertions }, defaultResolve); + return defaultResolve(specifier, { parentUrl, importAssertions }, defaultResolve); } diff --git a/test/fixtures/es-module-loaders/loader-with-dep.mjs b/test/fixtures/es-module-loaders/loader-with-dep.mjs index 1b5fd6c3c1642a..2ed2ccc0b1c8cd 100644 --- a/test/fixtures/es-module-loaders/loader-with-dep.mjs +++ b/test/fixtures/es-module-loaders/loader-with-dep.mjs @@ -3,9 +3,9 @@ import {createRequire} from '../../common/index.mjs'; const require = createRequire(import.meta.url); const dep = require('./loader-dep.js'); -export async function resolve(specifier, { parentURL, importAssertions }, defaultResolve) { +export async function resolve(specifier, { parentUrl, importAssertions }, defaultResolve) { return { - url: (await defaultResolve(specifier, { parentURL, importAssertions }, defaultResolve)).url, + url: (await defaultResolve(specifier, { parentUrl, importAssertions }, defaultResolve)).url, format: dep.format }; } diff --git a/test/fixtures/es-module-loaders/missing-dynamic-instantiate-hook.mjs b/test/fixtures/es-module-loaders/missing-dynamic-instantiate-hook.mjs index ec15eb0bb8fc24..71b44d23d6d943 100644 --- a/test/fixtures/es-module-loaders/missing-dynamic-instantiate-hook.mjs +++ b/test/fixtures/es-module-loaders/missing-dynamic-instantiate-hook.mjs @@ -1,10 +1,10 @@ -export function resolve(specifier, { parentURL }, defaultResolve) { +export function resolve(specifier, { parentUrl }, defaultResolve) { if (specifier === 'test') { return { url: 'file://' }; } - return defaultResolve(specifier, {parentURL}, defaultResolve); + return defaultResolve(specifier, {parentUrl}, defaultResolve); } export function getFormat(url, context, defaultGetFormat) { diff --git a/test/fixtures/es-module-loaders/mock-loader.mjs b/test/fixtures/es-module-loaders/mock-loader.mjs index 062be39603e851..ea70a1c43f3130 100644 --- a/test/fixtures/es-module-loaders/mock-loader.mjs +++ b/test/fixtures/es-module-loaders/mock-loader.mjs @@ -177,7 +177,7 @@ export async function resolve(specifier, context, defaultResolve) { } doDrainPort(); const def = await defaultResolve(specifier, context); - if (context.parentURL?.startsWith('mock-facade:')) { + if (context.parentUrl?.startsWith('mock-facade:')) { // Do nothing, let it get the "real" module } else if (mockedModuleExports.has(def.url)) { return { 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 5213ddedb34e8d..8068aa428b2e79 100644 --- a/test/fixtures/es-module-loaders/not-found-assert-loader.mjs +++ b/test/fixtures/es-module-loaders/not-found-assert-loader.mjs @@ -3,13 +3,13 @@ import assert from 'assert'; // a loader that asserts that the defaultResolve will throw "not found" // (skipping the top-level main of course) let mainLoad = true; -export async function resolve(specifier, { parentURL, importAssertions }, defaultResolve) { +export async function resolve(specifier, { parentUrl, importAssertions }, defaultResolve) { if (mainLoad) { mainLoad = false; - return defaultResolve(specifier, {parentURL, importAssertions}, defaultResolve); + return defaultResolve(specifier, {parentUrl, importAssertions}, defaultResolve); } try { - await defaultResolve(specifier, {parentURL, importAssertions}, defaultResolve); + await defaultResolve(specifier, {parentUrl, importAssertions}, defaultResolve); } catch (e) { assert.strictEqual(e.code, 'ERR_MODULE_NOT_FOUND'); From 106303679fa98c5337494aef0c89ffeeaa971832 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Thu, 2 Jun 2022 19:26:43 -0700 Subject: [PATCH 8/8] Revert "parentURL, responseURL -> parentUrl, responseUrl" This reverts commit a2923a90480ff05353317c829359dfb751da45b9. --- doc/api/esm.md | 20 +++--- lib/internal/modules/esm/fetch_module.js | 4 +- lib/internal/modules/esm/get_format.js | 8 +-- lib/internal/modules/esm/load.js | 12 ++-- lib/internal/modules/esm/loader.js | 66 +++++++++---------- lib/internal/modules/esm/resolve.js | 48 +++++++------- lib/repl.js | 12 ++-- test/es-module/test-esm-loader-hooks.mjs | 8 +-- .../es-module-loaders/example-loader.mjs | 6 +- .../es-module-loaders/http-loader.mjs | 6 +- .../loader-invalid-format.mjs | 6 +- .../es-module-loaders/loader-invalid-url.mjs | 6 +- .../es-module-loaders/loader-shared-dep.mjs | 4 +- .../es-module-loaders/loader-with-dep.mjs | 4 +- .../missing-dynamic-instantiate-hook.mjs | 4 +- .../es-module-loaders/mock-loader.mjs | 2 +- .../not-found-assert-loader.mjs | 6 +- 17 files changed, 111 insertions(+), 111 deletions(-) diff --git a/doc/api/esm.md b/doc/api/esm.md index 2228e6b19194c3..abf0486fa415d1 100644 --- a/doc/api/esm.md +++ b/doc/api/esm.md @@ -749,7 +749,7 @@ changes: * `context` {Object} * `conditions` {string\[]} Export conditions of the relevant `package.json` * `importAssertions` {Object} - * `parentUrl` {string|undefined} The module importing this one, or undefined + * `parentURL` {string|undefined} The module importing this one, or undefined if this is the Node.js entry point * `nextResolve` {Function} The subsequent `resolve` hook in the chain, or the Node.js default `resolve` hook after the last user-supplied `resolve` hook @@ -790,15 +790,15 @@ Node.js module specifier resolution behavior_ when calling `defaultResolve`, the ```js export async function resolve(specifier, context, nextResolve) { - const { parentUrl = null } = context; + const { parentURL = null } = context; if (Math.random() > 0.5) { // Some condition. // For some or all specifiers, do some custom logic for resolving. // Always return an object of the form {url: }. return { shortCircuit: true, - url: parentUrl ? - new URL(specifier, parentUrl).href : + url: parentURL ? + new URL(specifier, parentURL).href : new URL(specifier).href, }; } @@ -1007,7 +1007,7 @@ and there is no security. import { get } from 'node:https'; export function resolve(specifier, context, nextResolve) { - const { parentUrl = null } = context; + const { parentURL = null } = context; // Normally Node.js would error on specifiers starting with 'https://', so // this hook intercepts them and converts them into absolute URLs to be @@ -1017,10 +1017,10 @@ export function resolve(specifier, context, nextResolve) { shortCircuit: true, url: specifier }; - } else if (parentUrl && parentUrl.startsWith('https://')) { + } else if (parentURL && parentURL.startsWith('https://')) { return { shortCircuit: true, - url: new URL(specifier, parentUrl).href, + url: new URL(specifier, parentURL).href, }; } @@ -1083,20 +1083,20 @@ import { cwd } from 'node:process'; import { fileURLToPath, pathToFileURL } from 'node:url'; import CoffeeScript from 'coffeescript'; -const baseUrl = pathToFileURL(`${cwd()}/`).href; +const baseURL = pathToFileURL(`${cwd()}/`).href; // CoffeeScript files end in .coffee, .litcoffee or .coffee.md. const extensionsRegex = /\.coffee$|\.litcoffee$|\.coffee\.md$/; export async function resolve(specifier, context, nextResolve) { if (extensionsRegex.test(specifier)) { - const { parentUrl = baseUrl } = context; + const { parentURL = baseURL } = context; // Node.js normally errors on unknown file extensions, so return a URL for // specifiers ending in the CoffeeScript file extensions. return { shortCircuit: true, - url: new URL(specifier, parentUrl).href + url: new URL(specifier, parentURL).href }; } diff --git a/lib/internal/modules/esm/fetch_module.js b/lib/internal/modules/esm/fetch_module.js index dec22b682c1773..c65587e34c488f 100644 --- a/lib/internal/modules/esm/fetch_module.js +++ b/lib/internal/modules/esm/fetch_module.js @@ -217,7 +217,7 @@ async function isLocalAddress(hostname) { * @param {ESModuleContext} context * @returns {ReturnType} */ -function fetchModule(parsed, { parentUrl }) { +function fetchModule(parsed, { parentURL }) { const { href } = parsed; const existing = cacheForGET.get(href); if (existing) { @@ -228,7 +228,7 @@ function fetchModule(parsed, { parentUrl }) { if (is !== true) { throw new ERR_NETWORK_IMPORT_DISALLOWED( href, - parentUrl, + parentURL, 'http can only be used to load local resources (use https instead).' ); } diff --git a/lib/internal/modules/esm/get_format.js b/lib/internal/modules/esm/get_format.js index 1aa6befdedb0eb..950a769227c03f 100644 --- a/lib/internal/modules/esm/get_format.js +++ b/lib/internal/modules/esm/get_format.js @@ -48,7 +48,7 @@ function getDataProtocolModuleFormat(parsed) { /** * @param {URL} url - * @param {{parentUrl: string}} context + * @param {{parentURL: string}} context * @param {boolean} ignoreErrors * @returns {string} */ @@ -85,7 +85,7 @@ function getFileProtocolModuleFormat(url, context, ignoreErrors) { /** * @param {URL} url - * @param {{parentUrl: string}} context + * @param {{parentURL: string}} context * @returns {Promise | undefined} only works when enabled */ function getHttpProtocolModuleFormat(url, context) { @@ -101,7 +101,7 @@ function getHttpProtocolModuleFormat(url, context) { /** * @param {URL | URL['href']} url - * @param {{parentUrl: string}} context + * @param {{parentURL: string}} context * @returns {Promise | string | undefined} only works when enabled */ function defaultGetFormatWithoutErrors(url, context) { @@ -113,7 +113,7 @@ function defaultGetFormatWithoutErrors(url, context) { /** * @param {URL | URL['href']} url - * @param {{parentUrl: string}} context + * @param {{parentURL: string}} context * @returns {Promise | string | undefined} only works when enabled */ function defaultGetFormat(url, context) { diff --git a/lib/internal/modules/esm/load.js b/lib/internal/modules/esm/load.js index 1235792d876f45..86b31830457240 100644 --- a/lib/internal/modules/esm/load.js +++ b/lib/internal/modules/esm/load.js @@ -31,7 +31,7 @@ const DATA_URL_PATTERN = /^[^/]+\/[^,;]+(?:[^,]*?)(;base64)?,([\s\S]*)$/; async function getSource(url, context) { const parsed = new URL(url); - let responseUrl = url; + let responseURL = url; let source; if (parsed.protocol === 'file:') { source = await readFileAsync(parsed); @@ -48,7 +48,7 @@ async function getSource(url, context) { )) { const res = await fetchModule(parsed, context); source = await res.body; - responseUrl = res.resolvedHREF; + responseURL = res.resolvedHREF; } else { const supportedSchemes = ['file', 'data']; if (experimentalNetworkImports) { @@ -59,7 +59,7 @@ async function getSource(url, context) { if (policy?.manifest) { policy.manifest.assertIntegrity(parsed, source); } - return { responseUrl, source }; + return { responseURL, source }; } @@ -70,7 +70,7 @@ async function getSource(url, context) { * @returns {object} */ async function defaultLoad(url, context) { - let responseUrl = url; + let responseURL = url; const { importAssertions } = context; let { format, @@ -89,12 +89,12 @@ async function defaultLoad(url, context) { ) { source = null; } else if (source == null) { - ({ responseUrl, source } = await getSource(url, context)); + ({ responseURL, source } = await getSource(url, context)); } return { format, - responseUrl, + responseURL, source, }; } diff --git a/lib/internal/modules/esm/loader.js b/lib/internal/modules/esm/loader.js index 96f15927bed9e2..c16f9122f1d0e6 100644 --- a/lib/internal/modules/esm/loader.js +++ b/lib/internal/modules/esm/loader.js @@ -321,14 +321,14 @@ class ESMLoader { * @param {string} specifier The string after `from` in an `import` statement, * or the first parameter of an `import()` * expression - * @param {string | undefined} parentUrl The URL of the module importing this + * @param {string | undefined} parentURL The URL of the module importing this * one, unless this is the Node.js entry * point. * @param {Record} importAssertions Validations for the * module import. * @returns {Promise} The (possibly pending) module job */ - async getModuleJob(specifier, parentUrl, importAssertions) { + async getModuleJob(specifier, parentURL, importAssertions) { let importAssertionsForResolve; // By default, `this.#loaders` contains just the Node default load hook @@ -342,7 +342,7 @@ class ESMLoader { } const { format, url } = - await this.resolve(specifier, parentUrl, importAssertionsForResolve); + await this.resolve(specifier, parentURL, importAssertionsForResolve); let job = this.moduleMap.get(url, importAssertions.type); @@ -352,7 +352,7 @@ class ESMLoader { } if (job === undefined) { - job = this.#createModuleJob(url, importAssertions, parentUrl, format); + job = this.#createModuleJob(url, importAssertions, parentURL, format); } return job; @@ -363,17 +363,17 @@ class ESMLoader { * @param {string} url The absolute URL that was resolved for this module * @param {Record} importAssertions Validations for the * module import. - * @param {string} [parentUrl] The absolute URL of the module importing this + * @param {string} [parentURL] The absolute URL of the module importing this * one, unless this is the Node.js entry point * @param {string} [format] The format hint possibly returned by the * `resolve` hook * @returns {Promise} The (possibly pending) module job */ - #createModuleJob(url, importAssertions, parentUrl, format) { + #createModuleJob(url, importAssertions, parentURL, format) { const moduleProvider = async (url, isMain) => { const { format: finalFormat, - responseUrl, + responseURL, source, } = await this.load(url, { format, @@ -383,14 +383,14 @@ class ESMLoader { const translator = translators.get(finalFormat); if (!translator) { - throw new ERR_UNKNOWN_MODULE_FORMAT(finalFormat, responseUrl); + throw new ERR_UNKNOWN_MODULE_FORMAT(finalFormat, responseURL); } - return FunctionPrototypeCall(translator, this, responseUrl, source, isMain); + return FunctionPrototypeCall(translator, this, responseURL, source, isMain); }; const inspectBrk = ( - parentUrl === undefined && + parentURL === undefined && getOptionValue('--inspect-brk') ); @@ -399,7 +399,7 @@ class ESMLoader { url, importAssertions, moduleProvider, - parentUrl === undefined, + parentURL === undefined, inspectBrk ); @@ -416,14 +416,14 @@ class ESMLoader { * loader module. * * @param {string | string[]} specifiers Path(s) to the module. - * @param {string} parentUrl Path of the parent importing the module. + * @param {string} parentURL Path of the parent importing the module. * @param {Record} importAssertions Validations for the * module import. * @returns {Promise} * A collection of module export(s) or a list of collections of module * export(s). */ - async import(specifiers, parentUrl, importAssertions) { + async import(specifiers, parentURL, importAssertions) { // For loaders, `import` is passed multiple things to process, it returns a // list pairing the url and exports collected. This is especially useful for // error messaging, to identity from where an export came. But, in most @@ -439,7 +439,7 @@ class ESMLoader { const jobs = new Array(count); for (let i = 0; i < count; i++) { - jobs[i] = this.getModuleJob(specifiers[i], parentUrl, importAssertions) + jobs[i] = this.getModuleJob(specifiers[i], parentURL, importAssertions) .then((job) => job.run()) .then(({ module }) => module.getNamespace()); } @@ -550,27 +550,27 @@ class ESMLoader { format, source, } = loaded; - let responseUrl = loaded.responseUrl; + let responseURL = loaded.responseURL; - if (responseUrl === undefined) { - responseUrl = url; + if (responseURL === undefined) { + responseURL = url; } - let responseURL; - if (typeof responseUrl === 'string') { + let responseURLObj; + if (typeof responseURL === 'string') { try { - responseURL = new URL(responseUrl); + responseURLObj = new URL(responseURL); } catch { - // responseURL not defined will throw in next branch. + // responseURLObj not defined will throw in next branch. } } - if (responseURL?.href !== responseUrl) { + if (responseURLObj?.href !== responseURL) { throw new ERR_INVALID_RETURN_PROPERTY_VALUE( 'undefined or a fully resolved URL string', hookErrIdentifier, - 'responseUrl', - responseUrl, + 'responseURL', + responseURL, ); } @@ -610,7 +610,7 @@ class ESMLoader { return { format, - responseUrl, + responseURL, source, }; } @@ -713,27 +713,27 @@ class ESMLoader { * * @param {string} originalSpecifier The specified URL path of the module to * be resolved. - * @param {string} [parentUrl] The URL path of the module's parent. + * @param {string} [parentURL] The URL path of the module's parent. * @param {ImportAssertions} [importAssertions] Assertions from the import * statement or expression. * @returns {{ format: string, url: URL['href'] }} */ async resolve( originalSpecifier, - parentUrl, + parentURL, importAssertions = ObjectCreate(null) ) { - const isMain = parentUrl === undefined; + const isMain = parentURL === undefined; if ( !isMain && - typeof parentUrl !== 'string' && - !isURLInstance(parentUrl) + typeof parentURL !== 'string' && + !isURLInstance(parentURL) ) { throw new ERR_INVALID_ARG_TYPE( - 'parentUrl', + 'parentURL', ['string', 'URL'], - parentUrl, + parentURL, ); } const resolvers = this.#resolvers; @@ -749,7 +749,7 @@ class ESMLoader { const context = { conditions: DEFAULT_CONDITIONS, importAssertions, - parentUrl, + parentURL, }; const nextResolve = async (suppliedSpecifier, ctx = context) => { diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js index 43c3a81757769d..8767109d7c4177 100644 --- a/lib/internal/modules/esm/resolve.js +++ b/lib/internal/modules/esm/resolve.js @@ -969,12 +969,12 @@ function moduleResolve(specifier, base, conditions, preserveSymlinks) { /** * Try to resolve an import as a CommonJS module * @param {string} specifier - * @param {string} parentUrl + * @param {string} parentURL * @returns {boolean|string} */ -function resolveAsCommonJS(specifier, parentUrl) { +function resolveAsCommonJS(specifier, parentURL) { try { - const parent = fileURLToPath(parentUrl); + const parent = fileURLToPath(parentURL); const tmpModule = new CJSModule(parent, null); tmpModule.paths = CJSModule._nodeModulePaths(parent); @@ -1013,11 +1013,11 @@ function resolveAsCommonJS(specifier, parentUrl) { } // TODO(@JakobJingleheimer): de-dupe `specifier` & `parsed` -function checkIfDisallowedImport(specifier, parsed, parentURL) { - if (parentURL) { +function checkIfDisallowedImport(specifier, parsed, parsedParentURL) { + if (parsedParentURL) { if ( - parentURL.protocol === 'http:' || - parentURL.protocol === 'https:' + parsedParentURL.protocol === 'http:' || + parsedParentURL.protocol === 'https:' ) { if (shouldBeTreatedAsRelativeOrAbsolutePath(specifier)) { // data: and blob: disallowed due to allowing file: access via @@ -1028,7 +1028,7 @@ function checkIfDisallowedImport(specifier, parsed, parentURL) { ) { throw new ERR_NETWORK_IMPORT_DISALLOWED( specifier, - parentURL, + parsedParentURL, 'remote imports cannot import from a local location.' ); } @@ -1039,14 +1039,14 @@ function checkIfDisallowedImport(specifier, parsed, parentURL) { NativeModule.canBeRequiredWithoutScheme(specifier)) { throw new ERR_NETWORK_IMPORT_DISALLOWED( specifier, - parentURL, + parsedParentURL, 'remote imports cannot import from a local location.' ); } throw new ERR_NETWORK_IMPORT_DISALLOWED( specifier, - parentURL, + parsedParentURL, 'only relative and absolute specifiers are supported.' ); } @@ -1082,9 +1082,9 @@ function throwIfUnsupportedURLScheme(parsed, experimentalNetworkImports) { } async function defaultResolve(specifier, context = {}, defaultResolveUnused) { - let { parentUrl, conditions } = context; - if (parentUrl && policy?.manifest) { - const redirects = policy.manifest.getDependencyMapper(parentUrl); + let { parentURL, conditions } = context; + if (parentURL && policy?.manifest) { + const redirects = policy.manifest.getDependencyMapper(parentURL); if (redirects) { const { resolve, reaction } = redirects; const destination = resolve(specifier, new SafeSet(conditions)); @@ -1100,7 +1100,7 @@ async function defaultResolve(specifier, context = {}, defaultResolveUnused) { // Network requests can extract data by doing things like putting // secrets in query params reaction(new ERR_MANIFEST_DEPENDENCY_MISSING( - parentUrl, + parentURL, specifier, ArrayPrototypeJoin([...conditions], ', ')) ); @@ -1108,10 +1108,10 @@ async function defaultResolve(specifier, context = {}, defaultResolveUnused) { } } - let parsedparentURL; - if (parentUrl) { + let parsedParentURL; + if (parentURL) { try { - parsedparentURL = new URL(parentUrl); + parsedParentURL = new URL(parentURL); } catch { // Ignore exception } @@ -1120,7 +1120,7 @@ async function defaultResolve(specifier, context = {}, defaultResolveUnused) { let parsed; try { if (shouldBeTreatedAsRelativeOrAbsolutePath(specifier)) { - parsed = new URL(specifier, parsedparentURL); + parsed = new URL(specifier, parsedParentURL); } else { parsed = new URL(specifier); } @@ -1145,7 +1145,7 @@ async function defaultResolve(specifier, context = {}, defaultResolveUnused) { const maybeReturn = checkIfDisallowedImport( specifier, parsed, - parsedparentURL, + parsedParentURL, ); if (maybeReturn) return maybeReturn; @@ -1155,9 +1155,9 @@ async function defaultResolve(specifier, context = {}, defaultResolveUnused) { throwIfUnsupportedURLScheme(parsed, experimentalNetworkImports); - const isMain = parentUrl === undefined; + const isMain = parentURL === undefined; if (isMain) { - parentUrl = pathToFileURL(`${process.cwd()}/`).href; + parentURL = pathToFileURL(`${process.cwd()}/`).href; // This is the initial entry point to the program, and --input-type has // been passed as an option; but --input-type can only be used with @@ -1173,7 +1173,7 @@ async function defaultResolve(specifier, context = {}, defaultResolveUnused) { try { url = moduleResolve( specifier, - parentUrl, + parentURL, conditions, isMain ? preserveSymlinksMain : preserveSymlinks ); @@ -1185,7 +1185,7 @@ async function defaultResolve(specifier, context = {}, defaultResolveUnused) { if (StringPrototypeStartsWith(specifier, 'file://')) { specifier = fileURLToPath(specifier); } - const found = resolveAsCommonJS(specifier, parentUrl); + const found = resolveAsCommonJS(specifier, parentURL); if (found) { // Modify the stack and message string to include the hint const lines = StringPrototypeSplit(error.stack, '\n'); @@ -1235,7 +1235,7 @@ if (policy) { // This is a preflight check to avoid data exfiltration by query params etc. policy.manifest.mightAllow(ret.url, () => new ERR_MANIFEST_DEPENDENCY_MISSING( - context.parentUrl, + context.parentURL, specifier, context.conditions ) diff --git a/lib/repl.js b/lib/repl.js index 39c5413d7641bf..af9b607b022372 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -443,12 +443,12 @@ function REPLServer(prompt, } catch (e) { let recoverableError = false; if (e.name === 'SyntaxError') { - let parentUrl; + let parentURL; try { const { pathToFileURL } = require('url'); // Adding `/repl` prevents dynamic imports from loading relative // to the parent of `process.cwd()`. - parentUrl = pathToFileURL(path.join(process.cwd(), 'repl')).href; + parentURL = pathToFileURL(path.join(process.cwd(), 'repl')).href; } catch { // Continue regardless of error. } @@ -461,7 +461,7 @@ function REPLServer(prompt, filename: file, displayErrors: true, importModuleDynamically: (specifier, _, importAssertions) => { - return asyncESM.esmLoader.import(specifier, parentUrl, + return asyncESM.esmLoader.import(specifier, parentURL, importAssertions); } }); @@ -484,12 +484,12 @@ function REPLServer(prompt, return cb(null); if (err === null) { - let parentUrl; + let parentURL; try { const { pathToFileURL } = require('url'); // Adding `/repl` prevents dynamic imports from loading relative // to the parent of `process.cwd()`. - parentUrl = pathToFileURL(path.join(process.cwd(), 'repl')).href; + parentURL = pathToFileURL(path.join(process.cwd(), 'repl')).href; } catch { // Continue regardless of error. } @@ -505,7 +505,7 @@ function REPLServer(prompt, filename: file, displayErrors: true, importModuleDynamically: (specifier, _, importAssertions) => { - return asyncESM.esmLoader.import(specifier, parentUrl, + return asyncESM.esmLoader.import(specifier, parentURL, importAssertions); } }); diff --git a/test/es-module/test-esm-loader-hooks.mjs b/test/es-module/test-esm-loader-hooks.mjs index b3ead9e5ce0be2..d314a4d9aa0a5e 100644 --- a/test/es-module/test-esm-loader-hooks.mjs +++ b/test/es-module/test-esm-loader-hooks.mjs @@ -16,7 +16,7 @@ const { ESMLoader } = esmLoaderModule; Object.create(null), { type: 'json' }, ); - const parentUrl = 'file:///entrypoint.js'; + const parentURL = 'file:///entrypoint.js'; const resolvedURL = 'file:///foo/bar.js'; const suggestedFormat = 'test'; @@ -26,11 +26,11 @@ const { ESMLoader } = esmLoaderModule; assert.deepStrictEqual(Object.keys(context), [ 'conditions', 'importAssertions', - 'parentUrl', + 'parentURL', ]); assert.ok(Array.isArray(context.conditions)); assert.deepStrictEqual(context.importAssertions, importAssertions); - assert.strictEqual(context.parentUrl, parentUrl); + assert.strictEqual(context.parentURL, parentURL); assert.strictEqual(typeof defaultResolve, 'function'); return { @@ -76,7 +76,7 @@ const { ESMLoader } = esmLoaderModule; // Manually trigger hooks (since ESMLoader is not actually running) const job = await esmLoader.getModuleJob( originalSpecifier, - parentUrl, + parentURL, importAssertions, ); await job.modulePromise; diff --git a/test/fixtures/es-module-loaders/example-loader.mjs b/test/fixtures/es-module-loaders/example-loader.mjs index cad8c1699e1f79..f87054c8b70502 100644 --- a/test/fixtures/es-module-loaders/example-loader.mjs +++ b/test/fixtures/es-module-loaders/example-loader.mjs @@ -8,7 +8,7 @@ const JS_EXTENSIONS = new Set(['.js', '.mjs']); const baseURL = new URL('file://'); baseURL.pathname = process.cwd() + '/'; -export function resolve(specifier, { parentUrl = baseURL }, defaultResolve) { +export function resolve(specifier, { parentURL = baseURL }, defaultResolve) { if (builtinModules.includes(specifier)) { return { shortCircuit: true, @@ -17,11 +17,11 @@ export function resolve(specifier, { parentUrl = baseURL }, defaultResolve) { } if (/^\.{1,2}[/]/.test(specifier) !== true && !specifier.startsWith('file:')) { // For node_modules support: - // return defaultResolve(specifier, {parentUrl}, defaultResolve); + // return defaultResolve(specifier, {parentURL}, defaultResolve); throw new Error( `imports must be URLs or begin with './', or '../'; '${specifier}' does not`); } - const resolved = new URL(specifier, parentUrl); + const resolved = new URL(specifier, parentURL); return { shortCircuit: true, url: resolved.href diff --git a/test/fixtures/es-module-loaders/http-loader.mjs b/test/fixtures/es-module-loaders/http-loader.mjs index 8432748bd53124..f0add5d5b419f8 100644 --- a/test/fixtures/es-module-loaders/http-loader.mjs +++ b/test/fixtures/es-module-loaders/http-loader.mjs @@ -1,17 +1,17 @@ import { get } from 'http'; export function resolve(specifier, context, nextResolve) { - const { parentUrl = null } = context; + const { parentURL = null } = context; if (specifier.startsWith('http://')) { return { shortCircuit: true, url: specifier, }; - } else if (parentUrl?.startsWith('http://')) { + } else if (parentURL?.startsWith('http://')) { return { shortCircuit: true, - url: new URL(specifier, parentUrl).href, + url: new URL(specifier, parentURL).href, }; } diff --git a/test/fixtures/es-module-loaders/loader-invalid-format.mjs b/test/fixtures/es-module-loaders/loader-invalid-format.mjs index 1b341921090e2a..438d50dacba433 100644 --- a/test/fixtures/es-module-loaders/loader-invalid-format.mjs +++ b/test/fixtures/es-module-loaders/loader-invalid-format.mjs @@ -1,11 +1,11 @@ -export async function resolve(specifier, { parentUrl, importAssertions }, defaultResolve) { - if (parentUrl && specifier === '../fixtures/es-modules/test-esm-ok.mjs') { +export async function resolve(specifier, { parentURL, importAssertions }, defaultResolve) { + if (parentURL && specifier === '../fixtures/es-modules/test-esm-ok.mjs') { return { shortCircuit: true, url: 'file:///asdf', }; } - return defaultResolve(specifier, {parentUrl, importAssertions}, defaultResolve); + return defaultResolve(specifier, {parentURL, importAssertions}, defaultResolve); } export async function load(url, context, next) { diff --git a/test/fixtures/es-module-loaders/loader-invalid-url.mjs b/test/fixtures/es-module-loaders/loader-invalid-url.mjs index 3236aaef1e46e9..87d1a6a564b461 100644 --- a/test/fixtures/es-module-loaders/loader-invalid-url.mjs +++ b/test/fixtures/es-module-loaders/loader-invalid-url.mjs @@ -1,10 +1,10 @@ -export async function resolve(specifier, { parentUrl, importAssertions }, defaultResolve) { - if (parentUrl && specifier === '../fixtures/es-modules/test-esm-ok.mjs') { +export async function resolve(specifier, { parentURL, importAssertions }, defaultResolve) { + if (parentURL && specifier === '../fixtures/es-modules/test-esm-ok.mjs') { return { shortCircuit: true, url: specifier, importAssertions, }; } - return defaultResolve(specifier, {parentUrl, importAssertions}, defaultResolve); + return defaultResolve(specifier, {parentURL, importAssertions}, defaultResolve); } diff --git a/test/fixtures/es-module-loaders/loader-shared-dep.mjs b/test/fixtures/es-module-loaders/loader-shared-dep.mjs index 4ddb6126e82a96..387575794c00dc 100644 --- a/test/fixtures/es-module-loaders/loader-shared-dep.mjs +++ b/test/fixtures/es-module-loaders/loader-shared-dep.mjs @@ -5,7 +5,7 @@ import { createRequire } from '../../common/index.mjs'; const require = createRequire(import.meta.url); const dep = require('./loader-dep.js'); -export function resolve(specifier, { parentUrl, importAssertions }, defaultResolve) { +export function resolve(specifier, { parentURL, importAssertions }, defaultResolve) { assert.strictEqual(dep.format, 'module'); - return defaultResolve(specifier, { parentUrl, importAssertions }, defaultResolve); + return defaultResolve(specifier, { parentURL, importAssertions }, defaultResolve); } diff --git a/test/fixtures/es-module-loaders/loader-with-dep.mjs b/test/fixtures/es-module-loaders/loader-with-dep.mjs index 2ed2ccc0b1c8cd..1b5fd6c3c1642a 100644 --- a/test/fixtures/es-module-loaders/loader-with-dep.mjs +++ b/test/fixtures/es-module-loaders/loader-with-dep.mjs @@ -3,9 +3,9 @@ import {createRequire} from '../../common/index.mjs'; const require = createRequire(import.meta.url); const dep = require('./loader-dep.js'); -export async function resolve(specifier, { parentUrl, importAssertions }, defaultResolve) { +export async function resolve(specifier, { parentURL, importAssertions }, defaultResolve) { return { - url: (await defaultResolve(specifier, { parentUrl, importAssertions }, defaultResolve)).url, + url: (await defaultResolve(specifier, { parentURL, importAssertions }, defaultResolve)).url, format: dep.format }; } diff --git a/test/fixtures/es-module-loaders/missing-dynamic-instantiate-hook.mjs b/test/fixtures/es-module-loaders/missing-dynamic-instantiate-hook.mjs index 71b44d23d6d943..ec15eb0bb8fc24 100644 --- a/test/fixtures/es-module-loaders/missing-dynamic-instantiate-hook.mjs +++ b/test/fixtures/es-module-loaders/missing-dynamic-instantiate-hook.mjs @@ -1,10 +1,10 @@ -export function resolve(specifier, { parentUrl }, defaultResolve) { +export function resolve(specifier, { parentURL }, defaultResolve) { if (specifier === 'test') { return { url: 'file://' }; } - return defaultResolve(specifier, {parentUrl}, defaultResolve); + return defaultResolve(specifier, {parentURL}, defaultResolve); } export function getFormat(url, context, defaultGetFormat) { diff --git a/test/fixtures/es-module-loaders/mock-loader.mjs b/test/fixtures/es-module-loaders/mock-loader.mjs index ea70a1c43f3130..062be39603e851 100644 --- a/test/fixtures/es-module-loaders/mock-loader.mjs +++ b/test/fixtures/es-module-loaders/mock-loader.mjs @@ -177,7 +177,7 @@ export async function resolve(specifier, context, defaultResolve) { } doDrainPort(); const def = await defaultResolve(specifier, context); - if (context.parentUrl?.startsWith('mock-facade:')) { + if (context.parentURL?.startsWith('mock-facade:')) { // Do nothing, let it get the "real" module } else if (mockedModuleExports.has(def.url)) { return { 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 8068aa428b2e79..5213ddedb34e8d 100644 --- a/test/fixtures/es-module-loaders/not-found-assert-loader.mjs +++ b/test/fixtures/es-module-loaders/not-found-assert-loader.mjs @@ -3,13 +3,13 @@ import assert from 'assert'; // a loader that asserts that the defaultResolve will throw "not found" // (skipping the top-level main of course) let mainLoad = true; -export async function resolve(specifier, { parentUrl, importAssertions }, defaultResolve) { +export async function resolve(specifier, { parentURL, importAssertions }, defaultResolve) { if (mainLoad) { mainLoad = false; - return defaultResolve(specifier, {parentUrl, importAssertions}, defaultResolve); + return defaultResolve(specifier, {parentURL, importAssertions}, defaultResolve); } try { - await defaultResolve(specifier, {parentUrl, importAssertions}, defaultResolve); + await defaultResolve(specifier, {parentURL, importAssertions}, defaultResolve); } catch (e) { assert.strictEqual(e.code, 'ERR_MODULE_NOT_FOUND');