From 4ebb88fea5a9d83f2c4ec566e958d735f1fca138 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Fri, 7 May 2021 19:31:57 +0200 Subject: [PATCH] module: add support for `URL` to `import.meta.resolve` PR-URL: https://github.com/nodejs/node/pull/38587 Refs: https://github.com/nodejs/node/pull/38585 Reviewed-By: Luigi Pinca Reviewed-By: James M Snell --- doc/api/esm.md | 9 +++++++++ lib/internal/modules/esm/loader.js | 8 ++++---- test/es-module/test-esm-import-meta-resolve.mjs | 13 +++++++++++++ 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/doc/api/esm.md b/doc/api/esm.md index 4787dfc74da92d..f9787129db4337 100644 --- a/doc/api/esm.md +++ b/doc/api/esm.md @@ -280,6 +280,15 @@ 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..de9863ed71ce8a 100644 --- a/lib/internal/modules/esm/loader.js +++ b/lib/internal/modules/esm/loader.js @@ -13,6 +13,7 @@ const { } = primordials; const { + ERR_INVALID_ARG_TYPE, ERR_INVALID_ARG_VALUE, ERR_INVALID_MODULE_SPECIFIER, ERR_INVALID_RETURN_PROPERTY, @@ -20,8 +21,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 +83,8 @@ class Loader { async resolve(specifier, parentURL) { const isMain = parentURL === undefined; - if (!isMain) - validateString(parentURL, 'parentURL'); + if (!isMain && typeof parentURL !== 'string' && !isURLInstance(parentURL)) + 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..1fac362172ae34 100644 --- a/test/es-module/test-esm-import-meta-resolve.mjs +++ b/test/es-module/test-esm-import-meta-resolve.mjs @@ -19,6 +19,19 @@ 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); + 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());