From e0ca4e97abb4e216b780dd7c10e7cfe880f1923f Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Fri, 7 May 2021 19:31:57 +0200 Subject: [PATCH 1/3] module: add support for `URL` to `import.meta.resolve` --- doc/api/esm.md | 7 +++++++ lib/internal/modules/esm/loader.js | 7 +++---- test/es-module/test-esm-import-meta-resolve.mjs | 6 ++++++ 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/doc/api/esm.md b/doc/api/esm.md index 4787dfc74da92d..66f3c9acb68ec6 100644 --- a/doc/api/esm.md +++ b/doc/api/esm.md @@ -280,6 +280,13 @@ const buffer = readFileSync(new URL('./data.proto', import.meta.url)); ``` ### `import.meta.resolve(specifier[, parent])` + > Stability: 1 - Experimental diff --git a/lib/internal/modules/esm/loader.js b/lib/internal/modules/esm/loader.js index 8b2c89ba130aa7..3f4dbb992363e4 100644 --- a/lib/internal/modules/esm/loader.js +++ b/lib/internal/modules/esm/loader.js @@ -20,8 +20,7 @@ const { ERR_INVALID_RETURN_VALUE, ERR_UNKNOWN_MODULE_FORMAT } = require('internal/errors').codes; -const { URL, pathToFileURL } = require('internal/url'); -const { validateString } = require('internal/validators'); +const { URL, pathToFileURL, isURLInstance } = require('internal/url'); const ModuleMap = require('internal/modules/esm/module_map'); const ModuleJob = require('internal/modules/esm/module_job'); @@ -83,8 +82,8 @@ class Loader { async resolve(specifier, parentURL) { const isMain = parentURL === undefined; - if (!isMain) - validateString(parentURL, 'parentURL'); + if (!isMain && !isURLInstance(parentURL) && typeof parentURL !== 'string') + throw new ERR_INVALID_ARG_TYPE('parentURL', ['string', 'URL'], parentURL); const resolveResponse = await this._resolve( specifier, { parentURL, conditions: DEFAULT_CONDITIONS }, defaultResolve); diff --git a/test/es-module/test-esm-import-meta-resolve.mjs b/test/es-module/test-esm-import-meta-resolve.mjs index 911225e13c9d66..fdf06c76a44685 100644 --- a/test/es-module/test-esm-import-meta-resolve.mjs +++ b/test/es-module/test-esm-import-meta-resolve.mjs @@ -19,6 +19,12 @@ const fixtures = dirname.slice(0, dirname.lastIndexOf('/', dirname.length - 2) + await import.meta.resolve('../fixtures/empty-with-bom.txt'), fixtures + 'empty-with-bom.txt'); assert.strictEqual(await import.meta.resolve('../fixtures/'), fixtures); + assert.strictEqual( + await import.meta.resolve('../fixtures/', import.meta.url), + fixtures); + assert.strictEqual( + await import.meta.resolve('../fixtures/', new URL(import.meta.url)), + fixtures); assert.strictEqual(await import.meta.resolve('baz/', fixtures), fixtures + 'node_modules/baz/'); })().then(mustCall()); From 7d524e8155a13cb3f70e2fad97f8de0c0b38e236 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Fri, 7 May 2021 20:34:45 +0200 Subject: [PATCH 2/3] fixup! module: add support for `URL` to `import.meta.resolve` --- doc/api/esm.md | 6 ++++-- lib/internal/modules/esm/loader.js | 3 ++- test/es-module/test-esm-import-meta-resolve.mjs | 7 +++++++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/doc/api/esm.md b/doc/api/esm.md index 66f3c9acb68ec6..f9787129db4337 100644 --- a/doc/api/esm.md +++ b/doc/api/esm.md @@ -281,10 +281,12 @@ const buffer = readFileSync(new URL('./data.proto', import.meta.url)); ### `import.meta.resolve(specifier[, parent])` diff --git a/lib/internal/modules/esm/loader.js b/lib/internal/modules/esm/loader.js index 3f4dbb992363e4..1de0f565d62e0f 100644 --- a/lib/internal/modules/esm/loader.js +++ b/lib/internal/modules/esm/loader.js @@ -14,6 +14,7 @@ const { const { ERR_INVALID_ARG_VALUE, + ERR_INVALID_ARG_TYPE, ERR_INVALID_MODULE_SPECIFIER, ERR_INVALID_RETURN_PROPERTY, ERR_INVALID_RETURN_PROPERTY_VALUE, @@ -82,7 +83,7 @@ class Loader { async resolve(specifier, parentURL) { const isMain = parentURL === undefined; - if (!isMain && !isURLInstance(parentURL) && typeof parentURL !== 'string') + if (!isMain && typeof parentURL !== 'string' && !isURLInstance(parentURL)) throw new ERR_INVALID_ARG_TYPE('parentURL', ['string', 'URL'], parentURL); const resolveResponse = await this._resolve( diff --git a/test/es-module/test-esm-import-meta-resolve.mjs b/test/es-module/test-esm-import-meta-resolve.mjs index fdf06c76a44685..1fac362172ae34 100644 --- a/test/es-module/test-esm-import-meta-resolve.mjs +++ b/test/es-module/test-esm-import-meta-resolve.mjs @@ -25,6 +25,13 @@ const fixtures = dirname.slice(0, dirname.lastIndexOf('/', dirname.length - 2) + assert.strictEqual( await import.meta.resolve('../fixtures/', new URL(import.meta.url)), fixtures); + await Promise.all( + [[], {}, Symbol(), 0, 1, 1n, 1.1, () => {}, true, false].map((arg) => + assert.rejects(import.meta.resolve('../fixtures/', arg), { + code: 'ERR_INVALID_ARG_TYPE', + }) + ) + ); assert.strictEqual(await import.meta.resolve('baz/', fixtures), fixtures + 'node_modules/baz/'); })().then(mustCall()); From 900a8b0e7adc26308aa5f1f5bbe444a4ef3f2246 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Mon, 10 May 2021 15:07:16 -0700 Subject: [PATCH 3/3] [Squash] nit --- 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 1de0f565d62e0f..de9863ed71ce8a 100644 --- a/lib/internal/modules/esm/loader.js +++ b/lib/internal/modules/esm/loader.js @@ -13,8 +13,8 @@ const { } = primordials; const { - ERR_INVALID_ARG_VALUE, ERR_INVALID_ARG_TYPE, + ERR_INVALID_ARG_VALUE, ERR_INVALID_MODULE_SPECIFIER, ERR_INVALID_RETURN_PROPERTY, ERR_INVALID_RETURN_PROPERTY_VALUE,