Skip to content
This repository was archived by the owner on Feb 23, 2025. It is now read-only.

Commit

Permalink
chore: replace deprecated vercel entrypoint
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed May 1, 2023
1 parent 1d3605a commit d9960eb
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
46 changes: 46 additions & 0 deletions app/misc/vercel-entrypoint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import type { IncomingMessage, ServerResponse } from "node:http";
import { tinyassert } from "@hiogawa/utils";
import * as build from "@remix-run/dev/server-build";
import { createRequestHandler } from "@remix-run/express";

// borrow "@remix-run/express" adapter for vercel serverless deployment
// since "@remix-run/vercel" is going to deprecate (https://github.com/remix-run/remix/pull/5964)
// in favor of vercel's official integration (https://github.com/vercel/vercel/blob/da046d85de8121e6c851af8e51aff81984646e0b/packages/remix/defaults/server-node.mjs)

const handleRequest = createRequestHandler({ build });

export default async function vercelEntrypoint(
req: IncomingMessage,
res: ServerResponse
): Promise<void> {
// a few patches to fake express style Request/Response
// https://github.com/remix-run/remix/blame/71e94aa8a84cd9b63669dc9d826cbcbb0d2195a1/packages/remix-express/server.ts
// https://github.com/remix-run/remix/blame/71e94aa8a84cd9b63669dc9d826cbcbb0d2195a1/packages/remix-vercel/server.ts

const req2 = req as any;
const res2 = res as any;

req2.get = (arg: unknown) => {
tinyassert(arg === "host");
return req.headers["x-forwarded-host"] || req.headers["host"];
};

req2.protocol = req.headers["x-forwarded-proto"] || "https";

res2.status = (arg: unknown) => {
tinyassert(typeof arg === "number");
res.statusCode = arg;
};

res2.append = (k: unknown, v: unknown) => {
tinyassert(typeof k === "string");
tinyassert(Array.isArray(v));
res.setHeader(k, v);
};

let error: unknown;
await handleRequest(req2, res2, (e) => (error = e));
if (error) {
throw error;
}
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"@hiogawa/utils": "1.4.2-pre.4",
"@hiogawa/utils-react": "1.2.1-pre.4",
"@js-temporal/polyfill": "^0.4.3",
"@remix-run/express": "1.15.0",
"@remix-run/node": "1.15.0",
"@remix-run/react": "1.15.0",
"@remix-run/server-runtime": "1.15.0",
Expand Down Expand Up @@ -82,6 +83,7 @@
"@remix-run/serve": "1.15.0",
"@tsconfig/strictest": "^1.0.2",
"@types/bcryptjs": "^2.4.2",
"@types/express": "^4.17.17",
"@types/fs-extra": "^9.0.13",
"@types/node": "^16",
"@types/qs": "^6.9.7",
Expand Down
63 changes: 63 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d9960eb

Please sign in to comment.