wrangler 0.0.0-eebdb6c → 0.0.0-eec2a7074

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 (120) hide show
  1. package/README.md +50 -15
  2. package/bin/wrangler.js +94 -31
  3. package/config-schema.json +3126 -0
  4. package/kv-asset-handler.js +1 -0
  5. package/package.json +155 -81
  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-mock-analytics-engine.d.ts +3 -0
  21. package/templates/middleware/middleware-mock-analytics-engine.ts +30 -0
  22. package/templates/middleware/middleware-pretty-error.ts +40 -0
  23. package/templates/middleware/middleware-scheduled.ts +29 -0
  24. package/templates/middleware/middleware-serve-static-assets.d.ts +6 -0
  25. package/templates/middleware/middleware-serve-static-assets.ts +56 -0
  26. package/templates/modules-watch-stub.js +4 -0
  27. package/templates/new-worker-scheduled.js +17 -0
  28. package/templates/new-worker-scheduled.ts +32 -0
  29. package/templates/new-worker.js +15 -0
  30. package/templates/new-worker.ts +33 -0
  31. package/templates/no-op-worker.js +10 -0
  32. package/templates/pages-dev-pipeline.ts +32 -0
  33. package/templates/pages-dev-util.ts +55 -0
  34. package/templates/pages-shim.ts +9 -0
  35. package/templates/pages-template-plugin.ts +190 -0
  36. package/templates/pages-template-worker.ts +198 -0
  37. package/templates/startDevWorker/InspectorProxyWorker.ts +664 -0
  38. package/templates/startDevWorker/ProxyWorker.ts +336 -0
  39. package/templates/tsconfig-sanity.ts +11 -0
  40. package/templates/tsconfig.init.json +22 -0
  41. package/templates/tsconfig.json +8 -0
  42. package/wrangler-dist/InspectorProxyWorker.js +464 -0
  43. package/wrangler-dist/InspectorProxyWorker.js.map +6 -0
  44. package/wrangler-dist/ProxyWorker.js +241 -0
  45. package/wrangler-dist/ProxyWorker.js.map +6 -0
  46. package/wrangler-dist/cli.d.ts +26506 -0
  47. package/wrangler-dist/cli.js +210065 -116154
  48. package/wrangler-dist/wasm-sync.wasm +0 -0
  49. package/import_meta_url.js +0 -3
  50. package/miniflare-config-stubs/.env.empty +0 -0
  51. package/miniflare-config-stubs/package.empty.json +0 -1
  52. package/miniflare-config-stubs/wrangler.empty.toml +0 -0
  53. package/pages/functions/buildWorker.ts +0 -57
  54. package/pages/functions/filepath-routing.test.ts +0 -32
  55. package/pages/functions/filepath-routing.ts +0 -225
  56. package/pages/functions/identifiers.ts +0 -78
  57. package/pages/functions/routes.ts +0 -158
  58. package/pages/functions/template-worker.ts +0 -135
  59. package/src/__tests__/clipboardy-mock.js +0 -4
  60. package/src/__tests__/index.test.ts +0 -287
  61. package/src/__tests__/jest.setup.ts +0 -22
  62. package/src/__tests__/kv.test.ts +0 -1098
  63. package/src/__tests__/mock-cfetch.ts +0 -171
  64. package/src/__tests__/mock-dialogs.ts +0 -65
  65. package/src/__tests__/run-in-tmp.ts +0 -19
  66. package/src/__tests__/run-wrangler.ts +0 -32
  67. package/src/api/form_data.ts +0 -131
  68. package/src/api/inspect.ts +0 -430
  69. package/src/api/preview.ts +0 -128
  70. package/src/api/worker.ts +0 -155
  71. package/src/cfetch/index.ts +0 -103
  72. package/src/cfetch/internal.ts +0 -69
  73. package/src/cli.ts +0 -9
  74. package/src/config.ts +0 -131
  75. package/src/dev.tsx +0 -879
  76. package/src/dialogs.tsx +0 -77
  77. package/src/index.tsx +0 -1904
  78. package/src/kv.tsx +0 -267
  79. package/src/module-collection.ts +0 -64
  80. package/src/pages.tsx +0 -921
  81. package/src/proxy.ts +0 -103
  82. package/src/publish.ts +0 -350
  83. package/src/sites.tsx +0 -114
  84. package/src/tail.tsx +0 -73
  85. package/src/user.tsx +0 -1022
  86. package/static-asset-facade.js +0 -47
  87. package/vendor/@cloudflare/kv-asset-handler/CHANGELOG.md +0 -332
  88. package/vendor/@cloudflare/kv-asset-handler/LICENSE_APACHE +0 -176
  89. package/vendor/@cloudflare/kv-asset-handler/LICENSE_MIT +0 -25
  90. package/vendor/@cloudflare/kv-asset-handler/README.md +0 -245
  91. package/vendor/@cloudflare/kv-asset-handler/dist/index.d.ts +0 -32
  92. package/vendor/@cloudflare/kv-asset-handler/dist/index.js +0 -354
  93. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.d.ts +0 -13
  94. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.js +0 -148
  95. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.d.ts +0 -1
  96. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.js +0 -436
  97. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.d.ts +0 -1
  98. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.js +0 -40
  99. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.d.ts +0 -1
  100. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.js +0 -42
  101. package/vendor/@cloudflare/kv-asset-handler/dist/types.d.ts +0 -26
  102. package/vendor/@cloudflare/kv-asset-handler/dist/types.js +0 -31
  103. package/vendor/@cloudflare/kv-asset-handler/package.json +0 -52
  104. package/vendor/@cloudflare/kv-asset-handler/src/index.ts +0 -296
  105. package/vendor/@cloudflare/kv-asset-handler/src/mocks.ts +0 -136
  106. package/vendor/@cloudflare/kv-asset-handler/src/test/getAssetFromKV.ts +0 -464
  107. package/vendor/@cloudflare/kv-asset-handler/src/test/mapRequestToAsset.ts +0 -33
  108. package/vendor/@cloudflare/kv-asset-handler/src/test/serveSinglePageApp.ts +0 -42
  109. package/vendor/@cloudflare/kv-asset-handler/src/types.ts +0 -39
  110. package/vendor/wrangler-mime/CHANGELOG.md +0 -289
  111. package/vendor/wrangler-mime/LICENSE +0 -21
  112. package/vendor/wrangler-mime/Mime.js +0 -97
  113. package/vendor/wrangler-mime/README.md +0 -187
  114. package/vendor/wrangler-mime/cli.js +0 -46
  115. package/vendor/wrangler-mime/index.js +0 -4
  116. package/vendor/wrangler-mime/lite.js +0 -4
  117. package/vendor/wrangler-mime/package.json +0 -52
  118. package/vendor/wrangler-mime/types/other.js +0 -1
  119. package/vendor/wrangler-mime/types/standard.js +0 -1
  120. package/wrangler-dist/cli.js.map +0 -7
package/src/pages.tsx DELETED
@@ -1,921 +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 } 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
- functionsDirectory,
646
- onEnd,
647
- }: {
648
- scriptPath: string;
649
- functionsDirectory: string;
650
- onEnd?: () => void;
651
- }) {
652
- RUNNING_BUILDERS.forEach(
653
- (runningBuilder) => runningBuilder.stop && runningBuilder.stop()
654
- );
655
-
656
- const routesModule = join(tmpdir(), "./functionsRoutes.mjs");
657
-
658
- const config: Config = await generateConfigFromFileTree({
659
- baseDir: functionsDirectory,
660
- baseURL: "/",
661
- });
662
-
663
- await writeRoutesModule({
664
- config,
665
- srcDir: functionsDirectory,
666
- outfile: routesModule,
667
- });
668
-
669
- RUNNING_BUILDERS.push(
670
- await buildWorker({
671
- routesModule,
672
- outfile: scriptPath,
673
- minify: false, // TODO: Expose option to enable
674
- sourcemap: true,
675
- watch: true,
676
- onEnd,
677
- })
678
- );
679
- }
680
-
681
- export const pages: BuilderCallback<unknown, unknown> = (yargs) => {
682
- return yargs.command(
683
- "dev [directory] [-- command]",
684
- "🧑‍💻 Develop your full-stack Pages application locally",
685
- (yargs) => {
686
- return yargs
687
- .positional("directory", {
688
- type: "string",
689
- demandOption: undefined,
690
- description: "The directory of static assets to serve",
691
- })
692
- .positional("command", {
693
- type: "string",
694
- demandOption: undefined,
695
- description: "The proxy command to run",
696
- })
697
- .options({
698
- local: {
699
- type: "boolean",
700
- default: true,
701
- description: "Run on my machine",
702
- },
703
- port: {
704
- type: "number",
705
- default: 8788,
706
- description: "The port to listen on (serve from)",
707
- },
708
- proxy: {
709
- type: "number",
710
- description:
711
- "The port to proxy (where the static assets are served)",
712
- },
713
- "script-path": {
714
- type: "string",
715
- default: "_worker.js",
716
- description:
717
- "The location of the single Worker script if not using functions",
718
- },
719
- binding: {
720
- type: "array",
721
- description: "Bind variable/secret (KEY=VALUE)",
722
- alias: "b",
723
- },
724
- kv: {
725
- type: "array",
726
- description: "KV namespace to bind",
727
- alias: "k",
728
- },
729
- do: {
730
- type: "array",
731
- description: "Durable Object to bind (NAME=CLASS)",
732
- alias: "o",
733
- },
734
- // TODO: Miniflare user options
735
- });
736
- },
737
- async ({
738
- local,
739
- directory,
740
- port,
741
- proxy: requestedProxyPort,
742
- "script-path": singleWorkerScriptPath,
743
- binding: bindings = [],
744
- kv: kvs = [],
745
- do: durableObjects = [],
746
- "--": remaining = [],
747
- }) => {
748
- if (!local) {
749
- console.error("Only local mode is supported at the moment.");
750
- return;
751
- }
752
-
753
- const functionsDirectory = "./functions";
754
- const usingFunctions = existsSync(functionsDirectory);
755
-
756
- const command = remaining as (string | number)[];
757
-
758
- let proxyPort: number | void;
759
-
760
- if (directory === undefined) {
761
- proxyPort = await spawnProxyProcess({
762
- port: requestedProxyPort,
763
- command,
764
- });
765
- if (proxyPort === undefined) return undefined;
766
- }
767
-
768
- let miniflareArgs: MiniflareOptions = {};
769
-
770
- let scriptReady = true;
771
-
772
- if (usingFunctions) {
773
- const scriptPath = join(tmpdir(), "./functionsWorker.js");
774
-
775
- console.log(`Compiling worker to "${scriptPath}"...`);
776
-
777
- await buildFunctions({
778
- scriptPath,
779
- functionsDirectory,
780
- onEnd: () => {
781
- scriptReady = true;
782
- },
783
- });
784
-
785
- watch([functionsDirectory], {
786
- persistent: true,
787
- }).on("all", async () => {
788
- await buildFunctions({
789
- scriptPath,
790
- functionsDirectory,
791
- onEnd: () => {
792
- scriptReady = true;
793
- },
794
- });
795
- });
796
-
797
- miniflareArgs = {
798
- scriptPath,
799
- };
800
- } else {
801
- const scriptPath =
802
- directory !== undefined
803
- ? join(directory, singleWorkerScriptPath)
804
- : singleWorkerScriptPath;
805
-
806
- if (existsSync(scriptPath)) {
807
- miniflareArgs = {
808
- scriptPath,
809
- };
810
- } else {
811
- console.log("No functions. Shimming...");
812
- miniflareArgs = {
813
- // TODO: The fact that these request/response hacks are necessary is ridiculous.
814
- // We need to eliminate them from env.ASSETS.fetch (not sure if just local or prod as well)
815
- script: `
816
- export default {
817
- async fetch(request, env, context) {
818
- const response = await env.ASSETS.fetch(request.url, request)
819
- return new Response(response.body, response)
820
- }
821
- }`,
822
- };
823
- }
824
- }
825
-
826
- // Defer importing miniflare until we really need it
827
- const { Miniflare, Log, LogLevel } = await import("miniflare");
828
- const { Response, fetch } = await import("@miniflare/core");
829
-
830
- // Should only be called if no proxyPort, using `assert.fail()` here
831
- // means the type of `assetsFetch` is still `typeof fetch`
832
- const assetsFetch = proxyPort
833
- ? () => assert.fail()
834
- : await generateAssetsFetch(directory);
835
- const miniflare = new Miniflare({
836
- port,
837
- watch: true,
838
- modules: true,
839
-
840
- log: new Log(LogLevel.ERROR, { prefix: "pages" }),
841
- logUnhandledRejections: true,
842
- sourceMap: true,
843
-
844
- kvNamespaces: kvs.map((kv) => kv.toString()),
845
-
846
- durableObjects: Object.fromEntries(
847
- durableObjects.map((durableObject) =>
848
- durableObject.toString().split("=")
849
- )
850
- ),
851
-
852
- // User bindings
853
- bindings: {
854
- ...Object.fromEntries(
855
- bindings.map((binding) => binding.toString().split("="))
856
- ),
857
- },
858
-
859
- // env.ASSETS.fetch
860
- serviceBindings: {
861
- async ASSETS(request) {
862
- if (proxyPort) {
863
- try {
864
- const url = new URL(request.url);
865
- url.host = `127.0.0.1:${proxyPort}`;
866
- return await fetch(url, request);
867
- } catch (thrown) {
868
- console.error(`Could not proxy request: ${thrown}`);
869
-
870
- // TODO: Pretty error page
871
- return new Response(
872
- `[wrangler] Could not proxy request: ${thrown}`,
873
- { status: 502 }
874
- );
875
- }
876
- } else {
877
- try {
878
- return await assetsFetch(request);
879
- } catch (thrown) {
880
- console.error(`Could not serve static asset: ${thrown}`);
881
-
882
- // TODO: Pretty error page
883
- return new Response(
884
- `[wrangler] Could not serve static asset: ${thrown}`,
885
- { status: 502 }
886
- );
887
- }
888
- }
889
- },
890
- },
891
-
892
- kvPersist: true,
893
- durableObjectsPersist: true,
894
- cachePersist: true,
895
-
896
- ...miniflareArgs,
897
- });
898
-
899
- // Wait for esbuild to finish building the script
900
- while (!scriptReady) {}
901
-
902
- try {
903
- // `startServer` might throw if user code contains errors
904
- const server = await miniflare.startServer();
905
- console.log(`Serving at http://127.0.0.1:${port}/`);
906
-
907
- if (process.env.BROWSER !== "none") {
908
- await open(`http://127.0.0.1:${port}/`);
909
- }
910
-
911
- EXIT_CALLBACKS.push(() => {
912
- server.close();
913
- miniflare.dispose().catch((err) => miniflare.log.error(err));
914
- });
915
- } catch (e) {
916
- miniflare.log.error(e);
917
- EXIT("Could not start Miniflare.", 1);
918
- }
919
- }
920
- );
921
- };