Skip to content
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

feat(adapter-node): add http2 support #185

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

aleclarson
Copy link
Contributor

Added support for node:http2. I decided not to create a new adapter-node-http2 package, since they share a lot of code.

import { createServer } from '@hattip/adapter-node/http2'

Same exact API as @hattip/adapter-node HTTP/1.1 API.

Other entry points include:

  • @hattip/adapter-node/http2/native-fetch
  • @hattip/adapter-node/http2/whatwg-node

No tests have been added yet.

Comment on lines +3 to +6
"editor.formatOnSave": true,
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added this out of convenience.

Comment on lines +133 to +143
type GenericResponse = {
write(chunk: Uint8Array): boolean;
once(event: "drain", listener: () => void): void;
once(event: "error", listener: (err: unknown) => void): void;
off(event: "drain", listener: () => void): void;
off(event: "error", listener: (err: unknown) => void): void;
};

async function writeAndAwait(
chunk: Uint8Array,
res: ServerResponse,
res: GenericResponse,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Needed because write methods of http.ServerResponse and http2.Http2ServerResponse are not compatible according to the type-checker, even though they are when called without a callback.

Copy link
Contributor Author

@aleclarson aleclarson Nov 13, 2024

Choose a reason for hiding this comment

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

This file is used by functions that want to support both node:http and node:http2. For example, createRequestAdapter and sendResponse use these types.

@cyco130
Copy link
Member

cyco130 commented Nov 13, 2024

Looks great!

export interface DecoratedRequest extends Omit<IncomingMessage, "socket"> {
ip?: string;
protocol?: string;
socket: PossiblyEncryptedSocket;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This property was non-optional, but I saw another DecoratedRequest type in src/request.ts had it set as optional. Which one is right?

Copy link
Member

@cyco130 cyco130 Nov 13, 2024

Choose a reason for hiding this comment

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

It's not optional, req.socket is always defined.

What I'm trying to achieve is that there's a badly documented req.socket.encrypted property that only exists when using node:https. The adapter uses it to infer the protocol (https or http) part of the URL when origin isn't manually set and trustProxy is false. (req.protocol is a non-standard Express property, useful when embedding a Hattip app into an Express app as a middleware).

I think the one in src/request.ts is just an error on my part.

@aleclarson
Copy link
Contributor Author

aleclarson commented Nov 22, 2024

The test will fail since Node only supports HTTP/2 over TLS.

Also, it appears that native fetch in Node does not support HTTP/2 yet? nodejs/undici#2750

I don't know when I'll be able to investigate a solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants