wrangler 2.8.1 → 2.9.1

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 (81) hide show
  1. package/miniflare-dist/index.mjs +1 -14
  2. package/package.json +2 -2
  3. package/src/__tests__/d1/d1.test.ts +11 -56
  4. package/src/__tests__/d1/migrate.test.ts +141 -0
  5. package/src/__tests__/dev.test.tsx +5 -4
  6. package/src/__tests__/generate.test.ts +1 -1
  7. package/src/__tests__/helpers/end-event-loop.ts +6 -0
  8. package/src/__tests__/helpers/mock-get-pages-upload-token.ts +25 -0
  9. package/src/__tests__/helpers/mock-set-timeout.ts +16 -0
  10. package/src/__tests__/index.test.ts +36 -32
  11. package/src/__tests__/init.test.ts +1 -1
  12. package/src/__tests__/kv.test.ts +55 -44
  13. package/src/__tests__/pages/deployment-list.test.ts +78 -0
  14. package/src/__tests__/pages/pages.test.ts +81 -0
  15. package/src/__tests__/pages/project-create.test.ts +63 -0
  16. package/src/__tests__/pages/project-list.test.ts +108 -0
  17. package/src/__tests__/pages/project-upload.test.ts +481 -0
  18. package/src/__tests__/pages/publish.test.ts +2221 -0
  19. package/src/__tests__/parse.test.ts +106 -0
  20. package/src/__tests__/pubsub.test.ts +15 -12
  21. package/src/__tests__/queues.test.ts +35 -28
  22. package/src/__tests__/r2.test.ts +25 -20
  23. package/src/__tests__/tsconfig.tsbuildinfo +1 -1
  24. package/src/__tests__/worker-namespace.test.ts +26 -21
  25. package/src/api/dev.ts +80 -11
  26. package/src/api/pages/publish.tsx +7 -4
  27. package/src/bundle.ts +1 -1
  28. package/src/config/config.ts +7 -0
  29. package/src/config/index.ts +24 -20
  30. package/src/d1/backups.tsx +20 -24
  31. package/src/d1/create.tsx +6 -5
  32. package/src/d1/delete.ts +7 -10
  33. package/src/d1/execute.tsx +83 -85
  34. package/src/d1/index.ts +5 -6
  35. package/src/d1/list.tsx +21 -9
  36. package/src/d1/migrations/apply.tsx +13 -9
  37. package/src/d1/migrations/create.tsx +9 -10
  38. package/src/d1/migrations/list.tsx +19 -9
  39. package/src/d1/migrations/options.ts +2 -2
  40. package/src/d1/options.ts +3 -3
  41. package/src/delete.ts +5 -8
  42. package/src/deprecated/index.ts +7 -8
  43. package/src/dev.tsx +42 -80
  44. package/src/dispatch-namespace.ts +20 -16
  45. package/src/docs/index.ts +7 -8
  46. package/src/generate/index.ts +5 -7
  47. package/src/index.ts +22 -21
  48. package/src/init.ts +5 -7
  49. package/src/kv/index.ts +15 -17
  50. package/src/miniflare-cli/tsconfig.tsbuildinfo +1 -1
  51. package/src/pages/build.ts +6 -4
  52. package/src/pages/deployment-tails.ts +7 -10
  53. package/src/pages/deployments.tsx +6 -4
  54. package/src/pages/dev.ts +15 -17
  55. package/src/pages/functions/tsconfig.tsbuildinfo +1 -1
  56. package/src/pages/functions.ts +8 -4
  57. package/src/pages/index.ts +3 -3
  58. package/src/pages/projects.tsx +7 -12
  59. package/src/pages/publish.tsx +6 -4
  60. package/src/pages/types.ts +5 -0
  61. package/src/pages/upload.tsx +6 -4
  62. package/src/parse.ts +23 -1
  63. package/src/publish/index.ts +19 -15
  64. package/src/publish/publish.ts +2 -2
  65. package/src/pubsub/pubsub-commands.ts +18 -19
  66. package/src/queues/cli/commands/consumer/add.ts +18 -24
  67. package/src/queues/cli/commands/consumer/index.ts +3 -6
  68. package/src/queues/cli/commands/consumer/remove.ts +11 -18
  69. package/src/queues/cli/commands/create.ts +8 -8
  70. package/src/queues/cli/commands/delete.ts +8 -8
  71. package/src/queues/cli/commands/index.ts +3 -4
  72. package/src/queues/cli/commands/list.ts +8 -8
  73. package/src/r2/index.ts +28 -28
  74. package/src/secret/index.ts +9 -14
  75. package/src/tail/index.ts +6 -8
  76. package/src/yargs-types.ts +18 -5
  77. package/templates/checked-fetch.js +9 -1
  78. package/templates/tsconfig.init.json +1 -1
  79. package/templates/tsconfig.tsbuildinfo +1 -1
  80. package/wrangler-dist/cli.js +2180 -1799
  81. package/src/__tests__/pages.test.ts +0 -2898
@@ -1,2898 +0,0 @@
1
- /* eslint-disable no-shadow */
2
- import { Blob } from "node:buffer";
3
- import { mkdirSync, writeFileSync } from "node:fs";
4
- import { chdir } from "node:process";
5
- import { MockedRequest, rest } from "msw";
6
- import { FormData } from "undici";
7
- import { ROUTES_SPEC_VERSION } from "../pages/constants";
8
- import { isRoutesJSONSpec } from "../pages/functions/routes-validation";
9
- import { version } from "./../../package.json";
10
- import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
11
- import { mockConsoleMethods } from "./helpers/mock-console";
12
- import { msw } from "./helpers/msw";
13
- import { FileReaderSync } from "./helpers/msw/read-file-sync";
14
- import { runInTempDir } from "./helpers/run-in-tmp";
15
- import { runWrangler } from "./helpers/run-wrangler";
16
- import type { Deployment, Project, UploadPayloadFile } from "../pages/types";
17
- import type { RestRequest } from "msw";
18
-
19
- function mockGetToken(jwt: string) {
20
- msw.use(
21
- rest.get(
22
- "*/accounts/:accountId/pages/projects/foo/upload-token",
23
- (req, res, ctx) => {
24
- expect(req.params.accountId).toEqual("some-account-id");
25
-
26
- return res(
27
- ctx.status(200),
28
- ctx.json({ success: true, errors: [], messages: [], result: { jwt } })
29
- );
30
- }
31
- )
32
- );
33
- }
34
-
35
- describe("pages", () => {
36
- runInTempDir();
37
- const std = mockConsoleMethods();
38
- function endEventLoop() {
39
- return new Promise((resolve) => setImmediate(resolve));
40
- }
41
-
42
- beforeEach(() => {
43
- // @ts-expect-error we're using a very simple setTimeout mock here
44
- jest.spyOn(global, "setTimeout").mockImplementation((fn, _period) => {
45
- setImmediate(fn);
46
- });
47
- });
48
- afterEach(async () => {
49
- // Force a tick to ensure that all promises resolve
50
- await endEventLoop();
51
- // Reset MSW after tick to ensure that all requests have been handled
52
- msw.resetHandlers();
53
- msw.restoreHandlers();
54
- });
55
-
56
- it("should should display a list of available subcommands, for pages with no subcommand", async () => {
57
- await runWrangler("pages");
58
- await endEventLoop();
59
-
60
- expect(std.out).toMatchInlineSnapshot(`
61
- "wrangler pages
62
-
63
- ⚡️ Configure Cloudflare Pages
64
-
65
- Commands:
66
- wrangler pages dev [directory] [-- command..] 🧑‍💻 Develop your full-stack Pages application locally
67
- wrangler pages project ⚡️ Interact with your Pages projects
68
- wrangler pages deployment 🚀 Interact with the deployments of a project
69
- wrangler pages publish [directory] 🆙 Publish a directory of static assets as a Pages deployment
70
-
71
- Flags:
72
- -c, --config Path to .toml configuration file [string]
73
- -e, --env Environment to use for operations and .env files [string]
74
- -h, --help Show help [boolean]
75
- -v, --version Show version number [boolean]
76
-
77
- 🚧 'wrangler pages <command>' is a beta command. Please report any issues to https://github.com/cloudflare/wrangler2/issues/new/choose"
78
- `);
79
- });
80
-
81
- describe("beta message for subcommands", () => {
82
- it("should display for pages:dev", async () => {
83
- await expect(
84
- runWrangler("pages dev")
85
- ).rejects.toThrowErrorMatchingInlineSnapshot(
86
- `"Must specify a directory of static assets to serve or a command to run or a proxy port."`
87
- );
88
-
89
- expect(std.out).toMatchInlineSnapshot(`
90
- "🚧 'wrangler pages <command>' is a beta command. Please report any issues to https://github.com/cloudflare/wrangler2/issues/new/choose
91
-
92
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choose"
93
- `);
94
- });
95
-
96
- it("should display for pages:functions:build", async () => {
97
- await expect(runWrangler("pages functions build")).rejects.toThrowError();
98
-
99
- expect(std.out).toMatchInlineSnapshot(`
100
- "🚧 'wrangler pages <command>' is a beta command. Please report any issues to https://github.com/cloudflare/wrangler2/issues/new/choose
101
-
102
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choose"
103
- `);
104
- });
105
-
106
- it("should display for pages:functions:optimize-routes", async () => {
107
- await expect(
108
- runWrangler(
109
- 'pages functions optimize-routes --routes-path="/build/_routes.json" --output-routes-path="/build/_optimized-routes.json"'
110
- )
111
- ).rejects.toThrowError();
112
-
113
- expect(std.out).toMatchInlineSnapshot(`
114
- "🚧 'wrangler pages <command>' is a beta command. Please report any issues to https://github.com/cloudflare/wrangler2/issues/new/choose
115
-
116
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choose"
117
- `);
118
- });
119
- });
120
-
121
- describe("project list", () => {
122
- mockAccountId();
123
- mockApiToken();
124
-
125
- function mockListRequest(projects: unknown[]) {
126
- const requests = { count: 0 };
127
- msw.use(
128
- rest.get(
129
- "*/accounts/:accountId/pages/projects",
130
- async (req, res, ctx) => {
131
- requests.count++;
132
- const pageSize = Number(req.url.searchParams.get("per_page"));
133
- const page = Number(req.url.searchParams.get("page"));
134
- const expectedPageSize = 10;
135
- const expectedPage = requests.count;
136
- expect(req.params.accountId).toEqual("some-account-id");
137
- expect(pageSize).toEqual(expectedPageSize);
138
- expect(page).toEqual(expectedPage);
139
- expect(await req.text()).toEqual("");
140
-
141
- return res(
142
- ctx.status(200),
143
- ctx.json({
144
- success: true,
145
- errors: [],
146
- messages: [],
147
- result: projects.slice((page - 1) * pageSize, page * pageSize),
148
- })
149
- );
150
- }
151
- )
152
- );
153
- return requests;
154
- }
155
-
156
- it("should make request to list projects", async () => {
157
- const projects: Project[] = [
158
- {
159
- name: "dogs",
160
- subdomain: "docs.pages.dev",
161
- domains: ["dogs.pages.dev"],
162
- source: {
163
- type: "github",
164
- },
165
- latest_deployment: {
166
- modified_on: "2021-11-17T14:52:26.133835Z",
167
- },
168
- created_on: "2021-11-17T14:52:26.133835Z",
169
- production_branch: "main",
170
- },
171
- {
172
- name: "cats",
173
- subdomain: "cats.pages.dev",
174
- domains: ["cats.pages.dev", "kitten.com"],
175
- latest_deployment: {
176
- modified_on: "2021-11-17T14:52:26.133835Z",
177
- },
178
- created_on: "2021-11-17T14:52:26.133835Z",
179
- production_branch: "main",
180
- },
181
- ];
182
-
183
- const requests = mockListRequest(projects);
184
- await runWrangler("pages project list");
185
-
186
- expect(requests.count).toBe(1);
187
- });
188
-
189
- it("should make multiple requests for paginated results", async () => {
190
- const projects: Project[] = [];
191
- for (let i = 0; i < 15; i++) {
192
- projects.push({
193
- name: "dogs" + i,
194
- subdomain: i + "dogs.pages.dev",
195
- domains: [i + "dogs.pages.dev"],
196
- source: {
197
- type: "github",
198
- },
199
- latest_deployment: {
200
- modified_on: "2021-11-17T14:52:26.133835Z",
201
- },
202
- created_on: "2021-11-17T14:52:26.133835Z",
203
- production_branch: "main",
204
- });
205
- }
206
- const requests = mockListRequest(projects);
207
- await runWrangler("pages project list");
208
- expect(requests.count).toEqual(2);
209
- });
210
- });
211
-
212
- describe("project create", () => {
213
- mockAccountId();
214
- mockApiToken();
215
-
216
- it("should create a project with a production branch", async () => {
217
- msw.use(
218
- rest.post(
219
- "*/accounts/:accountId/pages/projects",
220
- async (req, res, ctx) => {
221
- const body = await req.json();
222
-
223
- expect(req.params.accountId).toEqual("some-account-id");
224
- expect(body).toEqual({
225
- name: "a-new-project",
226
- production_branch: "main",
227
- });
228
-
229
- return res.once(
230
- ctx.status(200),
231
- ctx.json({
232
- success: true,
233
- errors: [],
234
- messages: [],
235
- result: {
236
- name: "a-new-project",
237
- subdomain: "a-new-project.pages.dev",
238
- production_branch: "main",
239
- },
240
- })
241
- );
242
- }
243
- )
244
- );
245
- await runWrangler(
246
- "pages project create a-new-project --production-branch=main"
247
- );
248
- expect(std.out).toMatchInlineSnapshot(`
249
- "✨ Successfully created the 'a-new-project' project. It will be available at https://a-new-project.pages.dev/ once you create your first deployment.
250
- To deploy a folder of assets, run 'wrangler pages publish [directory]'."
251
- `);
252
- });
253
- });
254
-
255
- describe("deployment list", () => {
256
- mockAccountId();
257
- mockApiToken();
258
-
259
- function mockListRequest(deployments: unknown[]) {
260
- const requests = { count: 0 };
261
- msw.use(
262
- rest.get(
263
- "*/accounts/:accountId/pages/projects/:project/deployments",
264
- (req, res, ctx) => {
265
- requests.count++;
266
-
267
- expect(req.params.project).toEqual("images");
268
- expect(req.params.accountId).toEqual("some-account-id");
269
-
270
- return res.once(
271
- ctx.status(200),
272
- ctx.json({
273
- success: true,
274
- errors: [],
275
- messages: [],
276
- result: deployments,
277
- })
278
- );
279
- }
280
- )
281
- );
282
- return requests;
283
- }
284
-
285
- it("should make request to list deployments", async () => {
286
- const deployments: Deployment[] = [
287
- {
288
- id: "87bbc8fe-16be-45cd-81e0-63d722e82cdf",
289
- url: "https://87bbc8fe.images.pages.dev",
290
- environment: "preview",
291
- created_on: "2021-11-17T14:52:26.133835Z",
292
- latest_stage: {
293
- ended_on: "2021-11-17T14:52:26.133835Z",
294
- status: "success",
295
- },
296
- deployment_trigger: {
297
- metadata: {
298
- branch: "main",
299
- commit_hash: "c7649364c4cb32ad4f65b530b9424e8be5bec9d6",
300
- },
301
- },
302
- project_name: "images",
303
- },
304
- ];
305
-
306
- const requests = mockListRequest(deployments);
307
- await runWrangler("pages deployment list --project-name=images");
308
-
309
- expect(requests.count).toBe(1);
310
- });
311
- });
312
-
313
- describe("deployment create", () => {
314
- let actualProcessEnvCI: string | undefined;
315
-
316
- mockAccountId();
317
- mockApiToken();
318
- runInTempDir();
319
-
320
- //TODO Abstract MSW handlers that repeat to this level - JACOB
321
- beforeEach(() => {
322
- actualProcessEnvCI = process.env.CI;
323
- process.env.CI = "true";
324
- });
325
-
326
- afterEach(() => {
327
- process.env.CI = actualProcessEnvCI;
328
- });
329
-
330
- it("should be aliased with 'wrangler pages publish'", async () => {
331
- await runWrangler("pages publish --help");
332
- await endEventLoop();
333
-
334
- expect(std.out).toMatchInlineSnapshot(`
335
- "wrangler pages publish [directory]
336
-
337
- 🆙 Publish a directory of static assets as a Pages deployment
338
-
339
- Positionals:
340
- directory The directory of static files to upload [string]
341
-
342
- Flags:
343
- -e, --env Environment to use for operations and .env files [string]
344
- -h, --help Show help [boolean]
345
- -v, --version Show version number [boolean]
346
-
347
- Options:
348
- --project-name The name of the project you want to deploy to [string]
349
- --branch The name of the branch you want to deploy to [string]
350
- --commit-hash The SHA to attach to this deployment [string]
351
- --commit-message The commit message to attach to this deployment [string]
352
- --commit-dirty Whether or not the workspace should be considered dirty for this deployment [boolean]
353
- --skip-caching Skip asset caching which speeds up builds [boolean]
354
- --no-bundle Whether to run bundling on \`_worker.js\` before deploying [boolean] [default: true]
355
-
356
- 🚧 'wrangler pages <command>' is a beta command. Please report any issues to https://github.com/cloudflare/wrangler2/issues/new/choose"
357
- `);
358
- });
359
-
360
- it("should upload a directory of files", async () => {
361
- writeFileSync("logo.png", "foobar");
362
- mockGetToken("<<funfetti-auth-jwt>>");
363
-
364
- msw.use(
365
- rest.post("*/pages/assets/check-missing", async (req, res, ctx) => {
366
- const body = await req.json();
367
-
368
- expect(req.headers.get("Authorization")).toBe(
369
- "Bearer <<funfetti-auth-jwt>>"
370
- );
371
- expect(body).toMatchObject({
372
- hashes: ["2082190357cfd3617ccfe04f340c6247"],
373
- });
374
-
375
- return res.once(
376
- ctx.status(200),
377
- ctx.json({
378
- success: true,
379
- errors: [],
380
- messages: [],
381
- result: body.hashes,
382
- })
383
- );
384
- }),
385
- rest.post("*/pages/assets/upload", async (req, res, ctx) => {
386
- expect(req.headers.get("Authorization")).toMatchInlineSnapshot(
387
- `"Bearer <<funfetti-auth-jwt>>"`
388
- );
389
- expect(await req.json()).toMatchObject([
390
- {
391
- key: "2082190357cfd3617ccfe04f340c6247",
392
- value: Buffer.from("foobar").toString("base64"),
393
- metadata: {
394
- contentType: "image/png",
395
- },
396
- base64: true,
397
- },
398
- ]);
399
- return res.once(
400
- ctx.status(200),
401
- ctx.json({ success: true, errors: [], messages: [], result: null })
402
- );
403
- }),
404
- rest.post(
405
- "*/accounts/:accountId/pages/projects/foo/deployments",
406
- async (req, res, ctx) => {
407
- expect(req.params.accountId).toEqual("some-account-id");
408
- expect(await (req as RestRequestWithFormData).formData())
409
- .toMatchInlineSnapshot(`
410
- FormData {
411
- Symbol(state): Array [
412
- Object {
413
- "name": "manifest",
414
- "value": "{\\"/logo.png\\":\\"2082190357cfd3617ccfe04f340c6247\\"}",
415
- },
416
- ],
417
- }
418
- `);
419
- return res.once(
420
- ctx.status(200),
421
- ctx.json({
422
- success: true,
423
- errors: [],
424
- messages: [],
425
- result: {
426
- url: "https://abcxyz.foo.pages.dev/",
427
- },
428
- })
429
- );
430
- }
431
- ),
432
- rest.get(
433
- "*/accounts/:accountId/pages/projects/foo",
434
- async (req, res, ctx) => {
435
- expect(req.params.accountId).toEqual("some-account-id");
436
-
437
- return res.once(
438
- ctx.status(200),
439
- ctx.json({
440
- success: true,
441
- errors: [],
442
- messages: [],
443
- result: { deployment_configs: { production: {}, preview: {} } },
444
- })
445
- );
446
- }
447
- )
448
- );
449
-
450
- await runWrangler("pages publish . --project-name=foo");
451
-
452
- expect(std.out).toMatchInlineSnapshot(`
453
- "✨ Success! Uploaded 1 files (TIMINGS)
454
-
455
- ✨ Deployment complete! Take a peek over at https://abcxyz.foo.pages.dev/"
456
- `);
457
- });
458
-
459
- it("should retry uploads", async () => {
460
- writeFileSync("logo.txt", "foobar");
461
-
462
- mockGetToken("<<funfetti-auth-jwt>>");
463
-
464
- // Accumulate multiple requests then assert afterwards
465
- const requests: RestRequest[] = [];
466
- msw.use(
467
- rest.post("*/pages/assets/check-missing", async (req, res, ctx) => {
468
- const body = await req.json();
469
-
470
- expect(req.headers.get("Authorization")).toBe(
471
- "Bearer <<funfetti-auth-jwt>>"
472
- );
473
- expect(body).toMatchObject({
474
- hashes: ["1a98fb08af91aca4a7df1764a2c4ddb0"],
475
- });
476
-
477
- return res.once(
478
- ctx.status(200),
479
- ctx.json({
480
- success: true,
481
- errors: [],
482
- messages: [],
483
- result: body.hashes,
484
- })
485
- );
486
- }),
487
- rest.post("*/pages/assets/upload", async (req, res, ctx) => {
488
- requests.push(req);
489
- expect(req.headers.get("Authorization")).toBe(
490
- "Bearer <<funfetti-auth-jwt>>"
491
- );
492
- expect(await req.json()).toMatchObject([
493
- {
494
- key: "1a98fb08af91aca4a7df1764a2c4ddb0",
495
- value: Buffer.from("foobar").toString("base64"),
496
- metadata: {
497
- contentType: "text/plain",
498
- },
499
- base64: true,
500
- },
501
- ]);
502
-
503
- if (requests.length < 2) {
504
- return res(
505
- ctx.status(200),
506
- ctx.json({
507
- success: false,
508
- errors: [
509
- {
510
- code: 800000,
511
- message: "Something exploded, please retry",
512
- },
513
- ],
514
- messages: [],
515
- result: null,
516
- })
517
- );
518
- } else {
519
- return res(
520
- ctx.status(200),
521
- ctx.json({
522
- success: true,
523
- errors: [],
524
- messages: [],
525
- result: null,
526
- })
527
- );
528
- }
529
- }),
530
- rest.post(
531
- "*/accounts/:accountId/pages/projects/foo/deployments",
532
- async (req, res, ctx) => {
533
- expect(req.params.accountId).toEqual("some-account-id");
534
- expect(await (req as RestRequestWithFormData).formData())
535
- .toMatchInlineSnapshot(`
536
- FormData {
537
- Symbol(state): Array [
538
- Object {
539
- "name": "manifest",
540
- "value": "{\\"/logo.txt\\":\\"1a98fb08af91aca4a7df1764a2c4ddb0\\"}",
541
- },
542
- ],
543
- }
544
- `);
545
-
546
- return res.once(
547
- ctx.status(200),
548
- ctx.json({
549
- success: true,
550
- errors: [],
551
- messages: [],
552
- result: { url: "https://abcxyz.foo.pages.dev/" },
553
- })
554
- );
555
- }
556
- ),
557
- rest.get(
558
- "*/accounts/:accountId/pages/projects/foo",
559
- async (req, res, ctx) => {
560
- expect(req.params.accountId).toEqual("some-account-id");
561
-
562
- return res.once(
563
- ctx.status(200),
564
- ctx.json({
565
- success: true,
566
- errors: [],
567
- messages: [],
568
- result: { deployment_configs: { production: {}, preview: {} } },
569
- })
570
- );
571
- }
572
- )
573
- );
574
-
575
- await runWrangler("pages publish . --project-name=foo");
576
-
577
- expect(std.out).toMatchInlineSnapshot(`
578
- "✨ Success! Uploaded 1 files (TIMINGS)
579
-
580
- ✨ Deployment complete! Take a peek over at https://abcxyz.foo.pages.dev/"
581
- `);
582
- });
583
-
584
- it("should refetch a JWT if it expires while uploading", async () => {
585
- writeFileSync("logo.txt", "foobar");
586
- mockGetToken("<<funfetti-auth-jwt>>");
587
-
588
- const requests: RestRequest[] = [];
589
- msw.use(
590
- rest.post("*/pages/assets/check-missing", async (req, res, ctx) => {
591
- const body = (await req.json()) as { hashes: string[] };
592
-
593
- expect(req.headers.get("Authorization")).toBe(
594
- "Bearer <<funfetti-auth-jwt>>"
595
- );
596
- expect(body).toMatchObject({
597
- hashes: ["1a98fb08af91aca4a7df1764a2c4ddb0"],
598
- });
599
-
600
- return res.once(
601
- ctx.status(200),
602
- ctx.json({
603
- success: true,
604
- errors: [],
605
- messages: [],
606
- result: body.hashes,
607
- })
608
- );
609
- }),
610
- rest.post("*/pages/assets/upload", async (req, res, ctx) => {
611
- requests.push(req);
612
- expect(await req.json()).toMatchObject([
613
- {
614
- key: "1a98fb08af91aca4a7df1764a2c4ddb0",
615
- value: Buffer.from("foobar").toString("base64"),
616
- metadata: {
617
- contentType: "text/plain",
618
- },
619
- base64: true,
620
- },
621
- ]);
622
- // Fail just the first request
623
- if (requests.length < 2) {
624
- mockGetToken("<<funfetti-auth-jwt2>>");
625
- return res(
626
- ctx.status(200),
627
- ctx.json({
628
- success: false,
629
- errors: [
630
- {
631
- code: 8000013,
632
- message: "Authorization failed",
633
- },
634
- ],
635
- messages: [],
636
- result: null,
637
- })
638
- );
639
- } else {
640
- return res(
641
- ctx.status(200),
642
- ctx.json({
643
- success: true,
644
- errors: [],
645
- messages: [],
646
- result: null,
647
- })
648
- );
649
- }
650
- }),
651
- rest.post(
652
- "*/accounts/:accountId/pages/projects/foo/deployments",
653
- async (req, res, ctx) => {
654
- expect(req.params.accountId).toEqual("some-account-id");
655
- expect(await (req as RestRequestWithFormData).formData())
656
- .toMatchInlineSnapshot(`
657
- FormData {
658
- Symbol(state): Array [
659
- Object {
660
- "name": "manifest",
661
- "value": "{\\"/logo.txt\\":\\"1a98fb08af91aca4a7df1764a2c4ddb0\\"}",
662
- },
663
- ],
664
- }
665
- `);
666
-
667
- return res.once(
668
- ctx.status(200),
669
- ctx.json({
670
- success: true,
671
- errors: [],
672
- messages: [],
673
- result: { url: "https://abcxyz.foo.pages.dev/" },
674
- })
675
- );
676
- }
677
- ),
678
- rest.get(
679
- "*/accounts/:accountId/pages/projects/foo",
680
- async (req, res, ctx) => {
681
- expect(req.params.accountId).toEqual("some-account-id");
682
-
683
- return res.once(
684
- ctx.status(200),
685
- ctx.json({
686
- success: true,
687
- errors: [],
688
- messages: [],
689
- result: { deployment_configs: { production: {}, preview: {} } },
690
- })
691
- );
692
- }
693
- )
694
- );
695
-
696
- await runWrangler("pages publish . --project-name=foo");
697
-
698
- expect(requests[0].headers.get("Authorization")).toBe(
699
- "Bearer <<funfetti-auth-jwt>>"
700
- );
701
-
702
- expect(requests[1].headers.get("Authorization")).toBe(
703
- "Bearer <<funfetti-auth-jwt2>>"
704
- );
705
-
706
- expect(std.out).toMatchInlineSnapshot(`
707
- "✨ Success! Uploaded 1 files (TIMINGS)
708
-
709
- ✨ Deployment complete! Take a peek over at https://abcxyz.foo.pages.dev/"
710
- `);
711
- });
712
-
713
- it("should try to use multiple buckets (up to the max concurrency)", async () => {
714
- writeFileSync("logo.txt", "foobar");
715
- writeFileSync("logo.png", "foobar");
716
- writeFileSync("logo.html", "foobar");
717
- writeFileSync("logo.js", "foobar");
718
-
719
- mockGetToken("<<funfetti-auth-jwt>>");
720
-
721
- // Accumulate multiple requests then assert afterwards
722
- const requests: RestRequest[] = [];
723
- const bodies: UploadPayloadFile[][] = [];
724
- msw.use(
725
- rest.post("*/pages/assets/check-missing", async (req, res, ctx) => {
726
- const body = (await req.json()) as {
727
- hashes: string[];
728
- };
729
-
730
- expect(req.headers.get("Authorization")).toBe(
731
- "Bearer <<funfetti-auth-jwt>>"
732
- );
733
- expect(body).toMatchObject({
734
- hashes: expect.arrayContaining([
735
- "d96fef225537c9f5e44a3cb27fd0b492",
736
- "2082190357cfd3617ccfe04f340c6247",
737
- "6be321bef99e758250dac034474ddbb8",
738
- "1a98fb08af91aca4a7df1764a2c4ddb0",
739
- ]),
740
- });
741
-
742
- return res.once(
743
- ctx.status(200),
744
- ctx.json({
745
- success: true,
746
- errors: [],
747
- messages: [],
748
- result: body.hashes,
749
- })
750
- );
751
- }),
752
- rest.post("*/pages/assets/upload", async (req, res, ctx) => {
753
- requests.push(req);
754
-
755
- expect(req.headers.get("Authorization")).toBe(
756
- "Bearer <<funfetti-auth-jwt>>"
757
- );
758
- bodies.push((await req.json()) as UploadPayloadFile[]);
759
-
760
- return res(
761
- ctx.status(200),
762
- ctx.json({
763
- success: true,
764
- errors: [],
765
- messages: [],
766
- result: null,
767
- })
768
- );
769
- }),
770
- rest.post(
771
- "*/accounts/:accountId/pages/projects/foo/deployments",
772
- async (req, res, ctx) => {
773
- expect(req.params.accountId).toEqual("some-account-id");
774
-
775
- const body = await (req as RestRequestWithFormData).formData();
776
- const manifest = JSON.parse(body.get("manifest") as string);
777
-
778
- expect(manifest).toMatchInlineSnapshot(`
779
- Object {
780
- "/logo.html": "d96fef225537c9f5e44a3cb27fd0b492",
781
- "/logo.js": "6be321bef99e758250dac034474ddbb8",
782
- "/logo.png": "2082190357cfd3617ccfe04f340c6247",
783
- "/logo.txt": "1a98fb08af91aca4a7df1764a2c4ddb0",
784
- }
785
- `);
786
-
787
- return res.once(
788
- ctx.status(200),
789
- ctx.json({
790
- success: true,
791
- errors: [],
792
- messages: [],
793
- result: {
794
- url: "https://abcxyz.foo.pages.dev/",
795
- },
796
- })
797
- );
798
- }
799
- ),
800
- rest.get(
801
- "*/accounts/:accountId/pages/projects/foo",
802
- async (req, res, ctx) => {
803
- expect(req.params.accountId).toEqual("some-account-id");
804
-
805
- return res.once(
806
- ctx.status(200),
807
- ctx.json({
808
- success: true,
809
- errors: [],
810
- messages: [],
811
- result: {
812
- deployment_configs: { production: {}, preview: {} },
813
- },
814
- })
815
- );
816
- }
817
- )
818
- );
819
-
820
- await runWrangler("pages publish . --project-name=foo");
821
-
822
- // We have 3 buckets, so expect 3 uploads
823
- expect(requests.length).toBe(3);
824
-
825
- // One bucket should end up with 2 files
826
- expect(bodies.map((b) => b.length).sort()).toEqual([1, 1, 2]);
827
- // But we don't know the order, so flatten and test without ordering
828
- expect(bodies.flatMap((b) => b)).toEqual(
829
- expect.arrayContaining([
830
- {
831
- base64: true,
832
- key: "d96fef225537c9f5e44a3cb27fd0b492",
833
- metadata: { contentType: "text/html" },
834
- value: "Zm9vYmFy",
835
- },
836
- {
837
- base64: true,
838
- key: "1a98fb08af91aca4a7df1764a2c4ddb0",
839
- metadata: { contentType: "text/plain" },
840
- value: "Zm9vYmFy",
841
- },
842
- {
843
- base64: true,
844
- key: "6be321bef99e758250dac034474ddbb8",
845
- metadata: { contentType: "application/javascript" },
846
- value: "Zm9vYmFy",
847
- },
848
- {
849
- base64: true,
850
- key: "2082190357cfd3617ccfe04f340c6247",
851
- metadata: { contentType: "image/png" },
852
- value: "Zm9vYmFy",
853
- },
854
- ])
855
- );
856
-
857
- expect(std.out).toMatchInlineSnapshot(`
858
- "✨ Success! Uploaded 4 files (TIMINGS)
859
-
860
- ✨ Deployment complete! Take a peek over at https://abcxyz.foo.pages.dev/"
861
- `);
862
- });
863
-
864
- it("should resolve child directories correctly", async () => {
865
- mkdirSync("public");
866
- mkdirSync("public/imgs");
867
- writeFileSync("public/logo.txt", "foobar");
868
- writeFileSync("public/imgs/logo.png", "foobar");
869
- writeFileSync("public/logo.html", "foobar");
870
- writeFileSync("public/logo.js", "foobar");
871
-
872
- mockGetToken("<<funfetti-auth-jwt>>");
873
-
874
- // Accumulate multiple requests then assert afterwards
875
- const requests: RestRequest[] = [];
876
- const bodies: UploadPayloadFile[][] = [];
877
- msw.use(
878
- rest.post("*/pages/assets/check-missing", async (req, res, ctx) => {
879
- const body = (await req.json()) as {
880
- hashes: string[];
881
- };
882
-
883
- expect(req.headers.get("Authorization")).toBe(
884
- "Bearer <<funfetti-auth-jwt>>"
885
- );
886
- expect(body).toMatchObject({
887
- hashes: expect.arrayContaining([
888
- "d96fef225537c9f5e44a3cb27fd0b492",
889
- "2082190357cfd3617ccfe04f340c6247",
890
- "6be321bef99e758250dac034474ddbb8",
891
- "1a98fb08af91aca4a7df1764a2c4ddb0",
892
- ]),
893
- });
894
-
895
- return res.once(
896
- ctx.status(200),
897
- ctx.json({
898
- success: true,
899
- errors: [],
900
- messages: [],
901
- result: body.hashes,
902
- })
903
- );
904
- }),
905
- rest.post("*/pages/assets/upload", async (req, res, ctx) => {
906
- requests.push(req);
907
-
908
- expect(req.headers.get("Authorization")).toBe(
909
- "Bearer <<funfetti-auth-jwt>>"
910
- );
911
- bodies.push((await req.json()) as UploadPayloadFile[]);
912
-
913
- return res(
914
- ctx.status(200),
915
- ctx.json({
916
- success: true,
917
- errors: [],
918
- messages: [],
919
- result: null,
920
- })
921
- );
922
- }),
923
- rest.post(
924
- "*/accounts/:accountId/pages/projects/foo/deployments",
925
- async (req, res, ctx) => {
926
- expect(req.params.accountId).toEqual("some-account-id");
927
- const body = await (req as RestRequestWithFormData).formData();
928
- const manifest = JSON.parse(body.get("manifest") as string);
929
- expect(manifest).toMatchInlineSnapshot(`
930
- Object {
931
- "/imgs/logo.png": "2082190357cfd3617ccfe04f340c6247",
932
- "/logo.html": "d96fef225537c9f5e44a3cb27fd0b492",
933
- "/logo.js": "6be321bef99e758250dac034474ddbb8",
934
- "/logo.txt": "1a98fb08af91aca4a7df1764a2c4ddb0",
935
- }
936
- `);
937
-
938
- return res.once(
939
- ctx.status(200),
940
- ctx.json({
941
- success: true,
942
- errors: [],
943
- messages: [],
944
- result: { url: "https://abcxyz.foo.pages.dev/" },
945
- })
946
- );
947
- }
948
- ),
949
- rest.get(
950
- "*/accounts/:accountId/pages/projects/foo",
951
- async (req, res, ctx) => {
952
- expect(req.params.accountId).toEqual("some-account-id");
953
-
954
- return res.once(
955
- ctx.status(200),
956
- ctx.json({
957
- success: true,
958
- errors: [],
959
- messages: [],
960
- result: {
961
- deployment_configs: { production: {}, preview: {} },
962
- },
963
- })
964
- );
965
- }
966
- )
967
- );
968
-
969
- await runWrangler(`pages publish public --project-name=foo`);
970
-
971
- // We have 3 buckets, so expect 3 uploads
972
- expect(requests.length).toBe(3);
973
- // One bucket should end up with 2 files
974
- expect(bodies.map((b) => b.length).sort()).toEqual([1, 1, 2]);
975
- // But we don't know the order, so flatten and test without ordering
976
- expect(bodies.flatMap((b) => b)).toEqual(
977
- expect.arrayContaining([
978
- {
979
- base64: true,
980
- key: "d96fef225537c9f5e44a3cb27fd0b492",
981
- metadata: { contentType: "text/html" },
982
- value: "Zm9vYmFy",
983
- },
984
- {
985
- base64: true,
986
- key: "1a98fb08af91aca4a7df1764a2c4ddb0",
987
- metadata: { contentType: "text/plain" },
988
- value: "Zm9vYmFy",
989
- },
990
- {
991
- base64: true,
992
- key: "6be321bef99e758250dac034474ddbb8",
993
- metadata: { contentType: "application/javascript" },
994
- value: "Zm9vYmFy",
995
- },
996
- {
997
- base64: true,
998
- key: "2082190357cfd3617ccfe04f340c6247",
999
- metadata: { contentType: "image/png" },
1000
- value: "Zm9vYmFy",
1001
- },
1002
- ])
1003
- );
1004
-
1005
- expect(std.out).toMatchInlineSnapshot(`
1006
- "✨ Success! Uploaded 4 files (TIMINGS)
1007
-
1008
- ✨ Deployment complete! Take a peek over at https://abcxyz.foo.pages.dev/"
1009
- `);
1010
- });
1011
-
1012
- it("should resolve the current directory correctly", async () => {
1013
- mkdirSync("public");
1014
- mkdirSync("public/imgs");
1015
- writeFileSync("public/logo.txt", "foobar");
1016
- writeFileSync("public/imgs/logo.png", "foobar");
1017
- writeFileSync("public/logo.html", "foobar");
1018
- writeFileSync("public/logo.js", "foobar");
1019
-
1020
- mockGetToken("<<funfetti-auth-jwt>>");
1021
-
1022
- // Accumulate multiple requests then assert afterwards
1023
- const requests: RestRequest[] = [];
1024
- const bodies: UploadPayloadFile[][] = [];
1025
- msw.use(
1026
- rest.post("*/pages/assets/check-missing", async (req, res, ctx) => {
1027
- const body = (await req.json()) as {
1028
- hashes: string[];
1029
- };
1030
-
1031
- expect(req.headers.get("Authorization")).toBe(
1032
- "Bearer <<funfetti-auth-jwt>>"
1033
- );
1034
- expect(body).toMatchObject({
1035
- hashes: expect.arrayContaining([
1036
- "d96fef225537c9f5e44a3cb27fd0b492",
1037
- "2082190357cfd3617ccfe04f340c6247",
1038
- "6be321bef99e758250dac034474ddbb8",
1039
- "1a98fb08af91aca4a7df1764a2c4ddb0",
1040
- ]),
1041
- });
1042
-
1043
- return res.once(
1044
- ctx.status(200),
1045
- ctx.json({
1046
- success: true,
1047
- errors: [],
1048
- messages: [],
1049
- result: body.hashes,
1050
- })
1051
- );
1052
- }),
1053
- rest.post("*/pages/assets/upload", async (req, res, ctx) => {
1054
- requests.push(req);
1055
-
1056
- expect(req.headers.get("Authorization")).toBe(
1057
- "Bearer <<funfetti-auth-jwt>>"
1058
- );
1059
- bodies.push((await req.json()) as UploadPayloadFile[]);
1060
-
1061
- return res(
1062
- ctx.status(200),
1063
- ctx.json({
1064
- success: true,
1065
- errors: [],
1066
- messages: [],
1067
- result: null,
1068
- })
1069
- );
1070
- }),
1071
- rest.post(
1072
- "*/accounts/:accountId/pages/projects/foo/deployments",
1073
- async (req, res, ctx) => {
1074
- expect(req.params.accountId).toEqual("some-account-id");
1075
-
1076
- const body = await (req as RestRequestWithFormData).formData();
1077
- const manifest = JSON.parse(body.get("manifest") as string);
1078
- expect(manifest).toMatchInlineSnapshot(`
1079
- Object {
1080
- "/imgs/logo.png": "2082190357cfd3617ccfe04f340c6247",
1081
- "/logo.html": "d96fef225537c9f5e44a3cb27fd0b492",
1082
- "/logo.js": "6be321bef99e758250dac034474ddbb8",
1083
- "/logo.txt": "1a98fb08af91aca4a7df1764a2c4ddb0",
1084
- }
1085
- `);
1086
-
1087
- return res.once(
1088
- ctx.status(200),
1089
- ctx.json({
1090
- success: true,
1091
- errors: [],
1092
- messages: [],
1093
- result: { url: "https://abcxyz.foo.pages.dev/" },
1094
- })
1095
- );
1096
- }
1097
- ),
1098
- rest.get(
1099
- "*/accounts/:accountId/pages/projects/foo",
1100
- async (req, res, ctx) => {
1101
- expect(req.params.accountId).toEqual("some-account-id");
1102
-
1103
- return res.once(
1104
- ctx.status(200),
1105
- ctx.json({
1106
- success: true,
1107
- errors: [],
1108
- messages: [],
1109
- result: {
1110
- deployment_configs: { production: {}, preview: {} },
1111
- },
1112
- })
1113
- );
1114
- }
1115
- )
1116
- );
1117
-
1118
- chdir("public");
1119
- await runWrangler(`pages publish . --project-name=foo`);
1120
- // We have 3 buckets, so expect 3 uploads
1121
- expect(requests.length).toBe(3);
1122
- // One bucket should end up with 2 files
1123
- expect(bodies.map((b) => b.length).sort()).toEqual([1, 1, 2]);
1124
- // But we don't know the order, so flatten and test without ordering
1125
- expect(bodies.flatMap((b) => b)).toEqual(
1126
- expect.arrayContaining([
1127
- {
1128
- base64: true,
1129
- key: "d96fef225537c9f5e44a3cb27fd0b492",
1130
- metadata: { contentType: "text/html" },
1131
- value: "Zm9vYmFy",
1132
- },
1133
- {
1134
- base64: true,
1135
- key: "1a98fb08af91aca4a7df1764a2c4ddb0",
1136
- metadata: { contentType: "text/plain" },
1137
- value: "Zm9vYmFy",
1138
- },
1139
- {
1140
- base64: true,
1141
- key: "6be321bef99e758250dac034474ddbb8",
1142
- metadata: { contentType: "application/javascript" },
1143
- value: "Zm9vYmFy",
1144
- },
1145
- {
1146
- base64: true,
1147
- key: "2082190357cfd3617ccfe04f340c6247",
1148
- metadata: { contentType: "image/png" },
1149
- value: "Zm9vYmFy",
1150
- },
1151
- ])
1152
- );
1153
-
1154
- expect(std.out).toMatchInlineSnapshot(`
1155
- "✨ Success! Uploaded 4 files (TIMINGS)
1156
-
1157
- ✨ Deployment complete! Take a peek over at https://abcxyz.foo.pages.dev/"
1158
- `);
1159
- });
1160
-
1161
- it("should not error when directory names contain periods and houses a extensionless file", async () => {
1162
- mkdirSync(".well-known");
1163
- // Note: same content as previous test, but since it's a different extension,
1164
- // it hashes to a different value
1165
- writeFileSync(".well-known/foobar", "foobar");
1166
-
1167
- mockGetToken("<<funfetti-auth-jwt>>");
1168
-
1169
- msw.use(
1170
- rest.post("*/pages/assets/check-missing", async (req, res, ctx) => {
1171
- const body = (await req.json()) as {
1172
- hashes: string[];
1173
- };
1174
-
1175
- expect(req.headers.get("Authorization")).toBe(
1176
- "Bearer <<funfetti-auth-jwt>>"
1177
- );
1178
- expect(body).toMatchObject({
1179
- hashes: ["7b764dacfd211bebd8077828a7ddefd7"],
1180
- });
1181
-
1182
- return res.once(
1183
- ctx.status(200),
1184
- ctx.json({
1185
- success: true,
1186
- errors: [],
1187
- messages: [],
1188
- result: body.hashes,
1189
- })
1190
- );
1191
- }),
1192
-
1193
- rest.post("*/pages/assets/upload", async (req, res, ctx) => {
1194
- expect(req.headers.get("Authorization")).toBe(
1195
- "Bearer <<funfetti-auth-jwt>>"
1196
- );
1197
- const body = (await req.json()) as UploadPayloadFile[];
1198
- expect(body).toMatchObject([
1199
- {
1200
- key: "7b764dacfd211bebd8077828a7ddefd7",
1201
- value: Buffer.from("foobar").toString("base64"),
1202
- metadata: {
1203
- contentType: "application/octet-stream",
1204
- },
1205
- base64: true,
1206
- },
1207
- ]);
1208
- return res.once(
1209
- ctx.status(200),
1210
- ctx.json({
1211
- success: true,
1212
- errors: [],
1213
- messages: [],
1214
- result: null,
1215
- })
1216
- );
1217
- }),
1218
- rest.post(
1219
- "*/accounts/:accountId/pages/projects/foo/deployments",
1220
- async (req, res, ctx) => {
1221
- expect(req.params.accountId).toEqual("some-account-id");
1222
-
1223
- return res.once(
1224
- ctx.status(200),
1225
- ctx.json({
1226
- success: true,
1227
- errors: [],
1228
- messages: [],
1229
- result: { url: "https://abcxyz.foo.pages.dev/" },
1230
- })
1231
- );
1232
- }
1233
- ),
1234
- rest.get(
1235
- "*/accounts/:accountId/pages/projects/foo",
1236
- async (req, res, ctx) => {
1237
- expect(req.params.accountId).toEqual("some-account-id");
1238
-
1239
- return res.once(
1240
- ctx.status(200),
1241
- ctx.json({
1242
- success: true,
1243
- errors: [],
1244
- messages: [],
1245
- result: {
1246
- deployment_configs: { production: {}, preview: {} },
1247
- },
1248
- })
1249
- );
1250
- }
1251
- )
1252
- );
1253
-
1254
- await runWrangler("pages publish . --project-name=foo");
1255
-
1256
- expect(std.err).toMatchInlineSnapshot(`""`);
1257
- });
1258
-
1259
- it("should throw an error if user attempts to use config with pages", async () => {
1260
- await expect(
1261
- runWrangler("pages dev --config foo.toml")
1262
- ).rejects.toThrowErrorMatchingInlineSnapshot(
1263
- `"Pages does not support wrangler.toml"`
1264
- );
1265
- await expect(
1266
- runWrangler("pages publish --config foo.toml")
1267
- ).rejects.toThrowErrorMatchingInlineSnapshot(
1268
- `"Pages does not support wrangler.toml"`
1269
- );
1270
- });
1271
-
1272
- it("should upload a Functions project", async () => {
1273
- // set up the directory of static files to upload.
1274
- mkdirSync("public");
1275
- writeFileSync("public/README.md", "This is a readme");
1276
-
1277
- // set up /functions
1278
- mkdirSync("functions");
1279
- writeFileSync(
1280
- "functions/hello.js",
1281
- `
1282
- export async function onRequest() {
1283
- return new Response("Hello, world!");
1284
- }
1285
- `
1286
- );
1287
-
1288
- mockGetToken("<<funfetti-auth-jwt>>");
1289
-
1290
- msw.use(
1291
- rest.post("*/pages/assets/check-missing", async (req, res, ctx) => {
1292
- const body = (await req.json()) as {
1293
- hashes: string[];
1294
- };
1295
-
1296
- expect(req.headers.get("Authorization")).toBe(
1297
- "Bearer <<funfetti-auth-jwt>>"
1298
- );
1299
- expect(body).toMatchObject({
1300
- hashes: ["13a03eaf24ae98378acd36ea00f77f2f"],
1301
- });
1302
-
1303
- return res.once(
1304
- ctx.status(200),
1305
- ctx.json({
1306
- success: true,
1307
- errors: [],
1308
- messages: [],
1309
- result: body.hashes,
1310
- })
1311
- );
1312
- }),
1313
- rest.post("*/pages/assets/upload", async (req, res, ctx) => {
1314
- expect(req.headers.get("Authorization")).toBe(
1315
- "Bearer <<funfetti-auth-jwt>>"
1316
- );
1317
-
1318
- expect(await req.json()).toMatchObject([
1319
- {
1320
- key: "13a03eaf24ae98378acd36ea00f77f2f",
1321
- value: Buffer.from("This is a readme").toString("base64"),
1322
- metadata: {
1323
- contentType: "text/markdown",
1324
- },
1325
- base64: true,
1326
- },
1327
- ]);
1328
- return res.once(
1329
- ctx.status(200),
1330
- ctx.json({
1331
- success: true,
1332
- errors: [],
1333
- messages: [],
1334
- result: true,
1335
- })
1336
- );
1337
- }),
1338
- rest.post(`*/pages/assets/upsert-hashes`, async (req, res, ctx) => {
1339
- expect(req.headers.get("Authorization")).toBe(
1340
- "Bearer <<funfetti-auth-jwt>>"
1341
- );
1342
-
1343
- expect(await req.json()).toMatchObject({
1344
- hashes: ["13a03eaf24ae98378acd36ea00f77f2f"],
1345
- });
1346
-
1347
- return res.once(
1348
- ctx.status(200),
1349
- ctx.json({
1350
- success: true,
1351
- errors: [],
1352
- messages: [],
1353
- result: true,
1354
- })
1355
- );
1356
- }),
1357
-
1358
- rest.post(
1359
- "*/accounts/:accountId/pages/projects/foo/deployments",
1360
- async (req, res, ctx) => {
1361
- expect(req.params.accountId).toEqual("some-account-id");
1362
- const body = await (req as RestRequestWithFormData).formData();
1363
- const manifest = JSON.parse(body.get("manifest") as string);
1364
-
1365
- // for Functions projects, we auto-generate a `_worker.js`,
1366
- // `functions-filepath-routing-config.json`, and `_routes.json`
1367
- // file, based on the contents of `/functions`
1368
- const generatedWorkerJS = body.get("_worker.js") as string;
1369
- const generatedRoutesJSON = body.get("_routes.json") as string;
1370
- const generatedFilepathRoutingConfig = body.get(
1371
- "functions-filepath-routing-config.json"
1372
- ) as string;
1373
-
1374
- // make sure this is all we uploaded
1375
- expect([...body.keys()]).toEqual([
1376
- "manifest",
1377
- "functions-filepath-routing-config.json",
1378
- "_worker.js",
1379
- "_routes.json",
1380
- ]);
1381
-
1382
- expect(manifest).toMatchInlineSnapshot(`
1383
- Object {
1384
- "/README.md": "13a03eaf24ae98378acd36ea00f77f2f",
1385
- }
1386
- `);
1387
-
1388
- // the contents of the generated `_worker.js` file is pretty massive, so I don't
1389
- // think snapshot testing makes much sense here. Plus, calling
1390
- // `.toMatchInlineSnapshot()` without any arguments, in order to generate that
1391
- // snapshot value, doesn't generate anything in this case (probably because the
1392
- // file contents is too big). So for now, let's test that _worker.js was indeed
1393
- // generated and that the file size is greater than zero
1394
- expect(generatedWorkerJS).not.toBeNull();
1395
- expect(generatedWorkerJS.length).toBeGreaterThan(0);
1396
-
1397
- const maybeRoutesJSONSpec = JSON.parse(generatedRoutesJSON);
1398
- expect(isRoutesJSONSpec(maybeRoutesJSONSpec)).toBe(true);
1399
- expect(maybeRoutesJSONSpec).toMatchObject({
1400
- version: ROUTES_SPEC_VERSION,
1401
- description: `Generated by wrangler@${version}`,
1402
- include: ["/hello"],
1403
- exclude: [],
1404
- });
1405
-
1406
- // Make sure the routing config is valid json
1407
- const parsedFilepathRoutingConfig = JSON.parse(
1408
- generatedFilepathRoutingConfig
1409
- );
1410
- // The actual shape doesn't matter that much since this
1411
- // is only used for display in Dash, but it's still useful for
1412
- // tracking unexpected changes to this config.
1413
- expect(parsedFilepathRoutingConfig).toStrictEqual({
1414
- routes: [
1415
- {
1416
- routePath: "/hello",
1417
- mountPath: "/",
1418
- method: "",
1419
- module: ["hello.js:onRequest"],
1420
- },
1421
- ],
1422
- baseURL: "/",
1423
- });
1424
-
1425
- return res.once(
1426
- ctx.status(200),
1427
- ctx.json({
1428
- success: true,
1429
- errors: [],
1430
- messages: [],
1431
- result: {
1432
- url: "https://abcxyz.foo.pages.dev/",
1433
- },
1434
- })
1435
- );
1436
- }
1437
- ),
1438
- rest.get(
1439
- "*/accounts/:accountId/pages/projects/foo",
1440
- async (req, res, ctx) => {
1441
- expect(req.params.accountId).toEqual("some-account-id");
1442
-
1443
- return res.once(
1444
- ctx.status(200),
1445
- ctx.json({
1446
- success: true,
1447
- errors: [],
1448
- messages: [],
1449
- result: {
1450
- deployment_configs: { production: {}, preview: {} },
1451
- },
1452
- })
1453
- );
1454
- }
1455
- )
1456
- );
1457
-
1458
- await runWrangler("pages publish public --project-name=foo");
1459
-
1460
- expect(std.out).toMatchInlineSnapshot(`
1461
- "✨ Compiled Worker successfully
1462
- ✨ Success! Uploaded 1 files (TIMINGS)
1463
-
1464
- ✨ Uploading Functions
1465
- ✨ Deployment complete! Take a peek over at https://abcxyz.foo.pages.dev/"
1466
- `);
1467
-
1468
- expect(std.err).toMatchInlineSnapshot('""');
1469
- });
1470
-
1471
- it("should upload an Advanced Mode project", async () => {
1472
- // set up the directory of static files to upload.
1473
- mkdirSync("public");
1474
- writeFileSync("public/README.md", "This is a readme");
1475
-
1476
- // set up _worker.js
1477
- writeFileSync(
1478
- "public/_worker.js",
1479
- `
1480
- export default {
1481
- async fetch(request, env) {
1482
- const url = new URL(request.url);
1483
- return url.pathname.startsWith('/api/') ? new Response('Ok') : env.ASSETS.fetch(request);
1484
- }
1485
- };
1486
- `
1487
- );
1488
-
1489
- mockGetToken("<<funfetti-auth-jwt>>");
1490
-
1491
- msw.use(
1492
- rest.post("*/pages/assets/check-missing", async (req, res, ctx) => {
1493
- const body = (await req.json()) as {
1494
- hashes: string[];
1495
- };
1496
-
1497
- expect(req.headers.get("Authorization")).toBe(
1498
- "Bearer <<funfetti-auth-jwt>>"
1499
- );
1500
- expect(body).toMatchObject({
1501
- hashes: ["13a03eaf24ae98378acd36ea00f77f2f"],
1502
- });
1503
-
1504
- return res.once(
1505
- ctx.status(200),
1506
- ctx.json({
1507
- success: true,
1508
- errors: [],
1509
- messages: [],
1510
- result: body.hashes,
1511
- })
1512
- );
1513
- }),
1514
- rest.post("*/pages/assets/upload", async (req, res, ctx) => {
1515
- expect(req.headers.get("Authorization")).toBe(
1516
- "Bearer <<funfetti-auth-jwt>>"
1517
- );
1518
-
1519
- expect(await req.json()).toMatchObject([
1520
- {
1521
- key: "13a03eaf24ae98378acd36ea00f77f2f",
1522
- value: Buffer.from("This is a readme").toString("base64"),
1523
- metadata: {
1524
- contentType: "text/markdown",
1525
- },
1526
- base64: true,
1527
- },
1528
- ]);
1529
- return res.once(
1530
- ctx.status(200),
1531
- ctx.json({
1532
- success: true,
1533
- errors: [],
1534
- messages: [],
1535
- result: true,
1536
- })
1537
- );
1538
- }),
1539
- rest.post(
1540
- "*/accounts/:accountId/pages/projects/foo/deployments",
1541
- async (req, res, ctx) => {
1542
- expect(req.params.accountId).toEqual("some-account-id");
1543
- const body = await (req as RestRequestWithFormData).formData();
1544
- const manifest = JSON.parse(body.get("manifest") as string);
1545
- const customWorkerJS = body.get("_worker.js");
1546
-
1547
- // make sure this is all we uploaded
1548
- expect([...body.keys()].sort()).toEqual(
1549
- ["manifest", "_worker.js"].sort()
1550
- );
1551
-
1552
- expect(manifest).toMatchInlineSnapshot(`
1553
- Object {
1554
- "/README.md": "13a03eaf24ae98378acd36ea00f77f2f",
1555
- }
1556
- `);
1557
-
1558
- expect(customWorkerJS).toMatchInlineSnapshot(`
1559
- "
1560
- export default {
1561
- async fetch(request, env) {
1562
- const url = new URL(request.url);
1563
- return url.pathname.startsWith('/api/') ? new Response('Ok') : env.ASSETS.fetch(request);
1564
- }
1565
- };
1566
- "
1567
- `);
1568
- return res.once(
1569
- ctx.status(200),
1570
- ctx.json({
1571
- success: true,
1572
- errors: [],
1573
- messages: [],
1574
- result: {
1575
- url: "https://abcxyz.foo.pages.dev/",
1576
- },
1577
- })
1578
- );
1579
- }
1580
- ),
1581
- rest.get(
1582
- "*/accounts/:accountId/pages/projects/foo",
1583
- async (req, res, ctx) => {
1584
- expect(req.params.accountId).toEqual("some-account-id");
1585
-
1586
- return res.once(
1587
- ctx.status(200),
1588
- ctx.json({
1589
- success: true,
1590
- errors: [],
1591
- messages: [],
1592
- result: {
1593
- deployment_configs: { production: {}, preview: {} },
1594
- },
1595
- })
1596
- );
1597
- }
1598
- )
1599
- );
1600
-
1601
- await runWrangler("pages publish public --project-name=foo");
1602
-
1603
- expect(std.out).toMatchInlineSnapshot(`
1604
- "✨ Success! Uploaded 1 files (TIMINGS)
1605
-
1606
- ✨ Compiled Worker successfully
1607
- ✨ Uploading _worker.js
1608
- ✨ Deployment complete! Take a peek over at https://abcxyz.foo.pages.dev/"
1609
- `);
1610
-
1611
- expect(std.err).toMatchInlineSnapshot('""');
1612
- });
1613
-
1614
- it("should upload _routes.json for Functions projects, if provided", async () => {
1615
- // set up the directory of static files to upload.
1616
- mkdirSync("public");
1617
- writeFileSync("public/README.md", "This is a readme");
1618
-
1619
- // set up /functions
1620
- mkdirSync("functions");
1621
- writeFileSync(
1622
- "functions/hello.js",
1623
- `
1624
- export async function onRequest() {
1625
- return new Response("Hello, world!");
1626
- }
1627
- `
1628
- );
1629
-
1630
- writeFileSync(
1631
- "functions/goodbye.ts",
1632
- `
1633
- export async function onRequest() {
1634
- return new Response("Bye bye!");
1635
- }
1636
- `
1637
- );
1638
-
1639
- // set up _routes.json
1640
- writeFileSync(
1641
- "public/_routes.json",
1642
- `
1643
- {
1644
- "version": ${ROUTES_SPEC_VERSION},
1645
- "description": "Custom _routes.json file",
1646
- "include": ["/hello"],
1647
- "exclude": []
1648
- }
1649
- `
1650
- );
1651
-
1652
- mockGetToken("<<funfetti-auth-jwt>>");
1653
- msw.use(
1654
- rest.post("*/pages/assets/check-missing", async (req, res, ctx) => {
1655
- const body = (await req.json()) as {
1656
- hashes: string[];
1657
- };
1658
-
1659
- expect(req.headers.get("Authorization")).toBe(
1660
- "Bearer <<funfetti-auth-jwt>>"
1661
- );
1662
- expect(body).toMatchObject({
1663
- hashes: ["13a03eaf24ae98378acd36ea00f77f2f"],
1664
- });
1665
-
1666
- return res.once(
1667
- ctx.status(200),
1668
- ctx.json({
1669
- success: true,
1670
- errors: [],
1671
- messages: [],
1672
- result: body.hashes,
1673
- })
1674
- );
1675
- }),
1676
- rest.post("*/pages/assets/upload", async (req, res, ctx) => {
1677
- expect(req.headers.get("Authorization")).toBe(
1678
- "Bearer <<funfetti-auth-jwt>>"
1679
- );
1680
-
1681
- expect(await req.json()).toMatchObject([
1682
- {
1683
- key: "13a03eaf24ae98378acd36ea00f77f2f",
1684
- value: Buffer.from("This is a readme").toString("base64"),
1685
- metadata: {
1686
- contentType: "text/markdown",
1687
- },
1688
- base64: true,
1689
- },
1690
- ]);
1691
-
1692
- return res.once(
1693
- ctx.status(200),
1694
- ctx.json({
1695
- success: true,
1696
- errors: [],
1697
- messages: [],
1698
- result: null,
1699
- })
1700
- );
1701
- }),
1702
- rest.post(`*/pages/assets/upsert-hashes`, async (req, res, ctx) => {
1703
- expect(req.headers.get("Authorization")).toBe(
1704
- "Bearer <<funfetti-auth-jwt>>"
1705
- );
1706
-
1707
- expect(await req.json()).toMatchObject({
1708
- hashes: ["13a03eaf24ae98378acd36ea00f77f2f"],
1709
- });
1710
-
1711
- return res.once(
1712
- ctx.status(200),
1713
- ctx.json({
1714
- success: true,
1715
- errors: [],
1716
- messages: [],
1717
- result: true,
1718
- })
1719
- );
1720
- }),
1721
- rest.post(
1722
- "*/accounts/:accountId/pages/projects/foo/deployments",
1723
- async (req, res, ctx) => {
1724
- expect(req.params.accountId).toEqual("some-account-id");
1725
- const body = await (req as RestRequestWithFormData).formData();
1726
- const manifest = JSON.parse(body.get("manifest") as string);
1727
- const generatedWorkerJS = body.get("_worker.js") as string;
1728
- const customRoutesJSON = body.get("_routes.json") as string;
1729
- const generatedFilepathRoutingConfig = body.get(
1730
- "functions-filepath-routing-config.json"
1731
- ) as string;
1732
-
1733
- // make sure this is all we uploaded
1734
- expect([...body.keys()].sort()).toEqual(
1735
- [
1736
- "manifest",
1737
- "functions-filepath-routing-config.json",
1738
- "_worker.js",
1739
- "_routes.json",
1740
- ].sort()
1741
- );
1742
-
1743
- expect(manifest).toMatchInlineSnapshot(`
1744
- Object {
1745
- "/README.md": "13a03eaf24ae98378acd36ea00f77f2f",
1746
- }
1747
- `);
1748
-
1749
- // file content of generated `_worker.js` is too massive to snapshot test
1750
- expect(generatedWorkerJS).not.toBeNull();
1751
- expect(generatedWorkerJS.length).toBeGreaterThan(0);
1752
-
1753
- const customRoutes = JSON.parse(customRoutesJSON);
1754
- expect(customRoutes).toMatchObject({
1755
- version: ROUTES_SPEC_VERSION,
1756
- description: "Custom _routes.json file",
1757
- include: ["/hello"],
1758
- exclude: [],
1759
- });
1760
-
1761
- // Make sure the routing config is valid json
1762
- const parsedFilepathRoutingConfig = JSON.parse(
1763
- generatedFilepathRoutingConfig
1764
- );
1765
- // The actual shape doesn't matter that much since this
1766
- // is only used for display in Dash, but it's still useful for
1767
- // tracking unexpected changes to this config.
1768
- expect(parsedFilepathRoutingConfig).toStrictEqual({
1769
- routes: [
1770
- {
1771
- routePath: "/goodbye",
1772
- mountPath: "/",
1773
- method: "",
1774
- module: ["goodbye.ts:onRequest"],
1775
- },
1776
- {
1777
- routePath: "/hello",
1778
- mountPath: "/",
1779
- method: "",
1780
- module: ["hello.js:onRequest"],
1781
- },
1782
- ],
1783
- baseURL: "/",
1784
- });
1785
-
1786
- return res.once(
1787
- ctx.status(200),
1788
- ctx.json({
1789
- success: true,
1790
- errors: [],
1791
- messages: [],
1792
- result: {
1793
- url: "https://abcxyz.foo.pages.dev/",
1794
- },
1795
- })
1796
- );
1797
- }
1798
- ),
1799
- rest.get(
1800
- "*/accounts/:accountId/pages/projects/foo",
1801
- async (req, res, ctx) => {
1802
- expect(req.params.accountId).toEqual("some-account-id");
1803
-
1804
- return res.once(
1805
- ctx.status(200),
1806
- ctx.json({
1807
- success: true,
1808
- errors: [],
1809
- messages: [],
1810
- result: {
1811
- deployment_configs: { production: {}, preview: {} },
1812
- },
1813
- })
1814
- );
1815
- }
1816
- )
1817
- );
1818
-
1819
- await runWrangler("pages publish public --project-name=foo");
1820
-
1821
- expect(std.out).toMatchInlineSnapshot(`
1822
- "✨ Compiled Worker successfully
1823
- ✨ Success! Uploaded 1 files (TIMINGS)
1824
-
1825
- ✨ Uploading Functions
1826
- ✨ Uploading _routes.json
1827
- ✨ Deployment complete! Take a peek over at https://abcxyz.foo.pages.dev/"
1828
- `);
1829
-
1830
- expect(std.warn).toMatchInlineSnapshot(`""`);
1831
- expect(std.err).toMatchInlineSnapshot('""');
1832
- });
1833
-
1834
- it("should not deploy Functions projects that provide an invalid custom _routes.json file", async () => {
1835
- // set up the directory of static files to upload.
1836
- mkdirSync("public");
1837
- writeFileSync("public/README.md", "This is a readme");
1838
-
1839
- // set up _routes.json
1840
- writeFileSync(
1841
- "public/_routes.json",
1842
- `
1843
- {
1844
- "description": "Custom _routes.json file",
1845
- "include": [],
1846
- "exclude": []
1847
- }
1848
- `
1849
- );
1850
-
1851
- // set up /functions
1852
- mkdirSync("functions");
1853
- writeFileSync(
1854
- "functions/hello.js",
1855
- `
1856
- export async function onRequest() {
1857
- return new Response("Hello, world!");
1858
- }
1859
- `
1860
- );
1861
-
1862
- mockGetToken("<<funfetti-auth-jwt>>");
1863
- msw.use(
1864
- rest.post("*/pages/assets/check-missing", async (req, res, ctx) => {
1865
- const body = (await req.json()) as {
1866
- hashes: string[];
1867
- };
1868
-
1869
- expect(req.headers.get("Authorization")).toBe(
1870
- "Bearer <<funfetti-auth-jwt>>"
1871
- );
1872
- expect(body).toMatchObject({
1873
- hashes: ["13a03eaf24ae98378acd36ea00f77f2f"],
1874
- });
1875
-
1876
- return res.once(
1877
- ctx.status(200),
1878
- ctx.json({
1879
- success: true,
1880
- errors: [],
1881
- messages: [],
1882
- result: body.hashes,
1883
- })
1884
- );
1885
- }),
1886
- rest.post("*/pages/assets/upload", async (req, res, ctx) => {
1887
- expect(req.headers.get("Authorization")).toBe(
1888
- "Bearer <<funfetti-auth-jwt>>"
1889
- );
1890
-
1891
- expect(await req.json()).toMatchObject([
1892
- {
1893
- key: "13a03eaf24ae98378acd36ea00f77f2f",
1894
- value: Buffer.from("This is a readme").toString("base64"),
1895
- metadata: {
1896
- contentType: "text/markdown",
1897
- },
1898
- base64: true,
1899
- },
1900
- ]);
1901
-
1902
- return res.once(
1903
- ctx.status(200),
1904
- ctx.json({
1905
- success: true,
1906
- errors: [],
1907
- messages: [],
1908
- result: null,
1909
- })
1910
- );
1911
- }),
1912
- rest.get(
1913
- "*/accounts/:accountId/pages/projects/foo",
1914
- async (req, res, ctx) => {
1915
- expect(req.params.accountId).toEqual("some-account-id");
1916
-
1917
- return res.once(
1918
- ctx.status(200),
1919
- ctx.json({
1920
- success: true,
1921
- errors: [],
1922
- messages: [],
1923
- result: {
1924
- deployment_configs: { production: {}, preview: {} },
1925
- },
1926
- })
1927
- );
1928
- }
1929
- )
1930
- );
1931
-
1932
- await expect(runWrangler("pages publish public --project-name=foo"))
1933
- .rejects
1934
- .toThrow(`Invalid _routes.json file found at: public/_routes.json
1935
- Please make sure the JSON object has the following format:
1936
- {
1937
- version: ${ROUTES_SPEC_VERSION};
1938
- include: string[];
1939
- exclude: string[];
1940
- }
1941
- and that at least one include rule is provided.
1942
- `);
1943
- });
1944
-
1945
- it("should upload _routes.json for Advanced Mode projects, if provided", async () => {
1946
- // set up the directory of static files to upload.
1947
- mkdirSync("public");
1948
- writeFileSync("public/README.md", "This is a readme");
1949
-
1950
- // set up _routes.json
1951
- writeFileSync(
1952
- "public/_routes.json",
1953
- `
1954
- {
1955
- "version": ${ROUTES_SPEC_VERSION},
1956
- "description": "Custom _routes.json file",
1957
- "include": ["/api/*"],
1958
- "exclude": []
1959
- }
1960
- `
1961
- );
1962
-
1963
- // set up _worker.js
1964
- writeFileSync(
1965
- "public/_worker.js",
1966
- `
1967
- export default {
1968
- async fetch(request, env) {
1969
- const url = new URL(request.url);
1970
- return url.pathname.startsWith('/api/') ? new Response('Ok') : env.ASSETS.fetch(request);
1971
- }
1972
- };
1973
- `
1974
- );
1975
-
1976
- mockGetToken("<<funfetti-auth-jwt>>");
1977
-
1978
- msw.use(
1979
- rest.post("*/pages/assets/check-missing", async (req, res, ctx) => {
1980
- const body = (await req.json()) as {
1981
- hashes: string[];
1982
- };
1983
-
1984
- expect(req.headers.get("Authorization")).toBe(
1985
- "Bearer <<funfetti-auth-jwt>>"
1986
- );
1987
- expect(body).toMatchObject({
1988
- hashes: ["13a03eaf24ae98378acd36ea00f77f2f"],
1989
- });
1990
-
1991
- return res.once(
1992
- ctx.status(200),
1993
- ctx.json({
1994
- success: true,
1995
- errors: [],
1996
- messages: [],
1997
- result: body.hashes,
1998
- })
1999
- );
2000
- }),
2001
- rest.post("*/pages/assets/upload", async (req, res, ctx) => {
2002
- expect(req.headers.get("Authorization")).toBe(
2003
- "Bearer <<funfetti-auth-jwt>>"
2004
- );
2005
-
2006
- expect(await req.json()).toMatchObject([
2007
- {
2008
- key: "13a03eaf24ae98378acd36ea00f77f2f",
2009
- value: Buffer.from("This is a readme").toString("base64"),
2010
- metadata: {
2011
- contentType: "text/markdown",
2012
- },
2013
- base64: true,
2014
- },
2015
- ]);
2016
-
2017
- return res.once(
2018
- ctx.status(200),
2019
- ctx.json({
2020
- success: true,
2021
- errors: [],
2022
- messages: [],
2023
- result: null,
2024
- })
2025
- );
2026
- }),
2027
- rest.post(`*/pages/assets/upsert-hashes`, async (req, res, ctx) => {
2028
- expect(req.headers.get("Authorization")).toBe(
2029
- "Bearer <<funfetti-auth-jwt>>"
2030
- );
2031
-
2032
- expect(await req.json()).toMatchObject({
2033
- hashes: ["13a03eaf24ae98378acd36ea00f77f2f"],
2034
- });
2035
-
2036
- return res.once(
2037
- ctx.status(200),
2038
- ctx.json({
2039
- success: true,
2040
- errors: [],
2041
- messages: [],
2042
- result: true,
2043
- })
2044
- );
2045
- }),
2046
- rest.post(
2047
- "*/accounts/:accountId/pages/projects/foo/deployments",
2048
- async (req, res, ctx) => {
2049
- const body = await (req as RestRequestWithFormData).formData();
2050
-
2051
- const manifest = JSON.parse(body.get("manifest") as string);
2052
- const customWorkerJS = body.get("_worker.js") as string;
2053
- const customRoutesJSON = body.get("_routes.json") as string;
2054
-
2055
- // make sure this is all we uploaded
2056
- expect([...body.keys()]).toEqual([
2057
- "manifest",
2058
- "_worker.js",
2059
- "_routes.json",
2060
- ]);
2061
- expect(req.params.accountId).toEqual("some-account-id");
2062
- expect(manifest).toMatchInlineSnapshot(`
2063
- Object {
2064
- "/README.md": "13a03eaf24ae98378acd36ea00f77f2f",
2065
- }
2066
- `);
2067
-
2068
- expect(customWorkerJS).toMatchInlineSnapshot(`
2069
- "
2070
- export default {
2071
- async fetch(request, env) {
2072
- const url = new URL(request.url);
2073
- return url.pathname.startsWith('/api/') ? new Response('Ok') : env.ASSETS.fetch(request);
2074
- }
2075
- };
2076
- "
2077
- `);
2078
-
2079
- expect(JSON.parse(customRoutesJSON)).toMatchObject({
2080
- version: ROUTES_SPEC_VERSION,
2081
- description: "Custom _routes.json file",
2082
- include: ["/api/*"],
2083
- exclude: [],
2084
- });
2085
-
2086
- return res.once(
2087
- ctx.status(200),
2088
- ctx.json({
2089
- success: true,
2090
- errors: [],
2091
- messages: [],
2092
- result: {
2093
- url: "https://abcxyz.foo.pages.dev/",
2094
- },
2095
- })
2096
- );
2097
- }
2098
- ),
2099
- rest.get(
2100
- "*/accounts/:accountId/pages/projects/foo",
2101
- async (req, res, ctx) => {
2102
- expect(req.params.accountId).toEqual("some-account-id");
2103
-
2104
- return res.once(
2105
- ctx.status(200),
2106
- ctx.json({
2107
- success: true,
2108
- errors: [],
2109
- messages: [],
2110
- result: {
2111
- deployment_configs: { production: {}, preview: {} },
2112
- },
2113
- })
2114
- );
2115
- }
2116
- )
2117
- );
2118
-
2119
- await runWrangler("pages publish public --project-name=foo");
2120
-
2121
- expect(std.out).toMatchInlineSnapshot(`
2122
- "✨ Success! Uploaded 1 files (TIMINGS)
2123
-
2124
- ✨ Compiled Worker successfully
2125
- ✨ Uploading _worker.js
2126
- ✨ Uploading _routes.json
2127
- ✨ Deployment complete! Take a peek over at https://abcxyz.foo.pages.dev/"
2128
- `);
2129
-
2130
- expect(std.warn).toMatchInlineSnapshot(`""`);
2131
- expect(std.err).toMatchInlineSnapshot(`""`);
2132
- });
2133
-
2134
- it("should not deploy Advanced Mode projects that provide an invalid _routes.json file", async () => {
2135
- // set up the directory of static files to upload.
2136
- mkdirSync("public");
2137
- writeFileSync("public/README.md", "This is a readme");
2138
-
2139
- // set up _routes.json
2140
- writeFileSync(
2141
- "public/_routes.json",
2142
- `
2143
- {
2144
- "description": "Custom _routes.json file",
2145
- "include": [],
2146
- "exclude": []
2147
- }
2148
- `
2149
- );
2150
-
2151
- // set up _worker.js
2152
- writeFileSync(
2153
- "public/_worker.js",
2154
- `
2155
- export default {
2156
- async fetch(request, env) {
2157
- const url = new URL(request.url);
2158
- return url.pathname.startsWith('/api/') ? new Response('Ok') : env.ASSETS.fetch(request);
2159
- }
2160
- };
2161
- `
2162
- );
2163
-
2164
- mockGetToken("<<funfetti-auth-jwt>>");
2165
-
2166
- msw.use(
2167
- rest.post("*/pages/assets/check-missing", async (req, res, ctx) => {
2168
- const body = (await req.json()) as {
2169
- hashes: string[];
2170
- };
2171
-
2172
- expect(req.headers.get("Authorization")).toBe(
2173
- "Bearer <<funfetti-auth-jwt>>"
2174
- );
2175
- expect(body).toMatchObject({
2176
- hashes: ["13a03eaf24ae98378acd36ea00f77f2f"],
2177
- });
2178
-
2179
- return res.once(
2180
- ctx.status(200),
2181
- ctx.json({
2182
- success: true,
2183
- errors: [],
2184
- messages: [],
2185
- result: body.hashes,
2186
- })
2187
- );
2188
- }),
2189
- rest.post("*/pages/assets/upload", async (req, res, ctx) => {
2190
- expect(req.headers.get("Authorization")).toBe(
2191
- "Bearer <<funfetti-auth-jwt>>"
2192
- );
2193
-
2194
- expect(await req.json()).toMatchObject([
2195
- {
2196
- key: "13a03eaf24ae98378acd36ea00f77f2f",
2197
- value: Buffer.from("This is a readme").toString("base64"),
2198
- metadata: {
2199
- contentType: "text/markdown",
2200
- },
2201
- base64: true,
2202
- },
2203
- ]);
2204
-
2205
- return res.once(
2206
- ctx.status(200),
2207
- ctx.json({
2208
- success: true,
2209
- errors: [],
2210
- messages: [],
2211
- result: null,
2212
- })
2213
- );
2214
- }),
2215
-
2216
- rest.get(
2217
- "*/accounts/:accountId/pages/projects/foo",
2218
- async (req, res, ctx) => {
2219
- expect(req.params.accountId).toEqual("some-account-id");
2220
- return res.once(
2221
- ctx.status(200),
2222
- ctx.json({
2223
- success: true,
2224
- errors: [],
2225
- messages: [],
2226
- result: {
2227
- deployment_configs: { production: {}, preview: {} },
2228
- },
2229
- })
2230
- );
2231
- }
2232
- )
2233
- );
2234
-
2235
- await expect(runWrangler("pages publish public --project-name=foo"))
2236
- .rejects
2237
- .toThrow(`Invalid _routes.json file found at: public/_routes.json
2238
- Please make sure the JSON object has the following format:
2239
- {
2240
- version: ${ROUTES_SPEC_VERSION};
2241
- include: string[];
2242
- exclude: string[];
2243
- }
2244
- and that at least one include rule is provided.
2245
- `);
2246
- });
2247
-
2248
- it("should ignore the entire /functions directory if _worker.js is provided", async () => {
2249
- // set up the directory of static files to upload.
2250
- mkdirSync("public");
2251
- writeFileSync("public/README.md", "This is a readme");
2252
-
2253
- // set up /functions
2254
- mkdirSync("functions");
2255
- writeFileSync(
2256
- "functions/hello.js",
2257
- `
2258
- export async function onRequest() {
2259
- return new Response("Hello, world!");
2260
- }
2261
- `
2262
- );
2263
-
2264
- // set up _worker.js
2265
- writeFileSync(
2266
- "public/_worker.js",
2267
- `
2268
- export default {
2269
- async fetch(request, env) {
2270
- const url = new URL(request.url);
2271
- return url.pathname.startsWith('/api/') ? new Response('Ok') : env.ASSETS.fetch(request);
2272
- }
2273
- };
2274
- `
2275
- );
2276
-
2277
- mockGetToken("<<funfetti-auth-jwt>>");
2278
-
2279
- msw.use(
2280
- rest.post("*/pages/assets/check-missing", async (req, res, ctx) => {
2281
- const body = (await req.json()) as {
2282
- hashes: string[];
2283
- };
2284
-
2285
- expect(req.headers.get("Authorization")).toBe(
2286
- "Bearer <<funfetti-auth-jwt>>"
2287
- );
2288
- expect(body).toMatchObject({
2289
- hashes: ["13a03eaf24ae98378acd36ea00f77f2f"],
2290
- });
2291
-
2292
- return res.once(
2293
- ctx.status(200),
2294
- ctx.json({
2295
- success: true,
2296
- errors: [],
2297
- messages: [],
2298
- result: body.hashes,
2299
- })
2300
- );
2301
- }),
2302
- rest.post("*/pages/assets/upload", async (req, res, ctx) => {
2303
- expect(req.headers.get("Authorization")).toBe(
2304
- "Bearer <<funfetti-auth-jwt>>"
2305
- );
2306
-
2307
- expect(await req.json()).toMatchObject([
2308
- {
2309
- key: "13a03eaf24ae98378acd36ea00f77f2f",
2310
- value: Buffer.from("This is a readme").toString("base64"),
2311
- metadata: {
2312
- contentType: "text/markdown",
2313
- },
2314
- base64: true,
2315
- },
2316
- ]);
2317
-
2318
- return res.once(
2319
- ctx.status(200),
2320
- ctx.json({
2321
- success: true,
2322
- errors: [],
2323
- messages: [],
2324
- result: null,
2325
- })
2326
- );
2327
- }),
2328
-
2329
- rest.post(
2330
- "*/accounts/:accountId/pages/projects/foo/deployments",
2331
- async (req, res, ctx) => {
2332
- const body = await (req as RestRequestWithFormData).formData();
2333
- const manifest = JSON.parse(body.get("manifest") as string);
2334
- const customWorkerJS = body.get("_worker.js");
2335
-
2336
- expect(req.params.accountId).toEqual("some-account-id");
2337
- // make sure this is all we uploaded
2338
- expect([...body.keys()].sort()).toEqual(
2339
- ["manifest", "_worker.js"].sort()
2340
- );
2341
- expect(manifest).toMatchInlineSnapshot(`
2342
- Object {
2343
- "/README.md": "13a03eaf24ae98378acd36ea00f77f2f",
2344
- }
2345
- `);
2346
- expect(customWorkerJS).toMatchInlineSnapshot(`
2347
- "
2348
- export default {
2349
- async fetch(request, env) {
2350
- const url = new URL(request.url);
2351
- return url.pathname.startsWith('/api/') ? new Response('Ok') : env.ASSETS.fetch(request);
2352
- }
2353
- };
2354
- "
2355
- `);
2356
-
2357
- return res.once(
2358
- ctx.status(200),
2359
- ctx.json({
2360
- success: true,
2361
- errors: [],
2362
- messages: [],
2363
- result: {
2364
- url: "https://abcxyz.foo.pages.dev/",
2365
- },
2366
- })
2367
- );
2368
- }
2369
- ),
2370
- rest.get(
2371
- "*/accounts/:accountId/pages/projects/foo",
2372
- async (req, res, ctx) => {
2373
- expect(req.params.accountId).toEqual("some-account-id");
2374
-
2375
- return res.once(
2376
- ctx.status(200),
2377
- ctx.json({
2378
- success: true,
2379
- errors: [],
2380
- messages: [],
2381
- result: {
2382
- deployment_configs: { production: {}, preview: {} },
2383
- },
2384
- })
2385
- );
2386
- }
2387
- )
2388
- );
2389
-
2390
- await runWrangler("pages publish public --project-name=foo");
2391
-
2392
- expect(std.out).toMatchInlineSnapshot(`
2393
- "✨ Success! Uploaded 1 files (TIMINGS)
2394
-
2395
- ✨ Compiled Worker successfully
2396
- ✨ Uploading _worker.js
2397
- ✨ Deployment complete! Take a peek over at https://abcxyz.foo.pages.dev/"
2398
- `);
2399
-
2400
- expect(std.err).toMatchInlineSnapshot('""');
2401
- });
2402
- });
2403
-
2404
- describe("project upload", () => {
2405
- const ENV_COPY = process.env;
2406
-
2407
- mockAccountId();
2408
- mockApiToken();
2409
- runInTempDir();
2410
-
2411
- beforeEach(() => {
2412
- process.env.CI = "true";
2413
- process.env.CF_PAGES_UPLOAD_JWT = "<<funfetti-auth-jwt>>";
2414
- });
2415
-
2416
- afterEach(() => {
2417
- process.env = ENV_COPY;
2418
- });
2419
-
2420
- it("should upload a directory of files with a provided JWT", async () => {
2421
- writeFileSync("logo.png", "foobar");
2422
-
2423
- msw.use(
2424
- rest.post("*/pages/assets/check-missing", async (req, res, ctx) => {
2425
- const body = (await req.json()) as {
2426
- hashes: string[];
2427
- };
2428
-
2429
- expect(req.headers.get("Authorization")).toBe(
2430
- "Bearer <<funfetti-auth-jwt>>"
2431
- );
2432
- expect(body).toMatchObject({
2433
- hashes: ["2082190357cfd3617ccfe04f340c6247"],
2434
- });
2435
-
2436
- return res.once(
2437
- ctx.status(200),
2438
- ctx.json({
2439
- success: true,
2440
- errors: [],
2441
- messages: [],
2442
- result: body.hashes,
2443
- })
2444
- );
2445
- }),
2446
- rest.post("*/pages/assets/upload", async (req, res, ctx) => {
2447
- expect(req.headers.get("Authorization")).toBe(
2448
- "Bearer <<funfetti-auth-jwt>>"
2449
- );
2450
-
2451
- expect(await req.json()).toMatchObject([
2452
- {
2453
- base64: true,
2454
- key: "2082190357cfd3617ccfe04f340c6247",
2455
- metadata: {
2456
- contentType: "image/png",
2457
- },
2458
- value: "Zm9vYmFy",
2459
- },
2460
- ]);
2461
-
2462
- return res(
2463
- ctx.status(200),
2464
- ctx.json({
2465
- success: true,
2466
- errors: [],
2467
- messages: [],
2468
- result: null,
2469
- })
2470
- );
2471
- })
2472
- );
2473
-
2474
- await runWrangler("pages project upload .");
2475
-
2476
- expect(std.out).toMatchInlineSnapshot(`
2477
- "✨ Success! Uploaded 1 files (TIMINGS)
2478
-
2479
- ✨ Upload complete!"
2480
- `);
2481
- });
2482
-
2483
- it("should avoid uploading some files", async () => {
2484
- mkdirSync("some_dir/node_modules", { recursive: true });
2485
- mkdirSync("some_dir/functions", { recursive: true });
2486
-
2487
- writeFileSync("logo.png", "foobar");
2488
- writeFileSync("some_dir/functions/foo.js", "func");
2489
- writeFileSync("some_dir/_headers", "headersfile");
2490
-
2491
- writeFileSync("_headers", "headersfile");
2492
- writeFileSync("_redirects", "redirectsfile");
2493
- writeFileSync("_worker.js", "workerfile");
2494
- writeFileSync("_routes.json", "routesfile");
2495
- mkdirSync(".git");
2496
- writeFileSync(".git/foo", "gitfile");
2497
- writeFileSync("some_dir/node_modules/some_package", "nodefile");
2498
- mkdirSync("functions");
2499
- writeFileSync("functions/foo.js", "func");
2500
-
2501
- // Accumulate multiple requests then assert afterwards
2502
- const requests: RestRequest[] = [];
2503
- msw.use(
2504
- rest.post("*/pages/assets/check-missing", async (req, res, ctx) => {
2505
- const body = (await req.json()) as {
2506
- hashes: string[];
2507
- };
2508
-
2509
- expect(req.headers.get("Authorization")).toBe(
2510
- "Bearer <<funfetti-auth-jwt>>"
2511
- );
2512
- expect(body).toMatchObject({
2513
- hashes: [
2514
- "2082190357cfd3617ccfe04f340c6247",
2515
- "95dedb64e6d4940fc2e0f11f711cc2f4",
2516
- "09a79777abda8ccc8bdd51dd3ff8e9e9",
2517
- ],
2518
- });
2519
-
2520
- return res.once(
2521
- ctx.status(200),
2522
- ctx.json({
2523
- success: true,
2524
- errors: [],
2525
- messages: [],
2526
- result: body.hashes,
2527
- })
2528
- );
2529
- }),
2530
- rest.post("*/pages/assets/upload", (req, res, ctx) => {
2531
- requests.push(req);
2532
-
2533
- return res(
2534
- ctx.status(200),
2535
- ctx.json({
2536
- success: true,
2537
- errors: [],
2538
- messages: [],
2539
- result: null,
2540
- })
2541
- );
2542
- })
2543
- );
2544
-
2545
- await runWrangler("pages project upload .");
2546
- expect(requests.length).toBe(3);
2547
-
2548
- const resolvedRequests = (
2549
- await Promise.all(
2550
- requests.map(async (req) => await req.json<UploadPayloadFile[]>())
2551
- )
2552
- ).flat();
2553
-
2554
- const requestMap = resolvedRequests.reduce<{
2555
- [key: string]: UploadPayloadFile;
2556
- }>(
2557
- (requestMap, req) => Object.assign(requestMap, { [req.key]: req }),
2558
- {}
2559
- );
2560
-
2561
- for (const req of requests) {
2562
- expect(req.headers.get("Authorization")).toBe(
2563
- "Bearer <<funfetti-auth-jwt>>"
2564
- );
2565
- }
2566
-
2567
- expect(Object.keys(requestMap).length).toBe(3);
2568
-
2569
- expect(requestMap["95dedb64e6d4940fc2e0f11f711cc2f4"]).toMatchObject({
2570
- base64: true,
2571
- key: "95dedb64e6d4940fc2e0f11f711cc2f4",
2572
- metadata: {
2573
- contentType: "application/octet-stream",
2574
- },
2575
- value: "aGVhZGVyc2ZpbGU=",
2576
- });
2577
-
2578
- expect(requestMap["2082190357cfd3617ccfe04f340c6247"]).toMatchObject({
2579
- base64: true,
2580
- key: "2082190357cfd3617ccfe04f340c6247",
2581
- metadata: {
2582
- contentType: "image/png",
2583
- },
2584
- value: "Zm9vYmFy",
2585
- });
2586
-
2587
- expect(requestMap["09a79777abda8ccc8bdd51dd3ff8e9e9"]).toMatchObject({
2588
- base64: true,
2589
- key: "09a79777abda8ccc8bdd51dd3ff8e9e9",
2590
- metadata: {
2591
- contentType: "application/javascript",
2592
- },
2593
- value: "ZnVuYw==",
2594
- });
2595
-
2596
- expect(std.out).toMatchInlineSnapshot(`
2597
- "✨ Success! Uploaded 3 files (TIMINGS)
2598
-
2599
- ✨ Upload complete!"
2600
- `);
2601
- });
2602
-
2603
- it("should retry uploads", async () => {
2604
- writeFileSync("logo.txt", "foobar");
2605
-
2606
- // Accumulate multiple requests then assert afterwards
2607
- const requests: RestRequest[] = [];
2608
- msw.use(
2609
- rest.post("*/pages/assets/check-missing", async (req, res, ctx) => {
2610
- const body = (await req.json()) as { hashes: string[] };
2611
-
2612
- expect(req.headers.get("Authorization")).toBe(
2613
- "Bearer <<funfetti-auth-jwt>>"
2614
- );
2615
- expect(body).toMatchObject({
2616
- hashes: ["1a98fb08af91aca4a7df1764a2c4ddb0"],
2617
- });
2618
-
2619
- return res.once(
2620
- ctx.status(200),
2621
- ctx.json({
2622
- success: true,
2623
- errors: [],
2624
- messages: [],
2625
- result: body.hashes,
2626
- })
2627
- );
2628
- }),
2629
- rest.post("*/pages/assets/upload", async (req, res, ctx) => {
2630
- requests.push(req);
2631
-
2632
- if (requests.length < 2) {
2633
- return res(
2634
- ctx.status(200),
2635
- ctx.json({
2636
- success: false,
2637
- errors: [
2638
- {
2639
- code: 800000,
2640
- message: "Something exploded, please retry",
2641
- },
2642
- ],
2643
- messages: [],
2644
- result: null,
2645
- })
2646
- );
2647
- } else {
2648
- return res(
2649
- ctx.status(200),
2650
- ctx.json({
2651
- success: true,
2652
- errors: [],
2653
- messages: [],
2654
- result: null,
2655
- })
2656
- );
2657
- }
2658
- })
2659
- );
2660
-
2661
- await runWrangler("pages project upload .");
2662
-
2663
- // Assert two identical requests
2664
- expect(requests.length).toBe(2);
2665
- for (const init of requests) {
2666
- expect(init.headers.get("Authorization")).toBe(
2667
- "Bearer <<funfetti-auth-jwt>>"
2668
- );
2669
-
2670
- const body = (await init.json()) as UploadPayloadFile[];
2671
- expect(body).toMatchObject([
2672
- {
2673
- key: "1a98fb08af91aca4a7df1764a2c4ddb0",
2674
- value: Buffer.from("foobar").toString("base64"),
2675
- metadata: {
2676
- contentType: "text/plain",
2677
- },
2678
- base64: true,
2679
- },
2680
- ]);
2681
- }
2682
-
2683
- expect(std.out).toMatchInlineSnapshot(`
2684
- "✨ Success! Uploaded 1 files (TIMINGS)
2685
-
2686
- ✨ Upload complete!"
2687
- `);
2688
- });
2689
-
2690
- it("should try to use multiple buckets (up to the max concurrency)", async () => {
2691
- writeFileSync("logo.txt", "foobar");
2692
- writeFileSync("logo.png", "foobar");
2693
- writeFileSync("logo.html", "foobar");
2694
- writeFileSync("logo.js", "foobar");
2695
-
2696
- mockGetToken("<<funfetti-auth-jwt>>");
2697
-
2698
- // Accumulate multiple requests then assert afterwards
2699
- const requests: RestRequest[] = [];
2700
- msw.use(
2701
- rest.post("*/pages/assets/check-missing", async (req, res, ctx) => {
2702
- const body = (await req.json()) as { hashes: string[] };
2703
-
2704
- expect(req.headers.get("Authorization")).toBe(
2705
- "Bearer <<funfetti-auth-jwt>>"
2706
- );
2707
- expect(body).toMatchObject({
2708
- hashes: expect.arrayContaining([
2709
- "d96fef225537c9f5e44a3cb27fd0b492",
2710
- "2082190357cfd3617ccfe04f340c6247",
2711
- "6be321bef99e758250dac034474ddbb8",
2712
- "1a98fb08af91aca4a7df1764a2c4ddb0",
2713
- ]),
2714
- });
2715
-
2716
- return res.once(
2717
- ctx.status(200),
2718
- ctx.json({
2719
- success: true,
2720
- errors: [],
2721
- messages: [],
2722
- result: body.hashes,
2723
- })
2724
- );
2725
- }),
2726
- rest.post("*/pages/assets/upload", async (req, res, ctx) => {
2727
- requests.push(req);
2728
-
2729
- expect(req.headers.get("Authorization")).toBe(
2730
- "Bearer <<funfetti-auth-jwt>>"
2731
- );
2732
-
2733
- return res(
2734
- ctx.status(200),
2735
- ctx.json({
2736
- success: true,
2737
- errors: [],
2738
- messages: [],
2739
- result: null,
2740
- })
2741
- );
2742
- })
2743
- );
2744
-
2745
- await runWrangler("pages project upload .");
2746
-
2747
- // We have 3 buckets, so expect 3 uploads
2748
- expect(requests.length).toBe(3);
2749
- const bodies: UploadPayloadFile[][] = [];
2750
- for (const init of requests) {
2751
- bodies.push((await init.json()) as UploadPayloadFile[]);
2752
- }
2753
- // One bucket should end up with 2 files
2754
- expect(bodies.map((b) => b.length).sort()).toEqual([1, 1, 2]);
2755
- // But we don't know the order, so flatten and test without ordering
2756
- expect(bodies.flatMap((b) => b)).toEqual(
2757
- expect.arrayContaining([
2758
- {
2759
- base64: true,
2760
- key: "d96fef225537c9f5e44a3cb27fd0b492",
2761
- metadata: { contentType: "text/html" },
2762
- value: "Zm9vYmFy",
2763
- },
2764
- {
2765
- base64: true,
2766
- key: "1a98fb08af91aca4a7df1764a2c4ddb0",
2767
- metadata: { contentType: "text/plain" },
2768
- value: "Zm9vYmFy",
2769
- },
2770
- {
2771
- base64: true,
2772
- key: "6be321bef99e758250dac034474ddbb8",
2773
- metadata: { contentType: "application/javascript" },
2774
- value: "Zm9vYmFy",
2775
- },
2776
- {
2777
- base64: true,
2778
- key: "2082190357cfd3617ccfe04f340c6247",
2779
- metadata: { contentType: "image/png" },
2780
- value: "Zm9vYmFy",
2781
- },
2782
- ])
2783
- );
2784
-
2785
- expect(std.out).toMatchInlineSnapshot(`
2786
- "✨ Success! Uploaded 4 files (TIMINGS)
2787
-
2788
- ✨ Upload complete!"
2789
- `);
2790
- });
2791
-
2792
- it("should not error when directory names contain periods and houses a extensionless file", async () => {
2793
- mkdirSync(".well-known");
2794
- // Note: same content as previous test, but since it's a different extension,
2795
- // it hashes to a different value
2796
- writeFileSync(".well-known/foobar", "foobar");
2797
-
2798
- mockGetToken("<<funfetti-auth-jwt>>");
2799
-
2800
- msw.use(
2801
- rest.post(
2802
- "*/pages/assets/check-missing",
2803
-
2804
- async (req, res, ctx) => {
2805
- const body = (await req.json()) as { hashes: string[] };
2806
-
2807
- expect(req.headers.get("Authorization")).toBe(
2808
- "Bearer <<funfetti-auth-jwt>>"
2809
- );
2810
- expect(body).toMatchObject({
2811
- hashes: ["7b764dacfd211bebd8077828a7ddefd7"],
2812
- });
2813
-
2814
- return res.once(
2815
- ctx.status(200),
2816
- ctx.json({
2817
- success: true,
2818
- errors: [],
2819
- messages: [],
2820
- result: body.hashes,
2821
- })
2822
- );
2823
- }
2824
- ),
2825
- rest.post("*/pages/assets/upload", async (req, res, ctx) => {
2826
- expect(req.headers.get("Authorization")).toBe(
2827
- "Bearer <<funfetti-auth-jwt>>"
2828
- );
2829
- const body = (await req.json()) as UploadPayloadFile[];
2830
- expect(body).toMatchObject([
2831
- {
2832
- key: "7b764dacfd211bebd8077828a7ddefd7",
2833
- value: Buffer.from("foobar").toString("base64"),
2834
- metadata: {
2835
- contentType: "application/octet-stream",
2836
- },
2837
- base64: true,
2838
- },
2839
- ]);
2840
-
2841
- return res.once(
2842
- ctx.status(200),
2843
- ctx.json({
2844
- success: true,
2845
- errors: [],
2846
- messages: [],
2847
- result: null,
2848
- })
2849
- );
2850
- })
2851
- );
2852
-
2853
- await runWrangler("pages project upload .");
2854
-
2855
- expect(std.err).toMatchInlineSnapshot(`""`);
2856
- });
2857
- });
2858
- });
2859
-
2860
- function mockFormDataToString(this: FormData) {
2861
- const entries = [];
2862
- for (const [key, value] of this.entries()) {
2863
- if (value instanceof Blob) {
2864
- const reader = new FileReaderSync();
2865
- reader.readAsText(value);
2866
- const result = reader.result;
2867
- entries.push([key, result]);
2868
- } else {
2869
- entries.push([key, value]);
2870
- }
2871
- }
2872
- return JSON.stringify({
2873
- __formdata: entries,
2874
- });
2875
- }
2876
-
2877
- async function mockFormDataFromString(this: MockedRequest): Promise<FormData> {
2878
- const { __formdata } = await this.json();
2879
- expect(__formdata).toBeInstanceOf(Array);
2880
-
2881
- const form = new FormData();
2882
- for (const [key, value] of __formdata) {
2883
- form.set(key, value);
2884
- }
2885
- return form;
2886
- }
2887
-
2888
- // The following two functions workaround the fact that MSW does not yet support FormData in requests.
2889
- // We use the fact that MSW relies upon `node-fetch` internally, which will call `toString()` on the FormData object,
2890
- // rather than passing it through or serializing it as a proper FormData object.
2891
- // The hack is to serialize FormData to a JSON string by overriding `FormData.toString()`.
2892
- // And then to deserialize back to a FormData object by monkey-patching a `formData()` helper onto `MockedRequest`.
2893
- FormData.prototype.toString = mockFormDataToString;
2894
- export interface RestRequestWithFormData extends MockedRequest, RestRequest {
2895
- formData(): Promise<FormData>;
2896
- }
2897
- (MockedRequest.prototype as RestRequestWithFormData).formData =
2898
- mockFormDataFromString;