wrangler 0.0.0-e14e589 → 0.0.0-e1c615f

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/README.md +51 -0
  2. package/package.json +39 -26
  3. package/pages/functions/buildWorker.ts +57 -0
  4. package/pages/functions/filepath-routing.test.ts +39 -0
  5. package/pages/functions/filepath-routing.ts +221 -0
  6. package/pages/functions/identifiers.ts +78 -0
  7. package/pages/functions/routes.ts +158 -0
  8. package/pages/functions/template-worker.ts +137 -0
  9. package/src/__tests__/dev.test.tsx +66 -0
  10. package/src/__tests__/index.test.ts +254 -120
  11. package/src/__tests__/jest.setup.ts +22 -0
  12. package/src/__tests__/kv.test.ts +1098 -0
  13. package/src/__tests__/mock-cfetch.ts +171 -0
  14. package/src/__tests__/mock-dialogs.ts +65 -0
  15. package/src/__tests__/run-in-tmp.ts +19 -0
  16. package/src/__tests__/run-wrangler.ts +32 -0
  17. package/src/api/form_data.ts +78 -72
  18. package/src/api/inspect.ts +0 -11
  19. package/src/api/preview.ts +29 -24
  20. package/src/api/worker.ts +46 -31
  21. package/src/cfetch/index.ts +102 -0
  22. package/src/cfetch/internal.ts +69 -0
  23. package/src/cli.ts +6 -7
  24. package/src/config.ts +39 -4
  25. package/src/dev.tsx +332 -128
  26. package/src/dialogs.tsx +4 -12
  27. package/src/index.tsx +397 -222
  28. package/src/kv.tsx +101 -45
  29. package/src/module-collection.ts +64 -0
  30. package/src/pages.tsx +839 -211
  31. package/src/proxy.ts +103 -0
  32. package/src/publish.ts +117 -45
  33. package/src/sites.tsx +6 -7
  34. package/src/tail.tsx +6 -4
  35. package/src/user.tsx +9 -16
  36. package/wrangler-dist/cli.js +123794 -528
  37. package/wrangler-dist/cli.js.map +3 -3
  38. package/src/__tests__/fixtures/init/.gitkeep +0 -0
  39. package/src/__tests__/mock-cfetch.js +0 -46
  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 } 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,205 +151,818 @@ 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]}>[^/.]+)`);
220
197
  }
221
198
 
222
- const functionsDirectory = "./functions";
223
- const usingFunctions = existsSync(functionsDirectory);
199
+ const path_matches = rule.matchAll(/:(\w+)/g);
200
+ for (const match of path_matches) {
201
+ rule = rule.split(match[0]).join(`(?<${match[1]}>[^/]+)`);
202
+ }
224
203
 
225
- const proxy = await spawnProxyProcess({
226
- directory,
227
- port: requestedProxyPort,
228
- remaining: remaining as (string | number)[],
229
- });
230
- if (proxy === undefined) return undefined;
204
+ rule = "^" + rule + "$";
205
+
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
+ }
231
230
 
232
- const { port: proxyPort, exit } = proxy;
231
+ function generateHeadersMatcher(headersFile: string) {
232
+ if (existsSync(headersFile)) {
233
+ const contents = readFileSync(headersFile).toString();
233
234
 
234
- let miniflareArgs: MiniflareOptions = {};
235
+ // TODO: Log errors
236
+ const lines = contents
237
+ .split("\n")
238
+ .map((line) => line.trim())
239
+ .filter((line) => !line.startsWith("#") && line !== "");
235
240
 
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
- };
243
- } 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}.`
252
- );
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;
253
249
  }
254
250
 
255
- miniflareArgs = {
256
- scriptPath,
257
- };
251
+ const path = validateURL(line);
252
+ if (path) {
253
+ rule = {
254
+ path,
255
+ headers: {},
256
+ };
257
+ continue;
258
+ }
258
259
  }
259
260
 
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,
261
+ if (!line.includes(":")) continue;
265
262
 
266
- kvNamespaces: kvs.map((kv) => kv.toString()),
263
+ const [rawName, ...rawValue] = line.split(":");
264
+ const name = rawName.trim().toLowerCase();
265
+ const value = rawValue.join(":").trim();
267
266
 
268
- durableObjects: Object.fromEntries(
269
- durableObjects.map((durableObject) =>
270
- durableObject.toString().split("=")
271
- )
272
- ),
267
+ if (name === "") continue;
268
+ if (!rule) continue;
273
269
 
274
- bindings: {
275
- // User bindings
276
- ...Object.fromEntries(
277
- bindings.map((binding) => binding.toString().split("="))
278
- ),
270
+ const existingValues = rule.headers[name];
271
+ rule.headers[name] = existingValues
272
+ ? `${existingValues}, ${value}`
273
+ : value;
274
+ }
279
275
 
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
- },
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
+ );
304
288
 
305
- ...miniflareArgs,
289
+ return (request: Request) => {
290
+ const matches = rulesMatcher({
291
+ request,
306
292
  });
307
- const miniflareServer = await miniflare.createServer();
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
+ );
308
337
 
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."
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
+ }
392
+
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");
396
+
397
+ const headersFile = join(directory, "_headers");
398
+ const redirectsFile = join(directory, "_redirects");
399
+ const workerFile = join(directory, "_worker.js");
400
+
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;
472
+ } else {
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"
314
496
  );
497
+ return deconstructedResponse;
498
+ }
499
+ }
315
500
 
316
- const miniflarePort = address.port;
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;
508
+ }
317
509
 
318
- const app = express();
510
+ deconstructedResponse.status = 404;
511
+ return deconstructedResponse;
512
+ };
319
513
 
320
- app.use(
321
- "/",
322
- httpProxyMiddleware.createProxyMiddleware({
323
- target: `http://127.0.0.1:${miniflarePort}`,
324
- })
514
+ let asset;
515
+
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"
325
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
+ }
326
535
 
327
- app.use(
328
- "/",
329
- httpProxyMiddleware.createProxyMiddleware({
330
- target: `http://127.0.0.1:${proxyPort}`,
331
- })
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
+ }
544
+
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"
332
568
  );
569
+ return deconstructedResponse;
570
+ }
571
+ } else if (hasFileExtension(url.pathname)) {
572
+ notFound();
573
+ return deconstructedResponse;
574
+ }
333
575
 
334
- app.listen(port, () => {
335
- console.log(`Serving at http://127.0.0.1:${port}/`);
336
- });
337
- });
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;
338
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
+ };
597
+
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) || [];
605
+
606
+ matches.forEach((match) => {
607
+ Object.entries(match).forEach(([name, value]) => {
608
+ newHeaders.append(name, `${value}`);
609
+ });
610
+ });
611
+
612
+ const combinedHeaders = {
613
+ ...Object.fromEntries(headers.entries()),
614
+ ...Object.fromEntries(newHeaders.entries()),
615
+ };
616
+
617
+ deconstructedResponse.headers = new Headers({});
618
+ Object.entries(combinedHeaders).forEach(([name, value]) => {
619
+ if (value) deconstructedResponse.headers.set(name, value);
620
+ });
621
+ };
622
+
623
+ return async (input, init) => {
624
+ const request = new Request(input, init);
625
+ const deconstructedResponse = generateResponse(request);
626
+ attachHeaders(request, deconstructedResponse);
627
+
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
+ functionsDirectory,
646
+ minify = false,
647
+ sourcemap = false,
648
+ watch = false,
649
+ onEnd,
650
+ }: {
651
+ scriptPath: string;
652
+ functionsDirectory: string;
653
+ minify?: boolean;
654
+ sourcemap?: boolean;
655
+ watch?: boolean;
656
+ onEnd?: () => void;
657
+ }) {
658
+ RUNNING_BUILDERS.forEach(
659
+ (runningBuilder) => runningBuilder.stop && runningBuilder.stop()
660
+ );
661
+
662
+ const routesModule = join(tmpdir(), "./functionsRoutes.mjs");
663
+
664
+ const config: Config = await generateConfigFromFileTree({
665
+ baseDir: functionsDirectory,
666
+ baseURL: "/",
667
+ });
668
+
669
+ await writeRoutesModule({
670
+ config,
671
+ srcDir: functionsDirectory,
672
+ outfile: routesModule,
673
+ });
674
+
675
+ RUNNING_BUILDERS.push(
676
+ await buildWorker({
677
+ routesModule,
678
+ outfile: scriptPath,
679
+ minify,
680
+ sourcemap,
681
+ watch,
682
+ onEnd,
683
+ })
339
684
  );
685
+ }
686
+
687
+ export const pages: BuilderCallback<unknown, unknown> = (yargs) => {
688
+ return yargs
689
+ .command(
690
+ "dev [directory] [-- command]",
691
+ "🧑‍💻 Develop your full-stack Pages application locally",
692
+ (yargs) => {
693
+ return yargs
694
+ .positional("directory", {
695
+ type: "string",
696
+ demandOption: undefined,
697
+ description: "The directory of static assets to serve",
698
+ })
699
+ .positional("command", {
700
+ type: "string",
701
+ demandOption: undefined,
702
+ description: "The proxy command to run",
703
+ })
704
+ .options({
705
+ local: {
706
+ type: "boolean",
707
+ default: true,
708
+ description: "Run on my machine",
709
+ },
710
+ port: {
711
+ type: "number",
712
+ default: 8788,
713
+ description: "The port to listen on (serve from)",
714
+ },
715
+ proxy: {
716
+ type: "number",
717
+ description:
718
+ "The port to proxy (where the static assets are served)",
719
+ },
720
+ "script-path": {
721
+ type: "string",
722
+ default: "_worker.js",
723
+ description:
724
+ "The location of the single Worker script if not using functions",
725
+ },
726
+ binding: {
727
+ type: "array",
728
+ description: "Bind variable/secret (KEY=VALUE)",
729
+ alias: "b",
730
+ },
731
+ kv: {
732
+ type: "array",
733
+ description: "KV namespace to bind",
734
+ alias: "k",
735
+ },
736
+ do: {
737
+ type: "array",
738
+ description: "Durable Object to bind (NAME=CLASS)",
739
+ alias: "o",
740
+ },
741
+ // TODO: Miniflare user options
742
+ });
743
+ },
744
+ async ({
745
+ local,
746
+ directory,
747
+ port,
748
+ proxy: requestedProxyPort,
749
+ "script-path": singleWorkerScriptPath,
750
+ binding: bindings = [],
751
+ kv: kvs = [],
752
+ do: durableObjects = [],
753
+ "--": remaining = [],
754
+ }) => {
755
+ if (!local) {
756
+ console.error("Only local mode is supported at the moment.");
757
+ return;
758
+ }
759
+
760
+ const functionsDirectory = "./functions";
761
+ const usingFunctions = existsSync(functionsDirectory);
762
+
763
+ const command = remaining as (string | number)[];
764
+
765
+ let proxyPort: number | void;
766
+
767
+ if (directory === undefined) {
768
+ proxyPort = await spawnProxyProcess({
769
+ port: requestedProxyPort,
770
+ command,
771
+ });
772
+ if (proxyPort === undefined) return undefined;
773
+ }
774
+
775
+ let miniflareArgs: MiniflareOptions = {};
776
+
777
+ let scriptReady = true;
778
+
779
+ if (usingFunctions) {
780
+ const scriptPath = join(tmpdir(), "./functionsWorker.js");
781
+
782
+ console.log(`Compiling worker to "${scriptPath}"...`);
783
+
784
+ await buildFunctions({
785
+ scriptPath,
786
+ functionsDirectory,
787
+ sourcemap: true,
788
+ watch: true,
789
+ onEnd: () => {
790
+ scriptReady = true;
791
+ },
792
+ });
793
+
794
+ watch([functionsDirectory], {
795
+ persistent: true,
796
+ }).on("all", async () => {
797
+ await buildFunctions({
798
+ scriptPath,
799
+ functionsDirectory,
800
+ sourcemap: true,
801
+ watch: true,
802
+ onEnd: () => {
803
+ scriptReady = true;
804
+ },
805
+ });
806
+ });
807
+
808
+ miniflareArgs = {
809
+ scriptPath,
810
+ };
811
+ } else {
812
+ const scriptPath =
813
+ directory !== undefined
814
+ ? join(directory, singleWorkerScriptPath)
815
+ : singleWorkerScriptPath;
816
+
817
+ if (existsSync(scriptPath)) {
818
+ miniflareArgs = {
819
+ scriptPath,
820
+ };
821
+ } else {
822
+ console.log("No functions. Shimming...");
823
+ miniflareArgs = {
824
+ // TODO: The fact that these request/response hacks are necessary is ridiculous.
825
+ // We need to eliminate them from env.ASSETS.fetch (not sure if just local or prod as well)
826
+ script: `
827
+ export default {
828
+ async fetch(request, env, context) {
829
+ const response = await env.ASSETS.fetch(request.url, request)
830
+ return new Response(response.body, response)
831
+ }
832
+ }`,
833
+ };
834
+ }
835
+ }
836
+
837
+ // Defer importing miniflare until we really need it
838
+ const { Miniflare, Log, LogLevel } = await import("miniflare");
839
+ const { Response, fetch } = await import("@miniflare/core");
840
+
841
+ // Should only be called if no proxyPort, using `assert.fail()` here
842
+ // means the type of `assetsFetch` is still `typeof fetch`
843
+ const assetsFetch = proxyPort
844
+ ? () => assert.fail()
845
+ : await generateAssetsFetch(directory);
846
+ const miniflare = new Miniflare({
847
+ port,
848
+ watch: true,
849
+ modules: true,
850
+
851
+ log: new Log(LogLevel.ERROR, { prefix: "pages" }),
852
+ logUnhandledRejections: true,
853
+ sourceMap: true,
854
+
855
+ kvNamespaces: kvs.map((kv) => kv.toString()),
856
+
857
+ durableObjects: Object.fromEntries(
858
+ durableObjects.map((durableObject) =>
859
+ durableObject.toString().split("=")
860
+ )
861
+ ),
862
+
863
+ // User bindings
864
+ bindings: {
865
+ ...Object.fromEntries(
866
+ bindings.map((binding) => binding.toString().split("="))
867
+ ),
868
+ },
869
+
870
+ // env.ASSETS.fetch
871
+ serviceBindings: {
872
+ async ASSETS(request) {
873
+ if (proxyPort) {
874
+ try {
875
+ const url = new URL(request.url);
876
+ url.host = `127.0.0.1:${proxyPort}`;
877
+ return await fetch(url, request);
878
+ } catch (thrown) {
879
+ console.error(`Could not proxy request: ${thrown}`);
880
+
881
+ // TODO: Pretty error page
882
+ return new Response(
883
+ `[wrangler] Could not proxy request: ${thrown}`,
884
+ { status: 502 }
885
+ );
886
+ }
887
+ } else {
888
+ try {
889
+ return await assetsFetch(request);
890
+ } catch (thrown) {
891
+ console.error(`Could not serve static asset: ${thrown}`);
892
+
893
+ // TODO: Pretty error page
894
+ return new Response(
895
+ `[wrangler] Could not serve static asset: ${thrown}`,
896
+ { status: 502 }
897
+ );
898
+ }
899
+ }
900
+ },
901
+ },
902
+
903
+ kvPersist: true,
904
+ durableObjectsPersist: true,
905
+ cachePersist: true,
906
+
907
+ ...miniflareArgs,
908
+ });
909
+
910
+ // Wait for esbuild to finish building the script
911
+ while (!scriptReady) {}
912
+
913
+ try {
914
+ // `startServer` might throw if user code contains errors
915
+ const server = await miniflare.startServer();
916
+ console.log(`Serving at http://127.0.0.1:${port}/`);
917
+
918
+ if (process.env.BROWSER !== "none") {
919
+ try {
920
+ await open(`http://127.0.0.1:${port}/`);
921
+ } catch {}
922
+ }
923
+
924
+ EXIT_CALLBACKS.push(() => {
925
+ server.close();
926
+ miniflare.dispose().catch((err) => miniflare.log.error(err));
927
+ });
928
+ } catch (e) {
929
+ miniflare.log.error(e);
930
+ EXIT("Could not start Miniflare.", 1);
931
+ }
932
+ }
933
+ )
934
+ .command("functions", "Cloudflare Pages Functions", (yargs) =>
935
+ yargs.command(
936
+ "build",
937
+ "Compile a folder of Cloudflare Pages Functions into a single Worker",
938
+ (yargs) =>
939
+ yargs.options({
940
+ "script-path": {
941
+ type: "string",
942
+ default: "_worker.js",
943
+ description: "The location of the output Worker script",
944
+ },
945
+ minify: {
946
+ type: "boolean",
947
+ default: false,
948
+ description: "Minify the output Worker script",
949
+ },
950
+ sourcemap: {
951
+ type: "boolean",
952
+ default: false,
953
+ description: "Generate a sourcemap for the output Worker script",
954
+ },
955
+ }),
956
+ async ({ "script-path": scriptPath, minify, sourcemap }) => {
957
+ const functionsDirectory = "./functions";
958
+
959
+ await buildFunctions({
960
+ scriptPath,
961
+ functionsDirectory,
962
+ minify,
963
+ sourcemap,
964
+ });
965
+ }
966
+ )
967
+ );
340
968
  };