wrangler 0.0.0-e33bea9b → 0.0.0-e34d19867

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 (314) hide show
  1. package/README.md +33 -43
  2. package/bin/wrangler.js +16 -111
  3. package/config-schema.json +2422 -0
  4. package/package.json +182 -194
  5. package/templates/__tests__/tsconfig.json +1 -1
  6. package/templates/checked-fetch.js +11 -4
  7. package/templates/facade.d.ts +7 -2
  8. package/templates/gitignore +2 -4
  9. package/templates/init-tests/test-vitest-new-worker.js +1 -1
  10. package/templates/init-tests/test-vitest-new-worker.ts +3 -3
  11. package/templates/middleware/common.ts +9 -4
  12. package/templates/middleware/loader-modules.ts +94 -44
  13. package/templates/middleware/loader-sw.ts +6 -10
  14. package/templates/middleware/middleware-ensure-req-body-drained.ts +18 -0
  15. package/templates/middleware/middleware-patch-console-prefix.d.ts +3 -0
  16. package/templates/middleware/middleware-patch-console-prefix.ts +21 -0
  17. package/templates/middleware/middleware-scheduled.ts +15 -1
  18. package/templates/mixedMode/proxyServerWorker/index.ts +40 -0
  19. package/templates/mixedMode/proxyServerWorker/wrangler.jsonc +4 -0
  20. package/templates/modules-watch-stub.js +4 -0
  21. package/templates/new-worker-scheduled.ts +1 -1
  22. package/templates/new-worker.ts +1 -1
  23. package/templates/pages-dev-pipeline.ts +5 -3
  24. package/templates/pages-dev-util.ts +4 -1
  25. package/templates/pages-shim.ts +0 -3
  26. package/templates/pages-template-plugin.ts +14 -4
  27. package/templates/pages-template-worker.ts +12 -3
  28. package/templates/startDevWorker/InspectorProxyWorker.ts +679 -0
  29. package/templates/startDevWorker/ProxyWorker.ts +336 -0
  30. package/templates/strip-cf-connecting-ip-header.js +13 -0
  31. package/templates/tsconfig.init.json +4 -87
  32. package/templates/tsconfig.json +9 -2
  33. package/templates/tsconfig.tsbuildinfo +1 -1
  34. package/wrangler-dist/InspectorProxyWorker.js +471 -0
  35. package/wrangler-dist/InspectorProxyWorker.js.map +6 -0
  36. package/wrangler-dist/ProxyWorker.js +238 -0
  37. package/wrangler-dist/ProxyWorker.js.map +6 -0
  38. package/wrangler-dist/cli.d.ts +25620 -367
  39. package/wrangler-dist/cli.js +171822 -147792
  40. package/wrangler-dist/cli.js.map +7 -0
  41. package/Cloudflare_CA.pem +0 -18
  42. package/import_meta_url.js +0 -3
  43. package/miniflare-config-stubs/.env.empty +0 -0
  44. package/miniflare-config-stubs/package.empty.json +0 -1
  45. package/miniflare-config-stubs/wrangler.empty.toml +0 -0
  46. package/miniflare-dist/index.mjs +0 -6368
  47. package/src/__tests__/access.test.ts +0 -25
  48. package/src/__tests__/api-dev.test.ts +0 -238
  49. package/src/__tests__/api-devregistry.test.js +0 -64
  50. package/src/__tests__/api.test.ts +0 -80
  51. package/src/__tests__/config-cache-without-cache-dir.test.ts +0 -38
  52. package/src/__tests__/config-cache.test.ts +0 -42
  53. package/src/__tests__/configuration.test.ts +0 -4462
  54. package/src/__tests__/d1/d1.test.ts +0 -82
  55. package/src/__tests__/d1/execute.test.ts +0 -64
  56. package/src/__tests__/d1/migrate.test.ts +0 -155
  57. package/src/__tests__/d1/splitter.test.ts +0 -255
  58. package/src/__tests__/delete.test.ts +0 -266
  59. package/src/__tests__/deployments.test.ts +0 -129
  60. package/src/__tests__/dev.test.tsx +0 -1602
  61. package/src/__tests__/generate.test.ts +0 -238
  62. package/src/__tests__/get-host-from-url.test.ts +0 -16
  63. package/src/__tests__/guess-worker-format.test.ts +0 -120
  64. package/src/__tests__/helpers/clipboardy-mock.js +0 -4
  65. package/src/__tests__/helpers/cmd-shim.d.ts +0 -11
  66. package/src/__tests__/helpers/end-event-loop.ts +0 -6
  67. package/src/__tests__/helpers/mock-account-id.ts +0 -48
  68. package/src/__tests__/helpers/mock-auth-domain.ts +0 -20
  69. package/src/__tests__/helpers/mock-bin.ts +0 -36
  70. package/src/__tests__/helpers/mock-console.ts +0 -106
  71. package/src/__tests__/helpers/mock-dialogs.ts +0 -139
  72. package/src/__tests__/helpers/mock-get-pages-upload-token.ts +0 -25
  73. package/src/__tests__/helpers/mock-get-zone-from-host.ts +0 -11
  74. package/src/__tests__/helpers/mock-http-server.ts +0 -46
  75. package/src/__tests__/helpers/mock-istty.ts +0 -74
  76. package/src/__tests__/helpers/mock-known-routes.ts +0 -12
  77. package/src/__tests__/helpers/mock-kv.ts +0 -46
  78. package/src/__tests__/helpers/mock-oauth-flow.ts +0 -263
  79. package/src/__tests__/helpers/mock-process.ts +0 -34
  80. package/src/__tests__/helpers/mock-set-timeout.ts +0 -16
  81. package/src/__tests__/helpers/mock-stdin.ts +0 -108
  82. package/src/__tests__/helpers/mock-web-socket.ts +0 -29
  83. package/src/__tests__/helpers/msw/blob-worker.cjs +0 -19
  84. package/src/__tests__/helpers/msw/handlers/access.ts +0 -13
  85. package/src/__tests__/helpers/msw/handlers/deployments.ts +0 -93
  86. package/src/__tests__/helpers/msw/handlers/namespaces.ts +0 -81
  87. package/src/__tests__/helpers/msw/handlers/oauth.ts +0 -31
  88. package/src/__tests__/helpers/msw/handlers/r2.ts +0 -60
  89. package/src/__tests__/helpers/msw/handlers/script.ts +0 -56
  90. package/src/__tests__/helpers/msw/handlers/user.ts +0 -52
  91. package/src/__tests__/helpers/msw/handlers/zones.ts +0 -20
  92. package/src/__tests__/helpers/msw/index.ts +0 -50
  93. package/src/__tests__/helpers/msw/read-file-sync.js +0 -61
  94. package/src/__tests__/helpers/run-in-tmp.ts +0 -38
  95. package/src/__tests__/helpers/run-wrangler.ts +0 -16
  96. package/src/__tests__/helpers/string-dynamic-values-matcher.ts +0 -28
  97. package/src/__tests__/helpers/worker-scripts/child-wrangler.toml +0 -1
  98. package/src/__tests__/helpers/worker-scripts/hello-world-worker.js +0 -5
  99. package/src/__tests__/helpers/worker-scripts/hello-world-wrangler.toml +0 -1
  100. package/src/__tests__/helpers/worker-scripts/parent-worker.js +0 -8
  101. package/src/__tests__/helpers/worker-scripts/parent-wrangler.toml +0 -5
  102. package/src/__tests__/helpers/write-worker-source.ts +0 -31
  103. package/src/__tests__/helpers/write-wrangler-toml.ts +0 -17
  104. package/src/__tests__/https-options.test.ts +0 -163
  105. package/src/__tests__/index.test.ts +0 -274
  106. package/src/__tests__/init.test.ts +0 -3165
  107. package/src/__tests__/jest.setup.ts +0 -175
  108. package/src/__tests__/kv.test.ts +0 -1799
  109. package/src/__tests__/logger.test.ts +0 -141
  110. package/src/__tests__/logout.test.ts +0 -47
  111. package/src/__tests__/metrics.test.ts +0 -493
  112. package/src/__tests__/middleware.scheduled.test.ts +0 -145
  113. package/src/__tests__/middleware.test.ts +0 -816
  114. package/src/__tests__/mtls-certificates.test.ts +0 -585
  115. package/src/__tests__/package-manager.test.ts +0 -353
  116. package/src/__tests__/pages/deployment-list.test.ts +0 -78
  117. package/src/__tests__/pages/functions-build.test.ts +0 -452
  118. package/src/__tests__/pages/pages.test.ts +0 -81
  119. package/src/__tests__/pages/project-create.test.ts +0 -63
  120. package/src/__tests__/pages/project-list.test.ts +0 -108
  121. package/src/__tests__/pages/project-upload.test.ts +0 -481
  122. package/src/__tests__/pages/publish.test.ts +0 -2820
  123. package/src/__tests__/pages-deployment-tail.test.ts +0 -955
  124. package/src/__tests__/parse.test.ts +0 -436
  125. package/src/__tests__/paths.test.ts +0 -39
  126. package/src/__tests__/publish.test.ts +0 -8303
  127. package/src/__tests__/pubsub.test.ts +0 -493
  128. package/src/__tests__/queues.test.ts +0 -529
  129. package/src/__tests__/r2.test.ts +0 -374
  130. package/src/__tests__/route.test.ts +0 -45
  131. package/src/__tests__/secret.test.ts +0 -693
  132. package/src/__tests__/tail.test.ts +0 -958
  133. package/src/__tests__/test-old-node-version.js +0 -31
  134. package/src/__tests__/tsconfig-sanity.ts +0 -12
  135. package/src/__tests__/tsconfig.json +0 -8
  136. package/src/__tests__/tsconfig.tsbuildinfo +0 -1
  137. package/src/__tests__/type-generation.test.ts +0 -234
  138. package/src/__tests__/user.test.ts +0 -118
  139. package/src/__tests__/utils-collectKeyValues.test.ts +0 -47
  140. package/src/__tests__/validate-dev-props.test.ts +0 -56
  141. package/src/__tests__/version.test.ts +0 -35
  142. package/src/__tests__/whoami.test.tsx +0 -172
  143. package/src/__tests__/worker-namespace.test.ts +0 -339
  144. package/src/abort.d.ts +0 -3
  145. package/src/api/dev.ts +0 -327
  146. package/src/api/index.ts +0 -11
  147. package/src/api/mtls-certificate.ts +0 -148
  148. package/src/api/pages/create-worker-bundle-contents.ts +0 -75
  149. package/src/api/pages/index.ts +0 -5
  150. package/src/api/pages/publish.tsx +0 -355
  151. package/src/bundle-reporter.ts +0 -68
  152. package/src/bundle.ts +0 -894
  153. package/src/cfetch/index.ts +0 -158
  154. package/src/cfetch/internal.ts +0 -258
  155. package/src/cli.ts +0 -28
  156. package/src/config/README.md +0 -107
  157. package/src/config/config.ts +0 -282
  158. package/src/config/diagnostics.ts +0 -80
  159. package/src/config/environment.ts +0 -591
  160. package/src/config/index.ts +0 -385
  161. package/src/config/validation-helpers.ts +0 -597
  162. package/src/config/validation.ts +0 -2252
  163. package/src/config-cache.ts +0 -85
  164. package/src/create-worker-preview.ts +0 -293
  165. package/src/create-worker-upload-form.ts +0 -342
  166. package/src/d1/backups.tsx +0 -215
  167. package/src/d1/constants.ts +0 -2
  168. package/src/d1/create.tsx +0 -64
  169. package/src/d1/delete.ts +0 -53
  170. package/src/d1/execute.tsx +0 -354
  171. package/src/d1/formatTimeAgo.ts +0 -14
  172. package/src/d1/index.ts +0 -100
  173. package/src/d1/list.tsx +0 -60
  174. package/src/d1/migrations/apply.tsx +0 -206
  175. package/src/d1/migrations/create.tsx +0 -76
  176. package/src/d1/migrations/helpers.ts +0 -172
  177. package/src/d1/migrations/index.ts +0 -3
  178. package/src/d1/migrations/list.tsx +0 -92
  179. package/src/d1/migrations/options.ts +0 -23
  180. package/src/d1/options.ts +0 -22
  181. package/src/d1/splitter.ts +0 -161
  182. package/src/d1/types.ts +0 -25
  183. package/src/d1/utils.ts +0 -49
  184. package/src/delete.ts +0 -100
  185. package/src/deployments.ts +0 -116
  186. package/src/deprecated/index.ts +0 -144
  187. package/src/dev/dev-vars.ts +0 -39
  188. package/src/dev/dev.tsx +0 -600
  189. package/src/dev/get-local-persistence-path.ts +0 -31
  190. package/src/dev/local.tsx +0 -920
  191. package/src/dev/remote.tsx +0 -632
  192. package/src/dev/start-server.ts +0 -540
  193. package/src/dev/use-esbuild.ts +0 -209
  194. package/src/dev/validate-dev-props.ts +0 -40
  195. package/src/dev-registry.ts +0 -201
  196. package/src/dev.tsx +0 -928
  197. package/src/dialogs.ts +0 -136
  198. package/src/dispatch-namespace.ts +0 -211
  199. package/src/docs/index.ts +0 -97
  200. package/src/durable.ts +0 -102
  201. package/src/entry.ts +0 -327
  202. package/src/environment-variables/factory.ts +0 -88
  203. package/src/environment-variables/misc-variables.ts +0 -30
  204. package/src/errors.ts +0 -11
  205. package/src/generate/index.ts +0 -298
  206. package/src/git-client.ts +0 -135
  207. package/src/global-wrangler-config-path.ts +0 -26
  208. package/src/https-options.ts +0 -127
  209. package/src/index.ts +0 -705
  210. package/src/init.ts +0 -1023
  211. package/src/inspect.ts +0 -782
  212. package/src/intl-polyfill.d.ts +0 -139
  213. package/src/is-ci.ts +0 -14
  214. package/src/is-interactive.ts +0 -16
  215. package/src/jest.d.ts +0 -4
  216. package/src/kv/helpers.ts +0 -430
  217. package/src/kv/index.ts +0 -594
  218. package/src/logger.ts +0 -104
  219. package/src/metrics/index.ts +0 -5
  220. package/src/metrics/metrics-config.ts +0 -239
  221. package/src/metrics/metrics-dispatcher.ts +0 -96
  222. package/src/metrics/metrics-usage-headers.ts +0 -24
  223. package/src/metrics/send-event.ts +0 -98
  224. package/src/miniflare-cli/README.md +0 -30
  225. package/src/miniflare-cli/assets.ts +0 -251
  226. package/src/miniflare-cli/index.ts +0 -210
  227. package/src/miniflare-cli/request-context.ts +0 -40
  228. package/src/miniflare-cli/tsconfig.json +0 -9
  229. package/src/miniflare-cli/tsconfig.tsbuildinfo +0 -1
  230. package/src/miniflare-cli/types.ts +0 -11
  231. package/src/module-collection.ts +0 -267
  232. package/src/mtls-certificate/cli.ts +0 -155
  233. package/src/open-in-browser.ts +0 -17
  234. package/src/package-manager.ts +0 -219
  235. package/src/pages/build.ts +0 -410
  236. package/src/pages/buildFunctions.ts +0 -140
  237. package/src/pages/constants.ts +0 -18
  238. package/src/pages/deployment-tails.ts +0 -281
  239. package/src/pages/deployments.tsx +0 -86
  240. package/src/pages/dev.ts +0 -716
  241. package/src/pages/errors.ts +0 -67
  242. package/src/pages/functions/buildPlugin.ts +0 -112
  243. package/src/pages/functions/buildWorker.ts +0 -297
  244. package/src/pages/functions/filepath-routing.test.ts +0 -234
  245. package/src/pages/functions/filepath-routing.ts +0 -189
  246. package/src/pages/functions/identifiers.ts +0 -78
  247. package/src/pages/functions/routes-consolidation.test.ts +0 -250
  248. package/src/pages/functions/routes-consolidation.ts +0 -73
  249. package/src/pages/functions/routes-transformation.test.ts +0 -282
  250. package/src/pages/functions/routes-transformation.ts +0 -115
  251. package/src/pages/functions/routes-validation.test.ts +0 -403
  252. package/src/pages/functions/routes-validation.ts +0 -202
  253. package/src/pages/functions/routes.ts +0 -151
  254. package/src/pages/functions/tsconfig.json +0 -8
  255. package/src/pages/functions/tsconfig.tsbuildinfo +0 -1
  256. package/src/pages/functions.ts +0 -86
  257. package/src/pages/hash.ts +0 -13
  258. package/src/pages/index.ts +0 -102
  259. package/src/pages/projects.tsx +0 -162
  260. package/src/pages/prompt-select-project.tsx +0 -31
  261. package/src/pages/publish.tsx +0 -267
  262. package/src/pages/types.ts +0 -46
  263. package/src/pages/upload.tsx +0 -449
  264. package/src/pages/utils.ts +0 -23
  265. package/src/parse.ts +0 -308
  266. package/src/paths.ts +0 -71
  267. package/src/proxy.ts +0 -694
  268. package/src/publish/index.ts +0 -274
  269. package/src/publish/publish.ts +0 -1076
  270. package/src/pubsub/index.ts +0 -286
  271. package/src/pubsub/pubsub-commands.ts +0 -623
  272. package/src/queues/cli/commands/consumer/add.ts +0 -65
  273. package/src/queues/cli/commands/consumer/index.ts +0 -19
  274. package/src/queues/cli/commands/consumer/remove.ts +0 -31
  275. package/src/queues/cli/commands/create.ts +0 -25
  276. package/src/queues/cli/commands/delete.ts +0 -26
  277. package/src/queues/cli/commands/index.ts +0 -35
  278. package/src/queues/cli/commands/list.ts +0 -25
  279. package/src/queues/client.ts +0 -135
  280. package/src/queues/utils.ts +0 -18
  281. package/src/r2/constants.ts +0 -4
  282. package/src/r2/helpers.ts +0 -132
  283. package/src/r2/index.ts +0 -289
  284. package/src/routes.ts +0 -140
  285. package/src/secret/index.ts +0 -375
  286. package/src/selfsigned.d.ts +0 -29
  287. package/src/sites.ts +0 -372
  288. package/src/tail/createTail.ts +0 -406
  289. package/src/tail/filters.ts +0 -277
  290. package/src/tail/index.ts +0 -211
  291. package/src/tail/printing.ts +0 -122
  292. package/src/tsconfig-sanity.ts +0 -16
  293. package/src/type-generation.ts +0 -181
  294. package/src/update-check.ts +0 -19
  295. package/src/user/access.ts +0 -68
  296. package/src/user/auth-variables.ts +0 -113
  297. package/src/user/choose-account.tsx +0 -39
  298. package/src/user/generate-auth-url.ts +0 -33
  299. package/src/user/generate-random-state.ts +0 -16
  300. package/src/user/index.ts +0 -2
  301. package/src/user/user.ts +0 -1233
  302. package/src/utils/collectKeyValues.ts +0 -14
  303. package/src/whoami.ts +0 -135
  304. package/src/worker.ts +0 -264
  305. package/src/yargs-types.ts +0 -37
  306. package/src/zones.ts +0 -191
  307. package/templates/__tests__/tsconfig.tsbuildinfo +0 -1
  308. package/templates/d1-beta-facade.js +0 -244
  309. package/templates/first-party-worker-module-facade.ts +0 -19
  310. package/templates/format-dev-errors.ts +0 -34
  311. package/templates/serve-static-assets.ts +0 -66
  312. package/templates/service-bindings-module-facade.js +0 -55
  313. package/templates/service-bindings-sw-facade.js +0 -42
  314. package/wrangler-dist/wasm-sync.wasm +0 -0
@@ -1,2820 +0,0 @@
1
- /* eslint-disable no-shadow */
2
- import { Blob } from "node:buffer";
3
- import { mkdirSync, writeFileSync } from "node:fs";
4
- import { chdir } from "node:process";
5
- import { MockedRequest, rest } from "msw";
6
- import { FormData } from "undici";
7
- import { mockConsoleMethods } from "../helpers/mock-console";
8
- import { mockGetUploadTokenRequest } from "../helpers/mock-get-pages-upload-token";
9
- import { mockSetTimeout } from "../helpers/mock-set-timeout";
10
- import { version } from "./../../../package.json";
11
- import { ROUTES_SPEC_VERSION } from "./../../pages/constants";
12
- import { isRoutesJSONSpec } from "./../../pages/functions/routes-validation";
13
- import { endEventLoop } from "./../helpers/end-event-loop";
14
- import { mockAccountId, mockApiToken } from "./../helpers/mock-account-id";
15
- import { msw } from "./../helpers/msw";
16
- import { FileReaderSync } from "./../helpers/msw/read-file-sync";
17
- import { runInTempDir } from "./../helpers/run-in-tmp";
18
- import { runWrangler } from "./../helpers/run-wrangler";
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(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(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(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(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(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(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(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(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(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(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(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 hello.wasm
2249
- mkdirSync("wasm");
2250
- writeFileSync("wasm/hello.wasm", "HELLO WORLD");
2251
-
2252
- // set up Functions
2253
- mkdirSync("functions");
2254
- writeFileSync(
2255
- "functions/hello.js",
2256
- `
2257
- import hello from "./../wasm/hello.wasm";
2258
- export async function onRequest() {
2259
- const helloModule = await WebAssembly.instantiate(hello);
2260
- const greeting = helloModule.exports.hello;
2261
- return new Response(greeting);
2262
- }
2263
- `
2264
- );
2265
-
2266
- mockGetUploadTokenRequest(
2267
- "<<funfetti-auth-jwt>>",
2268
- "some-account-id",
2269
- "foo"
2270
- );
2271
-
2272
- msw.use(
2273
- // /pages/assets/check-missing
2274
- rest.post("*/pages/assets/check-missing", async (req, res, ctx) => {
2275
- const body = (await req.json()) as {
2276
- hashes: string[];
2277
- };
2278
-
2279
- expect(req.headers.get("Authorization")).toBe(
2280
- "Bearer <<funfetti-auth-jwt>>"
2281
- );
2282
- expect(body).toMatchObject({
2283
- hashes: ["13a03eaf24ae98378acd36ea00f77f2f"],
2284
- });
2285
-
2286
- return res.once(
2287
- ctx.status(200),
2288
- ctx.json({
2289
- success: true,
2290
- errors: [],
2291
- messages: [],
2292
- result: body.hashes,
2293
- })
2294
- );
2295
- }),
2296
-
2297
- // /pages/assets/upload
2298
- rest.post("*/pages/assets/upload", async (req, res, ctx) => {
2299
- expect(req.headers.get("Authorization")).toBe(
2300
- "Bearer <<funfetti-auth-jwt>>"
2301
- );
2302
-
2303
- expect(await req.json()).toMatchObject([
2304
- {
2305
- key: "13a03eaf24ae98378acd36ea00f77f2f",
2306
- value: Buffer.from("This is a readme").toString("base64"),
2307
- metadata: {
2308
- contentType: "text/markdown",
2309
- },
2310
- base64: true,
2311
- },
2312
- ]);
2313
-
2314
- return res.once(
2315
- ctx.status(200),
2316
- ctx.json({
2317
- success: true,
2318
- errors: [],
2319
- messages: [],
2320
- result: null,
2321
- })
2322
- );
2323
- }),
2324
-
2325
- // /accounts/:accountId/pages/projects/<project-name>/deployments
2326
- rest.post(
2327
- "*/accounts/:accountId/pages/projects/foo/deployments",
2328
- async (req, res, ctx) => {
2329
- const body = await (req as RestRequestWithFormData).formData();
2330
- const manifest = JSON.parse(body.get("manifest") as string);
2331
- const workerBundle = body.get("_worker.bundle") as string;
2332
-
2333
- expect(req.params.accountId).toEqual("some-account-id");
2334
- // make sure this is all we uploaded
2335
- expect([...body.keys()].sort()).toEqual(
2336
- [
2337
- "manifest",
2338
- "_worker.bundle",
2339
- "functions-filepath-routing-config.json",
2340
- "_routes.json",
2341
- ].sort()
2342
- );
2343
- expect(manifest).toMatchInlineSnapshot(`
2344
- Object {
2345
- "/README.md": "13a03eaf24ae98378acd36ea00f77f2f",
2346
- }
2347
- `);
2348
-
2349
- // some fields in workerBundle, such as the undici form boundary
2350
- // or the file hashes, are randomly generated. Let's replace these
2351
- // dynamic values with static ones so we can properly test the
2352
- // contents of `workerBundle`
2353
- // see https://jestjs.io/docs/snapshot-testing#property-matchers
2354
- let workerBundleWithConstantData = workerBundle.replace(
2355
- /------formdata-undici-0.[0-9]*/g,
2356
- "------formdata-undici-0.test"
2357
- );
2358
- workerBundleWithConstantData = workerBundleWithConstantData.replace(
2359
- /functionsWorker-0.[0-9]*.js/g,
2360
- "functionsWorker-0.test.js"
2361
- );
2362
- workerBundleWithConstantData = workerBundleWithConstantData.replace(
2363
- /[0-9a-z]*-hello.wasm/g,
2364
- "test-hello.wasm"
2365
- );
2366
-
2367
- // check we appended the metadata
2368
- expect(workerBundleWithConstantData).toContain(
2369
- `Content-Disposition: form-data; name="metadata"`
2370
- );
2371
- expect(workerBundleWithConstantData).toContain(
2372
- `{"main_module":"functionsWorker-0.test.js"}`
2373
- );
2374
-
2375
- // check we appended the compiled Worker
2376
- expect(workerBundleWithConstantData).toContain(
2377
- `Content-Disposition: form-data; name="functionsWorker-0.test.js"; filename="functionsWorker-0.test.js"`
2378
- );
2379
- expect(workerBundleWithConstantData).toContain(`
2380
- import hello from "./test-hello.wasm";
2381
- async function onRequest() {
2382
- const helloModule = await WebAssembly.instantiate(hello);
2383
- const greeting = helloModule.exports.hello;
2384
- return new Response(greeting);
2385
- }`);
2386
-
2387
- // check we appended the wasm module
2388
- expect(workerBundleWithConstantData).toContain(
2389
- `Content-Disposition: form-data; name="./test-hello.wasm"; filename="./test-hello.wasm"`
2390
- );
2391
- expect(workerBundleWithConstantData).toContain(`HELLO WORLD`);
2392
-
2393
- return res.once(
2394
- ctx.status(200),
2395
- ctx.json({
2396
- success: true,
2397
- errors: [],
2398
- messages: [],
2399
- result: {
2400
- url: "https://abcxyz.foo.pages.dev/",
2401
- },
2402
- })
2403
- );
2404
- }
2405
- ),
2406
-
2407
- // /accounts/:accountId/pages/projects/<project-name>
2408
- rest.get(
2409
- "*/accounts/:accountId/pages/projects/foo",
2410
- async (req, res, ctx) => {
2411
- expect(req.params.accountId).toEqual("some-account-id");
2412
-
2413
- return res.once(
2414
- ctx.status(200),
2415
- ctx.json({
2416
- success: true,
2417
- errors: [],
2418
- messages: [],
2419
- result: {
2420
- deployment_configs: { production: {}, preview: {} },
2421
- },
2422
- })
2423
- );
2424
- }
2425
- )
2426
- );
2427
-
2428
- await runWrangler("pages publish public --project-name=foo");
2429
-
2430
- expect(std.out).toMatchInlineSnapshot(`
2431
- "✨ Compiled Worker successfully
2432
- ✨ Success! Uploaded 1 files (TIMINGS)
2433
-
2434
- ✨ Uploading Functions bundle
2435
- ✨ Deployment complete! Take a peek over at https://abcxyz.foo.pages.dev/"
2436
- `);
2437
-
2438
- // make sure there were no errors
2439
- expect(std.err).toMatchInlineSnapshot('""');
2440
- });
2441
-
2442
- it("should bundle _worker.js and resolve its external module imports", async () => {
2443
- // set up the directory of static files to upload
2444
- mkdirSync("public");
2445
- writeFileSync("public/README.md", "This is a readme");
2446
-
2447
- // set up hello.wasm
2448
- mkdirSync("wasm");
2449
- writeFileSync("wasm/hello.wasm", "HELLO");
2450
-
2451
- // set up _worker.js
2452
- writeFileSync(
2453
- "public/_worker.js",
2454
- `
2455
- import hello from "./../wasm/hello.wasm";
2456
- export default {
2457
- async fetch(request, env) {
2458
- const url = new URL(request.url);
2459
- const helloModule = await WebAssembly.instantiate(hello);
2460
- const greeting = helloModule.exports.hello;
2461
- return url.pathname.startsWith('/hello') ? new Response(greeting) : env.ASSETS.fetch(request);
2462
- }
2463
- };
2464
- `
2465
- );
2466
-
2467
- mockGetUploadTokenRequest(
2468
- "<<funfetti-auth-jwt>>",
2469
- "some-account-id",
2470
- "foo"
2471
- );
2472
-
2473
- msw.use(
2474
- // /pages/assets/check-missing
2475
- rest.post("*/pages/assets/check-missing", async (req, res, ctx) => {
2476
- const body = (await req.json()) as {
2477
- hashes: string[];
2478
- };
2479
-
2480
- expect(req.headers.get("Authorization")).toBe(
2481
- "Bearer <<funfetti-auth-jwt>>"
2482
- );
2483
- expect(body).toMatchObject({
2484
- hashes: ["13a03eaf24ae98378acd36ea00f77f2f"],
2485
- });
2486
-
2487
- return res.once(
2488
- ctx.status(200),
2489
- ctx.json({
2490
- success: true,
2491
- errors: [],
2492
- messages: [],
2493
- result: body.hashes,
2494
- })
2495
- );
2496
- }),
2497
-
2498
- // /pages/assets/upload
2499
- rest.post("*/pages/assets/upload", async (req, res, ctx) => {
2500
- expect(req.headers.get("Authorization")).toBe(
2501
- "Bearer <<funfetti-auth-jwt>>"
2502
- );
2503
-
2504
- expect(await req.json()).toMatchObject([
2505
- {
2506
- key: "13a03eaf24ae98378acd36ea00f77f2f",
2507
- value: Buffer.from("This is a readme").toString("base64"),
2508
- metadata: {
2509
- contentType: "text/markdown",
2510
- },
2511
- base64: true,
2512
- },
2513
- ]);
2514
-
2515
- return res.once(
2516
- ctx.status(200),
2517
- ctx.json({
2518
- success: true,
2519
- errors: [],
2520
- messages: [],
2521
- result: null,
2522
- })
2523
- );
2524
- }),
2525
-
2526
- // /accounts/:accountId/pages/projects/<project-name>/deployments
2527
- rest.post(
2528
- "*/accounts/:accountId/pages/projects/foo/deployments",
2529
- async (req, res, ctx) => {
2530
- const body = await (req as RestRequestWithFormData).formData();
2531
- const manifest = JSON.parse(body.get("manifest") as string);
2532
- const workerBundle = body.get("_worker.bundle") as string;
2533
-
2534
- expect(req.params.accountId).toEqual("some-account-id");
2535
- // make sure this is all we uploaded
2536
- expect([...body.keys()].sort()).toEqual(
2537
- ["manifest", "_worker.bundle"].sort()
2538
- );
2539
- expect(manifest).toMatchInlineSnapshot(`
2540
- Object {
2541
- "/README.md": "13a03eaf24ae98378acd36ea00f77f2f",
2542
- }
2543
- `);
2544
- // some fields in workerBundle, such as the undici form boundary
2545
- // or the file hashes, are randomly generated. Let's replace these
2546
- // dynamic values with static ones so we can properly test the
2547
- // contents of `workerBundle`
2548
- // see https://jestjs.io/docs/snapshot-testing#property-matchers
2549
- let workerBundleWithConstantData = workerBundle.replace(
2550
- /------formdata-undici-0.[0-9]*/g,
2551
- "------formdata-undici-0.test"
2552
- );
2553
- workerBundleWithConstantData = workerBundleWithConstantData.replace(
2554
- /bundledWorker-0.[0-9]*.mjs/g,
2555
- "bundledWorker-0.test.mjs"
2556
- );
2557
- workerBundleWithConstantData = workerBundleWithConstantData.replace(
2558
- /[0-9a-z]*-hello.wasm/g,
2559
- "test-hello.wasm"
2560
- );
2561
-
2562
- // we care about a couple of things here, like the presence of `metadata`,
2563
- // `bundledWorker`, the wasm import, etc., and since `workerBundle` is
2564
- // small enough, let's go ahead and snapshot test the whole thing
2565
- expect(workerBundleWithConstantData).toMatchInlineSnapshot(`
2566
- "------formdata-undici-0.test
2567
- Content-Disposition: form-data; name=\\"metadata\\"
2568
-
2569
- {\\"main_module\\":\\"bundledWorker-0.test.mjs\\"}
2570
- ------formdata-undici-0.test
2571
- Content-Disposition: form-data; name=\\"bundledWorker-0.test.mjs\\"; filename=\\"bundledWorker-0.test.mjs\\"
2572
- Content-Type: application/javascript+module
2573
-
2574
- // _worker.js
2575
- import hello from \\"./test-hello.wasm\\";
2576
- var worker_default = {
2577
- async fetch(request, env) {
2578
- const url = new URL(request.url);
2579
- const helloModule = await WebAssembly.instantiate(hello);
2580
- const greeting = helloModule.exports.hello;
2581
- return url.pathname.startsWith(\\"/hello\\") ? new Response(greeting) : env.ASSETS.fetch(request);
2582
- }
2583
- };
2584
- export {
2585
- worker_default as default
2586
- };
2587
- //# sourceMappingURL=bundledWorker-0.test.mjs.map
2588
-
2589
- ------formdata-undici-0.test
2590
- Content-Disposition: form-data; name=\\"./test-hello.wasm\\"; filename=\\"./test-hello.wasm\\"
2591
- Content-Type: application/wasm
2592
-
2593
- HELLO
2594
- ------formdata-undici-0.test--"
2595
- `);
2596
-
2597
- return res.once(
2598
- ctx.status(200),
2599
- ctx.json({
2600
- success: true,
2601
- errors: [],
2602
- messages: [],
2603
- result: {
2604
- url: "https://abcxyz.foo.pages.dev/",
2605
- },
2606
- })
2607
- );
2608
- }
2609
- ),
2610
-
2611
- // /accounts/:accountId/pages/projects/<project-name>
2612
- rest.get(
2613
- "*/accounts/:accountId/pages/projects/foo",
2614
- async (req, res, ctx) => {
2615
- expect(req.params.accountId).toEqual("some-account-id");
2616
-
2617
- return res.once(
2618
- ctx.status(200),
2619
- ctx.json({
2620
- success: true,
2621
- errors: [],
2622
- messages: [],
2623
- result: {
2624
- deployment_configs: { production: {}, preview: {} },
2625
- },
2626
- })
2627
- );
2628
- }
2629
- )
2630
- );
2631
-
2632
- await runWrangler("pages publish public --project-name=foo");
2633
-
2634
- expect(std.out).toMatchInlineSnapshot(`
2635
- "✨ Success! Uploaded 1 files (TIMINGS)
2636
-
2637
- ✨ Compiled Worker successfully
2638
- ✨ Uploading Worker bundle
2639
- ✨ Deployment complete! Take a peek over at https://abcxyz.foo.pages.dev/"
2640
- `);
2641
-
2642
- // make sure there were no errors
2643
- expect(std.err).toMatchInlineSnapshot('""');
2644
- });
2645
-
2646
- describe("_worker.js bundling", () => {
2647
- beforeEach(() => {
2648
- mkdirSync("public");
2649
- writeFileSync(
2650
- "public/_worker.js",
2651
- `
2652
- export default {
2653
- async fetch(request, env) {
2654
- return new Response('Ok');
2655
- }
2656
- };
2657
- `
2658
- );
2659
- });
2660
-
2661
- const workerIsBundled = (contents: string) =>
2662
- contents.includes("worker_default as default");
2663
-
2664
- const simulateServer = (
2665
- generatedWorkerBundleCheck: (workerJsContent: string) => void
2666
- ) => {
2667
- mockGetUploadTokenRequest(
2668
- "<<funfetti-auth-jwt>>",
2669
- "some-account-id",
2670
- "foo"
2671
- );
2672
- msw.use(
2673
- rest.post("*/pages/assets/check-missing", async (req, res, ctx) =>
2674
- res.once(
2675
- ctx.status(200),
2676
- ctx.json({
2677
- success: true,
2678
- errors: [],
2679
- messages: [],
2680
- result: (await req.json()).hashes,
2681
- })
2682
- )
2683
- ),
2684
- rest.post("*/pages/assets/upload", async (_req, res, ctx) =>
2685
- res.once(
2686
- ctx.status(200),
2687
- ctx.json({
2688
- success: true,
2689
- errors: [],
2690
- messages: [],
2691
- result: null,
2692
- })
2693
- )
2694
- ),
2695
- rest.post(
2696
- "*/accounts/:accountId/pages/projects/foo/deployments",
2697
- async (req, res, ctx) => {
2698
- const body = await (req as RestRequestWithFormData).formData();
2699
- const generatedWorkerBundle = body.get("_worker.bundle") as string;
2700
-
2701
- generatedWorkerBundleCheck(generatedWorkerBundle);
2702
-
2703
- return res.once(
2704
- ctx.status(200),
2705
- ctx.json({
2706
- success: true,
2707
- errors: [],
2708
- messages: [],
2709
- result: {
2710
- url: "https://abcxyz.foo.pages.dev/",
2711
- },
2712
- })
2713
- );
2714
- }
2715
- ),
2716
- rest.get(
2717
- "*/accounts/:accountId/pages/projects/foo",
2718
- async (_req, res, ctx) =>
2719
- res.once(
2720
- ctx.status(200),
2721
- ctx.json({
2722
- success: true,
2723
- errors: [],
2724
- messages: [],
2725
- result: {
2726
- deployment_configs: { production: {}, preview: {} },
2727
- },
2728
- })
2729
- )
2730
- )
2731
- );
2732
- };
2733
-
2734
- it("should bundle the _worker.js when both `--bundle` and `--no-bundle` are omitted", async () => {
2735
- simulateServer((generatedWorkerJS) =>
2736
- expect(workerIsBundled(generatedWorkerJS)).toBeTruthy()
2737
- );
2738
- await runWrangler("pages publish public --project-name=foo");
2739
- expect(std.out).toContain("✨ Uploading Worker bundle");
2740
- });
2741
-
2742
- it("should not bundle the _worker.js when `--no-bundle` is set", async () => {
2743
- simulateServer((generatedWorkerJS) =>
2744
- expect(workerIsBundled(generatedWorkerJS)).toBeFalsy()
2745
- );
2746
- await runWrangler("pages publish public --project-name=foo --no-bundle");
2747
- expect(std.out).toContain("✨ Uploading Worker bundle");
2748
- });
2749
-
2750
- it("should not bundle the _worker.js when `--bundle` is set to false", async () => {
2751
- simulateServer((generatedWorkerJS) =>
2752
- expect(workerIsBundled(generatedWorkerJS)).toBeFalsy()
2753
- );
2754
- await runWrangler(
2755
- "pages publish public --project-name=foo --bundle=false"
2756
- );
2757
- expect(std.out).toContain("✨ Uploading Worker bundle");
2758
- });
2759
-
2760
- it("should bundle the _worker.js when the `--no-bundle` is set to false", async () => {
2761
- simulateServer((generatedWorkerJS) =>
2762
- expect(workerIsBundled(generatedWorkerJS)).toBeTruthy()
2763
- );
2764
- await runWrangler(
2765
- "pages publish public --no-bundle=false --project-name=foo"
2766
- );
2767
- expect(std.out).toContain("✨ Uploading Worker bundle");
2768
- });
2769
-
2770
- it("should bundle the _worker.js when the `--bundle` is set to true", async () => {
2771
- simulateServer((generatedWorkerJS) =>
2772
- expect(workerIsBundled(generatedWorkerJS)).toBeTruthy()
2773
- );
2774
- await runWrangler(
2775
- "pages publish public --bundle=true --project-name=foo"
2776
- );
2777
- expect(std.out).toContain("✨ Uploading Worker bundle");
2778
- });
2779
- });
2780
- });
2781
-
2782
- function mockFormDataToString(this: FormData) {
2783
- const entries = [];
2784
- for (const [key, value] of this.entries()) {
2785
- if (value instanceof Blob) {
2786
- const reader = new FileReaderSync();
2787
- reader.readAsText(value);
2788
- const result = reader.result;
2789
- entries.push([key, result]);
2790
- } else {
2791
- entries.push([key, value]);
2792
- }
2793
- }
2794
- return JSON.stringify({
2795
- __formdata: entries,
2796
- });
2797
- }
2798
-
2799
- async function mockFormDataFromString(this: MockedRequest): Promise<FormData> {
2800
- const { __formdata } = await this.json();
2801
- expect(__formdata).toBeInstanceOf(Array);
2802
-
2803
- const form = new FormData();
2804
- for (const [key, value] of __formdata) {
2805
- form.set(key, value);
2806
- }
2807
- return form;
2808
- }
2809
-
2810
- // The following two functions workaround the fact that MSW does not yet support FormData in requests.
2811
- // We use the fact that MSW relies upon `node-fetch` internally, which will call `toString()` on the FormData object,
2812
- // rather than passing it through or serializing it as a proper FormData object.
2813
- // The hack is to serialize FormData to a JSON string by overriding `FormData.toString()`.
2814
- // And then to deserialize back to a FormData object by monkey-patching a `formData()` helper onto `MockedRequest`.
2815
- FormData.prototype.toString = mockFormDataToString;
2816
- export interface RestRequestWithFormData extends MockedRequest, RestRequest {
2817
- formData(): Promise<FormData>;
2818
- }
2819
- (MockedRequest.prototype as RestRequestWithFormData).formData =
2820
- mockFormDataFromString;