wrangler 0.0.0-ece06ea → 0.0.0-edc4b53

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