@@ -134,19 +134,32 @@ function isRedirect(statusCode) {
134134 }
135135}
136136
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+
137149/**
138150 * @param {URL } parsed
139151 * @returns {Promise<CacheEntry> | CacheEntry }
140152 */
141- function fetchWithRedirects ( parsed ) {
153+ function fetchWithRedirects ( parsed , context ) {
142154 const existing = cacheForGET . get ( parsed . href ) ;
143155 if ( existing ) {
144156 return existing ;
145157 }
146158 const handler = parsed . protocol === 'http:' ? HTTPGet : HTTPSGet ;
147159 const result = ( async ( ) => {
160+ const accept = acceptMimes [ context . importAttributes ?. type ] ?? acceptMimes . default ;
148161 const req = handler ( parsed , {
149- headers : { Accept : '*/*' } ,
162+ headers : { Accept : accept } ,
150163 } ) ;
151164 // Note that `once` is used here to handle `error` and that it hits the
152165 // `finally` on network error/timeout.
@@ -162,7 +175,7 @@ function fetchWithRedirects(parsed) {
162175 'cannot redirect to non-network location' ,
163176 ) ;
164177 }
165- const entry = await fetchWithRedirects ( location ) ;
178+ const entry = await fetchWithRedirects ( location , context ) ;
166179 cacheForGET . set ( parsed . href , entry ) ;
167180 return entry ;
168181 }
@@ -262,7 +275,8 @@ async function isLocalAddress(hostname) {
262275 * @param {ESModuleContext } context
263276 * @returns {ReturnType<typeof fetchWithRedirects> }
264277 */
265- function fetchModule ( parsed , { parentURL } ) {
278+ function fetchModule ( parsed , context ) {
279+ const { parentURL } = context ;
266280 const { href } = parsed ;
267281 const existing = cacheForGET . get ( href ) ;
268282 if ( existing ) {
@@ -277,10 +291,10 @@ function fetchModule(parsed, { parentURL }) {
277291 'http can only be used to load local resources (use https instead).' ,
278292 ) ;
279293 }
280- return fetchWithRedirects ( parsed ) ;
294+ return fetchWithRedirects ( parsed , context ) ;
281295 } ) ;
282296 }
283- return fetchWithRedirects ( parsed ) ;
297+ return fetchWithRedirects ( parsed , context ) ;
284298}
285299
286300module . exports = {
0 commit comments