From af0919f8925a48ba24daeb0b7be5fa984071e254 Mon Sep 17 00:00:00 2001 From: Marco Ippolito Date: Tue, 10 Oct 2023 11:47:36 +0100 Subject: [PATCH] module: add application/json in accept header when fetching json module --- lib/internal/modules/esm/fetch_module.js | 3 ++- test/es-module/test-http-imports.mjs | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/internal/modules/esm/fetch_module.js b/lib/internal/modules/esm/fetch_module.js index 21b7456899604f..fbeea6f923d306 100644 --- a/lib/internal/modules/esm/fetch_module.js +++ b/lib/internal/modules/esm/fetch_module.js @@ -145,8 +145,9 @@ function fetchWithRedirects(parsed) { } const handler = parsed.protocol === 'http:' ? HTTPGet : HTTPSGet; const result = (async () => { + const extension = parsed.pathname.split('.').pop(); const req = handler(parsed, { - headers: { Accept: '*/*' }, + headers: { Accept: extension === 'json' ? 'application/json,*/*;' : '*/*;' }, }); // Note that `once` is used here to handle `error` and that it hits the // `finally` on network error/timeout. diff --git a/test/es-module/test-http-imports.mjs b/test/es-module/test-http-imports.mjs index 235d142d3555e3..c838f93597299f 100644 --- a/test/es-module/test-http-imports.mjs +++ b/test/es-module/test-http-imports.mjs @@ -67,6 +67,11 @@ for (const { protocol, createServer } of [ // ?body sets the body, string const server = createServer(function(_req, res) { const url = new URL(_req.url, host); + + if (url.pathname.includes('json')) { + assert.strictEqual(_req.headers.accept, 'application/json,*/*;'); + } + const redirect = url.searchParams.get('redirect'); if (url.pathname === '/not-found') { res.writeHead(404); @@ -203,6 +208,8 @@ for (const { protocol, createServer } of [ import(notFound.href), { code: 'ERR_MODULE_NOT_FOUND' }, ); + // Import file with json extension + await import(url.href + 'empty.json'); server.close(); }