-
Notifications
You must be signed in to change notification settings - Fork 210
/
Copy pathindex.ts
415 lines (383 loc) · 15.2 KB
/
index.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
// noinspection ES6ConvertVarToLetConst
import assert from "assert";
import http, { OutgoingHttpHeaders } from "http";
import https from "https";
import { Transform, Writable } from "stream";
import { ReadableStream } from "stream/web";
import { URL } from "url";
import zlib from "zlib";
import {
CorePluginSignatures,
MiniflareCore,
Request,
Response,
logResponse,
} from "@miniflare/core";
import { prefixError, randomHex } from "@miniflare/shared";
import { coupleWebSocket } from "@miniflare/web-sockets";
import { BodyInit, Headers } from "undici";
import { getAccessibleHosts } from "./helpers";
import { HTTPPlugin, RequestMeta } from "./plugin";
export * from "./helpers";
export * from "./plugin";
export const DEFAULT_PORT = 8787;
export type HTTPPluginSignatures = CorePluginSignatures & {
HTTPPlugin: typeof HTTPPlugin;
};
const liveReloadScript = `<script defer type="application/javascript">
(function () {
// Miniflare Live Reload
var url = new URL("/cdn-cgi/mf/reload", location.origin);
url.protocol = url.protocol.replace("http", "ws");
function reload() { location.reload(); }
function connect(reconnected) {
var ws = new WebSocket(url);
if (reconnected) ws.onopen = reload;
ws.onclose = function(e) {
e.code === 1012 ? reload() : e.code === 1000 || e.code === 1001 || setTimeout(connect, 1000, true);
}
}
connect();
})();
</script>`;
const liveReloadScriptLength = Buffer.byteLength(liveReloadScript);
export async function convertNodeRequest(
req: http.IncomingMessage,
meta?: RequestMeta
): Promise<{ request: Request; url: URL }> {
// @ts-expect-error encrypted is only defined in tls.TLSSocket
const protocol = req.socket.encrypted ? "https" : "http";
const origin = `${protocol}://${req.headers.host ?? "localhost"}`;
const url = new URL(req.url ?? "", origin);
let body: BodyInit | null = null;
if (req.method !== "GET" && req.method !== "HEAD") {
// Adapted from https://github.com/nodejs/undici/blob/ebea0f7084bb1efdb66c46409d1bfc87054b2870/lib/core/util.js#L269-L304
// to create a byte stream instead of a regular one. This means we don't
// create another "byte-TransformStream" later on to allow byob reads.
let iterator: AsyncIterableIterator<any>;
body = new ReadableStream({
type: "bytes",
start() {
iterator = req[Symbol.asyncIterator]();
},
async pull(controller) {
const { done, value } = await iterator.next();
if (done) {
queueMicrotask(() => controller.close());
} else {
const buffer = Buffer.isBuffer(value) ? value : Buffer.from(value);
controller.enqueue(new Uint8Array(buffer));
}
},
async cancel() {
await iterator.return?.();
},
});
}
// Add additional Cloudflare specific headers:
// https://support.cloudflare.com/hc/en-us/articles/200170986-How-does-Cloudflare-handle-HTTP-Request-headers-
const proto = meta?.forwardedProto ?? "https";
let ip = meta?.realIp ?? req.socket.remoteAddress ?? "";
// Convert IPv6 loopback address to IPv4 address
if (ip === "::1") ip = "127.0.0.1";
// Remove IPv6 prefix for IPv4 addresses
if (ip.startsWith("::ffff:")) ip = ip.substring("::ffff:".length);
// We're a bit naughty here mutating the incoming request, but this ensures
// the headers are included in the pretty-error page. If we used the new
// converted Request instance's headers, we wouldn't have connection, keep-
// alive, etc as we strip those. We need to take ownership of the request
// anyway though, since we're consuming its body.
req.headers["x-forwarded-proto"] ??= proto;
req.headers["x-real-ip"] ??= ip;
req.headers["cf-connecting-ip"] ??= ip;
req.headers["cf-ipcountry"] ??= meta?.cf?.country ?? "US";
req.headers["cf-ray"] ??= randomHex(16);
req.headers["cf-visitor"] ??= `{"scheme":"${proto}"}`;
req.headers["host"] = url.host;
// Build Headers object from request
const headers = new Headers();
for (const [name, values] of Object.entries(req.headers)) {
// These headers are unsupported in undici fetch requests, they're added
// automatically
if (
name === "transfer-encoding" ||
name === "connection" ||
name === "keep-alive" ||
name === "expect"
) {
continue;
}
if (Array.isArray(values)) {
for (const value of values) headers.append(name, value);
} else if (values !== undefined) {
headers.append(name, values);
}
}
// Create Request with additional Cloudflare specific properties:
// https://developers.cloudflare.com/workers/runtime-apis/request#incomingrequestcfproperties
const request = new Request(url, {
method: req.method,
headers,
body,
cf: meta?.cf,
});
return { request, url };
}
export type RequestListener = (
req: http.IncomingMessage,
res?: http.ServerResponse
) => Promise<Response | undefined>;
export function createRequestListener<Plugins extends HTTPPluginSignatures>(
mf: MiniflareCore<Plugins>
): RequestListener {
return async (req, res) => {
const { HTTPPlugin } = await mf.getPlugins();
const start = process.hrtime();
const { request, url } = await convertNodeRequest(
req,
await HTTPPlugin.getRequestMeta(req)
);
let response: Response | undefined;
let waitUntil: Promise<unknown[]> | undefined;
let status = 500;
// Check if path matches /cdn-cgi/* ignoring trailing slash. These paths
// can't be handled by workers and are used for utility interfaces.
const pathname = url.pathname.replace(/\/$/, "");
if (pathname.startsWith("/cdn-cgi/")) {
// TODO (someday): consider adding other utility interfaces for KV, DO, etc
// (maybe add another Plugin field/method/decorator for contributes)
if (pathname === "/cdn-cgi/mf/scheduled") {
req.method = "SCHD";
const time = url.searchParams.get("time");
const cron = url.searchParams.get("cron");
waitUntil = mf.dispatchScheduled(
time ? parseInt(time) : undefined,
cron ?? undefined
);
status = 200;
} else {
status = 404;
}
res?.writeHead(status, { "Content-Type": "text/plain; charset=UTF-8" });
res?.end();
} else {
try {
response = await mf.dispatchFetch(request);
waitUntil = response.waitUntil();
status = response.status;
const headers: OutgoingHttpHeaders = {};
// eslint-disable-next-line prefer-const
for (let [key, value] of response.headers) {
key = key.toLowerCase();
if (key === "set-cookie") {
// Multiple Set-Cookie headers should be treated as separate headers
// @ts-expect-error getAll is added to the Headers prototype by
// importing @miniflare/core
headers["set-cookie"] = response.headers.getAll("set-cookie");
} else {
headers[key] = value;
}
}
// If a Content-Encoding is set, and the user hasn't encoded the body,
// we're responsible for doing so.
const encoders: Transform[] = [];
if (headers["content-encoding"] && response.encodeBody === "auto") {
// Content-Length will be wrong as it's for the decoded length
delete headers["content-length"];
// Reverse of https://github.com/nodejs/undici/blob/48d9578f431cbbd6e74f77455ba92184f57096cf/lib/fetch/index.js#L1660
const codings = headers["content-encoding"]
.toString()
.toLowerCase()
.split(",")
.map((x) => x.trim());
for (const coding of codings) {
if (/(x-)?gzip/.test(coding)) {
encoders.push(zlib.createGzip());
} else if (/(x-)?deflate/.test(coding)) {
encoders.push(zlib.createDeflate());
} else if (coding === "br") {
encoders.push(zlib.createBrotliCompress());
} else {
// Unknown encoding, don't do any encoding at all
mf.log.warn(
`Unknown encoding \"${coding}\", sending plain response...`
);
delete headers["content-encoding"];
encoders.length = 0;
break;
}
}
}
// Add live reload script if enabled, this isn't an already encoded
// response, and it's HTML
const liveReloadEnabled =
HTTPPlugin.liveReload &&
response.encodeBody === "auto" &&
response.headers
.get("content-type")
?.toLowerCase()
.includes("text/html");
// If Content-Length is specified, and we're live-reloading, we'll
// need to adjust it to make room for the live reload script
const contentLength = response.headers.get("content-length");
if (liveReloadEnabled && contentLength !== null) {
const length = parseInt(contentLength);
if (!isNaN(length)) {
// Append length of live reload script
headers["content-length"] = length + liveReloadScriptLength;
}
}
res?.writeHead(status, headers);
// Response body may be null if empty
if (res) {
// `initialStream` is the stream we'll write the response to. It
// should end up as the first encoder, piping to the next encoder,
// and finally piping to the response:
//
// encoders[0] (initialStream) -> encoders[1] -> res
//
// Not using `pipeline(passThrough, ...encoders, res)` here as that
// gives a premature close error with server sent events. This also
// avoids creating an extra stream even when we're not encoding.
let initialStream: Writable = res;
for (let i = encoders.length - 1; i >= 0; i--) {
encoders[i].pipe(initialStream);
initialStream = encoders[i];
}
if (response.body) {
for await (const chunk of response.body) {
if (chunk) initialStream.write(chunk);
}
if (liveReloadEnabled) {
initialStream.write(liveReloadScript);
}
}
initialStream.end();
}
} catch (e: any) {
// MIME types aren't case sensitive
const accept = req.headers.accept?.toLowerCase() ?? "";
if (
accept.includes("text/html") ||
accept.includes("*/*") ||
accept.includes("text/*")
) {
// Send pretty HTML error page if client accepts it
const Youch: typeof import("youch").default = require("youch");
const youch = new Youch(e, req);
youch.addLink(() => {
const links = [
'<a href="https://developers.cloudflare.com/workers/" target="_blank" style="text-decoration:none">📚 Workers Docs</a>',
'<a href="https://discord.gg/cloudflaredev" target="_blank" style="text-decoration:none">💬 Workers Discord</a>',
'<a href="https://miniflare.dev" target="_blank" style="text-decoration:none">🔥 Miniflare Docs</a>',
];
// Live reload is basically a link right?
if (HTTPPlugin.liveReload) links.push(liveReloadScript);
return links.join("");
});
const errorHtml = await youch.toHTML();
res?.writeHead(500, { "Content-Type": "text/html; charset=UTF-8" });
res?.end(errorHtml, "utf8");
} else {
// Otherwise, send plaintext stack trace
res?.writeHead(500, { "Content-Type": "text/plain; charset=UTF-8" });
res?.end(e.stack, "utf8");
}
// Add method and URL to stack trace
mf.log.error(prefixError(`${req.method} ${req.url}`, e));
}
}
assert(req.method && req.url);
await logResponse(mf.log, {
start,
method: req.method,
url: req.url,
status,
waitUntil,
});
return response;
};
}
export async function createServer<Plugins extends HTTPPluginSignatures>(
mf: MiniflareCore<Plugins>,
options?: http.ServerOptions & https.ServerOptions
): Promise<http.Server | https.Server> {
const plugins = await mf.getPlugins();
const listener = createRequestListener(mf);
// Setup HTTP server
let server: http.Server | https.Server;
if (plugins.HTTPPlugin.httpsEnabled) {
const httpsOptions = plugins.HTTPPlugin.httpsOptions;
assert(httpsOptions);
server = https.createServer({ ...httpsOptions, ...options }, listener);
} else {
server = http.createServer(options ?? {}, listener);
}
const { WebSocketServer }: typeof import("ws") = require("ws");
// Setup WebSocket servers
const webSocketServer = new WebSocketServer({ noServer: true });
const liveReloadServer = new WebSocketServer({ noServer: true });
server.on("upgrade", async (request, socket, head) => {
// Only interested in pathname so base URL doesn't matter
const { pathname } = new URL(request.url ?? "", "http://localhost");
if (pathname === "/cdn-cgi/mf/reload") {
// If this is the for live-reload, handle the request ourselves
liveReloadServer.handleUpgrade(request, socket as any, head, (ws) => {
liveReloadServer.emit("connection", ws, request);
});
} else {
// Otherwise, handle the request in the worker
const response = await listener(request);
// Check web socket response was returned
const webSocket = response?.webSocket;
if (response?.status !== 101 || !webSocket) {
socket.write("HTTP/1.1 500 Internal Server Error\r\n\r\n");
socket.destroy();
mf.log.error(
new TypeError(
"Web Socket request did not return status 101 Switching Protocols response with Web Socket"
)
);
return;
}
// Accept and couple the Web Socket
webSocketServer.handleUpgrade(request, socket as any, head, (ws) => {
void coupleWebSocket(ws, webSocket);
webSocketServer.emit("connection", ws, request);
});
}
});
const reloadListener = () => {
// Reload all connected live reload clients
for (const ws of liveReloadServer.clients) {
ws.close(1012, "Service Restart");
}
// Close all existing web sockets on reload
for (const ws of webSocketServer.clients) {
ws.close(1012, "Service Restart");
}
};
mf.addEventListener("reload", reloadListener);
server.on("close", () => mf.removeEventListener("reload", reloadListener));
return server;
}
export async function startServer<Plugins extends HTTPPluginSignatures>(
mf: MiniflareCore<Plugins>,
options?: http.ServerOptions & https.ServerOptions
): Promise<http.Server | https.Server> {
const server = await createServer(mf, options);
const plugins = await mf.getPlugins();
const { httpsEnabled, host, port = DEFAULT_PORT } = plugins.HTTPPlugin;
return new Promise((resolve) => {
server.listen(port, host, () => {
const log = mf.log;
const protocol = httpsEnabled ? "https" : "http";
const accessibleHosts = host ? [host] : getAccessibleHosts(true);
log.info(`Listening on ${host ?? ""}:${port}`);
for (const accessibleHost of accessibleHosts) {
log.info(`- ${protocol}://${accessibleHost}:${port}`);
}
resolve(server);
});
});
}