/
proxy.ts
541 lines (494 loc) · 15.3 KB
/
proxy.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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
import { createServer as createHttpServer } from "node:http";
import { connect } from "node:http2";
import { createServer as createHttpsServer } from "node:https";
import https from "node:https";
import { networkInterfaces } from "node:os";
import { createHttpTerminator } from "http-terminator";
import { useEffect, useRef, useState } from "react";
import serveStatic from "serve-static";
import { getHttpsOptions } from "./https-options";
import { logger } from "./logger";
import type { CfPreviewToken } from "./create-worker-preview";
import type { HttpTerminator } from "http-terminator";
import type {
IncomingHttpHeaders,
RequestListener,
IncomingMessage,
ServerResponse,
Server as HttpServer,
} from "node:http";
import type { ClientHttp2Session, ServerHttp2Stream } from "node:http2";
import type { Server as HttpsServer } from "node:https";
import type { Duplex, Writable } from "node:stream";
/**
* `usePreviewServer` is a React hook that creates a local development
* server that can be used to develop a Worker.
*
* When we run `wrangler dev`, we start by uploading the compiled worker
* to the preview service, which responds with a preview token.
* (see `useWorker`/`createWorker` for details.)
* We can then use that token to connect to the preview server for a
* great local development experience. Further, as we change the worker,
* we can update the preview token transparently without having to restart
* the development server.
*/
/** Rewrite request headers to add the preview token. */
function addCfPreviewTokenHeader(
headers: IncomingHttpHeaders,
previewTokenValue: string
) {
headers["cf-workers-preview-token"] = previewTokenValue;
}
/**
* Rewrite references in request headers
* from the preview host to the local host.
*/
function rewriteRemoteHostToLocalHostInHeaders(
headers: IncomingHttpHeaders,
remoteHost: string,
localPort: number,
localProtocol: "https" | "http"
) {
for (const [name, value] of Object.entries(headers)) {
// Rewrite the remote host to the local host.
if (typeof value === "string" && value.includes(remoteHost)) {
headers[name] = value
.replaceAll(
`https://${remoteHost}`,
`${localProtocol}://localhost:${localPort}`
)
.replaceAll(remoteHost, `localhost:${localPort}`);
}
}
}
function writeHead(
socket: Writable,
res: Pick<
IncomingMessage,
"httpVersion" | "statusCode" | "statusMessage" | "headers"
>
) {
socket.write(
`HTTP/${res.httpVersion} ${res.statusCode} ${res.statusMessage}\r\n`
);
for (const [key, values] of Object.entries(res.headers)) {
if (Array.isArray(values)) {
for (const value of values) socket.write(`${key}: ${value}\r\n`);
} else {
socket.write(`${key}: ${values}\r\n`);
}
}
socket.write("\r\n");
}
type PreviewProxy = {
server: HttpServer | HttpsServer;
terminator: HttpTerminator;
};
export function usePreviewServer({
previewToken,
assetDirectory,
localProtocol,
localPort: port,
ip,
}: {
previewToken: CfPreviewToken | undefined;
assetDirectory: string | undefined;
localProtocol: "https" | "http";
localPort: number;
ip: string;
}) {
/** Creates an HTTP/1 proxy that sends requests over HTTP/2. */
const [proxy, setProxy] = useState<PreviewProxy>();
/**
* Create the instance of the local proxy server that will pass on
* requests to the preview worker.
*/
useEffect(() => {
if (proxy === undefined) {
createProxyServer(localProtocol)
.then((server) => {
setProxy({
server,
terminator: createHttpTerminator({
server,
gracefulTerminationTimeout: 0,
}),
});
})
.catch(async (err) => {
logger.error("Failed to create proxy server:", err);
});
}
}, [proxy, localProtocol]);
/**
* When we're not connected / getting a fresh token on changes,
* we'd like to buffer streams/requests until we're connected.
* Once connected, we can flush the buffered streams/requests.
* streamBufferRef is used to buffer http/2 streams, while
* requestResponseBufferRef is used to buffer http/1 requests.
*/
const streamBufferRef = useRef<
{ stream: ServerHttp2Stream; headers: IncomingHttpHeaders }[]
>([]);
const requestResponseBufferRef = useRef<
{ request: IncomingMessage; response: ServerResponse }[]
>([]);
/**
* The session doesn't last forever, and will eventually drop
* (usually within 5-15 minutes). When that happens, we simply
* restart the effect, effectively restarting the server. We use
* a state sigil as an effect dependency to do so.
*/
const [retryServerSetupSigil, setRetryServerSetupSigil] = useState<number>(0);
function retryServerSetup() {
setRetryServerSetupSigil((x) => x + 1);
}
useEffect(() => {
if (proxy === undefined) {
return;
}
// If we don't have a token, that means either we're just starting up,
// or we're refreshing the token.
if (!previewToken) {
const cleanupListeners: (() => void)[] = [];
const bufferStream = (
stream: ServerHttp2Stream,
headers: IncomingHttpHeaders
) => {
// store the stream in a buffer so we can replay it later
streamBufferRef.current.push({ stream, headers });
};
proxy.server.on("stream", bufferStream);
cleanupListeners.push(() => proxy.server.off("stream", bufferStream));
const bufferRequestResponse = (
request: IncomingMessage,
response: ServerResponse
) => {
// store the request and response in a buffer so we can replay it later
requestResponseBufferRef.current.push({ request, response });
};
proxy.server.on("request", bufferRequestResponse);
cleanupListeners.push(() =>
proxy.server.off("request", bufferRequestResponse)
);
return () => {
cleanupListeners.forEach((cleanup) => cleanup());
};
}
// We have a token. Let's proxy requests to the preview end point.
const cleanupListeners: (() => void)[] = [];
// create a ClientHttp2Session
logger.debug("PREVIEW URL:", `https://${previewToken.host}`);
const remote = connect(`https://${previewToken.host}`);
cleanupListeners.push(() => remote.destroy());
// As mentioned above, the session may die at any point,
// so we need to restart the effect.
remote.on("close", retryServerSetup);
cleanupListeners.push(() => remote.off("close", retryServerSetup));
/** HTTP/2 -> HTTP/2 */
const handleStream = createStreamHandler(
previewToken,
remote,
port,
localProtocol
);
proxy.server.on("stream", handleStream);
cleanupListeners.push(() => proxy.server.off("stream", handleStream));
// flush and replay buffered streams
streamBufferRef.current.forEach((buffer) =>
handleStream(buffer.stream, buffer.headers)
);
streamBufferRef.current = [];
/** HTTP/1 -> HTTP/2 */
const handleRequest: RequestListener = (
message: IncomingMessage,
response: ServerResponse
) => {
const { httpVersionMajor, headers, method, url } = message;
if (httpVersionMajor >= 2) {
return; // Already handled by the "stream" event.
}
addCfPreviewTokenHeader(headers, previewToken.value);
headers[":method"] = method;
headers[":path"] = url;
headers[":authority"] = previewToken.host;
headers[":scheme"] = "https";
for (const name of Object.keys(headers)) {
if (HTTP1_HEADERS.has(name.toLowerCase())) {
delete headers[name];
}
}
const request = message.pipe(remote.request(headers));
logger.debug(
"WORKER REQUEST",
new Date().toLocaleTimeString(),
method,
url
);
logger.debug("HEADERS", JSON.stringify(headers, null, 2));
logger.debug("PREVIEW TOKEN", previewToken);
request.on("response", (responseHeaders) => {
const status = responseHeaders[":status"] ?? 500;
// log all requests to terminal
logger.log(new Date().toLocaleTimeString(), method, url, status);
rewriteRemoteHostToLocalHostInHeaders(
responseHeaders,
previewToken.host,
port,
localProtocol
);
for (const name of Object.keys(responseHeaders)) {
if (name.startsWith(":")) {
delete responseHeaders[name];
}
}
response.writeHead(status, responseHeaders);
request.pipe(response, { end: true });
});
};
// If an asset path is defined, check the file system
// for a file first and serve if it exists.
const actualHandleRequest = assetDirectory
? createHandleAssetsRequest(assetDirectory, handleRequest)
: handleRequest;
proxy.server.on("request", actualHandleRequest);
cleanupListeners.push(() =>
proxy.server.off("request", actualHandleRequest)
);
// flush and replay buffered requests
requestResponseBufferRef.current.forEach(({ request, response }) =>
actualHandleRequest(request, response)
);
requestResponseBufferRef.current = [];
/** HTTP/1 -> WebSocket (over HTTP/1) */
const handleUpgrade = (
originalMessage: IncomingMessage,
originalSocket: Duplex,
originalHead: Buffer
) => {
const { headers, method, url } = originalMessage;
addCfPreviewTokenHeader(headers, previewToken.value);
headers["host"] = previewToken.host;
if (originalHead?.byteLength) originalSocket.unshift(originalHead);
const runtimeRequest = https.request(
{
hostname: previewToken.host,
path: url,
method,
headers,
},
(runtimeResponse) => {
if (!(runtimeResponse as { upgrade?: boolean }).upgrade) {
writeHead(originalSocket, runtimeResponse);
runtimeResponse.pipe(originalSocket);
}
}
);
runtimeRequest.on(
"upgrade",
(runtimeResponse, runtimeSocket, runtimeHead) => {
if (runtimeHead?.byteLength) runtimeSocket.unshift(runtimeHead);
writeHead(originalSocket, {
httpVersion: "1.1",
statusCode: 101,
statusMessage: "Switching Protocols",
headers: runtimeResponse.headers,
});
runtimeSocket.pipe(originalSocket).pipe(runtimeSocket);
}
);
originalMessage.pipe(runtimeRequest);
};
proxy.server.on("upgrade", handleUpgrade);
cleanupListeners.push(() => proxy.server.off("upgrade", handleUpgrade));
return () => {
cleanupListeners.forEach((cleanup) => cleanup());
};
}, [
previewToken,
assetDirectory,
port,
localProtocol,
proxy,
// We use a state value as a sigil to trigger reconnecting the server.
// It's not used inside the effect, so react-hooks/exhaustive-deps
// doesn't complain if it's not included in the dependency array.
// But its presence is critical, so Do NOT remove it from the dependency list.
retryServerSetupSigil,
]);
// Start/stop the server whenever the
// containing component is mounted/unmounted.
useEffect(() => {
const abortController = new AbortController();
if (proxy === undefined) {
return;
}
waitForPortToBeAvailable(port, {
retryPeriod: 200,
timeout: 2000,
abortSignal: abortController.signal,
})
.then(() => {
proxy.server.on("listening", () => {
const address = proxy.server.address();
const usedPort =
address && typeof address === "object" ? address.port : port;
logger.log(`⬣ Listening at ${localProtocol}://${ip}:${usedPort}`);
const accessibleHosts =
ip !== "0.0.0.0" ? [ip] : getAccessibleHosts();
for (const accessibleHost of accessibleHosts) {
logger.log(`- ${localProtocol}://${accessibleHost}:${usedPort}`);
}
});
proxy.server.listen(port, ip);
})
.catch((err) => {
if ((err as { code: string }).code !== "ABORT_ERR") {
logger.error(`Failed to start server: ${err}`);
}
});
return () => {
abortController.abort();
// Running `proxy.server.close()` does not close open connections, preventing the process from exiting.
// So we use this `terminator` to close all the connections and force the server to shutdown.
proxy.terminator
.terminate()
.catch(() => logger.error("Failed to terminate the proxy server."));
};
}, [port, ip, proxy, localProtocol]);
}
function createHandleAssetsRequest(
assetDirectory: string,
handleRequest: RequestListener
) {
const handleAsset = serveStatic(assetDirectory, {
cacheControl: false,
});
return (request: IncomingMessage, response: ServerResponse) => {
handleAsset(request, response, () => {
handleRequest(request, response);
});
};
}
/** A Set of headers we want to remove from HTTP/1 requests. */
const HTTP1_HEADERS = new Set([
"host",
"connection",
"upgrade",
"keep-alive",
"proxy-connection",
"transfer-encoding",
"http2-settings",
]);
async function createProxyServer(
localProtocol: "https" | "http"
): Promise<HttpServer | HttpsServer> {
const server: HttpServer | HttpsServer =
localProtocol === "https"
? createHttpsServer(await getHttpsOptions())
: createHttpServer();
return server
.on("upgrade", (req) => {
// log all websocket connections
logger.log(
new Date().toLocaleTimeString(),
req.method,
req.url,
101,
"(WebSocket)"
);
})
.on("error", (err) => {
// log all connection errors
logger.error(new Date().toLocaleTimeString(), err);
});
}
function createStreamHandler(
previewToken: CfPreviewToken,
remote: ClientHttp2Session,
localPort: number,
localProtocol: "https" | "http"
) {
return function handleStream(
stream: ServerHttp2Stream,
headers: IncomingHttpHeaders
) {
addCfPreviewTokenHeader(headers, previewToken.value);
headers[":authority"] = previewToken.host;
const request = stream.pipe(remote.request(headers));
request.on("response", (responseHeaders: IncomingHttpHeaders) => {
rewriteRemoteHostToLocalHostInHeaders(
responseHeaders,
previewToken.host,
localPort,
localProtocol
);
stream.respond(responseHeaders);
request.pipe(stream, { end: true });
});
};
}
/**
* A helper function that waits for a port to be available.
*/
export async function waitForPortToBeAvailable(
port: number,
options: { retryPeriod: number; timeout: number; abortSignal: AbortSignal }
): Promise<void> {
return new Promise((resolve, reject) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
options.abortSignal.addEventListener("abort", () => {
const abortError = new Error("waitForPortToBeAvailable() aborted");
(abortError as Error & { code: string }).code = "ABORT_ERR";
doReject(abortError);
});
const timeout = setTimeout(() => {
doReject(new Error(`Timed out waiting for port ${port}`));
}, options.timeout);
const interval = setInterval(checkPort, options.retryPeriod);
checkPort();
function doResolve() {
clearTimeout(timeout);
clearInterval(interval);
resolve();
}
function doReject(err: unknown) {
clearInterval(interval);
clearTimeout(timeout);
reject(err);
}
function checkPort() {
// Testing whether a port is 'available' involves simply
// trying to make a server listen on that port, and retrying
// until it succeeds.
const server = createHttpServer();
const terminator = createHttpTerminator({
server,
gracefulTerminationTimeout: 0, // default 1000
});
server.on("error", (err) => {
// @ts-expect-error non standard property on Error
if (err.code !== "EADDRINUSE") {
doReject(err);
}
});
server.listen(port, () =>
terminator
.terminate()
.then(doResolve, () =>
logger.error("Failed to terminate the port checker.")
)
);
}
});
}
function getAccessibleHosts(): string[] {
const hosts: string[] = [];
Object.values(networkInterfaces()).forEach((net) => {
net?.forEach(({ family, address }) => {
// @ts-expect-error the `family` property is numeric as of Node.js 18.0.0
if (family === "IPv4" || family === 4) hosts.push(address);
});
});
return hosts;
}