From 8d40818d7a95aad76f1bd7096595f44dab847de1 Mon Sep 17 00:00:00 2001 From: Marco Ippolito Date: Mon, 30 Oct 2023 18:51:52 +0200 Subject: [PATCH] module: add application/json in accept header when fetching json module PR-URL: https://github.com/nodejs/node/pull/50119 Refs: https://github.com/nodejs/node/issues/50116 Reviewed-By: Benjamin Gruenbaum Reviewed-By: Jacob Smith Reviewed-By: Antoine du Hamel --- lib/internal/modules/esm/fetch_module.js | 26 ++++++++++++++++++------ test/es-module/test-http-imports.mjs | 12 +++++++++++ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/lib/internal/modules/esm/fetch_module.js b/lib/internal/modules/esm/fetch_module.js index 21b7456899604f..b3491d97cb99c7 100644 --- a/lib/internal/modules/esm/fetch_module.js +++ b/lib/internal/modules/esm/fetch_module.js @@ -134,19 +134,32 @@ function isRedirect(statusCode) { } } +/** + * @typedef AcceptMimes possible values of Accept header when fetching a module + * @property {Promise | string} default default Accept header value. + * @property {Record} json Accept header value when fetching module with importAttributes json. + * @type {AcceptMimes} + */ +const acceptMimes = { + __proto_: null, + default: '*/*', + json: 'application/json,*/*;charset=utf-8;q=0.5', +}; + /** * @param {URL} parsed * @returns {Promise | CacheEntry} */ -function fetchWithRedirects(parsed) { +function fetchWithRedirects(parsed, context) { const existing = cacheForGET.get(parsed.href); if (existing) { return existing; } const handler = parsed.protocol === 'http:' ? HTTPGet : HTTPSGet; const result = (async () => { + const accept = acceptMimes[context.importAttributes?.type] ?? acceptMimes.default; const req = handler(parsed, { - headers: { Accept: '*/*' }, + headers: { Accept: accept }, }); // Note that `once` is used here to handle `error` and that it hits the // `finally` on network error/timeout. @@ -162,7 +175,7 @@ function fetchWithRedirects(parsed) { 'cannot redirect to non-network location', ); } - const entry = await fetchWithRedirects(location); + const entry = await fetchWithRedirects(location, context); cacheForGET.set(parsed.href, entry); return entry; } @@ -262,7 +275,8 @@ async function isLocalAddress(hostname) { * @param {ESModuleContext} context * @returns {ReturnType} */ -function fetchModule(parsed, { parentURL }) { +function fetchModule(parsed, context) { + const { parentURL } = context; const { href } = parsed; const existing = cacheForGET.get(href); if (existing) { @@ -277,10 +291,10 @@ function fetchModule(parsed, { parentURL }) { 'http can only be used to load local resources (use https instead).', ); } - return fetchWithRedirects(parsed); + return fetchWithRedirects(parsed, context); }); } - return fetchWithRedirects(parsed); + return fetchWithRedirects(parsed, context); } module.exports = { diff --git a/test/es-module/test-http-imports.mjs b/test/es-module/test-http-imports.mjs index 235d142d3555e3..e1cb1f5ced59c7 100644 --- a/test/es-module/test-http-imports.mjs +++ b/test/es-module/test-http-imports.mjs @@ -68,6 +68,11 @@ for (const { protocol, createServer } of [ const server = createServer(function(_req, res) { const url = new URL(_req.url, host); const redirect = url.searchParams.get('redirect'); + + if (url.pathname === 'json') { + common.mustCall(() => assert.strictEqual(_req.header.content, 'application/json,*/*;charset=utf-8;q=0.5')); + } + if (url.pathname === '/not-found') { res.writeHead(404); res.end(); @@ -204,6 +209,13 @@ for (const { protocol, createServer } of [ { code: 'ERR_MODULE_NOT_FOUND' }, ); + const jsonUrl = new URL(url.href + 'json'); + jsonUrl.searchParams.set('mime', 'application/json'); + jsonUrl.searchParams.set('body', '{"x": 1}'); + const json = await import(jsonUrl.href, { with: { type: 'json' } }); + assert.deepStrictEqual(Object.keys(json), ['default']); + assert.strictEqual(json.default.x, 1); + server.close(); } }