| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 921e36e commit 667d245
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -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 = { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -68,6 +68,11 @@ for (const { protocol, createServer } of [ | |||
| 68 | 68 | const server = createServer(function(_req, res) { | |
| 69 | 69 | const url = new URL(_req.url, host); | |
| 70 | 70 | const redirect = url.searchParams.get('redirect'); | |
| 71 | + | ||
| 72 | + if (url.pathname === 'json') { | ||
| 73 | + common.mustCall(() => assert.strictEqual(_req.header.content, 'application/json,*/*;charset=utf-8;q=0.5')); | ||
| 74 | + } | ||
| 75 | + | ||
| 71 | 76 | if (url.pathname === '/not-found') { | |
| 72 | 77 | res.writeHead(404); | |
| 73 | 78 | res.end(); | |
@@ -204,6 +209,13 @@ for (const { protocol, createServer } of [ | |||
| 204 | 209 | { code: 'ERR_MODULE_NOT_FOUND' }, | |
| 205 | 210 | ); | |
| 206 | 211 | ||
| 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, { with: { type: 'json' } }); | ||
| 216 | + assert.deepStrictEqual(Object.keys(json), ['default']); | ||
| 217 | + assert.strictEqual(json.default.x, 1); | ||
| 218 | + | ||
| 207 | 219 | server.close(); | |
| 208 | 220 | } | |
| 209 | 221 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments