Skip to content

Commit

Permalink
module: add application/json in accept header when fetching json module
Browse files Browse the repository at this point in the history
  • Loading branch information
marco-ippolito committed Oct 10, 2023
1 parent 20b996d commit cca1ab5
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions lib/internal/modules/esm/fetch_module.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,16 @@ function isRedirect(statusCode) {
* @param {URL} parsed
* @returns {Promise<CacheEntry> | 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 extension = context.importAssertions?.type;
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.
Expand All @@ -162,7 +163,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;
}
Expand Down Expand Up @@ -262,7 +263,8 @@ async function isLocalAddress(hostname) {
* @param {ESModuleContext} context
* @returns {ReturnType<typeof fetchWithRedirects>}
*/
function fetchModule(parsed, { parentURL }) {
function fetchModule(parsed, context) {
const { parentURL } = context;
const { href } = parsed;
const existing = cacheForGET.get(href);
if (existing) {
Expand All @@ -277,10 +279,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 = {
Expand Down

0 comments on commit cca1ab5

Please sign in to comment.