wrangler 0.0.0-ecc8b172 → 0.0.0-ecded1af1

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 (250) hide show
  1. package/README.md +17 -19
  2. package/bin/wrangler.js +24 -93
  3. package/config-schema.json +3115 -0
  4. package/package.json +197 -190
  5. package/templates/__tests__/pages-dev-util.test.ts +128 -0
  6. package/templates/__tests__/tsconfig-sanity.ts +12 -0
  7. package/templates/__tests__/tsconfig.json +8 -0
  8. package/templates/checked-fetch.js +19 -6
  9. package/templates/facade.d.ts +19 -0
  10. package/templates/gitignore +2 -3
  11. package/templates/init-tests/test-jest-new-worker.js +3 -5
  12. package/templates/init-tests/test-vitest-new-worker.js +4 -6
  13. package/templates/init-tests/test-vitest-new-worker.ts +25 -0
  14. package/templates/middleware/common.ts +9 -4
  15. package/templates/middleware/loader-modules.ts +94 -44
  16. package/templates/middleware/loader-sw.ts +48 -32
  17. package/templates/middleware/middleware-ensure-req-body-drained.ts +18 -0
  18. package/templates/middleware/middleware-miniflare3-json-error.ts +32 -0
  19. package/templates/middleware/middleware-mock-analytics-engine.d.ts +3 -0
  20. package/templates/middleware/middleware-mock-analytics-engine.ts +30 -0
  21. package/templates/middleware/middleware-scheduled.ts +15 -1
  22. package/templates/middleware/middleware-serve-static-assets.d.ts +6 -0
  23. package/templates/middleware/middleware-serve-static-assets.ts +56 -0
  24. package/templates/modules-watch-stub.js +4 -0
  25. package/templates/new-worker-scheduled.ts +1 -1
  26. package/templates/new-worker.ts +4 -1
  27. package/templates/pages-dev-pipeline.ts +10 -13
  28. package/templates/pages-dev-util.ts +55 -0
  29. package/templates/pages-shim.ts +1 -1
  30. package/templates/pages-template-plugin.ts +37 -14
  31. package/templates/pages-template-worker.ts +33 -7
  32. package/templates/startDevWorker/InspectorProxyWorker.ts +664 -0
  33. package/templates/startDevWorker/ProxyWorker.ts +336 -0
  34. package/templates/tsconfig-sanity.ts +11 -0
  35. package/templates/tsconfig.init.json +22 -0
  36. package/templates/tsconfig.json +5 -102
  37. package/wrangler-dist/InspectorProxyWorker.js +464 -0
  38. package/wrangler-dist/InspectorProxyWorker.js.map +6 -0
  39. package/wrangler-dist/ProxyWorker.js +241 -0
  40. package/wrangler-dist/ProxyWorker.js.map +6 -0
  41. package/wrangler-dist/cli.d.ts +26095 -471
  42. package/wrangler-dist/cli.js +178062 -122008
  43. package/wrangler-dist/wasm-sync.wasm +0 -0
  44. package/Cloudflare_CA.pem +0 -18
  45. package/import_meta_url.js +0 -3
  46. package/miniflare-config-stubs/.env.empty +0 -0
  47. package/miniflare-config-stubs/package.empty.json +0 -1
  48. package/miniflare-config-stubs/wrangler.empty.toml +0 -0
  49. package/miniflare-dist/index.mjs +0 -6310
  50. package/src/__tests__/api-dev.test.ts +0 -209
  51. package/src/__tests__/api-devregistry.test.js +0 -56
  52. package/src/__tests__/api.test.ts +0 -80
  53. package/src/__tests__/config-cache-without-cache-dir.test.ts +0 -38
  54. package/src/__tests__/config-cache.test.ts +0 -42
  55. package/src/__tests__/configuration.test.ts +0 -3864
  56. package/src/__tests__/d1.test.ts +0 -70
  57. package/src/__tests__/delete.test.ts +0 -116
  58. package/src/__tests__/dev.test.tsx +0 -1357
  59. package/src/__tests__/dialogs.test.tsx +0 -40
  60. package/src/__tests__/generate.test.ts +0 -235
  61. package/src/__tests__/get-host-from-url.test.ts +0 -16
  62. package/src/__tests__/guess-worker-format.test.ts +0 -85
  63. package/src/__tests__/helpers/clipboardy-mock.js +0 -4
  64. package/src/__tests__/helpers/cmd-shim.d.ts +0 -11
  65. package/src/__tests__/helpers/mock-account-id.ts +0 -48
  66. package/src/__tests__/helpers/mock-bin.ts +0 -36
  67. package/src/__tests__/helpers/mock-cfetch.ts +0 -333
  68. package/src/__tests__/helpers/mock-console.ts +0 -106
  69. package/src/__tests__/helpers/mock-dialogs.ts +0 -153
  70. package/src/__tests__/helpers/mock-get-zone-from-host.ts +0 -8
  71. package/src/__tests__/helpers/mock-http-server.ts +0 -46
  72. package/src/__tests__/helpers/mock-istty.ts +0 -74
  73. package/src/__tests__/helpers/mock-known-routes.ts +0 -7
  74. package/src/__tests__/helpers/mock-kv.ts +0 -33
  75. package/src/__tests__/helpers/mock-oauth-flow.ts +0 -252
  76. package/src/__tests__/helpers/mock-process.ts +0 -39
  77. package/src/__tests__/helpers/mock-stdin.ts +0 -108
  78. package/src/__tests__/helpers/mock-web-socket.ts +0 -29
  79. package/src/__tests__/helpers/msw/handlers/namespaces.ts +0 -104
  80. package/src/__tests__/helpers/msw/handlers/oauth.ts +0 -36
  81. package/src/__tests__/helpers/msw/handlers/r2.ts +0 -80
  82. package/src/__tests__/helpers/msw/handlers/script.ts +0 -75
  83. package/src/__tests__/helpers/msw/handlers/user.ts +0 -63
  84. package/src/__tests__/helpers/msw/index.ts +0 -15
  85. package/src/__tests__/helpers/run-in-tmp.ts +0 -38
  86. package/src/__tests__/helpers/run-wrangler.ts +0 -16
  87. package/src/__tests__/helpers/worker-scripts/child-wrangler.toml +0 -1
  88. package/src/__tests__/helpers/worker-scripts/hello-world-worker.js +0 -5
  89. package/src/__tests__/helpers/worker-scripts/hello-world-wrangler.toml +0 -1
  90. package/src/__tests__/helpers/worker-scripts/parent-worker.js +0 -8
  91. package/src/__tests__/helpers/worker-scripts/parent-wrangler.toml +0 -5
  92. package/src/__tests__/helpers/write-worker-source.ts +0 -31
  93. package/src/__tests__/helpers/write-wrangler-toml.ts +0 -17
  94. package/src/__tests__/https-options.test.ts +0 -163
  95. package/src/__tests__/index.test.ts +0 -251
  96. package/src/__tests__/init.test.ts +0 -2816
  97. package/src/__tests__/jest.setup.ts +0 -191
  98. package/src/__tests__/kv.test.ts +0 -1675
  99. package/src/__tests__/logger.test.ts +0 -141
  100. package/src/__tests__/metrics.test.ts +0 -450
  101. package/src/__tests__/middleware.scheduled.test.ts +0 -135
  102. package/src/__tests__/middleware.test.ts +0 -726
  103. package/src/__tests__/package-manager.test.ts +0 -353
  104. package/src/__tests__/pages.test.ts +0 -2111
  105. package/src/__tests__/parse.test.ts +0 -330
  106. package/src/__tests__/paths.test.ts +0 -39
  107. package/src/__tests__/publish.test.ts +0 -7119
  108. package/src/__tests__/pubsub.test.ts +0 -367
  109. package/src/__tests__/r2.test.ts +0 -371
  110. package/src/__tests__/route.test.ts +0 -45
  111. package/src/__tests__/secret.test.ts +0 -644
  112. package/src/__tests__/tail.test.ts +0 -800
  113. package/src/__tests__/test-old-node-version.js +0 -31
  114. package/src/__tests__/user.test.ts +0 -155
  115. package/src/__tests__/utils-collectKeyValues.test.ts +0 -47
  116. package/src/__tests__/validate-dev-props.test.ts +0 -56
  117. package/src/__tests__/version.test.ts +0 -35
  118. package/src/__tests__/whoami.test.tsx +0 -278
  119. package/src/__tests__/worker-namespace.test.ts +0 -321
  120. package/src/abort.d.ts +0 -3
  121. package/src/api/dev.ts +0 -208
  122. package/src/api/index.ts +0 -2
  123. package/src/bundle-reporter.tsx +0 -29
  124. package/src/bundle.ts +0 -756
  125. package/src/cfetch/index.ts +0 -160
  126. package/src/cfetch/internal.ts +0 -263
  127. package/src/cli.ts +0 -29
  128. package/src/config/README.md +0 -107
  129. package/src/config/config.ts +0 -275
  130. package/src/config/diagnostics.ts +0 -80
  131. package/src/config/environment.ts +0 -505
  132. package/src/config/index.ts +0 -293
  133. package/src/config/validation-helpers.ts +0 -597
  134. package/src/config/validation.ts +0 -1930
  135. package/src/config-cache.ts +0 -85
  136. package/src/create-worker-preview.ts +0 -275
  137. package/src/create-worker-upload-form.ts +0 -301
  138. package/src/d1/backups.tsx +0 -212
  139. package/src/d1/create.tsx +0 -54
  140. package/src/d1/delete.tsx +0 -56
  141. package/src/d1/execute.tsx +0 -294
  142. package/src/d1/formatTimeAgo.ts +0 -14
  143. package/src/d1/index.ts +0 -75
  144. package/src/d1/list.tsx +0 -48
  145. package/src/d1/options.ts +0 -12
  146. package/src/d1/types.tsx +0 -14
  147. package/src/d1/utils.ts +0 -39
  148. package/src/delete.ts +0 -86
  149. package/src/deprecated/index.ts +0 -453
  150. package/src/dev/dev-vars.ts +0 -36
  151. package/src/dev/dev.tsx +0 -575
  152. package/src/dev/get-local-persistence-path.tsx +0 -31
  153. package/src/dev/local.tsx +0 -686
  154. package/src/dev/remote.tsx +0 -601
  155. package/src/dev/start-server.ts +0 -506
  156. package/src/dev/use-esbuild.ts +0 -197
  157. package/src/dev/validate-dev-props.ts +0 -31
  158. package/src/dev-registry.tsx +0 -201
  159. package/src/dev.tsx +0 -865
  160. package/src/dialogs.tsx +0 -163
  161. package/src/dispatch-namespace.ts +0 -206
  162. package/src/durable.ts +0 -102
  163. package/src/entry.ts +0 -329
  164. package/src/environment-variables.ts +0 -50
  165. package/src/errors.ts +0 -11
  166. package/src/git-client.ts +0 -124
  167. package/src/global-wrangler-config-path.ts +0 -26
  168. package/src/https-options.ts +0 -127
  169. package/src/index.tsx +0 -582
  170. package/src/init.ts +0 -970
  171. package/src/inspect.ts +0 -747
  172. package/src/intl-polyfill.d.ts +0 -139
  173. package/src/is-ci.ts +0 -14
  174. package/src/is-interactive.ts +0 -12
  175. package/src/kv/helpers.ts +0 -430
  176. package/src/kv/index.ts +0 -645
  177. package/src/logger.ts +0 -80
  178. package/src/metrics/index.ts +0 -5
  179. package/src/metrics/metrics-config.ts +0 -239
  180. package/src/metrics/metrics-dispatcher.ts +0 -96
  181. package/src/metrics/metrics-usage-headers.ts +0 -24
  182. package/src/metrics/send-event.ts +0 -94
  183. package/src/miniflare-cli/README.md +0 -30
  184. package/src/miniflare-cli/assets.ts +0 -215
  185. package/src/miniflare-cli/index.ts +0 -208
  186. package/src/miniflare-cli/request-context.ts +0 -40
  187. package/src/module-collection.ts +0 -267
  188. package/src/open-in-browser.ts +0 -17
  189. package/src/package-manager.ts +0 -219
  190. package/src/pages/build.tsx +0 -238
  191. package/src/pages/constants.ts +0 -16
  192. package/src/pages/deployments.tsx +0 -106
  193. package/src/pages/dev.tsx +0 -689
  194. package/src/pages/errors.ts +0 -66
  195. package/src/pages/functions/buildPlugin.ts +0 -110
  196. package/src/pages/functions/buildWorker.ts +0 -156
  197. package/src/pages/functions/filepath-routing.test.ts +0 -234
  198. package/src/pages/functions/filepath-routing.ts +0 -189
  199. package/src/pages/functions/identifiers.ts +0 -78
  200. package/src/pages/functions/routes-consolidation.test.ts +0 -250
  201. package/src/pages/functions/routes-consolidation.ts +0 -73
  202. package/src/pages/functions/routes-transformation.test.ts +0 -282
  203. package/src/pages/functions/routes-transformation.ts +0 -115
  204. package/src/pages/functions/routes-validation.test.ts +0 -403
  205. package/src/pages/functions/routes-validation.ts +0 -202
  206. package/src/pages/functions/routes.ts +0 -151
  207. package/src/pages/functions.tsx +0 -82
  208. package/src/pages/hash.tsx +0 -13
  209. package/src/pages/index.tsx +0 -94
  210. package/src/pages/projects.tsx +0 -169
  211. package/src/pages/publish.tsx +0 -436
  212. package/src/pages/types.ts +0 -49
  213. package/src/pages/upload.tsx +0 -398
  214. package/src/pages/utils.ts +0 -12
  215. package/src/parse.ts +0 -286
  216. package/src/paths.ts +0 -71
  217. package/src/proxy.ts +0 -657
  218. package/src/publish/index.ts +0 -267
  219. package/src/publish/publish.ts +0 -913
  220. package/src/pubsub/index.ts +0 -286
  221. package/src/pubsub/pubsub-commands.tsx +0 -623
  222. package/src/r2/helpers.ts +0 -131
  223. package/src/r2/index.ts +0 -277
  224. package/src/routes.ts +0 -140
  225. package/src/secret/index.ts +0 -403
  226. package/src/selfsigned.d.ts +0 -29
  227. package/src/sites.tsx +0 -372
  228. package/src/tail/createTail.ts +0 -315
  229. package/src/tail/filters.ts +0 -277
  230. package/src/tail/index.ts +0 -217
  231. package/src/tail/printing.ts +0 -107
  232. package/src/update-check.ts +0 -19
  233. package/src/user/choose-account.tsx +0 -69
  234. package/src/user/env-vars.ts +0 -46
  235. package/src/user/generate-auth-url.ts +0 -33
  236. package/src/user/generate-random-state.ts +0 -16
  237. package/src/user/index.ts +0 -3
  238. package/src/user/user.tsx +0 -1192
  239. package/src/utils/collectKeyValues.ts +0 -14
  240. package/src/whoami.tsx +0 -169
  241. package/src/worker.ts +0 -234
  242. package/src/yargs-types.ts +0 -7
  243. package/src/zones.ts +0 -165
  244. package/templates/d1-beta-facade.js +0 -174
  245. package/templates/experimental-local-cache-stubs.js +0 -27
  246. package/templates/first-party-worker-module-facade.ts +0 -18
  247. package/templates/format-dev-errors.ts +0 -32
  248. package/templates/serve-static-assets.ts +0 -63
  249. package/templates/service-bindings-module-facade.js +0 -54
  250. package/templates/service-bindings-sw-facade.js +0 -42
@@ -1,2111 +0,0 @@
1
- import { mkdirSync, writeFileSync } from "node:fs";
2
- import { chdir } from "node:process";
3
- import { ROUTES_SPEC_VERSION } from "../pages/constants";
4
- import { isRoutesJSONSpec } from "../pages/functions/routes-validation";
5
- import { version } from "./../../package.json";
6
- import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
7
- import {
8
- createFetchResult,
9
- setMockRawResponse,
10
- setMockResponse,
11
- unsetAllMocks,
12
- } from "./helpers/mock-cfetch";
13
- import { mockConsoleMethods } from "./helpers/mock-console";
14
- import { runInTempDir } from "./helpers/run-in-tmp";
15
- import { runWrangler } from "./helpers/run-wrangler";
16
- import type { Deployment, Project, UploadPayloadFile } from "../pages/types";
17
- import type { FormData, RequestInit } from "undici";
18
-
19
- // Asserting within mock responses get swallowed, so run them out-of-band
20
- const outOfBandTests: (() => void)[] = [];
21
- function assertLater(fn: () => void) {
22
- outOfBandTests.push(fn);
23
- }
24
-
25
- function mockGetToken(jwt: string) {
26
- return setMockResponse(
27
- "/accounts/:accountId/pages/projects/foo/upload-token",
28
- async ([_url, accountId]) => {
29
- assertLater(() => {
30
- expect(accountId).toEqual("some-account-id");
31
- });
32
-
33
- return { jwt };
34
- }
35
- );
36
- }
37
-
38
- describe("pages", () => {
39
- runInTempDir();
40
- const std = mockConsoleMethods();
41
- function endEventLoop() {
42
- return new Promise((resolve) => setImmediate(resolve));
43
- }
44
- beforeEach(() => {
45
- outOfBandTests.length = 0;
46
- });
47
- afterEach(() => {
48
- outOfBandTests.forEach((fn) => fn());
49
- });
50
-
51
- beforeEach(() => {
52
- // @ts-expect-error we're using a very simple setTimeout mock here
53
- jest.spyOn(global, "setTimeout").mockImplementation((fn, _period) => {
54
- setImmediate(fn);
55
- });
56
- });
57
-
58
- it("should should display a list of available subcommands, for pages with no subcommand", async () => {
59
- await runWrangler("pages");
60
- await endEventLoop();
61
-
62
- expect(std.out).toMatchInlineSnapshot(`
63
- "wrangler pages
64
-
65
- ⚡️ Configure Cloudflare Pages
66
-
67
- Commands:
68
- wrangler pages dev [directory] [-- command..] 🧑‍💻 Develop your full-stack Pages application locally
69
- wrangler pages project ⚡️ Interact with your Pages projects
70
- wrangler pages deployment 🚀 Interact with the deployments of a project
71
- wrangler pages publish [directory] 🆙 Publish a directory of static assets as a Pages deployment
72
-
73
- Flags:
74
- -c, --config Path to .toml configuration file [string]
75
- -h, --help Show help [boolean]
76
- -v, --version Show version number [boolean]
77
-
78
- 🚧 'wrangler pages <command>' is a beta command. Please report any issues to https://github.com/cloudflare/wrangler2/issues/new/choose"
79
- `);
80
- });
81
-
82
- describe("beta message for subcommands", () => {
83
- it("should display for pages:dev", async () => {
84
- await expect(
85
- runWrangler("pages dev")
86
- ).rejects.toThrowErrorMatchingInlineSnapshot(
87
- `"Must specify a directory of static assets to serve or a command to run or a proxy port."`
88
- );
89
-
90
- expect(std.out).toMatchInlineSnapshot(`
91
- "🚧 'wrangler pages <command>' is a beta command. Please report any issues to https://github.com/cloudflare/wrangler2/issues/new/choose
92
-
93
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choose"
94
- `);
95
- });
96
-
97
- it("should display for pages:functions:build", async () => {
98
- await expect(runWrangler("pages functions build")).rejects.toThrowError();
99
-
100
- expect(std.out).toMatchInlineSnapshot(`
101
- "🚧 'wrangler pages <command>' is a beta command. Please report any issues to https://github.com/cloudflare/wrangler2/issues/new/choose
102
-
103
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choose"
104
- `);
105
- });
106
-
107
- it("should display for pages:functions:optimize-routes", async () => {
108
- await expect(
109
- runWrangler(
110
- 'pages functions optimize-routes --routes-path="/build/_routes.json" --output-routes-path="/build/_optimized-routes.json"'
111
- )
112
- ).rejects.toThrowError();
113
-
114
- expect(std.out).toMatchInlineSnapshot(`
115
- "🚧 'wrangler pages <command>' is a beta command. Please report any issues to https://github.com/cloudflare/wrangler2/issues/new/choose
116
-
117
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choose"
118
- `);
119
- });
120
- });
121
-
122
- describe("project list", () => {
123
- mockAccountId();
124
- mockApiToken();
125
-
126
- afterEach(() => {
127
- unsetAllMocks();
128
- });
129
-
130
- function mockListRequest(projects: unknown[]) {
131
- const requests = { count: 0 };
132
- setMockResponse(
133
- "/accounts/:accountId/pages/projects",
134
- ([_url, accountId], init, query) => {
135
- requests.count++;
136
- const pageSize = Number(query.get("per_page"));
137
- const page = Number(query.get("page"));
138
- const expectedPageSize = 10;
139
- const expectedPage = requests.count;
140
- assertLater(() => {
141
- expect(accountId).toEqual("some-account-id");
142
- expect(pageSize).toEqual(expectedPageSize);
143
- expect(page).toEqual(expectedPage);
144
- expect(init).toEqual({});
145
- });
146
- return projects.slice((page - 1) * pageSize, page * pageSize);
147
- }
148
- );
149
- return requests;
150
- }
151
-
152
- it("should make request to list projects", async () => {
153
- const projects: Project[] = [
154
- {
155
- name: "dogs",
156
- subdomain: "docs.pages.dev",
157
- domains: ["dogs.pages.dev"],
158
- source: {
159
- type: "github",
160
- },
161
- latest_deployment: {
162
- modified_on: "2021-11-17T14:52:26.133835Z",
163
- },
164
- created_on: "2021-11-17T14:52:26.133835Z",
165
- production_branch: "main",
166
- },
167
- {
168
- name: "cats",
169
- subdomain: "cats.pages.dev",
170
- domains: ["cats.pages.dev", "kitten.com"],
171
- latest_deployment: {
172
- modified_on: "2021-11-17T14:52:26.133835Z",
173
- },
174
- created_on: "2021-11-17T14:52:26.133835Z",
175
- production_branch: "main",
176
- },
177
- ];
178
-
179
- const requests = mockListRequest(projects);
180
- await runWrangler("pages project list");
181
-
182
- expect(requests.count).toBe(1);
183
- });
184
-
185
- it("should make multiple requests for paginated results", async () => {
186
- const projects: Project[] = [];
187
- for (let i = 0; i < 15; i++) {
188
- projects.push({
189
- name: "dogs" + i,
190
- subdomain: i + "dogs.pages.dev",
191
- domains: [i + "dogs.pages.dev"],
192
- source: {
193
- type: "github",
194
- },
195
- latest_deployment: {
196
- modified_on: "2021-11-17T14:52:26.133835Z",
197
- },
198
- created_on: "2021-11-17T14:52:26.133835Z",
199
- production_branch: "main",
200
- });
201
- }
202
- const requests = mockListRequest(projects);
203
- await runWrangler("pages project list");
204
- expect(requests.count).toEqual(2);
205
- });
206
- });
207
-
208
- describe("project create", () => {
209
- mockAccountId();
210
- mockApiToken();
211
-
212
- afterEach(() => {
213
- unsetAllMocks();
214
- });
215
-
216
- it("should create a project with a production branch", async () => {
217
- setMockResponse(
218
- "/accounts/:accountId/pages/projects",
219
- ([_url, accountId], init) => {
220
- const body = JSON.parse(init.body as string);
221
- assertLater(() => {
222
- expect(accountId).toEqual("some-account-id");
223
- expect(init.method).toEqual("POST");
224
- expect(body).toEqual({
225
- name: "a-new-project",
226
- production_branch: "main",
227
- });
228
- });
229
- return {
230
- name: "a-new-project",
231
- subdomain: "a-new-project.pages.dev",
232
- production_branch: "main",
233
- };
234
- }
235
- );
236
- await runWrangler(
237
- "pages project create a-new-project --production-branch=main"
238
- );
239
- expect(std.out).toMatchInlineSnapshot(`
240
- "✨ Successfully created the 'a-new-project' project. It will be available at https://a-new-project.pages.dev/ once you create your first deployment.
241
- To deploy a folder of assets, run 'wrangler pages publish [directory]'."
242
- `);
243
- });
244
- });
245
-
246
- describe("deployment list", () => {
247
- mockAccountId();
248
- mockApiToken();
249
-
250
- afterEach(() => {
251
- unsetAllMocks();
252
- });
253
- function mockListRequest(deployments: unknown[]) {
254
- const requests = { count: 0 };
255
- setMockResponse(
256
- "/accounts/:accountId/pages/projects/:project/deployments",
257
- ([_url, accountId, project]) => {
258
- requests.count++;
259
- assertLater(() => {
260
- expect(project).toEqual("images");
261
- expect(accountId).toEqual("some-account-id");
262
- });
263
- return deployments;
264
- }
265
- );
266
- return requests;
267
- }
268
-
269
- it("should make request to list deployments", async () => {
270
- const deployments: Deployment[] = [
271
- {
272
- id: "87bbc8fe-16be-45cd-81e0-63d722e82cdf",
273
- url: "https://87bbc8fe.images.pages.dev",
274
- environment: "preview",
275
- latest_stage: {
276
- ended_on: "2021-11-17T14:52:26.133835Z",
277
- status: "success",
278
- },
279
- deployment_trigger: {
280
- metadata: {
281
- branch: "main",
282
- commit_hash: "c7649364c4cb32ad4f65b530b9424e8be5bec9d6",
283
- },
284
- },
285
- project_name: "images",
286
- },
287
- ];
288
-
289
- const requests = mockListRequest(deployments);
290
- await runWrangler("pages deployment list --project-name=images");
291
-
292
- expect(requests.count).toBe(1);
293
- });
294
- });
295
-
296
- describe("deployment create", () => {
297
- let actualProcessEnvCI: string | undefined;
298
-
299
- mockAccountId();
300
- mockApiToken();
301
- runInTempDir();
302
-
303
- beforeEach(() => {
304
- actualProcessEnvCI = process.env.CI;
305
- process.env.CI = "true";
306
- });
307
-
308
- afterEach(() => {
309
- unsetAllMocks();
310
- process.env.CI = actualProcessEnvCI;
311
- });
312
-
313
- it("should be aliased with 'wrangler pages publish'", async () => {
314
- await runWrangler("pages publish --help");
315
- await endEventLoop();
316
-
317
- expect(std.out).toMatchInlineSnapshot(`
318
- "wrangler pages publish [directory]
319
-
320
- 🆙 Publish a directory of static assets as a Pages deployment
321
-
322
- Positionals:
323
- directory The directory of static files to upload [string]
324
-
325
- Flags:
326
- -h, --help Show help [boolean]
327
- -v, --version Show version number [boolean]
328
-
329
- Options:
330
- --project-name The name of the project you want to deploy to [string]
331
- --branch The name of the branch you want to deploy to [string]
332
- --commit-hash The SHA to attach to this deployment [string]
333
- --commit-message The commit message to attach to this deployment [string]
334
- --commit-dirty Whether or not the workspace should be considered dirty for this deployment [boolean]
335
-
336
- 🚧 'wrangler pages <command>' is a beta command. Please report any issues to https://github.com/cloudflare/wrangler2/issues/new/choose"
337
- `);
338
- });
339
-
340
- it("should upload a directory of files", async () => {
341
- writeFileSync("logo.png", "foobar");
342
-
343
- mockGetToken("<<funfetti-auth-jwt>>");
344
-
345
- setMockResponse(
346
- "/pages/assets/check-missing",
347
- "POST",
348
- async (_, init) => {
349
- const body = JSON.parse(init.body as string) as { hashes: string[] };
350
- assertLater(() => {
351
- expect(init.headers).toMatchObject({
352
- Authorization: "Bearer <<funfetti-auth-jwt>>",
353
- });
354
- expect(body).toMatchObject({
355
- hashes: ["2082190357cfd3617ccfe04f340c6247"],
356
- });
357
- });
358
- return body.hashes;
359
- }
360
- );
361
-
362
- setMockResponse("/pages/assets/upload", "POST", async (_, init) => {
363
- assertLater(() => {
364
- expect(init.headers).toMatchObject({
365
- Authorization: "Bearer <<funfetti-auth-jwt>>",
366
- });
367
- const body = JSON.parse(init.body as string) as UploadPayloadFile[];
368
- expect(body).toMatchObject([
369
- {
370
- key: "2082190357cfd3617ccfe04f340c6247",
371
- value: Buffer.from("foobar").toString("base64"),
372
- metadata: {
373
- contentType: "image/png",
374
- },
375
- base64: true,
376
- },
377
- ]);
378
- });
379
- });
380
-
381
- setMockResponse(
382
- "/accounts/:accountId/pages/projects/foo/deployments",
383
- async ([_url, accountId], init) => {
384
- assertLater(() => {
385
- expect(accountId).toEqual("some-account-id");
386
- expect(init.method).toEqual("POST");
387
- const body = init.body as FormData;
388
- const manifest = JSON.parse(body.get("manifest") as string);
389
- expect(manifest).toMatchInlineSnapshot(`
390
- Object {
391
- "/logo.png": "2082190357cfd3617ccfe04f340c6247",
392
- }
393
- `);
394
- });
395
-
396
- return {
397
- url: "https://abcxyz.foo.pages.dev/",
398
- };
399
- }
400
- );
401
-
402
- await runWrangler("pages publish . --project-name=foo");
403
-
404
- expect(std.out).toMatchInlineSnapshot(`
405
- "✨ Success! Uploaded 1 files (TIMINGS)
406
-
407
- ✨ Deployment complete! Take a peek over at https://abcxyz.foo.pages.dev/"
408
- `);
409
- });
410
-
411
- it("should retry uploads", async () => {
412
- writeFileSync("logo.txt", "foobar");
413
-
414
- mockGetToken("<<funfetti-auth-jwt>>");
415
-
416
- setMockResponse(
417
- "/pages/assets/check-missing",
418
- "POST",
419
- async (_, init) => {
420
- const body = JSON.parse(init.body as string) as { hashes: string[] };
421
- assertLater(() => {
422
- expect(init.headers).toMatchObject({
423
- Authorization: "Bearer <<funfetti-auth-jwt>>",
424
- });
425
- expect(body).toMatchObject({
426
- hashes: ["1a98fb08af91aca4a7df1764a2c4ddb0"],
427
- });
428
- });
429
- return body.hashes;
430
- }
431
- );
432
-
433
- // Accumulate multiple requests then assert afterwards
434
- const requests: RequestInit[] = [];
435
- setMockRawResponse("/pages/assets/upload", "POST", async (_, init) => {
436
- requests.push(init);
437
-
438
- if (requests.length < 2) {
439
- return createFetchResult(null, false, [
440
- {
441
- code: 800000,
442
- message: "Something exploded, please retry",
443
- },
444
- ]);
445
- } else {
446
- return createFetchResult(null, true);
447
- }
448
- });
449
-
450
- setMockResponse(
451
- "/accounts/:accountId/pages/projects/foo/deployments",
452
- async ([_url, accountId], init) => {
453
- assertLater(() => {
454
- expect(accountId).toEqual("some-account-id");
455
- expect(init.method).toEqual("POST");
456
- const body = init.body as FormData;
457
- const manifest = JSON.parse(body.get("manifest") as string);
458
- expect(manifest).toMatchInlineSnapshot(`
459
- Object {
460
- "/logo.txt": "1a98fb08af91aca4a7df1764a2c4ddb0",
461
- }
462
- `);
463
- });
464
-
465
- return {
466
- url: "https://abcxyz.foo.pages.dev/",
467
- };
468
- }
469
- );
470
-
471
- await runWrangler("pages publish . --project-name=foo");
472
-
473
- // Assert two identical requests
474
- expect(requests.length).toBe(2);
475
- for (const init of requests) {
476
- assertLater(() => {
477
- expect(init.headers).toMatchObject({
478
- Authorization: "Bearer <<funfetti-auth-jwt>>",
479
- });
480
-
481
- const body = JSON.parse(init.body as string) as UploadPayloadFile[];
482
- expect(body).toMatchObject([
483
- {
484
- key: "1a98fb08af91aca4a7df1764a2c4ddb0",
485
- value: Buffer.from("foobar").toString("base64"),
486
- metadata: {
487
- contentType: "text/plain",
488
- },
489
- base64: true,
490
- },
491
- ]);
492
- });
493
- }
494
-
495
- expect(std.out).toMatchInlineSnapshot(`
496
- "✨ Success! Uploaded 1 files (TIMINGS)
497
-
498
- ✨ Deployment complete! Take a peek over at https://abcxyz.foo.pages.dev/"
499
- `);
500
- });
501
-
502
- it("should refetch a JWT if it expires while uploading", async () => {
503
- writeFileSync("logo.txt", "foobar");
504
-
505
- const cancelMockGetToken = mockGetToken("<<funfetti-auth-jwt>>");
506
-
507
- setMockResponse(
508
- "/pages/assets/check-missing",
509
- "POST",
510
- async (_, init) => {
511
- const body = JSON.parse(init.body as string) as { hashes: string[] };
512
- assertLater(() => {
513
- expect(init.headers).toMatchObject({
514
- Authorization: "Bearer <<funfetti-auth-jwt>>",
515
- });
516
- expect(body).toMatchObject({
517
- hashes: ["1a98fb08af91aca4a7df1764a2c4ddb0"],
518
- });
519
- });
520
- return body.hashes;
521
- }
522
- );
523
-
524
- // Accumulate multiple requests then assert afterwards
525
- const requests: RequestInit[] = [];
526
- setMockRawResponse("/pages/assets/upload", "POST", async (_, init) => {
527
- requests.push(init);
528
-
529
- // Fail just the first request
530
- if (requests.length < 2) {
531
- cancelMockGetToken();
532
- mockGetToken("<<funfetti-auth-jwt2>>");
533
- return createFetchResult(null, false, [
534
- {
535
- code: 8000013,
536
- message: "Authorization failed",
537
- },
538
- ]);
539
- } else {
540
- return createFetchResult(null, true);
541
- }
542
- });
543
-
544
- setMockResponse(
545
- "/accounts/:accountId/pages/projects/foo/deployments",
546
- async ([_url, accountId], init) => {
547
- assertLater(() => {
548
- expect(accountId).toEqual("some-account-id");
549
- expect(init.method).toEqual("POST");
550
- const body = init.body as FormData;
551
- const manifest = JSON.parse(body.get("manifest") as string);
552
- expect(manifest).toMatchInlineSnapshot(`
553
- Object {
554
- "/logo.txt": "1a98fb08af91aca4a7df1764a2c4ddb0",
555
- }
556
- `);
557
- });
558
-
559
- return {
560
- url: "https://abcxyz.foo.pages.dev/",
561
- };
562
- }
563
- );
564
-
565
- await runWrangler("pages publish . --project-name=foo");
566
-
567
- // Assert two requests
568
- expect(requests.length).toBe(2);
569
-
570
- expect(requests[0].headers).toMatchObject({
571
- Authorization: "Bearer <<funfetti-auth-jwt>>",
572
- });
573
-
574
- expect(requests[1].headers).toMatchObject({
575
- Authorization: "Bearer <<funfetti-auth-jwt2>>",
576
- });
577
-
578
- for (const init of requests) {
579
- const body = JSON.parse(init.body as string) as UploadPayloadFile[];
580
- expect(body).toMatchObject([
581
- {
582
- key: "1a98fb08af91aca4a7df1764a2c4ddb0",
583
- value: Buffer.from("foobar").toString("base64"),
584
- metadata: {
585
- contentType: "text/plain",
586
- },
587
- base64: true,
588
- },
589
- ]);
590
- }
591
-
592
- expect(std.out).toMatchInlineSnapshot(`
593
- "✨ Success! Uploaded 1 files (TIMINGS)
594
-
595
- ✨ Deployment complete! Take a peek over at https://abcxyz.foo.pages.dev/"
596
- `);
597
- });
598
-
599
- it("should try to use multiple buckets (up to the max concurrency)", async () => {
600
- writeFileSync("logo.txt", "foobar");
601
- writeFileSync("logo.png", "foobar");
602
- writeFileSync("logo.html", "foobar");
603
- writeFileSync("logo.js", "foobar");
604
-
605
- mockGetToken("<<funfetti-auth-jwt>>");
606
-
607
- setMockResponse(
608
- "/pages/assets/check-missing",
609
- "POST",
610
- async (_, init) => {
611
- const body = JSON.parse(init.body as string) as { hashes: string[] };
612
- assertLater(() => {
613
- expect(init.headers).toMatchObject({
614
- Authorization: "Bearer <<funfetti-auth-jwt>>",
615
- });
616
- expect(body).toMatchObject({
617
- hashes: expect.arrayContaining([
618
- "d96fef225537c9f5e44a3cb27fd0b492",
619
- "2082190357cfd3617ccfe04f340c6247",
620
- "6be321bef99e758250dac034474ddbb8",
621
- "1a98fb08af91aca4a7df1764a2c4ddb0",
622
- ]),
623
- });
624
- });
625
- return body.hashes;
626
- }
627
- );
628
-
629
- // Accumulate multiple requests then assert afterwards
630
- const requests: RequestInit[] = [];
631
- setMockResponse("/pages/assets/upload", "POST", async (_, init) => {
632
- requests.push(init);
633
- });
634
-
635
- setMockResponse(
636
- "/accounts/:accountId/pages/projects/foo/deployments",
637
- async ([_url, accountId], init) => {
638
- assertLater(() => {
639
- expect(accountId).toEqual("some-account-id");
640
- expect(init.method).toEqual("POST");
641
- const body = init.body as FormData;
642
- const manifest = JSON.parse(body.get("manifest") as string);
643
- expect(manifest).toMatchInlineSnapshot(`
644
- Object {
645
- "/logo.html": "d96fef225537c9f5e44a3cb27fd0b492",
646
- "/logo.js": "6be321bef99e758250dac034474ddbb8",
647
- "/logo.png": "2082190357cfd3617ccfe04f340c6247",
648
- "/logo.txt": "1a98fb08af91aca4a7df1764a2c4ddb0",
649
- }
650
- `);
651
- });
652
-
653
- return {
654
- url: "https://abcxyz.foo.pages.dev/",
655
- };
656
- }
657
- );
658
-
659
- await runWrangler("pages publish . --project-name=foo");
660
-
661
- // We have 3 buckets, so expect 3 uploads
662
- expect(requests.length).toBe(3);
663
- const bodies: UploadPayloadFile[][] = [];
664
- for (const init of requests) {
665
- expect(init.headers).toMatchObject({
666
- Authorization: "Bearer <<funfetti-auth-jwt>>",
667
- });
668
- bodies.push(JSON.parse(init.body as string) as UploadPayloadFile[]);
669
- }
670
- // One bucket should end up with 2 files
671
- expect(bodies.map((b) => b.length).sort()).toEqual([1, 1, 2]);
672
- // But we don't know the order, so flatten and test without ordering
673
- expect(bodies.flatMap((b) => b)).toEqual(
674
- expect.arrayContaining([
675
- {
676
- base64: true,
677
- key: "d96fef225537c9f5e44a3cb27fd0b492",
678
- metadata: { contentType: "text/html" },
679
- value: "Zm9vYmFy",
680
- },
681
- {
682
- base64: true,
683
- key: "1a98fb08af91aca4a7df1764a2c4ddb0",
684
- metadata: { contentType: "text/plain" },
685
- value: "Zm9vYmFy",
686
- },
687
- {
688
- base64: true,
689
- key: "6be321bef99e758250dac034474ddbb8",
690
- metadata: { contentType: "application/javascript" },
691
- value: "Zm9vYmFy",
692
- },
693
- {
694
- base64: true,
695
- key: "2082190357cfd3617ccfe04f340c6247",
696
- metadata: { contentType: "image/png" },
697
- value: "Zm9vYmFy",
698
- },
699
- ])
700
- );
701
-
702
- expect(std.out).toMatchInlineSnapshot(`
703
- "✨ Success! Uploaded 4 files (TIMINGS)
704
-
705
- ✨ Deployment complete! Take a peek over at https://abcxyz.foo.pages.dev/"
706
- `);
707
- });
708
-
709
- it("should resolve child directories correctly", async () => {
710
- mkdirSync("public");
711
- mkdirSync("public/imgs");
712
- writeFileSync("public/logo.txt", "foobar");
713
- writeFileSync("public/imgs/logo.png", "foobar");
714
- writeFileSync("public/logo.html", "foobar");
715
- writeFileSync("public/logo.js", "foobar");
716
-
717
- mockGetToken("<<funfetti-auth-jwt>>");
718
-
719
- setMockResponse(
720
- "/pages/assets/check-missing",
721
- "POST",
722
- async (_, init) => {
723
- const body = JSON.parse(init.body as string) as {
724
- hashes: string[];
725
- };
726
- assertLater(() => {
727
- expect(init.headers).toMatchObject({
728
- Authorization: "Bearer <<funfetti-auth-jwt>>",
729
- });
730
- expect(body).toMatchObject({
731
- hashes: expect.arrayContaining([
732
- "d96fef225537c9f5e44a3cb27fd0b492",
733
- "2082190357cfd3617ccfe04f340c6247",
734
- "6be321bef99e758250dac034474ddbb8",
735
- "1a98fb08af91aca4a7df1764a2c4ddb0",
736
- ]),
737
- });
738
- });
739
- return body.hashes;
740
- }
741
- );
742
-
743
- // Accumulate multiple requests then assert afterwards
744
- const requests: RequestInit[] = [];
745
- setMockResponse("/pages/assets/upload", "POST", async (_, init) => {
746
- requests.push(init);
747
- });
748
-
749
- setMockResponse(
750
- "/accounts/:accountId/pages/projects/foo/deployments",
751
- async ([_url, accountId], init) => {
752
- assertLater(() => {
753
- expect(accountId).toEqual("some-account-id");
754
- expect(init.method).toEqual("POST");
755
- const body = init.body as FormData;
756
- const manifest = JSON.parse(body.get("manifest") as string);
757
- expect(manifest).toMatchInlineSnapshot(`
758
- Object {
759
- "/imgs/logo.png": "2082190357cfd3617ccfe04f340c6247",
760
- "/logo.html": "d96fef225537c9f5e44a3cb27fd0b492",
761
- "/logo.js": "6be321bef99e758250dac034474ddbb8",
762
- "/logo.txt": "1a98fb08af91aca4a7df1764a2c4ddb0",
763
- }
764
- `);
765
- });
766
-
767
- return {
768
- url: "https://abcxyz.foo.pages.dev/",
769
- };
770
- }
771
- );
772
-
773
- await runWrangler(`pages publish public --project-name=foo`);
774
-
775
- // We have 3 buckets, so expect 3 uploads
776
- expect(requests.length).toBe(3);
777
- const bodies: UploadPayloadFile[][] = [];
778
- for (const init of requests) {
779
- expect(init.headers).toMatchObject({
780
- Authorization: "Bearer <<funfetti-auth-jwt>>",
781
- });
782
- bodies.push(JSON.parse(init.body as string) as UploadPayloadFile[]);
783
- }
784
- // One bucket should end up with 2 files
785
- expect(bodies.map((b) => b.length).sort()).toEqual([1, 1, 2]);
786
- // But we don't know the order, so flatten and test without ordering
787
- expect(bodies.flatMap((b) => b)).toEqual(
788
- expect.arrayContaining([
789
- {
790
- base64: true,
791
- key: "d96fef225537c9f5e44a3cb27fd0b492",
792
- metadata: { contentType: "text/html" },
793
- value: "Zm9vYmFy",
794
- },
795
- {
796
- base64: true,
797
- key: "1a98fb08af91aca4a7df1764a2c4ddb0",
798
- metadata: { contentType: "text/plain" },
799
- value: "Zm9vYmFy",
800
- },
801
- {
802
- base64: true,
803
- key: "6be321bef99e758250dac034474ddbb8",
804
- metadata: { contentType: "application/javascript" },
805
- value: "Zm9vYmFy",
806
- },
807
- {
808
- base64: true,
809
- key: "2082190357cfd3617ccfe04f340c6247",
810
- metadata: { contentType: "image/png" },
811
- value: "Zm9vYmFy",
812
- },
813
- ])
814
- );
815
-
816
- expect(std.out).toMatchInlineSnapshot(`
817
- "✨ Success! Uploaded 4 files (TIMINGS)
818
-
819
- ✨ Deployment complete! Take a peek over at https://abcxyz.foo.pages.dev/"
820
- `);
821
- });
822
-
823
- it("should resolve the current directory correctly", async () => {
824
- mkdirSync("public");
825
- mkdirSync("public/imgs");
826
- writeFileSync("public/logo.txt", "foobar");
827
- writeFileSync("public/imgs/logo.png", "foobar");
828
- writeFileSync("public/logo.html", "foobar");
829
- writeFileSync("public/logo.js", "foobar");
830
-
831
- mockGetToken("<<funfetti-auth-jwt>>");
832
-
833
- setMockResponse(
834
- "/pages/assets/check-missing",
835
- "POST",
836
- async (_, init) => {
837
- const body = JSON.parse(init.body as string) as {
838
- hashes: string[];
839
- };
840
- assertLater(() => {
841
- expect(init.headers).toMatchObject({
842
- Authorization: "Bearer <<funfetti-auth-jwt>>",
843
- });
844
- expect(body).toMatchObject({
845
- hashes: expect.arrayContaining([
846
- "d96fef225537c9f5e44a3cb27fd0b492",
847
- "2082190357cfd3617ccfe04f340c6247",
848
- "6be321bef99e758250dac034474ddbb8",
849
- "1a98fb08af91aca4a7df1764a2c4ddb0",
850
- ]),
851
- });
852
- });
853
- return body.hashes;
854
- }
855
- );
856
-
857
- // Accumulate multiple requests then assert afterwards
858
- const requests: RequestInit[] = [];
859
- setMockResponse("/pages/assets/upload", "POST", async (_, init) => {
860
- requests.push(init);
861
- });
862
-
863
- setMockResponse(
864
- "/accounts/:accountId/pages/projects/foo/deployments",
865
- async ([_url, accountId], init) => {
866
- assertLater(() => {
867
- expect(accountId).toEqual("some-account-id");
868
- expect(init.method).toEqual("POST");
869
- const body = init.body as FormData;
870
- const manifest = JSON.parse(body.get("manifest") as string);
871
- expect(manifest).toMatchInlineSnapshot(`
872
- Object {
873
- "/imgs/logo.png": "2082190357cfd3617ccfe04f340c6247",
874
- "/logo.html": "d96fef225537c9f5e44a3cb27fd0b492",
875
- "/logo.js": "6be321bef99e758250dac034474ddbb8",
876
- "/logo.txt": "1a98fb08af91aca4a7df1764a2c4ddb0",
877
- }
878
- `);
879
- });
880
-
881
- return {
882
- url: "https://abcxyz.foo.pages.dev/",
883
- };
884
- }
885
- );
886
-
887
- chdir("public");
888
- await runWrangler(`pages publish . --project-name=foo`);
889
-
890
- // We have 3 buckets, so expect 3 uploads
891
- expect(requests.length).toBe(3);
892
- const bodies: UploadPayloadFile[][] = [];
893
- for (const init of requests) {
894
- expect(init.headers).toMatchObject({
895
- Authorization: "Bearer <<funfetti-auth-jwt>>",
896
- });
897
- bodies.push(JSON.parse(init.body as string) as UploadPayloadFile[]);
898
- }
899
- // One bucket should end up with 2 files
900
- expect(bodies.map((b) => b.length).sort()).toEqual([1, 1, 2]);
901
- // But we don't know the order, so flatten and test without ordering
902
- expect(bodies.flatMap((b) => b)).toEqual(
903
- expect.arrayContaining([
904
- {
905
- base64: true,
906
- key: "d96fef225537c9f5e44a3cb27fd0b492",
907
- metadata: { contentType: "text/html" },
908
- value: "Zm9vYmFy",
909
- },
910
- {
911
- base64: true,
912
- key: "1a98fb08af91aca4a7df1764a2c4ddb0",
913
- metadata: { contentType: "text/plain" },
914
- value: "Zm9vYmFy",
915
- },
916
- {
917
- base64: true,
918
- key: "6be321bef99e758250dac034474ddbb8",
919
- metadata: { contentType: "application/javascript" },
920
- value: "Zm9vYmFy",
921
- },
922
- {
923
- base64: true,
924
- key: "2082190357cfd3617ccfe04f340c6247",
925
- metadata: { contentType: "image/png" },
926
- value: "Zm9vYmFy",
927
- },
928
- ])
929
- );
930
-
931
- expect(std.out).toMatchInlineSnapshot(`
932
- "✨ Success! Uploaded 4 files (TIMINGS)
933
-
934
- ✨ Deployment complete! Take a peek over at https://abcxyz.foo.pages.dev/"
935
- `);
936
- });
937
-
938
- it("should not error when directory names contain periods and houses a extensionless file", async () => {
939
- mkdirSync(".well-known");
940
- // Note: same content as previous test, but since it's a different extension,
941
- // it hashes to a different value
942
- writeFileSync(".well-known/foobar", "foobar");
943
-
944
- mockGetToken("<<funfetti-auth-jwt>>");
945
-
946
- setMockResponse(
947
- "/pages/assets/check-missing",
948
- "POST",
949
- async (_, init) => {
950
- const body = JSON.parse(init.body as string) as { hashes: string[] };
951
- assertLater(() => {
952
- expect(init.headers).toMatchObject({
953
- Authorization: "Bearer <<funfetti-auth-jwt>>",
954
- });
955
- expect(body).toMatchObject({
956
- hashes: ["7b764dacfd211bebd8077828a7ddefd7"],
957
- });
958
- });
959
- return body.hashes;
960
- }
961
- );
962
-
963
- setMockResponse("/pages/assets/upload", "POST", async (_, init) => {
964
- assertLater(() => {
965
- expect(init.headers).toMatchObject({
966
- Authorization: "Bearer <<funfetti-auth-jwt>>",
967
- });
968
- const body = JSON.parse(init.body as string) as UploadPayloadFile[];
969
- expect(body).toMatchObject([
970
- {
971
- key: "7b764dacfd211bebd8077828a7ddefd7",
972
- value: Buffer.from("foobar").toString("base64"),
973
- metadata: {
974
- contentType: "application/octet-stream",
975
- },
976
- base64: true,
977
- },
978
- ]);
979
- });
980
- });
981
-
982
- setMockResponse(
983
- "/accounts/:accountId/pages/projects/foo/deployments",
984
- async () => ({
985
- url: "https://abcxyz.foo.pages.dev/",
986
- })
987
- );
988
-
989
- await runWrangler("pages publish . --project-name=foo");
990
-
991
- expect(std.err).toMatchInlineSnapshot(`""`);
992
- });
993
-
994
- it("should throw an error if user attempts to use config with pages", async () => {
995
- await expect(
996
- runWrangler("pages dev --config foo.toml")
997
- ).rejects.toThrowErrorMatchingInlineSnapshot(
998
- `"Pages does not support wrangler.toml"`
999
- );
1000
- await expect(
1001
- runWrangler("pages publish --config foo.toml")
1002
- ).rejects.toThrowErrorMatchingInlineSnapshot(
1003
- `"Pages does not support wrangler.toml"`
1004
- );
1005
- });
1006
-
1007
- it("should upload a Functions project", async () => {
1008
- // set up the directory of static files to upload.
1009
- mkdirSync("public");
1010
- writeFileSync("public/README.md", "This is a readme");
1011
-
1012
- // set up /functions
1013
- mkdirSync("functions");
1014
- writeFileSync(
1015
- "functions/hello.js",
1016
- `
1017
- export async function onRequest() {
1018
- return new Response("Hello, world!");
1019
- }
1020
- `
1021
- );
1022
-
1023
- mockGetToken("<<funfetti-auth-jwt>>");
1024
-
1025
- setMockResponse(
1026
- "/pages/assets/check-missing",
1027
- "POST",
1028
- async (_, init) => {
1029
- const body = JSON.parse(init.body as string) as { hashes: string[] };
1030
- assertLater(() => {
1031
- expect(init.headers).toMatchObject({
1032
- Authorization: "Bearer <<funfetti-auth-jwt>>",
1033
- });
1034
- expect(body).toMatchObject({
1035
- hashes: ["13a03eaf24ae98378acd36ea00f77f2f"],
1036
- });
1037
- });
1038
- return body.hashes;
1039
- }
1040
- );
1041
-
1042
- setMockResponse("/pages/assets/upload", "POST", async (_, init) => {
1043
- assertLater(() => {
1044
- expect(init.headers).toMatchObject({
1045
- Authorization: "Bearer <<funfetti-auth-jwt>>",
1046
- });
1047
- const body = JSON.parse(init.body as string) as UploadPayloadFile[];
1048
- expect(body).toMatchObject([
1049
- {
1050
- key: "13a03eaf24ae98378acd36ea00f77f2f",
1051
- value: Buffer.from("This is a readme").toString("base64"),
1052
- metadata: {
1053
- contentType: "text/markdown",
1054
- },
1055
- base64: true,
1056
- },
1057
- ]);
1058
- });
1059
- });
1060
-
1061
- setMockResponse(
1062
- `/pages/assets/upsert-hashes`,
1063
- "POST",
1064
- async (_, init) => {
1065
- assertLater(() => {
1066
- expect(init.headers).toMatchObject({
1067
- Authorization: "Bearer <<funfetti-auth-jwt>>",
1068
- });
1069
- const body = JSON.parse(init.body as string) as UploadPayloadFile[];
1070
- expect(body).toMatchObject({
1071
- hashes: ["13a03eaf24ae98378acd36ea00f77f2f"],
1072
- });
1073
- });
1074
-
1075
- return Promise.resolve(true);
1076
- }
1077
- );
1078
-
1079
- setMockResponse(
1080
- "/accounts/:accountId/pages/projects/foo/deployments",
1081
- async ([_url, accountId], init) => {
1082
- assertLater(async () => {
1083
- expect(accountId).toEqual("some-account-id");
1084
- expect(init.method).toEqual("POST");
1085
- const body = init.body as FormData;
1086
- const manifest = JSON.parse(body.get("manifest") as string);
1087
-
1088
- // for Functions projects, we auto-generate a `_worker.js` and `_routes.json`
1089
- // file, based on the contents of `/functions`
1090
- const generatedWorkerJS = body.get("_worker.js") as Blob;
1091
- const generatedRoutesJSON = await (
1092
- body.get("_routes.json") as Blob
1093
- ).text();
1094
-
1095
- // make sure this is all we uploaded
1096
- expect([...body.keys()]).toEqual([
1097
- "manifest",
1098
- "_worker.js",
1099
- "_routes.json",
1100
- ]);
1101
-
1102
- expect(manifest).toMatchInlineSnapshot(`
1103
- Object {
1104
- "/README.md": "13a03eaf24ae98378acd36ea00f77f2f",
1105
- }
1106
- `);
1107
-
1108
- // the contents of the generated `_worker.js` file is pretty massive, so I don't
1109
- // think snapshot testing makes much sense here. Plus, calling
1110
- // `.toMatchInlineSnapshot()` without any arguments, in order to generate that
1111
- // snapshot value, doesn't generate anything in this case (probably because the
1112
- // file contents is too big). So for now, let's test that _worker.js was indeed
1113
- // generated and that the file size is greater than zero
1114
- expect(generatedWorkerJS).not.toBeNull();
1115
- expect(generatedWorkerJS.size).toBeGreaterThan(0);
1116
-
1117
- const maybeRoutesJSONSpec = JSON.parse(generatedRoutesJSON);
1118
- expect(isRoutesJSONSpec(maybeRoutesJSONSpec)).toBe(true);
1119
- expect(maybeRoutesJSONSpec).toMatchObject({
1120
- version: ROUTES_SPEC_VERSION,
1121
- description: `Generated by wrangler@${version}`,
1122
- include: ["/hello"],
1123
- exclude: [],
1124
- });
1125
- });
1126
-
1127
- return {
1128
- url: "https://abcxyz.foo.pages.dev/",
1129
- };
1130
- }
1131
- );
1132
-
1133
- await runWrangler("pages publish public --project-name=foo");
1134
-
1135
- expect(std.out).toMatchInlineSnapshot(`
1136
- "Compiled Worker successfully.
1137
- ✨ Success! Uploaded 1 files (TIMINGS)
1138
-
1139
- ✨ Uploading Functions
1140
- ✨ Deployment complete! Take a peek over at https://abcxyz.foo.pages.dev/"
1141
- `);
1142
-
1143
- expect(std.err).toMatchInlineSnapshot('""');
1144
- });
1145
-
1146
- it("should upload an Advanced Mode project", async () => {
1147
- // set up the directory of static files to upload.
1148
- mkdirSync("public");
1149
- writeFileSync("public/README.md", "This is a readme");
1150
-
1151
- // set up _worker.js
1152
- writeFileSync(
1153
- "public/_worker.js",
1154
- `
1155
- export default {
1156
- async fetch(request, env) {
1157
- const url = new URL(request.url);
1158
- return url.pathname.startsWith('/api/') ? new Response('Ok') : env.ASSETS.fetch(request);
1159
- };
1160
- `
1161
- );
1162
-
1163
- mockGetToken("<<funfetti-auth-jwt>>");
1164
-
1165
- setMockResponse(
1166
- "/pages/assets/check-missing",
1167
- "POST",
1168
- async (_, init) => {
1169
- const body = JSON.parse(init.body as string) as { hashes: string[] };
1170
- assertLater(() => {
1171
- expect(init.headers).toMatchObject({
1172
- Authorization: "Bearer <<funfetti-auth-jwt>>",
1173
- });
1174
- expect(body).toMatchObject({
1175
- hashes: ["13a03eaf24ae98378acd36ea00f77f2f"],
1176
- });
1177
- });
1178
- return body.hashes;
1179
- }
1180
- );
1181
-
1182
- setMockResponse("/pages/assets/upload", "POST", async (_, init) => {
1183
- assertLater(() => {
1184
- expect(init.headers).toMatchObject({
1185
- Authorization: "Bearer <<funfetti-auth-jwt>>",
1186
- });
1187
- const body = JSON.parse(init.body as string) as UploadPayloadFile[];
1188
- expect(body).toMatchObject([
1189
- {
1190
- key: "13a03eaf24ae98378acd36ea00f77f2f",
1191
- value: Buffer.from("This is a readme").toString("base64"),
1192
- metadata: {
1193
- contentType: "text/markdown",
1194
- },
1195
- base64: true,
1196
- },
1197
- ]);
1198
- });
1199
- });
1200
-
1201
- setMockResponse(
1202
- "/accounts/:accountId/pages/projects/foo/deployments",
1203
- async ([_url, accountId], init) => {
1204
- assertLater(async () => {
1205
- expect(accountId).toEqual("some-account-id");
1206
- expect(init.method).toEqual("POST");
1207
- const body = init.body as FormData;
1208
- const manifest = JSON.parse(body.get("manifest") as string);
1209
- const customWorkerJS = await (
1210
- body.get("_worker.js") as Blob
1211
- ).text();
1212
-
1213
- // make sure this is all we uploaded
1214
- expect([...body.keys()]).toEqual(["manifest", "_worker.js"]);
1215
-
1216
- expect(manifest).toMatchInlineSnapshot(`
1217
- Object {
1218
- "/README.md": "13a03eaf24ae98378acd36ea00f77f2f",
1219
- }
1220
- `);
1221
-
1222
- expect(customWorkerJS).toMatchInlineSnapshot(`
1223
- "
1224
- export default {
1225
- async fetch(request, env) {
1226
- const url = new URL(request.url);
1227
- return url.pathname.startsWith('/api/') ? new Response('Ok') : env.ASSETS.fetch(request);
1228
- };
1229
- "
1230
- `);
1231
- });
1232
-
1233
- return {
1234
- url: "https://abcxyz.foo.pages.dev/",
1235
- };
1236
- }
1237
- );
1238
-
1239
- await runWrangler("pages publish public --project-name=foo");
1240
-
1241
- expect(std.out).toMatchInlineSnapshot(`
1242
- "✨ Success! Uploaded 1 files (TIMINGS)
1243
-
1244
- ✨ Uploading _worker.js
1245
- ✨ Deployment complete! Take a peek over at https://abcxyz.foo.pages.dev/"
1246
- `);
1247
-
1248
- expect(std.err).toMatchInlineSnapshot('""');
1249
- });
1250
-
1251
- it("should upload _routes.json for Functions projects, if provided", async () => {
1252
- // set up the directory of static files to upload.
1253
- mkdirSync("public");
1254
- writeFileSync("public/README.md", "This is a readme");
1255
-
1256
- // set up /functions
1257
- mkdirSync("functions");
1258
- writeFileSync(
1259
- "functions/hello.js",
1260
- `
1261
- export async function onRequest() {
1262
- return new Response("Hello, world!");
1263
- }
1264
- `
1265
- );
1266
-
1267
- writeFileSync(
1268
- "functions/goodbye.ts",
1269
- `
1270
- export async function onRequest() {
1271
- return new Response("Bye bye!");
1272
- }
1273
- `
1274
- );
1275
-
1276
- // set up _routes.json
1277
- writeFileSync(
1278
- "public/_routes.json",
1279
- `
1280
- {
1281
- "version": ${ROUTES_SPEC_VERSION},
1282
- "description": "Custom _routes.json file",
1283
- "include": ["/hello"],
1284
- "exclude": []
1285
- }
1286
- `
1287
- );
1288
-
1289
- mockGetToken("<<funfetti-auth-jwt>>");
1290
-
1291
- setMockResponse(
1292
- "/pages/assets/check-missing",
1293
- "POST",
1294
- async (_, init) => {
1295
- const body = JSON.parse(init.body as string) as { hashes: string[] };
1296
- assertLater(() => {
1297
- expect(init.headers).toMatchObject({
1298
- Authorization: "Bearer <<funfetti-auth-jwt>>",
1299
- });
1300
- expect(body).toMatchObject({
1301
- hashes: ["13a03eaf24ae98378acd36ea00f77f2f"],
1302
- });
1303
- });
1304
- return body.hashes;
1305
- }
1306
- );
1307
-
1308
- setMockResponse("/pages/assets/upload", "POST", async (_, init) => {
1309
- assertLater(() => {
1310
- expect(init.headers).toMatchObject({
1311
- Authorization: "Bearer <<funfetti-auth-jwt>>",
1312
- });
1313
- const body = JSON.parse(init.body as string) as UploadPayloadFile[];
1314
- expect(body).toMatchObject([
1315
- {
1316
- key: "13a03eaf24ae98378acd36ea00f77f2f",
1317
- value: Buffer.from("This is a readme").toString("base64"),
1318
- metadata: {
1319
- contentType: "text/markdown",
1320
- },
1321
- base64: true,
1322
- },
1323
- ]);
1324
- });
1325
- });
1326
-
1327
- setMockResponse(
1328
- `/pages/assets/upsert-hashes`,
1329
- "POST",
1330
- async (_, init) => {
1331
- assertLater(() => {
1332
- expect(init.headers).toMatchObject({
1333
- Authorization: "Bearer <<funfetti-auth-jwt>>",
1334
- });
1335
- const body = JSON.parse(init.body as string) as UploadPayloadFile[];
1336
- expect(body).toMatchObject({
1337
- hashes: ["13a03eaf24ae98378acd36ea00f77f2f"],
1338
- });
1339
- });
1340
-
1341
- return Promise.resolve(true);
1342
- }
1343
- );
1344
-
1345
- setMockResponse(
1346
- "/accounts/:accountId/pages/projects/foo/deployments",
1347
- async ([_url, accountId], init) => {
1348
- assertLater(async () => {
1349
- expect(accountId).toEqual("some-account-id");
1350
- expect(init.method).toEqual("POST");
1351
- const body = init.body as FormData;
1352
- const manifest = JSON.parse(body.get("manifest") as string);
1353
- const generatedWorkerJS = body.get("_worker.js") as Blob;
1354
- const customRoutesJSON = await (
1355
- body.get("_routes.json") as Blob
1356
- ).text();
1357
-
1358
- // make sure this is all we uploaded
1359
- expect([...body.keys()]).toEqual([
1360
- "manifest",
1361
- "_worker.js",
1362
- "_routes.json",
1363
- ]);
1364
-
1365
- expect(manifest).toMatchInlineSnapshot(`
1366
- Object {
1367
- "/README.md": "13a03eaf24ae98378acd36ea00f77f2f",
1368
- }
1369
- `);
1370
-
1371
- // file content of generated `_worker.js` is too massive to snapshot test
1372
- expect(generatedWorkerJS).not.toBeNull();
1373
- expect(generatedWorkerJS.size).toBeGreaterThan(0);
1374
-
1375
- const customRoutes = JSON.parse(customRoutesJSON);
1376
- expect(customRoutes).toMatchObject({
1377
- version: ROUTES_SPEC_VERSION,
1378
- description: "Custom _routes.json file",
1379
- include: ["/hello"],
1380
- exclude: [],
1381
- });
1382
- });
1383
-
1384
- return {
1385
- url: "https://abcxyz.foo.pages.dev/",
1386
- };
1387
- }
1388
- );
1389
-
1390
- await runWrangler("pages publish public --project-name=foo");
1391
-
1392
- expect(std.out).toMatchInlineSnapshot(`
1393
- "Compiled Worker successfully.
1394
- ✨ Success! Uploaded 1 files (TIMINGS)
1395
-
1396
- ✨ Uploading Functions
1397
- ✨ Uploading _routes.json
1398
- ✨ Deployment complete! Take a peek over at https://abcxyz.foo.pages.dev/"
1399
- `);
1400
-
1401
- expect(std.warn).toMatchInlineSnapshot(`
1402
- "▲ [WARNING] _routes.json is an experimental feature and is subject to change. Please use with care.
1403
-
1404
- "
1405
- `);
1406
-
1407
- expect(std.err).toMatchInlineSnapshot('""');
1408
- });
1409
-
1410
- it("should not deploy Functions projects that provide an invalid custom _routes.json file", async () => {
1411
- // set up the directory of static files to upload.
1412
- mkdirSync("public");
1413
- writeFileSync("public/README.md", "This is a readme");
1414
-
1415
- // set up _routes.json
1416
- writeFileSync(
1417
- "public/_routes.json",
1418
- `
1419
- {
1420
- "description": "Custom _routes.json file",
1421
- "include": [],
1422
- "exclude": []
1423
- }
1424
- `
1425
- );
1426
-
1427
- // set up /functions
1428
- mkdirSync("functions");
1429
- writeFileSync(
1430
- "functions/hello.js",
1431
- `
1432
- export async function onRequest() {
1433
- return new Response("Hello, world!");
1434
- }
1435
- `
1436
- );
1437
-
1438
- mockGetToken("<<funfetti-auth-jwt>>");
1439
-
1440
- setMockResponse(
1441
- "/pages/assets/check-missing",
1442
- "POST",
1443
- async (_, init) => {
1444
- const body = JSON.parse(init.body as string) as { hashes: string[] };
1445
- assertLater(() => {
1446
- expect(init.headers).toMatchObject({
1447
- Authorization: "Bearer <<funfetti-auth-jwt>>",
1448
- });
1449
- expect(body).toMatchObject({
1450
- hashes: ["13a03eaf24ae98378acd36ea00f77f2f"],
1451
- });
1452
- });
1453
- return body.hashes;
1454
- }
1455
- );
1456
-
1457
- setMockResponse("/pages/assets/upload", "POST", async (_, init) => {
1458
- assertLater(() => {
1459
- expect(init.headers).toMatchObject({
1460
- Authorization: "Bearer <<funfetti-auth-jwt>>",
1461
- });
1462
- const body = JSON.parse(init.body as string) as UploadPayloadFile[];
1463
- expect(body).toMatchObject([
1464
- {
1465
- key: "13a03eaf24ae98378acd36ea00f77f2f",
1466
- value: Buffer.from("This is a readme").toString("base64"),
1467
- metadata: {
1468
- contentType: "text/markdown",
1469
- },
1470
- base64: true,
1471
- },
1472
- ]);
1473
- });
1474
- });
1475
-
1476
- await expect(runWrangler("pages publish public --project-name=foo"))
1477
- .rejects
1478
- .toThrow(`Invalid _routes.json file found at: public/_routes.json
1479
- Please make sure the JSON object has the following format:
1480
- {
1481
- version: ${ROUTES_SPEC_VERSION};
1482
- include: string[];
1483
- exclude: string[];
1484
- }
1485
- and that at least one include rule is provided.
1486
- `);
1487
- });
1488
-
1489
- it("should upload _routes.json for Advanced Mode projects, if provided", async () => {
1490
- // set up the directory of static files to upload.
1491
- mkdirSync("public");
1492
- writeFileSync("public/README.md", "This is a readme");
1493
-
1494
- // set up _routes.json
1495
- writeFileSync(
1496
- "public/_routes.json",
1497
- `
1498
- {
1499
- "version": ${ROUTES_SPEC_VERSION},
1500
- "description": "Custom _routes.json file",
1501
- "include": ["/api/*"],
1502
- "exclude": []
1503
- }
1504
- `
1505
- );
1506
-
1507
- // set up _worker.js
1508
- writeFileSync(
1509
- "public/_worker.js",
1510
- `
1511
- export default {
1512
- async fetch(request, env) {
1513
- const url = new URL(request.url);
1514
- return url.pathname.startsWith('/api/') ? new Response('Ok') : env.ASSETS.fetch(request);
1515
- };
1516
- `
1517
- );
1518
-
1519
- mockGetToken("<<funfetti-auth-jwt>>");
1520
-
1521
- setMockResponse(
1522
- "/pages/assets/check-missing",
1523
- "POST",
1524
- async (_, init) => {
1525
- const body = JSON.parse(init.body as string) as { hashes: string[] };
1526
- assertLater(() => {
1527
- expect(init.headers).toMatchObject({
1528
- Authorization: "Bearer <<funfetti-auth-jwt>>",
1529
- });
1530
- expect(body).toMatchObject({
1531
- hashes: ["13a03eaf24ae98378acd36ea00f77f2f"],
1532
- });
1533
- });
1534
- return body.hashes;
1535
- }
1536
- );
1537
-
1538
- setMockResponse("/pages/assets/upload", "POST", async (_, init) => {
1539
- assertLater(() => {
1540
- expect(init.headers).toMatchObject({
1541
- Authorization: "Bearer <<funfetti-auth-jwt>>",
1542
- });
1543
- const body = JSON.parse(init.body as string) as UploadPayloadFile[];
1544
- expect(body).toMatchObject([
1545
- {
1546
- key: "13a03eaf24ae98378acd36ea00f77f2f",
1547
- value: Buffer.from("This is a readme").toString("base64"),
1548
- metadata: {
1549
- contentType: "text/markdown",
1550
- },
1551
- base64: true,
1552
- },
1553
- ]);
1554
- });
1555
- });
1556
-
1557
- setMockResponse(
1558
- `/pages/assets/upsert-hashes`,
1559
- "POST",
1560
- async (_, init) => {
1561
- assertLater(() => {
1562
- expect(init.headers).toMatchObject({
1563
- Authorization: "Bearer <<funfetti-auth-jwt>>",
1564
- });
1565
- const body = JSON.parse(init.body as string) as UploadPayloadFile[];
1566
- expect(body).toMatchObject({
1567
- hashes: ["13a03eaf24ae98378acd36ea00f77f2f"],
1568
- });
1569
- });
1570
-
1571
- return Promise.resolve(true);
1572
- }
1573
- );
1574
-
1575
- setMockResponse(
1576
- "/accounts/:accountId/pages/projects/foo/deployments",
1577
- async ([_url, accountId], init) => {
1578
- assertLater(async () => {
1579
- expect(accountId).toEqual("some-account-id");
1580
- expect(init.method).toEqual("POST");
1581
- const body = init.body as FormData;
1582
- const manifest = JSON.parse(body.get("manifest") as string);
1583
- const customWorkerJS = await (
1584
- body.get("_worker.js") as Blob
1585
- ).text();
1586
- const customRoutesJSON = await (
1587
- body.get("_routes.json") as Blob
1588
- ).text();
1589
-
1590
- // make sure this is all we uploaded
1591
- expect([...body.keys()]).toEqual([
1592
- "manifest",
1593
- "_worker.js",
1594
- "_routes.json",
1595
- ]);
1596
-
1597
- expect(manifest).toMatchInlineSnapshot(`
1598
- Object {
1599
- "/README.md": "13a03eaf24ae98378acd36ea00f77f2f",
1600
- }
1601
- `);
1602
-
1603
- expect(customWorkerJS).toMatchInlineSnapshot(`
1604
- "
1605
- export default {
1606
- async fetch(request, env) {
1607
- const url = new URL(request.url);
1608
- return url.pathname.startsWith('/api/') ? new Response('Ok') : env.ASSETS.fetch(request);
1609
- };
1610
- "
1611
- `);
1612
-
1613
- const customRoutes = JSON.parse(customRoutesJSON);
1614
- expect(customRoutes).toMatchObject({
1615
- version: ROUTES_SPEC_VERSION,
1616
- description: "Custom _routes.json file",
1617
- include: ["/api/*"],
1618
- exclude: [],
1619
- });
1620
- });
1621
-
1622
- return {
1623
- url: "https://abcxyz.foo.pages.dev/",
1624
- };
1625
- }
1626
- );
1627
-
1628
- await runWrangler("pages publish public --project-name=foo");
1629
-
1630
- expect(std.out).toMatchInlineSnapshot(`
1631
- "✨ Success! Uploaded 1 files (TIMINGS)
1632
-
1633
- ✨ Uploading _worker.js
1634
- ✨ Uploading _routes.json
1635
- ✨ Deployment complete! Take a peek over at https://abcxyz.foo.pages.dev/"
1636
- `);
1637
-
1638
- expect(std.warn).toMatchInlineSnapshot(`
1639
- "▲ [WARNING] _routes.json is an experimental feature and is subject to change. Please use with care.
1640
-
1641
- "
1642
- `);
1643
- expect(std.err).toMatchInlineSnapshot(`""`);
1644
- });
1645
-
1646
- it("should not deploy Advanced Mode projects that provide an invalid _routes.json file", async () => {
1647
- // set up the directory of static files to upload.
1648
- mkdirSync("public");
1649
- writeFileSync("public/README.md", "This is a readme");
1650
-
1651
- // set up _routes.json
1652
- writeFileSync(
1653
- "public/_routes.json",
1654
- `
1655
- {
1656
- "description": "Custom _routes.json file",
1657
- "include": [],
1658
- "exclude": []
1659
- }
1660
- `
1661
- );
1662
-
1663
- // set up _worker.js
1664
- writeFileSync(
1665
- "public/_worker.js",
1666
- `
1667
- export default {
1668
- async fetch(request, env) {
1669
- const url = new URL(request.url);
1670
- return url.pathname.startsWith('/api/') ? new Response('Ok') : env.ASSETS.fetch(request);
1671
- };
1672
- `
1673
- );
1674
-
1675
- mockGetToken("<<funfetti-auth-jwt>>");
1676
-
1677
- setMockResponse(
1678
- "/pages/assets/check-missing",
1679
- "POST",
1680
- async (_, init) => {
1681
- const body = JSON.parse(init.body as string) as { hashes: string[] };
1682
- assertLater(() => {
1683
- expect(init.headers).toMatchObject({
1684
- Authorization: "Bearer <<funfetti-auth-jwt>>",
1685
- });
1686
- expect(body).toMatchObject({
1687
- hashes: ["13a03eaf24ae98378acd36ea00f77f2f"],
1688
- });
1689
- });
1690
- return body.hashes;
1691
- }
1692
- );
1693
-
1694
- setMockResponse("/pages/assets/upload", "POST", async (_, init) => {
1695
- assertLater(() => {
1696
- expect(init.headers).toMatchObject({
1697
- Authorization: "Bearer <<funfetti-auth-jwt>>",
1698
- });
1699
- const body = JSON.parse(init.body as string) as UploadPayloadFile[];
1700
- expect(body).toMatchObject([
1701
- {
1702
- key: "13a03eaf24ae98378acd36ea00f77f2f",
1703
- value: Buffer.from("This is a readme").toString("base64"),
1704
- metadata: {
1705
- contentType: "text/markdown",
1706
- },
1707
- base64: true,
1708
- },
1709
- ]);
1710
- });
1711
- });
1712
-
1713
- await expect(runWrangler("pages publish public --project-name=foo"))
1714
- .rejects
1715
- .toThrow(`Invalid _routes.json file found at: public/_routes.json
1716
- Please make sure the JSON object has the following format:
1717
- {
1718
- version: ${ROUTES_SPEC_VERSION};
1719
- include: string[];
1720
- exclude: string[];
1721
- }
1722
- and that at least one include rule is provided.
1723
- `);
1724
- });
1725
-
1726
- it("should ignore the entire /functions directory if _worker.js is provided", async () => {
1727
- // set up the directory of static files to upload.
1728
- mkdirSync("public");
1729
- writeFileSync("public/README.md", "This is a readme");
1730
-
1731
- // set up /functions
1732
- mkdirSync("functions");
1733
- writeFileSync(
1734
- "functions/hello.js",
1735
- `
1736
- export async function onRequest() {
1737
- return new Response("Hello, world!");
1738
- }
1739
- `
1740
- );
1741
-
1742
- // set up _worker.js
1743
- writeFileSync(
1744
- "public/_worker.js",
1745
- `
1746
- export default {
1747
- async fetch(request, env) {
1748
- const url = new URL(request.url);
1749
- return url.pathname.startsWith('/api/') ? new Response('Ok') : env.ASSETS.fetch(request);
1750
- };
1751
- `
1752
- );
1753
-
1754
- mockGetToken("<<funfetti-auth-jwt>>");
1755
-
1756
- setMockResponse(
1757
- "/pages/assets/check-missing",
1758
- "POST",
1759
- async (_, init) => {
1760
- const body = JSON.parse(init.body as string) as { hashes: string[] };
1761
- assertLater(() => {
1762
- expect(init.headers).toMatchObject({
1763
- Authorization: "Bearer <<funfetti-auth-jwt>>",
1764
- });
1765
- expect(body).toMatchObject({
1766
- hashes: ["13a03eaf24ae98378acd36ea00f77f2f"],
1767
- });
1768
- });
1769
- return body.hashes;
1770
- }
1771
- );
1772
-
1773
- setMockResponse("/pages/assets/upload", "POST", async (_, init) => {
1774
- assertLater(() => {
1775
- expect(init.headers).toMatchObject({
1776
- Authorization: "Bearer <<funfetti-auth-jwt>>",
1777
- });
1778
- const body = JSON.parse(init.body as string) as UploadPayloadFile[];
1779
- expect(body).toMatchObject([
1780
- {
1781
- key: "13a03eaf24ae98378acd36ea00f77f2f",
1782
- value: Buffer.from("This is a readme").toString("base64"),
1783
- metadata: {
1784
- contentType: "text/markdown",
1785
- },
1786
- base64: true,
1787
- },
1788
- ]);
1789
- });
1790
- });
1791
-
1792
- setMockResponse(
1793
- "/accounts/:accountId/pages/projects/foo/deployments",
1794
- async ([_url, accountId], init) => {
1795
- assertLater(async () => {
1796
- expect(accountId).toEqual("some-account-id");
1797
- expect(init.method).toEqual("POST");
1798
- const body = init.body as FormData;
1799
- const manifest = JSON.parse(body.get("manifest") as string);
1800
- const customWorkerJS = await (
1801
- body.get("_worker.js") as Blob
1802
- ).text();
1803
-
1804
- // make sure this is all we uploaded
1805
- expect([...body.keys()]).toEqual(["manifest", "_worker.js"]);
1806
-
1807
- expect(manifest).toMatchInlineSnapshot(`
1808
- Object {
1809
- "/README.md": "13a03eaf24ae98378acd36ea00f77f2f",
1810
- }
1811
- `);
1812
-
1813
- expect(customWorkerJS).toMatchInlineSnapshot(`
1814
- "
1815
- export default {
1816
- async fetch(request, env) {
1817
- const url = new URL(request.url);
1818
- return url.pathname.startsWith('/api/') ? new Response('Ok') : env.ASSETS.fetch(request);
1819
- };
1820
- "
1821
- `);
1822
- });
1823
-
1824
- return {
1825
- url: "https://abcxyz.foo.pages.dev/",
1826
- };
1827
- }
1828
- );
1829
-
1830
- await runWrangler("pages publish public --project-name=foo");
1831
-
1832
- expect(std.out).toMatchInlineSnapshot(`
1833
- "✨ Success! Uploaded 1 files (TIMINGS)
1834
-
1835
- ✨ Uploading _worker.js
1836
- ✨ Deployment complete! Take a peek over at https://abcxyz.foo.pages.dev/"
1837
- `);
1838
-
1839
- expect(std.err).toMatchInlineSnapshot('""');
1840
- });
1841
- });
1842
-
1843
- describe("project upload", () => {
1844
- const ENV_COPY = process.env;
1845
-
1846
- mockAccountId();
1847
- mockApiToken();
1848
- runInTempDir();
1849
-
1850
- beforeEach(() => {
1851
- process.env.CI = "true";
1852
- process.env.CF_PAGES_UPLOAD_JWT = "<<funfetti-auth-jwt>>";
1853
- });
1854
-
1855
- afterEach(() => {
1856
- unsetAllMocks();
1857
- process.env = ENV_COPY;
1858
- });
1859
-
1860
- it("should upload a directory of files with a provided JWT", async () => {
1861
- writeFileSync("logo.png", "foobar");
1862
-
1863
- setMockResponse(
1864
- "/pages/assets/check-missing",
1865
- "POST",
1866
- async (_, init) => {
1867
- const body = JSON.parse(init.body as string) as { hashes: string[] };
1868
- assertLater(() => {
1869
- expect(init.headers).toMatchObject({
1870
- Authorization: "Bearer <<funfetti-auth-jwt>>",
1871
- });
1872
- expect(body).toMatchObject({
1873
- hashes: ["2082190357cfd3617ccfe04f340c6247"],
1874
- });
1875
- });
1876
- return body.hashes;
1877
- }
1878
- );
1879
-
1880
- setMockResponse("/pages/assets/upload", "POST", async (_, init) => {
1881
- assertLater(() => {
1882
- expect(init.headers).toMatchObject({
1883
- Authorization: "Bearer <<funfetti-auth-jwt>>",
1884
- });
1885
- const body = JSON.parse(init.body as string) as UploadPayloadFile[];
1886
- expect(body).toMatchObject([
1887
- {
1888
- key: "2082190357cfd3617ccfe04f340c6247",
1889
- value: Buffer.from("foobar").toString("base64"),
1890
- metadata: {
1891
- contentType: "image/png",
1892
- },
1893
- base64: true,
1894
- },
1895
- ]);
1896
- });
1897
- });
1898
-
1899
- await runWrangler("pages project upload .");
1900
-
1901
- expect(std.out).toMatchInlineSnapshot(`
1902
- "✨ Success! Uploaded 1 files (TIMINGS)
1903
-
1904
- ✨ Upload complete!"
1905
- `);
1906
- });
1907
-
1908
- it("should retry uploads", async () => {
1909
- writeFileSync("logo.txt", "foobar");
1910
-
1911
- setMockResponse(
1912
- "/pages/assets/check-missing",
1913
- "POST",
1914
- async (_, init) => {
1915
- const body = JSON.parse(init.body as string) as { hashes: string[] };
1916
- assertLater(() => {
1917
- expect(init.headers).toMatchObject({
1918
- Authorization: "Bearer <<funfetti-auth-jwt>>",
1919
- });
1920
- expect(body).toMatchObject({
1921
- hashes: ["1a98fb08af91aca4a7df1764a2c4ddb0"],
1922
- });
1923
- });
1924
- return body.hashes;
1925
- }
1926
- );
1927
-
1928
- // Accumulate multiple requests then assert afterwards
1929
- const requests: RequestInit[] = [];
1930
- setMockRawResponse("/pages/assets/upload", "POST", async (_, init) => {
1931
- requests.push(init);
1932
-
1933
- if (requests.length < 2) {
1934
- return createFetchResult(null, false, [
1935
- {
1936
- code: 800000,
1937
- message: "Something exploded, please retry",
1938
- },
1939
- ]);
1940
- } else {
1941
- return createFetchResult(null, true);
1942
- }
1943
- });
1944
-
1945
- await runWrangler("pages project upload .");
1946
-
1947
- // Assert two identical requests
1948
- expect(requests.length).toBe(2);
1949
- for (const init of requests) {
1950
- assertLater(() => {
1951
- expect(init.headers).toMatchObject({
1952
- Authorization: "Bearer <<funfetti-auth-jwt>>",
1953
- });
1954
-
1955
- const body = JSON.parse(init.body as string) as UploadPayloadFile[];
1956
- expect(body).toMatchObject([
1957
- {
1958
- key: "1a98fb08af91aca4a7df1764a2c4ddb0",
1959
- value: Buffer.from("foobar").toString("base64"),
1960
- metadata: {
1961
- contentType: "text/plain",
1962
- },
1963
- base64: true,
1964
- },
1965
- ]);
1966
- });
1967
- }
1968
-
1969
- expect(std.out).toMatchInlineSnapshot(`
1970
- "✨ Success! Uploaded 1 files (TIMINGS)
1971
-
1972
- ✨ Upload complete!"
1973
- `);
1974
- });
1975
-
1976
- it("should try to use multiple buckets (up to the max concurrency)", async () => {
1977
- writeFileSync("logo.txt", "foobar");
1978
- writeFileSync("logo.png", "foobar");
1979
- writeFileSync("logo.html", "foobar");
1980
- writeFileSync("logo.js", "foobar");
1981
-
1982
- mockGetToken("<<funfetti-auth-jwt>>");
1983
-
1984
- setMockResponse(
1985
- "/pages/assets/check-missing",
1986
- "POST",
1987
- async (_, init) => {
1988
- const body = JSON.parse(init.body as string) as { hashes: string[] };
1989
- assertLater(() => {
1990
- expect(init.headers).toMatchObject({
1991
- Authorization: "Bearer <<funfetti-auth-jwt>>",
1992
- });
1993
- expect(body).toMatchObject({
1994
- hashes: expect.arrayContaining([
1995
- "d96fef225537c9f5e44a3cb27fd0b492",
1996
- "2082190357cfd3617ccfe04f340c6247",
1997
- "6be321bef99e758250dac034474ddbb8",
1998
- "1a98fb08af91aca4a7df1764a2c4ddb0",
1999
- ]),
2000
- });
2001
- });
2002
- return body.hashes;
2003
- }
2004
- );
2005
-
2006
- // Accumulate multiple requests then assert afterwards
2007
- const requests: RequestInit[] = [];
2008
- setMockResponse("/pages/assets/upload", "POST", async (_, init) => {
2009
- requests.push(init);
2010
- });
2011
-
2012
- await runWrangler("pages project upload .");
2013
-
2014
- // We have 3 buckets, so expect 3 uploads
2015
- expect(requests.length).toBe(3);
2016
- const bodies: UploadPayloadFile[][] = [];
2017
- for (const init of requests) {
2018
- expect(init.headers).toMatchObject({
2019
- Authorization: "Bearer <<funfetti-auth-jwt>>",
2020
- });
2021
- bodies.push(JSON.parse(init.body as string) as UploadPayloadFile[]);
2022
- }
2023
- // One bucket should end up with 2 files
2024
- expect(bodies.map((b) => b.length).sort()).toEqual([1, 1, 2]);
2025
- // But we don't know the order, so flatten and test without ordering
2026
- expect(bodies.flatMap((b) => b)).toEqual(
2027
- expect.arrayContaining([
2028
- {
2029
- base64: true,
2030
- key: "d96fef225537c9f5e44a3cb27fd0b492",
2031
- metadata: { contentType: "text/html" },
2032
- value: "Zm9vYmFy",
2033
- },
2034
- {
2035
- base64: true,
2036
- key: "1a98fb08af91aca4a7df1764a2c4ddb0",
2037
- metadata: { contentType: "text/plain" },
2038
- value: "Zm9vYmFy",
2039
- },
2040
- {
2041
- base64: true,
2042
- key: "6be321bef99e758250dac034474ddbb8",
2043
- metadata: { contentType: "application/javascript" },
2044
- value: "Zm9vYmFy",
2045
- },
2046
- {
2047
- base64: true,
2048
- key: "2082190357cfd3617ccfe04f340c6247",
2049
- metadata: { contentType: "image/png" },
2050
- value: "Zm9vYmFy",
2051
- },
2052
- ])
2053
- );
2054
-
2055
- expect(std.out).toMatchInlineSnapshot(`
2056
- "✨ Success! Uploaded 4 files (TIMINGS)
2057
-
2058
- ✨ Upload complete!"
2059
- `);
2060
- });
2061
-
2062
- it("should not error when directory names contain periods and houses a extensionless file", async () => {
2063
- mkdirSync(".well-known");
2064
- // Note: same content as previous test, but since it's a different extension,
2065
- // it hashes to a different value
2066
- writeFileSync(".well-known/foobar", "foobar");
2067
-
2068
- mockGetToken("<<funfetti-auth-jwt>>");
2069
-
2070
- setMockResponse(
2071
- "/pages/assets/check-missing",
2072
- "POST",
2073
- async (_, init) => {
2074
- const body = JSON.parse(init.body as string) as { hashes: string[] };
2075
- assertLater(() => {
2076
- expect(init.headers).toMatchObject({
2077
- Authorization: "Bearer <<funfetti-auth-jwt>>",
2078
- });
2079
- expect(body).toMatchObject({
2080
- hashes: ["7b764dacfd211bebd8077828a7ddefd7"],
2081
- });
2082
- });
2083
- return body.hashes;
2084
- }
2085
- );
2086
-
2087
- setMockResponse("/pages/assets/upload", "POST", async (_, init) => {
2088
- assertLater(() => {
2089
- expect(init.headers).toMatchObject({
2090
- Authorization: "Bearer <<funfetti-auth-jwt>>",
2091
- });
2092
- const body = JSON.parse(init.body as string) as UploadPayloadFile[];
2093
- expect(body).toMatchObject([
2094
- {
2095
- key: "7b764dacfd211bebd8077828a7ddefd7",
2096
- value: Buffer.from("foobar").toString("base64"),
2097
- metadata: {
2098
- contentType: "application/octet-stream",
2099
- },
2100
- base64: true,
2101
- },
2102
- ]);
2103
- });
2104
- });
2105
-
2106
- await runWrangler("pages project upload .");
2107
-
2108
- expect(std.err).toMatchInlineSnapshot(`""`);
2109
- });
2110
- });
2111
- });