wrangler 0.0.0-e14e589 → 0.0.0-e2a26a0

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.
Files changed (61) hide show
  1. package/README.md +51 -0
  2. package/bin/wrangler.js +2 -2
  3. package/package.json +49 -30
  4. package/pages/functions/buildWorker.ts +62 -0
  5. package/pages/functions/filepath-routing.test.ts +47 -0
  6. package/pages/functions/filepath-routing.ts +231 -0
  7. package/pages/functions/identifiers.ts +78 -0
  8. package/pages/functions/routes.ts +160 -0
  9. package/pages/functions/template-worker.ts +136 -0
  10. package/src/__tests__/dev.test.tsx +71 -0
  11. package/src/__tests__/{clipboardy-mock.js → helpers/clipboardy-mock.js} +0 -0
  12. package/src/__tests__/helpers/cmd-shim.d.ts +11 -0
  13. package/src/__tests__/helpers/mock-account-id.ts +30 -0
  14. package/src/__tests__/helpers/mock-bin.ts +33 -0
  15. package/src/__tests__/helpers/mock-cfetch.ts +207 -0
  16. package/src/__tests__/helpers/mock-console.ts +56 -0
  17. package/src/__tests__/helpers/mock-dialogs.ts +65 -0
  18. package/src/__tests__/helpers/mock-kv.ts +40 -0
  19. package/src/__tests__/helpers/mock-user.ts +27 -0
  20. package/src/__tests__/helpers/run-in-tmp.ts +29 -0
  21. package/src/__tests__/helpers/run-wrangler.ts +14 -0
  22. package/src/__tests__/index.test.ts +509 -119
  23. package/src/__tests__/jest.setup.ts +37 -0
  24. package/src/__tests__/kv.test.ts +1139 -0
  25. package/src/__tests__/logout.test.ts +50 -0
  26. package/src/__tests__/package-manager.test.ts +206 -0
  27. package/src/__tests__/publish.test.ts +1222 -0
  28. package/src/__tests__/secret.test.ts +210 -0
  29. package/src/__tests__/whoami.test.tsx +86 -0
  30. package/src/api/form_data.ts +85 -76
  31. package/src/api/preview.ts +29 -24
  32. package/src/api/worker.ts +50 -35
  33. package/src/cfetch/index.ts +102 -0
  34. package/src/cfetch/internal.ts +101 -0
  35. package/src/cli.ts +6 -7
  36. package/src/config.ts +630 -85
  37. package/src/dev.tsx +446 -282
  38. package/src/dialogs.tsx +6 -14
  39. package/src/index.tsx +773 -426
  40. package/src/inspect.ts +596 -0
  41. package/src/kv.tsx +118 -45
  42. package/src/module-collection.ts +63 -0
  43. package/src/package-manager.ts +120 -0
  44. package/src/pages.tsx +917 -211
  45. package/src/paths.ts +26 -0
  46. package/src/proxy.ts +370 -0
  47. package/src/publish.ts +220 -88
  48. package/src/sites.tsx +180 -55
  49. package/src/tail.tsx +6 -4
  50. package/src/user.tsx +77 -87
  51. package/src/whoami.tsx +69 -0
  52. package/templates/new-worker.js +15 -0
  53. package/templates/new-worker.ts +15 -0
  54. package/{static-asset-facade.js → templates/static-asset-facade.js} +0 -0
  55. package/wrangler-dist/cli.js +125330 -557
  56. package/wrangler-dist/cli.js.map +3 -3
  57. package/src/__tests__/fixtures/init/.gitkeep +0 -0
  58. package/src/__tests__/mock-cfetch.js +0 -46
  59. package/src/api/inspect.ts +0 -441
  60. package/src/cfetch.ts +0 -72
  61. package/src/util/fetch.ts +0 -74
package/src/pages.tsx CHANGED
@@ -1,24 +1,49 @@
1
- import type { BuilderCallback } from "yargs";
2
- import { join } from "path";
3
- import { tmpdir } from "os";
4
- import { existsSync } from "fs";
5
- import type { ChildProcess } from "child_process";
6
- import { execSync, spawn } from "child_process";
7
- import express from "express";
1
+ /* eslint-disable no-shadow */
2
+
3
+ import { execSync, spawn } from "node:child_process";
4
+ import { existsSync, lstatSync, readFileSync, writeFileSync } from "node:fs";
5
+ import { tmpdir } from "node:os";
6
+ import { join } from "node:path";
7
+ import { URL } from "node:url";
8
+ import { watch } from "chokidar";
9
+ import { getType } from "mime";
10
+ import open from "open";
11
+ import { buildWorker } from "../pages/functions/buildWorker";
12
+ import { generateConfigFromFileTree } from "../pages/functions/filepath-routing";
13
+ import { writeRoutesModule } from "../pages/functions/routes";
14
+ import { toUrlPath } from "./paths";
15
+ import type { Config } from "../pages/functions/routes";
16
+ import type { Headers, Request, fetch } from "@miniflare/core";
17
+ import type { BuildResult } from "esbuild";
8
18
  import type { MiniflareOptions } from "miniflare";
9
- import type { RequestInfo, RequestInit } from "@miniflare/core";
10
- import httpProxyMiddleware from "http-proxy-middleware";
19
+ import type { BuilderCallback } from "yargs";
20
+
21
+ // Defer importing miniflare until we really need it. This takes ~0.5s
22
+ // and also modifies some `stream/web` and `undici` prototypes, so we
23
+ // don't want to do this if pages commands aren't being called.
11
24
 
12
- type Exit = (message?: string) => undefined;
25
+ const EXIT_CALLBACKS: (() => void)[] = [];
26
+ const EXIT = (message?: string, code?: number) => {
27
+ if (message) console.log(message);
28
+ if (code) process.exitCode = code;
29
+ EXIT_CALLBACKS.forEach((callback) => callback());
30
+ process.exit(code);
31
+ };
32
+
33
+ process.on("SIGINT", () => EXIT());
34
+ process.on("SIGTERM", () => EXIT());
13
35
 
14
- const isWindows = () => process.platform === "win32";
36
+ function isWindows() {
37
+ return process.platform === "win32";
38
+ }
15
39
 
16
40
  const SECONDS_TO_WAIT_FOR_PROXY = 5;
17
41
 
18
- const sleep = async (ms: number) =>
42
+ async function sleep(ms: number) {
19
43
  await new Promise((resolve) => setTimeout(resolve, ms));
44
+ }
20
45
 
21
- const getPids = (pid: number) => {
46
+ function getPids(pid: number) {
22
47
  const pids: number[] = [pid];
23
48
  let command: string, regExp: RegExp;
24
49
 
@@ -43,9 +68,9 @@ const getPids = (pid: number) => {
43
68
  } catch {}
44
69
 
45
70
  return pids;
46
- };
71
+ }
47
72
 
48
- const getPort = (pid: number) => {
73
+ function getPort(pid: number) {
49
74
  let command: string, regExp: RegExp;
50
75
 
51
76
  if (isWindows()) {
@@ -70,44 +95,36 @@ const getPort = (pid: number) => {
70
95
  `Error scanning for ports of process with PID ${pid}: ${thrown}`
71
96
  );
72
97
  }
73
- };
98
+ }
74
99
 
75
- const spawnProxyProcess = async ({
76
- directory,
100
+ async function spawnProxyProcess({
77
101
  port,
78
- remaining,
102
+ command,
79
103
  }: {
80
- directory?: string;
81
104
  port?: number;
82
- remaining: (string | number)[];
83
- }) => {
84
- let proxy: ChildProcess;
85
-
86
- const exit: Exit = (message) => {
87
- if (message) console.error(message);
88
- if (proxy) proxy.kill();
89
- return undefined;
90
- };
91
-
92
- if (directory !== undefined) {
93
- console.log(`Serving ${directory}...`);
94
- const args = ["serve", directory];
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() }
105
+ command: (string | number)[];
106
+ }): Promise<void | number> {
107
+ if (command.length === 0)
108
+ return EXIT(
109
+ "Must specify a directory of static assets to serve or a command to run.",
110
+ 1
109
111
  );
110
- }
112
+
113
+ console.log(`Running ${command.join(" ")}...`);
114
+ const proxy = spawn(
115
+ command[0].toString(),
116
+ command.slice(1).map((value) => value.toString()),
117
+ {
118
+ shell: isWindows(),
119
+ env: {
120
+ BROWSER: "none",
121
+ ...process.env,
122
+ },
123
+ }
124
+ );
125
+ EXIT_CALLBACKS.push(() => {
126
+ proxy.kill();
127
+ });
111
128
 
112
129
  proxy.stdout.on("data", (data) => {
113
130
  console.log(`[proxy]: ${data}`);
@@ -136,205 +153,894 @@ const spawnProxyProcess = async ({
136
153
  .filter((port) => port !== undefined)[0];
137
154
 
138
155
  if (port === undefined) {
139
- return exit(
140
- "Could not automatically determin proxy port. Please specify the proxy port with --proxy."
156
+ return EXIT(
157
+ "Could not automatically determine proxy port. Please specify the proxy port with --proxy.",
158
+ 1
141
159
  );
142
160
  } else {
143
161
  console.log(`Automatically determined the proxy port to be ${port}.`);
144
162
  }
145
163
  }
146
164
 
147
- return { proxy, port, exit };
148
- };
165
+ return port;
166
+ }
149
167
 
150
- export const pages: BuilderCallback<unknown, unknown> = (yargs) => {
151
- return yargs.command(
152
- "dev [directory] [-- command]",
153
- "🧑‍💻 Develop your full-stack Pages application locally",
154
- (yargs) => {
155
- return yargs
156
- .positional("directory", {
157
- type: "string",
158
- demandOption: undefined,
159
- description: "The directory of static assets to serve",
160
- })
161
- .positional("command", {
162
- type: "string",
163
- demandOption: undefined,
164
- description: "The proxy command to run",
165
- })
166
- .options({
167
- local: {
168
- type: "boolean",
169
- default: true,
170
- description: "Run on my machine",
171
- },
172
- port: {
173
- type: "number",
174
- default: 8788,
175
- description: "The port to listen on (serve from)",
176
- },
177
- proxy: {
178
- type: "number",
179
- description:
180
- "The port to proxy (where the static assets are served)",
181
- },
182
- "script-path": {
183
- type: "string",
184
- default: "_worker.js",
185
- description:
186
- "The location of the single Worker script if not using functions",
187
- },
188
- binding: {
189
- type: "array",
190
- description: "Bind variable/secret (KEY=VALUE)",
191
- alias: "b",
192
- },
193
- kv: {
194
- type: "array",
195
- description: "KV namespace to bind",
196
- alias: "k",
197
- },
198
- do: {
199
- type: "array",
200
- description: "Durable Object to bind (NAME=CLASS)",
201
- alias: "o",
202
- },
203
- // TODO: Miniflare user options
204
- });
205
- },
206
- async ({
207
- local,
208
- directory,
209
- port,
210
- proxy: requestedProxyPort,
211
- "script-path": singleWorkerScriptPath,
212
- binding: bindings = [],
213
- kv: kvs = [],
214
- do: durableObjects = [],
215
- "--": remaining = [],
216
- }) => {
217
- if (!local) {
218
- console.error("Only local mode is supported at the moment.");
219
- return;
168
+ function escapeRegex(str: string) {
169
+ return str.replace(/[-/\\^$*+?.()|[]{}]/g, "\\$&");
170
+ }
171
+
172
+ type Replacements = Record<string, string>;
173
+
174
+ function replacer(str: string, replacements: Replacements) {
175
+ for (const [replacement, value] of Object.entries(replacements)) {
176
+ str = str.replace(`:${replacement}`, value);
177
+ }
178
+ return str;
179
+ }
180
+
181
+ function generateRulesMatcher<T>(
182
+ rules?: Record<string, T>,
183
+ replacer: (match: T, replacements: Replacements) => T = (match) => match
184
+ ) {
185
+ // TODO: How can you test cross-host rules?
186
+ if (!rules) return () => [];
187
+
188
+ const compiledRules = Object.entries(rules)
189
+ .map(([rule, match]) => {
190
+ const crossHost = rule.startsWith("https://");
191
+
192
+ rule = rule.split("*").map(escapeRegex).join("(?<splat>.*)");
193
+
194
+ const host_matches = rule.matchAll(
195
+ /(?<=^https:\\\/\\\/[^/]*?):([^\\]+)(?=\\)/g
196
+ );
197
+ for (const match of host_matches) {
198
+ rule = rule.split(match[0]).join(`(?<${match[1]}>[^/.]+)`);
220
199
  }
221
200
 
222
- const functionsDirectory = "./functions";
223
- const usingFunctions = existsSync(functionsDirectory);
201
+ const path_matches = rule.matchAll(/:(\w+)/g);
202
+ for (const match of path_matches) {
203
+ rule = rule.split(match[0]).join(`(?<${match[1]}>[^/]+)`);
204
+ }
224
205
 
225
- const proxy = await spawnProxyProcess({
226
- directory,
227
- port: requestedProxyPort,
228
- remaining: remaining as (string | number)[],
206
+ rule = "^" + rule + "$";
207
+
208
+ try {
209
+ const regExp = new RegExp(rule);
210
+ return [{ crossHost, regExp }, match];
211
+ } catch {}
212
+ })
213
+ .filter((value) => value !== undefined) as [
214
+ { crossHost: boolean; regExp: RegExp },
215
+ T
216
+ ][];
217
+
218
+ return ({ request }: { request: Request }) => {
219
+ const { pathname, host } = new URL(request.url);
220
+
221
+ return compiledRules
222
+ .map(([{ crossHost, regExp }, match]) => {
223
+ const test = crossHost ? `https://${host}${pathname}` : pathname;
224
+ const result = regExp.exec(test);
225
+ if (result) {
226
+ return replacer(match, result.groups || {});
227
+ }
228
+ })
229
+ .filter((value) => value !== undefined) as T[];
230
+ };
231
+ }
232
+
233
+ function generateHeadersMatcher(headersFile: string) {
234
+ if (existsSync(headersFile)) {
235
+ const contents = readFileSync(headersFile).toString();
236
+
237
+ // TODO: Log errors
238
+ const lines = contents
239
+ .split("\n")
240
+ .map((line) => line.trim())
241
+ .filter((line) => !line.startsWith("#") && line !== "");
242
+
243
+ const rules: Record<string, Record<string, string>> = {};
244
+ let rule: { path: string; headers: Record<string, string> } | undefined =
245
+ undefined;
246
+
247
+ for (const line of lines) {
248
+ if (/^([^\s]+:\/\/|^\/)/.test(line)) {
249
+ if (rule && Object.keys(rule.headers).length > 0) {
250
+ rules[rule.path] = rule.headers;
251
+ }
252
+
253
+ const path = validateURL(line);
254
+ if (path) {
255
+ rule = {
256
+ path,
257
+ headers: {},
258
+ };
259
+ continue;
260
+ }
261
+ }
262
+
263
+ if (!line.includes(":")) continue;
264
+
265
+ const [rawName, ...rawValue] = line.split(":");
266
+ const name = rawName.trim().toLowerCase();
267
+ const value = rawValue.join(":").trim();
268
+
269
+ if (name === "") continue;
270
+ if (!rule) continue;
271
+
272
+ const existingValues = rule.headers[name];
273
+ rule.headers[name] = existingValues
274
+ ? `${existingValues}, ${value}`
275
+ : value;
276
+ }
277
+
278
+ if (rule && Object.keys(rule.headers).length > 0) {
279
+ rules[rule.path] = rule.headers;
280
+ }
281
+
282
+ const rulesMatcher = generateRulesMatcher(rules, (match, replacements) =>
283
+ Object.fromEntries(
284
+ Object.entries(match).map(([name, value]) => [
285
+ name,
286
+ replacer(value, replacements),
287
+ ])
288
+ )
289
+ );
290
+
291
+ return (request: Request) => {
292
+ const matches = rulesMatcher({
293
+ request,
229
294
  });
230
- if (proxy === undefined) return undefined;
295
+ if (matches) return matches;
296
+ };
297
+ } else {
298
+ return () => undefined;
299
+ }
300
+ }
301
+
302
+ function generateRedirectsMatcher(redirectsFile: string) {
303
+ if (existsSync(redirectsFile)) {
304
+ const contents = readFileSync(redirectsFile).toString();
305
+
306
+ // TODO: Log errors
307
+ const lines = contents
308
+ .split("\n")
309
+ .map((line) => line.trim())
310
+ .filter((line) => !line.startsWith("#") && line !== "");
311
+
312
+ const rules = Object.fromEntries(
313
+ lines
314
+ .map((line) => line.split(" "))
315
+ .filter((tokens) => tokens.length === 2 || tokens.length === 3)
316
+ .map((tokens) => {
317
+ const from = validateURL(tokens[0], true, false, false);
318
+ const to = validateURL(tokens[1], false, true, true);
319
+ let status: number | undefined = parseInt(tokens[2]) || 302;
320
+ status = [301, 302, 303, 307, 308].includes(status)
321
+ ? status
322
+ : undefined;
323
+
324
+ return from && to && status ? [from, { to, status }] : undefined;
325
+ })
326
+ .filter((rule) => rule !== undefined) as [
327
+ string,
328
+ { to: string; status?: number }
329
+ ][]
330
+ );
331
+
332
+ const rulesMatcher = generateRulesMatcher(
333
+ rules,
334
+ ({ status, to }, replacements) => ({
335
+ status,
336
+ to: replacer(to, replacements),
337
+ })
338
+ );
339
+
340
+ return (request: Request) => {
341
+ const match = rulesMatcher({
342
+ request,
343
+ })[0];
344
+ if (match) return match;
345
+ };
346
+ } else {
347
+ return () => undefined;
348
+ }
349
+ }
350
+
351
+ function extractPathname(
352
+ path = "/",
353
+ includeSearch: boolean,
354
+ includeHash: boolean
355
+ ) {
356
+ if (!path.startsWith("/")) path = `/${path}`;
357
+ const url = new URL(`//${path}`, "relative://");
358
+ return `${url.pathname}${includeSearch ? url.search : ""}${
359
+ includeHash ? url.hash : ""
360
+ }`;
361
+ }
362
+
363
+ function validateURL(
364
+ token: string,
365
+ onlyRelative = false,
366
+ includeSearch = false,
367
+ includeHash = false
368
+ ) {
369
+ const host = /^https:\/\/+(?<host>[^/]+)\/?(?<path>.*)/.exec(token);
370
+ if (host && host.groups && host.groups.host) {
371
+ if (onlyRelative) return;
372
+
373
+ return `https://${host.groups.host}${extractPathname(
374
+ host.groups.path,
375
+ includeSearch,
376
+ includeHash
377
+ )}`;
378
+ } else {
379
+ if (!token.startsWith("/") && onlyRelative) token = `/${token}`;
380
+
381
+ const path = /^\//.exec(token);
382
+ if (path) {
383
+ try {
384
+ return extractPathname(token, includeSearch, includeHash);
385
+ } catch {}
386
+ }
387
+ }
388
+ return "";
389
+ }
231
390
 
232
- const { port: proxyPort, exit } = proxy;
391
+ function hasFileExtension(pathname: string) {
392
+ return /\/.+\.[a-z0-9]+$/i.test(pathname);
393
+ }
233
394
 
234
- let miniflareArgs: MiniflareOptions = {};
395
+ async function generateAssetsFetch(directory: string): Promise<typeof fetch> {
396
+ // Defer importing miniflare until we really need it
397
+ const { Headers, Request, Response } = await import("@miniflare/core");
235
398
 
236
- if (usingFunctions) {
237
- const scriptPath = join(tmpdir(), "./functionsWorker.js");
238
- miniflareArgs = {
239
- scriptPath,
240
- buildWatchPaths: [functionsDirectory],
241
- buildCommand: `npx @cloudflare/pages-functions-compiler build ${functionsDirectory} --outfile ${scriptPath}`,
242
- };
399
+ const headersFile = join(directory, "_headers");
400
+ const redirectsFile = join(directory, "_redirects");
401
+ const workerFile = join(directory, "_worker.js");
402
+
403
+ const ignoredFiles = [headersFile, redirectsFile, workerFile];
404
+
405
+ const assetExists = (path: string) => {
406
+ path = join(directory, path);
407
+ return (
408
+ existsSync(path) &&
409
+ lstatSync(path).isFile() &&
410
+ !ignoredFiles.includes(path)
411
+ );
412
+ };
413
+
414
+ const getAsset = (path: string) => {
415
+ if (assetExists(path)) {
416
+ return join(directory, path);
417
+ }
418
+ };
419
+
420
+ let redirectsMatcher = generateRedirectsMatcher(redirectsFile);
421
+ let headersMatcher = generateHeadersMatcher(headersFile);
422
+
423
+ watch([headersFile, redirectsFile], {
424
+ persistent: true,
425
+ }).on("change", (path) => {
426
+ switch (path) {
427
+ case headersFile: {
428
+ console.log("_headers modified. Re-evaluating...");
429
+ headersMatcher = generateHeadersMatcher(headersFile);
430
+ break;
431
+ }
432
+ case redirectsFile: {
433
+ console.log("_redirects modified. Re-evaluating...");
434
+ redirectsMatcher = generateRedirectsMatcher(redirectsFile);
435
+ break;
436
+ }
437
+ }
438
+ });
439
+
440
+ const serveAsset = (file: string) => {
441
+ return readFileSync(file);
442
+ };
443
+
444
+ const generateResponse = (request: Request) => {
445
+ const url = new URL(request.url);
446
+
447
+ const deconstructedResponse: {
448
+ status: number;
449
+ headers: Headers;
450
+ body?: Buffer;
451
+ } = {
452
+ status: 200,
453
+ headers: new Headers(),
454
+ body: undefined,
455
+ };
456
+
457
+ const match = redirectsMatcher(request);
458
+ if (match) {
459
+ const { status, to } = match;
460
+
461
+ let location = to;
462
+ let search;
463
+
464
+ if (to.startsWith("/")) {
465
+ search = new URL(location, "http://fakehost").search;
243
466
  } else {
244
- const scriptPath =
245
- directory !== undefined
246
- ? join(directory, singleWorkerScriptPath)
247
- : singleWorkerScriptPath;
467
+ search = new URL(location).search;
468
+ }
469
+
470
+ location = `${location}${search ? "" : url.search}`;
471
+
472
+ if (status && [301, 302, 303, 307, 308].includes(status)) {
473
+ deconstructedResponse.status = status;
474
+ } else {
475
+ deconstructedResponse.status = 302;
476
+ }
477
+
478
+ deconstructedResponse.headers.set("Location", location);
479
+ return deconstructedResponse;
480
+ }
248
481
 
249
- if (!existsSync(scriptPath)) {
250
- return exit(
251
- `No Worker script found at ${scriptPath}. Please either create a functions directory or create a single Worker at ${scriptPath}.`
482
+ if (!request.method?.match(/^(get|head)$/i)) {
483
+ deconstructedResponse.status = 405;
484
+ return deconstructedResponse;
485
+ }
486
+
487
+ const notFound = () => {
488
+ let cwd = url.pathname;
489
+ while (cwd) {
490
+ cwd = cwd.slice(0, cwd.lastIndexOf("/"));
491
+
492
+ if ((asset = getAsset(`${cwd}/404.html`))) {
493
+ deconstructedResponse.status = 404;
494
+ deconstructedResponse.body = serveAsset(asset);
495
+ deconstructedResponse.headers.set(
496
+ "Content-Type",
497
+ getType(asset) || "application/octet-stream"
252
498
  );
499
+ return deconstructedResponse;
253
500
  }
501
+ }
254
502
 
255
- miniflareArgs = {
256
- scriptPath,
257
- };
503
+ if ((asset = getAsset(`/index.html`))) {
504
+ deconstructedResponse.body = serveAsset(asset);
505
+ deconstructedResponse.headers.set(
506
+ "Content-Type",
507
+ getType(asset) || "application/octet-stream"
508
+ );
509
+ return deconstructedResponse;
258
510
  }
259
511
 
260
- const { Miniflare } = await import("miniflare");
261
- const { Request, Response, fetch } = await import("@miniflare/core");
262
- const miniflare = new Miniflare({
263
- watch: true,
264
- modules: true,
512
+ deconstructedResponse.status = 404;
513
+ return deconstructedResponse;
514
+ };
265
515
 
266
- kvNamespaces: kvs.map((kv) => kv.toString()),
516
+ let asset;
267
517
 
268
- durableObjects: Object.fromEntries(
269
- durableObjects.map((durableObject) =>
270
- durableObject.toString().split("=")
271
- )
272
- ),
518
+ if (url.pathname.endsWith("/")) {
519
+ if ((asset = getAsset(`${url.pathname}/index.html`))) {
520
+ deconstructedResponse.body = serveAsset(asset);
521
+ deconstructedResponse.headers.set(
522
+ "Content-Type",
523
+ getType(asset) || "application/octet-stream"
524
+ );
525
+ return deconstructedResponse;
526
+ } else if (
527
+ (asset = getAsset(`${url.pathname.replace(/\/$/, ".html")}`))
528
+ ) {
529
+ deconstructedResponse.status = 301;
530
+ deconstructedResponse.headers.set(
531
+ "Location",
532
+ `${url.pathname.slice(0, -1)}${url.search}`
533
+ );
534
+ return deconstructedResponse;
535
+ }
536
+ }
273
537
 
274
- bindings: {
275
- // User bindings
276
- ...Object.fromEntries(
277
- bindings.map((binding) => binding.toString().split("="))
538
+ if (url.pathname.endsWith("/index")) {
539
+ deconstructedResponse.status = 301;
540
+ deconstructedResponse.headers.set(
541
+ "Location",
542
+ `${url.pathname.slice(0, -"index".length)}${url.search}`
543
+ );
544
+ return deconstructedResponse;
545
+ }
546
+
547
+ if ((asset = getAsset(url.pathname))) {
548
+ if (url.pathname.endsWith(".html")) {
549
+ const extensionlessPath = url.pathname.slice(0, -".html".length);
550
+ if (getAsset(extensionlessPath) || extensionlessPath === "/") {
551
+ deconstructedResponse.body = serveAsset(asset);
552
+ deconstructedResponse.headers.set(
553
+ "Content-Type",
554
+ getType(asset) || "application/octet-stream"
555
+ );
556
+ return deconstructedResponse;
557
+ } else {
558
+ deconstructedResponse.status = 301;
559
+ deconstructedResponse.headers.set(
560
+ "Location",
561
+ `${extensionlessPath}${url.search}`
562
+ );
563
+ return deconstructedResponse;
564
+ }
565
+ } else {
566
+ deconstructedResponse.body = serveAsset(asset);
567
+ deconstructedResponse.headers.set(
568
+ "Content-Type",
569
+ getType(asset) || "application/octet-stream"
570
+ );
571
+ return deconstructedResponse;
572
+ }
573
+ } else if (hasFileExtension(url.pathname)) {
574
+ notFound();
575
+ return deconstructedResponse;
576
+ }
577
+
578
+ if ((asset = getAsset(`${url.pathname}.html`))) {
579
+ deconstructedResponse.body = serveAsset(asset);
580
+ deconstructedResponse.headers.set(
581
+ "Content-Type",
582
+ getType(asset) || "application/octet-stream"
583
+ );
584
+ return deconstructedResponse;
585
+ }
586
+
587
+ if ((asset = getAsset(`${url.pathname}/index.html`))) {
588
+ deconstructedResponse.status = 301;
589
+ deconstructedResponse.headers.set(
590
+ "Location",
591
+ `${url.pathname}/${url.search}`
592
+ );
593
+ return deconstructedResponse;
594
+ } else {
595
+ notFound();
596
+ return deconstructedResponse;
597
+ }
598
+ };
599
+
600
+ const attachHeaders = (
601
+ request: Request,
602
+ deconstructedResponse: { status: number; headers: Headers; body?: Buffer }
603
+ ) => {
604
+ const headers = deconstructedResponse.headers;
605
+ const newHeaders = new Headers({});
606
+ const matches = headersMatcher(request) || [];
607
+
608
+ matches.forEach((match) => {
609
+ Object.entries(match).forEach(([name, value]) => {
610
+ newHeaders.append(name, `${value}`);
611
+ });
612
+ });
613
+
614
+ const combinedHeaders = {
615
+ ...Object.fromEntries(headers.entries()),
616
+ ...Object.fromEntries(newHeaders.entries()),
617
+ };
618
+
619
+ deconstructedResponse.headers = new Headers({});
620
+ Object.entries(combinedHeaders).forEach(([name, value]) => {
621
+ if (value) deconstructedResponse.headers.set(name, value);
622
+ });
623
+ };
624
+
625
+ return async (input, init) => {
626
+ const request = new Request(input, init);
627
+ const deconstructedResponse = generateResponse(request);
628
+ attachHeaders(request, deconstructedResponse);
629
+
630
+ const headers = new Headers();
631
+
632
+ [...deconstructedResponse.headers.entries()].forEach(([name, value]) => {
633
+ if (value) headers.set(name, value);
634
+ });
635
+
636
+ return new Response(deconstructedResponse.body, {
637
+ headers,
638
+ status: deconstructedResponse.status,
639
+ });
640
+ };
641
+ }
642
+
643
+ const RUNNING_BUILDERS: BuildResult[] = [];
644
+
645
+ async function buildFunctions({
646
+ scriptPath,
647
+ outputConfigPath,
648
+ functionsDirectory,
649
+ minify = false,
650
+ sourcemap = false,
651
+ fallbackService = "ASSETS",
652
+ watch = false,
653
+ onEnd,
654
+ }: {
655
+ scriptPath: string;
656
+ outputConfigPath?: string;
657
+ functionsDirectory: string;
658
+ minify?: boolean;
659
+ sourcemap?: boolean;
660
+ fallbackService?: string;
661
+ watch?: boolean;
662
+ onEnd?: () => void;
663
+ }) {
664
+ RUNNING_BUILDERS.forEach(
665
+ (runningBuilder) => runningBuilder.stop && runningBuilder.stop()
666
+ );
667
+
668
+ const routesModule = join(tmpdir(), "./functionsRoutes.mjs");
669
+ const baseURL = toUrlPath("/");
670
+
671
+ const config: Config = await generateConfigFromFileTree({
672
+ baseDir: functionsDirectory,
673
+ baseURL,
674
+ });
675
+
676
+ if (outputConfigPath) {
677
+ writeFileSync(
678
+ outputConfigPath,
679
+ JSON.stringify({ ...config, baseURL }, null, 2)
680
+ );
681
+ }
682
+
683
+ await writeRoutesModule({
684
+ config,
685
+ srcDir: functionsDirectory,
686
+ outfile: routesModule,
687
+ });
688
+
689
+ RUNNING_BUILDERS.push(
690
+ await buildWorker({
691
+ routesModule,
692
+ outfile: scriptPath,
693
+ minify,
694
+ sourcemap,
695
+ fallbackService,
696
+ watch,
697
+ onEnd,
698
+ })
699
+ );
700
+ }
701
+
702
+ export const pages: BuilderCallback<unknown, unknown> = (yargs) => {
703
+ return yargs
704
+ .command(
705
+ "dev [directory] [-- command]",
706
+ "🧑‍💻 Develop your full-stack Pages application locally",
707
+ (yargs) => {
708
+ return yargs
709
+ .positional("directory", {
710
+ type: "string",
711
+ demandOption: undefined,
712
+ description: "The directory of static assets to serve",
713
+ })
714
+ .positional("command", {
715
+ type: "string",
716
+ demandOption: undefined,
717
+ description: "The proxy command to run",
718
+ })
719
+ .options({
720
+ local: {
721
+ type: "boolean",
722
+ default: true,
723
+ description: "Run on my machine",
724
+ },
725
+ port: {
726
+ type: "number",
727
+ default: 8788,
728
+ description: "The port to listen on (serve from)",
729
+ },
730
+ proxy: {
731
+ type: "number",
732
+ description:
733
+ "The port to proxy (where the static assets are served)",
734
+ },
735
+ "script-path": {
736
+ type: "string",
737
+ default: "_worker.js",
738
+ description:
739
+ "The location of the single Worker script if not using functions",
740
+ },
741
+ binding: {
742
+ type: "array",
743
+ description: "Bind variable/secret (KEY=VALUE)",
744
+ alias: "b",
745
+ },
746
+ kv: {
747
+ type: "array",
748
+ description: "KV namespace to bind",
749
+ alias: "k",
750
+ },
751
+ do: {
752
+ type: "array",
753
+ description: "Durable Object to bind (NAME=CLASS)",
754
+ alias: "o",
755
+ },
756
+ "live-reload": {
757
+ type: "boolean",
758
+ default: false,
759
+ description: "Auto reload HTML pages when change is detected",
760
+ },
761
+ // TODO: Miniflare user options
762
+ });
763
+ },
764
+ async ({
765
+ local,
766
+ directory,
767
+ port,
768
+ proxy: requestedProxyPort,
769
+ "script-path": singleWorkerScriptPath,
770
+ binding: bindings = [],
771
+ kv: kvs = [],
772
+ do: durableObjects = [],
773
+ "live-reload": liveReload,
774
+ "--": remaining = [],
775
+ }) => {
776
+ if (!local) {
777
+ console.error("Only local mode is supported at the moment.");
778
+ return;
779
+ }
780
+
781
+ const functionsDirectory = "./functions";
782
+ const usingFunctions = existsSync(functionsDirectory);
783
+
784
+ const command = remaining as (string | number)[];
785
+
786
+ let proxyPort: number | void;
787
+
788
+ if (directory === undefined) {
789
+ proxyPort = await spawnProxyProcess({
790
+ port: requestedProxyPort,
791
+ command,
792
+ });
793
+ if (proxyPort === undefined) return undefined;
794
+ }
795
+
796
+ let miniflareArgs: MiniflareOptions = {};
797
+
798
+ let scriptReadyResolve;
799
+ const scriptReadyPromise = new Promise(
800
+ (resolve) => (scriptReadyResolve = resolve)
801
+ );
802
+
803
+ if (usingFunctions) {
804
+ const scriptPath = join(tmpdir(), "./functionsWorker.js");
805
+
806
+ console.log(`Compiling worker to "${scriptPath}"...`);
807
+
808
+ await buildFunctions({
809
+ scriptPath,
810
+ functionsDirectory,
811
+ sourcemap: true,
812
+ watch: true,
813
+ onEnd: () => scriptReadyResolve(),
814
+ });
815
+
816
+ watch([functionsDirectory], {
817
+ persistent: true,
818
+ ignoreInitial: true,
819
+ }).on("all", async () => {
820
+ await buildFunctions({
821
+ scriptPath,
822
+ functionsDirectory,
823
+ sourcemap: true,
824
+ watch: true,
825
+ onEnd: () => scriptReadyResolve(),
826
+ });
827
+ });
828
+
829
+ miniflareArgs = {
830
+ scriptPath,
831
+ };
832
+ } else {
833
+ scriptReadyResolve();
834
+
835
+ const scriptPath =
836
+ directory !== undefined
837
+ ? join(directory, singleWorkerScriptPath)
838
+ : singleWorkerScriptPath;
839
+
840
+ if (existsSync(scriptPath)) {
841
+ miniflareArgs = {
842
+ scriptPath,
843
+ };
844
+ } else {
845
+ console.log("No functions. Shimming...");
846
+ miniflareArgs = {
847
+ // TODO: The fact that these request/response hacks are necessary is ridiculous.
848
+ // We need to eliminate them from env.ASSETS.fetch (not sure if just local or prod as well)
849
+ script: `
850
+ export default {
851
+ async fetch(request, env, context) {
852
+ const response = await env.ASSETS.fetch(request.url, request)
853
+ return new Response(response.body, response)
854
+ }
855
+ }`,
856
+ };
857
+ }
858
+ }
859
+
860
+ // Defer importing miniflare until we really need it
861
+ const { Miniflare, Log, LogLevel } = await import("miniflare");
862
+ const { Response, fetch } = await import("@miniflare/core");
863
+
864
+ // Wait for esbuild to finish building before starting Miniflare.
865
+ // This must be before the call to `new Miniflare`, as that will
866
+ // asynchronously start loading the script. `await startServer()`
867
+ // internally just waits for that promise to resolve.
868
+ await scriptReadyPromise;
869
+
870
+ // `assetsFetch()` will only be called if there is `proxyPort` defined.
871
+ // We only define `proxyPort`, above, when there is no `directory` defined.
872
+ const assetsFetch =
873
+ directory !== undefined
874
+ ? await generateAssetsFetch(directory)
875
+ : invalidAssetsFetch;
876
+
877
+ const miniflare = new Miniflare({
878
+ port,
879
+ watch: true,
880
+ modules: true,
881
+
882
+ log: new Log(LogLevel.ERROR, { prefix: "pages" }),
883
+ logUnhandledRejections: true,
884
+ sourceMap: true,
885
+
886
+ kvNamespaces: kvs.map((kv) => kv.toString()),
887
+
888
+ durableObjects: Object.fromEntries(
889
+ durableObjects.map((durableObject) =>
890
+ durableObject.toString().split("=")
891
+ )
278
892
  ),
279
893
 
894
+ // User bindings
895
+ bindings: {
896
+ ...Object.fromEntries(
897
+ bindings.map((binding) => binding.toString().split("="))
898
+ ),
899
+ },
900
+
280
901
  // env.ASSETS.fetch
281
- ASSETS: {
282
- fetch: async (
283
- input: RequestInfo,
284
- init?: RequestInit | undefined
285
- ) => {
286
- try {
287
- let request = new Request(input, init);
288
- const url = new URL(request.url);
289
- url.host = `127.0.0.1:${proxyPort}`;
290
- request = new Request(url.toString(), request);
291
- return await fetch(request);
292
- } catch (thrown) {
293
- console.error(`Could not proxy request: ${thrown}`);
294
-
295
- // TODO: Pretty error page
296
- return new Response(
297
- `[wrangler] Could not proxy request: ${thrown}`,
298
- { status: 502 }
299
- );
902
+ serviceBindings: {
903
+ async ASSETS(request) {
904
+ if (proxyPort) {
905
+ try {
906
+ const url = new URL(request.url);
907
+ url.host = `localhost:${proxyPort}`;
908
+ return await fetch(url, request);
909
+ } catch (thrown) {
910
+ console.error(`Could not proxy request: ${thrown}`);
911
+
912
+ // TODO: Pretty error page
913
+ return new Response(
914
+ `[wrangler] Could not proxy request: ${thrown}`,
915
+ { status: 502 }
916
+ );
917
+ }
918
+ } else {
919
+ try {
920
+ return await assetsFetch(request);
921
+ } catch (thrown) {
922
+ console.error(`Could not serve static asset: ${thrown}`);
923
+
924
+ // TODO: Pretty error page
925
+ return new Response(
926
+ `[wrangler] Could not serve static asset: ${thrown}`,
927
+ { status: 502 }
928
+ );
929
+ }
300
930
  }
301
931
  },
302
932
  },
303
- },
304
933
 
305
- ...miniflareArgs,
306
- });
307
- const miniflareServer = await miniflare.createServer();
934
+ kvPersist: true,
935
+ durableObjectsPersist: true,
936
+ cachePersist: true,
937
+ liveReload,
308
938
 
309
- miniflareServer.listen(0, () => {
310
- const address = miniflareServer.address();
311
- if (typeof address === "string")
312
- return exit(
313
- "Could not determine Miniflare's port. Please report this issue to the Wrangler team."
314
- );
939
+ ...miniflareArgs,
940
+ });
315
941
 
316
- const miniflarePort = address.port;
942
+ try {
943
+ // `startServer` might throw if user code contains errors
944
+ const server = await miniflare.startServer();
945
+ console.log(`Serving at http://localhost:${port}/`);
317
946
 
318
- const app = express();
947
+ if (process.env.BROWSER !== "none") {
948
+ const childProcess = await open(`http://localhost:${port}/`);
949
+ // fail silently if the open command doesn't work (e.g. in GitHub Codespaces)
950
+ childProcess.on("error", (_err) => {});
951
+ }
319
952
 
320
- app.use(
321
- "/",
322
- httpProxyMiddleware.createProxyMiddleware({
323
- target: `http://127.0.0.1:${miniflarePort}`,
324
- })
325
- );
953
+ if (directory !== undefined && liveReload) {
954
+ watch([directory], {
955
+ persistent: true,
956
+ ignoreInitial: true,
957
+ }).on("all", async () => {
958
+ await miniflare.reload();
959
+ });
960
+ }
326
961
 
327
- app.use(
328
- "/",
329
- httpProxyMiddleware.createProxyMiddleware({
330
- target: `http://127.0.0.1:${proxyPort}`,
331
- })
332
- );
962
+ EXIT_CALLBACKS.push(() => {
963
+ server.close();
964
+ miniflare.dispose().catch((err) => miniflare.log.error(err));
965
+ });
966
+ } catch (e) {
967
+ miniflare.log.error(e);
968
+ EXIT("Could not start Miniflare.", 1);
969
+ }
970
+ }
971
+ )
972
+ .command("functions", false, (yargs) =>
973
+ // we hide this command from help output because
974
+ // it's not meant to be used directly right now
975
+ yargs.command(
976
+ "build [directory]",
977
+ "Compile a folder of Cloudflare Pages Functions into a single Worker",
978
+ (yargs) =>
979
+ yargs
980
+ .positional("directory", {
981
+ type: "string",
982
+ default: "functions",
983
+ description: "The directory of Pages Functions",
984
+ })
985
+ .options({
986
+ "script-path": {
987
+ type: "string",
988
+ default: "_worker.js",
989
+ description: "The location of the output Worker script",
990
+ },
991
+ "output-config-path": {
992
+ type: "string",
993
+ description: "The location for the output config file",
994
+ },
995
+ minify: {
996
+ type: "boolean",
997
+ default: false,
998
+ description: "Minify the output Worker script",
999
+ },
1000
+ sourcemap: {
1001
+ type: "boolean",
1002
+ default: false,
1003
+ description:
1004
+ "Generate a sourcemap for the output Worker script",
1005
+ },
1006
+ "fallback-service": {
1007
+ type: "string",
1008
+ default: "ASSETS",
1009
+ description:
1010
+ "The service to fallback to at the end of the `next` chain. Setting to '' will fallback to the global `fetch`.",
1011
+ },
1012
+ watch: {
1013
+ type: "boolean",
1014
+ default: false,
1015
+ description:
1016
+ "Watch for changes to the functions and automatically rebuild the Worker script",
1017
+ },
1018
+ }),
1019
+ async ({
1020
+ directory,
1021
+ "script-path": scriptPath,
1022
+ "output-config-path": outputConfigPath,
1023
+ minify,
1024
+ sourcemap,
1025
+ fallbackService,
1026
+ watch,
1027
+ }) => {
1028
+ await buildFunctions({
1029
+ scriptPath,
1030
+ outputConfigPath,
1031
+ functionsDirectory: directory,
1032
+ minify,
1033
+ sourcemap,
1034
+ fallbackService,
1035
+ watch,
1036
+ });
1037
+ }
1038
+ )
1039
+ );
1040
+ };
333
1041
 
334
- app.listen(port, () => {
335
- console.log(`Serving at http://127.0.0.1:${port}/`);
336
- });
337
- });
338
- }
1042
+ const invalidAssetsFetch: typeof fetch = () => {
1043
+ throw new Error(
1044
+ "Trying to fetch assets directly when there is no `directory` option specified, and not in `local` mode."
339
1045
  );
340
1046
  };