wrangler 0.0.0-e178d6fb → 0.0.0-e1b77719

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