wrangler 0.0.0-e6733a3 → 0.0.0-e6ada079

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.

Potentially problematic release.


This version of wrangler might be problematic. Click here for more details.

Files changed (119) hide show
  1. package/README.md +47 -16
  2. package/bin/wrangler.js +94 -31
  3. package/config-schema.json +3100 -0
  4. package/kv-asset-handler.js +1 -0
  5. package/package.json +154 -82
  6. package/templates/__tests__/pages-dev-util.test.ts +128 -0
  7. package/templates/__tests__/tsconfig-sanity.ts +12 -0
  8. package/templates/__tests__/tsconfig.json +8 -0
  9. package/templates/checked-fetch.js +30 -0
  10. package/templates/facade.d.ts +19 -0
  11. package/templates/gitignore +170 -0
  12. package/templates/init-tests/test-jest-new-worker.js +23 -0
  13. package/templates/init-tests/test-vitest-new-worker.js +24 -0
  14. package/templates/init-tests/test-vitest-new-worker.ts +25 -0
  15. package/templates/middleware/common.ts +67 -0
  16. package/templates/middleware/loader-modules.ts +134 -0
  17. package/templates/middleware/loader-sw.ts +229 -0
  18. package/templates/middleware/middleware-ensure-req-body-drained.ts +18 -0
  19. package/templates/middleware/middleware-miniflare3-json-error.ts +32 -0
  20. package/templates/middleware/middleware-pretty-error.ts +40 -0
  21. package/templates/middleware/middleware-scheduled.ts +15 -0
  22. package/templates/middleware/middleware-serve-static-assets.d.ts +6 -0
  23. package/templates/middleware/middleware-serve-static-assets.ts +56 -0
  24. package/templates/modules-watch-stub.js +4 -0
  25. package/templates/new-worker-scheduled.js +17 -0
  26. package/templates/new-worker-scheduled.ts +32 -0
  27. package/templates/new-worker.js +15 -0
  28. package/templates/new-worker.ts +33 -0
  29. package/templates/no-op-worker.js +10 -0
  30. package/templates/pages-dev-pipeline.ts +32 -0
  31. package/templates/pages-dev-util.ts +55 -0
  32. package/templates/pages-shim.ts +9 -0
  33. package/templates/pages-template-plugin.ts +190 -0
  34. package/templates/pages-template-worker.ts +198 -0
  35. package/templates/startDevWorker/InspectorProxyWorker.ts +664 -0
  36. package/templates/startDevWorker/ProxyWorker.ts +334 -0
  37. package/templates/tsconfig-sanity.ts +11 -0
  38. package/templates/tsconfig.init.json +22 -0
  39. package/templates/tsconfig.json +8 -0
  40. package/wrangler-dist/InspectorProxyWorker.js +464 -0
  41. package/wrangler-dist/InspectorProxyWorker.js.map +6 -0
  42. package/wrangler-dist/ProxyWorker.js +240 -0
  43. package/wrangler-dist/ProxyWorker.js.map +6 -0
  44. package/wrangler-dist/cli.d.ts +26391 -0
  45. package/wrangler-dist/cli.js +204293 -116652
  46. package/wrangler-dist/wasm-sync.wasm +0 -0
  47. package/import_meta_url.js +0 -3
  48. package/miniflare-config-stubs/.env.empty +0 -0
  49. package/miniflare-config-stubs/package.empty.json +0 -1
  50. package/miniflare-config-stubs/wrangler.empty.toml +0 -0
  51. package/pages/functions/buildWorker.ts +0 -62
  52. package/pages/functions/filepath-routing.test.ts +0 -39
  53. package/pages/functions/filepath-routing.ts +0 -221
  54. package/pages/functions/identifiers.ts +0 -78
  55. package/pages/functions/routes.ts +0 -158
  56. package/pages/functions/template-worker.ts +0 -144
  57. package/src/__tests__/clipboardy-mock.js +0 -4
  58. package/src/__tests__/dev.test.tsx +0 -66
  59. package/src/__tests__/index.test.ts +0 -287
  60. package/src/__tests__/jest.setup.ts +0 -22
  61. package/src/__tests__/kv.test.ts +0 -1098
  62. package/src/__tests__/mock-cfetch.ts +0 -171
  63. package/src/__tests__/mock-dialogs.ts +0 -65
  64. package/src/__tests__/run-in-tmp.ts +0 -19
  65. package/src/__tests__/run-wrangler.ts +0 -32
  66. package/src/api/form_data.ts +0 -131
  67. package/src/api/preview.ts +0 -128
  68. package/src/api/worker.ts +0 -155
  69. package/src/cfetch/index.ts +0 -102
  70. package/src/cfetch/internal.ts +0 -69
  71. package/src/cli.ts +0 -9
  72. package/src/config.ts +0 -487
  73. package/src/dev.tsx +0 -771
  74. package/src/dialogs.tsx +0 -77
  75. package/src/index.tsx +0 -1974
  76. package/src/inspect.ts +0 -524
  77. package/src/kv.tsx +0 -267
  78. package/src/module-collection.ts +0 -64
  79. package/src/pages.tsx +0 -1031
  80. package/src/proxy.ts +0 -294
  81. package/src/publish.ts +0 -358
  82. package/src/sites.tsx +0 -114
  83. package/src/tail.tsx +0 -73
  84. package/src/user.tsx +0 -1025
  85. package/static-asset-facade.js +0 -47
  86. package/vendor/@cloudflare/kv-asset-handler/CHANGELOG.md +0 -332
  87. package/vendor/@cloudflare/kv-asset-handler/LICENSE_APACHE +0 -176
  88. package/vendor/@cloudflare/kv-asset-handler/LICENSE_MIT +0 -25
  89. package/vendor/@cloudflare/kv-asset-handler/README.md +0 -245
  90. package/vendor/@cloudflare/kv-asset-handler/dist/index.d.ts +0 -32
  91. package/vendor/@cloudflare/kv-asset-handler/dist/index.js +0 -354
  92. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.d.ts +0 -13
  93. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.js +0 -148
  94. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.d.ts +0 -1
  95. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.js +0 -436
  96. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.d.ts +0 -1
  97. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.js +0 -40
  98. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.d.ts +0 -1
  99. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.js +0 -42
  100. package/vendor/@cloudflare/kv-asset-handler/dist/types.d.ts +0 -26
  101. package/vendor/@cloudflare/kv-asset-handler/dist/types.js +0 -31
  102. package/vendor/@cloudflare/kv-asset-handler/package.json +0 -52
  103. package/vendor/@cloudflare/kv-asset-handler/src/index.ts +0 -296
  104. package/vendor/@cloudflare/kv-asset-handler/src/mocks.ts +0 -136
  105. package/vendor/@cloudflare/kv-asset-handler/src/test/getAssetFromKV.ts +0 -464
  106. package/vendor/@cloudflare/kv-asset-handler/src/test/mapRequestToAsset.ts +0 -33
  107. package/vendor/@cloudflare/kv-asset-handler/src/test/serveSinglePageApp.ts +0 -42
  108. package/vendor/@cloudflare/kv-asset-handler/src/types.ts +0 -39
  109. package/vendor/wrangler-mime/CHANGELOG.md +0 -289
  110. package/vendor/wrangler-mime/LICENSE +0 -21
  111. package/vendor/wrangler-mime/Mime.js +0 -97
  112. package/vendor/wrangler-mime/README.md +0 -187
  113. package/vendor/wrangler-mime/cli.js +0 -46
  114. package/vendor/wrangler-mime/index.js +0 -4
  115. package/vendor/wrangler-mime/lite.js +0 -4
  116. package/vendor/wrangler-mime/package.json +0 -52
  117. package/vendor/wrangler-mime/types/other.js +0 -1
  118. package/vendor/wrangler-mime/types/standard.js +0 -1
  119. package/wrangler-dist/cli.js.map +0 -7
package/src/pages.tsx DELETED
@@ -1,1031 +0,0 @@
1
- /* eslint-disable no-shadow */
2
-
3
- import assert from "assert";
4
- import type { BuilderCallback } from "yargs";
5
- import { join } from "path";
6
- import { tmpdir } from "os";
7
- import { existsSync, lstatSync, readFileSync, writeFileSync } from "fs";
8
- import { execSync, spawn } from "child_process";
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";
23
- import type { MiniflareOptions } from "miniflare";
24
-
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());
35
-
36
- function isWindows() {
37
- return process.platform === "win32";
38
- }
39
-
40
- const SECONDS_TO_WAIT_FOR_PROXY = 5;
41
-
42
- async function sleep(ms: number) {
43
- await new Promise((resolve) => setTimeout(resolve, ms));
44
- }
45
-
46
- function getPids(pid: number) {
47
- const pids: number[] = [pid];
48
- let command: string, regExp: RegExp;
49
-
50
- if (isWindows()) {
51
- command = `wmic process where (ParentProcessId=${pid}) get ProcessId`;
52
- regExp = new RegExp(/(\d+)/);
53
- } else {
54
- command = `pgrep -P ${pid}`;
55
- regExp = new RegExp(/(\d+)/);
56
- }
57
-
58
- try {
59
- const newPids = (
60
- execSync(command)
61
- .toString()
62
- .split("\n")
63
- .map((line) => line.match(regExp))
64
- .filter((line) => line !== null) as RegExpExecArray[]
65
- ).map((match) => parseInt(match[1]));
66
-
67
- pids.push(...newPids.map(getPids).flat());
68
- } catch {}
69
-
70
- return pids;
71
- }
72
-
73
- function getPort(pid: number) {
74
- let command: string, regExp: RegExp;
75
-
76
- if (isWindows()) {
77
- command = "\\windows\\system32\\netstat.exe -nao";
78
- regExp = new RegExp(`TCP\\s+.*:(\\d+)\\s+.*:\\d+\\s+LISTENING\\s+${pid}`);
79
- } else {
80
- command = "lsof -nPi";
81
- regExp = new RegExp(`${pid}\\s+.*TCP\\s+.*:(\\d+)\\s+\\(LISTEN\\)`);
82
- }
83
-
84
- try {
85
- const matches = execSync(command)
86
- .toString()
87
- .split("\n")
88
- .map((line) => line.match(regExp))
89
- .filter((line) => line !== null) as RegExpExecArray[];
90
-
91
- const match = matches[0];
92
- if (match) return parseInt(match[1]);
93
- } catch (thrown) {
94
- console.error(
95
- `Error scanning for ports of process with PID ${pid}: ${thrown}`
96
- );
97
- }
98
- }
99
-
100
- async function spawnProxyProcess({
101
- port,
102
- command,
103
- }: {
104
- port?: number;
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
111
- );
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());
126
-
127
- proxy.stdout.on("data", (data) => {
128
- console.log(`[proxy]: ${data}`);
129
- });
130
-
131
- proxy.stderr.on("data", (data) => {
132
- console.error(`[proxy]: ${data}`);
133
- });
134
-
135
- proxy.on("close", (code) => {
136
- console.error(`Proxy exited with status ${code}.`);
137
- });
138
-
139
- // Wait for proxy process to start...
140
- while (!proxy.pid) {}
141
-
142
- if (port === undefined) {
143
- console.log(
144
- `Sleeping ${SECONDS_TO_WAIT_FOR_PROXY} seconds to allow proxy process to start before attempting to automatically determine port...`
145
- );
146
- console.log("To skip, specify the proxy port with --proxy.");
147
- await sleep(SECONDS_TO_WAIT_FOR_PROXY * 1000);
148
-
149
- port = getPids(proxy.pid)
150
- .map(getPort)
151
- .filter((port) => port !== undefined)[0];
152
-
153
- if (port === undefined) {
154
- return EXIT(
155
- "Could not automatically determine proxy port. Please specify the proxy port with --proxy.",
156
- 1
157
- );
158
- } else {
159
- console.log(`Automatically determined the proxy port to be ${port}.`);
160
- }
161
- }
162
-
163
- return port;
164
- }
165
-
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]}>[^/]+)`);
202
- }
203
-
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
- }
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,
292
- });
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
- }
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"
496
- );
497
- return deconstructedResponse;
498
- }
499
- }
500
-
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
- }
509
-
510
- deconstructedResponse.status = 404;
511
- return deconstructedResponse;
512
- };
513
-
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"
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
- }
535
-
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"
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
- };
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
- 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
- );
665
-
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",
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)
798
- );
799
-
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,
933
- });
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
- );
1031
- };