-
-
Notifications
You must be signed in to change notification settings - Fork 18
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
aleclarson
wants to merge
7
commits into
hattipjs:main
Choose a base branch
from
aleclarson:feat/adapter-node-http2
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.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
354859c
feat(adapter-node): add http2 support
aleclarson 573d152
fix incorrect re-export
aleclarson 45ce9c8
socket is always there
aleclarson eb9b1f2
add missing descriptions
aleclarson 09373f0
feat: update static middleware to support http2
aleclarson f49f180
set moduleResolution=bundler in testbed/basic/tsconfig.json
aleclarson f22ea93
add test for http2 node adapter
aleclarson 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 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 |
---|---|---|
@@ -1,5 +1,9 @@ | ||
{ | ||
"typescript.tsdk": "node_modules/typescript/lib", | ||
"editor.formatOnSave": true, | ||
"[typescript]": { | ||
"editor.defaultFormatter": "esbenp.prettier-vscode" | ||
}, | ||
"deno.enablePaths": ["./_deno"], | ||
"deno.enable": false | ||
} |
This file contains 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 was deleted.
Oops, something went wrong.
This file contains 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,28 @@ | ||
import type { HattipHandler } from "@hattip/core"; | ||
import * as http from "node:http"; | ||
import { NodeAdapterOptions } from "../types"; | ||
import { NodePlatformInfo, Server, ServerOptions } from "../types/http"; | ||
import { createMiddleware } from "../middleware"; | ||
|
||
/** | ||
* Create an HTTP/1.1 server | ||
*/ | ||
export function createServer( | ||
handler: HattipHandler<NodePlatformInfo>, | ||
adapterOptions?: NodeAdapterOptions, | ||
serverOptions?: ServerOptions, | ||
): Server { | ||
const listener = createMiddleware(handler, adapterOptions); | ||
return serverOptions | ||
? http.createServer(serverOptions, listener) | ||
: http.createServer(listener); | ||
} | ||
|
||
export { createMiddleware } from "../middleware"; | ||
|
||
export type { NodeAdapterOptions } from "../types"; | ||
export type { | ||
DecoratedRequest, | ||
NodeMiddleware, | ||
NodePlatformInfo, | ||
} from "../types/http"; |
This file contains 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,3 @@ | ||
import "../polyfills/node-fetch"; | ||
|
||
export * from "./common"; |
This file contains 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,3 @@ | ||
import "../polyfills/native-fetch"; | ||
|
||
export * from "./common"; |
This file contains 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,3 @@ | ||
import "../polyfills/whatwg-node"; | ||
|
||
export * from "./common"; |
This file contains 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,37 @@ | ||
import type { HattipHandler } from "@hattip/core"; | ||
import * as http2 from "node:http2"; | ||
import { NodeAdapterOptions } from "../types"; | ||
import { | ||
IncomingMessage, | ||
NodePlatformInfo, | ||
Server, | ||
ServerOptions, | ||
ServerResponse, | ||
} from "../types/http2"; | ||
import * as hattip from "../middleware"; | ||
|
||
/** | ||
* Create an HTTP/2 server | ||
*/ | ||
export function createServer( | ||
handler: HattipHandler<NodePlatformInfo>, | ||
adapterOptions?: NodeAdapterOptions, | ||
serverOptions?: ServerOptions, | ||
): Server { | ||
const listener = hattip.createMiddleware(handler, adapterOptions); | ||
return serverOptions | ||
? http2.createServer(serverOptions, listener) | ||
: http2.createServer(listener); | ||
} | ||
|
||
export const createMiddleware = hattip.createMiddleware< | ||
IncomingMessage, | ||
ServerResponse | ||
>; | ||
|
||
export type { NodeAdapterOptions } from "../types"; | ||
export type { | ||
DecoratedRequest, | ||
NodeMiddleware, | ||
NodePlatformInfo, | ||
} from "../types/http2"; |
This file contains 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,3 @@ | ||
import "../polyfills/node-fetch"; | ||
|
||
export * from "./common"; |
This file contains 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,3 @@ | ||
import "../polyfills/native-fetch"; | ||
|
||
export * from "./common"; |
This file contains 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,3 @@ | ||
import "../polyfills/whatwg-node"; | ||
|
||
export * from "./common"; |
This file was deleted.
Oops, something went wrong.
This file contains 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,85 @@ | ||
import type { AdapterRequestContext, HattipHandler } from "@hattip/core"; | ||
import process from "node:process"; | ||
import { createRequestAdapter } from "./request"; | ||
import { sendResponse } from "./response"; | ||
import { NodeMiddleware, NodePlatformInfo } from "./types/common"; | ||
import { IncomingMessage, NodeAdapterOptions, ServerResponse } from "./types"; | ||
import type * as http from "./types/http"; | ||
|
||
/** | ||
* Creates a request handler to be passed to http.createServer() or used as a | ||
* middleware in Connect-style frameworks like Express. | ||
*/ | ||
export function createMiddleware< | ||
NodeRequest extends IncomingMessage = http.IncomingMessage, | ||
NodeResponse extends ServerResponse = http.ServerResponse, | ||
>( | ||
handler: HattipHandler<NodePlatformInfo<NodeRequest, NodeResponse>>, | ||
options: NodeAdapterOptions = {}, | ||
): NodeMiddleware<NodeRequest, NodeResponse> { | ||
const { alwaysCallNext = true, ...requestOptions } = options; | ||
|
||
const requestAdapter = createRequestAdapter(requestOptions); | ||
|
||
return async (req, res, next) => { | ||
try { | ||
const [request, ip] = requestAdapter(req, res); | ||
|
||
let passThroughCalled = false; | ||
|
||
const context: AdapterRequestContext< | ||
NodePlatformInfo<NodeRequest, NodeResponse> | ||
> = { | ||
request, | ||
|
||
ip, | ||
|
||
env(variable) { | ||
return process.env[variable]; | ||
}, | ||
|
||
waitUntil(promise) { | ||
// Do nothing | ||
void promise; | ||
}, | ||
|
||
passThrough() { | ||
passThroughCalled = true; | ||
}, | ||
|
||
platform: { | ||
name: "node", | ||
request: req, | ||
response: res, | ||
}, | ||
}; | ||
|
||
const response = await handler(context); | ||
|
||
if (passThroughCalled && next) { | ||
next(); | ||
return; | ||
} | ||
|
||
await sendResponse(req, res, response); | ||
|
||
if (next && alwaysCallNext) { | ||
next(); | ||
} | ||
} catch (error) { | ||
if (next) { | ||
next(error); | ||
} else { | ||
console.error(error); | ||
|
||
if (!res.headersSent) { | ||
res.statusCode = 500; | ||
} | ||
|
||
if (!res.writableEnded) { | ||
res.end(); | ||
} | ||
} | ||
} | ||
}; | ||
} |
This file contains 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 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,7 @@ | ||
import installNodeFetch from "@hattip/polyfills/node-fetch"; | ||
import installGetSetCookie from "@hattip/polyfills/get-set-cookie"; | ||
import installCrypto from "@hattip/polyfills/crypto"; | ||
|
||
installNodeFetch(); | ||
installGetSetCookie(); | ||
installCrypto(); |
This file contains 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,8 @@ | ||
// TODO: Remove or update this rule! | ||
import installCrypto from "@hattip/polyfills/crypto"; | ||
import installGetSetCookie from "@hattip/polyfills/get-set-cookie"; | ||
import installWhatwgNodeFetch from "@hattip/polyfills/whatwg-node"; | ||
|
||
installWhatwgNodeFetch(); | ||
installGetSetCookie(); | ||
installCrypto(); |
Oops, something went wrong.
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.
There was a problem hiding this comment.
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.