From b68db72746fe9c997d29de6675115b67d481c694 Mon Sep 17 00:00:00 2001 From: Bradley Farias Date: Sun, 27 Feb 2022 03:20:42 -0600 Subject: [PATCH] esm: fix relative imports for https PR-URL: https://github.com/nodejs/node/pull/42119 Reviewed-By: Geoffrey Booth Reviewed-By: Guy Bedford Reviewed-By: Antoine du Hamel Reviewed-By: Mohammed Keyvanzadeh Reviewed-By: Ricky Zhou <0x19951125@gmail.com> Reviewed-By: Benjamin Gruenbaum --- lib/internal/modules/esm/resolve.js | 2 +- test/es-module/test-http-imports.mjs | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js index a8bc02aeb0a739..3576f75f0a40a6 100644 --- a/lib/internal/modules/esm/resolve.js +++ b/lib/internal/modules/esm/resolve.js @@ -1177,7 +1177,7 @@ async function defaultResolve(specifier, context = {}, defaultResolveUnused) { ) ) ) { - return { url: specifier }; + return { url: parsed.href }; } } catch { // Ignore exception diff --git a/test/es-module/test-http-imports.mjs b/test/es-module/test-http-imports.mjs index a9a5204ffb7947..139c72a4e26c65 100644 --- a/test/es-module/test-http-imports.mjs +++ b/test/es-module/test-http-imports.mjs @@ -61,6 +61,10 @@ for (const { protocol, createServer } of [ const host = new URL(base); host.protocol = protocol; host.hostname = hostname; + // /not-found is a 404 + // ?redirect causes a redirect, no body. JSON.parse({status:number,location:string}) + // ?mime sets the content-type, string + // ?body sets the body, string const server = createServer(function(_req, res) { const url = new URL(_req.url, host); const redirect = url.searchParams.get('redirect'); @@ -128,6 +132,14 @@ for (const { protocol, createServer } of [ assert.strict.equal(depsNS.data, 1); assert.strict.equal(depsNS.http, ns); + const relativeDeps = new URL(url.href); + relativeDeps.searchParams.set('body', ` + import * as http from "./"; + export {http}; + `); + const relativeDepsNS = await import(relativeDeps.href); + assert.strict.deepStrictEqual(Object.keys(relativeDepsNS), ['http']); + assert.strict.equal(relativeDepsNS.http, ns); const fileDep = new URL(url.href); const { href } = pathToFileURL(path('/es-modules/message.mjs')); fileDep.searchParams.set('body', `