@@ -134,19 +134,32 @@ function isRedirect(statusCode) {
134
134
}
135
135
}
136
136
137
+ /**
138
+ * @typedef AcceptMimes possible values of Accept header when fetching a module
139
+ * @property {Promise<string> | string } default default Accept header value.
140
+ * @property {Record<string, string> } json Accept header value when fetching module with importAttributes json.
141
+ * @type {AcceptMimes }
142
+ */
143
+ const acceptMimes = {
144
+ __proto_ : null ,
145
+ default : '*/*' ,
146
+ json : 'application/json,*/*;charset=utf-8;q=0.5' ,
147
+ } ;
148
+
137
149
/**
138
150
* @param {URL } parsed
139
151
* @returns {Promise<CacheEntry> | CacheEntry }
140
152
*/
141
- function fetchWithRedirects ( parsed ) {
153
+ function fetchWithRedirects ( parsed , context ) {
142
154
const existing = cacheForGET . get ( parsed . href ) ;
143
155
if ( existing ) {
144
156
return existing ;
145
157
}
146
158
const handler = parsed . protocol === 'http:' ? HTTPGet : HTTPSGet ;
147
159
const result = ( async ( ) => {
160
+ const accept = acceptMimes [ context . importAttributes ?. type ] ?? acceptMimes . default ;
148
161
const req = handler ( parsed , {
149
- headers : { Accept : '*/*' } ,
162
+ headers : { Accept : accept } ,
150
163
} ) ;
151
164
// Note that `once` is used here to handle `error` and that it hits the
152
165
// `finally` on network error/timeout.
@@ -162,7 +175,7 @@ function fetchWithRedirects(parsed) {
162
175
'cannot redirect to non-network location' ,
163
176
) ;
164
177
}
165
- const entry = await fetchWithRedirects ( location ) ;
178
+ const entry = await fetchWithRedirects ( location , context ) ;
166
179
cacheForGET . set ( parsed . href , entry ) ;
167
180
return entry ;
168
181
}
@@ -262,7 +275,8 @@ async function isLocalAddress(hostname) {
262
275
* @param {ESModuleContext } context
263
276
* @returns {ReturnType<typeof fetchWithRedirects> }
264
277
*/
265
- function fetchModule ( parsed , { parentURL } ) {
278
+ function fetchModule ( parsed , context ) {
279
+ const { parentURL } = context ;
266
280
const { href } = parsed ;
267
281
const existing = cacheForGET . get ( href ) ;
268
282
if ( existing ) {
@@ -277,10 +291,10 @@ function fetchModule(parsed, { parentURL }) {
277
291
'http can only be used to load local resources (use https instead).' ,
278
292
) ;
279
293
}
280
- return fetchWithRedirects ( parsed ) ;
294
+ return fetchWithRedirects ( parsed , context ) ;
281
295
} ) ;
282
296
}
283
- return fetchWithRedirects ( parsed ) ;
297
+ return fetchWithRedirects ( parsed , context ) ;
284
298
}
285
299
286
300
module . exports = {
0 commit comments