wrangler 0.0.0-bf67bf2 → 0.0.0-bfbb0be

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