Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/fetch/src/headers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,19 @@ it('toFetchHeaders', () => {
['x-custom-header', 'custom-value'],
])
})

it('toFetchHeaders skips http2 pseudo-headers', () => {
const standardHeaders: StandardHeaders = {
':method': 'POST',
':path': '/hello?foo=bar',
':scheme': 'https',
':authority': 'example.com',
'content-type': 'application/json',
}

const fetchHeaders = toFetchHeaders(standardHeaders)

expect([...fetchHeaders]).toEqual([
['content-type', 'application/json'],
])
})
8 changes: 8 additions & 0 deletions packages/fetch/src/headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,19 @@ export function toStandardHeaders(headers: Headers): StandardHeaders {

/**
* Convert standard headers to fetch headers.
*
* HTTP/2 pseudo-headers (`:method`, `:path`, ...), which node's http2 request
* headers carry, are skipped: they are not header fields (RFC 9113 §8.3) and
* fetch rejects their names.
*/
export function toFetchHeaders(standardHeaders: StandardHeaders): Headers {
const headers = new Headers()

for (const key of Object.keys(standardHeaders)) {
if (key.startsWith(':')) {
continue
}

const value = standardHeaders[key]
if (Array.isArray(value)) {
for (const v of value) {
Expand Down
3 changes: 3 additions & 0 deletions tests/data-transfer.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ErrorEvent, unwrapEvent, withEventMeta } from '@standard-server/core'
import { toFetchHeaders } from '@standard-server/fetch'
import { isAsyncIteratorObject, sleep } from '@standard-server/shared'
import { expectPeerMessages } from './client-server'
import { createExpressjsClientServerTest } from './client-server.expressjs'
Expand Down Expand Up @@ -87,6 +88,8 @@ describe.each([
...headers,
'x-from': 'client',
})
// node http2 request headers carry pseudo-headers, which fetch rejects
expect(toFetchHeaders(request.headers).get('x-from')).toEqual('client')
})

it.each([
Expand Down
Loading