-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
server.ts
135 lines (118 loc) · 3.42 KB
/
server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import type * as express from "express";
import type {
AppLoadContext,
ServerBuild,
RequestInit as NodeRequestInit,
Response as NodeResponse,
} from "@remix-run/node";
import {
AbortController as NodeAbortController,
createRequestHandler as createRemixRequestHandler,
Headers as NodeHeaders,
Request as NodeRequest,
writeReadableStreamToWritable,
} from "@remix-run/node";
/**
* A function that returns the value to use as `context` in route `loader` and
* `action` functions.
*
* You can think of this as an escape hatch that allows you to pass
* environment/platform-specific values through to your loader/action, such as
* values that are generated by Express middleware like `req.session`.
*/
export type GetLoadContextFunction = (
req: express.Request,
res: express.Response
) => AppLoadContext;
export type RequestHandler = (
req: express.Request,
res: express.Response,
next: express.NextFunction
) => Promise<void>;
/**
* Returns a request handler for Express that serves the response using Remix.
*/
export function createRequestHandler({
build,
getLoadContext,
mode = process.env.NODE_ENV,
}: {
build: ServerBuild;
getLoadContext?: GetLoadContextFunction;
mode?: string;
}): RequestHandler {
let handleRequest = createRemixRequestHandler(build, mode);
return async (
req: express.Request,
res: express.Response,
next: express.NextFunction
) => {
try {
let request = createRemixRequest(req, res);
let loadContext = getLoadContext?.(req, res);
let response = (await handleRequest(
request,
loadContext
)) as NodeResponse;
await sendRemixResponse(res, response);
} catch (error: unknown) {
// Express doesn't support async functions, so we have to pass along the
// error manually using next().
next(error);
}
};
}
export function createRemixHeaders(
requestHeaders: express.Request["headers"]
): NodeHeaders {
let headers = new NodeHeaders();
for (let [key, values] of Object.entries(requestHeaders)) {
if (values) {
if (Array.isArray(values)) {
for (let value of values) {
headers.append(key, value);
}
} else {
headers.set(key, values);
}
}
}
return headers;
}
export function createRemixRequest(
req: express.Request,
res: express.Response
): NodeRequest {
let url = new URL(`${req.protocol}://${req.get("host")}${req.url}`);
// Abort action/loaders once we can no longer write a response
let controller = new NodeAbortController();
res.on("close", () => controller.abort());
let init: NodeRequestInit = {
method: req.method,
headers: createRemixHeaders(req.headers),
// Cast until reason/throwIfAborted added
// https://github.com/mysticatea/abort-controller/issues/36
signal: controller.signal as NodeRequestInit["signal"],
};
if (req.method !== "GET" && req.method !== "HEAD") {
init.body = req;
}
return new NodeRequest(url.href, init);
}
export async function sendRemixResponse(
res: express.Response,
nodeResponse: NodeResponse
): Promise<void> {
res.statusMessage = nodeResponse.statusText;
res.status(nodeResponse.status);
for (let [key, values] of Object.entries(nodeResponse.headers.raw())) {
for (let value of values) {
res.append(key, value);
}
}
if (nodeResponse.body) {
await writeReadableStreamToWritable(nodeResponse.body, res);
} else {
res.end();
}
}