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
Open
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
4 changes: 4 additions & 0 deletions .vscode/settings.json
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"
},
Comment on lines +3 to +6
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.

"deno.enablePaths": ["./_deno"],
"deno.enable": false
}
9 changes: 6 additions & 3 deletions packages/adapter/adapter-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
"dist"
],
"exports": {
".": "./dist/index.js",
"./native-fetch": "./dist/native-fetch.js",
"./whatwg-node": "./dist/whatwg-node.js",
".": "./dist/http/index.js",
"./native-fetch": "./dist/http/native-fetch.js",
"./whatwg-node": "./dist/http/whatwg-node.js",
"./http2": "./dist/http2/index.js",
"./http2/native-fetch": "./dist/http2/native-fetch.js",
"./http2/whatwg-node": "./dist/http2/whatwg-node.js",
"./request": "./dist/request.js",
"./response": "./dist/response.js",
"./fast-fetch": "./dist/fast-fetch.js"
Expand Down
135 changes: 0 additions & 135 deletions packages/adapter/adapter-node/src/common.ts

This file was deleted.

28 changes: 28 additions & 0 deletions packages/adapter/adapter-node/src/http/common.ts
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";
3 changes: 3 additions & 0 deletions packages/adapter/adapter-node/src/http/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import "../polyfills/node-fetch";

export * from "./common";
3 changes: 3 additions & 0 deletions packages/adapter/adapter-node/src/http/native-fetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import "../polyfills/native-fetch";

export * from "./common";
3 changes: 3 additions & 0 deletions packages/adapter/adapter-node/src/http/whatwg-node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import "../polyfills/whatwg-node";

export * from "./common";
37 changes: 37 additions & 0 deletions packages/adapter/adapter-node/src/http2/common.ts
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";
3 changes: 3 additions & 0 deletions packages/adapter/adapter-node/src/http2/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import "../polyfills/node-fetch";

export * from "./common";
3 changes: 3 additions & 0 deletions packages/adapter/adapter-node/src/http2/native-fetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import "../polyfills/native-fetch";

export * from "./common";
3 changes: 3 additions & 0 deletions packages/adapter/adapter-node/src/http2/whatwg-node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import "../polyfills/whatwg-node";

export * from "./common";
25 changes: 0 additions & 25 deletions packages/adapter/adapter-node/src/index.ts

This file was deleted.

85 changes: 85 additions & 0 deletions packages/adapter/adapter-node/src/middleware.ts
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();
}
}
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,3 @@ for (const key of Object.keys(webStream)) {
(globalThis as any)[key] = (webStream as any)[key];
}
}

export type {
DecoratedRequest,
NodeMiddleware,
NodeAdapterOptions,
NodePlatformInfo,
} from "./common";

export { createMiddleware, createServer } from "./common";
7 changes: 7 additions & 0 deletions packages/adapter/adapter-node/src/polyfills/node-fetch.ts
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();
8 changes: 8 additions & 0 deletions packages/adapter/adapter-node/src/polyfills/whatwg-node.ts
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();
Loading
Loading