wrangler 0.0.0-e8e684c7 → 0.0.0-e9106dfa5

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