wrangler 0.0.0-e14e589 → 0.0.0-e928f94
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +51 -0
- package/package.json +22 -20
- package/src/__tests__/index.test.ts +5 -5
- package/src/__tests__/mock-cfetch.js +1 -1
- package/src/api/form_data.ts +35 -19
- package/src/api/preview.ts +28 -23
- package/src/api/worker.ts +24 -3
- package/src/config.ts +27 -1
- package/src/dev.tsx +77 -23
- package/src/dialogs.tsx +1 -1
- package/src/index.tsx +121 -17
- package/src/kv.tsx +2 -2
- package/src/module-collection.ts +64 -0
- package/src/pages.tsx +583 -80
- package/src/publish.ts +82 -14
- package/src/sites.tsx +1 -1
- package/src/tail.tsx +1 -1
- package/wrangler-dist/cli.js +124109 -508
- package/wrangler-dist/cli.js.map +3 -3
- package/src/util/fetch.ts +0 -74
package/src/pages.tsx
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import type { BuilderCallback } from "yargs";
|
|
2
2
|
import { join } from "path";
|
|
3
3
|
import { tmpdir } from "os";
|
|
4
|
-
import { existsSync } from "fs";
|
|
5
|
-
import type { ChildProcess } from "child_process";
|
|
4
|
+
import { existsSync, lstatSync, readFileSync } from "fs";
|
|
6
5
|
import { execSync, spawn } from "child_process";
|
|
7
|
-
import
|
|
6
|
+
import { Headers, Request, Response } from "undici";
|
|
8
7
|
import type { MiniflareOptions } from "miniflare";
|
|
9
8
|
import type { RequestInfo, RequestInit } from "@miniflare/core";
|
|
10
|
-
import
|
|
9
|
+
import open from "open";
|
|
10
|
+
import { watch } from "chokidar";
|
|
11
11
|
|
|
12
12
|
type Exit = (message?: string) => undefined;
|
|
13
13
|
|
|
@@ -73,41 +73,35 @@ const getPort = (pid: number) => {
|
|
|
73
73
|
};
|
|
74
74
|
|
|
75
75
|
const spawnProxyProcess = async ({
|
|
76
|
-
directory,
|
|
77
76
|
port,
|
|
78
|
-
|
|
77
|
+
command,
|
|
79
78
|
}: {
|
|
80
|
-
directory?: string;
|
|
81
79
|
port?: number;
|
|
82
|
-
|
|
80
|
+
command: (string | number)[];
|
|
83
81
|
}) => {
|
|
84
|
-
let proxy: ChildProcess;
|
|
85
|
-
|
|
86
82
|
const exit: Exit = (message) => {
|
|
87
83
|
if (message) console.error(message);
|
|
88
84
|
if (proxy) proxy.kill();
|
|
89
85
|
return undefined;
|
|
90
86
|
};
|
|
91
87
|
|
|
92
|
-
if (
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
if (port) args.push("-p", port.toString());
|
|
96
|
-
proxy = spawn("npx", args, { shell: isWindows() });
|
|
97
|
-
} else {
|
|
98
|
-
const command = remaining;
|
|
99
|
-
if (command.length === 0)
|
|
100
|
-
return exit(
|
|
101
|
-
"Must specify a directory of static assets to serve or a command to run."
|
|
102
|
-
);
|
|
103
|
-
|
|
104
|
-
console.log(`Running ${command.join(" ")}...`);
|
|
105
|
-
proxy = spawn(
|
|
106
|
-
command[0].toString(),
|
|
107
|
-
command.slice(1).map((value) => value.toString()),
|
|
108
|
-
{ shell: isWindows() }
|
|
88
|
+
if (command.length === 0)
|
|
89
|
+
return exit(
|
|
90
|
+
"Must specify a directory of static assets to serve or a command to run."
|
|
109
91
|
);
|
|
110
|
-
|
|
92
|
+
|
|
93
|
+
console.log(`Running ${command.join(" ")}...`);
|
|
94
|
+
const proxy = spawn(
|
|
95
|
+
command[0].toString(),
|
|
96
|
+
command.slice(1).map((value) => value.toString()),
|
|
97
|
+
{
|
|
98
|
+
shell: isWindows(),
|
|
99
|
+
env: {
|
|
100
|
+
BROWSER: "none",
|
|
101
|
+
...process.env,
|
|
102
|
+
},
|
|
103
|
+
}
|
|
104
|
+
);
|
|
111
105
|
|
|
112
106
|
proxy.stdout.on("data", (data) => {
|
|
113
107
|
console.log(`[proxy]: ${data}`);
|
|
@@ -137,14 +131,460 @@ const spawnProxyProcess = async ({
|
|
|
137
131
|
|
|
138
132
|
if (port === undefined) {
|
|
139
133
|
return exit(
|
|
140
|
-
"Could not automatically
|
|
134
|
+
"Could not automatically determine proxy port. Please specify the proxy port with --proxy."
|
|
141
135
|
);
|
|
142
136
|
} else {
|
|
143
137
|
console.log(`Automatically determined the proxy port to be ${port}.`);
|
|
144
138
|
}
|
|
145
139
|
}
|
|
146
140
|
|
|
147
|
-
return {
|
|
141
|
+
return { port, exit };
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
const escapeRegex = (str: string) => {
|
|
145
|
+
return str.replace(/[-/\\^$*+?.()|[]{}]/g, "\\$&");
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
export type Replacements = Record<string, string>;
|
|
149
|
+
|
|
150
|
+
export const replacer = (str: string, replacements: Replacements) => {
|
|
151
|
+
for (const [replacement, value] of Object.entries(replacements)) {
|
|
152
|
+
str = str.replace(`:${replacement}`, value);
|
|
153
|
+
}
|
|
154
|
+
return str;
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
export const generateRulesMatcher = <T,>(
|
|
158
|
+
rules?: Record<string, T>,
|
|
159
|
+
replacer: (match: T, replacements: Replacements) => T = (match) => match
|
|
160
|
+
) => {
|
|
161
|
+
// TODO: How can you test cross-host rules?
|
|
162
|
+
if (!rules) return () => [];
|
|
163
|
+
|
|
164
|
+
const compiledRules = Object.entries(rules)
|
|
165
|
+
.map(([rule, match]) => {
|
|
166
|
+
const crossHost = rule.startsWith("https://");
|
|
167
|
+
|
|
168
|
+
rule = rule.split("*").map(escapeRegex).join("(?<splat>.*)");
|
|
169
|
+
|
|
170
|
+
const host_matches = rule.matchAll(
|
|
171
|
+
/(?<=^https:\\\/\\\/[^/]*?):([^\\]+)(?=\\)/g
|
|
172
|
+
);
|
|
173
|
+
for (const match of host_matches) {
|
|
174
|
+
rule = rule.split(match[0]).join(`(?<${match[1]}>[^/.]+)`);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const path_matches = rule.matchAll(/:(\w+)/g);
|
|
178
|
+
for (const match of path_matches) {
|
|
179
|
+
rule = rule.split(match[0]).join(`(?<${match[1]}>[^/]+)`);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
rule = "^" + rule + "$";
|
|
183
|
+
|
|
184
|
+
try {
|
|
185
|
+
const regExp = new RegExp(rule);
|
|
186
|
+
return [{ crossHost, regExp }, match];
|
|
187
|
+
} catch {}
|
|
188
|
+
})
|
|
189
|
+
.filter((value) => value !== undefined) as [
|
|
190
|
+
{ crossHost: boolean; regExp: RegExp },
|
|
191
|
+
T
|
|
192
|
+
][];
|
|
193
|
+
|
|
194
|
+
return ({ request }: { request: Request }) => {
|
|
195
|
+
const { pathname, host } = new URL(request.url);
|
|
196
|
+
|
|
197
|
+
return compiledRules
|
|
198
|
+
.map(([{ crossHost, regExp }, match]) => {
|
|
199
|
+
const test = crossHost ? `https://${host}${pathname}` : pathname;
|
|
200
|
+
const result = regExp.exec(test);
|
|
201
|
+
if (result) {
|
|
202
|
+
return replacer(match, result.groups || {});
|
|
203
|
+
}
|
|
204
|
+
})
|
|
205
|
+
.filter((value) => value !== undefined) as T[];
|
|
206
|
+
};
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
const generateHeadersMatcher = (headersFile: string) => {
|
|
210
|
+
if (existsSync(headersFile)) {
|
|
211
|
+
const contents = readFileSync(headersFile).toString();
|
|
212
|
+
|
|
213
|
+
// TODO: Log errors
|
|
214
|
+
const lines = contents
|
|
215
|
+
.split("\n")
|
|
216
|
+
.map((line) => line.trim())
|
|
217
|
+
.filter((line) => !line.startsWith("#") && line !== "");
|
|
218
|
+
|
|
219
|
+
const rules: Record<string, Record<string, string>> = {};
|
|
220
|
+
let rule: { path: string; headers: Record<string, string> } | undefined =
|
|
221
|
+
undefined;
|
|
222
|
+
|
|
223
|
+
for (const line of lines) {
|
|
224
|
+
if (/^([^\s]+:\/\/|^\/)/.test(line)) {
|
|
225
|
+
if (rule && Object.keys(rule.headers).length > 0) {
|
|
226
|
+
rules[rule.path] = rule.headers;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const path = validateURL(line);
|
|
230
|
+
if (path) {
|
|
231
|
+
rule = {
|
|
232
|
+
path,
|
|
233
|
+
headers: {},
|
|
234
|
+
};
|
|
235
|
+
continue;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
if (!line.includes(":")) continue;
|
|
240
|
+
|
|
241
|
+
const [rawName, ...rawValue] = line.split(":");
|
|
242
|
+
const name = rawName.trim().toLowerCase();
|
|
243
|
+
const value = rawValue.join(":").trim();
|
|
244
|
+
|
|
245
|
+
if (name === "") continue;
|
|
246
|
+
if (!rule) continue;
|
|
247
|
+
|
|
248
|
+
const existingValues = rule.headers[name];
|
|
249
|
+
rule.headers[name] = existingValues
|
|
250
|
+
? `${existingValues}, ${value}`
|
|
251
|
+
: value;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
if (rule && Object.keys(rule.headers).length > 0) {
|
|
255
|
+
rules[rule.path] = rule.headers;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
const rulesMatcher = generateRulesMatcher(rules, (match, replacements) =>
|
|
259
|
+
Object.fromEntries(
|
|
260
|
+
Object.entries(match).map(([name, value]) => [
|
|
261
|
+
name,
|
|
262
|
+
replacer(value, replacements),
|
|
263
|
+
])
|
|
264
|
+
)
|
|
265
|
+
);
|
|
266
|
+
|
|
267
|
+
return (request: Request) => {
|
|
268
|
+
const matches = rulesMatcher({
|
|
269
|
+
request,
|
|
270
|
+
});
|
|
271
|
+
if (matches) return matches;
|
|
272
|
+
};
|
|
273
|
+
} else {
|
|
274
|
+
return () => undefined;
|
|
275
|
+
}
|
|
276
|
+
};
|
|
277
|
+
|
|
278
|
+
const generateRedirectsMatcher = (redirectsFile: string) => {
|
|
279
|
+
if (existsSync(redirectsFile)) {
|
|
280
|
+
const contents = readFileSync(redirectsFile).toString();
|
|
281
|
+
|
|
282
|
+
// TODO: Log errors
|
|
283
|
+
const lines = contents
|
|
284
|
+
.split("\n")
|
|
285
|
+
.map((line) => line.trim())
|
|
286
|
+
.filter((line) => !line.startsWith("#") && line !== "");
|
|
287
|
+
|
|
288
|
+
const rules = Object.fromEntries(
|
|
289
|
+
lines
|
|
290
|
+
.map((line) => line.split(" "))
|
|
291
|
+
.filter((tokens) => tokens.length === 2 || tokens.length === 3)
|
|
292
|
+
.map((tokens) => {
|
|
293
|
+
const from = validateURL(tokens[0], true, false, false);
|
|
294
|
+
const to = validateURL(tokens[1], false, true, true);
|
|
295
|
+
let status: number | undefined = parseInt(tokens[2]) || 302;
|
|
296
|
+
status = [301, 302, 303, 307, 308].includes(status)
|
|
297
|
+
? status
|
|
298
|
+
: undefined;
|
|
299
|
+
|
|
300
|
+
return from && to && status ? [from, { to, status }] : undefined;
|
|
301
|
+
})
|
|
302
|
+
.filter((rule) => rule !== undefined) as [
|
|
303
|
+
string,
|
|
304
|
+
{ to: string; status?: number }
|
|
305
|
+
][]
|
|
306
|
+
);
|
|
307
|
+
|
|
308
|
+
const rulesMatcher = generateRulesMatcher(
|
|
309
|
+
rules,
|
|
310
|
+
({ status, to }, replacements) => ({
|
|
311
|
+
status,
|
|
312
|
+
to: replacer(to, replacements),
|
|
313
|
+
})
|
|
314
|
+
);
|
|
315
|
+
|
|
316
|
+
return (request: Request) => {
|
|
317
|
+
const match = rulesMatcher({
|
|
318
|
+
request,
|
|
319
|
+
})[0];
|
|
320
|
+
if (match) return match;
|
|
321
|
+
};
|
|
322
|
+
} else {
|
|
323
|
+
return () => undefined;
|
|
324
|
+
}
|
|
325
|
+
};
|
|
326
|
+
|
|
327
|
+
const extractPathname = (
|
|
328
|
+
path = "/",
|
|
329
|
+
includeSearch: boolean,
|
|
330
|
+
includeHash: boolean
|
|
331
|
+
) => {
|
|
332
|
+
if (!path.startsWith("/")) path = `/${path}`;
|
|
333
|
+
const url = new URL(`//${path}`, "relative://");
|
|
334
|
+
return `${url.pathname}${includeSearch ? url.search : ""}${
|
|
335
|
+
includeHash ? url.hash : ""
|
|
336
|
+
}`;
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
const validateURL = (
|
|
340
|
+
token: string,
|
|
341
|
+
onlyRelative = false,
|
|
342
|
+
includeSearch = false,
|
|
343
|
+
includeHash = false
|
|
344
|
+
) => {
|
|
345
|
+
const host = /^https:\/\/+(?<host>[^/]+)\/?(?<path>.*)/.exec(token);
|
|
346
|
+
if (host && host.groups && host.groups.host) {
|
|
347
|
+
if (onlyRelative) return;
|
|
348
|
+
|
|
349
|
+
return `https://${host.groups.host}${extractPathname(
|
|
350
|
+
host.groups.path,
|
|
351
|
+
includeSearch,
|
|
352
|
+
includeHash
|
|
353
|
+
)}`;
|
|
354
|
+
} else {
|
|
355
|
+
if (!token.startsWith("/") && onlyRelative) token = `/${token}`;
|
|
356
|
+
|
|
357
|
+
const path = /^\//.exec(token);
|
|
358
|
+
if (path) {
|
|
359
|
+
try {
|
|
360
|
+
return extractPathname(token, includeSearch, includeHash);
|
|
361
|
+
} catch {}
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
return "";
|
|
365
|
+
};
|
|
366
|
+
|
|
367
|
+
const hasFileExtension = (pathname: string) =>
|
|
368
|
+
/\/.+\.[a-z0-9]+$/i.test(pathname);
|
|
369
|
+
|
|
370
|
+
const generateAssetsFetch = async (
|
|
371
|
+
directory: string
|
|
372
|
+
): Promise<typeof fetch> => {
|
|
373
|
+
const headersFile = join(directory, "_headers");
|
|
374
|
+
const redirectsFile = join(directory, "_redirects");
|
|
375
|
+
const workerFile = join(directory, "_worker.js");
|
|
376
|
+
|
|
377
|
+
const ignoredFiles = [headersFile, redirectsFile, workerFile];
|
|
378
|
+
|
|
379
|
+
const assetExists = (path: string) => {
|
|
380
|
+
path = join(directory, path);
|
|
381
|
+
return (
|
|
382
|
+
existsSync(path) &&
|
|
383
|
+
lstatSync(path).isFile() &&
|
|
384
|
+
!ignoredFiles.includes(path)
|
|
385
|
+
);
|
|
386
|
+
};
|
|
387
|
+
|
|
388
|
+
const getAsset = (path: string) => {
|
|
389
|
+
if (assetExists(path)) {
|
|
390
|
+
return join(directory, path);
|
|
391
|
+
}
|
|
392
|
+
};
|
|
393
|
+
|
|
394
|
+
let redirectsMatcher = generateRedirectsMatcher(redirectsFile);
|
|
395
|
+
let headersMatcher = generateHeadersMatcher(headersFile);
|
|
396
|
+
|
|
397
|
+
watch([headersFile, redirectsFile], {
|
|
398
|
+
persistent: true,
|
|
399
|
+
}).on("change", (path) => {
|
|
400
|
+
switch (path) {
|
|
401
|
+
case headersFile: {
|
|
402
|
+
console.log("_headers modified. Re-evaluating...");
|
|
403
|
+
headersMatcher = generateHeadersMatcher(headersFile);
|
|
404
|
+
break;
|
|
405
|
+
}
|
|
406
|
+
case redirectsFile: {
|
|
407
|
+
console.log("_redirects modified. Re-evaluating...");
|
|
408
|
+
redirectsMatcher = generateRedirectsMatcher(redirectsFile);
|
|
409
|
+
break;
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
const serveAsset = (file: string) => {
|
|
415
|
+
return readFileSync(file);
|
|
416
|
+
};
|
|
417
|
+
|
|
418
|
+
const generateResponse = (request: Request) => {
|
|
419
|
+
const url = new URL(request.url);
|
|
420
|
+
|
|
421
|
+
const deconstructedResponse: {
|
|
422
|
+
status: number;
|
|
423
|
+
headers: Headers;
|
|
424
|
+
body?: Buffer;
|
|
425
|
+
} = {
|
|
426
|
+
status: 200,
|
|
427
|
+
headers: new Headers(),
|
|
428
|
+
body: undefined,
|
|
429
|
+
};
|
|
430
|
+
|
|
431
|
+
const match = redirectsMatcher(request);
|
|
432
|
+
if (match) {
|
|
433
|
+
const { status, to } = match;
|
|
434
|
+
|
|
435
|
+
let location = to;
|
|
436
|
+
let search;
|
|
437
|
+
|
|
438
|
+
if (to.startsWith("/")) {
|
|
439
|
+
search = new URL(location, "http://fakehost").search;
|
|
440
|
+
} else {
|
|
441
|
+
search = new URL(location).search;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
location = `${location}${search ? "" : url.search}`;
|
|
445
|
+
|
|
446
|
+
if (status && [301, 302, 303, 307, 308].includes(status)) {
|
|
447
|
+
deconstructedResponse.status = status;
|
|
448
|
+
} else {
|
|
449
|
+
deconstructedResponse.status = 302;
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
deconstructedResponse.headers.set("Location", location);
|
|
453
|
+
return deconstructedResponse;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
if (!request.method?.match(/^(get|head)$/i)) {
|
|
457
|
+
deconstructedResponse.status = 405;
|
|
458
|
+
return deconstructedResponse;
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
const notFound = () => {
|
|
462
|
+
let cwd = url.pathname;
|
|
463
|
+
while (cwd) {
|
|
464
|
+
cwd = cwd.slice(0, cwd.lastIndexOf("/"));
|
|
465
|
+
|
|
466
|
+
if ((asset = getAsset(`${cwd}/404.html`))) {
|
|
467
|
+
deconstructedResponse.status = 404;
|
|
468
|
+
deconstructedResponse.body = serveAsset(asset);
|
|
469
|
+
return deconstructedResponse;
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
if ((asset = getAsset(`/index.html`))) {
|
|
474
|
+
deconstructedResponse.body = serveAsset(asset);
|
|
475
|
+
return deconstructedResponse;
|
|
476
|
+
}
|
|
477
|
+
};
|
|
478
|
+
|
|
479
|
+
let asset;
|
|
480
|
+
|
|
481
|
+
if (url.pathname.endsWith("/")) {
|
|
482
|
+
if ((asset = getAsset(`${url.pathname}/index.html`))) {
|
|
483
|
+
deconstructedResponse.body = serveAsset(asset);
|
|
484
|
+
return deconstructedResponse;
|
|
485
|
+
} else if (
|
|
486
|
+
(asset = getAsset(`${url.pathname.replace(/\/$/, ".html")}`))
|
|
487
|
+
) {
|
|
488
|
+
deconstructedResponse.status = 301;
|
|
489
|
+
deconstructedResponse.headers.set(
|
|
490
|
+
"Location",
|
|
491
|
+
`${url.pathname.slice(0, -1)}${url.search}`
|
|
492
|
+
);
|
|
493
|
+
return deconstructedResponse;
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
if (url.pathname.endsWith("/index")) {
|
|
498
|
+
deconstructedResponse.status = 301;
|
|
499
|
+
deconstructedResponse.headers.set(
|
|
500
|
+
"Location",
|
|
501
|
+
`${url.pathname.slice(0, -"index".length)}${url.search}`
|
|
502
|
+
);
|
|
503
|
+
return deconstructedResponse;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
if ((asset = getAsset(url.pathname))) {
|
|
507
|
+
if (url.pathname.endsWith(".html")) {
|
|
508
|
+
const extensionlessPath = url.pathname.slice(0, -".html".length);
|
|
509
|
+
if (getAsset(extensionlessPath) || extensionlessPath === "/") {
|
|
510
|
+
deconstructedResponse.body = serveAsset(asset);
|
|
511
|
+
return deconstructedResponse;
|
|
512
|
+
} else {
|
|
513
|
+
deconstructedResponse.status = 301;
|
|
514
|
+
deconstructedResponse.headers.set(
|
|
515
|
+
"Location",
|
|
516
|
+
`${extensionlessPath}${url.search}`
|
|
517
|
+
);
|
|
518
|
+
return deconstructedResponse;
|
|
519
|
+
}
|
|
520
|
+
} else {
|
|
521
|
+
deconstructedResponse.body = serveAsset(asset);
|
|
522
|
+
return deconstructedResponse;
|
|
523
|
+
}
|
|
524
|
+
} else if (hasFileExtension(url.pathname)) {
|
|
525
|
+
notFound();
|
|
526
|
+
return deconstructedResponse;
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
if ((asset = getAsset(`${url.pathname}.html`))) {
|
|
530
|
+
deconstructedResponse.body = serveAsset(asset);
|
|
531
|
+
return deconstructedResponse;
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
if ((asset = getAsset(`${url.pathname}/index.html`))) {
|
|
535
|
+
deconstructedResponse.status = 301;
|
|
536
|
+
deconstructedResponse.headers.set(
|
|
537
|
+
"Location",
|
|
538
|
+
`${url.pathname}/${url.search}`
|
|
539
|
+
);
|
|
540
|
+
return deconstructedResponse;
|
|
541
|
+
} else {
|
|
542
|
+
notFound();
|
|
543
|
+
return deconstructedResponse;
|
|
544
|
+
}
|
|
545
|
+
};
|
|
546
|
+
|
|
547
|
+
const attachHeaders = (
|
|
548
|
+
request: Request,
|
|
549
|
+
deconstructedResponse: { status: number; headers: Headers; body?: Buffer }
|
|
550
|
+
) => {
|
|
551
|
+
const headers = deconstructedResponse.headers;
|
|
552
|
+
const newHeaders = new Headers({});
|
|
553
|
+
const matches = headersMatcher(request) || [];
|
|
554
|
+
|
|
555
|
+
matches.forEach((match) => {
|
|
556
|
+
Object.entries(match).forEach(([name, value]) => {
|
|
557
|
+
newHeaders.append(name, `${value}`);
|
|
558
|
+
});
|
|
559
|
+
});
|
|
560
|
+
|
|
561
|
+
const combinedHeaders = {
|
|
562
|
+
...Object.fromEntries(headers.entries()),
|
|
563
|
+
...Object.fromEntries(newHeaders.entries()),
|
|
564
|
+
};
|
|
565
|
+
|
|
566
|
+
deconstructedResponse.headers = new Headers({});
|
|
567
|
+
Object.entries(combinedHeaders).forEach(([name, value]) => {
|
|
568
|
+
if (value) deconstructedResponse.headers.set(name, value);
|
|
569
|
+
});
|
|
570
|
+
};
|
|
571
|
+
|
|
572
|
+
return (async (input, init) => {
|
|
573
|
+
const request = new Request(input, init);
|
|
574
|
+
const deconstructedResponse = generateResponse(request);
|
|
575
|
+
attachHeaders(request, deconstructedResponse);
|
|
576
|
+
|
|
577
|
+
const headers = new Headers();
|
|
578
|
+
|
|
579
|
+
[...deconstructedResponse.headers.entries()].forEach(([name, value]) => {
|
|
580
|
+
if (value) headers.set(name, value);
|
|
581
|
+
});
|
|
582
|
+
|
|
583
|
+
return new Response(deconstructedResponse.body, {
|
|
584
|
+
headers,
|
|
585
|
+
status: deconstructedResponse.status,
|
|
586
|
+
});
|
|
587
|
+
}) as any;
|
|
148
588
|
};
|
|
149
589
|
|
|
150
590
|
export const pages: BuilderCallback<unknown, unknown> = (yargs) => {
|
|
@@ -222,14 +662,27 @@ export const pages: BuilderCallback<unknown, unknown> = (yargs) => {
|
|
|
222
662
|
const functionsDirectory = "./functions";
|
|
223
663
|
const usingFunctions = existsSync(functionsDirectory);
|
|
224
664
|
|
|
225
|
-
const
|
|
226
|
-
directory,
|
|
227
|
-
port: requestedProxyPort,
|
|
228
|
-
remaining: remaining as (string | number)[],
|
|
229
|
-
});
|
|
230
|
-
if (proxy === undefined) return undefined;
|
|
665
|
+
const command = remaining as (string | number)[];
|
|
231
666
|
|
|
232
|
-
|
|
667
|
+
let proxyPort: number | undefined;
|
|
668
|
+
let exit: Exit = (message) => {
|
|
669
|
+
console.error(message);
|
|
670
|
+
return undefined;
|
|
671
|
+
};
|
|
672
|
+
|
|
673
|
+
if (directory === undefined) {
|
|
674
|
+
const proxy = await spawnProxyProcess({
|
|
675
|
+
port: requestedProxyPort,
|
|
676
|
+
command,
|
|
677
|
+
});
|
|
678
|
+
if (proxy === undefined) return undefined;
|
|
679
|
+
|
|
680
|
+
exit = proxy.exit;
|
|
681
|
+
proxyPort = proxy.port;
|
|
682
|
+
|
|
683
|
+
process.on("SIGINT", () => exit());
|
|
684
|
+
process.on("SIGTERM", () => exit());
|
|
685
|
+
}
|
|
233
686
|
|
|
234
687
|
let miniflareArgs: MiniflareOptions = {};
|
|
235
688
|
|
|
@@ -257,12 +710,57 @@ export const pages: BuilderCallback<unknown, unknown> = (yargs) => {
|
|
|
257
710
|
};
|
|
258
711
|
}
|
|
259
712
|
|
|
260
|
-
const { Miniflare } = await import("miniflare");
|
|
261
|
-
const {
|
|
713
|
+
const { Miniflare, Log, LogLevel } = await import("miniflare");
|
|
714
|
+
const { fetch } = await import("@miniflare/core");
|
|
715
|
+
|
|
716
|
+
class MiniflareLogger extends Log {
|
|
717
|
+
logWithLevel(level: number, message: string) {
|
|
718
|
+
if (level <= this.level) {
|
|
719
|
+
switch (level) {
|
|
720
|
+
case LogLevel.NONE: {
|
|
721
|
+
break;
|
|
722
|
+
}
|
|
723
|
+
case LogLevel.ERROR: {
|
|
724
|
+
console.error(message);
|
|
725
|
+
break;
|
|
726
|
+
}
|
|
727
|
+
case LogLevel.WARN: {
|
|
728
|
+
console.warn(message);
|
|
729
|
+
break;
|
|
730
|
+
}
|
|
731
|
+
case LogLevel.INFO: {
|
|
732
|
+
console.info(message);
|
|
733
|
+
break;
|
|
734
|
+
}
|
|
735
|
+
case LogLevel.DEBUG: {
|
|
736
|
+
console.debug(message);
|
|
737
|
+
break;
|
|
738
|
+
}
|
|
739
|
+
case LogLevel.VERBOSE: {
|
|
740
|
+
console.debug(message);
|
|
741
|
+
break;
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
log(message: string) {}
|
|
748
|
+
|
|
749
|
+
error(message: Error) {
|
|
750
|
+
this.logWithLevel(
|
|
751
|
+
LogLevel.ERROR,
|
|
752
|
+
message.stack ? message.stack : `${message}`
|
|
753
|
+
);
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
|
|
262
757
|
const miniflare = new Miniflare({
|
|
758
|
+
port,
|
|
263
759
|
watch: true,
|
|
264
760
|
modules: true,
|
|
265
761
|
|
|
762
|
+
log: new MiniflareLogger(LogLevel.ERROR),
|
|
763
|
+
|
|
266
764
|
kvNamespaces: kvs.map((kv) => kv.toString()),
|
|
267
765
|
|
|
268
766
|
durableObjects: Object.fromEntries(
|
|
@@ -283,57 +781,62 @@ export const pages: BuilderCallback<unknown, unknown> = (yargs) => {
|
|
|
283
781
|
input: RequestInfo,
|
|
284
782
|
init?: RequestInit | undefined
|
|
285
783
|
) => {
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
784
|
+
if (proxyPort) {
|
|
785
|
+
try {
|
|
786
|
+
let request = new Request(input as any, init);
|
|
787
|
+
const url = new URL(request.url);
|
|
788
|
+
url.host = `127.0.0.1:${proxyPort}`;
|
|
789
|
+
request = new Request(url.toString(), request);
|
|
790
|
+
return await fetch(request);
|
|
791
|
+
} catch (thrown) {
|
|
792
|
+
console.error(`Could not proxy request: ${thrown}`);
|
|
793
|
+
|
|
794
|
+
// TODO: Pretty error page
|
|
795
|
+
return new Response(
|
|
796
|
+
`[wrangler] Could not proxy request: ${thrown}`,
|
|
797
|
+
{ status: 502 }
|
|
798
|
+
);
|
|
799
|
+
}
|
|
800
|
+
} else {
|
|
801
|
+
try {
|
|
802
|
+
return await (
|
|
803
|
+
await generateAssetsFetch(directory)
|
|
804
|
+
)(input as any, init as any);
|
|
805
|
+
} catch (thrown) {
|
|
806
|
+
console.error(`Could not serve static asset: ${thrown}`);
|
|
807
|
+
|
|
808
|
+
// TODO: Pretty error page
|
|
809
|
+
return new Response(
|
|
810
|
+
`[wrangler] Could not serve static asset: ${thrown}`,
|
|
811
|
+
{ status: 502 }
|
|
812
|
+
);
|
|
813
|
+
}
|
|
300
814
|
}
|
|
301
815
|
},
|
|
302
816
|
},
|
|
303
817
|
},
|
|
304
818
|
|
|
819
|
+
kvPersist: true,
|
|
820
|
+
durableObjectsPersist: true,
|
|
821
|
+
cachePersist: true,
|
|
822
|
+
|
|
305
823
|
...miniflareArgs,
|
|
306
824
|
});
|
|
307
|
-
const miniflareServer = await miniflare.createServer();
|
|
308
825
|
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
if (typeof address === "string")
|
|
312
|
-
return exit(
|
|
313
|
-
"Could not determine Miniflare's port. Please report this issue to the Wrangler team."
|
|
314
|
-
);
|
|
315
|
-
|
|
316
|
-
const miniflarePort = address.port;
|
|
317
|
-
|
|
318
|
-
const app = express();
|
|
319
|
-
|
|
320
|
-
app.use(
|
|
321
|
-
"/",
|
|
322
|
-
httpProxyMiddleware.createProxyMiddleware({
|
|
323
|
-
target: `http://127.0.0.1:${miniflarePort}`,
|
|
324
|
-
})
|
|
325
|
-
);
|
|
826
|
+
const server = await miniflare.startServer();
|
|
827
|
+
console.log(`Serving at http://127.0.0.1:${port}/`);
|
|
326
828
|
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
target: `http://127.0.0.1:${proxyPort}`,
|
|
331
|
-
})
|
|
332
|
-
);
|
|
829
|
+
if (process.env.BROWSER !== "none") {
|
|
830
|
+
open(`http://127.0.0.1:${port}/`);
|
|
831
|
+
}
|
|
333
832
|
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
833
|
+
process.on("SIGINT", () => {
|
|
834
|
+
server.close();
|
|
835
|
+
miniflare.dispose();
|
|
836
|
+
});
|
|
837
|
+
process.on("SIGTERM", () => {
|
|
838
|
+
server.close();
|
|
839
|
+
miniflare.dispose();
|
|
337
840
|
});
|
|
338
841
|
}
|
|
339
842
|
);
|