Skip to content

Commit 3b8d97d

Browse files
committed
refactor(network.fetch): handle iterable header form and tidy test
`Dispatcher.dispatch` accepts headers as a Map/web-Headers iterable in addition to the flat string[] and plain object forms. The previous object branch routed iterables through Object.entries, which would silently drop every header for Map-like inputs. Detect Symbol.iterator and consume the iterator directly when present. Also drop the underscore prefix on the test's `req` parameter since it is used.
1 parent 5ba9d32 commit 3b8d97d

2 files changed

Lines changed: 9 additions & 3 deletions

File tree

network/fetch/src/dispatcher.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,14 @@ function stripSecFetchHeaders (dispatch: Dispatcher['dispatch']): Dispatcher['di
4747
}
4848
opts = { ...opts, headers: filtered }
4949
} else if (typeof opts.headers === 'object') {
50+
// undici also accepts an iterable of [key, value] pairs (e.g. a Map or
51+
// web Headers). Use that iterator when present; otherwise fall back to
52+
// Object.entries for plain IncomingHttpHeaders objects.
53+
const entries = Symbol.iterator in opts.headers
54+
? (opts.headers as Iterable<[string, string | string[] | undefined]>)
55+
: Object.entries(opts.headers as Record<string, string | string[] | undefined>)
5056
const headers: Record<string, string | string[] | undefined> = {}
51-
for (const [key, value] of Object.entries(opts.headers as Record<string, string | string[] | undefined>)) {
57+
for (const [key, value] of entries) {
5258
if (!key.toLowerCase().startsWith('sec-fetch-')) {
5359
headers[key] = value
5460
}

network/fetch/test/fetchFromRegistry.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,8 @@ test('createDispatchedFetch returns a fetch bound to the given dispatcher option
313313

314314
test('sec-fetch-* headers are stripped from requests', async () => {
315315
const receivedHeaders = await new Promise<http.IncomingHttpHeaders>((resolve, reject) => {
316-
const server = http.createServer((_req, res) => {
317-
resolve(_req.headers)
316+
const server = http.createServer((req, res) => {
317+
resolve(req.headers)
318318
res.writeHead(200, { 'content-type': 'application/json' })
319319
res.end('{"ok":true}')
320320
})

0 commit comments

Comments
 (0)