wrangler 0.0.0-e64812e → 0.0.0-e6c9c5c0

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 (338) hide show
  1. package/README.md +23 -3
  2. package/bin/wrangler.js +170 -62
  3. package/kv-asset-handler.js +1 -0
  4. package/miniflare-dist/index.mjs +1499 -21
  5. package/package.json +193 -152
  6. package/src/__tests__/access.test.ts +25 -0
  7. package/src/__tests__/api-dev.test.ts +238 -0
  8. package/src/__tests__/api-devregistry.test.js +64 -0
  9. package/src/__tests__/api.test.ts +80 -0
  10. package/src/__tests__/config-cache-without-cache-dir.test.ts +38 -0
  11. package/src/__tests__/config-cache.test.ts +30 -24
  12. package/src/__tests__/configuration.test.ts +4152 -3363
  13. package/src/__tests__/d1/d1.test.ts +127 -0
  14. package/src/__tests__/d1/splitter.test.ts +255 -0
  15. package/src/__tests__/delete.test.ts +266 -0
  16. package/src/__tests__/deployments.test.ts +105 -0
  17. package/src/__tests__/dev.test.tsx +1558 -952
  18. package/src/__tests__/generate.test.ts +238 -0
  19. package/src/__tests__/get-host-from-url.test.ts +16 -0
  20. package/src/__tests__/guess-worker-format.test.ts +68 -68
  21. package/src/__tests__/helpers/cmd-shim.d.ts +6 -6
  22. package/src/__tests__/helpers/mock-account-id.ts +24 -24
  23. package/src/__tests__/helpers/mock-auth-domain.ts +20 -0
  24. package/src/__tests__/helpers/mock-bin.ts +20 -20
  25. package/src/__tests__/helpers/mock-console.ts +58 -39
  26. package/src/__tests__/helpers/mock-dialogs.ts +88 -79
  27. package/src/__tests__/helpers/mock-get-zone-from-host.ts +11 -0
  28. package/src/__tests__/helpers/mock-http-server.ts +30 -30
  29. package/src/__tests__/helpers/mock-istty.ts +65 -18
  30. package/src/__tests__/helpers/mock-known-routes.ts +12 -0
  31. package/src/__tests__/helpers/mock-kv.ts +41 -28
  32. package/src/__tests__/helpers/mock-oauth-flow.ts +241 -233
  33. package/src/__tests__/helpers/mock-process.ts +34 -0
  34. package/src/__tests__/helpers/mock-stdin.ts +82 -77
  35. package/src/__tests__/helpers/mock-web-socket.ts +21 -21
  36. package/src/__tests__/helpers/msw/blob-worker.cjs +19 -0
  37. package/src/__tests__/helpers/msw/handlers/access.ts +13 -0
  38. package/src/__tests__/helpers/msw/handlers/deployments.ts +69 -0
  39. package/src/__tests__/helpers/msw/handlers/namespaces.ts +81 -0
  40. package/src/__tests__/helpers/msw/handlers/oauth.ts +31 -0
  41. package/src/__tests__/helpers/msw/handlers/r2.ts +60 -0
  42. package/src/__tests__/helpers/msw/handlers/script.ts +56 -0
  43. package/src/__tests__/helpers/msw/handlers/user.ts +52 -0
  44. package/src/__tests__/helpers/msw/handlers/zones.ts +20 -0
  45. package/src/__tests__/helpers/msw/index.ts +50 -0
  46. package/src/__tests__/helpers/msw/read-file-sync.js +61 -0
  47. package/src/__tests__/helpers/run-in-tmp.ts +27 -28
  48. package/src/__tests__/helpers/run-wrangler.ts +8 -8
  49. package/src/__tests__/helpers/worker-scripts/child-wrangler.toml +1 -0
  50. package/src/__tests__/helpers/worker-scripts/hello-world-worker.js +5 -0
  51. package/src/__tests__/helpers/worker-scripts/hello-world-wrangler.toml +1 -0
  52. package/src/__tests__/helpers/worker-scripts/parent-worker.js +8 -0
  53. package/src/__tests__/helpers/worker-scripts/parent-wrangler.toml +5 -0
  54. package/src/__tests__/helpers/write-worker-source.ts +16 -16
  55. package/src/__tests__/helpers/write-wrangler-toml.ts +9 -9
  56. package/src/__tests__/https-options.test.ts +142 -109
  57. package/src/__tests__/index.test.ts +245 -233
  58. package/src/__tests__/init.test.ts +3135 -1605
  59. package/src/__tests__/jest.setup.ts +148 -50
  60. package/src/__tests__/kv.test.ts +1778 -1471
  61. package/src/__tests__/logger.test.ts +100 -74
  62. package/src/__tests__/logout.test.ts +47 -0
  63. package/src/__tests__/metrics.test.ts +493 -0
  64. package/src/__tests__/middleware.scheduled.test.ts +145 -0
  65. package/src/__tests__/middleware.test.ts +750 -0
  66. package/src/__tests__/package-manager.test.ts +303 -303
  67. package/src/__tests__/pages-deployment-tail.test.ts +955 -0
  68. package/src/__tests__/pages.test.ts +2887 -743
  69. package/src/__tests__/parse.test.ts +252 -252
  70. package/src/__tests__/paths.test.ts +39 -0
  71. package/src/__tests__/publish.test.ts +8004 -5355
  72. package/src/__tests__/pubsub.test.ts +490 -0
  73. package/src/__tests__/queues.test.ts +522 -0
  74. package/src/__tests__/r2.test.ts +364 -228
  75. package/src/__tests__/route.test.ts +18 -18
  76. package/src/__tests__/secret.test.ts +681 -469
  77. package/src/__tests__/tail.test.ts +835 -567
  78. package/src/__tests__/test-old-node-version.js +31 -0
  79. package/src/__tests__/tsconfig-sanity.ts +12 -0
  80. package/src/__tests__/tsconfig.json +8 -0
  81. package/src/__tests__/tsconfig.tsbuildinfo +1 -0
  82. package/src/__tests__/type-generation.test.ts +233 -0
  83. package/src/__tests__/user.test.ts +107 -120
  84. package/src/__tests__/utils-collectKeyValues.test.ts +47 -0
  85. package/src/__tests__/validate-dev-props.test.ts +56 -0
  86. package/src/__tests__/version.test.ts +35 -0
  87. package/src/__tests__/whoami.test.tsx +165 -98
  88. package/src/__tests__/worker-namespace.test.ts +334 -0
  89. package/src/abort.d.ts +1 -1
  90. package/src/api/dev.ts +259 -0
  91. package/src/api/index.ts +3 -0
  92. package/src/api/pages/index.ts +5 -0
  93. package/src/api/pages/publish.tsx +321 -0
  94. package/src/bundle-reporter.ts +68 -0
  95. package/src/bundle.ts +838 -163
  96. package/src/cfetch/index.ts +120 -83
  97. package/src/cfetch/internal.ts +210 -97
  98. package/src/cli.ts +23 -8
  99. package/src/config/config.ts +236 -195
  100. package/src/config/diagnostics.ts +61 -61
  101. package/src/config/environment.ts +516 -357
  102. package/src/config/index.ts +326 -193
  103. package/src/config/validation-helpers.ts +379 -357
  104. package/src/config/validation.ts +1979 -1367
  105. package/src/config-cache.ts +81 -41
  106. package/src/create-worker-preview.ts +239 -142
  107. package/src/create-worker-upload-form.ts +309 -238
  108. package/src/d1/backups.tsx +219 -0
  109. package/src/d1/constants.ts +2 -0
  110. package/src/d1/create.tsx +63 -0
  111. package/src/d1/delete.ts +56 -0
  112. package/src/d1/execute.tsx +323 -0
  113. package/src/d1/formatTimeAgo.ts +14 -0
  114. package/src/d1/index.ts +101 -0
  115. package/src/d1/list.tsx +48 -0
  116. package/src/d1/migrations/apply.tsx +197 -0
  117. package/src/d1/migrations/create.tsx +77 -0
  118. package/src/d1/migrations/helpers.ts +147 -0
  119. package/src/d1/migrations/index.ts +3 -0
  120. package/src/d1/migrations/list.tsx +79 -0
  121. package/src/d1/migrations/options.ts +18 -0
  122. package/src/d1/options.ts +22 -0
  123. package/src/d1/splitter.ts +161 -0
  124. package/src/d1/types.ts +24 -0
  125. package/src/d1/utils.ts +48 -0
  126. package/src/delete.ts +103 -0
  127. package/src/deployments.ts +93 -0
  128. package/src/deprecated/index.ts +145 -0
  129. package/src/dev/dev-vars.ts +20 -17
  130. package/src/dev/dev.tsx +547 -352
  131. package/src/dev/get-local-persistence-path.ts +31 -0
  132. package/src/dev/local.tsx +903 -298
  133. package/src/dev/remote.tsx +620 -215
  134. package/src/dev/start-server.ts +538 -0
  135. package/src/dev/use-esbuild.ts +184 -97
  136. package/src/dev/validate-dev-props.ts +31 -0
  137. package/src/dev-registry.ts +201 -0
  138. package/src/dev.tsx +949 -0
  139. package/src/dialogs.ts +136 -0
  140. package/src/dispatch-namespace.ts +207 -0
  141. package/src/docs/index.ts +98 -0
  142. package/src/durable.ts +87 -87
  143. package/src/entry.ts +236 -230
  144. package/src/environment-variables/factory.ts +88 -0
  145. package/src/environment-variables/misc-variables.ts +30 -0
  146. package/src/errors.ts +6 -6
  147. package/src/generate/index.ts +300 -0
  148. package/src/git-client.ts +135 -0
  149. package/src/global-wrangler-config-path.ts +26 -0
  150. package/src/https-options.ts +84 -80
  151. package/src/index.ts +694 -0
  152. package/src/init.ts +1024 -0
  153. package/src/inspect.ts +698 -593
  154. package/src/intl-polyfill.d.ts +123 -123
  155. package/src/is-ci.ts +14 -0
  156. package/src/is-interactive.ts +16 -0
  157. package/src/jest.d.ts +4 -0
  158. package/src/kv/helpers.ts +430 -0
  159. package/src/kv/index.ts +596 -0
  160. package/src/logger.ts +80 -42
  161. package/src/metrics/index.ts +5 -0
  162. package/src/metrics/metrics-config.ts +239 -0
  163. package/src/metrics/metrics-dispatcher.ts +96 -0
  164. package/src/metrics/metrics-usage-headers.ts +24 -0
  165. package/src/metrics/send-event.ts +98 -0
  166. package/src/miniflare-cli/assets.ts +250 -0
  167. package/src/miniflare-cli/index.ts +202 -33
  168. package/src/miniflare-cli/request-context.ts +18 -18
  169. package/src/miniflare-cli/tsconfig.json +9 -0
  170. package/src/miniflare-cli/tsconfig.tsbuildinfo +1 -0
  171. package/src/miniflare-cli/types.ts +11 -0
  172. package/src/module-collection.ts +221 -212
  173. package/src/open-in-browser.ts +4 -6
  174. package/src/package-manager.ts +173 -128
  175. package/src/pages/build.ts +150 -0
  176. package/src/pages/buildFunctions.ts +129 -0
  177. package/src/pages/constants.ts +18 -0
  178. package/src/pages/deployment-tails.ts +284 -0
  179. package/src/pages/deployments.tsx +84 -0
  180. package/src/pages/dev.ts +716 -0
  181. package/src/pages/errors.ts +66 -0
  182. package/src/pages/functions/buildPlugin.ts +90 -0
  183. package/src/pages/functions/buildWorker.ts +288 -0
  184. package/{pages → src/pages}/functions/filepath-routing.test.ts +113 -113
  185. package/src/pages/functions/filepath-routing.ts +189 -0
  186. package/src/pages/functions/identifiers.ts +78 -0
  187. package/src/pages/functions/routes-consolidation.test.ts +250 -0
  188. package/src/pages/functions/routes-consolidation.ts +73 -0
  189. package/src/pages/functions/routes-transformation.test.ts +282 -0
  190. package/src/pages/functions/routes-transformation.ts +115 -0
  191. package/src/pages/functions/routes-validation.test.ts +403 -0
  192. package/src/pages/functions/routes-validation.ts +202 -0
  193. package/src/pages/functions/routes.ts +151 -0
  194. package/src/pages/functions/tsconfig.json +8 -0
  195. package/src/pages/functions/tsconfig.tsbuildinfo +1 -0
  196. package/src/pages/functions.ts +82 -0
  197. package/src/pages/hash.ts +13 -0
  198. package/src/pages/index.ts +102 -0
  199. package/src/pages/projects.tsx +167 -0
  200. package/src/pages/prompt-select-project.tsx +31 -0
  201. package/src/pages/publish.tsx +263 -0
  202. package/src/pages/types.ts +41 -0
  203. package/src/pages/upload.tsx +447 -0
  204. package/src/pages/utils.ts +23 -0
  205. package/src/parse.ts +209 -125
  206. package/src/paths.ts +53 -8
  207. package/src/proxy.ts +623 -394
  208. package/src/publish/index.ts +270 -0
  209. package/src/publish/publish.ts +1064 -0
  210. package/src/pubsub/index.ts +286 -0
  211. package/src/pubsub/pubsub-commands.ts +624 -0
  212. package/src/queues/cli/commands/consumer/add.ts +71 -0
  213. package/src/queues/cli/commands/consumer/index.ts +22 -0
  214. package/src/queues/cli/commands/consumer/remove.ts +38 -0
  215. package/src/queues/cli/commands/create.ts +25 -0
  216. package/src/queues/cli/commands/delete.ts +26 -0
  217. package/src/queues/cli/commands/index.ts +36 -0
  218. package/src/queues/cli/commands/list.ts +25 -0
  219. package/src/queues/client.ts +135 -0
  220. package/src/queues/utils.ts +18 -0
  221. package/src/r2/constants.ts +4 -0
  222. package/src/r2/helpers.ts +131 -0
  223. package/src/r2/index.ts +289 -0
  224. package/src/routes.ts +140 -0
  225. package/src/secret/index.ts +379 -0
  226. package/src/selfsigned.d.ts +23 -23
  227. package/src/sites.ts +372 -0
  228. package/src/tail/createTail.ts +406 -0
  229. package/src/tail/filters.ts +108 -110
  230. package/src/tail/index.ts +210 -296
  231. package/src/tail/printing.ts +101 -46
  232. package/src/tsconfig-sanity.ts +16 -0
  233. package/src/type-generation.ts +181 -0
  234. package/src/update-check.ts +11 -11
  235. package/src/user/access.ts +68 -0
  236. package/src/user/auth-variables.ts +113 -0
  237. package/src/user/choose-account.tsx +39 -0
  238. package/src/user/generate-auth-url.ts +33 -0
  239. package/src/user/generate-random-state.ts +16 -0
  240. package/src/user/index.ts +2 -0
  241. package/src/user/user.ts +1232 -0
  242. package/src/utils/collectKeyValues.ts +14 -0
  243. package/src/whoami.ts +135 -0
  244. package/src/worker.ts +177 -106
  245. package/src/yargs-types.ts +24 -0
  246. package/src/zones.ts +191 -0
  247. package/templates/__tests__/pages-dev-util.test.ts +128 -0
  248. package/templates/__tests__/tsconfig-sanity.ts +12 -0
  249. package/templates/__tests__/tsconfig.json +8 -0
  250. package/templates/__tests__/tsconfig.tsbuildinfo +1 -0
  251. package/templates/checked-fetch.js +15 -0
  252. package/templates/d1-beta-facade.js +241 -0
  253. package/templates/facade.d.ts +14 -0
  254. package/templates/first-party-worker-module-facade.ts +19 -0
  255. package/templates/format-dev-errors.ts +33 -0
  256. package/templates/init-tests/test-jest-new-worker.js +23 -0
  257. package/templates/init-tests/test-vitest-new-worker.js +24 -0
  258. package/templates/init-tests/test-vitest-new-worker.ts +25 -0
  259. package/templates/middleware/common.ts +62 -0
  260. package/templates/middleware/loader-modules.ts +84 -0
  261. package/templates/middleware/loader-sw.ts +233 -0
  262. package/templates/middleware/middleware-miniflare3-json-error.ts +20 -0
  263. package/templates/middleware/middleware-pretty-error.ts +40 -0
  264. package/templates/middleware/middleware-scheduled.ts +15 -0
  265. package/templates/new-worker-scheduled.js +3 -3
  266. package/templates/new-worker-scheduled.ts +15 -15
  267. package/templates/new-worker.js +3 -3
  268. package/templates/new-worker.ts +15 -15
  269. package/templates/no-op-worker.js +10 -0
  270. package/templates/pages-dev-pipeline.ts +31 -0
  271. package/templates/pages-dev-util.ts +52 -0
  272. package/templates/pages-shim.ts +12 -0
  273. package/templates/pages-template-plugin.ts +176 -0
  274. package/templates/pages-template-worker.ts +189 -0
  275. package/templates/serve-static-assets.ts +65 -0
  276. package/templates/service-bindings-module-facade.js +54 -0
  277. package/templates/service-bindings-sw-facade.js +42 -0
  278. package/templates/tsconfig-sanity.ts +11 -0
  279. package/templates/tsconfig.init.json +106 -0
  280. package/templates/tsconfig.json +6 -103
  281. package/templates/tsconfig.tsbuildinfo +1 -0
  282. package/wrangler-dist/cli.d.ts +1028 -0
  283. package/wrangler-dist/cli.js +129259 -110922
  284. package/wrangler-dist/wasm-sync.wasm +0 -0
  285. package/pages/functions/buildPlugin.ts +0 -105
  286. package/pages/functions/buildWorker.ts +0 -151
  287. package/pages/functions/filepath-routing.ts +0 -189
  288. package/pages/functions/identifiers.ts +0 -78
  289. package/pages/functions/routes.ts +0 -156
  290. package/pages/functions/template-plugin.ts +0 -147
  291. package/pages/functions/template-worker.ts +0 -143
  292. package/src/__tests__/helpers/faye-websocket.d.ts +0 -6
  293. package/src/__tests__/helpers/mock-cfetch.ts +0 -211
  294. package/src/dialogs.tsx +0 -135
  295. package/src/environment-variables.ts +0 -35
  296. package/src/index.tsx +0 -2864
  297. package/src/kv.ts +0 -408
  298. package/src/miniflare-cli/enum-keys.ts +0 -17
  299. package/src/pages.tsx +0 -2038
  300. package/src/publish.ts +0 -583
  301. package/src/r2.ts +0 -50
  302. package/src/sites.tsx +0 -295
  303. package/src/user.tsx +0 -1203
  304. package/src/whoami.tsx +0 -70
  305. package/templates/static-asset-facade.js +0 -47
  306. package/vendor/@cloudflare/kv-asset-handler/CHANGELOG.md +0 -332
  307. package/vendor/@cloudflare/kv-asset-handler/LICENSE_APACHE +0 -176
  308. package/vendor/@cloudflare/kv-asset-handler/LICENSE_MIT +0 -25
  309. package/vendor/@cloudflare/kv-asset-handler/README.md +0 -245
  310. package/vendor/@cloudflare/kv-asset-handler/dist/index.d.ts +0 -32
  311. package/vendor/@cloudflare/kv-asset-handler/dist/index.js +0 -354
  312. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.d.ts +0 -13
  313. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.js +0 -148
  314. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.d.ts +0 -1
  315. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.js +0 -436
  316. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.d.ts +0 -1
  317. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.js +0 -40
  318. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.d.ts +0 -1
  319. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.js +0 -42
  320. package/vendor/@cloudflare/kv-asset-handler/dist/types.d.ts +0 -26
  321. package/vendor/@cloudflare/kv-asset-handler/dist/types.js +0 -31
  322. package/vendor/@cloudflare/kv-asset-handler/package.json +0 -52
  323. package/vendor/@cloudflare/kv-asset-handler/src/index.ts +0 -296
  324. package/vendor/@cloudflare/kv-asset-handler/src/mocks.ts +0 -136
  325. package/vendor/@cloudflare/kv-asset-handler/src/test/getAssetFromKV.ts +0 -464
  326. package/vendor/@cloudflare/kv-asset-handler/src/test/mapRequestToAsset.ts +0 -33
  327. package/vendor/@cloudflare/kv-asset-handler/src/test/serveSinglePageApp.ts +0 -42
  328. package/vendor/@cloudflare/kv-asset-handler/src/types.ts +0 -39
  329. package/vendor/wrangler-mime/CHANGELOG.md +0 -289
  330. package/vendor/wrangler-mime/LICENSE +0 -21
  331. package/vendor/wrangler-mime/Mime.js +0 -97
  332. package/vendor/wrangler-mime/README.md +0 -187
  333. package/vendor/wrangler-mime/cli.js +0 -46
  334. package/vendor/wrangler-mime/index.js +0 -4
  335. package/vendor/wrangler-mime/lite.js +0 -4
  336. package/vendor/wrangler-mime/package.json +0 -52
  337. package/vendor/wrangler-mime/types/other.js +0 -1
  338. package/vendor/wrangler-mime/types/standard.js +0 -1
@@ -1,754 +1,2898 @@
1
+ /* eslint-disable no-shadow */
2
+ import { Blob } from "node:buffer";
1
3
  import { mkdirSync, writeFileSync } from "node:fs";
4
+ import { chdir } from "node:process";
5
+ import { MockedRequest, rest } from "msw";
6
+ import { FormData } from "undici";
7
+ import { ROUTES_SPEC_VERSION } from "../pages/constants";
8
+ import { isRoutesJSONSpec } from "../pages/functions/routes-validation";
9
+ import { version } from "./../../package.json";
2
10
  import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
3
- import {
4
- createFetchResult,
5
- setMockRawResponse,
6
- setMockResponse,
7
- unsetAllMocks,
8
- } from "./helpers/mock-cfetch";
9
11
  import { mockConsoleMethods } from "./helpers/mock-console";
12
+ import { msw } from "./helpers/msw";
13
+ import { FileReaderSync } from "./helpers/msw/read-file-sync";
10
14
  import { runInTempDir } from "./helpers/run-in-tmp";
11
15
  import { runWrangler } from "./helpers/run-wrangler";
12
- import type { Deployment, Project, UploadPayloadFile } from "../pages";
13
- import type { FormData, RequestInit } from "undici";
14
-
15
- // Asserting within mock responses get swallowed, so run them out-of-band
16
- const outOfBandTests: (() => void)[] = [];
17
- function assertLater(fn: () => void) {
18
- outOfBandTests.push(fn);
19
- }
16
+ import type { Deployment, Project, UploadPayloadFile } from "../pages/types";
17
+ import type { RestRequest } from "msw";
20
18
 
21
19
  function mockGetToken(jwt: string) {
22
- return setMockResponse(
23
- "/accounts/:accountId/pages/projects/foo/upload-token",
24
- async ([_url, accountId]) => {
25
- assertLater(() => {
26
- expect(accountId).toEqual("some-account-id");
27
- });
28
-
29
- return { jwt };
30
- }
31
- );
20
+ msw.use(
21
+ rest.get(
22
+ "*/accounts/:accountId/pages/projects/foo/upload-token",
23
+ (req, res, ctx) => {
24
+ expect(req.params.accountId).toEqual("some-account-id");
25
+
26
+ return res(
27
+ ctx.status(200),
28
+ ctx.json({ success: true, errors: [], messages: [], result: { jwt } })
29
+ );
30
+ }
31
+ )
32
+ );
32
33
  }
33
34
 
34
35
  describe("pages", () => {
35
- runInTempDir();
36
- const std = mockConsoleMethods();
37
- function endEventLoop() {
38
- return new Promise((resolve) => setImmediate(resolve));
39
- }
40
- beforeEach(() => {
41
- outOfBandTests.length = 0;
42
- });
43
- afterEach(() => {
44
- outOfBandTests.forEach((fn) => fn());
45
- });
46
-
47
- it("should should display a list of available subcommands, for pages with no subcommand", async () => {
48
- await runWrangler("pages");
49
- await endEventLoop();
50
-
51
- expect(std.out).toMatchInlineSnapshot(`
52
- "wrangler pages
53
-
54
- ⚡️ Configure Cloudflare Pages
55
-
56
- Commands:
57
- wrangler pages dev [directory] [-- command..] 🧑‍💻 Develop your full-stack Pages application locally
58
- wrangler pages project ⚡️ Interact with your Pages projects
59
- wrangler pages deployment 🚀 Interact with the deployments of a project
60
- wrangler pages publish [directory] 🆙 Publish a directory of static assets as a Pages deployment
61
-
62
- Flags:
63
- -c, --config Path to .toml configuration file [string]
64
- -h, --help Show help [boolean]
65
- -v, --version Show version number [boolean]
66
-
67
- 🚧 'wrangler pages <command>' is a beta command. Please report any issues to https://github.com/cloudflare/wrangler2/issues/new/choose"
68
- `);
69
- });
70
-
71
- describe("beta message for subcommands", () => {
72
- it("should display for pages:dev", async () => {
73
- await expect(
74
- runWrangler("pages dev")
75
- ).rejects.toThrowErrorMatchingInlineSnapshot(
76
- `"Must specify a directory of static assets to serve or a command to run."`
77
- );
78
-
79
- expect(std.out).toMatchInlineSnapshot(`
80
- "🚧 'wrangler pages <command>' is a beta command. Please report any issues to https://github.com/cloudflare/wrangler2/issues/new/choose
81
-
82
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choose"
83
- `);
84
- });
85
-
86
- it("should display for pages:functions:build", async () => {
87
- await expect(runWrangler("pages functions build")).rejects.toThrowError();
88
-
89
- expect(std.out).toMatchInlineSnapshot(`
90
- "🚧 'wrangler pages <command>' is a beta command. Please report any issues to https://github.com/cloudflare/wrangler2/issues/new/choose
91
-
92
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choose"
93
- `);
94
- });
95
- });
96
-
97
- describe("project list", () => {
98
- mockAccountId();
99
- mockApiToken();
100
-
101
- afterEach(() => {
102
- unsetAllMocks();
103
- });
104
-
105
- function mockListRequest(projects: unknown[]) {
106
- const requests = { count: 0 };
107
- setMockResponse(
108
- "/accounts/:accountId/pages/projects",
109
- ([_url, accountId], init, query) => {
110
- requests.count++;
111
- const pageSize = Number(query.get("per_page"));
112
- const page = Number(query.get("page"));
113
- const expectedPageSize = 10;
114
- const expectedPage = requests.count;
115
- assertLater(() => {
116
- expect(accountId).toEqual("some-account-id");
117
- expect(pageSize).toEqual(expectedPageSize);
118
- expect(page).toEqual(expectedPage);
119
- expect(init).toEqual({});
120
- });
121
- return projects.slice((page - 1) * pageSize, page * pageSize);
122
- }
123
- );
124
- return requests;
125
- }
126
-
127
- it("should make request to list projects", async () => {
128
- const projects: Project[] = [
129
- {
130
- name: "dogs",
131
- subdomain: "docs.pages.dev",
132
- domains: ["dogs.pages.dev"],
133
- source: {
134
- type: "github",
135
- },
136
- latest_deployment: {
137
- modified_on: "2021-11-17T14:52:26.133835Z",
138
- },
139
- created_on: "2021-11-17T14:52:26.133835Z",
140
- production_branch: "main",
141
- },
142
- {
143
- name: "cats",
144
- subdomain: "cats.pages.dev",
145
- domains: ["cats.pages.dev", "kitten.com"],
146
- latest_deployment: {
147
- modified_on: "2021-11-17T14:52:26.133835Z",
148
- },
149
- created_on: "2021-11-17T14:52:26.133835Z",
150
- production_branch: "main",
151
- },
152
- ];
153
-
154
- const requests = mockListRequest(projects);
155
- await runWrangler("pages project list");
156
-
157
- expect(requests.count).toBe(1);
158
- });
159
-
160
- it("should make multiple requests for paginated results", async () => {
161
- const projects: Project[] = [];
162
- for (let i = 0; i < 15; i++) {
163
- projects.push({
164
- name: "dogs" + i,
165
- subdomain: i + "dogs.pages.dev",
166
- domains: [i + "dogs.pages.dev"],
167
- source: {
168
- type: "github",
169
- },
170
- latest_deployment: {
171
- modified_on: "2021-11-17T14:52:26.133835Z",
172
- },
173
- created_on: "2021-11-17T14:52:26.133835Z",
174
- production_branch: "main",
175
- });
176
- }
177
- const requests = mockListRequest(projects);
178
- await runWrangler("pages project list");
179
- expect(requests.count).toEqual(2);
180
- });
181
- });
182
-
183
- describe("project create", () => {
184
- mockAccountId();
185
- mockApiToken();
186
-
187
- afterEach(() => {
188
- unsetAllMocks();
189
- });
190
-
191
- it("should create a project with a production branch", async () => {
192
- setMockResponse(
193
- "/accounts/:accountId/pages/projects",
194
- ([_url, accountId], init) => {
195
- const body = JSON.parse(init.body as string);
196
- assertLater(() => {
197
- expect(accountId).toEqual("some-account-id");
198
- expect(init.method).toEqual("POST");
199
- expect(body).toEqual({
200
- name: "a-new-project",
201
- production_branch: "main",
202
- });
203
- });
204
- return {
205
- name: "a-new-project",
206
- subdomain: "a-new-project.pages.dev",
207
- production_branch: "main",
208
- };
209
- }
210
- );
211
- await runWrangler(
212
- "pages project create a-new-project --production-branch=main"
213
- );
214
- expect(std.out).toMatchInlineSnapshot(`
215
- "✨ Successfully created the 'a-new-project' project. It will be available at https://a-new-project.pages.dev/ once you create your first deployment.
216
- To deploy a folder of assets, run 'wrangler pages publish [directory]'."
217
- `);
218
- });
219
- });
220
-
221
- describe("deployment list", () => {
222
- mockAccountId();
223
- mockApiToken();
224
-
225
- afterEach(() => {
226
- unsetAllMocks();
227
- });
228
- function mockListRequest(deployments: unknown[]) {
229
- const requests = { count: 0 };
230
- setMockResponse(
231
- "/accounts/:accountId/pages/projects/:project/deployments",
232
- ([_url, accountId, project]) => {
233
- requests.count++;
234
- assertLater(() => {
235
- expect(project).toEqual("images");
236
- expect(accountId).toEqual("some-account-id");
237
- });
238
- return deployments;
239
- }
240
- );
241
- return requests;
242
- }
243
-
244
- it("should make request to list deployments", async () => {
245
- const deployments: Deployment[] = [
246
- {
247
- id: "87bbc8fe-16be-45cd-81e0-63d722e82cdf",
248
- url: "https://87bbc8fe.images.pages.dev",
249
- environment: "preview",
250
- latest_stage: {
251
- ended_on: "2021-11-17T14:52:26.133835Z",
252
- status: "success",
253
- },
254
- deployment_trigger: {
255
- metadata: {
256
- branch: "main",
257
- commit_hash: "c7649364c4cb32ad4f65b530b9424e8be5bec9d6",
258
- },
259
- },
260
- project_name: "images",
261
- },
262
- ];
263
-
264
- const requests = mockListRequest(deployments);
265
- await runWrangler("pages deployment list --project-name=images");
266
-
267
- expect(requests.count).toBe(1);
268
- });
269
- });
270
-
271
- describe("deployment create", () => {
272
- let actualProcessEnvCI: string | undefined;
273
-
274
- mockAccountId();
275
- mockApiToken();
276
- runInTempDir();
277
-
278
- beforeEach(() => {
279
- actualProcessEnvCI = process.env.CI;
280
- process.env.CI = "true";
281
- });
282
-
283
- afterEach(() => {
284
- unsetAllMocks();
285
- process.env.CI = actualProcessEnvCI;
286
- });
287
-
288
- it("should be aliased with 'wrangler pages publish'", async () => {
289
- await runWrangler("pages publish --help");
290
- await endEventLoop();
291
-
292
- expect(std.out).toMatchInlineSnapshot(`
293
- "wrangler pages publish [directory]
294
-
295
- 🆙 Publish a directory of static assets as a Pages deployment
296
-
297
- Positionals:
298
- directory The directory of static files to upload [string]
299
-
300
- Flags:
301
- -c, --config Path to .toml configuration file [string]
302
- -h, --help Show help [boolean]
303
- -v, --version Show version number [boolean]
304
-
305
- Options:
306
- --project-name The name of the project you want to deploy to [string]
307
- --branch The name of the branch you want to deploy to [string]
308
- --commit-hash The SHA to attach to this deployment [string]
309
- --commit-message The commit message to attach to this deployment [string]
310
- --commit-dirty Whether or not the workspace should be considered dirty for this deployment [boolean]
311
-
312
- 🚧 'wrangler pages <command>' is a beta command. Please report any issues to https://github.com/cloudflare/wrangler2/issues/new/choose"
313
- `);
314
- });
315
-
316
- it("should upload a directory of files", async () => {
317
- writeFileSync("logo.png", "foobar");
318
-
319
- setMockResponse(
320
- "/accounts/:accountId/pages/projects/foo/upload-token",
321
- async ([_url, accountId]) => {
322
- assertLater(() => {
323
- expect(accountId).toEqual("some-account-id");
324
- });
325
-
326
- return {
327
- jwt: "<<funfetti-auth-jwt>>",
328
- };
329
- }
330
- );
331
-
332
- setMockResponse(
333
- "/pages/assets/check-missing",
334
- "POST",
335
- async (_, init) => {
336
- const body = JSON.parse(init.body as string) as { hashes: string[] };
337
- assertLater(() => {
338
- expect(init.headers).toMatchObject({
339
- Authorization: "Bearer <<funfetti-auth-jwt>>",
340
- });
341
- expect(body).toMatchObject({
342
- hashes: ["2082190357cfd3617ccfe04f340c6247"],
343
- });
344
- });
345
- return body.hashes;
346
- }
347
- );
348
-
349
- setMockResponse("/pages/assets/upload", "POST", async (_, init) => {
350
- assertLater(() => {
351
- expect(init.headers).toMatchObject({
352
- Authorization: "Bearer <<funfetti-auth-jwt>>",
353
- });
354
- const body = JSON.parse(init.body as string) as UploadPayloadFile[];
355
- expect(body).toMatchObject([
356
- {
357
- key: "2082190357cfd3617ccfe04f340c6247",
358
- value: Buffer.from("foobar").toString("base64"),
359
- metadata: {
360
- contentType: "image/png",
361
- },
362
- base64: true,
363
- },
364
- ]);
365
- });
366
- });
367
-
368
- setMockResponse(
369
- "/accounts/:accountId/pages/projects/foo/deployments",
370
- async ([_url, accountId], init) => {
371
- assertLater(() => {
372
- expect(accountId).toEqual("some-account-id");
373
- expect(init.method).toEqual("POST");
374
- const body = init.body as FormData;
375
- const manifest = JSON.parse(body.get("manifest") as string);
376
- expect(manifest).toMatchInlineSnapshot(`
377
- Object {
378
- "/logo.png": "2082190357cfd3617ccfe04f340c6247",
379
- }
380
- `);
381
- });
382
-
383
- return {
384
- url: "https://abcxyz.foo.pages.dev/",
385
- };
386
- }
387
- );
388
-
389
- await runWrangler("pages publish . --project-name=foo");
390
-
391
- // TODO: Unmounting somehow loses this output
392
-
393
- // expect(std.out).toMatchInlineSnapshot(`
394
- // "✨ Success! Uploaded 1 files (TIMINGS)
395
-
396
- // ✨ Deployment complete! Take a peek over at https://abcxyz.foo.pages.dev/"
397
- // `);
398
- });
399
-
400
- it("should retry uploads", async () => {
401
- writeFileSync("logo.txt", "foobar");
402
-
403
- mockGetToken("<<funfetti-auth-jwt>>");
404
-
405
- setMockResponse(
406
- "/pages/assets/check-missing",
407
- "POST",
408
- async (_, init) => {
409
- const body = JSON.parse(init.body as string) as { hashes: string[] };
410
- assertLater(() => {
411
- expect(init.headers).toMatchObject({
412
- Authorization: "Bearer <<funfetti-auth-jwt>>",
413
- });
414
- expect(body).toMatchObject({
415
- hashes: ["1a98fb08af91aca4a7df1764a2c4ddb0"],
416
- });
417
- });
418
- return body.hashes;
419
- }
420
- );
421
-
422
- // Accumulate multiple requests then assert afterwards
423
- const requests: RequestInit[] = [];
424
- setMockRawResponse("/pages/assets/upload", "POST", async (_, init) => {
425
- requests.push(init);
426
-
427
- if (requests.length < 2) {
428
- return createFetchResult(null, false, [
429
- {
430
- code: 800000,
431
- message: "Something exploded, please retry",
432
- },
433
- ]);
434
- } else {
435
- return createFetchResult(null, true);
436
- }
437
- });
438
-
439
- setMockResponse(
440
- "/accounts/:accountId/pages/projects/foo/deployments",
441
- async ([_url, accountId], init) => {
442
- assertLater(() => {
443
- expect(accountId).toEqual("some-account-id");
444
- expect(init.method).toEqual("POST");
445
- const body = init.body as FormData;
446
- const manifest = JSON.parse(body.get("manifest") as string);
447
- expect(manifest).toMatchInlineSnapshot(`
448
- Object {
449
- "/logo.txt": "1a98fb08af91aca4a7df1764a2c4ddb0",
450
- }
451
- `);
452
- });
453
-
454
- return {
455
- url: "https://abcxyz.foo.pages.dev/",
456
- };
457
- }
458
- );
459
-
460
- await runWrangler("pages publish . --project-name=foo");
461
-
462
- // Assert two identical requests
463
- expect(requests.length).toBe(2);
464
- for (const init of requests) {
465
- assertLater(() => {
466
- expect(init.headers).toMatchObject({
467
- Authorization: "Bearer <<funfetti-auth-jwt>>",
468
- });
469
-
470
- const body = JSON.parse(init.body as string) as UploadPayloadFile[];
471
- expect(body).toMatchObject([
472
- {
473
- key: "1a98fb08af91aca4a7df1764a2c4ddb0",
474
- value: Buffer.from("foobar").toString("base64"),
475
- metadata: {
476
- contentType: "text/plain",
477
- },
478
- base64: true,
479
- },
480
- ]);
481
- });
482
- }
483
-
484
- expect(std.out).toMatchInlineSnapshot(`
485
- "✨ Success! Uploaded 1 files (TIMINGS)
486
-
487
- ✨ Deployment complete! Take a peek over at https://abcxyz.foo.pages.dev/"
488
- `);
489
- });
490
-
491
- it("should refetch a JWT if it expires while uploading", async () => {
492
- writeFileSync("logo.txt", "foobar");
493
-
494
- const cancelMockGetToken = mockGetToken("<<funfetti-auth-jwt>>");
495
-
496
- setMockResponse(
497
- "/pages/assets/check-missing",
498
- "POST",
499
- async (_, init) => {
500
- const body = JSON.parse(init.body as string) as { hashes: string[] };
501
- assertLater(() => {
502
- expect(init.headers).toMatchObject({
503
- Authorization: "Bearer <<funfetti-auth-jwt>>",
504
- });
505
- expect(body).toMatchObject({
506
- hashes: ["1a98fb08af91aca4a7df1764a2c4ddb0"],
507
- });
508
- });
509
- return body.hashes;
510
- }
511
- );
512
-
513
- // Accumulate multiple requests then assert afterwards
514
- const requests: RequestInit[] = [];
515
- setMockRawResponse("/pages/assets/upload", "POST", async (_, init) => {
516
- requests.push(init);
517
-
518
- // Fail just the first request
519
- if (requests.length < 2) {
520
- cancelMockGetToken();
521
- mockGetToken("<<funfetti-auth-jwt2>>");
522
- return createFetchResult(null, false, [
523
- {
524
- code: 8000013,
525
- message: "Authorization failed",
526
- },
527
- ]);
528
- } else {
529
- return createFetchResult(null, true);
530
- }
531
- });
532
-
533
- setMockResponse(
534
- "/accounts/:accountId/pages/projects/foo/deployments",
535
- async ([_url, accountId], init) => {
536
- assertLater(() => {
537
- expect(accountId).toEqual("some-account-id");
538
- expect(init.method).toEqual("POST");
539
- const body = init.body as FormData;
540
- const manifest = JSON.parse(body.get("manifest") as string);
541
- expect(manifest).toMatchInlineSnapshot(`
542
- Object {
543
- "/logo.txt": "1a98fb08af91aca4a7df1764a2c4ddb0",
544
- }
545
- `);
546
- });
547
-
548
- return {
549
- url: "https://abcxyz.foo.pages.dev/",
550
- };
551
- }
552
- );
553
-
554
- await runWrangler("pages publish . --project-name=foo");
555
-
556
- // Assert two requests
557
- expect(requests.length).toBe(2);
558
-
559
- expect(requests[0].headers).toMatchObject({
560
- Authorization: "Bearer <<funfetti-auth-jwt>>",
561
- });
562
-
563
- expect(requests[1].headers).toMatchObject({
564
- Authorization: "Bearer <<funfetti-auth-jwt2>>",
565
- });
566
-
567
- for (const init of requests) {
568
- const body = JSON.parse(init.body as string) as UploadPayloadFile[];
569
- expect(body).toMatchObject([
570
- {
571
- key: "1a98fb08af91aca4a7df1764a2c4ddb0",
572
- value: Buffer.from("foobar").toString("base64"),
573
- metadata: {
574
- contentType: "text/plain",
575
- },
576
- base64: true,
577
- },
578
- ]);
579
- }
580
-
581
- expect(std.out).toMatchInlineSnapshot(`
582
- "✨ Success! Uploaded 1 files (TIMINGS)
583
-
584
- ✨ Deployment complete! Take a peek over at https://abcxyz.foo.pages.dev/"
585
- `);
586
- });
587
-
588
- it("should try to use multiple buckets (up to the max concurrency)", async () => {
589
- writeFileSync("logo.txt", "foobar");
590
- writeFileSync("logo.png", "foobar");
591
- writeFileSync("logo.html", "foobar");
592
- writeFileSync("logo.js", "foobar");
593
-
594
- mockGetToken("<<funfetti-auth-jwt>>");
595
-
596
- setMockResponse(
597
- "/pages/assets/check-missing",
598
- "POST",
599
- async (_, init) => {
600
- const body = JSON.parse(init.body as string) as { hashes: string[] };
601
- assertLater(() => {
602
- expect(init.headers).toMatchObject({
603
- Authorization: "Bearer <<funfetti-auth-jwt>>",
604
- });
605
- expect(body).toMatchObject({
606
- hashes: expect.arrayContaining([
607
- "d96fef225537c9f5e44a3cb27fd0b492",
608
- "2082190357cfd3617ccfe04f340c6247",
609
- "6be321bef99e758250dac034474ddbb8",
610
- "1a98fb08af91aca4a7df1764a2c4ddb0",
611
- ]),
612
- });
613
- });
614
- return body.hashes;
615
- }
616
- );
617
-
618
- // Accumulate multiple requests then assert afterwards
619
- const requests: RequestInit[] = [];
620
- setMockResponse("/pages/assets/upload", "POST", async (_, init) => {
621
- requests.push(init);
622
- });
623
-
624
- setMockResponse(
625
- "/accounts/:accountId/pages/projects/foo/deployments",
626
- async ([_url, accountId], init) => {
627
- assertLater(() => {
628
- expect(accountId).toEqual("some-account-id");
629
- expect(init.method).toEqual("POST");
630
- const body = init.body as FormData;
631
- const manifest = JSON.parse(body.get("manifest") as string);
632
- expect(manifest).toMatchInlineSnapshot(`
633
- Object {
634
- "/logo.html": "d96fef225537c9f5e44a3cb27fd0b492",
635
- "/logo.js": "6be321bef99e758250dac034474ddbb8",
636
- "/logo.png": "2082190357cfd3617ccfe04f340c6247",
637
- "/logo.txt": "1a98fb08af91aca4a7df1764a2c4ddb0",
638
- }
639
- `);
640
- });
641
-
642
- return {
643
- url: "https://abcxyz.foo.pages.dev/",
644
- };
645
- }
646
- );
647
-
648
- await runWrangler("pages publish . --project-name=foo");
649
-
650
- // We have 3 buckets, so expect 3 uploads
651
- expect(requests.length).toBe(3);
652
- const bodies: UploadPayloadFile[][] = [];
653
- for (const init of requests) {
654
- expect(init.headers).toMatchObject({
655
- Authorization: "Bearer <<funfetti-auth-jwt>>",
656
- });
657
- bodies.push(JSON.parse(init.body as string) as UploadPayloadFile[]);
658
- }
659
- // First bucket should end up with 2 files
660
- expect(bodies.map((b) => b.length)).toEqual([2, 1, 1]);
661
- // But we don't know the order, so flatten and test without ordering
662
- expect(bodies.flatMap((b) => b)).toEqual(
663
- expect.arrayContaining([
664
- {
665
- base64: true,
666
- key: "d96fef225537c9f5e44a3cb27fd0b492",
667
- metadata: { contentType: "text/html" },
668
- value: "Zm9vYmFy",
669
- },
670
- {
671
- base64: true,
672
- key: "1a98fb08af91aca4a7df1764a2c4ddb0",
673
- metadata: { contentType: "text/plain" },
674
- value: "Zm9vYmFy",
675
- },
676
- {
677
- base64: true,
678
- key: "6be321bef99e758250dac034474ddbb8",
679
- metadata: { contentType: "application/javascript" },
680
- value: "Zm9vYmFy",
681
- },
682
- {
683
- base64: true,
684
- key: "2082190357cfd3617ccfe04f340c6247",
685
- metadata: { contentType: "image/png" },
686
- value: "Zm9vYmFy",
687
- },
688
- ])
689
- );
690
-
691
- expect(std.out).toMatchInlineSnapshot(`
692
- "✨ Success! Uploaded 4 files (TIMINGS)
693
-
694
- ✨ Deployment complete! Take a peek over at https://abcxyz.foo.pages.dev/"
695
- `);
696
- });
697
-
698
- it("should not error when directory names contain periods and houses a extensionless file", async () => {
699
- mkdirSync(".well-known");
700
- // Note: same content as previous test, but since it's a different extension,
701
- // it hashes to a different value
702
- writeFileSync(".well-known/foobar", "foobar");
703
-
704
- mockGetToken("<<funfetti-auth-jwt>>");
705
-
706
- setMockResponse(
707
- "/pages/assets/check-missing",
708
- "POST",
709
- async (_, init) => {
710
- const body = JSON.parse(init.body as string) as { hashes: string[] };
711
- assertLater(() => {
712
- expect(init.headers).toMatchObject({
713
- Authorization: "Bearer <<funfetti-auth-jwt>>",
714
- });
715
- expect(body).toMatchObject({
716
- hashes: ["7b764dacfd211bebd8077828a7ddefd7"],
717
- });
718
- });
719
- return body.hashes;
720
- }
721
- );
722
-
723
- setMockResponse("/pages/assets/upload", "POST", async (_, init) => {
724
- assertLater(() => {
725
- expect(init.headers).toMatchObject({
726
- Authorization: "Bearer <<funfetti-auth-jwt>>",
727
- });
728
- const body = JSON.parse(init.body as string) as UploadPayloadFile[];
729
- expect(body).toMatchObject([
730
- {
731
- key: "7b764dacfd211bebd8077828a7ddefd7",
732
- value: Buffer.from("foobar").toString("base64"),
733
- metadata: {
734
- contentType: "application/octet-stream",
735
- },
736
- base64: true,
737
- },
738
- ]);
739
- });
740
- });
741
-
742
- setMockResponse(
743
- "/accounts/:accountId/pages/projects/foo/deployments",
744
- async () => ({
745
- url: "https://abcxyz.foo.pages.dev/",
746
- })
747
- );
748
-
749
- await runWrangler("pages publish . --project-name=foo");
750
-
751
- expect(std.err).toMatchInlineSnapshot(`""`);
752
- });
753
- });
36
+ runInTempDir();
37
+ const std = mockConsoleMethods();
38
+ function endEventLoop() {
39
+ return new Promise((resolve) => setImmediate(resolve));
40
+ }
41
+
42
+ beforeEach(() => {
43
+ // @ts-expect-error we're using a very simple setTimeout mock here
44
+ jest.spyOn(global, "setTimeout").mockImplementation((fn, _period) => {
45
+ setImmediate(fn);
46
+ });
47
+ });
48
+ afterEach(async () => {
49
+ // Force a tick to ensure that all promises resolve
50
+ await endEventLoop();
51
+ // Reset MSW after tick to ensure that all requests have been handled
52
+ msw.resetHandlers();
53
+ msw.restoreHandlers();
54
+ });
55
+
56
+ it("should should display a list of available subcommands, for pages with no subcommand", async () => {
57
+ await runWrangler("pages");
58
+ await endEventLoop();
59
+
60
+ expect(std.out).toMatchInlineSnapshot(`
61
+ "wrangler pages
62
+
63
+ ⚡️ Configure Cloudflare Pages
64
+
65
+ Commands:
66
+ wrangler pages dev [directory] [-- command..] 🧑‍💻 Develop your full-stack Pages application locally
67
+ wrangler pages project ⚡️ Interact with your Pages projects
68
+ wrangler pages deployment 🚀 Interact with the deployments of a project
69
+ wrangler pages publish [directory] 🆙 Publish a directory of static assets as a Pages deployment
70
+
71
+ Flags:
72
+ -c, --config Path to .toml configuration file [string]
73
+ -e, --env Environment to use for operations and .env files [string]
74
+ -h, --help Show help [boolean]
75
+ -v, --version Show version number [boolean]
76
+
77
+ 🚧 'wrangler pages <command>' is a beta command. Please report any issues to https://github.com/cloudflare/wrangler2/issues/new/choose"
78
+ `);
79
+ });
80
+
81
+ describe("beta message for subcommands", () => {
82
+ it("should display for pages:dev", async () => {
83
+ await expect(
84
+ runWrangler("pages dev")
85
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
86
+ `"Must specify a directory of static assets to serve or a command to run or a proxy port."`
87
+ );
88
+
89
+ expect(std.out).toMatchInlineSnapshot(`
90
+ "🚧 'wrangler pages <command>' is a beta command. Please report any issues to https://github.com/cloudflare/wrangler2/issues/new/choose
91
+
92
+ If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choose"
93
+ `);
94
+ });
95
+
96
+ it("should display for pages:functions:build", async () => {
97
+ await expect(runWrangler("pages functions build")).rejects.toThrowError();
98
+
99
+ expect(std.out).toMatchInlineSnapshot(`
100
+ "🚧 'wrangler pages <command>' is a beta command. Please report any issues to https://github.com/cloudflare/wrangler2/issues/new/choose
101
+
102
+ If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choose"
103
+ `);
104
+ });
105
+
106
+ it("should display for pages:functions:optimize-routes", async () => {
107
+ await expect(
108
+ runWrangler(
109
+ 'pages functions optimize-routes --routes-path="/build/_routes.json" --output-routes-path="/build/_optimized-routes.json"'
110
+ )
111
+ ).rejects.toThrowError();
112
+
113
+ expect(std.out).toMatchInlineSnapshot(`
114
+ "🚧 'wrangler pages <command>' is a beta command. Please report any issues to https://github.com/cloudflare/wrangler2/issues/new/choose
115
+
116
+ If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choose"
117
+ `);
118
+ });
119
+ });
120
+
121
+ describe("project list", () => {
122
+ mockAccountId();
123
+ mockApiToken();
124
+
125
+ function mockListRequest(projects: unknown[]) {
126
+ const requests = { count: 0 };
127
+ msw.use(
128
+ rest.get(
129
+ "*/accounts/:accountId/pages/projects",
130
+ async (req, res, ctx) => {
131
+ requests.count++;
132
+ const pageSize = Number(req.url.searchParams.get("per_page"));
133
+ const page = Number(req.url.searchParams.get("page"));
134
+ const expectedPageSize = 10;
135
+ const expectedPage = requests.count;
136
+ expect(req.params.accountId).toEqual("some-account-id");
137
+ expect(pageSize).toEqual(expectedPageSize);
138
+ expect(page).toEqual(expectedPage);
139
+ expect(await req.text()).toEqual("");
140
+
141
+ return res(
142
+ ctx.status(200),
143
+ ctx.json({
144
+ success: true,
145
+ errors: [],
146
+ messages: [],
147
+ result: projects.slice((page - 1) * pageSize, page * pageSize),
148
+ })
149
+ );
150
+ }
151
+ )
152
+ );
153
+ return requests;
154
+ }
155
+
156
+ it("should make request to list projects", async () => {
157
+ const projects: Project[] = [
158
+ {
159
+ name: "dogs",
160
+ subdomain: "docs.pages.dev",
161
+ domains: ["dogs.pages.dev"],
162
+ source: {
163
+ type: "github",
164
+ },
165
+ latest_deployment: {
166
+ modified_on: "2021-11-17T14:52:26.133835Z",
167
+ },
168
+ created_on: "2021-11-17T14:52:26.133835Z",
169
+ production_branch: "main",
170
+ },
171
+ {
172
+ name: "cats",
173
+ subdomain: "cats.pages.dev",
174
+ domains: ["cats.pages.dev", "kitten.com"],
175
+ latest_deployment: {
176
+ modified_on: "2021-11-17T14:52:26.133835Z",
177
+ },
178
+ created_on: "2021-11-17T14:52:26.133835Z",
179
+ production_branch: "main",
180
+ },
181
+ ];
182
+
183
+ const requests = mockListRequest(projects);
184
+ await runWrangler("pages project list");
185
+
186
+ expect(requests.count).toBe(1);
187
+ });
188
+
189
+ it("should make multiple requests for paginated results", async () => {
190
+ const projects: Project[] = [];
191
+ for (let i = 0; i < 15; i++) {
192
+ projects.push({
193
+ name: "dogs" + i,
194
+ subdomain: i + "dogs.pages.dev",
195
+ domains: [i + "dogs.pages.dev"],
196
+ source: {
197
+ type: "github",
198
+ },
199
+ latest_deployment: {
200
+ modified_on: "2021-11-17T14:52:26.133835Z",
201
+ },
202
+ created_on: "2021-11-17T14:52:26.133835Z",
203
+ production_branch: "main",
204
+ });
205
+ }
206
+ const requests = mockListRequest(projects);
207
+ await runWrangler("pages project list");
208
+ expect(requests.count).toEqual(2);
209
+ });
210
+ });
211
+
212
+ describe("project create", () => {
213
+ mockAccountId();
214
+ mockApiToken();
215
+
216
+ it("should create a project with a production branch", async () => {
217
+ msw.use(
218
+ rest.post(
219
+ "*/accounts/:accountId/pages/projects",
220
+ async (req, res, ctx) => {
221
+ const body = await req.json();
222
+
223
+ expect(req.params.accountId).toEqual("some-account-id");
224
+ expect(body).toEqual({
225
+ name: "a-new-project",
226
+ production_branch: "main",
227
+ });
228
+
229
+ return res.once(
230
+ ctx.status(200),
231
+ ctx.json({
232
+ success: true,
233
+ errors: [],
234
+ messages: [],
235
+ result: {
236
+ name: "a-new-project",
237
+ subdomain: "a-new-project.pages.dev",
238
+ production_branch: "main",
239
+ },
240
+ })
241
+ );
242
+ }
243
+ )
244
+ );
245
+ await runWrangler(
246
+ "pages project create a-new-project --production-branch=main"
247
+ );
248
+ expect(std.out).toMatchInlineSnapshot(`
249
+ "✨ Successfully created the 'a-new-project' project. It will be available at https://a-new-project.pages.dev/ once you create your first deployment.
250
+ To deploy a folder of assets, run 'wrangler pages publish [directory]'."
251
+ `);
252
+ });
253
+ });
254
+
255
+ describe("deployment list", () => {
256
+ mockAccountId();
257
+ mockApiToken();
258
+
259
+ function mockListRequest(deployments: unknown[]) {
260
+ const requests = { count: 0 };
261
+ msw.use(
262
+ rest.get(
263
+ "*/accounts/:accountId/pages/projects/:project/deployments",
264
+ (req, res, ctx) => {
265
+ requests.count++;
266
+
267
+ expect(req.params.project).toEqual("images");
268
+ expect(req.params.accountId).toEqual("some-account-id");
269
+
270
+ return res.once(
271
+ ctx.status(200),
272
+ ctx.json({
273
+ success: true,
274
+ errors: [],
275
+ messages: [],
276
+ result: deployments,
277
+ })
278
+ );
279
+ }
280
+ )
281
+ );
282
+ return requests;
283
+ }
284
+
285
+ it("should make request to list deployments", async () => {
286
+ const deployments: Deployment[] = [
287
+ {
288
+ id: "87bbc8fe-16be-45cd-81e0-63d722e82cdf",
289
+ url: "https://87bbc8fe.images.pages.dev",
290
+ environment: "preview",
291
+ created_on: "2021-11-17T14:52:26.133835Z",
292
+ latest_stage: {
293
+ ended_on: "2021-11-17T14:52:26.133835Z",
294
+ status: "success",
295
+ },
296
+ deployment_trigger: {
297
+ metadata: {
298
+ branch: "main",
299
+ commit_hash: "c7649364c4cb32ad4f65b530b9424e8be5bec9d6",
300
+ },
301
+ },
302
+ project_name: "images",
303
+ },
304
+ ];
305
+
306
+ const requests = mockListRequest(deployments);
307
+ await runWrangler("pages deployment list --project-name=images");
308
+
309
+ expect(requests.count).toBe(1);
310
+ });
311
+ });
312
+
313
+ describe("deployment create", () => {
314
+ let actualProcessEnvCI: string | undefined;
315
+
316
+ mockAccountId();
317
+ mockApiToken();
318
+ runInTempDir();
319
+
320
+ //TODO Abstract MSW handlers that repeat to this level - JACOB
321
+ beforeEach(() => {
322
+ actualProcessEnvCI = process.env.CI;
323
+ process.env.CI = "true";
324
+ });
325
+
326
+ afterEach(() => {
327
+ process.env.CI = actualProcessEnvCI;
328
+ });
329
+
330
+ it("should be aliased with 'wrangler pages publish'", async () => {
331
+ await runWrangler("pages publish --help");
332
+ await endEventLoop();
333
+
334
+ expect(std.out).toMatchInlineSnapshot(`
335
+ "wrangler pages publish [directory]
336
+
337
+ 🆙 Publish a directory of static assets as a Pages deployment
338
+
339
+ Positionals:
340
+ directory The directory of static files to upload [string]
341
+
342
+ Flags:
343
+ -e, --env Environment to use for operations and .env files [string]
344
+ -h, --help Show help [boolean]
345
+ -v, --version Show version number [boolean]
346
+
347
+ Options:
348
+ --project-name The name of the project you want to deploy to [string]
349
+ --branch The name of the branch you want to deploy to [string]
350
+ --commit-hash The SHA to attach to this deployment [string]
351
+ --commit-message The commit message to attach to this deployment [string]
352
+ --commit-dirty Whether or not the workspace should be considered dirty for this deployment [boolean]
353
+ --skip-caching Skip asset caching which speeds up builds [boolean]
354
+ --no-bundle Whether to run bundling on \`_worker.js\` before deploying [boolean] [default: true]
355
+
356
+ 🚧 'wrangler pages <command>' is a beta command. Please report any issues to https://github.com/cloudflare/wrangler2/issues/new/choose"
357
+ `);
358
+ });
359
+
360
+ it("should upload a directory of files", async () => {
361
+ writeFileSync("logo.png", "foobar");
362
+ mockGetToken("<<funfetti-auth-jwt>>");
363
+
364
+ msw.use(
365
+ rest.post("*/pages/assets/check-missing", async (req, res, ctx) => {
366
+ const body = await req.json();
367
+
368
+ expect(req.headers.get("Authorization")).toBe(
369
+ "Bearer <<funfetti-auth-jwt>>"
370
+ );
371
+ expect(body).toMatchObject({
372
+ hashes: ["2082190357cfd3617ccfe04f340c6247"],
373
+ });
374
+
375
+ return res.once(
376
+ ctx.status(200),
377
+ ctx.json({
378
+ success: true,
379
+ errors: [],
380
+ messages: [],
381
+ result: body.hashes,
382
+ })
383
+ );
384
+ }),
385
+ rest.post("*/pages/assets/upload", async (req, res, ctx) => {
386
+ expect(req.headers.get("Authorization")).toMatchInlineSnapshot(
387
+ `"Bearer <<funfetti-auth-jwt>>"`
388
+ );
389
+ expect(await req.json()).toMatchObject([
390
+ {
391
+ key: "2082190357cfd3617ccfe04f340c6247",
392
+ value: Buffer.from("foobar").toString("base64"),
393
+ metadata: {
394
+ contentType: "image/png",
395
+ },
396
+ base64: true,
397
+ },
398
+ ]);
399
+ return res.once(
400
+ ctx.status(200),
401
+ ctx.json({ success: true, errors: [], messages: [], result: null })
402
+ );
403
+ }),
404
+ rest.post(
405
+ "*/accounts/:accountId/pages/projects/foo/deployments",
406
+ async (req, res, ctx) => {
407
+ expect(req.params.accountId).toEqual("some-account-id");
408
+ expect(await (req as RestRequestWithFormData).formData())
409
+ .toMatchInlineSnapshot(`
410
+ FormData {
411
+ Symbol(state): Array [
412
+ Object {
413
+ "name": "manifest",
414
+ "value": "{\\"/logo.png\\":\\"2082190357cfd3617ccfe04f340c6247\\"}",
415
+ },
416
+ ],
417
+ }
418
+ `);
419
+ return res.once(
420
+ ctx.status(200),
421
+ ctx.json({
422
+ success: true,
423
+ errors: [],
424
+ messages: [],
425
+ result: {
426
+ url: "https://abcxyz.foo.pages.dev/",
427
+ },
428
+ })
429
+ );
430
+ }
431
+ ),
432
+ rest.get(
433
+ "*/accounts/:accountId/pages/projects/foo",
434
+ async (req, res, ctx) => {
435
+ expect(req.params.accountId).toEqual("some-account-id");
436
+
437
+ return res.once(
438
+ ctx.status(200),
439
+ ctx.json({
440
+ success: true,
441
+ errors: [],
442
+ messages: [],
443
+ result: { deployment_configs: { production: {}, preview: {} } },
444
+ })
445
+ );
446
+ }
447
+ )
448
+ );
449
+
450
+ await runWrangler("pages publish . --project-name=foo");
451
+
452
+ expect(std.out).toMatchInlineSnapshot(`
453
+ "✨ Success! Uploaded 1 files (TIMINGS)
454
+
455
+ ✨ Deployment complete! Take a peek over at https://abcxyz.foo.pages.dev/"
456
+ `);
457
+ });
458
+
459
+ it("should retry uploads", async () => {
460
+ writeFileSync("logo.txt", "foobar");
461
+
462
+ mockGetToken("<<funfetti-auth-jwt>>");
463
+
464
+ // Accumulate multiple requests then assert afterwards
465
+ const requests: RestRequest[] = [];
466
+ msw.use(
467
+ rest.post("*/pages/assets/check-missing", async (req, res, ctx) => {
468
+ const body = await req.json();
469
+
470
+ expect(req.headers.get("Authorization")).toBe(
471
+ "Bearer <<funfetti-auth-jwt>>"
472
+ );
473
+ expect(body).toMatchObject({
474
+ hashes: ["1a98fb08af91aca4a7df1764a2c4ddb0"],
475
+ });
476
+
477
+ return res.once(
478
+ ctx.status(200),
479
+ ctx.json({
480
+ success: true,
481
+ errors: [],
482
+ messages: [],
483
+ result: body.hashes,
484
+ })
485
+ );
486
+ }),
487
+ rest.post("*/pages/assets/upload", async (req, res, ctx) => {
488
+ requests.push(req);
489
+ expect(req.headers.get("Authorization")).toBe(
490
+ "Bearer <<funfetti-auth-jwt>>"
491
+ );
492
+ expect(await req.json()).toMatchObject([
493
+ {
494
+ key: "1a98fb08af91aca4a7df1764a2c4ddb0",
495
+ value: Buffer.from("foobar").toString("base64"),
496
+ metadata: {
497
+ contentType: "text/plain",
498
+ },
499
+ base64: true,
500
+ },
501
+ ]);
502
+
503
+ if (requests.length < 2) {
504
+ return res(
505
+ ctx.status(200),
506
+ ctx.json({
507
+ success: false,
508
+ errors: [
509
+ {
510
+ code: 800000,
511
+ message: "Something exploded, please retry",
512
+ },
513
+ ],
514
+ messages: [],
515
+ result: null,
516
+ })
517
+ );
518
+ } else {
519
+ return res(
520
+ ctx.status(200),
521
+ ctx.json({
522
+ success: true,
523
+ errors: [],
524
+ messages: [],
525
+ result: null,
526
+ })
527
+ );
528
+ }
529
+ }),
530
+ rest.post(
531
+ "*/accounts/:accountId/pages/projects/foo/deployments",
532
+ async (req, res, ctx) => {
533
+ expect(req.params.accountId).toEqual("some-account-id");
534
+ expect(await (req as RestRequestWithFormData).formData())
535
+ .toMatchInlineSnapshot(`
536
+ FormData {
537
+ Symbol(state): Array [
538
+ Object {
539
+ "name": "manifest",
540
+ "value": "{\\"/logo.txt\\":\\"1a98fb08af91aca4a7df1764a2c4ddb0\\"}",
541
+ },
542
+ ],
543
+ }
544
+ `);
545
+
546
+ return res.once(
547
+ ctx.status(200),
548
+ ctx.json({
549
+ success: true,
550
+ errors: [],
551
+ messages: [],
552
+ result: { url: "https://abcxyz.foo.pages.dev/" },
553
+ })
554
+ );
555
+ }
556
+ ),
557
+ rest.get(
558
+ "*/accounts/:accountId/pages/projects/foo",
559
+ async (req, res, ctx) => {
560
+ expect(req.params.accountId).toEqual("some-account-id");
561
+
562
+ return res.once(
563
+ ctx.status(200),
564
+ ctx.json({
565
+ success: true,
566
+ errors: [],
567
+ messages: [],
568
+ result: { deployment_configs: { production: {}, preview: {} } },
569
+ })
570
+ );
571
+ }
572
+ )
573
+ );
574
+
575
+ await runWrangler("pages publish . --project-name=foo");
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 refetch a JWT if it expires while uploading", async () => {
585
+ writeFileSync("logo.txt", "foobar");
586
+ mockGetToken("<<funfetti-auth-jwt>>");
587
+
588
+ const requests: RestRequest[] = [];
589
+ msw.use(
590
+ rest.post("*/pages/assets/check-missing", async (req, res, ctx) => {
591
+ const body = (await req.json()) as { hashes: string[] };
592
+
593
+ expect(req.headers.get("Authorization")).toBe(
594
+ "Bearer <<funfetti-auth-jwt>>"
595
+ );
596
+ expect(body).toMatchObject({
597
+ hashes: ["1a98fb08af91aca4a7df1764a2c4ddb0"],
598
+ });
599
+
600
+ return res.once(
601
+ ctx.status(200),
602
+ ctx.json({
603
+ success: true,
604
+ errors: [],
605
+ messages: [],
606
+ result: body.hashes,
607
+ })
608
+ );
609
+ }),
610
+ rest.post("*/pages/assets/upload", async (req, res, ctx) => {
611
+ requests.push(req);
612
+ expect(await req.json()).toMatchObject([
613
+ {
614
+ key: "1a98fb08af91aca4a7df1764a2c4ddb0",
615
+ value: Buffer.from("foobar").toString("base64"),
616
+ metadata: {
617
+ contentType: "text/plain",
618
+ },
619
+ base64: true,
620
+ },
621
+ ]);
622
+ // Fail just the first request
623
+ if (requests.length < 2) {
624
+ mockGetToken("<<funfetti-auth-jwt2>>");
625
+ return res(
626
+ ctx.status(200),
627
+ ctx.json({
628
+ success: false,
629
+ errors: [
630
+ {
631
+ code: 8000013,
632
+ message: "Authorization failed",
633
+ },
634
+ ],
635
+ messages: [],
636
+ result: null,
637
+ })
638
+ );
639
+ } else {
640
+ return res(
641
+ ctx.status(200),
642
+ ctx.json({
643
+ success: true,
644
+ errors: [],
645
+ messages: [],
646
+ result: null,
647
+ })
648
+ );
649
+ }
650
+ }),
651
+ rest.post(
652
+ "*/accounts/:accountId/pages/projects/foo/deployments",
653
+ async (req, res, ctx) => {
654
+ expect(req.params.accountId).toEqual("some-account-id");
655
+ expect(await (req as RestRequestWithFormData).formData())
656
+ .toMatchInlineSnapshot(`
657
+ FormData {
658
+ Symbol(state): Array [
659
+ Object {
660
+ "name": "manifest",
661
+ "value": "{\\"/logo.txt\\":\\"1a98fb08af91aca4a7df1764a2c4ddb0\\"}",
662
+ },
663
+ ],
664
+ }
665
+ `);
666
+
667
+ return res.once(
668
+ ctx.status(200),
669
+ ctx.json({
670
+ success: true,
671
+ errors: [],
672
+ messages: [],
673
+ result: { url: "https://abcxyz.foo.pages.dev/" },
674
+ })
675
+ );
676
+ }
677
+ ),
678
+ rest.get(
679
+ "*/accounts/:accountId/pages/projects/foo",
680
+ async (req, res, ctx) => {
681
+ expect(req.params.accountId).toEqual("some-account-id");
682
+
683
+ return res.once(
684
+ ctx.status(200),
685
+ ctx.json({
686
+ success: true,
687
+ errors: [],
688
+ messages: [],
689
+ result: { deployment_configs: { production: {}, preview: {} } },
690
+ })
691
+ );
692
+ }
693
+ )
694
+ );
695
+
696
+ await runWrangler("pages publish . --project-name=foo");
697
+
698
+ expect(requests[0].headers.get("Authorization")).toBe(
699
+ "Bearer <<funfetti-auth-jwt>>"
700
+ );
701
+
702
+ expect(requests[1].headers.get("Authorization")).toBe(
703
+ "Bearer <<funfetti-auth-jwt2>>"
704
+ );
705
+
706
+ expect(std.out).toMatchInlineSnapshot(`
707
+ "✨ Success! Uploaded 1 files (TIMINGS)
708
+
709
+ ✨ Deployment complete! Take a peek over at https://abcxyz.foo.pages.dev/"
710
+ `);
711
+ });
712
+
713
+ it("should try to use multiple buckets (up to the max concurrency)", async () => {
714
+ writeFileSync("logo.txt", "foobar");
715
+ writeFileSync("logo.png", "foobar");
716
+ writeFileSync("logo.html", "foobar");
717
+ writeFileSync("logo.js", "foobar");
718
+
719
+ mockGetToken("<<funfetti-auth-jwt>>");
720
+
721
+ // Accumulate multiple requests then assert afterwards
722
+ const requests: RestRequest[] = [];
723
+ const bodies: UploadPayloadFile[][] = [];
724
+ msw.use(
725
+ rest.post("*/pages/assets/check-missing", async (req, res, ctx) => {
726
+ const body = (await req.json()) as {
727
+ hashes: string[];
728
+ };
729
+
730
+ expect(req.headers.get("Authorization")).toBe(
731
+ "Bearer <<funfetti-auth-jwt>>"
732
+ );
733
+ expect(body).toMatchObject({
734
+ hashes: expect.arrayContaining([
735
+ "d96fef225537c9f5e44a3cb27fd0b492",
736
+ "2082190357cfd3617ccfe04f340c6247",
737
+ "6be321bef99e758250dac034474ddbb8",
738
+ "1a98fb08af91aca4a7df1764a2c4ddb0",
739
+ ]),
740
+ });
741
+
742
+ return res.once(
743
+ ctx.status(200),
744
+ ctx.json({
745
+ success: true,
746
+ errors: [],
747
+ messages: [],
748
+ result: body.hashes,
749
+ })
750
+ );
751
+ }),
752
+ rest.post("*/pages/assets/upload", async (req, res, ctx) => {
753
+ requests.push(req);
754
+
755
+ expect(req.headers.get("Authorization")).toBe(
756
+ "Bearer <<funfetti-auth-jwt>>"
757
+ );
758
+ bodies.push((await req.json()) as UploadPayloadFile[]);
759
+
760
+ return res(
761
+ ctx.status(200),
762
+ ctx.json({
763
+ success: true,
764
+ errors: [],
765
+ messages: [],
766
+ result: null,
767
+ })
768
+ );
769
+ }),
770
+ rest.post(
771
+ "*/accounts/:accountId/pages/projects/foo/deployments",
772
+ async (req, res, ctx) => {
773
+ expect(req.params.accountId).toEqual("some-account-id");
774
+
775
+ const body = await (req as RestRequestWithFormData).formData();
776
+ const manifest = JSON.parse(body.get("manifest") as string);
777
+
778
+ expect(manifest).toMatchInlineSnapshot(`
779
+ Object {
780
+ "/logo.html": "d96fef225537c9f5e44a3cb27fd0b492",
781
+ "/logo.js": "6be321bef99e758250dac034474ddbb8",
782
+ "/logo.png": "2082190357cfd3617ccfe04f340c6247",
783
+ "/logo.txt": "1a98fb08af91aca4a7df1764a2c4ddb0",
784
+ }
785
+ `);
786
+
787
+ return res.once(
788
+ ctx.status(200),
789
+ ctx.json({
790
+ success: true,
791
+ errors: [],
792
+ messages: [],
793
+ result: {
794
+ url: "https://abcxyz.foo.pages.dev/",
795
+ },
796
+ })
797
+ );
798
+ }
799
+ ),
800
+ rest.get(
801
+ "*/accounts/:accountId/pages/projects/foo",
802
+ async (req, res, ctx) => {
803
+ expect(req.params.accountId).toEqual("some-account-id");
804
+
805
+ return res.once(
806
+ ctx.status(200),
807
+ ctx.json({
808
+ success: true,
809
+ errors: [],
810
+ messages: [],
811
+ result: {
812
+ deployment_configs: { production: {}, preview: {} },
813
+ },
814
+ })
815
+ );
816
+ }
817
+ )
818
+ );
819
+
820
+ await runWrangler("pages publish . --project-name=foo");
821
+
822
+ // We have 3 buckets, so expect 3 uploads
823
+ expect(requests.length).toBe(3);
824
+
825
+ // One bucket should end up with 2 files
826
+ expect(bodies.map((b) => b.length).sort()).toEqual([1, 1, 2]);
827
+ // But we don't know the order, so flatten and test without ordering
828
+ expect(bodies.flatMap((b) => b)).toEqual(
829
+ expect.arrayContaining([
830
+ {
831
+ base64: true,
832
+ key: "d96fef225537c9f5e44a3cb27fd0b492",
833
+ metadata: { contentType: "text/html" },
834
+ value: "Zm9vYmFy",
835
+ },
836
+ {
837
+ base64: true,
838
+ key: "1a98fb08af91aca4a7df1764a2c4ddb0",
839
+ metadata: { contentType: "text/plain" },
840
+ value: "Zm9vYmFy",
841
+ },
842
+ {
843
+ base64: true,
844
+ key: "6be321bef99e758250dac034474ddbb8",
845
+ metadata: { contentType: "application/javascript" },
846
+ value: "Zm9vYmFy",
847
+ },
848
+ {
849
+ base64: true,
850
+ key: "2082190357cfd3617ccfe04f340c6247",
851
+ metadata: { contentType: "image/png" },
852
+ value: "Zm9vYmFy",
853
+ },
854
+ ])
855
+ );
856
+
857
+ expect(std.out).toMatchInlineSnapshot(`
858
+ "✨ Success! Uploaded 4 files (TIMINGS)
859
+
860
+ ✨ Deployment complete! Take a peek over at https://abcxyz.foo.pages.dev/"
861
+ `);
862
+ });
863
+
864
+ it("should resolve child directories correctly", async () => {
865
+ mkdirSync("public");
866
+ mkdirSync("public/imgs");
867
+ writeFileSync("public/logo.txt", "foobar");
868
+ writeFileSync("public/imgs/logo.png", "foobar");
869
+ writeFileSync("public/logo.html", "foobar");
870
+ writeFileSync("public/logo.js", "foobar");
871
+
872
+ mockGetToken("<<funfetti-auth-jwt>>");
873
+
874
+ // Accumulate multiple requests then assert afterwards
875
+ const requests: RestRequest[] = [];
876
+ const bodies: UploadPayloadFile[][] = [];
877
+ msw.use(
878
+ rest.post("*/pages/assets/check-missing", async (req, res, ctx) => {
879
+ const body = (await req.json()) as {
880
+ hashes: string[];
881
+ };
882
+
883
+ expect(req.headers.get("Authorization")).toBe(
884
+ "Bearer <<funfetti-auth-jwt>>"
885
+ );
886
+ expect(body).toMatchObject({
887
+ hashes: expect.arrayContaining([
888
+ "d96fef225537c9f5e44a3cb27fd0b492",
889
+ "2082190357cfd3617ccfe04f340c6247",
890
+ "6be321bef99e758250dac034474ddbb8",
891
+ "1a98fb08af91aca4a7df1764a2c4ddb0",
892
+ ]),
893
+ });
894
+
895
+ return res.once(
896
+ ctx.status(200),
897
+ ctx.json({
898
+ success: true,
899
+ errors: [],
900
+ messages: [],
901
+ result: body.hashes,
902
+ })
903
+ );
904
+ }),
905
+ rest.post("*/pages/assets/upload", async (req, res, ctx) => {
906
+ requests.push(req);
907
+
908
+ expect(req.headers.get("Authorization")).toBe(
909
+ "Bearer <<funfetti-auth-jwt>>"
910
+ );
911
+ bodies.push((await req.json()) as UploadPayloadFile[]);
912
+
913
+ return res(
914
+ ctx.status(200),
915
+ ctx.json({
916
+ success: true,
917
+ errors: [],
918
+ messages: [],
919
+ result: null,
920
+ })
921
+ );
922
+ }),
923
+ rest.post(
924
+ "*/accounts/:accountId/pages/projects/foo/deployments",
925
+ async (req, res, ctx) => {
926
+ expect(req.params.accountId).toEqual("some-account-id");
927
+ const body = await (req as RestRequestWithFormData).formData();
928
+ const manifest = JSON.parse(body.get("manifest") as string);
929
+ expect(manifest).toMatchInlineSnapshot(`
930
+ Object {
931
+ "/imgs/logo.png": "2082190357cfd3617ccfe04f340c6247",
932
+ "/logo.html": "d96fef225537c9f5e44a3cb27fd0b492",
933
+ "/logo.js": "6be321bef99e758250dac034474ddbb8",
934
+ "/logo.txt": "1a98fb08af91aca4a7df1764a2c4ddb0",
935
+ }
936
+ `);
937
+
938
+ return res.once(
939
+ ctx.status(200),
940
+ ctx.json({
941
+ success: true,
942
+ errors: [],
943
+ messages: [],
944
+ result: { url: "https://abcxyz.foo.pages.dev/" },
945
+ })
946
+ );
947
+ }
948
+ ),
949
+ rest.get(
950
+ "*/accounts/:accountId/pages/projects/foo",
951
+ async (req, res, ctx) => {
952
+ expect(req.params.accountId).toEqual("some-account-id");
953
+
954
+ return res.once(
955
+ ctx.status(200),
956
+ ctx.json({
957
+ success: true,
958
+ errors: [],
959
+ messages: [],
960
+ result: {
961
+ deployment_configs: { production: {}, preview: {} },
962
+ },
963
+ })
964
+ );
965
+ }
966
+ )
967
+ );
968
+
969
+ await runWrangler(`pages publish public --project-name=foo`);
970
+
971
+ // We have 3 buckets, so expect 3 uploads
972
+ expect(requests.length).toBe(3);
973
+ // One bucket should end up with 2 files
974
+ expect(bodies.map((b) => b.length).sort()).toEqual([1, 1, 2]);
975
+ // But we don't know the order, so flatten and test without ordering
976
+ expect(bodies.flatMap((b) => b)).toEqual(
977
+ expect.arrayContaining([
978
+ {
979
+ base64: true,
980
+ key: "d96fef225537c9f5e44a3cb27fd0b492",
981
+ metadata: { contentType: "text/html" },
982
+ value: "Zm9vYmFy",
983
+ },
984
+ {
985
+ base64: true,
986
+ key: "1a98fb08af91aca4a7df1764a2c4ddb0",
987
+ metadata: { contentType: "text/plain" },
988
+ value: "Zm9vYmFy",
989
+ },
990
+ {
991
+ base64: true,
992
+ key: "6be321bef99e758250dac034474ddbb8",
993
+ metadata: { contentType: "application/javascript" },
994
+ value: "Zm9vYmFy",
995
+ },
996
+ {
997
+ base64: true,
998
+ key: "2082190357cfd3617ccfe04f340c6247",
999
+ metadata: { contentType: "image/png" },
1000
+ value: "Zm9vYmFy",
1001
+ },
1002
+ ])
1003
+ );
1004
+
1005
+ expect(std.out).toMatchInlineSnapshot(`
1006
+ "✨ Success! Uploaded 4 files (TIMINGS)
1007
+
1008
+ ✨ Deployment complete! Take a peek over at https://abcxyz.foo.pages.dev/"
1009
+ `);
1010
+ });
1011
+
1012
+ it("should resolve the current directory correctly", async () => {
1013
+ mkdirSync("public");
1014
+ mkdirSync("public/imgs");
1015
+ writeFileSync("public/logo.txt", "foobar");
1016
+ writeFileSync("public/imgs/logo.png", "foobar");
1017
+ writeFileSync("public/logo.html", "foobar");
1018
+ writeFileSync("public/logo.js", "foobar");
1019
+
1020
+ mockGetToken("<<funfetti-auth-jwt>>");
1021
+
1022
+ // Accumulate multiple requests then assert afterwards
1023
+ const requests: RestRequest[] = [];
1024
+ const bodies: UploadPayloadFile[][] = [];
1025
+ msw.use(
1026
+ rest.post("*/pages/assets/check-missing", async (req, res, ctx) => {
1027
+ const body = (await req.json()) as {
1028
+ hashes: string[];
1029
+ };
1030
+
1031
+ expect(req.headers.get("Authorization")).toBe(
1032
+ "Bearer <<funfetti-auth-jwt>>"
1033
+ );
1034
+ expect(body).toMatchObject({
1035
+ hashes: expect.arrayContaining([
1036
+ "d96fef225537c9f5e44a3cb27fd0b492",
1037
+ "2082190357cfd3617ccfe04f340c6247",
1038
+ "6be321bef99e758250dac034474ddbb8",
1039
+ "1a98fb08af91aca4a7df1764a2c4ddb0",
1040
+ ]),
1041
+ });
1042
+
1043
+ return res.once(
1044
+ ctx.status(200),
1045
+ ctx.json({
1046
+ success: true,
1047
+ errors: [],
1048
+ messages: [],
1049
+ result: body.hashes,
1050
+ })
1051
+ );
1052
+ }),
1053
+ rest.post("*/pages/assets/upload", async (req, res, ctx) => {
1054
+ requests.push(req);
1055
+
1056
+ expect(req.headers.get("Authorization")).toBe(
1057
+ "Bearer <<funfetti-auth-jwt>>"
1058
+ );
1059
+ bodies.push((await req.json()) as UploadPayloadFile[]);
1060
+
1061
+ return res(
1062
+ ctx.status(200),
1063
+ ctx.json({
1064
+ success: true,
1065
+ errors: [],
1066
+ messages: [],
1067
+ result: null,
1068
+ })
1069
+ );
1070
+ }),
1071
+ rest.post(
1072
+ "*/accounts/:accountId/pages/projects/foo/deployments",
1073
+ async (req, res, ctx) => {
1074
+ expect(req.params.accountId).toEqual("some-account-id");
1075
+
1076
+ const body = await (req as RestRequestWithFormData).formData();
1077
+ const manifest = JSON.parse(body.get("manifest") as string);
1078
+ expect(manifest).toMatchInlineSnapshot(`
1079
+ Object {
1080
+ "/imgs/logo.png": "2082190357cfd3617ccfe04f340c6247",
1081
+ "/logo.html": "d96fef225537c9f5e44a3cb27fd0b492",
1082
+ "/logo.js": "6be321bef99e758250dac034474ddbb8",
1083
+ "/logo.txt": "1a98fb08af91aca4a7df1764a2c4ddb0",
1084
+ }
1085
+ `);
1086
+
1087
+ return res.once(
1088
+ ctx.status(200),
1089
+ ctx.json({
1090
+ success: true,
1091
+ errors: [],
1092
+ messages: [],
1093
+ result: { url: "https://abcxyz.foo.pages.dev/" },
1094
+ })
1095
+ );
1096
+ }
1097
+ ),
1098
+ rest.get(
1099
+ "*/accounts/:accountId/pages/projects/foo",
1100
+ async (req, res, ctx) => {
1101
+ expect(req.params.accountId).toEqual("some-account-id");
1102
+
1103
+ return res.once(
1104
+ ctx.status(200),
1105
+ ctx.json({
1106
+ success: true,
1107
+ errors: [],
1108
+ messages: [],
1109
+ result: {
1110
+ deployment_configs: { production: {}, preview: {} },
1111
+ },
1112
+ })
1113
+ );
1114
+ }
1115
+ )
1116
+ );
1117
+
1118
+ chdir("public");
1119
+ await runWrangler(`pages publish . --project-name=foo`);
1120
+ // We have 3 buckets, so expect 3 uploads
1121
+ expect(requests.length).toBe(3);
1122
+ // One bucket should end up with 2 files
1123
+ expect(bodies.map((b) => b.length).sort()).toEqual([1, 1, 2]);
1124
+ // But we don't know the order, so flatten and test without ordering
1125
+ expect(bodies.flatMap((b) => b)).toEqual(
1126
+ expect.arrayContaining([
1127
+ {
1128
+ base64: true,
1129
+ key: "d96fef225537c9f5e44a3cb27fd0b492",
1130
+ metadata: { contentType: "text/html" },
1131
+ value: "Zm9vYmFy",
1132
+ },
1133
+ {
1134
+ base64: true,
1135
+ key: "1a98fb08af91aca4a7df1764a2c4ddb0",
1136
+ metadata: { contentType: "text/plain" },
1137
+ value: "Zm9vYmFy",
1138
+ },
1139
+ {
1140
+ base64: true,
1141
+ key: "6be321bef99e758250dac034474ddbb8",
1142
+ metadata: { contentType: "application/javascript" },
1143
+ value: "Zm9vYmFy",
1144
+ },
1145
+ {
1146
+ base64: true,
1147
+ key: "2082190357cfd3617ccfe04f340c6247",
1148
+ metadata: { contentType: "image/png" },
1149
+ value: "Zm9vYmFy",
1150
+ },
1151
+ ])
1152
+ );
1153
+
1154
+ expect(std.out).toMatchInlineSnapshot(`
1155
+ "✨ Success! Uploaded 4 files (TIMINGS)
1156
+
1157
+ ✨ Deployment complete! Take a peek over at https://abcxyz.foo.pages.dev/"
1158
+ `);
1159
+ });
1160
+
1161
+ it("should not error when directory names contain periods and houses a extensionless file", async () => {
1162
+ mkdirSync(".well-known");
1163
+ // Note: same content as previous test, but since it's a different extension,
1164
+ // it hashes to a different value
1165
+ writeFileSync(".well-known/foobar", "foobar");
1166
+
1167
+ mockGetToken("<<funfetti-auth-jwt>>");
1168
+
1169
+ msw.use(
1170
+ rest.post("*/pages/assets/check-missing", async (req, res, ctx) => {
1171
+ const body = (await req.json()) as {
1172
+ hashes: string[];
1173
+ };
1174
+
1175
+ expect(req.headers.get("Authorization")).toBe(
1176
+ "Bearer <<funfetti-auth-jwt>>"
1177
+ );
1178
+ expect(body).toMatchObject({
1179
+ hashes: ["7b764dacfd211bebd8077828a7ddefd7"],
1180
+ });
1181
+
1182
+ return res.once(
1183
+ ctx.status(200),
1184
+ ctx.json({
1185
+ success: true,
1186
+ errors: [],
1187
+ messages: [],
1188
+ result: body.hashes,
1189
+ })
1190
+ );
1191
+ }),
1192
+
1193
+ rest.post("*/pages/assets/upload", async (req, res, ctx) => {
1194
+ expect(req.headers.get("Authorization")).toBe(
1195
+ "Bearer <<funfetti-auth-jwt>>"
1196
+ );
1197
+ const body = (await req.json()) as UploadPayloadFile[];
1198
+ expect(body).toMatchObject([
1199
+ {
1200
+ key: "7b764dacfd211bebd8077828a7ddefd7",
1201
+ value: Buffer.from("foobar").toString("base64"),
1202
+ metadata: {
1203
+ contentType: "application/octet-stream",
1204
+ },
1205
+ base64: true,
1206
+ },
1207
+ ]);
1208
+ return res.once(
1209
+ ctx.status(200),
1210
+ ctx.json({
1211
+ success: true,
1212
+ errors: [],
1213
+ messages: [],
1214
+ result: null,
1215
+ })
1216
+ );
1217
+ }),
1218
+ rest.post(
1219
+ "*/accounts/:accountId/pages/projects/foo/deployments",
1220
+ async (req, res, ctx) => {
1221
+ expect(req.params.accountId).toEqual("some-account-id");
1222
+
1223
+ return res.once(
1224
+ ctx.status(200),
1225
+ ctx.json({
1226
+ success: true,
1227
+ errors: [],
1228
+ messages: [],
1229
+ result: { url: "https://abcxyz.foo.pages.dev/" },
1230
+ })
1231
+ );
1232
+ }
1233
+ ),
1234
+ rest.get(
1235
+ "*/accounts/:accountId/pages/projects/foo",
1236
+ async (req, res, ctx) => {
1237
+ expect(req.params.accountId).toEqual("some-account-id");
1238
+
1239
+ return res.once(
1240
+ ctx.status(200),
1241
+ ctx.json({
1242
+ success: true,
1243
+ errors: [],
1244
+ messages: [],
1245
+ result: {
1246
+ deployment_configs: { production: {}, preview: {} },
1247
+ },
1248
+ })
1249
+ );
1250
+ }
1251
+ )
1252
+ );
1253
+
1254
+ await runWrangler("pages publish . --project-name=foo");
1255
+
1256
+ expect(std.err).toMatchInlineSnapshot(`""`);
1257
+ });
1258
+
1259
+ it("should throw an error if user attempts to use config with pages", async () => {
1260
+ await expect(
1261
+ runWrangler("pages dev --config foo.toml")
1262
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
1263
+ `"Pages does not support wrangler.toml"`
1264
+ );
1265
+ await expect(
1266
+ runWrangler("pages publish --config foo.toml")
1267
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
1268
+ `"Pages does not support wrangler.toml"`
1269
+ );
1270
+ });
1271
+
1272
+ it("should upload a Functions project", async () => {
1273
+ // set up the directory of static files to upload.
1274
+ mkdirSync("public");
1275
+ writeFileSync("public/README.md", "This is a readme");
1276
+
1277
+ // set up /functions
1278
+ mkdirSync("functions");
1279
+ writeFileSync(
1280
+ "functions/hello.js",
1281
+ `
1282
+ export async function onRequest() {
1283
+ return new Response("Hello, world!");
1284
+ }
1285
+ `
1286
+ );
1287
+
1288
+ mockGetToken("<<funfetti-auth-jwt>>");
1289
+
1290
+ msw.use(
1291
+ rest.post("*/pages/assets/check-missing", async (req, res, ctx) => {
1292
+ const body = (await req.json()) as {
1293
+ hashes: string[];
1294
+ };
1295
+
1296
+ expect(req.headers.get("Authorization")).toBe(
1297
+ "Bearer <<funfetti-auth-jwt>>"
1298
+ );
1299
+ expect(body).toMatchObject({
1300
+ hashes: ["13a03eaf24ae98378acd36ea00f77f2f"],
1301
+ });
1302
+
1303
+ return res.once(
1304
+ ctx.status(200),
1305
+ ctx.json({
1306
+ success: true,
1307
+ errors: [],
1308
+ messages: [],
1309
+ result: body.hashes,
1310
+ })
1311
+ );
1312
+ }),
1313
+ rest.post("*/pages/assets/upload", async (req, res, ctx) => {
1314
+ expect(req.headers.get("Authorization")).toBe(
1315
+ "Bearer <<funfetti-auth-jwt>>"
1316
+ );
1317
+
1318
+ expect(await req.json()).toMatchObject([
1319
+ {
1320
+ key: "13a03eaf24ae98378acd36ea00f77f2f",
1321
+ value: Buffer.from("This is a readme").toString("base64"),
1322
+ metadata: {
1323
+ contentType: "text/markdown",
1324
+ },
1325
+ base64: true,
1326
+ },
1327
+ ]);
1328
+ return res.once(
1329
+ ctx.status(200),
1330
+ ctx.json({
1331
+ success: true,
1332
+ errors: [],
1333
+ messages: [],
1334
+ result: true,
1335
+ })
1336
+ );
1337
+ }),
1338
+ rest.post(`*/pages/assets/upsert-hashes`, async (req, res, ctx) => {
1339
+ expect(req.headers.get("Authorization")).toBe(
1340
+ "Bearer <<funfetti-auth-jwt>>"
1341
+ );
1342
+
1343
+ expect(await req.json()).toMatchObject({
1344
+ hashes: ["13a03eaf24ae98378acd36ea00f77f2f"],
1345
+ });
1346
+
1347
+ return res.once(
1348
+ ctx.status(200),
1349
+ ctx.json({
1350
+ success: true,
1351
+ errors: [],
1352
+ messages: [],
1353
+ result: true,
1354
+ })
1355
+ );
1356
+ }),
1357
+
1358
+ rest.post(
1359
+ "*/accounts/:accountId/pages/projects/foo/deployments",
1360
+ async (req, res, ctx) => {
1361
+ expect(req.params.accountId).toEqual("some-account-id");
1362
+ const body = await (req as RestRequestWithFormData).formData();
1363
+ const manifest = JSON.parse(body.get("manifest") as string);
1364
+
1365
+ // for Functions projects, we auto-generate a `_worker.js`,
1366
+ // `functions-filepath-routing-config.json`, and `_routes.json`
1367
+ // file, based on the contents of `/functions`
1368
+ const generatedWorkerJS = body.get("_worker.js") as string;
1369
+ const generatedRoutesJSON = body.get("_routes.json") as string;
1370
+ const generatedFilepathRoutingConfig = body.get(
1371
+ "functions-filepath-routing-config.json"
1372
+ ) as string;
1373
+
1374
+ // make sure this is all we uploaded
1375
+ expect([...body.keys()]).toEqual([
1376
+ "manifest",
1377
+ "functions-filepath-routing-config.json",
1378
+ "_worker.js",
1379
+ "_routes.json",
1380
+ ]);
1381
+
1382
+ expect(manifest).toMatchInlineSnapshot(`
1383
+ Object {
1384
+ "/README.md": "13a03eaf24ae98378acd36ea00f77f2f",
1385
+ }
1386
+ `);
1387
+
1388
+ // the contents of the generated `_worker.js` file is pretty massive, so I don't
1389
+ // think snapshot testing makes much sense here. Plus, calling
1390
+ // `.toMatchInlineSnapshot()` without any arguments, in order to generate that
1391
+ // snapshot value, doesn't generate anything in this case (probably because the
1392
+ // file contents is too big). So for now, let's test that _worker.js was indeed
1393
+ // generated and that the file size is greater than zero
1394
+ expect(generatedWorkerJS).not.toBeNull();
1395
+ expect(generatedWorkerJS.length).toBeGreaterThan(0);
1396
+
1397
+ const maybeRoutesJSONSpec = JSON.parse(generatedRoutesJSON);
1398
+ expect(isRoutesJSONSpec(maybeRoutesJSONSpec)).toBe(true);
1399
+ expect(maybeRoutesJSONSpec).toMatchObject({
1400
+ version: ROUTES_SPEC_VERSION,
1401
+ description: `Generated by wrangler@${version}`,
1402
+ include: ["/hello"],
1403
+ exclude: [],
1404
+ });
1405
+
1406
+ // Make sure the routing config is valid json
1407
+ const parsedFilepathRoutingConfig = JSON.parse(
1408
+ generatedFilepathRoutingConfig
1409
+ );
1410
+ // The actual shape doesn't matter that much since this
1411
+ // is only used for display in Dash, but it's still useful for
1412
+ // tracking unexpected changes to this config.
1413
+ expect(parsedFilepathRoutingConfig).toStrictEqual({
1414
+ routes: [
1415
+ {
1416
+ routePath: "/hello",
1417
+ mountPath: "/",
1418
+ method: "",
1419
+ module: ["hello.js:onRequest"],
1420
+ },
1421
+ ],
1422
+ baseURL: "/",
1423
+ });
1424
+
1425
+ return res.once(
1426
+ ctx.status(200),
1427
+ ctx.json({
1428
+ success: true,
1429
+ errors: [],
1430
+ messages: [],
1431
+ result: {
1432
+ url: "https://abcxyz.foo.pages.dev/",
1433
+ },
1434
+ })
1435
+ );
1436
+ }
1437
+ ),
1438
+ rest.get(
1439
+ "*/accounts/:accountId/pages/projects/foo",
1440
+ async (req, res, ctx) => {
1441
+ expect(req.params.accountId).toEqual("some-account-id");
1442
+
1443
+ return res.once(
1444
+ ctx.status(200),
1445
+ ctx.json({
1446
+ success: true,
1447
+ errors: [],
1448
+ messages: [],
1449
+ result: {
1450
+ deployment_configs: { production: {}, preview: {} },
1451
+ },
1452
+ })
1453
+ );
1454
+ }
1455
+ )
1456
+ );
1457
+
1458
+ await runWrangler("pages publish public --project-name=foo");
1459
+
1460
+ expect(std.out).toMatchInlineSnapshot(`
1461
+ "✨ Compiled Worker successfully
1462
+ ✨ Success! Uploaded 1 files (TIMINGS)
1463
+
1464
+ ✨ Uploading Functions
1465
+ ✨ Deployment complete! Take a peek over at https://abcxyz.foo.pages.dev/"
1466
+ `);
1467
+
1468
+ expect(std.err).toMatchInlineSnapshot('""');
1469
+ });
1470
+
1471
+ it("should upload an Advanced Mode project", async () => {
1472
+ // set up the directory of static files to upload.
1473
+ mkdirSync("public");
1474
+ writeFileSync("public/README.md", "This is a readme");
1475
+
1476
+ // set up _worker.js
1477
+ writeFileSync(
1478
+ "public/_worker.js",
1479
+ `
1480
+ export default {
1481
+ async fetch(request, env) {
1482
+ const url = new URL(request.url);
1483
+ return url.pathname.startsWith('/api/') ? new Response('Ok') : env.ASSETS.fetch(request);
1484
+ }
1485
+ };
1486
+ `
1487
+ );
1488
+
1489
+ mockGetToken("<<funfetti-auth-jwt>>");
1490
+
1491
+ msw.use(
1492
+ rest.post("*/pages/assets/check-missing", async (req, res, ctx) => {
1493
+ const body = (await req.json()) as {
1494
+ hashes: string[];
1495
+ };
1496
+
1497
+ expect(req.headers.get("Authorization")).toBe(
1498
+ "Bearer <<funfetti-auth-jwt>>"
1499
+ );
1500
+ expect(body).toMatchObject({
1501
+ hashes: ["13a03eaf24ae98378acd36ea00f77f2f"],
1502
+ });
1503
+
1504
+ return res.once(
1505
+ ctx.status(200),
1506
+ ctx.json({
1507
+ success: true,
1508
+ errors: [],
1509
+ messages: [],
1510
+ result: body.hashes,
1511
+ })
1512
+ );
1513
+ }),
1514
+ rest.post("*/pages/assets/upload", async (req, res, ctx) => {
1515
+ expect(req.headers.get("Authorization")).toBe(
1516
+ "Bearer <<funfetti-auth-jwt>>"
1517
+ );
1518
+
1519
+ expect(await req.json()).toMatchObject([
1520
+ {
1521
+ key: "13a03eaf24ae98378acd36ea00f77f2f",
1522
+ value: Buffer.from("This is a readme").toString("base64"),
1523
+ metadata: {
1524
+ contentType: "text/markdown",
1525
+ },
1526
+ base64: true,
1527
+ },
1528
+ ]);
1529
+ return res.once(
1530
+ ctx.status(200),
1531
+ ctx.json({
1532
+ success: true,
1533
+ errors: [],
1534
+ messages: [],
1535
+ result: true,
1536
+ })
1537
+ );
1538
+ }),
1539
+ rest.post(
1540
+ "*/accounts/:accountId/pages/projects/foo/deployments",
1541
+ async (req, res, ctx) => {
1542
+ expect(req.params.accountId).toEqual("some-account-id");
1543
+ const body = await (req as RestRequestWithFormData).formData();
1544
+ const manifest = JSON.parse(body.get("manifest") as string);
1545
+ const customWorkerJS = body.get("_worker.js");
1546
+
1547
+ // make sure this is all we uploaded
1548
+ expect([...body.keys()].sort()).toEqual(
1549
+ ["manifest", "_worker.js"].sort()
1550
+ );
1551
+
1552
+ expect(manifest).toMatchInlineSnapshot(`
1553
+ Object {
1554
+ "/README.md": "13a03eaf24ae98378acd36ea00f77f2f",
1555
+ }
1556
+ `);
1557
+
1558
+ expect(customWorkerJS).toMatchInlineSnapshot(`
1559
+ "
1560
+ export default {
1561
+ async fetch(request, env) {
1562
+ const url = new URL(request.url);
1563
+ return url.pathname.startsWith('/api/') ? new Response('Ok') : env.ASSETS.fetch(request);
1564
+ }
1565
+ };
1566
+ "
1567
+ `);
1568
+ return res.once(
1569
+ ctx.status(200),
1570
+ ctx.json({
1571
+ success: true,
1572
+ errors: [],
1573
+ messages: [],
1574
+ result: {
1575
+ url: "https://abcxyz.foo.pages.dev/",
1576
+ },
1577
+ })
1578
+ );
1579
+ }
1580
+ ),
1581
+ rest.get(
1582
+ "*/accounts/:accountId/pages/projects/foo",
1583
+ async (req, res, ctx) => {
1584
+ expect(req.params.accountId).toEqual("some-account-id");
1585
+
1586
+ return res.once(
1587
+ ctx.status(200),
1588
+ ctx.json({
1589
+ success: true,
1590
+ errors: [],
1591
+ messages: [],
1592
+ result: {
1593
+ deployment_configs: { production: {}, preview: {} },
1594
+ },
1595
+ })
1596
+ );
1597
+ }
1598
+ )
1599
+ );
1600
+
1601
+ await runWrangler("pages publish public --project-name=foo");
1602
+
1603
+ expect(std.out).toMatchInlineSnapshot(`
1604
+ "✨ Success! Uploaded 1 files (TIMINGS)
1605
+
1606
+ ✨ Compiled Worker successfully
1607
+ ✨ Uploading _worker.js
1608
+ ✨ Deployment complete! Take a peek over at https://abcxyz.foo.pages.dev/"
1609
+ `);
1610
+
1611
+ expect(std.err).toMatchInlineSnapshot('""');
1612
+ });
1613
+
1614
+ it("should upload _routes.json for Functions projects, if provided", async () => {
1615
+ // set up the directory of static files to upload.
1616
+ mkdirSync("public");
1617
+ writeFileSync("public/README.md", "This is a readme");
1618
+
1619
+ // set up /functions
1620
+ mkdirSync("functions");
1621
+ writeFileSync(
1622
+ "functions/hello.js",
1623
+ `
1624
+ export async function onRequest() {
1625
+ return new Response("Hello, world!");
1626
+ }
1627
+ `
1628
+ );
1629
+
1630
+ writeFileSync(
1631
+ "functions/goodbye.ts",
1632
+ `
1633
+ export async function onRequest() {
1634
+ return new Response("Bye bye!");
1635
+ }
1636
+ `
1637
+ );
1638
+
1639
+ // set up _routes.json
1640
+ writeFileSync(
1641
+ "public/_routes.json",
1642
+ `
1643
+ {
1644
+ "version": ${ROUTES_SPEC_VERSION},
1645
+ "description": "Custom _routes.json file",
1646
+ "include": ["/hello"],
1647
+ "exclude": []
1648
+ }
1649
+ `
1650
+ );
1651
+
1652
+ mockGetToken("<<funfetti-auth-jwt>>");
1653
+ msw.use(
1654
+ rest.post("*/pages/assets/check-missing", async (req, res, ctx) => {
1655
+ const body = (await req.json()) as {
1656
+ hashes: string[];
1657
+ };
1658
+
1659
+ expect(req.headers.get("Authorization")).toBe(
1660
+ "Bearer <<funfetti-auth-jwt>>"
1661
+ );
1662
+ expect(body).toMatchObject({
1663
+ hashes: ["13a03eaf24ae98378acd36ea00f77f2f"],
1664
+ });
1665
+
1666
+ return res.once(
1667
+ ctx.status(200),
1668
+ ctx.json({
1669
+ success: true,
1670
+ errors: [],
1671
+ messages: [],
1672
+ result: body.hashes,
1673
+ })
1674
+ );
1675
+ }),
1676
+ rest.post("*/pages/assets/upload", async (req, res, ctx) => {
1677
+ expect(req.headers.get("Authorization")).toBe(
1678
+ "Bearer <<funfetti-auth-jwt>>"
1679
+ );
1680
+
1681
+ expect(await req.json()).toMatchObject([
1682
+ {
1683
+ key: "13a03eaf24ae98378acd36ea00f77f2f",
1684
+ value: Buffer.from("This is a readme").toString("base64"),
1685
+ metadata: {
1686
+ contentType: "text/markdown",
1687
+ },
1688
+ base64: true,
1689
+ },
1690
+ ]);
1691
+
1692
+ return res.once(
1693
+ ctx.status(200),
1694
+ ctx.json({
1695
+ success: true,
1696
+ errors: [],
1697
+ messages: [],
1698
+ result: null,
1699
+ })
1700
+ );
1701
+ }),
1702
+ rest.post(`*/pages/assets/upsert-hashes`, async (req, res, ctx) => {
1703
+ expect(req.headers.get("Authorization")).toBe(
1704
+ "Bearer <<funfetti-auth-jwt>>"
1705
+ );
1706
+
1707
+ expect(await req.json()).toMatchObject({
1708
+ hashes: ["13a03eaf24ae98378acd36ea00f77f2f"],
1709
+ });
1710
+
1711
+ return res.once(
1712
+ ctx.status(200),
1713
+ ctx.json({
1714
+ success: true,
1715
+ errors: [],
1716
+ messages: [],
1717
+ result: true,
1718
+ })
1719
+ );
1720
+ }),
1721
+ rest.post(
1722
+ "*/accounts/:accountId/pages/projects/foo/deployments",
1723
+ async (req, res, ctx) => {
1724
+ expect(req.params.accountId).toEqual("some-account-id");
1725
+ const body = await (req as RestRequestWithFormData).formData();
1726
+ const manifest = JSON.parse(body.get("manifest") as string);
1727
+ const generatedWorkerJS = body.get("_worker.js") as string;
1728
+ const customRoutesJSON = body.get("_routes.json") as string;
1729
+ const generatedFilepathRoutingConfig = body.get(
1730
+ "functions-filepath-routing-config.json"
1731
+ ) as string;
1732
+
1733
+ // make sure this is all we uploaded
1734
+ expect([...body.keys()].sort()).toEqual(
1735
+ [
1736
+ "manifest",
1737
+ "functions-filepath-routing-config.json",
1738
+ "_worker.js",
1739
+ "_routes.json",
1740
+ ].sort()
1741
+ );
1742
+
1743
+ expect(manifest).toMatchInlineSnapshot(`
1744
+ Object {
1745
+ "/README.md": "13a03eaf24ae98378acd36ea00f77f2f",
1746
+ }
1747
+ `);
1748
+
1749
+ // file content of generated `_worker.js` is too massive to snapshot test
1750
+ expect(generatedWorkerJS).not.toBeNull();
1751
+ expect(generatedWorkerJS.length).toBeGreaterThan(0);
1752
+
1753
+ const customRoutes = JSON.parse(customRoutesJSON);
1754
+ expect(customRoutes).toMatchObject({
1755
+ version: ROUTES_SPEC_VERSION,
1756
+ description: "Custom _routes.json file",
1757
+ include: ["/hello"],
1758
+ exclude: [],
1759
+ });
1760
+
1761
+ // Make sure the routing config is valid json
1762
+ const parsedFilepathRoutingConfig = JSON.parse(
1763
+ generatedFilepathRoutingConfig
1764
+ );
1765
+ // The actual shape doesn't matter that much since this
1766
+ // is only used for display in Dash, but it's still useful for
1767
+ // tracking unexpected changes to this config.
1768
+ expect(parsedFilepathRoutingConfig).toStrictEqual({
1769
+ routes: [
1770
+ {
1771
+ routePath: "/goodbye",
1772
+ mountPath: "/",
1773
+ method: "",
1774
+ module: ["goodbye.ts:onRequest"],
1775
+ },
1776
+ {
1777
+ routePath: "/hello",
1778
+ mountPath: "/",
1779
+ method: "",
1780
+ module: ["hello.js:onRequest"],
1781
+ },
1782
+ ],
1783
+ baseURL: "/",
1784
+ });
1785
+
1786
+ return res.once(
1787
+ ctx.status(200),
1788
+ ctx.json({
1789
+ success: true,
1790
+ errors: [],
1791
+ messages: [],
1792
+ result: {
1793
+ url: "https://abcxyz.foo.pages.dev/",
1794
+ },
1795
+ })
1796
+ );
1797
+ }
1798
+ ),
1799
+ rest.get(
1800
+ "*/accounts/:accountId/pages/projects/foo",
1801
+ async (req, res, ctx) => {
1802
+ expect(req.params.accountId).toEqual("some-account-id");
1803
+
1804
+ return res.once(
1805
+ ctx.status(200),
1806
+ ctx.json({
1807
+ success: true,
1808
+ errors: [],
1809
+ messages: [],
1810
+ result: {
1811
+ deployment_configs: { production: {}, preview: {} },
1812
+ },
1813
+ })
1814
+ );
1815
+ }
1816
+ )
1817
+ );
1818
+
1819
+ await runWrangler("pages publish public --project-name=foo");
1820
+
1821
+ expect(std.out).toMatchInlineSnapshot(`
1822
+ "✨ Compiled Worker successfully
1823
+ ✨ Success! Uploaded 1 files (TIMINGS)
1824
+
1825
+ ✨ Uploading Functions
1826
+ ✨ Uploading _routes.json
1827
+ ✨ Deployment complete! Take a peek over at https://abcxyz.foo.pages.dev/"
1828
+ `);
1829
+
1830
+ expect(std.warn).toMatchInlineSnapshot(`""`);
1831
+ expect(std.err).toMatchInlineSnapshot('""');
1832
+ });
1833
+
1834
+ it("should not deploy Functions projects that provide an invalid custom _routes.json file", async () => {
1835
+ // set up the directory of static files to upload.
1836
+ mkdirSync("public");
1837
+ writeFileSync("public/README.md", "This is a readme");
1838
+
1839
+ // set up _routes.json
1840
+ writeFileSync(
1841
+ "public/_routes.json",
1842
+ `
1843
+ {
1844
+ "description": "Custom _routes.json file",
1845
+ "include": [],
1846
+ "exclude": []
1847
+ }
1848
+ `
1849
+ );
1850
+
1851
+ // set up /functions
1852
+ mkdirSync("functions");
1853
+ writeFileSync(
1854
+ "functions/hello.js",
1855
+ `
1856
+ export async function onRequest() {
1857
+ return new Response("Hello, world!");
1858
+ }
1859
+ `
1860
+ );
1861
+
1862
+ mockGetToken("<<funfetti-auth-jwt>>");
1863
+ msw.use(
1864
+ rest.post("*/pages/assets/check-missing", async (req, res, ctx) => {
1865
+ const body = (await req.json()) as {
1866
+ hashes: string[];
1867
+ };
1868
+
1869
+ expect(req.headers.get("Authorization")).toBe(
1870
+ "Bearer <<funfetti-auth-jwt>>"
1871
+ );
1872
+ expect(body).toMatchObject({
1873
+ hashes: ["13a03eaf24ae98378acd36ea00f77f2f"],
1874
+ });
1875
+
1876
+ return res.once(
1877
+ ctx.status(200),
1878
+ ctx.json({
1879
+ success: true,
1880
+ errors: [],
1881
+ messages: [],
1882
+ result: body.hashes,
1883
+ })
1884
+ );
1885
+ }),
1886
+ rest.post("*/pages/assets/upload", async (req, res, ctx) => {
1887
+ expect(req.headers.get("Authorization")).toBe(
1888
+ "Bearer <<funfetti-auth-jwt>>"
1889
+ );
1890
+
1891
+ expect(await req.json()).toMatchObject([
1892
+ {
1893
+ key: "13a03eaf24ae98378acd36ea00f77f2f",
1894
+ value: Buffer.from("This is a readme").toString("base64"),
1895
+ metadata: {
1896
+ contentType: "text/markdown",
1897
+ },
1898
+ base64: true,
1899
+ },
1900
+ ]);
1901
+
1902
+ return res.once(
1903
+ ctx.status(200),
1904
+ ctx.json({
1905
+ success: true,
1906
+ errors: [],
1907
+ messages: [],
1908
+ result: null,
1909
+ })
1910
+ );
1911
+ }),
1912
+ rest.get(
1913
+ "*/accounts/:accountId/pages/projects/foo",
1914
+ async (req, res, ctx) => {
1915
+ expect(req.params.accountId).toEqual("some-account-id");
1916
+
1917
+ return res.once(
1918
+ ctx.status(200),
1919
+ ctx.json({
1920
+ success: true,
1921
+ errors: [],
1922
+ messages: [],
1923
+ result: {
1924
+ deployment_configs: { production: {}, preview: {} },
1925
+ },
1926
+ })
1927
+ );
1928
+ }
1929
+ )
1930
+ );
1931
+
1932
+ await expect(runWrangler("pages publish public --project-name=foo"))
1933
+ .rejects
1934
+ .toThrow(`Invalid _routes.json file found at: public/_routes.json
1935
+ Please make sure the JSON object has the following format:
1936
+ {
1937
+ version: ${ROUTES_SPEC_VERSION};
1938
+ include: string[];
1939
+ exclude: string[];
1940
+ }
1941
+ and that at least one include rule is provided.
1942
+ `);
1943
+ });
1944
+
1945
+ it("should upload _routes.json for Advanced Mode projects, if provided", async () => {
1946
+ // set up the directory of static files to upload.
1947
+ mkdirSync("public");
1948
+ writeFileSync("public/README.md", "This is a readme");
1949
+
1950
+ // set up _routes.json
1951
+ writeFileSync(
1952
+ "public/_routes.json",
1953
+ `
1954
+ {
1955
+ "version": ${ROUTES_SPEC_VERSION},
1956
+ "description": "Custom _routes.json file",
1957
+ "include": ["/api/*"],
1958
+ "exclude": []
1959
+ }
1960
+ `
1961
+ );
1962
+
1963
+ // set up _worker.js
1964
+ writeFileSync(
1965
+ "public/_worker.js",
1966
+ `
1967
+ export default {
1968
+ async fetch(request, env) {
1969
+ const url = new URL(request.url);
1970
+ return url.pathname.startsWith('/api/') ? new Response('Ok') : env.ASSETS.fetch(request);
1971
+ }
1972
+ };
1973
+ `
1974
+ );
1975
+
1976
+ mockGetToken("<<funfetti-auth-jwt>>");
1977
+
1978
+ msw.use(
1979
+ rest.post("*/pages/assets/check-missing", async (req, res, ctx) => {
1980
+ const body = (await req.json()) as {
1981
+ hashes: string[];
1982
+ };
1983
+
1984
+ expect(req.headers.get("Authorization")).toBe(
1985
+ "Bearer <<funfetti-auth-jwt>>"
1986
+ );
1987
+ expect(body).toMatchObject({
1988
+ hashes: ["13a03eaf24ae98378acd36ea00f77f2f"],
1989
+ });
1990
+
1991
+ return res.once(
1992
+ ctx.status(200),
1993
+ ctx.json({
1994
+ success: true,
1995
+ errors: [],
1996
+ messages: [],
1997
+ result: body.hashes,
1998
+ })
1999
+ );
2000
+ }),
2001
+ rest.post("*/pages/assets/upload", async (req, res, ctx) => {
2002
+ expect(req.headers.get("Authorization")).toBe(
2003
+ "Bearer <<funfetti-auth-jwt>>"
2004
+ );
2005
+
2006
+ expect(await req.json()).toMatchObject([
2007
+ {
2008
+ key: "13a03eaf24ae98378acd36ea00f77f2f",
2009
+ value: Buffer.from("This is a readme").toString("base64"),
2010
+ metadata: {
2011
+ contentType: "text/markdown",
2012
+ },
2013
+ base64: true,
2014
+ },
2015
+ ]);
2016
+
2017
+ return res.once(
2018
+ ctx.status(200),
2019
+ ctx.json({
2020
+ success: true,
2021
+ errors: [],
2022
+ messages: [],
2023
+ result: null,
2024
+ })
2025
+ );
2026
+ }),
2027
+ rest.post(`*/pages/assets/upsert-hashes`, async (req, res, ctx) => {
2028
+ expect(req.headers.get("Authorization")).toBe(
2029
+ "Bearer <<funfetti-auth-jwt>>"
2030
+ );
2031
+
2032
+ expect(await req.json()).toMatchObject({
2033
+ hashes: ["13a03eaf24ae98378acd36ea00f77f2f"],
2034
+ });
2035
+
2036
+ return res.once(
2037
+ ctx.status(200),
2038
+ ctx.json({
2039
+ success: true,
2040
+ errors: [],
2041
+ messages: [],
2042
+ result: true,
2043
+ })
2044
+ );
2045
+ }),
2046
+ rest.post(
2047
+ "*/accounts/:accountId/pages/projects/foo/deployments",
2048
+ async (req, res, ctx) => {
2049
+ const body = await (req as RestRequestWithFormData).formData();
2050
+
2051
+ const manifest = JSON.parse(body.get("manifest") as string);
2052
+ const customWorkerJS = body.get("_worker.js") as string;
2053
+ const customRoutesJSON = body.get("_routes.json") as string;
2054
+
2055
+ // make sure this is all we uploaded
2056
+ expect([...body.keys()]).toEqual([
2057
+ "manifest",
2058
+ "_worker.js",
2059
+ "_routes.json",
2060
+ ]);
2061
+ expect(req.params.accountId).toEqual("some-account-id");
2062
+ expect(manifest).toMatchInlineSnapshot(`
2063
+ Object {
2064
+ "/README.md": "13a03eaf24ae98378acd36ea00f77f2f",
2065
+ }
2066
+ `);
2067
+
2068
+ expect(customWorkerJS).toMatchInlineSnapshot(`
2069
+ "
2070
+ export default {
2071
+ async fetch(request, env) {
2072
+ const url = new URL(request.url);
2073
+ return url.pathname.startsWith('/api/') ? new Response('Ok') : env.ASSETS.fetch(request);
2074
+ }
2075
+ };
2076
+ "
2077
+ `);
2078
+
2079
+ expect(JSON.parse(customRoutesJSON)).toMatchObject({
2080
+ version: ROUTES_SPEC_VERSION,
2081
+ description: "Custom _routes.json file",
2082
+ include: ["/api/*"],
2083
+ exclude: [],
2084
+ });
2085
+
2086
+ return res.once(
2087
+ ctx.status(200),
2088
+ ctx.json({
2089
+ success: true,
2090
+ errors: [],
2091
+ messages: [],
2092
+ result: {
2093
+ url: "https://abcxyz.foo.pages.dev/",
2094
+ },
2095
+ })
2096
+ );
2097
+ }
2098
+ ),
2099
+ rest.get(
2100
+ "*/accounts/:accountId/pages/projects/foo",
2101
+ async (req, res, ctx) => {
2102
+ expect(req.params.accountId).toEqual("some-account-id");
2103
+
2104
+ return res.once(
2105
+ ctx.status(200),
2106
+ ctx.json({
2107
+ success: true,
2108
+ errors: [],
2109
+ messages: [],
2110
+ result: {
2111
+ deployment_configs: { production: {}, preview: {} },
2112
+ },
2113
+ })
2114
+ );
2115
+ }
2116
+ )
2117
+ );
2118
+
2119
+ await runWrangler("pages publish public --project-name=foo");
2120
+
2121
+ expect(std.out).toMatchInlineSnapshot(`
2122
+ "✨ Success! Uploaded 1 files (TIMINGS)
2123
+
2124
+ ✨ Compiled Worker successfully
2125
+ ✨ Uploading _worker.js
2126
+ ✨ Uploading _routes.json
2127
+ ✨ Deployment complete! Take a peek over at https://abcxyz.foo.pages.dev/"
2128
+ `);
2129
+
2130
+ expect(std.warn).toMatchInlineSnapshot(`""`);
2131
+ expect(std.err).toMatchInlineSnapshot(`""`);
2132
+ });
2133
+
2134
+ it("should not deploy Advanced Mode projects that provide an invalid _routes.json file", async () => {
2135
+ // set up the directory of static files to upload.
2136
+ mkdirSync("public");
2137
+ writeFileSync("public/README.md", "This is a readme");
2138
+
2139
+ // set up _routes.json
2140
+ writeFileSync(
2141
+ "public/_routes.json",
2142
+ `
2143
+ {
2144
+ "description": "Custom _routes.json file",
2145
+ "include": [],
2146
+ "exclude": []
2147
+ }
2148
+ `
2149
+ );
2150
+
2151
+ // set up _worker.js
2152
+ writeFileSync(
2153
+ "public/_worker.js",
2154
+ `
2155
+ export default {
2156
+ async fetch(request, env) {
2157
+ const url = new URL(request.url);
2158
+ return url.pathname.startsWith('/api/') ? new Response('Ok') : env.ASSETS.fetch(request);
2159
+ }
2160
+ };
2161
+ `
2162
+ );
2163
+
2164
+ mockGetToken("<<funfetti-auth-jwt>>");
2165
+
2166
+ msw.use(
2167
+ rest.post("*/pages/assets/check-missing", async (req, res, ctx) => {
2168
+ const body = (await req.json()) as {
2169
+ hashes: string[];
2170
+ };
2171
+
2172
+ expect(req.headers.get("Authorization")).toBe(
2173
+ "Bearer <<funfetti-auth-jwt>>"
2174
+ );
2175
+ expect(body).toMatchObject({
2176
+ hashes: ["13a03eaf24ae98378acd36ea00f77f2f"],
2177
+ });
2178
+
2179
+ return res.once(
2180
+ ctx.status(200),
2181
+ ctx.json({
2182
+ success: true,
2183
+ errors: [],
2184
+ messages: [],
2185
+ result: body.hashes,
2186
+ })
2187
+ );
2188
+ }),
2189
+ rest.post("*/pages/assets/upload", async (req, res, ctx) => {
2190
+ expect(req.headers.get("Authorization")).toBe(
2191
+ "Bearer <<funfetti-auth-jwt>>"
2192
+ );
2193
+
2194
+ expect(await req.json()).toMatchObject([
2195
+ {
2196
+ key: "13a03eaf24ae98378acd36ea00f77f2f",
2197
+ value: Buffer.from("This is a readme").toString("base64"),
2198
+ metadata: {
2199
+ contentType: "text/markdown",
2200
+ },
2201
+ base64: true,
2202
+ },
2203
+ ]);
2204
+
2205
+ return res.once(
2206
+ ctx.status(200),
2207
+ ctx.json({
2208
+ success: true,
2209
+ errors: [],
2210
+ messages: [],
2211
+ result: null,
2212
+ })
2213
+ );
2214
+ }),
2215
+
2216
+ rest.get(
2217
+ "*/accounts/:accountId/pages/projects/foo",
2218
+ async (req, res, ctx) => {
2219
+ expect(req.params.accountId).toEqual("some-account-id");
2220
+ return res.once(
2221
+ ctx.status(200),
2222
+ ctx.json({
2223
+ success: true,
2224
+ errors: [],
2225
+ messages: [],
2226
+ result: {
2227
+ deployment_configs: { production: {}, preview: {} },
2228
+ },
2229
+ })
2230
+ );
2231
+ }
2232
+ )
2233
+ );
2234
+
2235
+ await expect(runWrangler("pages publish public --project-name=foo"))
2236
+ .rejects
2237
+ .toThrow(`Invalid _routes.json file found at: public/_routes.json
2238
+ Please make sure the JSON object has the following format:
2239
+ {
2240
+ version: ${ROUTES_SPEC_VERSION};
2241
+ include: string[];
2242
+ exclude: string[];
2243
+ }
2244
+ and that at least one include rule is provided.
2245
+ `);
2246
+ });
2247
+
2248
+ it("should ignore the entire /functions directory if _worker.js is provided", async () => {
2249
+ // set up the directory of static files to upload.
2250
+ mkdirSync("public");
2251
+ writeFileSync("public/README.md", "This is a readme");
2252
+
2253
+ // set up /functions
2254
+ mkdirSync("functions");
2255
+ writeFileSync(
2256
+ "functions/hello.js",
2257
+ `
2258
+ export async function onRequest() {
2259
+ return new Response("Hello, world!");
2260
+ }
2261
+ `
2262
+ );
2263
+
2264
+ // set up _worker.js
2265
+ writeFileSync(
2266
+ "public/_worker.js",
2267
+ `
2268
+ export default {
2269
+ async fetch(request, env) {
2270
+ const url = new URL(request.url);
2271
+ return url.pathname.startsWith('/api/') ? new Response('Ok') : env.ASSETS.fetch(request);
2272
+ }
2273
+ };
2274
+ `
2275
+ );
2276
+
2277
+ mockGetToken("<<funfetti-auth-jwt>>");
2278
+
2279
+ msw.use(
2280
+ rest.post("*/pages/assets/check-missing", async (req, res, ctx) => {
2281
+ const body = (await req.json()) as {
2282
+ hashes: string[];
2283
+ };
2284
+
2285
+ expect(req.headers.get("Authorization")).toBe(
2286
+ "Bearer <<funfetti-auth-jwt>>"
2287
+ );
2288
+ expect(body).toMatchObject({
2289
+ hashes: ["13a03eaf24ae98378acd36ea00f77f2f"],
2290
+ });
2291
+
2292
+ return res.once(
2293
+ ctx.status(200),
2294
+ ctx.json({
2295
+ success: true,
2296
+ errors: [],
2297
+ messages: [],
2298
+ result: body.hashes,
2299
+ })
2300
+ );
2301
+ }),
2302
+ rest.post("*/pages/assets/upload", async (req, res, ctx) => {
2303
+ expect(req.headers.get("Authorization")).toBe(
2304
+ "Bearer <<funfetti-auth-jwt>>"
2305
+ );
2306
+
2307
+ expect(await req.json()).toMatchObject([
2308
+ {
2309
+ key: "13a03eaf24ae98378acd36ea00f77f2f",
2310
+ value: Buffer.from("This is a readme").toString("base64"),
2311
+ metadata: {
2312
+ contentType: "text/markdown",
2313
+ },
2314
+ base64: true,
2315
+ },
2316
+ ]);
2317
+
2318
+ return res.once(
2319
+ ctx.status(200),
2320
+ ctx.json({
2321
+ success: true,
2322
+ errors: [],
2323
+ messages: [],
2324
+ result: null,
2325
+ })
2326
+ );
2327
+ }),
2328
+
2329
+ rest.post(
2330
+ "*/accounts/:accountId/pages/projects/foo/deployments",
2331
+ async (req, res, ctx) => {
2332
+ const body = await (req as RestRequestWithFormData).formData();
2333
+ const manifest = JSON.parse(body.get("manifest") as string);
2334
+ const customWorkerJS = body.get("_worker.js");
2335
+
2336
+ expect(req.params.accountId).toEqual("some-account-id");
2337
+ // make sure this is all we uploaded
2338
+ expect([...body.keys()].sort()).toEqual(
2339
+ ["manifest", "_worker.js"].sort()
2340
+ );
2341
+ expect(manifest).toMatchInlineSnapshot(`
2342
+ Object {
2343
+ "/README.md": "13a03eaf24ae98378acd36ea00f77f2f",
2344
+ }
2345
+ `);
2346
+ expect(customWorkerJS).toMatchInlineSnapshot(`
2347
+ "
2348
+ export default {
2349
+ async fetch(request, env) {
2350
+ const url = new URL(request.url);
2351
+ return url.pathname.startsWith('/api/') ? new Response('Ok') : env.ASSETS.fetch(request);
2352
+ }
2353
+ };
2354
+ "
2355
+ `);
2356
+
2357
+ return res.once(
2358
+ ctx.status(200),
2359
+ ctx.json({
2360
+ success: true,
2361
+ errors: [],
2362
+ messages: [],
2363
+ result: {
2364
+ url: "https://abcxyz.foo.pages.dev/",
2365
+ },
2366
+ })
2367
+ );
2368
+ }
2369
+ ),
2370
+ rest.get(
2371
+ "*/accounts/:accountId/pages/projects/foo",
2372
+ async (req, res, ctx) => {
2373
+ expect(req.params.accountId).toEqual("some-account-id");
2374
+
2375
+ return res.once(
2376
+ ctx.status(200),
2377
+ ctx.json({
2378
+ success: true,
2379
+ errors: [],
2380
+ messages: [],
2381
+ result: {
2382
+ deployment_configs: { production: {}, preview: {} },
2383
+ },
2384
+ })
2385
+ );
2386
+ }
2387
+ )
2388
+ );
2389
+
2390
+ await runWrangler("pages publish public --project-name=foo");
2391
+
2392
+ expect(std.out).toMatchInlineSnapshot(`
2393
+ "✨ Success! Uploaded 1 files (TIMINGS)
2394
+
2395
+ ✨ Compiled Worker successfully
2396
+ ✨ Uploading _worker.js
2397
+ ✨ Deployment complete! Take a peek over at https://abcxyz.foo.pages.dev/"
2398
+ `);
2399
+
2400
+ expect(std.err).toMatchInlineSnapshot('""');
2401
+ });
2402
+ });
2403
+
2404
+ describe("project upload", () => {
2405
+ const ENV_COPY = process.env;
2406
+
2407
+ mockAccountId();
2408
+ mockApiToken();
2409
+ runInTempDir();
2410
+
2411
+ beforeEach(() => {
2412
+ process.env.CI = "true";
2413
+ process.env.CF_PAGES_UPLOAD_JWT = "<<funfetti-auth-jwt>>";
2414
+ });
2415
+
2416
+ afterEach(() => {
2417
+ process.env = ENV_COPY;
2418
+ });
2419
+
2420
+ it("should upload a directory of files with a provided JWT", async () => {
2421
+ writeFileSync("logo.png", "foobar");
2422
+
2423
+ msw.use(
2424
+ rest.post("*/pages/assets/check-missing", async (req, res, ctx) => {
2425
+ const body = (await req.json()) as {
2426
+ hashes: string[];
2427
+ };
2428
+
2429
+ expect(req.headers.get("Authorization")).toBe(
2430
+ "Bearer <<funfetti-auth-jwt>>"
2431
+ );
2432
+ expect(body).toMatchObject({
2433
+ hashes: ["2082190357cfd3617ccfe04f340c6247"],
2434
+ });
2435
+
2436
+ return res.once(
2437
+ ctx.status(200),
2438
+ ctx.json({
2439
+ success: true,
2440
+ errors: [],
2441
+ messages: [],
2442
+ result: body.hashes,
2443
+ })
2444
+ );
2445
+ }),
2446
+ rest.post("*/pages/assets/upload", async (req, res, ctx) => {
2447
+ expect(req.headers.get("Authorization")).toBe(
2448
+ "Bearer <<funfetti-auth-jwt>>"
2449
+ );
2450
+
2451
+ expect(await req.json()).toMatchObject([
2452
+ {
2453
+ base64: true,
2454
+ key: "2082190357cfd3617ccfe04f340c6247",
2455
+ metadata: {
2456
+ contentType: "image/png",
2457
+ },
2458
+ value: "Zm9vYmFy",
2459
+ },
2460
+ ]);
2461
+
2462
+ return res(
2463
+ ctx.status(200),
2464
+ ctx.json({
2465
+ success: true,
2466
+ errors: [],
2467
+ messages: [],
2468
+ result: null,
2469
+ })
2470
+ );
2471
+ })
2472
+ );
2473
+
2474
+ await runWrangler("pages project upload .");
2475
+
2476
+ expect(std.out).toMatchInlineSnapshot(`
2477
+ "✨ Success! Uploaded 1 files (TIMINGS)
2478
+
2479
+ ✨ Upload complete!"
2480
+ `);
2481
+ });
2482
+
2483
+ it("should avoid uploading some files", async () => {
2484
+ mkdirSync("some_dir/node_modules", { recursive: true });
2485
+ mkdirSync("some_dir/functions", { recursive: true });
2486
+
2487
+ writeFileSync("logo.png", "foobar");
2488
+ writeFileSync("some_dir/functions/foo.js", "func");
2489
+ writeFileSync("some_dir/_headers", "headersfile");
2490
+
2491
+ writeFileSync("_headers", "headersfile");
2492
+ writeFileSync("_redirects", "redirectsfile");
2493
+ writeFileSync("_worker.js", "workerfile");
2494
+ writeFileSync("_routes.json", "routesfile");
2495
+ mkdirSync(".git");
2496
+ writeFileSync(".git/foo", "gitfile");
2497
+ writeFileSync("some_dir/node_modules/some_package", "nodefile");
2498
+ mkdirSync("functions");
2499
+ writeFileSync("functions/foo.js", "func");
2500
+
2501
+ // Accumulate multiple requests then assert afterwards
2502
+ const requests: RestRequest[] = [];
2503
+ msw.use(
2504
+ rest.post("*/pages/assets/check-missing", async (req, res, ctx) => {
2505
+ const body = (await req.json()) as {
2506
+ hashes: string[];
2507
+ };
2508
+
2509
+ expect(req.headers.get("Authorization")).toBe(
2510
+ "Bearer <<funfetti-auth-jwt>>"
2511
+ );
2512
+ expect(body).toMatchObject({
2513
+ hashes: [
2514
+ "2082190357cfd3617ccfe04f340c6247",
2515
+ "95dedb64e6d4940fc2e0f11f711cc2f4",
2516
+ "09a79777abda8ccc8bdd51dd3ff8e9e9",
2517
+ ],
2518
+ });
2519
+
2520
+ return res.once(
2521
+ ctx.status(200),
2522
+ ctx.json({
2523
+ success: true,
2524
+ errors: [],
2525
+ messages: [],
2526
+ result: body.hashes,
2527
+ })
2528
+ );
2529
+ }),
2530
+ rest.post("*/pages/assets/upload", (req, res, ctx) => {
2531
+ requests.push(req);
2532
+
2533
+ return res(
2534
+ ctx.status(200),
2535
+ ctx.json({
2536
+ success: true,
2537
+ errors: [],
2538
+ messages: [],
2539
+ result: null,
2540
+ })
2541
+ );
2542
+ })
2543
+ );
2544
+
2545
+ await runWrangler("pages project upload .");
2546
+ expect(requests.length).toBe(3);
2547
+
2548
+ const resolvedRequests = (
2549
+ await Promise.all(
2550
+ requests.map(async (req) => await req.json<UploadPayloadFile[]>())
2551
+ )
2552
+ ).flat();
2553
+
2554
+ const requestMap = resolvedRequests.reduce<{
2555
+ [key: string]: UploadPayloadFile;
2556
+ }>(
2557
+ (requestMap, req) => Object.assign(requestMap, { [req.key]: req }),
2558
+ {}
2559
+ );
2560
+
2561
+ for (const req of requests) {
2562
+ expect(req.headers.get("Authorization")).toBe(
2563
+ "Bearer <<funfetti-auth-jwt>>"
2564
+ );
2565
+ }
2566
+
2567
+ expect(Object.keys(requestMap).length).toBe(3);
2568
+
2569
+ expect(requestMap["95dedb64e6d4940fc2e0f11f711cc2f4"]).toMatchObject({
2570
+ base64: true,
2571
+ key: "95dedb64e6d4940fc2e0f11f711cc2f4",
2572
+ metadata: {
2573
+ contentType: "application/octet-stream",
2574
+ },
2575
+ value: "aGVhZGVyc2ZpbGU=",
2576
+ });
2577
+
2578
+ expect(requestMap["2082190357cfd3617ccfe04f340c6247"]).toMatchObject({
2579
+ base64: true,
2580
+ key: "2082190357cfd3617ccfe04f340c6247",
2581
+ metadata: {
2582
+ contentType: "image/png",
2583
+ },
2584
+ value: "Zm9vYmFy",
2585
+ });
2586
+
2587
+ expect(requestMap["09a79777abda8ccc8bdd51dd3ff8e9e9"]).toMatchObject({
2588
+ base64: true,
2589
+ key: "09a79777abda8ccc8bdd51dd3ff8e9e9",
2590
+ metadata: {
2591
+ contentType: "application/javascript",
2592
+ },
2593
+ value: "ZnVuYw==",
2594
+ });
2595
+
2596
+ expect(std.out).toMatchInlineSnapshot(`
2597
+ "✨ Success! Uploaded 3 files (TIMINGS)
2598
+
2599
+ ✨ Upload complete!"
2600
+ `);
2601
+ });
2602
+
2603
+ it("should retry uploads", async () => {
2604
+ writeFileSync("logo.txt", "foobar");
2605
+
2606
+ // Accumulate multiple requests then assert afterwards
2607
+ const requests: RestRequest[] = [];
2608
+ msw.use(
2609
+ rest.post("*/pages/assets/check-missing", async (req, res, ctx) => {
2610
+ const body = (await req.json()) as { hashes: string[] };
2611
+
2612
+ expect(req.headers.get("Authorization")).toBe(
2613
+ "Bearer <<funfetti-auth-jwt>>"
2614
+ );
2615
+ expect(body).toMatchObject({
2616
+ hashes: ["1a98fb08af91aca4a7df1764a2c4ddb0"],
2617
+ });
2618
+
2619
+ return res.once(
2620
+ ctx.status(200),
2621
+ ctx.json({
2622
+ success: true,
2623
+ errors: [],
2624
+ messages: [],
2625
+ result: body.hashes,
2626
+ })
2627
+ );
2628
+ }),
2629
+ rest.post("*/pages/assets/upload", async (req, res, ctx) => {
2630
+ requests.push(req);
2631
+
2632
+ if (requests.length < 2) {
2633
+ return res(
2634
+ ctx.status(200),
2635
+ ctx.json({
2636
+ success: false,
2637
+ errors: [
2638
+ {
2639
+ code: 800000,
2640
+ message: "Something exploded, please retry",
2641
+ },
2642
+ ],
2643
+ messages: [],
2644
+ result: null,
2645
+ })
2646
+ );
2647
+ } else {
2648
+ return res(
2649
+ ctx.status(200),
2650
+ ctx.json({
2651
+ success: true,
2652
+ errors: [],
2653
+ messages: [],
2654
+ result: null,
2655
+ })
2656
+ );
2657
+ }
2658
+ })
2659
+ );
2660
+
2661
+ await runWrangler("pages project upload .");
2662
+
2663
+ // Assert two identical requests
2664
+ expect(requests.length).toBe(2);
2665
+ for (const init of requests) {
2666
+ expect(init.headers.get("Authorization")).toBe(
2667
+ "Bearer <<funfetti-auth-jwt>>"
2668
+ );
2669
+
2670
+ const body = (await init.json()) as UploadPayloadFile[];
2671
+ expect(body).toMatchObject([
2672
+ {
2673
+ key: "1a98fb08af91aca4a7df1764a2c4ddb0",
2674
+ value: Buffer.from("foobar").toString("base64"),
2675
+ metadata: {
2676
+ contentType: "text/plain",
2677
+ },
2678
+ base64: true,
2679
+ },
2680
+ ]);
2681
+ }
2682
+
2683
+ expect(std.out).toMatchInlineSnapshot(`
2684
+ "✨ Success! Uploaded 1 files (TIMINGS)
2685
+
2686
+ ✨ Upload complete!"
2687
+ `);
2688
+ });
2689
+
2690
+ it("should try to use multiple buckets (up to the max concurrency)", async () => {
2691
+ writeFileSync("logo.txt", "foobar");
2692
+ writeFileSync("logo.png", "foobar");
2693
+ writeFileSync("logo.html", "foobar");
2694
+ writeFileSync("logo.js", "foobar");
2695
+
2696
+ mockGetToken("<<funfetti-auth-jwt>>");
2697
+
2698
+ // Accumulate multiple requests then assert afterwards
2699
+ const requests: RestRequest[] = [];
2700
+ msw.use(
2701
+ rest.post("*/pages/assets/check-missing", async (req, res, ctx) => {
2702
+ const body = (await req.json()) as { hashes: string[] };
2703
+
2704
+ expect(req.headers.get("Authorization")).toBe(
2705
+ "Bearer <<funfetti-auth-jwt>>"
2706
+ );
2707
+ expect(body).toMatchObject({
2708
+ hashes: expect.arrayContaining([
2709
+ "d96fef225537c9f5e44a3cb27fd0b492",
2710
+ "2082190357cfd3617ccfe04f340c6247",
2711
+ "6be321bef99e758250dac034474ddbb8",
2712
+ "1a98fb08af91aca4a7df1764a2c4ddb0",
2713
+ ]),
2714
+ });
2715
+
2716
+ return res.once(
2717
+ ctx.status(200),
2718
+ ctx.json({
2719
+ success: true,
2720
+ errors: [],
2721
+ messages: [],
2722
+ result: body.hashes,
2723
+ })
2724
+ );
2725
+ }),
2726
+ rest.post("*/pages/assets/upload", async (req, res, ctx) => {
2727
+ requests.push(req);
2728
+
2729
+ expect(req.headers.get("Authorization")).toBe(
2730
+ "Bearer <<funfetti-auth-jwt>>"
2731
+ );
2732
+
2733
+ return res(
2734
+ ctx.status(200),
2735
+ ctx.json({
2736
+ success: true,
2737
+ errors: [],
2738
+ messages: [],
2739
+ result: null,
2740
+ })
2741
+ );
2742
+ })
2743
+ );
2744
+
2745
+ await runWrangler("pages project upload .");
2746
+
2747
+ // We have 3 buckets, so expect 3 uploads
2748
+ expect(requests.length).toBe(3);
2749
+ const bodies: UploadPayloadFile[][] = [];
2750
+ for (const init of requests) {
2751
+ bodies.push((await init.json()) as UploadPayloadFile[]);
2752
+ }
2753
+ // One bucket should end up with 2 files
2754
+ expect(bodies.map((b) => b.length).sort()).toEqual([1, 1, 2]);
2755
+ // But we don't know the order, so flatten and test without ordering
2756
+ expect(bodies.flatMap((b) => b)).toEqual(
2757
+ expect.arrayContaining([
2758
+ {
2759
+ base64: true,
2760
+ key: "d96fef225537c9f5e44a3cb27fd0b492",
2761
+ metadata: { contentType: "text/html" },
2762
+ value: "Zm9vYmFy",
2763
+ },
2764
+ {
2765
+ base64: true,
2766
+ key: "1a98fb08af91aca4a7df1764a2c4ddb0",
2767
+ metadata: { contentType: "text/plain" },
2768
+ value: "Zm9vYmFy",
2769
+ },
2770
+ {
2771
+ base64: true,
2772
+ key: "6be321bef99e758250dac034474ddbb8",
2773
+ metadata: { contentType: "application/javascript" },
2774
+ value: "Zm9vYmFy",
2775
+ },
2776
+ {
2777
+ base64: true,
2778
+ key: "2082190357cfd3617ccfe04f340c6247",
2779
+ metadata: { contentType: "image/png" },
2780
+ value: "Zm9vYmFy",
2781
+ },
2782
+ ])
2783
+ );
2784
+
2785
+ expect(std.out).toMatchInlineSnapshot(`
2786
+ "✨ Success! Uploaded 4 files (TIMINGS)
2787
+
2788
+ ✨ Upload complete!"
2789
+ `);
2790
+ });
2791
+
2792
+ it("should not error when directory names contain periods and houses a extensionless file", async () => {
2793
+ mkdirSync(".well-known");
2794
+ // Note: same content as previous test, but since it's a different extension,
2795
+ // it hashes to a different value
2796
+ writeFileSync(".well-known/foobar", "foobar");
2797
+
2798
+ mockGetToken("<<funfetti-auth-jwt>>");
2799
+
2800
+ msw.use(
2801
+ rest.post(
2802
+ "*/pages/assets/check-missing",
2803
+
2804
+ async (req, res, ctx) => {
2805
+ const body = (await req.json()) as { hashes: string[] };
2806
+
2807
+ expect(req.headers.get("Authorization")).toBe(
2808
+ "Bearer <<funfetti-auth-jwt>>"
2809
+ );
2810
+ expect(body).toMatchObject({
2811
+ hashes: ["7b764dacfd211bebd8077828a7ddefd7"],
2812
+ });
2813
+
2814
+ return res.once(
2815
+ ctx.status(200),
2816
+ ctx.json({
2817
+ success: true,
2818
+ errors: [],
2819
+ messages: [],
2820
+ result: body.hashes,
2821
+ })
2822
+ );
2823
+ }
2824
+ ),
2825
+ rest.post("*/pages/assets/upload", async (req, res, ctx) => {
2826
+ expect(req.headers.get("Authorization")).toBe(
2827
+ "Bearer <<funfetti-auth-jwt>>"
2828
+ );
2829
+ const body = (await req.json()) as UploadPayloadFile[];
2830
+ expect(body).toMatchObject([
2831
+ {
2832
+ key: "7b764dacfd211bebd8077828a7ddefd7",
2833
+ value: Buffer.from("foobar").toString("base64"),
2834
+ metadata: {
2835
+ contentType: "application/octet-stream",
2836
+ },
2837
+ base64: true,
2838
+ },
2839
+ ]);
2840
+
2841
+ return res.once(
2842
+ ctx.status(200),
2843
+ ctx.json({
2844
+ success: true,
2845
+ errors: [],
2846
+ messages: [],
2847
+ result: null,
2848
+ })
2849
+ );
2850
+ })
2851
+ );
2852
+
2853
+ await runWrangler("pages project upload .");
2854
+
2855
+ expect(std.err).toMatchInlineSnapshot(`""`);
2856
+ });
2857
+ });
754
2858
  });
2859
+
2860
+ function mockFormDataToString(this: FormData) {
2861
+ const entries = [];
2862
+ for (const [key, value] of this.entries()) {
2863
+ if (value instanceof Blob) {
2864
+ const reader = new FileReaderSync();
2865
+ reader.readAsText(value);
2866
+ const result = reader.result;
2867
+ entries.push([key, result]);
2868
+ } else {
2869
+ entries.push([key, value]);
2870
+ }
2871
+ }
2872
+ return JSON.stringify({
2873
+ __formdata: entries,
2874
+ });
2875
+ }
2876
+
2877
+ async function mockFormDataFromString(this: MockedRequest): Promise<FormData> {
2878
+ const { __formdata } = await this.json();
2879
+ expect(__formdata).toBeInstanceOf(Array);
2880
+
2881
+ const form = new FormData();
2882
+ for (const [key, value] of __formdata) {
2883
+ form.set(key, value);
2884
+ }
2885
+ return form;
2886
+ }
2887
+
2888
+ // The following two functions workaround the fact that MSW does not yet support FormData in requests.
2889
+ // We use the fact that MSW relies upon `node-fetch` internally, which will call `toString()` on the FormData object,
2890
+ // rather than passing it through or serializing it as a proper FormData object.
2891
+ // The hack is to serialize FormData to a JSON string by overriding `FormData.toString()`.
2892
+ // And then to deserialize back to a FormData object by monkey-patching a `formData()` helper onto `MockedRequest`.
2893
+ FormData.prototype.toString = mockFormDataToString;
2894
+ export interface RestRequestWithFormData extends MockedRequest, RestRequest {
2895
+ formData(): Promise<FormData>;
2896
+ }
2897
+ (MockedRequest.prototype as RestRequestWithFormData).formData =
2898
+ mockFormDataFromString;