Repository navigation
Expand file tree
/
Copy pathserver.node.ts
More file actions
67 lines (55 loc) · 1.79 KB
/
Copy pathserver.node.ts
File metadata and controls
67 lines (55 loc) · 1.79 KB
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
import * as http from "node:http";
import { createRequestListener } from "remix/node-fetch-server";
import type { FetchHandler } from "remix/node-fetch-server";
import {
CDN_ORIGIN_SECRET_HEADER,
resolveNodeBuyerIp,
} from "./app/buyer-ip.ts";
import { app, closeNodeApp } from "./app/node.ts";
const isHmr = Boolean(
process.env.NODE_ENV === "development" && process.env.REMIX_NODE_HMR,
);
const hmrProxyPort = process.env.HMR_PROXY_PORT
? parsePort("HMR_PROXY_PORT", process.env.HMR_PROXY_PORT)
: null;
const port = parsePort("PORT", process.env.PORT ?? "44100");
const handler: FetchHandler = (request, client) => {
let buyerIp = resolveNodeBuyerIp(request, process.env, client.address);
// Shopify's generic API proxy forwards most request headers upstream.
request.headers.delete(CDN_ORIGIN_SECRET_HEADER);
return app.fetch(request, { buyerIp, env: process.env });
};
const server = http.createServer(
createRequestListener(handler, {
trustProxy: isHmr || process.env.TRUST_PROXY === "true",
}),
);
server.listen(port, () => {
if (isHmr) {
import("remix/node-hmr/runtime").then((nodeHmr) =>
nodeHmr.emitServerReady(),
);
}
console.log(`Server listening on http://localhost:${hmrProxyPort ?? port}`);
});
function parsePort(name: string, value: string): number {
let port = Number(value);
if (!Number.isInteger(port) || port <= 0 || port > 65_535) {
throw new Error(
`Invalid ${name} value "${value}". Expected a port from 1 to 65535.`,
);
}
return port;
}
let shuttingDown = false;
function shutdown() {
if (shuttingDown) return;
shuttingDown = true;
server.close(async (error) => {
await closeNodeApp();
process.exit(error ? 1 : 0);
});
server.closeAllConnections();
}
process.on("SIGINT", shutdown);
process.on("SIGTERM", shutdown);