wrangler 0.0.0-e6733a3 → 0.0.0-e9307365

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 (220) hide show
  1. package/Cloudflare_CA.pem +18 -0
  2. package/README.md +51 -14
  3. package/bin/wrangler.js +159 -27
  4. package/kv-asset-handler.js +1 -0
  5. package/miniflare-dist/index.mjs +5416 -0
  6. package/package.json +171 -124
  7. package/src/__tests__/config-cache-without-cache-dir.test.ts +38 -0
  8. package/src/__tests__/config-cache.test.ts +42 -0
  9. package/src/__tests__/configuration.test.ts +4022 -0
  10. package/src/__tests__/dev.test.tsx +1290 -62
  11. package/src/__tests__/generate.test.ts +93 -0
  12. package/src/__tests__/guess-worker-format.test.ts +85 -0
  13. package/src/__tests__/{clipboardy-mock.js → helpers/clipboardy-mock.js} +0 -0
  14. package/src/__tests__/helpers/cmd-shim.d.ts +11 -0
  15. package/src/__tests__/helpers/mock-account-id.ts +48 -0
  16. package/src/__tests__/helpers/mock-bin.ts +36 -0
  17. package/src/__tests__/helpers/mock-cfetch.ts +263 -0
  18. package/src/__tests__/helpers/mock-console.ts +97 -0
  19. package/src/__tests__/helpers/mock-dialogs.ts +153 -0
  20. package/src/__tests__/helpers/mock-http-server.ts +46 -0
  21. package/src/__tests__/helpers/mock-istty.ts +74 -0
  22. package/src/__tests__/helpers/mock-kv.ts +33 -0
  23. package/src/__tests__/helpers/mock-oauth-flow.ts +250 -0
  24. package/src/__tests__/helpers/mock-process.ts +39 -0
  25. package/src/__tests__/helpers/mock-stdin.ts +108 -0
  26. package/src/__tests__/helpers/mock-web-socket.ts +29 -0
  27. package/src/__tests__/helpers/run-in-tmp.ts +38 -0
  28. package/src/__tests__/helpers/run-wrangler.ts +16 -0
  29. package/src/__tests__/helpers/write-worker-source.ts +31 -0
  30. package/src/__tests__/helpers/write-wrangler-toml.ts +17 -0
  31. package/src/__tests__/https-options.test.ts +163 -0
  32. package/src/__tests__/index.test.ts +242 -284
  33. package/src/__tests__/init.test.ts +1992 -0
  34. package/src/__tests__/jest.setup.ts +138 -10
  35. package/src/__tests__/kv.test.ts +1459 -886
  36. package/src/__tests__/logger.test.ts +141 -0
  37. package/src/__tests__/metrics.test.ts +450 -0
  38. package/src/__tests__/package-manager.test.ts +353 -0
  39. package/src/__tests__/pages.test.ts +1261 -0
  40. package/src/__tests__/parse.test.ts +330 -0
  41. package/src/__tests__/publish.test.ts +6914 -0
  42. package/src/__tests__/pubsub.test.ts +367 -0
  43. package/src/__tests__/r2.test.ts +317 -0
  44. package/src/__tests__/route.test.ts +45 -0
  45. package/src/__tests__/secret.test.ts +492 -0
  46. package/src/__tests__/tail.test.ts +698 -0
  47. package/src/__tests__/test-old-node-version.js +31 -0
  48. package/src/__tests__/user.test.ts +144 -0
  49. package/src/__tests__/validate-dev-props.test.ts +56 -0
  50. package/src/__tests__/version.test.ts +35 -0
  51. package/src/__tests__/whoami.test.tsx +289 -0
  52. package/src/__tests__/worker-namespace.test.ts +327 -0
  53. package/src/abort.d.ts +3 -0
  54. package/src/api/dev.ts +86 -0
  55. package/src/api/index.ts +1 -0
  56. package/src/bundle-reporter.tsx +29 -0
  57. package/src/bundle.ts +472 -0
  58. package/src/cfetch/index.ts +88 -69
  59. package/src/cfetch/internal.ts +156 -44
  60. package/src/cli.ts +24 -6
  61. package/src/config/README.md +107 -0
  62. package/src/config/config.ts +263 -0
  63. package/src/config/diagnostics.ts +80 -0
  64. package/src/config/environment.ts +485 -0
  65. package/src/config/index.ts +251 -0
  66. package/src/config/validation-helpers.ts +584 -0
  67. package/src/config/validation.ts +1856 -0
  68. package/src/config-cache.ts +84 -0
  69. package/src/create-worker-preview.ts +257 -0
  70. package/src/create-worker-upload-form.ts +288 -0
  71. package/src/dev/dev-vars.ts +36 -0
  72. package/src/dev/dev.tsx +531 -0
  73. package/src/dev/local.tsx +385 -0
  74. package/src/dev/remote.tsx +413 -0
  75. package/src/dev/use-esbuild.ts +175 -0
  76. package/src/dev/validate-dev-props.ts +31 -0
  77. package/src/dev-registry.tsx +157 -0
  78. package/src/dev.tsx +603 -758
  79. package/src/dialogs.tsx +117 -59
  80. package/src/durable.ts +102 -0
  81. package/src/entry.ts +328 -0
  82. package/src/environment-variables.ts +35 -0
  83. package/src/errors.ts +11 -0
  84. package/src/faye-websocket.d.ts +6 -0
  85. package/src/generate.ts +131 -0
  86. package/src/git-client.ts +42 -0
  87. package/src/global-wrangler-config-path.ts +26 -0
  88. package/src/https-options.ts +127 -0
  89. package/src/index.tsx +2166 -1956
  90. package/src/init.ts +549 -0
  91. package/src/inspect.ts +703 -477
  92. package/src/intl-polyfill.d.ts +139 -0
  93. package/src/is-interactive.ts +12 -0
  94. package/src/kv.ts +430 -0
  95. package/src/logger.ts +73 -0
  96. package/src/metrics/index.ts +5 -0
  97. package/src/metrics/is-ci.ts +14 -0
  98. package/src/metrics/metrics-config.ts +239 -0
  99. package/src/metrics/metrics-dispatcher.ts +96 -0
  100. package/src/metrics/metrics-usage-headers.ts +24 -0
  101. package/src/metrics/send-event.ts +92 -0
  102. package/src/miniflare-cli/README.md +30 -0
  103. package/src/miniflare-cli/assets.ts +543 -0
  104. package/src/miniflare-cli/enum-keys.ts +17 -0
  105. package/src/miniflare-cli/index.ts +85 -0
  106. package/src/miniflare-cli/request-context.ts +40 -0
  107. package/src/module-collection.ts +247 -53
  108. package/src/open-in-browser.ts +17 -0
  109. package/src/package-manager.ts +219 -0
  110. package/src/pages/build.tsx +204 -0
  111. package/src/pages/constants.ts +8 -0
  112. package/src/pages/deployments.tsx +106 -0
  113. package/src/pages/dev.tsx +414 -0
  114. package/src/pages/functions/buildPlugin.ts +105 -0
  115. package/src/pages/functions/buildWorker.ts +151 -0
  116. package/src/pages/functions/filepath-routing.test.ts +234 -0
  117. package/src/pages/functions/filepath-routing.ts +189 -0
  118. package/src/pages/functions/identifiers.ts +78 -0
  119. package/src/pages/functions/routes.ts +151 -0
  120. package/src/pages/index.tsx +84 -0
  121. package/src/pages/projects.tsx +163 -0
  122. package/src/pages/publish.tsx +338 -0
  123. package/src/pages/types.ts +40 -0
  124. package/src/pages/upload.tsx +415 -0
  125. package/src/pages/utils.ts +12 -0
  126. package/src/parse.ts +286 -0
  127. package/src/paths.ts +26 -0
  128. package/src/preview.ts +31 -0
  129. package/src/proxy.ts +434 -252
  130. package/src/publish.ts +794 -345
  131. package/src/pubsub/index.ts +286 -0
  132. package/src/pubsub/pubsub-commands.tsx +623 -0
  133. package/src/r2.ts +118 -0
  134. package/src/selfsigned.d.ts +29 -0
  135. package/src/sites.tsx +357 -99
  136. package/src/tail/filters.ts +279 -0
  137. package/src/tail/index.ts +299 -0
  138. package/src/tail/printing.ts +67 -0
  139. package/src/update-check.ts +19 -0
  140. package/src/user/choose-account.tsx +69 -0
  141. package/src/user/env-vars.ts +46 -0
  142. package/src/user/generate-auth-url.ts +33 -0
  143. package/src/user/generate-random-state.ts +16 -0
  144. package/src/user/index.ts +3 -0
  145. package/src/user/user.tsx +1189 -0
  146. package/src/whoami.tsx +169 -0
  147. package/src/worker-namespace.ts +206 -0
  148. package/src/worker.ts +203 -0
  149. package/src/zones.ts +76 -0
  150. package/templates/checked-fetch.js +17 -0
  151. package/templates/first-party-worker-module-facade.ts +18 -0
  152. package/templates/format-dev-errors.ts +32 -0
  153. package/templates/gitignore +171 -0
  154. package/templates/new-worker-scheduled.js +17 -0
  155. package/templates/new-worker-scheduled.ts +32 -0
  156. package/templates/new-worker.js +15 -0
  157. package/templates/new-worker.ts +30 -0
  158. package/templates/no-op-worker.js +10 -0
  159. package/templates/pages-shim.ts +9 -0
  160. package/templates/pages-template-plugin.ts +155 -0
  161. package/templates/pages-template-worker.ts +161 -0
  162. package/templates/serve-static-assets.ts +63 -0
  163. package/templates/service-bindings-module-facade.js +51 -0
  164. package/templates/service-bindings-sw-facade.js +39 -0
  165. package/templates/tsconfig.json +105 -0
  166. package/wrangler-dist/cli.d.ts +327 -0
  167. package/wrangler-dist/cli.js +134880 -109997
  168. package/pages/functions/buildWorker.ts +0 -62
  169. package/pages/functions/filepath-routing.test.ts +0 -39
  170. package/pages/functions/filepath-routing.ts +0 -221
  171. package/pages/functions/identifiers.ts +0 -78
  172. package/pages/functions/routes.ts +0 -158
  173. package/pages/functions/template-worker.ts +0 -144
  174. package/src/__tests__/mock-cfetch.ts +0 -171
  175. package/src/__tests__/mock-dialogs.ts +0 -65
  176. package/src/__tests__/run-in-tmp.ts +0 -19
  177. package/src/__tests__/run-wrangler.ts +0 -32
  178. package/src/api/form_data.ts +0 -131
  179. package/src/api/preview.ts +0 -128
  180. package/src/api/worker.ts +0 -155
  181. package/src/config.ts +0 -487
  182. package/src/kv.tsx +0 -267
  183. package/src/pages.tsx +0 -1031
  184. package/src/tail.tsx +0 -73
  185. package/src/user.tsx +0 -1025
  186. package/static-asset-facade.js +0 -47
  187. package/vendor/@cloudflare/kv-asset-handler/CHANGELOG.md +0 -332
  188. package/vendor/@cloudflare/kv-asset-handler/LICENSE_APACHE +0 -176
  189. package/vendor/@cloudflare/kv-asset-handler/LICENSE_MIT +0 -25
  190. package/vendor/@cloudflare/kv-asset-handler/README.md +0 -245
  191. package/vendor/@cloudflare/kv-asset-handler/dist/index.d.ts +0 -32
  192. package/vendor/@cloudflare/kv-asset-handler/dist/index.js +0 -354
  193. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.d.ts +0 -13
  194. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.js +0 -148
  195. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.d.ts +0 -1
  196. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.js +0 -436
  197. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.d.ts +0 -1
  198. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.js +0 -40
  199. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.d.ts +0 -1
  200. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.js +0 -42
  201. package/vendor/@cloudflare/kv-asset-handler/dist/types.d.ts +0 -26
  202. package/vendor/@cloudflare/kv-asset-handler/dist/types.js +0 -31
  203. package/vendor/@cloudflare/kv-asset-handler/package.json +0 -52
  204. package/vendor/@cloudflare/kv-asset-handler/src/index.ts +0 -296
  205. package/vendor/@cloudflare/kv-asset-handler/src/mocks.ts +0 -136
  206. package/vendor/@cloudflare/kv-asset-handler/src/test/getAssetFromKV.ts +0 -464
  207. package/vendor/@cloudflare/kv-asset-handler/src/test/mapRequestToAsset.ts +0 -33
  208. package/vendor/@cloudflare/kv-asset-handler/src/test/serveSinglePageApp.ts +0 -42
  209. package/vendor/@cloudflare/kv-asset-handler/src/types.ts +0 -39
  210. package/vendor/wrangler-mime/CHANGELOG.md +0 -289
  211. package/vendor/wrangler-mime/LICENSE +0 -21
  212. package/vendor/wrangler-mime/Mime.js +0 -97
  213. package/vendor/wrangler-mime/README.md +0 -187
  214. package/vendor/wrangler-mime/cli.js +0 -46
  215. package/vendor/wrangler-mime/index.js +0 -4
  216. package/vendor/wrangler-mime/lite.js +0 -4
  217. package/vendor/wrangler-mime/package.json +0 -52
  218. package/vendor/wrangler-mime/types/other.js +0 -1
  219. package/vendor/wrangler-mime/types/standard.js +0 -1
  220. package/wrangler-dist/cli.js.map +0 -7
@@ -0,0 +1,1261 @@
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
+ });