wrangler 0.0.0-e87ad068 → 0.0.0-e8a2a1d9

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