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
21 changes: 18 additions & 3 deletions packages/dev/src/lib/reqres.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
import { IncomingHttpHeaders, IncomingMessage } from 'node:http'
import { IncomingMessage } from 'node:http'
import { Readable } from 'node:stream'

export const normalizeHeaders = (headers: IncomingHttpHeaders): HeadersInit => {
export const normalizeHeaders = (request: IncomingMessage) => {
const result: [string, string][] = []
let headers = request.headers

// Handle HTTP/2 pseudo-headers: https://www.rfc-editor.org/rfc/rfc9113.html#name-request-pseudo-header-field
// In certain versions of Node.js, the built-in `Request` constructor from undici throws
// if a header starts with a colon.
if (request.httpVersionMajor >= 2) {
headers = { ...headers }
delete headers[':authority']
delete headers[':method']
delete headers[':path']
delete headers[':scheme']
}

for (const [key, value] of Object.entries(headers)) {
if (Array.isArray(value)) {
Expand Down Expand Up @@ -43,11 +55,14 @@ export const getNormalizedRequestFromNodeRequest = (
? null
: (Readable.toWeb(input) as unknown as ReadableStream<unknown>)

const normalizedHeaders = normalizeHeaders(input)
normalizedHeaders.push(['x-nf-request-id', requestID])

return new Request(fullUrl, {
body,
// @ts-expect-error Not typed!
duplex: 'half',
headers: normalizeHeaders({ ...input.headers, 'x-nf-request-id': requestID }),
headers: normalizedHeaders,
method,
})
}
44 changes: 44 additions & 0 deletions packages/dev/src/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { readFile } from 'node:fs/promises'
import { IncomingMessage } from 'node:http'
import { Socket } from 'node:net'
import { resolve } from 'node:path'

import { createImageServerHandler, Fixture, generateImage, getImageResponseSize, HTTPServer } from '@netlify/dev-utils'
Expand All @@ -14,6 +16,48 @@ describe('Handling requests', () => {
vi.unstubAllEnvs()
})

test('Handles HTTP/2 Node.js requests', async () => {
const fixture = new Fixture()
.withFile(
'netlify.toml',
`[build]
publish = "public"
`,
)
.withFile('public/index.html', 'Hello from static file')
const directory = await fixture.create()
const dev = new NetlifyDev({
projectRoot: directory,
geolocation: { enabled: false },
})
await dev.start()

const nodeReq = new IncomingMessage(new Socket())
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wish we could keep this high level and just "create an HTTP/2 request" but there doesn't appear to be a good way to do that.

nodeReq.httpVersionMajor = 2
nodeReq.httpVersionMinor = 0
nodeReq.method = 'GET'
nodeReq.url = '/index.html'
nodeReq.headers = {
accept: 'text/html',
host: 'example.netlify.app',
'user-agent': 'test-agent',
// These four HTTP/2 pseudo request headers are required per the HTTP/2 spec:
// https://www.rfc-editor.org/rfc/rfc9113.html#name-request-pseudo-header-field
// These show up here like any other header on Node.js IncomingMessage objects,
':method': 'GET',
':path': '/index.html',
':scheme': 'https',
':authority': 'example.netlify.app',
}
const result = await dev.handleAndIntrospectNodeRequest(nodeReq)

expect(result?.response.ok).toBe(true)
expect(await result?.response.text()).toBe('Hello from static file')

await dev.stop()
await fixture.destroy()
})

describe('No linked site', () => {
test('Same-site rewrite to a static file', async () => {
const fixture = new Fixture()
Expand Down
Loading