Repository navigation
fix: serve Next.js over Harper's HTTP/2 secure port #73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jjohnson-hdb
wants to merge
2
commits into
main
Choose a base branch
from
fix/http2-next-request-handler
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+289
−5
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import { connect } from 'node:http2'; | ||
| import { gunzipSync } from 'node:zlib'; | ||
| import { fixture } from './fixture.ts'; | ||
|
|
||
| const { test, expect } = fixture('next-16', { http: { http2: true, mtls: false } }); | ||
|
|
||
| const SECURE_PORT = 9927; | ||
|
|
||
| async function getHttp2(origin: string, path: string, headers: Record<string, string> = {}) { | ||
| const client = connect(origin, { rejectUnauthorized: false }); | ||
| try { | ||
| return await new Promise<{ status: number; headers: Record<string, any>; body: Buffer }>((resolve, reject) => { | ||
| client.on('error', reject); | ||
| const stream = client.request({ ':path': path, ...headers }); | ||
| const chunks: Buffer[] = []; | ||
| let responseHeaders: Record<string, any> = {}; | ||
| stream.on('response', (received) => (responseHeaders = received)); | ||
| stream.on('data', (chunk: Buffer) => chunks.push(chunk)); | ||
| stream.on('end', () => | ||
| resolve({ status: responseHeaders[':status'], headers: responseHeaders, body: Buffer.concat(chunks) }) | ||
| ); | ||
| stream.on('error', reject); | ||
| stream.end(); | ||
| }); | ||
| } finally { | ||
| client.close(); | ||
| } | ||
| } | ||
|
|
||
| function decode(response: { headers: Record<string, any>; body: Buffer }): string { | ||
| return (response.headers['content-encoding'] === 'gzip' ? gunzipSync(response.body) : response.body).toString(); | ||
| } | ||
|
|
||
| test('home page renders over HTTP/2', async ({ harper }) => { | ||
| const response = await getHttp2(`https://${harper.hostname}:${SECURE_PORT}`, '/', { | ||
| accept: 'text/html', | ||
| 'accept-encoding': 'gzip', | ||
| }); | ||
| expect(response.status).toBe(200); | ||
| expect(response.headers['content-encoding']).toBe('gzip'); | ||
| expect(decode(response)).toContain('Next.js v16'); | ||
| }); | ||
|
|
||
| test('static assets are served over HTTP/2', async ({ harper }) => { | ||
| const origin = `https://${harper.hostname}:${SECURE_PORT}`; | ||
| const page = decode(await getHttp2(origin, '/', { accept: 'text/html' })); | ||
| const asset = page.match(/\/_next\/static\/[^"]+\.js/)?.[0]; | ||
| expect(asset).toBeTruthy(); | ||
|
|
||
| const response = await getHttp2(origin, asset!, { 'accept-encoding': 'gzip' }); | ||
| expect(response.status).toBe(200); | ||
| expect(decode(response).length).toBeGreaterThan(0); | ||
| }); | ||
|
|
||
| test('HTTP/1.1 on the same secure port is unaffected', async ({ request, harper }) => { | ||
| const response = await request.get(`https://${harper.hostname}:${SECURE_PORT}/`, { ignoreHTTPSErrors: true }); | ||
| expect(response.status()).toBe(200); | ||
| expect(await response.text()).toContain('Next.js v16'); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,192 @@ | ||
| import { describe, it, after, before } from 'node:test'; | ||
| import assert from 'node:assert'; | ||
| import { createRequire } from 'node:module'; | ||
| import { createServer as createHttpServer, request as httpRequest, type IncomingMessage, type ServerResponse } from 'node:http'; | ||
| import { connect, constants, createServer as createHttp2Server, type Http2Server } from 'node:http2'; | ||
| import type { AddressInfo } from 'node:net'; | ||
| import { gunzipSync } from 'node:zlib'; | ||
|
|
||
| import { adaptHttp2ForNext } from './http2Compat.js'; | ||
|
|
||
| const compression = createRequire(import.meta.url)('next-16/dist/compiled/compression'); | ||
|
|
||
| const BODY = 'x'.repeat(4096); | ||
|
|
||
| type Handler = (request: any, response: any) => void; | ||
|
|
||
| async function listenHttp2(handler: Handler): Promise<{ server: Http2Server; url: string }> { | ||
| const server = createHttp2Server(handler); | ||
| await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve)); | ||
| return { server, url: `http://127.0.0.1:${(server.address() as AddressInfo).port}` }; | ||
| } | ||
|
|
||
| async function getHttp2(url: string, headers: Record<string, string> = {}) { | ||
| const client = connect(url); | ||
| try { | ||
| return await new Promise<{ status: number; headers: Record<string, any>; body: Buffer }>((resolve, reject) => { | ||
| client.on('error', reject); | ||
| const stream = client.request({ ':path': '/', ...headers }); | ||
| const chunks: Buffer[] = []; | ||
| let responseHeaders: Record<string, any> = {}; | ||
| stream.on('response', (received) => (responseHeaders = received)); | ||
| stream.on('data', (chunk: Buffer) => chunks.push(chunk)); | ||
| stream.on('end', () => | ||
| resolve({ status: responseHeaders[':status'], headers: responseHeaders, body: Buffer.concat(chunks) }) | ||
| ); | ||
| stream.on('error', reject); | ||
| stream.end(); | ||
| }); | ||
| } finally { | ||
| client.close(); | ||
| } | ||
| } | ||
|
|
||
| function compressedHandler(adapt: boolean, onError?: (error: Error) => void): Handler { | ||
| const compress = compression(); | ||
| return (request, response) => { | ||
| if (adapt) adaptHttp2ForNext(request, response); | ||
| compress(request, response, () => { | ||
| response.setHeader('content-type', 'text/html'); | ||
| try { | ||
| response.write(BODY.slice(0, 2048)); | ||
| response.end(BODY.slice(2048)); | ||
| } catch (error) { | ||
| onError?.(error as Error); | ||
| response.stream.close(); | ||
| } | ||
| }); | ||
| }; | ||
| } | ||
|
|
||
| describe('adaptHttp2ForNext', () => { | ||
| describe('over HTTP/2', () => { | ||
| let observed: { hasOwnProperty: string; ownAuthority: boolean; host: string; authority: string } | undefined; | ||
| let unadaptedHasOwnProperty: string | undefined; | ||
| let unadaptedHost: string | undefined; | ||
| let unadaptedError: Error | undefined; | ||
| const servers: Http2Server[] = []; | ||
| let headersURL: string; | ||
| let adaptedURL: string; | ||
| let unadaptedURL: string; | ||
|
|
||
| before(async () => { | ||
| const headers = await listenHttp2((request, response) => { | ||
| adaptHttp2ForNext(request, response); | ||
| observed = { | ||
| hasOwnProperty: typeof request.headers.hasOwnProperty, | ||
| ownAuthority: request.headers.hasOwnProperty(':authority'), | ||
| host: request.headers.host, | ||
| authority: request.headers[':authority'], | ||
| }; | ||
| response.end(); | ||
| }); | ||
| const adapted = await listenHttp2(compressedHandler(true)); | ||
| const unadapted = await listenHttp2((request, response) => { | ||
| unadaptedHasOwnProperty = typeof request.headers.hasOwnProperty; | ||
| unadaptedHost = request.headers.host; | ||
| compressedHandler(false, (error) => (unadaptedError = error))(request, response); | ||
| }); | ||
| servers.push(headers.server, adapted.server, unadapted.server); | ||
| headersURL = headers.url; | ||
| adaptedURL = adapted.url; | ||
| unadaptedURL = unadapted.url; | ||
| }); | ||
|
|
||
| after(() => { | ||
| for (const server of servers) server.close(); | ||
| }); | ||
|
|
||
| it('gives request headers the Object prototype Next.js calls hasOwnProperty on', async () => { | ||
| const response = await getHttp2(headersURL); | ||
| assert.strictEqual(response.status, 200); | ||
| assert.strictEqual(observed?.hasOwnProperty, 'function'); | ||
| assert.strictEqual(observed?.ownAuthority, true); | ||
| }); | ||
|
|
||
| it('fills host from :authority, which HTTP/2 sends in its place', async () => { | ||
| await getHttp2(headersURL); | ||
| assert.ok(observed?.authority); | ||
| assert.strictEqual(observed?.host, observed?.authority); | ||
| }); | ||
|
|
||
| it('keeps a host header the client sent', async () => { | ||
| await getHttp2(headersURL, { host: 'example.test' }); | ||
| assert.strictEqual(observed?.host, 'example.test'); | ||
| }); | ||
|
|
||
| it("lets Next.js's compression middleware write a gzipped response", async () => { | ||
| const response = await getHttp2(adaptedURL, { 'accept-encoding': 'gzip' }); | ||
| assert.strictEqual(response.status, 200); | ||
| assert.strictEqual(response.headers['content-encoding'], 'gzip'); | ||
| assert.strictEqual(gunzipSync(response.body).toString(), BODY); | ||
| }); | ||
|
|
||
| it('lets the compression middleware write an uncompressed response', async () => { | ||
| const response = await getHttp2(adaptedURL); | ||
| assert.strictEqual(response.status, 200); | ||
| assert.strictEqual(response.headers['content-encoding'], undefined); | ||
| assert.strictEqual(response.body.toString(), BODY); | ||
| }); | ||
|
|
||
| it('is needed: unadapted, headers lack hasOwnProperty and host, and the compression middleware throws', async () => { | ||
| await getHttp2(unadaptedURL, { 'accept-encoding': 'gzip' }); | ||
| assert.strictEqual(unadaptedHasOwnProperty, 'undefined'); | ||
| assert.strictEqual(unadaptedHost, undefined); | ||
| assert.match(unadaptedError?.message ?? '', /_implicitHeader is not a function/); | ||
| }); | ||
|
|
||
| it('does not throw when the client has already reset the stream', async () => { | ||
| let outcome: string | undefined; | ||
| const { server, url } = await listenHttp2((request, response) => { | ||
| adaptHttp2ForNext(request, response); | ||
| request.stream.on('close', () => { | ||
| try { | ||
| response._implicitHeader(); | ||
| outcome = 'ok'; | ||
| } catch (error) { | ||
| outcome = (error as NodeJS.ErrnoException).code; | ||
| } | ||
| }); | ||
| }); | ||
| try { | ||
| const client = connect(url); | ||
| const stream = client.request({ ':path': '/' }); | ||
| stream.on('error', () => {}); | ||
| await new Promise<void>((resolve) => { | ||
| stream.on('close', resolve); | ||
| setTimeout(() => stream.close(constants.NGHTTP2_CANCEL), 20); | ||
| }); | ||
| client.close(); | ||
| await new Promise((resolve) => setTimeout(resolve, 50)); | ||
| assert.strictEqual(outcome, 'ok'); | ||
| } finally { | ||
| server.close(); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| it('leaves HTTP/1 requests untouched', async () => { | ||
| let observed: { headersProto: object | null; implicitHeader: unknown } | undefined; | ||
| const server = createHttpServer((request: IncomingMessage, response: ServerResponse) => { | ||
| const ownBefore = Object.hasOwn(response, '_implicitHeader'); | ||
| adaptHttp2ForNext(request, response); | ||
| observed = { | ||
| headersProto: Object.getPrototypeOf(request.headers), | ||
| implicitHeader: Object.hasOwn(response, '_implicitHeader') !== ownBefore, | ||
| }; | ||
| response.end(); | ||
| }); | ||
| await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve)); | ||
| try { | ||
| const { port } = server.address() as AddressInfo; | ||
| await new Promise<void>((resolve, reject) => | ||
| httpRequest({ host: '127.0.0.1', port }, (response) => response.resume().on('end', resolve)) | ||
| .on('error', reject) | ||
| .end() | ||
| ); | ||
| assert.deepStrictEqual(observed, { headersProto: Object.prototype, implicitHeader: false }); | ||
| } finally { | ||
| server.close(); | ||
| } | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import { Http2ServerRequest, type Http2ServerResponse } from 'node:http2'; | ||
| import type { IncomingMessage, ServerResponse } from 'node:http'; | ||
|
|
||
| /** | ||
| * Next.js's request handler assumes Node's HTTP/1 objects; Harper's HTTP/2 port hands it `node:http2` | ||
| * compatibility objects instead. Next.js calls `headers.hasOwnProperty()` (absent on HTTP/2's | ||
| * null-prototype headers), reads `host` (HTTP/2 sends `:authority`), and its bundled `compression` | ||
| * calls `res._implicitHeader()` (absent on `Http2ServerResponse`). HTTP/1 requests are left untouched. | ||
| */ | ||
| export function adaptHttp2ForNext( | ||
| request: IncomingMessage | Http2ServerRequest, | ||
| response: ServerResponse | Http2ServerResponse | ||
| ): void { | ||
| if (!(request instanceof Http2ServerRequest)) return; | ||
|
|
||
| const headers = request.headers; | ||
| if (Object.getPrototypeOf(headers) === null) Object.setPrototypeOf(headers, Object.prototype); | ||
| headers.host ??= headers[':authority']; | ||
|
jjohnson-hdb marked this conversation as resolved.
|
||
|
|
||
| const http2Response = response as Http2ServerResponse & { _implicitHeader?: () => void }; | ||
| if (typeof http2Response._implicitHeader !== 'function') { | ||
| http2Response._implicitHeader = implicitHeader; | ||
| } | ||
| } | ||
|
|
||
| // `compression` calls this on every write because `Http2ServerResponse` never sets `_header`. | ||
| function implicitHeader(this: Http2ServerResponse): void { | ||
| if (!this.headersSent) this.writeHead(this.statusCode); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.