Repository navigation
Expand file tree
/
Copy pathserver.mjs
More file actions
269 lines (246 loc) · 7.79 KB
/
Copy pathserver.mjs
File metadata and controls
269 lines (246 loc) · 7.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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import { createServer } from "node:http";
import { join } from "node:path";
import { pathToFileURL } from "node:url";
import next from "next";
import { negotiatesOnAccept } from "./lib/content-negotiation.mjs";
import {
createPrecompressedAssetHandler,
loadPrecompressedAssets,
} from "./lib/precompressed-assets.mjs";
import { normalizeOriginVary } from "./lib/vary.mjs";
import { DRAIN_SIGNAL, isDraining, startDraining } from "./server-drain.mjs";
const DEFAULT_PORT = 3000;
const FORCE_CLOSE_MS = 15_000;
export { negotiatesOnAccept };
/** @param {string[]} args */
function portFrom(args) {
const flagIndex = args.findIndex(
(value) => value === "-p" || value === "--port",
);
const value =
flagIndex >= 0 ? args[flagIndex + 1] : process.env.PORT || DEFAULT_PORT;
const port = Number.parseInt(String(value), 10);
if (!Number.isInteger(port) || port < 1 || port > 65_535) {
throw new Error(`Invalid server port: ${value}`);
}
return port;
}
/**
* @param {import("node:http").OutgoingHttpHeaders | import("node:http").OutgoingHttpHeader[]} headers
* @param {string | number | readonly string[] | undefined} existing
* @param {boolean} accept
*/
function normalizeWriteHeadVary(headers, existing, accept) {
if (Array.isArray(headers)) {
const merged = [];
const varyValues = [existing];
let varyIndex = -1;
for (let index = 0; index < headers.length; index += 2) {
if (String(headers[index]).toLowerCase() === "vary") {
if (varyIndex < 0) varyIndex = merged.length;
varyValues.push(headers[index + 1]);
} else {
merged.push(headers[index], headers[index + 1]);
}
}
const vary = normalizeOriginVary(varyValues, { accept });
if (vary) {
merged.splice(varyIndex < 0 ? merged.length : varyIndex, 0, "Vary", vary);
}
return merged;
}
const merged = { ...headers };
const keys = Object.keys(merged).filter(
(name) => name.toLowerCase() === "vary",
);
const vary = normalizeOriginVary(
[existing, ...keys.map((key) => merged[key])],
{ accept },
);
for (const key of keys) delete merged[key];
if (vary) merged.Vary = vary;
return merged;
}
/**
* Next's renderer can replace earlier Vary values. Normalize the header at the
* final Node response boundary. Cloudflare owns compression, so the origin
* must not emit Accept-Encoding variance.
*
* @param {import("node:http").IncomingMessage} request
* @param {import("node:http").ServerResponse} response
*/
export function installFinalVariance(request, response) {
const accept = negotiatesOnAccept(request.url);
const setHeader = response.setHeader;
response.setHeader = function setNormalizedHeader(name, value) {
if (String(name).toLowerCase() !== "vary") {
return setHeader.call(this, name, value);
}
const vary = normalizeOriginVary(value, { accept });
if (!vary) {
this.removeHeader(name);
return this;
}
return setHeader.call(this, name, vary);
};
const writeHead = response.writeHead;
response.writeHead = function writeNormalizedHead(
statusCode,
statusMessageOrHeaders,
headers,
) {
const currentVary = normalizeOriginVary(this.getHeader("Vary"), {
accept,
});
if (currentVary) setHeader.call(this, "Vary", currentVary);
else this.removeHeader("Vary");
if (typeof statusMessageOrHeaders === "string") {
return writeHead.call(
this,
statusCode,
statusMessageOrHeaders,
headers
? normalizeWriteHeadVary(headers, currentVary, accept)
: undefined,
);
}
return writeHead.call(
this,
statusCode,
statusMessageOrHeaders
? normalizeWriteHeadVary(statusMessageOrHeaders, currentVary, accept)
: undefined,
);
};
if (accept) response.setHeader("Vary", "Accept");
}
/** @param {string | undefined} value */
export function drainMsFrom(value) {
if (value === undefined || value === "") return 0;
const drainMs = Number(value);
if (!Number.isSafeInteger(drainMs) || drainMs < 0) {
throw new Error(`Invalid DOCS_SHUTDOWN_DRAIN_MS: ${value}`);
}
return drainMs;
}
/**
* Stops the server without cutting requests. The drain signal makes
* /api/ready fail; after `drainMs`, once Docker has marked the container
* unhealthy and Traefik has stopped routing to it, the server stops accepting
* connections and exits when its requests finish. SIGTERM and SIGINT skip the
* drain. Requests still open `forceMs` after the close are cut.
*
* @param {{
* server: import("node:http").Server,
* closeApp?: () => unknown,
* drainMs: number,
* forceMs?: number,
* signals?: Pick<NodeJS.EventEmitter, "on">,
* exit?: (code: number) => void,
* log?: (message: string) => void,
* }} options
*/
export function installGracefulShutdown({
server,
closeApp,
drainMs,
forceMs = FORCE_CLOSE_MS,
signals = process,
exit = (code) => process.exit(code),
log = console.log,
}) {
let closing = false;
function close(reason) {
if (closing) return;
closing = true;
log(`> Closing (${reason})`);
const force = setTimeout(() => {
log("> Closing: requests still open, cutting them");
server.closeAllConnections();
exit(1);
}, forceMs);
force.unref();
server.close(() => {
Promise.resolve()
.then(() => closeApp?.())
.catch((error) => console.error(error))
.finally(() => {
clearTimeout(force);
exit(0);
});
});
}
signals.on(DRAIN_SIGNAL, () => {
if (closing || isDraining()) return;
startDraining();
log(`> Draining for ${drainMs} ms`);
setTimeout(() => close("drained"), drainMs);
});
for (const signal of ["SIGTERM", "SIGINT"]) {
signals.on(signal, () => close(signal));
}
return {
/** Keep-alive sockets would otherwise hold the closing server open. */
isClosing: () => closing,
};
}
/**
* The server's request handler. Every response names the image's commit,
* which deploys wait for, and Vary is normalized. `serveAsset` answers first
* when it can (see createPrecompressedAssetHandler).
*
* @param {{
* handle: (request: import("node:http").IncomingMessage, response: import("node:http").ServerResponse) => unknown,
* isClosing: () => boolean,
* version?: string,
* serveAsset?: (request: import("node:http").IncomingMessage, response: import("node:http").ServerResponse) => boolean,
* }} options
*/
export function createRequestHandler({
handle,
isClosing,
version,
serveAsset,
}) {
return function handleRequest(request, response) {
if (isClosing()) response.shouldKeepAlive = false;
if (version) response.setHeader("X-App-Version", version);
installFinalVariance(request, response);
if (serveAsset?.(request, response)) return;
handle(request, response);
};
}
async function start() {
const port = portFrom(process.argv.slice(2));
const hostname = process.env.LPM_DOCS_HOST || "0.0.0.0";
const drainMs = drainMsFrom(process.env.DOCS_SHUTDOWN_DRAIN_MS);
// The commit the image was built from; deploys wait until it's served.
const version = process.env.APP_VERSION;
const app = next({ dev: false, hostname, port });
const [assets] = await Promise.all([
loadPrecompressedAssets(join(process.cwd(), ".next", "static")),
app.prepare(),
]);
const server = createServer(
createRequestHandler({
handle: app.getRequestHandler(),
isClosing: () => shutdown.isClosing(),
version,
serveAsset: createPrecompressedAssetHandler(assets),
}),
);
const shutdown = installGracefulShutdown({
server,
closeApp: () => app.close(),
drainMs,
});
server.listen(port, hostname, () => {
console.log(`> Ready on http://${hostname}:${port}`);
});
}
if (
process.argv[1] &&
import.meta.url === pathToFileURL(process.argv[1]).href
) {
await start();
}