wrangler 2.9.0 → 2.10.0

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 (82) hide show
  1. package/README.md +3 -3
  2. package/miniflare-dist/index.mjs +2 -15
  3. package/package.json +9 -9
  4. package/src/__tests__/configuration.test.ts +70 -0
  5. package/src/__tests__/d1/d1.test.ts +3 -6
  6. package/src/__tests__/d1/execute.test.ts +64 -0
  7. package/src/__tests__/d1/migrate.test.ts +107 -0
  8. package/src/__tests__/deployments.test.ts +40 -16
  9. package/src/__tests__/dev.test.tsx +3 -3
  10. package/src/__tests__/generate.test.ts +1 -1
  11. package/src/__tests__/helpers/end-event-loop.ts +6 -0
  12. package/src/__tests__/helpers/mock-get-pages-upload-token.ts +25 -0
  13. package/src/__tests__/helpers/mock-set-timeout.ts +16 -0
  14. package/src/__tests__/helpers/msw/handlers/deployments.ts +40 -16
  15. package/src/__tests__/helpers/string-dynamic-values-matcher.ts +28 -0
  16. package/src/__tests__/index.test.ts +3 -4
  17. package/src/__tests__/init.test.ts +1 -1
  18. package/src/__tests__/kv.test.ts +8 -8
  19. package/src/__tests__/middleware.test.ts +65 -0
  20. package/src/__tests__/mtls-certificates.test.ts +585 -0
  21. package/src/__tests__/pages/deployment-list.test.ts +78 -0
  22. package/src/__tests__/pages/functions-build.test.ts +402 -0
  23. package/src/__tests__/pages/pages.test.ts +81 -0
  24. package/src/__tests__/pages/project-create.test.ts +63 -0
  25. package/src/__tests__/pages/project-list.test.ts +108 -0
  26. package/src/__tests__/pages/project-upload.test.ts +481 -0
  27. package/src/__tests__/pages/publish.test.ts +2745 -0
  28. package/src/__tests__/publish.test.ts +58 -27
  29. package/src/__tests__/queues.test.ts +2 -2
  30. package/src/__tests__/secret.test.ts +4 -4
  31. package/src/__tests__/tsconfig.tsbuildinfo +1 -1
  32. package/src/__tests__/user.test.ts +1 -1
  33. package/src/__tests__/whoami.test.tsx +1 -1
  34. package/src/__tests__/worker-namespace.test.ts +1 -1
  35. package/src/api/index.ts +8 -0
  36. package/src/api/mtls-certificate.ts +148 -0
  37. package/src/api/pages/create-worker-bundle-contents.ts +75 -0
  38. package/src/api/pages/publish.tsx +52 -8
  39. package/src/bundle.ts +6 -5
  40. package/src/config/config.ts +7 -7
  41. package/src/config/environment.ts +9 -2
  42. package/src/config/index.ts +13 -0
  43. package/src/config/validation.ts +50 -3
  44. package/src/create-worker-upload-form.ts +9 -0
  45. package/src/d1/execute.tsx +124 -91
  46. package/src/d1/migrations/apply.tsx +36 -29
  47. package/src/d1/migrations/create.tsx +10 -8
  48. package/src/d1/migrations/helpers.ts +63 -38
  49. package/src/d1/migrations/list.tsx +31 -20
  50. package/src/d1/migrations/options.ts +6 -1
  51. package/src/d1/types.ts +1 -0
  52. package/src/d1/utils.ts +2 -1
  53. package/src/deployments.ts +62 -39
  54. package/src/dev/dev.tsx +1 -15
  55. package/src/dev/remote.tsx +2 -2
  56. package/src/dev.tsx +9 -6
  57. package/src/generate/index.ts +1 -1
  58. package/src/index.ts +15 -5
  59. package/src/miniflare-cli/assets.ts +1 -1
  60. package/src/miniflare-cli/tsconfig.tsbuildinfo +1 -1
  61. package/src/mtls-certificate/cli.ts +155 -0
  62. package/src/pages/build.ts +103 -23
  63. package/src/pages/buildFunctions.ts +32 -31
  64. package/src/pages/dev.ts +4 -2
  65. package/src/pages/functions/tsconfig.tsbuildinfo +1 -1
  66. package/src/pages/publish.tsx +12 -1
  67. package/src/pages/utils.ts +1 -1
  68. package/src/publish/publish.ts +3 -2
  69. package/src/secret/index.ts +1 -0
  70. package/src/sites.ts +1 -1
  71. package/src/tail/filters.ts +1 -1
  72. package/src/user/user.ts +4 -3
  73. package/src/worker.ts +6 -0
  74. package/templates/format-dev-errors.ts +1 -0
  75. package/templates/new-worker.ts +3 -0
  76. package/templates/serve-static-assets.ts +1 -0
  77. package/templates/service-bindings-module-facade.js +1 -0
  78. package/templates/tsconfig.init.json +1 -1
  79. package/templates/tsconfig.tsbuildinfo +1 -1
  80. package/wrangler-dist/cli.d.ts +82 -2
  81. package/wrangler-dist/cli.js +1726 -1616
  82. package/src/__tests__/pages.test.ts +0 -2905
@@ -0,0 +1,481 @@
1
+ /* eslint-disable no-shadow */
2
+ import { mkdirSync, writeFileSync } from "node:fs";
3
+ import { rest } from "msw";
4
+ import { endEventLoop } from "../helpers/end-event-loop";
5
+ import { mockGetUploadTokenRequest } from "../helpers/mock-get-pages-upload-token";
6
+ import { mockSetTimeout } from "../helpers/mock-set-timeout";
7
+ import { mockAccountId, mockApiToken } from "./../helpers/mock-account-id";
8
+ import { mockConsoleMethods } from "./../helpers/mock-console";
9
+ import { msw } from "./../helpers/msw";
10
+ import { runInTempDir } from "./../helpers/run-in-tmp";
11
+ import { runWrangler } from "./../helpers/run-wrangler";
12
+ import type { UploadPayloadFile } from "./../../pages/types";
13
+ import type { RestRequest } from "msw";
14
+
15
+ describe("project upload", () => {
16
+ const ENV_COPY = process.env;
17
+ const std = mockConsoleMethods();
18
+
19
+ runInTempDir();
20
+ mockAccountId();
21
+ mockApiToken();
22
+ mockSetTimeout();
23
+
24
+ beforeEach(() => {
25
+ process.env.CI = "true";
26
+ process.env.CF_PAGES_UPLOAD_JWT = "<<funfetti-auth-jwt>>";
27
+ });
28
+
29
+ afterEach(async () => {
30
+ process.env = ENV_COPY;
31
+
32
+ // Force a tick to ensure that all promises resolve
33
+ await endEventLoop();
34
+ // Reset MSW after tick to ensure that all requests have been handled
35
+ msw.resetHandlers();
36
+ msw.restoreHandlers();
37
+ });
38
+
39
+ it("should upload a directory of files with a provided JWT", async () => {
40
+ writeFileSync("logo.png", "foobar");
41
+
42
+ msw.use(
43
+ rest.post("*/pages/assets/check-missing", async (req, res, ctx) => {
44
+ const body = (await req.json()) as {
45
+ hashes: string[];
46
+ };
47
+
48
+ expect(req.headers.get("Authorization")).toBe(
49
+ "Bearer <<funfetti-auth-jwt>>"
50
+ );
51
+ expect(body).toMatchObject({
52
+ hashes: ["2082190357cfd3617ccfe04f340c6247"],
53
+ });
54
+
55
+ return res.once(
56
+ ctx.status(200),
57
+ ctx.json({
58
+ success: true,
59
+ errors: [],
60
+ messages: [],
61
+ result: body.hashes,
62
+ })
63
+ );
64
+ }),
65
+ rest.post("*/pages/assets/upload", async (req, res, ctx) => {
66
+ expect(req.headers.get("Authorization")).toBe(
67
+ "Bearer <<funfetti-auth-jwt>>"
68
+ );
69
+
70
+ expect(await req.json()).toMatchObject([
71
+ {
72
+ base64: true,
73
+ key: "2082190357cfd3617ccfe04f340c6247",
74
+ metadata: {
75
+ contentType: "image/png",
76
+ },
77
+ value: "Zm9vYmFy",
78
+ },
79
+ ]);
80
+
81
+ return res(
82
+ ctx.status(200),
83
+ ctx.json({
84
+ success: true,
85
+ errors: [],
86
+ messages: [],
87
+ result: null,
88
+ })
89
+ );
90
+ })
91
+ );
92
+
93
+ await runWrangler("pages project upload .");
94
+
95
+ expect(std.out).toMatchInlineSnapshot(`
96
+ "✨ Success! Uploaded 1 files (TIMINGS)
97
+
98
+ ✨ Upload complete!"
99
+ `);
100
+ });
101
+
102
+ it("should avoid uploading some files", async () => {
103
+ mkdirSync("some_dir/node_modules", { recursive: true });
104
+ mkdirSync("some_dir/functions", { recursive: true });
105
+
106
+ writeFileSync("logo.png", "foobar");
107
+ writeFileSync("some_dir/functions/foo.js", "func");
108
+ writeFileSync("some_dir/_headers", "headersfile");
109
+
110
+ writeFileSync("_headers", "headersfile");
111
+ writeFileSync("_redirects", "redirectsfile");
112
+ writeFileSync("_worker.js", "workerfile");
113
+ writeFileSync("_routes.json", "routesfile");
114
+ mkdirSync(".git");
115
+ writeFileSync(".git/foo", "gitfile");
116
+ writeFileSync("some_dir/node_modules/some_package", "nodefile");
117
+ mkdirSync("functions");
118
+ writeFileSync("functions/foo.js", "func");
119
+
120
+ // Accumulate multiple requests then assert afterwards
121
+ const requests: RestRequest[] = [];
122
+ msw.use(
123
+ rest.post("*/pages/assets/check-missing", async (req, res, ctx) => {
124
+ const body = (await req.json()) as {
125
+ hashes: string[];
126
+ };
127
+
128
+ expect(req.headers.get("Authorization")).toBe(
129
+ "Bearer <<funfetti-auth-jwt>>"
130
+ );
131
+ expect(body).toMatchObject({
132
+ hashes: [
133
+ "2082190357cfd3617ccfe04f340c6247",
134
+ "95dedb64e6d4940fc2e0f11f711cc2f4",
135
+ "09a79777abda8ccc8bdd51dd3ff8e9e9",
136
+ ],
137
+ });
138
+
139
+ return res.once(
140
+ ctx.status(200),
141
+ ctx.json({
142
+ success: true,
143
+ errors: [],
144
+ messages: [],
145
+ result: body.hashes,
146
+ })
147
+ );
148
+ }),
149
+ rest.post("*/pages/assets/upload", (req, res, ctx) => {
150
+ requests.push(req);
151
+
152
+ return res(
153
+ ctx.status(200),
154
+ ctx.json({
155
+ success: true,
156
+ errors: [],
157
+ messages: [],
158
+ result: null,
159
+ })
160
+ );
161
+ })
162
+ );
163
+
164
+ await runWrangler("pages project upload .");
165
+ expect(requests.length).toBe(3);
166
+
167
+ const resolvedRequests = (
168
+ await Promise.all(
169
+ requests.map(async (req) => await req.json<UploadPayloadFile[]>())
170
+ )
171
+ ).flat();
172
+
173
+ const requestMap = resolvedRequests.reduce<{
174
+ [key: string]: UploadPayloadFile;
175
+ }>((requestMap, req) => Object.assign(requestMap, { [req.key]: req }), {});
176
+
177
+ for (const req of requests) {
178
+ expect(req.headers.get("Authorization")).toBe(
179
+ "Bearer <<funfetti-auth-jwt>>"
180
+ );
181
+ }
182
+
183
+ expect(Object.keys(requestMap).length).toBe(3);
184
+
185
+ expect(requestMap["95dedb64e6d4940fc2e0f11f711cc2f4"]).toMatchObject({
186
+ base64: true,
187
+ key: "95dedb64e6d4940fc2e0f11f711cc2f4",
188
+ metadata: {
189
+ contentType: "application/octet-stream",
190
+ },
191
+ value: "aGVhZGVyc2ZpbGU=",
192
+ });
193
+
194
+ expect(requestMap["2082190357cfd3617ccfe04f340c6247"]).toMatchObject({
195
+ base64: true,
196
+ key: "2082190357cfd3617ccfe04f340c6247",
197
+ metadata: {
198
+ contentType: "image/png",
199
+ },
200
+ value: "Zm9vYmFy",
201
+ });
202
+
203
+ expect(requestMap["09a79777abda8ccc8bdd51dd3ff8e9e9"]).toMatchObject({
204
+ base64: true,
205
+ key: "09a79777abda8ccc8bdd51dd3ff8e9e9",
206
+ metadata: {
207
+ contentType: "application/javascript",
208
+ },
209
+ value: "ZnVuYw==",
210
+ });
211
+
212
+ expect(std.out).toMatchInlineSnapshot(`
213
+ "✨ Success! Uploaded 3 files (TIMINGS)
214
+
215
+ ✨ Upload complete!"
216
+ `);
217
+ });
218
+
219
+ it("should retry uploads", async () => {
220
+ writeFileSync("logo.txt", "foobar");
221
+
222
+ // Accumulate multiple requests then assert afterwards
223
+ const requests: RestRequest[] = [];
224
+ msw.use(
225
+ rest.post("*/pages/assets/check-missing", async (req, res, ctx) => {
226
+ const body = (await req.json()) as { hashes: string[] };
227
+
228
+ expect(req.headers.get("Authorization")).toBe(
229
+ "Bearer <<funfetti-auth-jwt>>"
230
+ );
231
+ expect(body).toMatchObject({
232
+ hashes: ["1a98fb08af91aca4a7df1764a2c4ddb0"],
233
+ });
234
+
235
+ return res.once(
236
+ ctx.status(200),
237
+ ctx.json({
238
+ success: true,
239
+ errors: [],
240
+ messages: [],
241
+ result: body.hashes,
242
+ })
243
+ );
244
+ }),
245
+ rest.post("*/pages/assets/upload", async (req, res, ctx) => {
246
+ requests.push(req);
247
+
248
+ if (requests.length < 2) {
249
+ return res(
250
+ ctx.status(200),
251
+ ctx.json({
252
+ success: false,
253
+ errors: [
254
+ {
255
+ code: 800000,
256
+ message: "Something exploded, please retry",
257
+ },
258
+ ],
259
+ messages: [],
260
+ result: null,
261
+ })
262
+ );
263
+ } else {
264
+ return res(
265
+ ctx.status(200),
266
+ ctx.json({
267
+ success: true,
268
+ errors: [],
269
+ messages: [],
270
+ result: null,
271
+ })
272
+ );
273
+ }
274
+ })
275
+ );
276
+
277
+ await runWrangler("pages project upload .");
278
+
279
+ // Assert two identical requests
280
+ expect(requests.length).toBe(2);
281
+ for (const init of requests) {
282
+ expect(init.headers.get("Authorization")).toBe(
283
+ "Bearer <<funfetti-auth-jwt>>"
284
+ );
285
+
286
+ const body = (await init.json()) as UploadPayloadFile[];
287
+ expect(body).toMatchObject([
288
+ {
289
+ key: "1a98fb08af91aca4a7df1764a2c4ddb0",
290
+ value: Buffer.from("foobar").toString("base64"),
291
+ metadata: {
292
+ contentType: "text/plain",
293
+ },
294
+ base64: true,
295
+ },
296
+ ]);
297
+ }
298
+
299
+ expect(std.out).toMatchInlineSnapshot(`
300
+ "✨ Success! Uploaded 1 files (TIMINGS)
301
+
302
+ ✨ Upload complete!"
303
+ `);
304
+ });
305
+
306
+ it("should try to use multiple buckets (up to the max concurrency)", async () => {
307
+ writeFileSync("logo.txt", "foobar");
308
+ writeFileSync("logo.png", "foobar");
309
+ writeFileSync("logo.html", "foobar");
310
+ writeFileSync("logo.js", "foobar");
311
+
312
+ mockGetUploadTokenRequest(
313
+ "<<funfetti-auth-jwt>>",
314
+ "some-account-id",
315
+ "foo"
316
+ );
317
+
318
+ // Accumulate multiple requests then assert afterwards
319
+ const requests: RestRequest[] = [];
320
+ msw.use(
321
+ rest.post("*/pages/assets/check-missing", async (req, res, ctx) => {
322
+ const body = (await req.json()) as { hashes: string[] };
323
+
324
+ expect(req.headers.get("Authorization")).toBe(
325
+ "Bearer <<funfetti-auth-jwt>>"
326
+ );
327
+ expect(body).toMatchObject({
328
+ hashes: expect.arrayContaining([
329
+ "d96fef225537c9f5e44a3cb27fd0b492",
330
+ "2082190357cfd3617ccfe04f340c6247",
331
+ "6be321bef99e758250dac034474ddbb8",
332
+ "1a98fb08af91aca4a7df1764a2c4ddb0",
333
+ ]),
334
+ });
335
+
336
+ return res.once(
337
+ ctx.status(200),
338
+ ctx.json({
339
+ success: true,
340
+ errors: [],
341
+ messages: [],
342
+ result: body.hashes,
343
+ })
344
+ );
345
+ }),
346
+ rest.post("*/pages/assets/upload", async (req, res, ctx) => {
347
+ requests.push(req);
348
+
349
+ expect(req.headers.get("Authorization")).toBe(
350
+ "Bearer <<funfetti-auth-jwt>>"
351
+ );
352
+
353
+ return res(
354
+ ctx.status(200),
355
+ ctx.json({
356
+ success: true,
357
+ errors: [],
358
+ messages: [],
359
+ result: null,
360
+ })
361
+ );
362
+ })
363
+ );
364
+
365
+ await runWrangler("pages project upload .");
366
+
367
+ // We have 3 buckets, so expect 3 uploads
368
+ expect(requests.length).toBe(3);
369
+ const bodies: UploadPayloadFile[][] = [];
370
+ for (const init of requests) {
371
+ bodies.push((await init.json()) as UploadPayloadFile[]);
372
+ }
373
+ // One bucket should end up with 2 files
374
+ expect(bodies.map((b) => b.length).sort()).toEqual([1, 1, 2]);
375
+ // But we don't know the order, so flatten and test without ordering
376
+ expect(bodies.flatMap((b) => b)).toEqual(
377
+ expect.arrayContaining([
378
+ {
379
+ base64: true,
380
+ key: "d96fef225537c9f5e44a3cb27fd0b492",
381
+ metadata: { contentType: "text/html" },
382
+ value: "Zm9vYmFy",
383
+ },
384
+ {
385
+ base64: true,
386
+ key: "1a98fb08af91aca4a7df1764a2c4ddb0",
387
+ metadata: { contentType: "text/plain" },
388
+ value: "Zm9vYmFy",
389
+ },
390
+ {
391
+ base64: true,
392
+ key: "6be321bef99e758250dac034474ddbb8",
393
+ metadata: { contentType: "application/javascript" },
394
+ value: "Zm9vYmFy",
395
+ },
396
+ {
397
+ base64: true,
398
+ key: "2082190357cfd3617ccfe04f340c6247",
399
+ metadata: { contentType: "image/png" },
400
+ value: "Zm9vYmFy",
401
+ },
402
+ ])
403
+ );
404
+
405
+ expect(std.out).toMatchInlineSnapshot(`
406
+ "✨ Success! Uploaded 4 files (TIMINGS)
407
+
408
+ ✨ Upload complete!"
409
+ `);
410
+ });
411
+
412
+ it("should not error when directory names contain periods and houses a extensionless file", async () => {
413
+ mkdirSync(".well-known");
414
+ // Note: same content as previous test, but since it's a different extension,
415
+ // it hashes to a different value
416
+ writeFileSync(".well-known/foobar", "foobar");
417
+
418
+ mockGetUploadTokenRequest(
419
+ "<<funfetti-auth-jwt>>",
420
+ "some-account-id",
421
+ "foo"
422
+ );
423
+
424
+ msw.use(
425
+ rest.post(
426
+ "*/pages/assets/check-missing",
427
+
428
+ async (req, res, ctx) => {
429
+ const body = (await req.json()) as { hashes: string[] };
430
+
431
+ expect(req.headers.get("Authorization")).toBe(
432
+ "Bearer <<funfetti-auth-jwt>>"
433
+ );
434
+ expect(body).toMatchObject({
435
+ hashes: ["7b764dacfd211bebd8077828a7ddefd7"],
436
+ });
437
+
438
+ return res.once(
439
+ ctx.status(200),
440
+ ctx.json({
441
+ success: true,
442
+ errors: [],
443
+ messages: [],
444
+ result: body.hashes,
445
+ })
446
+ );
447
+ }
448
+ ),
449
+ rest.post("*/pages/assets/upload", async (req, res, ctx) => {
450
+ expect(req.headers.get("Authorization")).toBe(
451
+ "Bearer <<funfetti-auth-jwt>>"
452
+ );
453
+ const body = (await req.json()) as UploadPayloadFile[];
454
+ expect(body).toMatchObject([
455
+ {
456
+ key: "7b764dacfd211bebd8077828a7ddefd7",
457
+ value: Buffer.from("foobar").toString("base64"),
458
+ metadata: {
459
+ contentType: "application/octet-stream",
460
+ },
461
+ base64: true,
462
+ },
463
+ ]);
464
+
465
+ return res.once(
466
+ ctx.status(200),
467
+ ctx.json({
468
+ success: true,
469
+ errors: [],
470
+ messages: [],
471
+ result: null,
472
+ })
473
+ );
474
+ })
475
+ );
476
+
477
+ await runWrangler("pages project upload .");
478
+
479
+ expect(std.err).toMatchInlineSnapshot(`""`);
480
+ });
481
+ });