wrangler 2.0.22 → 2.0.25

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 (75) hide show
  1. package/README.md +20 -2
  2. package/bin/wrangler.js +1 -1
  3. package/miniflare-dist/index.mjs +643 -7
  4. package/package.json +17 -5
  5. package/src/__tests__/configuration.test.ts +89 -17
  6. package/src/__tests__/dev.test.tsx +121 -8
  7. package/src/__tests__/generate.test.ts +93 -0
  8. package/src/__tests__/helpers/mock-cfetch.ts +54 -2
  9. package/src/__tests__/index.test.ts +10 -27
  10. package/src/__tests__/jest.setup.ts +31 -1
  11. package/src/__tests__/kv.test.ts +82 -61
  12. package/src/__tests__/metrics.test.ts +5 -0
  13. package/src/__tests__/publish.test.ts +573 -254
  14. package/src/__tests__/r2.test.ts +173 -71
  15. package/src/__tests__/tail.test.ts +93 -39
  16. package/src/__tests__/user.test.ts +1 -0
  17. package/src/__tests__/validate-dev-props.test.ts +56 -0
  18. package/src/__tests__/version.test.ts +35 -0
  19. package/src/__tests__/whoami.test.tsx +60 -1
  20. package/src/api/dev.ts +49 -9
  21. package/src/bundle.ts +298 -37
  22. package/src/cfetch/internal.ts +34 -2
  23. package/src/config/config.ts +15 -3
  24. package/src/config/environment.ts +40 -8
  25. package/src/config/index.ts +13 -0
  26. package/src/config/validation.ts +111 -9
  27. package/src/create-worker-preview.ts +3 -1
  28. package/src/create-worker-upload-form.ts +25 -0
  29. package/src/dev/dev.tsx +145 -31
  30. package/src/dev/local.tsx +116 -24
  31. package/src/dev/remote.tsx +39 -12
  32. package/src/dev/use-esbuild.ts +28 -0
  33. package/src/dev/validate-dev-props.ts +31 -0
  34. package/src/dev-registry.tsx +160 -0
  35. package/src/dev.tsx +148 -67
  36. package/src/generate.ts +112 -14
  37. package/src/index.tsx +252 -7
  38. package/src/inspect.ts +90 -5
  39. package/src/metrics/index.ts +1 -0
  40. package/src/metrics/metrics-dispatcher.ts +1 -0
  41. package/src/metrics/metrics-usage-headers.ts +24 -0
  42. package/src/metrics/send-event.ts +2 -2
  43. package/src/miniflare-cli/assets.ts +546 -0
  44. package/src/miniflare-cli/index.ts +157 -6
  45. package/src/module-collection.ts +3 -3
  46. package/src/pages/build.tsx +36 -28
  47. package/src/pages/constants.ts +4 -0
  48. package/src/pages/deployments.tsx +10 -10
  49. package/src/pages/dev.tsx +155 -651
  50. package/src/pages/functions/buildPlugin.ts +4 -0
  51. package/src/pages/functions/buildWorker.ts +4 -0
  52. package/src/pages/functions/routes-consolidation.test.ts +66 -0
  53. package/src/pages/functions/routes-consolidation.ts +29 -0
  54. package/src/pages/functions/routes-transformation.test.ts +271 -0
  55. package/src/pages/functions/routes-transformation.ts +125 -0
  56. package/src/pages/projects.tsx +9 -3
  57. package/src/pages/publish.tsx +57 -15
  58. package/src/pages/types.ts +9 -0
  59. package/src/pages/upload.tsx +38 -21
  60. package/src/publish.ts +139 -112
  61. package/src/r2.ts +81 -0
  62. package/src/tail/index.ts +15 -2
  63. package/src/tail/printing.ts +41 -3
  64. package/src/user/choose-account.tsx +20 -11
  65. package/src/user/user.tsx +20 -2
  66. package/src/whoami.tsx +79 -1
  67. package/src/worker.ts +12 -0
  68. package/templates/first-party-worker-module-facade.ts +18 -0
  69. package/templates/format-dev-errors.ts +32 -0
  70. package/templates/pages-shim.ts +9 -0
  71. package/templates/{static-asset-facade.js → serve-static-assets.ts} +21 -7
  72. package/templates/service-bindings-module-facade.js +51 -0
  73. package/templates/service-bindings-sw-facade.js +39 -0
  74. package/wrangler-dist/cli.d.ts +38 -3
  75. package/wrangler-dist/cli.js +45244 -25199
@@ -0,0 +1,546 @@
1
+ import { existsSync, lstatSync, readFileSync } from "node:fs";
2
+ import { join } from "node:path";
3
+ import { fetch as miniflareFetch } from "@miniflare/core";
4
+ import { watch } from "chokidar";
5
+ import { getType } from "mime";
6
+ import { Response } from "miniflare";
7
+ import type { Headers as MiniflareHeaders } from "@miniflare/core";
8
+ import type { Log } from "miniflare";
9
+ import type {
10
+ Request as MiniflareRequest,
11
+ RequestInfo,
12
+ RequestInit,
13
+ } from "miniflare";
14
+
15
+ export interface Options {
16
+ log: Log;
17
+ proxyPort?: number;
18
+ directory?: string;
19
+ }
20
+
21
+ export default async function generateASSETSBinding(options: Options) {
22
+ const assetsFetch =
23
+ options.directory !== undefined
24
+ ? await generateAssetsFetch(options.directory, options.log)
25
+ : invalidAssetsFetch;
26
+
27
+ return async function (request: MiniflareRequest) {
28
+ if (options.proxyPort) {
29
+ try {
30
+ const url = new URL(request.url);
31
+ url.host = `localhost:${options.proxyPort}`;
32
+ return await miniflareFetch(url, request);
33
+ } catch (thrown) {
34
+ options.log.error(new Error(`Could not proxy request: ${thrown}`));
35
+
36
+ // TODO: Pretty error page
37
+ return new Response(`[wrangler] Could not proxy request: ${thrown}`, {
38
+ status: 502,
39
+ });
40
+ }
41
+ } else {
42
+ try {
43
+ return await assetsFetch(request);
44
+ } catch (thrown) {
45
+ options.log.error(new Error(`Could not serve static asset: ${thrown}`));
46
+
47
+ // TODO: Pretty error page
48
+ return new Response(
49
+ `[wrangler] Could not serve static asset: ${thrown}`,
50
+ { status: 502 }
51
+ );
52
+ }
53
+ }
54
+ };
55
+ }
56
+
57
+ function escapeRegex(str: string) {
58
+ return str.replace(/[-/\\^$*+?.()|[]{}]/g, "\\$&");
59
+ }
60
+
61
+ type Replacements = Record<string, string>;
62
+
63
+ function replacer(str: string, replacements: Replacements) {
64
+ for (const [replacement, value] of Object.entries(replacements)) {
65
+ str = str.replace(`:${replacement}`, value);
66
+ }
67
+ return str;
68
+ }
69
+
70
+ function generateRulesMatcher<T>(
71
+ rules?: Record<string, T>,
72
+ replacerFn: (match: T, replacements: Replacements) => T = (match) => match
73
+ ) {
74
+ // TODO: How can you test cross-host rules?
75
+ if (!rules) return () => [];
76
+
77
+ const compiledRules = Object.entries(rules)
78
+ .map(([rule, match]) => {
79
+ const crossHost = rule.startsWith("https://");
80
+
81
+ rule = rule.split("*").map(escapeRegex).join("(?<splat>.*)");
82
+
83
+ const host_matches = rule.matchAll(
84
+ /(?<=^https:\\\/\\\/[^/]*?):([^\\]+)(?=\\)/g
85
+ );
86
+ for (const hostMatch of host_matches) {
87
+ rule = rule.split(hostMatch[0]).join(`(?<${hostMatch[1]}>[^/.]+)`);
88
+ }
89
+
90
+ const path_matches = rule.matchAll(/:(\w+)/g);
91
+ for (const pathMatch of path_matches) {
92
+ rule = rule.split(pathMatch[0]).join(`(?<${pathMatch[1]}>[^/]+)`);
93
+ }
94
+
95
+ rule = "^" + rule + "$";
96
+
97
+ try {
98
+ const regExp = new RegExp(rule);
99
+ return [{ crossHost, regExp }, match];
100
+ } catch {}
101
+ })
102
+ .filter((value) => value !== undefined) as [
103
+ { crossHost: boolean; regExp: RegExp },
104
+ T
105
+ ][];
106
+
107
+ return ({ request }: { request: MiniflareRequest }) => {
108
+ const { pathname, host } = new URL(request.url);
109
+
110
+ return compiledRules
111
+ .map(([{ crossHost, regExp }, match]) => {
112
+ const test = crossHost ? `https://${host}${pathname}` : pathname;
113
+ const result = regExp.exec(test);
114
+ if (result) {
115
+ return replacerFn(match, result.groups || {});
116
+ }
117
+ })
118
+ .filter((value) => value !== undefined) as T[];
119
+ };
120
+ }
121
+
122
+ function generateHeadersMatcher(headersFile: string) {
123
+ if (existsSync(headersFile)) {
124
+ const contents = readFileSync(headersFile).toString();
125
+
126
+ // TODO: Log errors
127
+ const lines = contents
128
+ .split("\n")
129
+ .map((line) => line.trim())
130
+ .filter((line) => !line.startsWith("#") && line !== "");
131
+
132
+ const rules: Record<string, Record<string, string>> = {};
133
+ let rule: { path: string; headers: Record<string, string> } | undefined =
134
+ undefined;
135
+
136
+ for (const line of lines) {
137
+ if (/^([^\s]+:\/\/|^\/)/.test(line)) {
138
+ if (rule && Object.keys(rule.headers).length > 0) {
139
+ rules[rule.path] = rule.headers;
140
+ }
141
+
142
+ const path = validateURL(line);
143
+ if (path) {
144
+ rule = {
145
+ path,
146
+ headers: {},
147
+ };
148
+ continue;
149
+ }
150
+ }
151
+
152
+ if (!line.includes(":")) continue;
153
+
154
+ const [rawName, ...rawValue] = line.split(":");
155
+ const name = rawName.trim().toLowerCase();
156
+ const value = rawValue.join(":").trim();
157
+
158
+ if (name === "") continue;
159
+ if (!rule) continue;
160
+
161
+ const existingValues = rule.headers[name];
162
+ rule.headers[name] = existingValues
163
+ ? `${existingValues}, ${value}`
164
+ : value;
165
+ }
166
+
167
+ if (rule && Object.keys(rule.headers).length > 0) {
168
+ rules[rule.path] = rule.headers;
169
+ }
170
+
171
+ const rulesMatcher = generateRulesMatcher(rules, (match, replacements) =>
172
+ Object.fromEntries(
173
+ Object.entries(match).map(([name, value]) => [
174
+ name,
175
+ replacer(value, replacements),
176
+ ])
177
+ )
178
+ );
179
+
180
+ return (request: MiniflareRequest) => {
181
+ const matches = rulesMatcher({
182
+ request,
183
+ });
184
+ if (matches) return matches;
185
+ };
186
+ } else {
187
+ return () => undefined;
188
+ }
189
+ }
190
+
191
+ function generateRedirectsMatcher(redirectsFile: string) {
192
+ if (existsSync(redirectsFile)) {
193
+ const contents = readFileSync(redirectsFile).toString();
194
+
195
+ // TODO: Log errors
196
+ const lines = contents
197
+ .split("\n")
198
+ .map((line) => line.trim())
199
+ .filter((line) => !line.startsWith("#") && line !== "");
200
+
201
+ const rules = Object.fromEntries(
202
+ lines
203
+ .map((line) => line.split(" "))
204
+ .filter((tokens) => tokens.length === 2 || tokens.length === 3)
205
+ .map((tokens) => {
206
+ const from = validateURL(tokens[0], true, false, false);
207
+ const to = validateURL(tokens[1], false, true, true);
208
+ let status: number | undefined = parseInt(tokens[2]) || 302;
209
+ status = [301, 302, 303, 307, 308].includes(status)
210
+ ? status
211
+ : undefined;
212
+
213
+ return from && to && status ? [from, { to, status }] : undefined;
214
+ })
215
+ .filter((rule) => rule !== undefined) as [
216
+ string,
217
+ { to: string; status?: number }
218
+ ][]
219
+ );
220
+
221
+ const rulesMatcher = generateRulesMatcher(
222
+ rules,
223
+ ({ status, to }, replacements) => ({
224
+ status,
225
+ to: replacer(to, replacements),
226
+ })
227
+ );
228
+
229
+ return (request: MiniflareRequest) => {
230
+ const match = rulesMatcher({
231
+ request,
232
+ })[0];
233
+ if (match) return match;
234
+ };
235
+ } else {
236
+ return () => undefined;
237
+ }
238
+ }
239
+
240
+ function extractPathname(
241
+ path = "/",
242
+ includeSearch: boolean,
243
+ includeHash: boolean
244
+ ) {
245
+ if (!path.startsWith("/")) path = `/${path}`;
246
+ const url = new URL(`//${path}`, "relative://");
247
+ return `${url.pathname}${includeSearch ? url.search : ""}${
248
+ includeHash ? url.hash : ""
249
+ }`;
250
+ }
251
+
252
+ function validateURL(
253
+ token: string,
254
+ onlyRelative = false,
255
+ includeSearch = false,
256
+ includeHash = false
257
+ ) {
258
+ const host = /^https:\/\/+(?<host>[^/]+)\/?(?<path>.*)/.exec(token);
259
+ if (host && host.groups && host.groups.host) {
260
+ if (onlyRelative) return;
261
+
262
+ return `https://${host.groups.host}${extractPathname(
263
+ host.groups.path,
264
+ includeSearch,
265
+ includeHash
266
+ )}`;
267
+ } else {
268
+ if (!token.startsWith("/") && onlyRelative) token = `/${token}`;
269
+
270
+ const path = /^\//.exec(token);
271
+ if (path) {
272
+ try {
273
+ return extractPathname(token, includeSearch, includeHash);
274
+ } catch {}
275
+ }
276
+ }
277
+ return "";
278
+ }
279
+
280
+ function hasFileExtension(pathname: string) {
281
+ return /\/.+\.[a-z0-9]+$/i.test(pathname);
282
+ }
283
+
284
+ async function generateAssetsFetch(
285
+ directory: string,
286
+ log: Log
287
+ ): Promise<typeof miniflareFetch> {
288
+ // Defer importing miniflare until we really need it
289
+ const { Headers, Request } = await import("@miniflare/core");
290
+
291
+ const headersFile = join(directory, "_headers");
292
+ const redirectsFile = join(directory, "_redirects");
293
+ const workerFile = join(directory, "_worker.js");
294
+
295
+ const ignoredFiles = [headersFile, redirectsFile, workerFile];
296
+
297
+ const assetExists = (path: string) => {
298
+ path = join(directory, path);
299
+ return (
300
+ existsSync(path) &&
301
+ lstatSync(path).isFile() &&
302
+ !ignoredFiles.includes(path)
303
+ );
304
+ };
305
+
306
+ const getAsset = (path: string) => {
307
+ if (assetExists(path)) {
308
+ return join(directory, path);
309
+ }
310
+ };
311
+
312
+ let redirectsMatcher = generateRedirectsMatcher(redirectsFile);
313
+ let headersMatcher = generateHeadersMatcher(headersFile);
314
+
315
+ watch([headersFile, redirectsFile], {
316
+ persistent: true,
317
+ }).on("change", (path) => {
318
+ switch (path) {
319
+ case headersFile: {
320
+ log.log("_headers modified. Re-evaluating...");
321
+ headersMatcher = generateHeadersMatcher(headersFile);
322
+ break;
323
+ }
324
+ case redirectsFile: {
325
+ log.log("_redirects modified. Re-evaluating...");
326
+ redirectsMatcher = generateRedirectsMatcher(redirectsFile);
327
+ break;
328
+ }
329
+ }
330
+ });
331
+
332
+ const serveAsset = (file: string) => {
333
+ return readFileSync(file);
334
+ };
335
+
336
+ const generateResponse = (request: MiniflareRequest) => {
337
+ const url = new URL(request.url);
338
+ let assetName = url.pathname;
339
+ try {
340
+ //it's possible for someone to send a URL like http://fakehost/abc%2 which would fail to decode
341
+ assetName = decodeURIComponent(url.pathname);
342
+ } catch {}
343
+
344
+ const deconstructedResponse: {
345
+ status: number;
346
+ headers: MiniflareHeaders;
347
+ body?: Buffer;
348
+ } = {
349
+ status: 200,
350
+ headers: new Headers(),
351
+ body: undefined,
352
+ };
353
+
354
+ const match = redirectsMatcher(request);
355
+ if (match) {
356
+ const { status, to } = match;
357
+
358
+ let location = to;
359
+ let search;
360
+
361
+ if (to.startsWith("/")) {
362
+ search = new URL(location, "http://fakehost").search;
363
+ } else {
364
+ search = new URL(location).search;
365
+ }
366
+
367
+ location = `${location}${search ? "" : url.search}`;
368
+
369
+ if (status && [301, 302, 303, 307, 308].includes(status)) {
370
+ deconstructedResponse.status = status;
371
+ } else {
372
+ deconstructedResponse.status = 302;
373
+ }
374
+
375
+ deconstructedResponse.headers.set("Location", location);
376
+ return deconstructedResponse;
377
+ }
378
+
379
+ if (!request.method?.match(/^(get|head)$/i)) {
380
+ deconstructedResponse.status = 405;
381
+ return deconstructedResponse;
382
+ }
383
+
384
+ const notFound = () => {
385
+ let cwd = assetName;
386
+ while (cwd) {
387
+ cwd = cwd.slice(0, cwd.lastIndexOf("/"));
388
+
389
+ if ((asset = getAsset(`${cwd}/404.html`))) {
390
+ deconstructedResponse.status = 404;
391
+ deconstructedResponse.body = serveAsset(asset);
392
+ deconstructedResponse.headers.set(
393
+ "Content-Type",
394
+ getType(asset) || "application/octet-stream"
395
+ );
396
+ return deconstructedResponse;
397
+ }
398
+ }
399
+
400
+ if ((asset = getAsset(`/index.html`))) {
401
+ deconstructedResponse.body = serveAsset(asset);
402
+ deconstructedResponse.headers.set(
403
+ "Content-Type",
404
+ getType(asset) || "application/octet-stream"
405
+ );
406
+ return deconstructedResponse;
407
+ }
408
+
409
+ deconstructedResponse.status = 404;
410
+ return deconstructedResponse;
411
+ };
412
+
413
+ let asset;
414
+
415
+ if (assetName.endsWith("/")) {
416
+ if ((asset = getAsset(`${assetName}/index.html`))) {
417
+ deconstructedResponse.body = serveAsset(asset);
418
+ deconstructedResponse.headers.set(
419
+ "Content-Type",
420
+ getType(asset) || "application/octet-stream"
421
+ );
422
+ return deconstructedResponse;
423
+ } else if ((asset = getAsset(`${assetName.replace(/\/$/, ".html")}`))) {
424
+ deconstructedResponse.status = 301;
425
+ deconstructedResponse.headers.set(
426
+ "Location",
427
+ `${assetName.slice(0, -1)}${url.search}`
428
+ );
429
+ return deconstructedResponse;
430
+ }
431
+ }
432
+
433
+ if (assetName.endsWith("/index")) {
434
+ deconstructedResponse.status = 301;
435
+ deconstructedResponse.headers.set(
436
+ "Location",
437
+ `${assetName.slice(0, -"index".length)}${url.search}`
438
+ );
439
+ return deconstructedResponse;
440
+ }
441
+
442
+ if ((asset = getAsset(assetName))) {
443
+ if (assetName.endsWith(".html")) {
444
+ const extensionlessPath = assetName.slice(0, -".html".length);
445
+ if (getAsset(extensionlessPath) || extensionlessPath === "/") {
446
+ deconstructedResponse.body = serveAsset(asset);
447
+ deconstructedResponse.headers.set(
448
+ "Content-Type",
449
+ getType(asset) || "application/octet-stream"
450
+ );
451
+ return deconstructedResponse;
452
+ } else {
453
+ deconstructedResponse.status = 301;
454
+ deconstructedResponse.headers.set(
455
+ "Location",
456
+ `${extensionlessPath}${url.search}`
457
+ );
458
+ return deconstructedResponse;
459
+ }
460
+ } else {
461
+ deconstructedResponse.body = serveAsset(asset);
462
+ deconstructedResponse.headers.set(
463
+ "Content-Type",
464
+ getType(asset) || "application/octet-stream"
465
+ );
466
+ return deconstructedResponse;
467
+ }
468
+ } else if (hasFileExtension(assetName)) {
469
+ notFound();
470
+ return deconstructedResponse;
471
+ }
472
+
473
+ if ((asset = getAsset(`${assetName}.html`))) {
474
+ deconstructedResponse.body = serveAsset(asset);
475
+ deconstructedResponse.headers.set(
476
+ "Content-Type",
477
+ getType(asset) || "application/octet-stream"
478
+ );
479
+ return deconstructedResponse;
480
+ }
481
+
482
+ if ((asset = getAsset(`${assetName}/index.html`))) {
483
+ deconstructedResponse.status = 301;
484
+ deconstructedResponse.headers.set(
485
+ "Location",
486
+ `${assetName}/${url.search}`
487
+ );
488
+ return deconstructedResponse;
489
+ } else {
490
+ notFound();
491
+ return deconstructedResponse;
492
+ }
493
+ };
494
+
495
+ const attachHeaders = (
496
+ request: MiniflareRequest,
497
+ deconstructedResponse: {
498
+ status: number;
499
+ headers: MiniflareHeaders;
500
+ body?: Buffer;
501
+ }
502
+ ) => {
503
+ const headers = deconstructedResponse.headers;
504
+ const newHeaders = new Headers({});
505
+ const matches = headersMatcher(request) || [];
506
+
507
+ matches.forEach((match) => {
508
+ Object.entries(match).forEach(([name, value]) => {
509
+ newHeaders.append(name, `${value}`);
510
+ });
511
+ });
512
+
513
+ const combinedHeaders = {
514
+ ...Object.fromEntries(headers.entries()),
515
+ ...Object.fromEntries(newHeaders.entries()),
516
+ };
517
+
518
+ deconstructedResponse.headers = new Headers({});
519
+ Object.entries(combinedHeaders).forEach(([name, value]) => {
520
+ if (value) deconstructedResponse.headers.set(name, value);
521
+ });
522
+ };
523
+
524
+ return async (input: RequestInfo, init?: RequestInit) => {
525
+ const request = new Request(input, init);
526
+ const deconstructedResponse = generateResponse(request);
527
+ attachHeaders(request, deconstructedResponse);
528
+
529
+ const headers = new Headers();
530
+
531
+ [...deconstructedResponse.headers.entries()].forEach(([name, value]) => {
532
+ if (value) headers.set(name, value);
533
+ });
534
+
535
+ return new Response(deconstructedResponse.body, {
536
+ headers,
537
+ status: deconstructedResponse.status,
538
+ });
539
+ };
540
+ }
541
+
542
+ const invalidAssetsFetch: typeof miniflareFetch = () => {
543
+ throw new Error(
544
+ "Trying to fetch assets directly when there is no `directory` option specified."
545
+ );
546
+ };