Skip to content

Commit cca1ab5

Browse files
module: add application/json in accept header when fetching json module
1 parent 20b996d commit cca1ab5

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

lib/internal/modules/esm/fetch_module.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -138,15 +138,16 @@ function isRedirect(statusCode) {
138138
* @param {URL} parsed
139139
* @returns {Promise<CacheEntry> | CacheEntry}
140140
*/
141-
function fetchWithRedirects(parsed) {
141+
function fetchWithRedirects(parsed, context) {
142142
const existing = cacheForGET.get(parsed.href);
143143
if (existing) {
144144
return existing;
145145
}
146146
const handler = parsed.protocol === 'http:' ? HTTPGet : HTTPSGet;
147147
const result = (async () => {
148+
const extension = context.importAssertions?.type;
148149
const req = handler(parsed, {
149-
headers: { Accept: '*/*' },
150+
headers: { Accept: extension === 'json' ? 'application/json,*/*;' : '*/*;' },
150151
});
151152
// Note that `once` is used here to handle `error` and that it hits the
152153
// `finally` on network error/timeout.
@@ -162,7 +163,7 @@ function fetchWithRedirects(parsed) {
162163
'cannot redirect to non-network location',
163164
);
164165
}
165-
const entry = await fetchWithRedirects(location);
166+
const entry = await fetchWithRedirects(location, context);
166167
cacheForGET.set(parsed.href, entry);
167168
return entry;
168169
}
@@ -262,7 +263,8 @@ async function isLocalAddress(hostname) {
262263
* @param {ESModuleContext} context
263264
* @returns {ReturnType<typeof fetchWithRedirects>}
264265
*/
265-
function fetchModule(parsed, { parentURL }) {
266+
function fetchModule(parsed, context) {
267+
const { parentURL } = context;
266268
const { href } = parsed;
267269
const existing = cacheForGET.get(href);
268270
if (existing) {
@@ -277,10 +279,10 @@ function fetchModule(parsed, { parentURL }) {
277279
'http can only be used to load local resources (use https instead).',
278280
);
279281
}
280-
return fetchWithRedirects(parsed);
282+
return fetchWithRedirects(parsed, context);
281283
});
282284
}
283-
return fetchWithRedirects(parsed);
285+
return fetchWithRedirects(parsed, context);
284286
}
285287

286288
module.exports = {

0 commit comments

Comments
 (0)