From 0dd46393919a9e0a407c7c2ffe70c9d1f7c54110 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Tue, 10 Sep 2024 01:00:33 +0200 Subject: [PATCH] esm: fix support for `URL` instances in `import.meta.resolve` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/54690 Reviewed-By: Michaƫl Zasso Reviewed-By: Jacob Smith Reviewed-By: Yagiz Nizipli Reviewed-By: LiviaMedeiros Reviewed-By: James M Snell --- lib/internal/modules/esm/loader.js | 2 ++ .../test-esm-import-meta-resolve.mjs | 19 ++++++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/internal/modules/esm/loader.js b/lib/internal/modules/esm/loader.js index e194d4a2ae0028..9c7a52019ced60 100644 --- a/lib/internal/modules/esm/loader.js +++ b/lib/internal/modules/esm/loader.js @@ -498,6 +498,7 @@ class ModuleLoader { * @returns {{ format: string, url: URL['href'] }} */ resolve(originalSpecifier, parentURL, importAttributes) { + originalSpecifier = `${originalSpecifier}`; if (this.#customizations) { return this.#customizations.resolve(originalSpecifier, parentURL, importAttributes); } @@ -516,6 +517,7 @@ class ModuleLoader { * `import.meta.resolve` which must happen synchronously. */ resolveSync(originalSpecifier, parentURL, importAttributes) { + originalSpecifier = `${originalSpecifier}`; if (this.#customizations) { return this.#customizations.resolveSync(originalSpecifier, parentURL, importAttributes); } diff --git a/test/es-module/test-esm-import-meta-resolve.mjs b/test/es-module/test-esm-import-meta-resolve.mjs index 4291f882753774..a9c26b7aecb926 100644 --- a/test/es-module/test-esm-import-meta-resolve.mjs +++ b/test/es-module/test-esm-import-meta-resolve.mjs @@ -1,5 +1,5 @@ // Flags: --experimental-import-meta-resolve -import '../common/index.mjs'; +import { spawnPromisified } from '../common/index.mjs'; import assert from 'assert'; import { spawn } from 'child_process'; import { execPath } from 'process'; @@ -90,3 +90,20 @@ await assert.rejects(import('data:text/javascript,export default import.meta.res cp.stdin.end('import "data:text/javascript,console.log(import.meta.resolve(%22node:os%22))"'); assert.match((await cp.stdout.toArray()).toString(), /^node:os\r?\n$/); } + +{ + const result = await spawnPromisified(execPath, [ + '--no-warnings', + '--input-type=module', + '--import', 'data:text/javascript,import{register}from"node:module";register("data:text/javascript,")', + '--eval', + 'console.log(import.meta.resolve(new URL("http://example.com")))', + ]); + + assert.deepStrictEqual(result, { + code: 0, + signal: null, + stderr: '', + stdout: 'http://example.com/\n', + }); +}