wrangler 0.0.0-e9e9872 → 0.0.0-e9f8a2fa

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 (178) hide show
  1. package/README.md +25 -13
  2. package/bin/wrangler.js +51 -61
  3. package/config-schema.json +2939 -0
  4. package/package.json +222 -160
  5. package/templates/__tests__/pages-dev-util.test.ts +128 -0
  6. package/templates/__tests__/tsconfig-sanity.ts +12 -0
  7. package/templates/__tests__/tsconfig.json +8 -0
  8. package/templates/checked-fetch.js +19 -6
  9. package/templates/facade.d.ts +19 -0
  10. package/templates/gitignore +0 -4
  11. package/templates/init-tests/test-jest-new-worker.js +23 -0
  12. package/templates/init-tests/test-vitest-new-worker.js +24 -0
  13. package/templates/init-tests/test-vitest-new-worker.ts +25 -0
  14. package/templates/middleware/common.ts +67 -0
  15. package/templates/middleware/loader-modules.ts +133 -0
  16. package/templates/middleware/loader-sw.ts +229 -0
  17. package/templates/middleware/middleware-ensure-req-body-drained.ts +18 -0
  18. package/templates/middleware/middleware-miniflare3-json-error.ts +32 -0
  19. package/templates/middleware/middleware-pretty-error.ts +40 -0
  20. package/templates/middleware/middleware-scheduled.ts +15 -0
  21. package/templates/middleware/middleware-serve-static-assets.d.ts +6 -0
  22. package/templates/middleware/middleware-serve-static-assets.ts +56 -0
  23. package/templates/modules-watch-stub.js +4 -0
  24. package/templates/new-worker-scheduled.ts +1 -1
  25. package/templates/new-worker.js +1 -1
  26. package/templates/new-worker.ts +4 -1
  27. package/templates/pages-dev-pipeline.ts +32 -0
  28. package/templates/pages-dev-util.ts +55 -0
  29. package/templates/pages-shim.ts +9 -0
  30. package/templates/pages-template-plugin.ts +53 -18
  31. package/templates/pages-template-worker.ts +49 -12
  32. package/templates/startDevWorker/InspectorProxyWorker.ts +664 -0
  33. package/templates/startDevWorker/ProxyWorker.ts +334 -0
  34. package/templates/tsconfig-sanity.ts +11 -0
  35. package/templates/tsconfig.init.json +105 -0
  36. package/templates/tsconfig.json +5 -102
  37. package/wrangler-dist/InspectorProxyWorker.js +464 -0
  38. package/wrangler-dist/InspectorProxyWorker.js.map +6 -0
  39. package/wrangler-dist/ProxyWorker.js +240 -0
  40. package/wrangler-dist/ProxyWorker.js.map +6 -0
  41. package/wrangler-dist/cli.d.ts +23879 -146
  42. package/wrangler-dist/cli.js +186358 -107113
  43. package/wrangler-dist/wasm-sync.wasm +0 -0
  44. package/import_meta_url.js +0 -3
  45. package/miniflare-config-stubs/.env.empty +0 -0
  46. package/miniflare-config-stubs/package.empty.json +0 -1
  47. package/miniflare-config-stubs/wrangler.empty.toml +0 -0
  48. package/miniflare-dist/index.mjs +0 -4894
  49. package/src/__tests__/config-cache-without-cache-dir.test.ts +0 -38
  50. package/src/__tests__/config-cache.test.ts +0 -42
  51. package/src/__tests__/configuration.test.ts +0 -3946
  52. package/src/__tests__/dev.test.tsx +0 -1159
  53. package/src/__tests__/guess-worker-format.test.ts +0 -85
  54. package/src/__tests__/helpers/clipboardy-mock.js +0 -4
  55. package/src/__tests__/helpers/cmd-shim.d.ts +0 -11
  56. package/src/__tests__/helpers/mock-account-id.ts +0 -48
  57. package/src/__tests__/helpers/mock-bin.ts +0 -36
  58. package/src/__tests__/helpers/mock-cfetch.ts +0 -211
  59. package/src/__tests__/helpers/mock-console.ts +0 -97
  60. package/src/__tests__/helpers/mock-dialogs.ts +0 -153
  61. package/src/__tests__/helpers/mock-http-server.ts +0 -46
  62. package/src/__tests__/helpers/mock-istty.ts +0 -74
  63. package/src/__tests__/helpers/mock-kv.ts +0 -33
  64. package/src/__tests__/helpers/mock-oauth-flow.ts +0 -250
  65. package/src/__tests__/helpers/mock-process.ts +0 -39
  66. package/src/__tests__/helpers/mock-stdin.ts +0 -108
  67. package/src/__tests__/helpers/mock-web-socket.ts +0 -29
  68. package/src/__tests__/helpers/run-in-tmp.ts +0 -38
  69. package/src/__tests__/helpers/run-wrangler.ts +0 -16
  70. package/src/__tests__/helpers/write-worker-source.ts +0 -31
  71. package/src/__tests__/helpers/write-wrangler-toml.ts +0 -17
  72. package/src/__tests__/https-options.test.ts +0 -163
  73. package/src/__tests__/index.test.ts +0 -262
  74. package/src/__tests__/init.test.ts +0 -1992
  75. package/src/__tests__/jest.setup.ts +0 -120
  76. package/src/__tests__/kv.test.ts +0 -1650
  77. package/src/__tests__/logger.test.ts +0 -141
  78. package/src/__tests__/package-manager.test.ts +0 -353
  79. package/src/__tests__/pages.test.ts +0 -1254
  80. package/src/__tests__/parse.test.ts +0 -330
  81. package/src/__tests__/publish.test.ts +0 -6595
  82. package/src/__tests__/pubsub.test.ts +0 -367
  83. package/src/__tests__/r2.test.ts +0 -233
  84. package/src/__tests__/route.test.ts +0 -45
  85. package/src/__tests__/secret.test.ts +0 -492
  86. package/src/__tests__/tail.test.ts +0 -698
  87. package/src/__tests__/user.test.ts +0 -143
  88. package/src/__tests__/whoami.test.tsx +0 -230
  89. package/src/__tests__/worker-namespace.test.ts +0 -327
  90. package/src/abort.d.ts +0 -3
  91. package/src/api/dev.ts +0 -52
  92. package/src/api/index.ts +0 -1
  93. package/src/bundle-reporter.tsx +0 -29
  94. package/src/bundle.ts +0 -212
  95. package/src/cfetch/index.ts +0 -121
  96. package/src/cfetch/internal.ts +0 -149
  97. package/src/cli.ts +0 -27
  98. package/src/config/README.md +0 -107
  99. package/src/config/config.ts +0 -243
  100. package/src/config/diagnostics.ts +0 -80
  101. package/src/config/environment.ts +0 -453
  102. package/src/config/index.ts +0 -238
  103. package/src/config/validation-helpers.ts +0 -584
  104. package/src/config/validation.ts +0 -1745
  105. package/src/config-cache.ts +0 -83
  106. package/src/create-worker-preview.ts +0 -255
  107. package/src/create-worker-upload-form.ts +0 -263
  108. package/src/dev/dev-vars.ts +0 -36
  109. package/src/dev/dev.tsx +0 -417
  110. package/src/dev/local.tsx +0 -354
  111. package/src/dev/remote.tsx +0 -382
  112. package/src/dev/use-esbuild.ts +0 -150
  113. package/src/dev.tsx +0 -538
  114. package/src/dialogs.tsx +0 -135
  115. package/src/durable.ts +0 -102
  116. package/src/entry.ts +0 -328
  117. package/src/environment-variables.ts +0 -35
  118. package/src/errors.ts +0 -11
  119. package/src/faye-websocket.d.ts +0 -6
  120. package/src/generate.ts +0 -33
  121. package/src/git-client.ts +0 -42
  122. package/src/global-wrangler-config-path.ts +0 -26
  123. package/src/https-options.ts +0 -127
  124. package/src/index.tsx +0 -1896
  125. package/src/init.ts +0 -549
  126. package/src/inspect.ts +0 -662
  127. package/src/intl-polyfill.d.ts +0 -139
  128. package/src/is-interactive.ts +0 -12
  129. package/src/kv.ts +0 -430
  130. package/src/logger.ts +0 -73
  131. package/src/miniflare-cli/README.md +0 -30
  132. package/src/miniflare-cli/enum-keys.ts +0 -17
  133. package/src/miniflare-cli/index.ts +0 -53
  134. package/src/miniflare-cli/request-context.ts +0 -40
  135. package/src/module-collection.ts +0 -258
  136. package/src/open-in-browser.ts +0 -17
  137. package/src/package-manager.ts +0 -219
  138. package/src/pages/build.tsx +0 -202
  139. package/src/pages/constants.ts +0 -7
  140. package/src/pages/deployments.tsx +0 -101
  141. package/src/pages/dev.tsx +0 -964
  142. package/src/pages/functions/buildPlugin.ts +0 -105
  143. package/src/pages/functions/buildWorker.ts +0 -151
  144. package/src/pages/functions/filepath-routing.test.ts +0 -234
  145. package/src/pages/functions/filepath-routing.ts +0 -189
  146. package/src/pages/functions/identifiers.ts +0 -78
  147. package/src/pages/functions/routes.ts +0 -151
  148. package/src/pages/index.tsx +0 -84
  149. package/src/pages/projects.tsx +0 -157
  150. package/src/pages/publish.tsx +0 -335
  151. package/src/pages/types.ts +0 -40
  152. package/src/pages/upload.tsx +0 -396
  153. package/src/pages/utils.ts +0 -12
  154. package/src/parse.ts +0 -286
  155. package/src/paths.ts +0 -26
  156. package/src/preview.ts +0 -31
  157. package/src/proxy.ts +0 -476
  158. package/src/publish.ts +0 -780
  159. package/src/pubsub/index.ts +0 -286
  160. package/src/pubsub/pubsub-commands.tsx +0 -577
  161. package/src/r2.ts +0 -50
  162. package/src/selfsigned.d.ts +0 -29
  163. package/src/sites.tsx +0 -372
  164. package/src/tail/filters.ts +0 -279
  165. package/src/tail/index.ts +0 -299
  166. package/src/tail/printing.ts +0 -67
  167. package/src/update-check.ts +0 -19
  168. package/src/user/choose-account.tsx +0 -60
  169. package/src/user/env-vars.ts +0 -46
  170. package/src/user/generate-auth-url.ts +0 -33
  171. package/src/user/generate-random-state.ts +0 -16
  172. package/src/user/index.ts +0 -3
  173. package/src/user/user.tsx +0 -1170
  174. package/src/whoami.tsx +0 -90
  175. package/src/worker-namespace.ts +0 -190
  176. package/src/worker.ts +0 -191
  177. package/src/zones.ts +0 -76
  178. package/templates/static-asset-facade.js +0 -49
@@ -1,1254 +0,0 @@
1
- import { mkdirSync, writeFileSync } from "node:fs";
2
- import { chdir } from "node:process";
3
- import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
4
- import {
5
- createFetchResult,
6
- setMockRawResponse,
7
- setMockResponse,
8
- unsetAllMocks,
9
- } from "./helpers/mock-cfetch";
10
- import { mockConsoleMethods } from "./helpers/mock-console";
11
- import { runInTempDir } from "./helpers/run-in-tmp";
12
- import { runWrangler } from "./helpers/run-wrangler";
13
- import type { Deployment, Project, UploadPayloadFile } from "../pages/types";
14
- import type { FormData, RequestInit } from "undici";
15
-
16
- // Asserting within mock responses get swallowed, so run them out-of-band
17
- const outOfBandTests: (() => void)[] = [];
18
- function assertLater(fn: () => void) {
19
- outOfBandTests.push(fn);
20
- }
21
-
22
- function mockGetToken(jwt: string) {
23
- return setMockResponse(
24
- "/accounts/:accountId/pages/projects/foo/upload-token",
25
- async ([_url, accountId]) => {
26
- assertLater(() => {
27
- expect(accountId).toEqual("some-account-id");
28
- });
29
-
30
- return { jwt };
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
- beforeEach(() => {
42
- outOfBandTests.length = 0;
43
- });
44
- afterEach(() => {
45
- outOfBandTests.forEach((fn) => fn());
46
- });
47
-
48
- it("should should display a list of available subcommands, for pages with no subcommand", async () => {
49
- await runWrangler("pages");
50
- await endEventLoop();
51
-
52
- expect(std.out).toMatchInlineSnapshot(`
53
- "wrangler pages
54
-
55
- ⚡️ Configure Cloudflare Pages
56
-
57
- Commands:
58
- wrangler pages dev [directory] [-- command..] 🧑‍💻 Develop your full-stack Pages application locally
59
- wrangler pages project ⚡️ Interact with your Pages projects
60
- wrangler pages deployment 🚀 Interact with the deployments of a project
61
- wrangler pages publish [directory] 🆙 Publish a directory of static assets as a Pages deployment
62
-
63
- Flags:
64
- -c, --config Path to .toml configuration file [string]
65
- -h, --help Show help [boolean]
66
- -v, --version Show version number [boolean]
67
-
68
- 🚧 'wrangler pages <command>' is a beta command. Please report any issues to https://github.com/cloudflare/wrangler2/issues/new/choose"
69
- `);
70
- });
71
-
72
- describe("beta message for subcommands", () => {
73
- it("should display for pages:dev", async () => {
74
- await expect(
75
- runWrangler("pages dev")
76
- ).rejects.toThrowErrorMatchingInlineSnapshot(
77
- `"Must specify a directory of static assets to serve or a command to run."`
78
- );
79
-
80
- expect(std.out).toMatchInlineSnapshot(`
81
- "🚧 'wrangler pages <command>' is a beta command. Please report any issues to https://github.com/cloudflare/wrangler2/issues/new/choose
82
-
83
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choose"
84
- `);
85
- });
86
-
87
- it("should display for pages:functions:build", async () => {
88
- await expect(runWrangler("pages functions build")).rejects.toThrowError();
89
-
90
- expect(std.out).toMatchInlineSnapshot(`
91
- "🚧 'wrangler pages <command>' is a beta command. Please report any issues to https://github.com/cloudflare/wrangler2/issues/new/choose
92
-
93
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choose"
94
- `);
95
- });
96
- });
97
-
98
- describe("project list", () => {
99
- mockAccountId();
100
- mockApiToken();
101
-
102
- afterEach(() => {
103
- unsetAllMocks();
104
- });
105
-
106
- function mockListRequest(projects: unknown[]) {
107
- const requests = { count: 0 };
108
- setMockResponse(
109
- "/accounts/:accountId/pages/projects",
110
- ([_url, accountId], init, query) => {
111
- requests.count++;
112
- const pageSize = Number(query.get("per_page"));
113
- const page = Number(query.get("page"));
114
- const expectedPageSize = 10;
115
- const expectedPage = requests.count;
116
- assertLater(() => {
117
- expect(accountId).toEqual("some-account-id");
118
- expect(pageSize).toEqual(expectedPageSize);
119
- expect(page).toEqual(expectedPage);
120
- expect(init).toEqual({});
121
- });
122
- return projects.slice((page - 1) * pageSize, page * pageSize);
123
- }
124
- );
125
- return requests;
126
- }
127
-
128
- it("should make request to list projects", async () => {
129
- const projects: Project[] = [
130
- {
131
- name: "dogs",
132
- subdomain: "docs.pages.dev",
133
- domains: ["dogs.pages.dev"],
134
- source: {
135
- type: "github",
136
- },
137
- latest_deployment: {
138
- modified_on: "2021-11-17T14:52:26.133835Z",
139
- },
140
- created_on: "2021-11-17T14:52:26.133835Z",
141
- production_branch: "main",
142
- },
143
- {
144
- name: "cats",
145
- subdomain: "cats.pages.dev",
146
- domains: ["cats.pages.dev", "kitten.com"],
147
- latest_deployment: {
148
- modified_on: "2021-11-17T14:52:26.133835Z",
149
- },
150
- created_on: "2021-11-17T14:52:26.133835Z",
151
- production_branch: "main",
152
- },
153
- ];
154
-
155
- const requests = mockListRequest(projects);
156
- await runWrangler("pages project list");
157
-
158
- expect(requests.count).toBe(1);
159
- });
160
-
161
- it("should make multiple requests for paginated results", async () => {
162
- const projects: Project[] = [];
163
- for (let i = 0; i < 15; i++) {
164
- projects.push({
165
- name: "dogs" + i,
166
- subdomain: i + "dogs.pages.dev",
167
- domains: [i + "dogs.pages.dev"],
168
- source: {
169
- type: "github",
170
- },
171
- latest_deployment: {
172
- modified_on: "2021-11-17T14:52:26.133835Z",
173
- },
174
- created_on: "2021-11-17T14:52:26.133835Z",
175
- production_branch: "main",
176
- });
177
- }
178
- const requests = mockListRequest(projects);
179
- await runWrangler("pages project list");
180
- expect(requests.count).toEqual(2);
181
- });
182
- });
183
-
184
- describe("project create", () => {
185
- mockAccountId();
186
- mockApiToken();
187
-
188
- afterEach(() => {
189
- unsetAllMocks();
190
- });
191
-
192
- it("should create a project with a production branch", async () => {
193
- setMockResponse(
194
- "/accounts/:accountId/pages/projects",
195
- ([_url, accountId], init) => {
196
- const body = JSON.parse(init.body as string);
197
- assertLater(() => {
198
- expect(accountId).toEqual("some-account-id");
199
- expect(init.method).toEqual("POST");
200
- expect(body).toEqual({
201
- name: "a-new-project",
202
- production_branch: "main",
203
- });
204
- });
205
- return {
206
- name: "a-new-project",
207
- subdomain: "a-new-project.pages.dev",
208
- production_branch: "main",
209
- };
210
- }
211
- );
212
- await runWrangler(
213
- "pages project create a-new-project --production-branch=main"
214
- );
215
- expect(std.out).toMatchInlineSnapshot(`
216
- "✨ Successfully created the 'a-new-project' project. It will be available at https://a-new-project.pages.dev/ once you create your first deployment.
217
- To deploy a folder of assets, run 'wrangler pages publish [directory]'."
218
- `);
219
- });
220
- });
221
-
222
- describe("deployment list", () => {
223
- mockAccountId();
224
- mockApiToken();
225
-
226
- afterEach(() => {
227
- unsetAllMocks();
228
- });
229
- function mockListRequest(deployments: unknown[]) {
230
- const requests = { count: 0 };
231
- setMockResponse(
232
- "/accounts/:accountId/pages/projects/:project/deployments",
233
- ([_url, accountId, project]) => {
234
- requests.count++;
235
- assertLater(() => {
236
- expect(project).toEqual("images");
237
- expect(accountId).toEqual("some-account-id");
238
- });
239
- return deployments;
240
- }
241
- );
242
- return requests;
243
- }
244
-
245
- it("should make request to list deployments", async () => {
246
- const deployments: Deployment[] = [
247
- {
248
- id: "87bbc8fe-16be-45cd-81e0-63d722e82cdf",
249
- url: "https://87bbc8fe.images.pages.dev",
250
- environment: "preview",
251
- latest_stage: {
252
- ended_on: "2021-11-17T14:52:26.133835Z",
253
- status: "success",
254
- },
255
- deployment_trigger: {
256
- metadata: {
257
- branch: "main",
258
- commit_hash: "c7649364c4cb32ad4f65b530b9424e8be5bec9d6",
259
- },
260
- },
261
- project_name: "images",
262
- },
263
- ];
264
-
265
- const requests = mockListRequest(deployments);
266
- await runWrangler("pages deployment list --project-name=images");
267
-
268
- expect(requests.count).toBe(1);
269
- });
270
- });
271
-
272
- describe("deployment create", () => {
273
- let actualProcessEnvCI: string | undefined;
274
-
275
- mockAccountId();
276
- mockApiToken();
277
- runInTempDir();
278
-
279
- beforeEach(() => {
280
- actualProcessEnvCI = process.env.CI;
281
- process.env.CI = "true";
282
- });
283
-
284
- afterEach(() => {
285
- unsetAllMocks();
286
- process.env.CI = actualProcessEnvCI;
287
- });
288
-
289
- it("should be aliased with 'wrangler pages publish'", async () => {
290
- await runWrangler("pages publish --help");
291
- await endEventLoop();
292
-
293
- expect(std.out).toMatchInlineSnapshot(`
294
- "wrangler pages publish [directory]
295
-
296
- 🆙 Publish a directory of static assets as a Pages deployment
297
-
298
- Positionals:
299
- directory The directory of static files to upload [string]
300
-
301
- Flags:
302
- -h, --help Show help [boolean]
303
- -v, --version Show version number [boolean]
304
-
305
- Options:
306
- --project-name The name of the project you want to deploy to [string]
307
- --branch The name of the branch you want to deploy to [string]
308
- --commit-hash The SHA to attach to this deployment [string]
309
- --commit-message The commit message to attach to this deployment [string]
310
- --commit-dirty Whether or not the workspace should be considered dirty for this deployment [boolean]
311
-
312
- 🚧 'wrangler pages <command>' is a beta command. Please report any issues to https://github.com/cloudflare/wrangler2/issues/new/choose"
313
- `);
314
- });
315
-
316
- it("should upload a directory of files", async () => {
317
- writeFileSync("logo.png", "foobar");
318
-
319
- mockGetToken("<<funfetti-auth-jwt>>");
320
-
321
- setMockResponse(
322
- "/pages/assets/check-missing",
323
- "POST",
324
- async (_, init) => {
325
- const body = JSON.parse(init.body as string) as { hashes: string[] };
326
- assertLater(() => {
327
- expect(init.headers).toMatchObject({
328
- Authorization: "Bearer <<funfetti-auth-jwt>>",
329
- });
330
- expect(body).toMatchObject({
331
- hashes: ["2082190357cfd3617ccfe04f340c6247"],
332
- });
333
- });
334
- return body.hashes;
335
- }
336
- );
337
-
338
- setMockResponse("/pages/assets/upload", "POST", async (_, init) => {
339
- assertLater(() => {
340
- expect(init.headers).toMatchObject({
341
- Authorization: "Bearer <<funfetti-auth-jwt>>",
342
- });
343
- const body = JSON.parse(init.body as string) as UploadPayloadFile[];
344
- expect(body).toMatchObject([
345
- {
346
- key: "2082190357cfd3617ccfe04f340c6247",
347
- value: Buffer.from("foobar").toString("base64"),
348
- metadata: {
349
- contentType: "image/png",
350
- },
351
- base64: true,
352
- },
353
- ]);
354
- });
355
- });
356
-
357
- setMockResponse(
358
- "/accounts/:accountId/pages/projects/foo/deployments",
359
- async ([_url, accountId], init) => {
360
- assertLater(() => {
361
- expect(accountId).toEqual("some-account-id");
362
- expect(init.method).toEqual("POST");
363
- const body = init.body as FormData;
364
- const manifest = JSON.parse(body.get("manifest") as string);
365
- expect(manifest).toMatchInlineSnapshot(`
366
- Object {
367
- "/logo.png": "2082190357cfd3617ccfe04f340c6247",
368
- }
369
- `);
370
- });
371
-
372
- return {
373
- url: "https://abcxyz.foo.pages.dev/",
374
- };
375
- }
376
- );
377
-
378
- await runWrangler("pages publish . --project-name=foo");
379
-
380
- // TODO: Unmounting somehow loses this output
381
-
382
- // expect(std.out).toMatchInlineSnapshot(`
383
- // "✨ Success! Uploaded 1 files (TIMINGS)
384
-
385
- // ✨ Deployment complete! Take a peek over at https://abcxyz.foo.pages.dev/"
386
- // `);
387
- });
388
-
389
- it("should retry uploads", async () => {
390
- writeFileSync("logo.txt", "foobar");
391
-
392
- mockGetToken("<<funfetti-auth-jwt>>");
393
-
394
- setMockResponse(
395
- "/pages/assets/check-missing",
396
- "POST",
397
- async (_, init) => {
398
- const body = JSON.parse(init.body as string) as { hashes: string[] };
399
- assertLater(() => {
400
- expect(init.headers).toMatchObject({
401
- Authorization: "Bearer <<funfetti-auth-jwt>>",
402
- });
403
- expect(body).toMatchObject({
404
- hashes: ["1a98fb08af91aca4a7df1764a2c4ddb0"],
405
- });
406
- });
407
- return body.hashes;
408
- }
409
- );
410
-
411
- // Accumulate multiple requests then assert afterwards
412
- const requests: RequestInit[] = [];
413
- setMockRawResponse("/pages/assets/upload", "POST", async (_, init) => {
414
- requests.push(init);
415
-
416
- if (requests.length < 2) {
417
- return createFetchResult(null, false, [
418
- {
419
- code: 800000,
420
- message: "Something exploded, please retry",
421
- },
422
- ]);
423
- } else {
424
- return createFetchResult(null, true);
425
- }
426
- });
427
-
428
- setMockResponse(
429
- "/accounts/:accountId/pages/projects/foo/deployments",
430
- async ([_url, accountId], init) => {
431
- assertLater(() => {
432
- expect(accountId).toEqual("some-account-id");
433
- expect(init.method).toEqual("POST");
434
- const body = init.body as FormData;
435
- const manifest = JSON.parse(body.get("manifest") as string);
436
- expect(manifest).toMatchInlineSnapshot(`
437
- Object {
438
- "/logo.txt": "1a98fb08af91aca4a7df1764a2c4ddb0",
439
- }
440
- `);
441
- });
442
-
443
- return {
444
- url: "https://abcxyz.foo.pages.dev/",
445
- };
446
- }
447
- );
448
-
449
- await runWrangler("pages publish . --project-name=foo");
450
-
451
- // Assert two identical requests
452
- expect(requests.length).toBe(2);
453
- for (const init of requests) {
454
- assertLater(() => {
455
- expect(init.headers).toMatchObject({
456
- Authorization: "Bearer <<funfetti-auth-jwt>>",
457
- });
458
-
459
- const body = JSON.parse(init.body as string) as UploadPayloadFile[];
460
- expect(body).toMatchObject([
461
- {
462
- key: "1a98fb08af91aca4a7df1764a2c4ddb0",
463
- value: Buffer.from("foobar").toString("base64"),
464
- metadata: {
465
- contentType: "text/plain",
466
- },
467
- base64: true,
468
- },
469
- ]);
470
- });
471
- }
472
-
473
- expect(std.out).toMatchInlineSnapshot(`
474
- "✨ Success! Uploaded 1 files (TIMINGS)
475
-
476
- ✨ Deployment complete! Take a peek over at https://abcxyz.foo.pages.dev/"
477
- `);
478
- });
479
-
480
- it("should refetch a JWT if it expires while uploading", async () => {
481
- writeFileSync("logo.txt", "foobar");
482
-
483
- const cancelMockGetToken = mockGetToken("<<funfetti-auth-jwt>>");
484
-
485
- setMockResponse(
486
- "/pages/assets/check-missing",
487
- "POST",
488
- async (_, init) => {
489
- const body = JSON.parse(init.body as string) as { hashes: string[] };
490
- assertLater(() => {
491
- expect(init.headers).toMatchObject({
492
- Authorization: "Bearer <<funfetti-auth-jwt>>",
493
- });
494
- expect(body).toMatchObject({
495
- hashes: ["1a98fb08af91aca4a7df1764a2c4ddb0"],
496
- });
497
- });
498
- return body.hashes;
499
- }
500
- );
501
-
502
- // Accumulate multiple requests then assert afterwards
503
- const requests: RequestInit[] = [];
504
- setMockRawResponse("/pages/assets/upload", "POST", async (_, init) => {
505
- requests.push(init);
506
-
507
- // Fail just the first request
508
- if (requests.length < 2) {
509
- cancelMockGetToken();
510
- mockGetToken("<<funfetti-auth-jwt2>>");
511
- return createFetchResult(null, false, [
512
- {
513
- code: 8000013,
514
- message: "Authorization failed",
515
- },
516
- ]);
517
- } else {
518
- return createFetchResult(null, true);
519
- }
520
- });
521
-
522
- setMockResponse(
523
- "/accounts/:accountId/pages/projects/foo/deployments",
524
- async ([_url, accountId], init) => {
525
- assertLater(() => {
526
- expect(accountId).toEqual("some-account-id");
527
- expect(init.method).toEqual("POST");
528
- const body = init.body as FormData;
529
- const manifest = JSON.parse(body.get("manifest") as string);
530
- expect(manifest).toMatchInlineSnapshot(`
531
- Object {
532
- "/logo.txt": "1a98fb08af91aca4a7df1764a2c4ddb0",
533
- }
534
- `);
535
- });
536
-
537
- return {
538
- url: "https://abcxyz.foo.pages.dev/",
539
- };
540
- }
541
- );
542
-
543
- await runWrangler("pages publish . --project-name=foo");
544
-
545
- // Assert two requests
546
- expect(requests.length).toBe(2);
547
-
548
- expect(requests[0].headers).toMatchObject({
549
- Authorization: "Bearer <<funfetti-auth-jwt>>",
550
- });
551
-
552
- expect(requests[1].headers).toMatchObject({
553
- Authorization: "Bearer <<funfetti-auth-jwt2>>",
554
- });
555
-
556
- for (const init of requests) {
557
- const body = JSON.parse(init.body as string) as UploadPayloadFile[];
558
- expect(body).toMatchObject([
559
- {
560
- key: "1a98fb08af91aca4a7df1764a2c4ddb0",
561
- value: Buffer.from("foobar").toString("base64"),
562
- metadata: {
563
- contentType: "text/plain",
564
- },
565
- base64: true,
566
- },
567
- ]);
568
- }
569
-
570
- expect(std.out).toMatchInlineSnapshot(`
571
- "✨ Success! Uploaded 1 files (TIMINGS)
572
-
573
- ✨ Deployment complete! Take a peek over at https://abcxyz.foo.pages.dev/"
574
- `);
575
- });
576
-
577
- it("should try to use multiple buckets (up to the max concurrency)", async () => {
578
- writeFileSync("logo.txt", "foobar");
579
- writeFileSync("logo.png", "foobar");
580
- writeFileSync("logo.html", "foobar");
581
- writeFileSync("logo.js", "foobar");
582
-
583
- mockGetToken("<<funfetti-auth-jwt>>");
584
-
585
- setMockResponse(
586
- "/pages/assets/check-missing",
587
- "POST",
588
- async (_, init) => {
589
- const body = JSON.parse(init.body as string) as { hashes: string[] };
590
- assertLater(() => {
591
- expect(init.headers).toMatchObject({
592
- Authorization: "Bearer <<funfetti-auth-jwt>>",
593
- });
594
- expect(body).toMatchObject({
595
- hashes: expect.arrayContaining([
596
- "d96fef225537c9f5e44a3cb27fd0b492",
597
- "2082190357cfd3617ccfe04f340c6247",
598
- "6be321bef99e758250dac034474ddbb8",
599
- "1a98fb08af91aca4a7df1764a2c4ddb0",
600
- ]),
601
- });
602
- });
603
- return body.hashes;
604
- }
605
- );
606
-
607
- // Accumulate multiple requests then assert afterwards
608
- const requests: RequestInit[] = [];
609
- setMockResponse("/pages/assets/upload", "POST", async (_, init) => {
610
- requests.push(init);
611
- });
612
-
613
- setMockResponse(
614
- "/accounts/:accountId/pages/projects/foo/deployments",
615
- async ([_url, accountId], init) => {
616
- assertLater(() => {
617
- expect(accountId).toEqual("some-account-id");
618
- expect(init.method).toEqual("POST");
619
- const body = init.body as FormData;
620
- const manifest = JSON.parse(body.get("manifest") as string);
621
- expect(manifest).toMatchInlineSnapshot(`
622
- Object {
623
- "/logo.html": "d96fef225537c9f5e44a3cb27fd0b492",
624
- "/logo.js": "6be321bef99e758250dac034474ddbb8",
625
- "/logo.png": "2082190357cfd3617ccfe04f340c6247",
626
- "/logo.txt": "1a98fb08af91aca4a7df1764a2c4ddb0",
627
- }
628
- `);
629
- });
630
-
631
- return {
632
- url: "https://abcxyz.foo.pages.dev/",
633
- };
634
- }
635
- );
636
-
637
- await runWrangler("pages publish . --project-name=foo");
638
-
639
- // We have 3 buckets, so expect 3 uploads
640
- expect(requests.length).toBe(3);
641
- const bodies: UploadPayloadFile[][] = [];
642
- for (const init of requests) {
643
- expect(init.headers).toMatchObject({
644
- Authorization: "Bearer <<funfetti-auth-jwt>>",
645
- });
646
- bodies.push(JSON.parse(init.body as string) as UploadPayloadFile[]);
647
- }
648
- // First bucket should end up with 2 files
649
- expect(bodies.map((b) => b.length)).toEqual([2, 1, 1]);
650
- // But we don't know the order, so flatten and test without ordering
651
- expect(bodies.flatMap((b) => b)).toEqual(
652
- expect.arrayContaining([
653
- {
654
- base64: true,
655
- key: "d96fef225537c9f5e44a3cb27fd0b492",
656
- metadata: { contentType: "text/html" },
657
- value: "Zm9vYmFy",
658
- },
659
- {
660
- base64: true,
661
- key: "1a98fb08af91aca4a7df1764a2c4ddb0",
662
- metadata: { contentType: "text/plain" },
663
- value: "Zm9vYmFy",
664
- },
665
- {
666
- base64: true,
667
- key: "6be321bef99e758250dac034474ddbb8",
668
- metadata: { contentType: "application/javascript" },
669
- value: "Zm9vYmFy",
670
- },
671
- {
672
- base64: true,
673
- key: "2082190357cfd3617ccfe04f340c6247",
674
- metadata: { contentType: "image/png" },
675
- value: "Zm9vYmFy",
676
- },
677
- ])
678
- );
679
-
680
- expect(std.out).toMatchInlineSnapshot(`
681
- "✨ Success! Uploaded 4 files (TIMINGS)
682
-
683
- ✨ Deployment complete! Take a peek over at https://abcxyz.foo.pages.dev/"
684
- `);
685
- });
686
-
687
- it("should resolve child directories correctly", async () => {
688
- mkdirSync("public");
689
- mkdirSync("public/imgs");
690
- writeFileSync("public/logo.txt", "foobar");
691
- writeFileSync("public/imgs/logo.png", "foobar");
692
- writeFileSync("public/logo.html", "foobar");
693
- writeFileSync("public/logo.js", "foobar");
694
-
695
- mockGetToken("<<funfetti-auth-jwt>>");
696
-
697
- setMockResponse(
698
- "/pages/assets/check-missing",
699
- "POST",
700
- async (_, init) => {
701
- const body = JSON.parse(init.body as string) as {
702
- hashes: string[];
703
- };
704
- assertLater(() => {
705
- expect(init.headers).toMatchObject({
706
- Authorization: "Bearer <<funfetti-auth-jwt>>",
707
- });
708
- expect(body).toMatchObject({
709
- hashes: expect.arrayContaining([
710
- "d96fef225537c9f5e44a3cb27fd0b492",
711
- "2082190357cfd3617ccfe04f340c6247",
712
- "6be321bef99e758250dac034474ddbb8",
713
- "1a98fb08af91aca4a7df1764a2c4ddb0",
714
- ]),
715
- });
716
- });
717
- return body.hashes;
718
- }
719
- );
720
-
721
- // Accumulate multiple requests then assert afterwards
722
- const requests: RequestInit[] = [];
723
- setMockResponse("/pages/assets/upload", "POST", async (_, init) => {
724
- requests.push(init);
725
- });
726
-
727
- setMockResponse(
728
- "/accounts/:accountId/pages/projects/foo/deployments",
729
- async ([_url, accountId], init) => {
730
- assertLater(() => {
731
- expect(accountId).toEqual("some-account-id");
732
- expect(init.method).toEqual("POST");
733
- const body = init.body as FormData;
734
- const manifest = JSON.parse(body.get("manifest") as string);
735
- expect(manifest).toMatchInlineSnapshot(`
736
- Object {
737
- "/imgs/logo.png": "2082190357cfd3617ccfe04f340c6247",
738
- "/logo.html": "d96fef225537c9f5e44a3cb27fd0b492",
739
- "/logo.js": "6be321bef99e758250dac034474ddbb8",
740
- "/logo.txt": "1a98fb08af91aca4a7df1764a2c4ddb0",
741
- }
742
- `);
743
- });
744
-
745
- return {
746
- url: "https://abcxyz.foo.pages.dev/",
747
- };
748
- }
749
- );
750
-
751
- await runWrangler(`pages publish public --project-name=foo`);
752
-
753
- // We have 3 buckets, so expect 3 uploads
754
- expect(requests.length).toBe(3);
755
- const bodies: UploadPayloadFile[][] = [];
756
- for (const init of requests) {
757
- expect(init.headers).toMatchObject({
758
- Authorization: "Bearer <<funfetti-auth-jwt>>",
759
- });
760
- bodies.push(JSON.parse(init.body as string) as UploadPayloadFile[]);
761
- }
762
- // First bucket should end up with 2 files
763
- expect(bodies.map((b) => b.length)).toEqual([2, 1, 1]);
764
- // But we don't know the order, so flatten and test without ordering
765
- expect(bodies.flatMap((b) => b)).toEqual(
766
- expect.arrayContaining([
767
- {
768
- base64: true,
769
- key: "d96fef225537c9f5e44a3cb27fd0b492",
770
- metadata: { contentType: "text/html" },
771
- value: "Zm9vYmFy",
772
- },
773
- {
774
- base64: true,
775
- key: "1a98fb08af91aca4a7df1764a2c4ddb0",
776
- metadata: { contentType: "text/plain" },
777
- value: "Zm9vYmFy",
778
- },
779
- {
780
- base64: true,
781
- key: "6be321bef99e758250dac034474ddbb8",
782
- metadata: { contentType: "application/javascript" },
783
- value: "Zm9vYmFy",
784
- },
785
- {
786
- base64: true,
787
- key: "2082190357cfd3617ccfe04f340c6247",
788
- metadata: { contentType: "image/png" },
789
- value: "Zm9vYmFy",
790
- },
791
- ])
792
- );
793
-
794
- expect(std.out).toMatchInlineSnapshot(`
795
- "✨ Success! Uploaded 4 files (TIMINGS)
796
-
797
- ✨ Deployment complete! Take a peek over at https://abcxyz.foo.pages.dev/"
798
- `);
799
- });
800
-
801
- it("should resolve the current directory correctly", async () => {
802
- mkdirSync("public");
803
- mkdirSync("public/imgs");
804
- writeFileSync("public/logo.txt", "foobar");
805
- writeFileSync("public/imgs/logo.png", "foobar");
806
- writeFileSync("public/logo.html", "foobar");
807
- writeFileSync("public/logo.js", "foobar");
808
-
809
- mockGetToken("<<funfetti-auth-jwt>>");
810
-
811
- setMockResponse(
812
- "/pages/assets/check-missing",
813
- "POST",
814
- async (_, init) => {
815
- const body = JSON.parse(init.body as string) as {
816
- hashes: string[];
817
- };
818
- assertLater(() => {
819
- expect(init.headers).toMatchObject({
820
- Authorization: "Bearer <<funfetti-auth-jwt>>",
821
- });
822
- expect(body).toMatchObject({
823
- hashes: expect.arrayContaining([
824
- "d96fef225537c9f5e44a3cb27fd0b492",
825
- "2082190357cfd3617ccfe04f340c6247",
826
- "6be321bef99e758250dac034474ddbb8",
827
- "1a98fb08af91aca4a7df1764a2c4ddb0",
828
- ]),
829
- });
830
- });
831
- return body.hashes;
832
- }
833
- );
834
-
835
- // Accumulate multiple requests then assert afterwards
836
- const requests: RequestInit[] = [];
837
- setMockResponse("/pages/assets/upload", "POST", async (_, init) => {
838
- requests.push(init);
839
- });
840
-
841
- setMockResponse(
842
- "/accounts/:accountId/pages/projects/foo/deployments",
843
- async ([_url, accountId], init) => {
844
- assertLater(() => {
845
- expect(accountId).toEqual("some-account-id");
846
- expect(init.method).toEqual("POST");
847
- const body = init.body as FormData;
848
- const manifest = JSON.parse(body.get("manifest") as string);
849
- expect(manifest).toMatchInlineSnapshot(`
850
- Object {
851
- "/imgs/logo.png": "2082190357cfd3617ccfe04f340c6247",
852
- "/logo.html": "d96fef225537c9f5e44a3cb27fd0b492",
853
- "/logo.js": "6be321bef99e758250dac034474ddbb8",
854
- "/logo.txt": "1a98fb08af91aca4a7df1764a2c4ddb0",
855
- }
856
- `);
857
- });
858
-
859
- return {
860
- url: "https://abcxyz.foo.pages.dev/",
861
- };
862
- }
863
- );
864
-
865
- chdir("public");
866
- await runWrangler(`pages publish . --project-name=foo`);
867
-
868
- // We have 3 buckets, so expect 3 uploads
869
- expect(requests.length).toBe(3);
870
- const bodies: UploadPayloadFile[][] = [];
871
- for (const init of requests) {
872
- expect(init.headers).toMatchObject({
873
- Authorization: "Bearer <<funfetti-auth-jwt>>",
874
- });
875
- bodies.push(JSON.parse(init.body as string) as UploadPayloadFile[]);
876
- }
877
- // First bucket should end up with 2 files
878
- expect(bodies.map((b) => b.length)).toEqual([2, 1, 1]);
879
- // But we don't know the order, so flatten and test without ordering
880
- expect(bodies.flatMap((b) => b)).toEqual(
881
- expect.arrayContaining([
882
- {
883
- base64: true,
884
- key: "d96fef225537c9f5e44a3cb27fd0b492",
885
- metadata: { contentType: "text/html" },
886
- value: "Zm9vYmFy",
887
- },
888
- {
889
- base64: true,
890
- key: "1a98fb08af91aca4a7df1764a2c4ddb0",
891
- metadata: { contentType: "text/plain" },
892
- value: "Zm9vYmFy",
893
- },
894
- {
895
- base64: true,
896
- key: "6be321bef99e758250dac034474ddbb8",
897
- metadata: { contentType: "application/javascript" },
898
- value: "Zm9vYmFy",
899
- },
900
- {
901
- base64: true,
902
- key: "2082190357cfd3617ccfe04f340c6247",
903
- metadata: { contentType: "image/png" },
904
- value: "Zm9vYmFy",
905
- },
906
- ])
907
- );
908
-
909
- expect(std.out).toMatchInlineSnapshot(`
910
- "✨ Success! Uploaded 4 files (TIMINGS)
911
-
912
- ✨ Deployment complete! Take a peek over at https://abcxyz.foo.pages.dev/"
913
- `);
914
- });
915
-
916
- it("should not error when directory names contain periods and houses a extensionless file", async () => {
917
- mkdirSync(".well-known");
918
- // Note: same content as previous test, but since it's a different extension,
919
- // it hashes to a different value
920
- writeFileSync(".well-known/foobar", "foobar");
921
-
922
- mockGetToken("<<funfetti-auth-jwt>>");
923
-
924
- setMockResponse(
925
- "/pages/assets/check-missing",
926
- "POST",
927
- async (_, init) => {
928
- const body = JSON.parse(init.body as string) as { hashes: string[] };
929
- assertLater(() => {
930
- expect(init.headers).toMatchObject({
931
- Authorization: "Bearer <<funfetti-auth-jwt>>",
932
- });
933
- expect(body).toMatchObject({
934
- hashes: ["7b764dacfd211bebd8077828a7ddefd7"],
935
- });
936
- });
937
- return body.hashes;
938
- }
939
- );
940
-
941
- setMockResponse("/pages/assets/upload", "POST", async (_, init) => {
942
- assertLater(() => {
943
- expect(init.headers).toMatchObject({
944
- Authorization: "Bearer <<funfetti-auth-jwt>>",
945
- });
946
- const body = JSON.parse(init.body as string) as UploadPayloadFile[];
947
- expect(body).toMatchObject([
948
- {
949
- key: "7b764dacfd211bebd8077828a7ddefd7",
950
- value: Buffer.from("foobar").toString("base64"),
951
- metadata: {
952
- contentType: "application/octet-stream",
953
- },
954
- base64: true,
955
- },
956
- ]);
957
- });
958
- });
959
-
960
- setMockResponse(
961
- "/accounts/:accountId/pages/projects/foo/deployments",
962
- async () => ({
963
- url: "https://abcxyz.foo.pages.dev/",
964
- })
965
- );
966
-
967
- await runWrangler("pages publish . --project-name=foo");
968
-
969
- expect(std.err).toMatchInlineSnapshot(`""`);
970
- });
971
-
972
- it("should throw an error if user attempts to use config with pages", async () => {
973
- await expect(
974
- runWrangler("pages dev --config foo.toml")
975
- ).rejects.toThrowErrorMatchingInlineSnapshot(
976
- `"Pages does not support wrangler.toml"`
977
- );
978
- await expect(
979
- runWrangler("pages publish --config foo.toml")
980
- ).rejects.toThrowErrorMatchingInlineSnapshot(
981
- `"Pages does not support wrangler.toml"`
982
- );
983
- });
984
- });
985
-
986
- describe("project upload", () => {
987
- const ENV_COPY = process.env;
988
-
989
- mockAccountId();
990
- mockApiToken();
991
- runInTempDir();
992
-
993
- beforeEach(() => {
994
- process.env.CI = "true";
995
- process.env.CF_PAGES_UPLOAD_JWT = "<<funfetti-auth-jwt>>";
996
- });
997
-
998
- afterEach(() => {
999
- unsetAllMocks();
1000
- process.env = ENV_COPY;
1001
- });
1002
-
1003
- it("should upload a directory of files with a provided JWT", async () => {
1004
- writeFileSync("logo.png", "foobar");
1005
-
1006
- setMockResponse(
1007
- "/pages/assets/check-missing",
1008
- "POST",
1009
- async (_, init) => {
1010
- const body = JSON.parse(init.body as string) as { hashes: string[] };
1011
- assertLater(() => {
1012
- expect(init.headers).toMatchObject({
1013
- Authorization: "Bearer <<funfetti-auth-jwt>>",
1014
- });
1015
- expect(body).toMatchObject({
1016
- hashes: ["2082190357cfd3617ccfe04f340c6247"],
1017
- });
1018
- });
1019
- return body.hashes;
1020
- }
1021
- );
1022
-
1023
- setMockResponse("/pages/assets/upload", "POST", async (_, init) => {
1024
- assertLater(() => {
1025
- expect(init.headers).toMatchObject({
1026
- Authorization: "Bearer <<funfetti-auth-jwt>>",
1027
- });
1028
- const body = JSON.parse(init.body as string) as UploadPayloadFile[];
1029
- expect(body).toMatchObject([
1030
- {
1031
- key: "2082190357cfd3617ccfe04f340c6247",
1032
- value: Buffer.from("foobar").toString("base64"),
1033
- metadata: {
1034
- contentType: "image/png",
1035
- },
1036
- base64: true,
1037
- },
1038
- ]);
1039
- });
1040
- });
1041
-
1042
- await runWrangler("pages project upload .");
1043
-
1044
- expect(std.out).toMatchInlineSnapshot(`
1045
- "✨ Success! Uploaded 1 files (TIMINGS)
1046
-
1047
- ✨ Upload complete!"
1048
- `);
1049
- });
1050
-
1051
- it("should retry uploads", async () => {
1052
- writeFileSync("logo.txt", "foobar");
1053
-
1054
- setMockResponse(
1055
- "/pages/assets/check-missing",
1056
- "POST",
1057
- async (_, init) => {
1058
- const body = JSON.parse(init.body as string) as { hashes: string[] };
1059
- assertLater(() => {
1060
- expect(init.headers).toMatchObject({
1061
- Authorization: "Bearer <<funfetti-auth-jwt>>",
1062
- });
1063
- expect(body).toMatchObject({
1064
- hashes: ["1a98fb08af91aca4a7df1764a2c4ddb0"],
1065
- });
1066
- });
1067
- return body.hashes;
1068
- }
1069
- );
1070
-
1071
- // Accumulate multiple requests then assert afterwards
1072
- const requests: RequestInit[] = [];
1073
- setMockRawResponse("/pages/assets/upload", "POST", async (_, init) => {
1074
- requests.push(init);
1075
-
1076
- if (requests.length < 2) {
1077
- return createFetchResult(null, false, [
1078
- {
1079
- code: 800000,
1080
- message: "Something exploded, please retry",
1081
- },
1082
- ]);
1083
- } else {
1084
- return createFetchResult(null, true);
1085
- }
1086
- });
1087
-
1088
- await runWrangler("pages project upload .");
1089
-
1090
- // Assert two identical requests
1091
- expect(requests.length).toBe(2);
1092
- for (const init of requests) {
1093
- assertLater(() => {
1094
- expect(init.headers).toMatchObject({
1095
- Authorization: "Bearer <<funfetti-auth-jwt>>",
1096
- });
1097
-
1098
- const body = JSON.parse(init.body as string) as UploadPayloadFile[];
1099
- expect(body).toMatchObject([
1100
- {
1101
- key: "1a98fb08af91aca4a7df1764a2c4ddb0",
1102
- value: Buffer.from("foobar").toString("base64"),
1103
- metadata: {
1104
- contentType: "text/plain",
1105
- },
1106
- base64: true,
1107
- },
1108
- ]);
1109
- });
1110
- }
1111
-
1112
- expect(std.out).toMatchInlineSnapshot(`
1113
- "✨ Success! Uploaded 1 files (TIMINGS)
1114
-
1115
- ✨ Upload complete!"
1116
- `);
1117
- });
1118
-
1119
- it("should try to use multiple buckets (up to the max concurrency)", async () => {
1120
- writeFileSync("logo.txt", "foobar");
1121
- writeFileSync("logo.png", "foobar");
1122
- writeFileSync("logo.html", "foobar");
1123
- writeFileSync("logo.js", "foobar");
1124
-
1125
- mockGetToken("<<funfetti-auth-jwt>>");
1126
-
1127
- setMockResponse(
1128
- "/pages/assets/check-missing",
1129
- "POST",
1130
- async (_, init) => {
1131
- const body = JSON.parse(init.body as string) as { hashes: string[] };
1132
- assertLater(() => {
1133
- expect(init.headers).toMatchObject({
1134
- Authorization: "Bearer <<funfetti-auth-jwt>>",
1135
- });
1136
- expect(body).toMatchObject({
1137
- hashes: expect.arrayContaining([
1138
- "d96fef225537c9f5e44a3cb27fd0b492",
1139
- "2082190357cfd3617ccfe04f340c6247",
1140
- "6be321bef99e758250dac034474ddbb8",
1141
- "1a98fb08af91aca4a7df1764a2c4ddb0",
1142
- ]),
1143
- });
1144
- });
1145
- return body.hashes;
1146
- }
1147
- );
1148
-
1149
- // Accumulate multiple requests then assert afterwards
1150
- const requests: RequestInit[] = [];
1151
- setMockResponse("/pages/assets/upload", "POST", async (_, init) => {
1152
- requests.push(init);
1153
- });
1154
-
1155
- await runWrangler("pages project upload .");
1156
-
1157
- // We have 3 buckets, so expect 3 uploads
1158
- expect(requests.length).toBe(3);
1159
- const bodies: UploadPayloadFile[][] = [];
1160
- for (const init of requests) {
1161
- expect(init.headers).toMatchObject({
1162
- Authorization: "Bearer <<funfetti-auth-jwt>>",
1163
- });
1164
- bodies.push(JSON.parse(init.body as string) as UploadPayloadFile[]);
1165
- }
1166
- // First bucket should end up with 2 files
1167
- expect(bodies.map((b) => b.length)).toEqual([2, 1, 1]);
1168
- // But we don't know the order, so flatten and test without ordering
1169
- expect(bodies.flatMap((b) => b)).toEqual(
1170
- expect.arrayContaining([
1171
- {
1172
- base64: true,
1173
- key: "d96fef225537c9f5e44a3cb27fd0b492",
1174
- metadata: { contentType: "text/html" },
1175
- value: "Zm9vYmFy",
1176
- },
1177
- {
1178
- base64: true,
1179
- key: "1a98fb08af91aca4a7df1764a2c4ddb0",
1180
- metadata: { contentType: "text/plain" },
1181
- value: "Zm9vYmFy",
1182
- },
1183
- {
1184
- base64: true,
1185
- key: "6be321bef99e758250dac034474ddbb8",
1186
- metadata: { contentType: "application/javascript" },
1187
- value: "Zm9vYmFy",
1188
- },
1189
- {
1190
- base64: true,
1191
- key: "2082190357cfd3617ccfe04f340c6247",
1192
- metadata: { contentType: "image/png" },
1193
- value: "Zm9vYmFy",
1194
- },
1195
- ])
1196
- );
1197
-
1198
- expect(std.out).toMatchInlineSnapshot(`
1199
- "✨ Success! Uploaded 4 files (TIMINGS)
1200
-
1201
- ✨ Upload complete!"
1202
- `);
1203
- });
1204
-
1205
- it("should not error when directory names contain periods and houses a extensionless file", async () => {
1206
- mkdirSync(".well-known");
1207
- // Note: same content as previous test, but since it's a different extension,
1208
- // it hashes to a different value
1209
- writeFileSync(".well-known/foobar", "foobar");
1210
-
1211
- mockGetToken("<<funfetti-auth-jwt>>");
1212
-
1213
- setMockResponse(
1214
- "/pages/assets/check-missing",
1215
- "POST",
1216
- async (_, init) => {
1217
- const body = JSON.parse(init.body as string) as { hashes: string[] };
1218
- assertLater(() => {
1219
- expect(init.headers).toMatchObject({
1220
- Authorization: "Bearer <<funfetti-auth-jwt>>",
1221
- });
1222
- expect(body).toMatchObject({
1223
- hashes: ["7b764dacfd211bebd8077828a7ddefd7"],
1224
- });
1225
- });
1226
- return body.hashes;
1227
- }
1228
- );
1229
-
1230
- setMockResponse("/pages/assets/upload", "POST", async (_, init) => {
1231
- assertLater(() => {
1232
- expect(init.headers).toMatchObject({
1233
- Authorization: "Bearer <<funfetti-auth-jwt>>",
1234
- });
1235
- const body = JSON.parse(init.body as string) as UploadPayloadFile[];
1236
- expect(body).toMatchObject([
1237
- {
1238
- key: "7b764dacfd211bebd8077828a7ddefd7",
1239
- value: Buffer.from("foobar").toString("base64"),
1240
- metadata: {
1241
- contentType: "application/octet-stream",
1242
- },
1243
- base64: true,
1244
- },
1245
- ]);
1246
- });
1247
- });
1248
-
1249
- await runWrangler("pages project upload .");
1250
-
1251
- expect(std.err).toMatchInlineSnapshot(`""`);
1252
- });
1253
- });
1254
- });