Skip to content

Commit ee25f49

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

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-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 = {

test/es-module/test-http-imports.mjs

+12
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ for (const { protocol, createServer } of [
6868
const server = createServer(function(_req, res) {
6969
const url = new URL(_req.url, host);
7070
const redirect = url.searchParams.get('redirect');
71+
72+
if (url.pathname === 'json') {
73+
common.mustCall(() => assert.strictEqual(_req.header.content, 'application/json,*/*;'));
74+
}
75+
7176
if (url.pathname === '/not-found') {
7277
res.writeHead(404);
7378
res.end();
@@ -204,6 +209,13 @@ for (const { protocol, createServer } of [
204209
{ code: 'ERR_MODULE_NOT_FOUND' },
205210
);
206211

212+
const jsonUrl = new URL(url.href + 'json');
213+
jsonUrl.searchParams.set('mime', 'application/json');
214+
jsonUrl.searchParams.set('body', '{"x": 1}');
215+
const json = await import(jsonUrl.href, { assert: { type: 'json' } });
216+
assert.deepStrictEqual(Object.keys(json), ['default']);
217+
assert.strictEqual(json.default.x, 1);
218+
207219
server.close();
208220
}
209221
}

0 commit comments

Comments
 (0)