wrangler 0.0.0-e95e5a0 → 0.0.0-e981d0be5

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 (184) hide show
  1. package/README.md +46 -34
  2. package/bin/wrangler.js +84 -39
  3. package/config-schema.json +2278 -0
  4. package/kv-asset-handler.js +1 -0
  5. package/package.json +132 -99
  6. package/templates/__tests__/pages-dev-util.test.ts +128 -0
  7. package/templates/__tests__/tsconfig-sanity.ts +12 -0
  8. package/templates/__tests__/tsconfig.json +8 -0
  9. package/templates/checked-fetch.js +30 -0
  10. package/templates/facade.d.ts +19 -0
  11. package/templates/gitignore +2 -3
  12. package/templates/init-tests/test-jest-new-worker.js +23 -0
  13. package/templates/init-tests/test-vitest-new-worker.js +24 -0
  14. package/templates/init-tests/test-vitest-new-worker.ts +25 -0
  15. package/templates/middleware/common.ts +67 -0
  16. package/templates/middleware/loader-modules.ts +134 -0
  17. package/templates/middleware/loader-sw.ts +229 -0
  18. package/templates/middleware/middleware-ensure-req-body-drained.ts +18 -0
  19. package/templates/middleware/middleware-miniflare3-json-error.ts +32 -0
  20. package/templates/middleware/middleware-mock-analytics-engine.d.ts +3 -0
  21. package/templates/middleware/middleware-mock-analytics-engine.ts +30 -0
  22. package/templates/middleware/middleware-patch-console-prefix.d.ts +3 -0
  23. package/templates/middleware/middleware-patch-console-prefix.ts +21 -0
  24. package/templates/middleware/middleware-pretty-error.ts +40 -0
  25. package/templates/middleware/middleware-scheduled.ts +29 -0
  26. package/templates/modules-watch-stub.js +4 -0
  27. package/templates/new-worker-scheduled.js +17 -0
  28. package/templates/new-worker-scheduled.ts +32 -0
  29. package/templates/new-worker.js +3 -3
  30. package/templates/new-worker.ts +22 -4
  31. package/templates/no-op-worker.js +10 -0
  32. package/templates/pages-dev-pipeline.ts +33 -0
  33. package/templates/pages-dev-util.ts +55 -0
  34. package/templates/pages-shim.ts +9 -0
  35. package/templates/pages-template-plugin.ts +190 -0
  36. package/templates/pages-template-worker.ts +198 -0
  37. package/templates/startDevWorker/InspectorProxyWorker.ts +679 -0
  38. package/templates/startDevWorker/ProxyWorker.ts +336 -0
  39. package/templates/tsconfig-sanity.ts +11 -0
  40. package/templates/tsconfig.init.json +22 -0
  41. package/templates/tsconfig.json +13 -103
  42. package/templates/tsconfig.tsbuildinfo +1 -0
  43. package/wrangler-dist/InspectorProxyWorker.js +471 -0
  44. package/wrangler-dist/InspectorProxyWorker.js.map +6 -0
  45. package/wrangler-dist/ProxyWorker.js +238 -0
  46. package/wrangler-dist/ProxyWorker.js.map +6 -0
  47. package/wrangler-dist/cli.d.ts +26693 -0
  48. package/wrangler-dist/cli.js +148512 -106990
  49. package/import_meta_url.js +0 -3
  50. package/miniflare-config-stubs/.env.empty +0 -0
  51. package/miniflare-config-stubs/package.empty.json +0 -1
  52. package/miniflare-config-stubs/wrangler.empty.toml +0 -0
  53. package/miniflare-dist/index.mjs +0 -4886
  54. package/pages/functions/buildPlugin.ts +0 -92
  55. package/pages/functions/buildWorker.ts +0 -138
  56. package/pages/functions/filepath-routing.test.ts +0 -234
  57. package/pages/functions/filepath-routing.ts +0 -189
  58. package/pages/functions/identifiers.ts +0 -78
  59. package/pages/functions/routes.ts +0 -156
  60. package/pages/functions/template-plugin.ts +0 -146
  61. package/pages/functions/template-worker.ts +0 -143
  62. package/src/__tests__/config-cache.test.ts +0 -36
  63. package/src/__tests__/configuration.test.ts +0 -3125
  64. package/src/__tests__/dev.test.tsx +0 -762
  65. package/src/__tests__/guess-worker-format.test.ts +0 -85
  66. package/src/__tests__/helpers/clipboardy-mock.js +0 -4
  67. package/src/__tests__/helpers/cmd-shim.d.ts +0 -11
  68. package/src/__tests__/helpers/faye-websocket.d.ts +0 -6
  69. package/src/__tests__/helpers/mock-account-id.ts +0 -48
  70. package/src/__tests__/helpers/mock-bin.ts +0 -36
  71. package/src/__tests__/helpers/mock-cfetch.ts +0 -211
  72. package/src/__tests__/helpers/mock-console.ts +0 -87
  73. package/src/__tests__/helpers/mock-dialogs.ts +0 -90
  74. package/src/__tests__/helpers/mock-http-server.ts +0 -46
  75. package/src/__tests__/helpers/mock-istty.ts +0 -27
  76. package/src/__tests__/helpers/mock-kv.ts +0 -33
  77. package/src/__tests__/helpers/mock-oauth-flow.ts +0 -255
  78. package/src/__tests__/helpers/mock-stdin.ts +0 -103
  79. package/src/__tests__/helpers/mock-web-socket.ts +0 -29
  80. package/src/__tests__/helpers/run-in-tmp.ts +0 -39
  81. package/src/__tests__/helpers/run-wrangler.ts +0 -16
  82. package/src/__tests__/helpers/write-worker-source.ts +0 -31
  83. package/src/__tests__/helpers/write-wrangler-toml.ts +0 -17
  84. package/src/__tests__/https-options.test.ts +0 -130
  85. package/src/__tests__/index.test.ts +0 -249
  86. package/src/__tests__/init.test.ts +0 -1516
  87. package/src/__tests__/jest.setup.ts +0 -69
  88. package/src/__tests__/kv.test.ts +0 -1454
  89. package/src/__tests__/logger.test.ts +0 -115
  90. package/src/__tests__/package-manager.test.ts +0 -206
  91. package/src/__tests__/pages.test.ts +0 -329
  92. package/src/__tests__/parse.test.ts +0 -322
  93. package/src/__tests__/publish.test.ts +0 -4875
  94. package/src/__tests__/r2.test.ts +0 -214
  95. package/src/__tests__/route.test.ts +0 -45
  96. package/src/__tests__/secret.test.ts +0 -446
  97. package/src/__tests__/tail.test.ts +0 -690
  98. package/src/__tests__/user.test.ts +0 -131
  99. package/src/__tests__/whoami.test.tsx +0 -105
  100. package/src/bundle.ts +0 -169
  101. package/src/cfetch/index.ts +0 -104
  102. package/src/cfetch/internal.ts +0 -137
  103. package/src/cli.ts +0 -13
  104. package/src/config/README.md +0 -107
  105. package/src/config/config.ts +0 -234
  106. package/src/config/diagnostics.ts +0 -80
  107. package/src/config/environment.ts +0 -394
  108. package/src/config/index.ts +0 -63
  109. package/src/config/validation-helpers.ts +0 -575
  110. package/src/config/validation.ts +0 -1424
  111. package/src/config-cache.ts +0 -45
  112. package/src/create-worker-preview.ts +0 -175
  113. package/src/create-worker-upload-form.ts +0 -240
  114. package/src/dev/dev-vars.ts +0 -36
  115. package/src/dev/dev.tsx +0 -418
  116. package/src/dev/local.tsx +0 -312
  117. package/src/dev/remote.tsx +0 -213
  118. package/src/dev/use-esbuild.ts +0 -118
  119. package/src/dialogs.tsx +0 -87
  120. package/src/entry.ts +0 -267
  121. package/src/environment-variables.ts +0 -35
  122. package/src/errors.ts +0 -11
  123. package/src/https-options.ts +0 -123
  124. package/src/index.tsx +0 -2668
  125. package/src/inspect.ts +0 -623
  126. package/src/intl-polyfill.d.ts +0 -139
  127. package/src/kv.ts +0 -319
  128. package/src/logger.ts +0 -66
  129. package/src/miniflare-cli/README.md +0 -30
  130. package/src/miniflare-cli/enum-keys.ts +0 -17
  131. package/src/miniflare-cli/index.ts +0 -41
  132. package/src/miniflare-cli/request-context.ts +0 -40
  133. package/src/module-collection.ts +0 -258
  134. package/src/open-in-browser.ts +0 -26
  135. package/src/package-manager.ts +0 -127
  136. package/src/pages.tsx +0 -1866
  137. package/src/parse.ts +0 -183
  138. package/src/paths.ts +0 -26
  139. package/src/proxy.ts +0 -449
  140. package/src/publish.ts +0 -469
  141. package/src/r2.ts +0 -50
  142. package/src/selfsigned.d.ts +0 -29
  143. package/src/sites.tsx +0 -293
  144. package/src/tail/filters.ts +0 -279
  145. package/src/tail/index.ts +0 -299
  146. package/src/tail/printing.ts +0 -67
  147. package/src/update-check.ts +0 -19
  148. package/src/user.tsx +0 -1193
  149. package/src/whoami.tsx +0 -70
  150. package/src/worker.ts +0 -172
  151. package/templates/static-asset-facade.js +0 -47
  152. package/vendor/@cloudflare/kv-asset-handler/CHANGELOG.md +0 -332
  153. package/vendor/@cloudflare/kv-asset-handler/LICENSE_APACHE +0 -176
  154. package/vendor/@cloudflare/kv-asset-handler/LICENSE_MIT +0 -25
  155. package/vendor/@cloudflare/kv-asset-handler/README.md +0 -245
  156. package/vendor/@cloudflare/kv-asset-handler/dist/index.d.ts +0 -32
  157. package/vendor/@cloudflare/kv-asset-handler/dist/index.js +0 -354
  158. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.d.ts +0 -13
  159. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.js +0 -148
  160. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.d.ts +0 -1
  161. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.js +0 -436
  162. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.d.ts +0 -1
  163. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.js +0 -40
  164. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.d.ts +0 -1
  165. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.js +0 -42
  166. package/vendor/@cloudflare/kv-asset-handler/dist/types.d.ts +0 -26
  167. package/vendor/@cloudflare/kv-asset-handler/dist/types.js +0 -31
  168. package/vendor/@cloudflare/kv-asset-handler/package.json +0 -52
  169. package/vendor/@cloudflare/kv-asset-handler/src/index.ts +0 -296
  170. package/vendor/@cloudflare/kv-asset-handler/src/mocks.ts +0 -136
  171. package/vendor/@cloudflare/kv-asset-handler/src/test/getAssetFromKV.ts +0 -464
  172. package/vendor/@cloudflare/kv-asset-handler/src/test/mapRequestToAsset.ts +0 -33
  173. package/vendor/@cloudflare/kv-asset-handler/src/test/serveSinglePageApp.ts +0 -42
  174. package/vendor/@cloudflare/kv-asset-handler/src/types.ts +0 -39
  175. package/vendor/wrangler-mime/CHANGELOG.md +0 -289
  176. package/vendor/wrangler-mime/LICENSE +0 -21
  177. package/vendor/wrangler-mime/Mime.js +0 -97
  178. package/vendor/wrangler-mime/README.md +0 -187
  179. package/vendor/wrangler-mime/cli.js +0 -46
  180. package/vendor/wrangler-mime/index.js +0 -4
  181. package/vendor/wrangler-mime/lite.js +0 -4
  182. package/vendor/wrangler-mime/package.json +0 -52
  183. package/vendor/wrangler-mime/types/other.js +0 -1
  184. package/vendor/wrangler-mime/types/standard.js +0 -1
@@ -1,4875 +0,0 @@
1
- import * as fs from "node:fs";
2
- import * as path from "node:path";
3
- import * as TOML from "@iarna/toml";
4
- import { writeAuthConfigFile } from "../user";
5
- import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
6
- import {
7
- createFetchResult,
8
- setMockRawResponse,
9
- setMockResponse,
10
- unsetAllMocks,
11
- unsetMockFetchKVGetValues,
12
- } from "./helpers/mock-cfetch";
13
- import { mockConsoleMethods, normalizeSlashes } from "./helpers/mock-console";
14
- import { useMockIsTTY } from "./helpers/mock-istty";
15
- import { mockKeyListRequest } from "./helpers/mock-kv";
16
- import { mockOAuthFlow } from "./helpers/mock-oauth-flow";
17
- import { runInTempDir } from "./helpers/run-in-tmp";
18
- import { runWrangler } from "./helpers/run-wrangler";
19
- import { writeWorkerSource } from "./helpers/write-worker-source";
20
- import writeWranglerToml from "./helpers/write-wrangler-toml";
21
- import type { Config } from "../config";
22
- import type { WorkerMetadata } from "../create-worker-upload-form";
23
- import type { KVNamespaceInfo } from "../kv";
24
- import type { CfWorkerInit } from "../worker";
25
- import type { FormData, File } from "undici";
26
-
27
- describe("publish", () => {
28
- mockAccountId();
29
- mockApiToken();
30
- runInTempDir({ homedir: "./home" });
31
- const { setIsTTY } = useMockIsTTY();
32
- const std = mockConsoleMethods();
33
- const {
34
- mockOAuthServerCallback,
35
- mockGrantAccessToken,
36
- mockGrantAuthorization,
37
- mockGetMemberships,
38
- } = mockOAuthFlow();
39
-
40
- beforeEach(() => {
41
- // @ts-expect-error we're using a very simple setTimeout mock here
42
- jest.spyOn(global, "setTimeout").mockImplementation((fn, _period) => {
43
- setImmediate(fn);
44
- });
45
- setIsTTY(true);
46
- });
47
-
48
- afterEach(() => {
49
- unsetAllMocks();
50
- unsetMockFetchKVGetValues();
51
- });
52
-
53
- describe("authentication", () => {
54
- mockApiToken({ apiToken: null });
55
- beforeEach(() => {
56
- // @ts-expect-error disable the mock we'd setup earlier
57
- // or else our server won't bother listening for oauth requests
58
- // and will timeout and fail
59
- global.setTimeout.mockRestore();
60
- });
61
-
62
- it("drops a user into the login flow if they're unauthenticated", async () => {
63
- writeWranglerToml();
64
- writeWorkerSource();
65
- mockSubDomainRequest();
66
- mockUploadWorkerRequest();
67
-
68
- mockOAuthServerCallback();
69
- const accessTokenRequest = mockGrantAccessToken({ respondWith: "ok" });
70
- mockGrantAuthorization({ respondWith: "success" });
71
-
72
- await expect(runWrangler("publish index.js")).resolves.toBeUndefined();
73
-
74
- expect(accessTokenRequest.actual.url).toEqual(
75
- accessTokenRequest.expected.url
76
- );
77
-
78
- expect(std.out).toMatchInlineSnapshot(`
79
- "Attempting to login via OAuth...
80
- Successfully logged in.
81
- Uploaded test-name (TIMINGS)
82
- Published test-name (TIMINGS)
83
- test-name.test-sub-domain.workers.dev"
84
- `);
85
- expect(std.warn).toMatchInlineSnapshot(`""`);
86
- expect(std.err).toMatchInlineSnapshot(`""`);
87
- });
88
-
89
- it("warns a user when they're authenticated with an API token in wrangler config file", async () => {
90
- writeWranglerToml();
91
- writeWorkerSource();
92
- mockSubDomainRequest();
93
- mockUploadWorkerRequest();
94
- writeAuthConfigFile({
95
- api_token: "some-api-token",
96
- });
97
-
98
- const accessTokenRequest = mockGrantAccessToken({ respondWith: "ok" });
99
- mockGrantAuthorization({ respondWith: "success" });
100
-
101
- await expect(runWrangler("publish index.js")).resolves.toBeUndefined();
102
-
103
- expect(accessTokenRequest.actual.url).toEqual(
104
- accessTokenRequest.expected.url
105
- );
106
-
107
- expect(std.out).toMatchInlineSnapshot(`
108
- "Attempting to login via OAuth...
109
- Successfully logged in.
110
- Uploaded test-name (TIMINGS)
111
- Published test-name (TIMINGS)
112
- test-name.test-sub-domain.workers.dev"
113
- `);
114
- expect(std.warn).toMatchInlineSnapshot(`
115
- "▲ [WARNING] It looks like you have used Wrangler 1's \`config\` command to login with an API token.
116
-
117
- This is no longer supported in the current version of Wrangler.
118
- If you wish to authenticate via an API token then please set the \`CLOUDFLARE_API_TOKEN\`
119
- environment variable.
120
-
121
- "
122
- `);
123
- expect(std.err).toMatchInlineSnapshot(`""`);
124
- });
125
-
126
- describe("non-TTY", () => {
127
- const ENV_COPY = process.env;
128
-
129
- afterEach(() => {
130
- process.env = ENV_COPY;
131
- });
132
-
133
- it("should not throw an error in non-TTY if 'CLOUDFLARE_API_TOKEN' & 'account_id' are in scope", async () => {
134
- process.env = {
135
- CLOUDFLARE_API_TOKEN: "123456789",
136
- };
137
- setIsTTY(false);
138
- writeWranglerToml({
139
- account_id: "some-account-id",
140
- });
141
- writeWorkerSource();
142
- mockSubDomainRequest();
143
- mockUploadWorkerRequest();
144
- mockOAuthServerCallback();
145
-
146
- await runWrangler("publish index.js");
147
-
148
- expect(std.out).toMatchInlineSnapshot(`
149
- "Uploaded test-name (TIMINGS)
150
- Published test-name (TIMINGS)
151
- test-name.test-sub-domain.workers.dev"
152
- `);
153
- expect(std.err).toMatchInlineSnapshot(`""`);
154
- });
155
-
156
- it("should not throw an error if 'CLOUDFLARE_ACCOUNT_ID' & 'CLOUDFLARE_API_TOKEN' are in scope", async () => {
157
- process.env = {
158
- CLOUDFLARE_API_TOKEN: "hunter2",
159
- CLOUDFLARE_ACCOUNT_ID: "some-account-id",
160
- };
161
- setIsTTY(false);
162
- writeWranglerToml();
163
- writeWorkerSource();
164
- mockSubDomainRequest();
165
- mockUploadWorkerRequest();
166
- mockOAuthServerCallback();
167
- mockGetMemberships({
168
- success: true,
169
- result: [],
170
- });
171
-
172
- await runWrangler("publish index.js");
173
-
174
- expect(std.out).toMatchInlineSnapshot(`
175
- "Uploaded test-name (TIMINGS)
176
- Published test-name (TIMINGS)
177
- test-name.test-sub-domain.workers.dev"
178
- `);
179
- expect(std.err).toMatchInlineSnapshot(`""`);
180
- });
181
-
182
- it("should throw an error in non-TTY & there is more than one account associated with API token", async () => {
183
- setIsTTY(false);
184
- process.env = {
185
- CLOUDFLARE_API_TOKEN: "hunter2",
186
- CLOUDFLARE_ACCOUNT_ID: undefined,
187
- };
188
- writeWranglerToml({
189
- account_id: undefined,
190
- });
191
- writeWorkerSource();
192
- mockSubDomainRequest();
193
- mockUploadWorkerRequest();
194
- mockOAuthServerCallback();
195
- mockGetMemberships({
196
- success: true,
197
- result: [
198
- { id: "IG-88", account: { id: "1701", name: "enterprise" } },
199
- { id: "R2-D2", account: { id: "nx01", name: "enterprise-nx" } },
200
- ],
201
- });
202
-
203
- await expect(runWrangler("publish index.js")).rejects
204
- .toMatchInlineSnapshot(`
205
- [Error: More than one account available but unable to select one in non-interactive mode.
206
- Please set the appropriate \`account_id\` in your \`wrangler.toml\` file.
207
- Available accounts are ("<name>" - "<id>"):
208
- "enterprise" - "1701")
209
- "enterprise-nx" - "nx01")]
210
- `);
211
- });
212
-
213
- it("should throw error in non-TTY if 'CLOUDFLARE_API_TOKEN' is missing", async () => {
214
- setIsTTY(false);
215
- writeWranglerToml({
216
- account_id: undefined,
217
- });
218
- process.env = {
219
- CLOUDFLARE_API_TOKEN: undefined,
220
- CLOUDFLARE_ACCOUNT_ID: "badwolf",
221
- };
222
- writeWorkerSource();
223
- mockSubDomainRequest();
224
- mockUploadWorkerRequest();
225
- mockOAuthServerCallback();
226
- mockGetMemberships({
227
- success: true,
228
- result: [
229
- { id: "IG-88", account: { id: "1701", name: "enterprise" } },
230
- { id: "R2-D2", account: { id: "nx01", name: "enterprise-nx" } },
231
- ],
232
- });
233
-
234
- await expect(runWrangler("publish index.js")).rejects.toThrowError();
235
-
236
- expect(std.err).toMatchInlineSnapshot(`
237
- "X [ERROR] In a non-interactive environment, it's necessary to set a CLOUDFLARE_API_TOKEN environment variable for wrangler to work. Please go to https://developers.cloudflare.com/api/tokens/create/ for instructions on how to create an api token, and assign its value to CLOUDFLARE_API_TOKEN.
238
-
239
- "
240
- `);
241
- });
242
- it("should throw error with no account ID provided and no members retrieved", async () => {
243
- setIsTTY(false);
244
- writeWranglerToml({
245
- account_id: undefined,
246
- });
247
- process.env = {
248
- CLOUDFLARE_API_TOKEN: "picard",
249
- CLOUDFLARE_ACCOUNT_ID: undefined,
250
- };
251
- writeWorkerSource();
252
- mockSubDomainRequest();
253
- mockUploadWorkerRequest();
254
- mockOAuthServerCallback();
255
- mockGetMemberships({
256
- success: false,
257
- result: [],
258
- });
259
-
260
- await expect(runWrangler("publish index.js")).rejects.toThrowError();
261
-
262
- expect(std.err).toMatchInlineSnapshot(`
263
- "X [ERROR] Failed to automatically retrieve account IDs for the logged in user. In a non-interactive environment, it is mandatory to specify an account ID, either by assigning its value to CLOUDFLARE_ACCOUNT_ID, or as \`account_id\` in your \`wrangler.toml\` file.
264
-
265
- "
266
- `);
267
- });
268
- });
269
- });
270
-
271
- describe("environments", () => {
272
- it("should use legacy environments by default", async () => {
273
- writeWranglerToml({ env: { "some-env": {} } });
274
- writeWorkerSource();
275
- mockSubDomainRequest();
276
- mockUploadWorkerRequest({
277
- env: "some-env",
278
- legacyEnv: true,
279
- });
280
- await runWrangler("publish index.js --env some-env");
281
- expect(std.out).toMatchInlineSnapshot(`
282
- "Uploaded test-name-some-env (TIMINGS)
283
- Published test-name-some-env (TIMINGS)
284
- test-name-some-env.test-sub-domain.workers.dev"
285
- `);
286
- expect(std.err).toMatchInlineSnapshot(`""`);
287
- expect(std.warn).toMatchInlineSnapshot(`""`);
288
- });
289
-
290
- describe("legacy", () => {
291
- it("uses the script name when no environment is specified", async () => {
292
- writeWranglerToml();
293
- writeWorkerSource();
294
- mockSubDomainRequest();
295
- mockUploadWorkerRequest({
296
- legacyEnv: true,
297
- });
298
- await runWrangler("publish index.js --legacy-env true");
299
- expect(std.out).toMatchInlineSnapshot(`
300
- "Uploaded test-name (TIMINGS)
301
- Published test-name (TIMINGS)
302
- test-name.test-sub-domain.workers.dev"
303
- `);
304
- expect(std.err).toMatchInlineSnapshot(`""`);
305
- expect(std.warn).toMatchInlineSnapshot(`""`);
306
- });
307
-
308
- it("appends the environment name when provided, and there is associated config", async () => {
309
- writeWranglerToml({ env: { "some-env": {} } });
310
- writeWorkerSource();
311
- mockSubDomainRequest();
312
- mockUploadWorkerRequest({
313
- env: "some-env",
314
- legacyEnv: true,
315
- });
316
- await runWrangler("publish index.js --env some-env --legacy-env true");
317
- expect(std.out).toMatchInlineSnapshot(`
318
- "Uploaded test-name-some-env (TIMINGS)
319
- Published test-name-some-env (TIMINGS)
320
- test-name-some-env.test-sub-domain.workers.dev"
321
- `);
322
- expect(std.err).toMatchInlineSnapshot(`""`);
323
- expect(std.warn).toMatchInlineSnapshot(`""`);
324
- });
325
-
326
- it("appends the environment name when provided (with a warning), if there are no configured environments", async () => {
327
- writeWranglerToml({});
328
- writeWorkerSource();
329
- mockSubDomainRequest();
330
- mockUploadWorkerRequest({
331
- env: "some-env",
332
- legacyEnv: true,
333
- });
334
- await runWrangler("publish index.js --env some-env --legacy-env true");
335
- expect(std.out).toMatchInlineSnapshot(`
336
- "Uploaded test-name-some-env (TIMINGS)
337
- Published test-name-some-env (TIMINGS)
338
- test-name-some-env.test-sub-domain.workers.dev"
339
- `);
340
- expect(std.err).toMatchInlineSnapshot(`""`);
341
- expect(std.warn).toMatchInlineSnapshot(`
342
- "▲ [WARNING] Processing wrangler.toml configuration:
343
-
344
- - No environment found in configuration with name \\"some-env\\".
345
- Before using \`--env=some-env\` there should be an equivalent environment section in the
346
- configuration.
347
-
348
- Consider adding an environment configuration section to the wrangler.toml file:
349
- \`\`\`
350
- [env.some-env]
351
- \`\`\`
352
-
353
-
354
- "
355
- `);
356
- });
357
-
358
- it("should throw an error when an environment name when provided, which doesn't match those in the config", async () => {
359
- writeWranglerToml({ env: { "other-env": {} } });
360
- writeWorkerSource();
361
- mockSubDomainRequest();
362
- await expect(
363
- runWrangler("publish index.js --env some-env --legacy-env true")
364
- ).rejects.toThrowErrorMatchingInlineSnapshot(`
365
- "Processing wrangler.toml configuration:
366
- - No environment found in configuration with name \\"some-env\\".
367
- Before using \`--env=some-env\` there should be an equivalent environment section in the configuration.
368
- The available configured environment names are: [\\"other-env\\"]
369
-
370
- Consider adding an environment configuration section to the wrangler.toml file:
371
- \`\`\`
372
- [env.some-env]
373
- \`\`\`
374
- "
375
- `);
376
- });
377
-
378
- it("should throw an error w/ helpful message when using --env --name", async () => {
379
- writeWranglerToml({ env: { "some-env": {} } });
380
- writeWorkerSource();
381
- mockSubDomainRequest();
382
- await runWrangler(
383
- "publish index.js --name voyager --env some-env --legacy-env true"
384
- ).catch((err) =>
385
- expect(err).toMatchInlineSnapshot(`
386
- [Error: In legacy environment mode you cannot use --name and --env together. If you want to specify a Worker name for a specific environment you can add the following to your wrangler.toml config:
387
- [env.some-env]
388
- name = "voyager"
389
- ]
390
- `)
391
- );
392
- });
393
- });
394
-
395
- describe("services", () => {
396
- it("uses the script name when no environment is specified", async () => {
397
- writeWranglerToml();
398
- writeWorkerSource();
399
- mockSubDomainRequest();
400
- mockUploadWorkerRequest({
401
- legacyEnv: false,
402
- });
403
- await runWrangler("publish index.js --legacy-env false");
404
- expect(std.out).toMatchInlineSnapshot(`
405
- "Uploaded test-name (TIMINGS)
406
- Published test-name (TIMINGS)
407
- test-name.test-sub-domain.workers.dev"
408
- `);
409
- expect(std.err).toMatchInlineSnapshot(`""`);
410
- expect(std.warn).toMatchInlineSnapshot(`
411
- "▲ [WARNING] Processing wrangler.toml configuration:
412
-
413
- - Experimental: Service environments are in beta, and their behaviour is guaranteed to change in
414
- the future. DO NOT USE IN PRODUCTION.
415
-
416
- "
417
- `);
418
- });
419
-
420
- it("publishes as an environment when provided", async () => {
421
- writeWranglerToml({ env: { "some-env": {} } });
422
- writeWorkerSource();
423
- mockSubDomainRequest();
424
- mockUploadWorkerRequest({
425
- env: "some-env",
426
- legacyEnv: false,
427
- });
428
- await runWrangler("publish index.js --env some-env --legacy-env false");
429
- expect(std.out).toMatchInlineSnapshot(`
430
- "Uploaded test-name (some-env) (TIMINGS)
431
- Published test-name (some-env) (TIMINGS)
432
- some-env.test-name.test-sub-domain.workers.dev"
433
- `);
434
- expect(std.err).toMatchInlineSnapshot(`""`);
435
- expect(std.warn).toMatchInlineSnapshot(`
436
- "▲ [WARNING] Processing wrangler.toml configuration:
437
-
438
- - Experimental: Service environments are in beta, and their behaviour is guaranteed to change in
439
- the future. DO NOT USE IN PRODUCTION.
440
-
441
- "
442
- `);
443
- });
444
- });
445
- });
446
-
447
- it("should resolve wrangler.toml relative to the entrypoint", async () => {
448
- fs.mkdirSync("./some-path/worker", { recursive: true });
449
- fs.writeFileSync(
450
- "./some-path/wrangler.toml",
451
- TOML.stringify({
452
- name: "test-name",
453
- compatibility_date: "2022-01-12",
454
- vars: { xyz: 123 },
455
- }),
456
- "utf-8"
457
- );
458
- writeWorkerSource({ basePath: "./some-path/worker" });
459
- mockUploadWorkerRequest({
460
- expectedBindings: [
461
- {
462
- json: 123,
463
- name: "xyz",
464
- type: "json",
465
- },
466
- ],
467
- expectedCompatibilityDate: "2022-01-12",
468
- });
469
- mockSubDomainRequest();
470
- await runWrangler("publish ./some-path/worker/index.js");
471
- expect(std.out).toMatchInlineSnapshot(`
472
- "Uploaded test-name (TIMINGS)
473
- Published test-name (TIMINGS)
474
- test-name.test-sub-domain.workers.dev"
475
- `);
476
- expect(std.err).toMatchInlineSnapshot(`""`);
477
- });
478
-
479
- describe("routes", () => {
480
- it("should publish the worker to a route", async () => {
481
- writeWranglerToml({
482
- routes: ["example.com/some-route/*"],
483
- });
484
- writeWorkerSource();
485
- mockUpdateWorkerRequest({ enabled: false });
486
- mockUploadWorkerRequest({ expectedType: "esm" });
487
- mockPublishRoutesRequest({ routes: ["example.com/some-route/*"] });
488
- await runWrangler("publish ./index");
489
- });
490
-
491
- it("should publish to a route with a pattern/{zone_id|zone_name} combo", async () => {
492
- writeWranglerToml({
493
- routes: [
494
- "some-example.com/some-route/*",
495
- { pattern: "*a-boring-website.com", zone_id: "54sdf7fsda" },
496
- {
497
- pattern: "*another-boring-website.com",
498
- zone_name: "some-zone.com",
499
- },
500
- { pattern: "example.com/some-route/*", zone_id: "JGHFHG654gjcj" },
501
- "more-examples.com/*",
502
- ],
503
- });
504
- writeWorkerSource();
505
- mockUpdateWorkerRequest({ enabled: false });
506
- mockUploadWorkerRequest({ expectedType: "esm" });
507
- mockPublishRoutesRequest({
508
- routes: [
509
- "some-example.com/some-route/*",
510
- { pattern: "*a-boring-website.com", zone_id: "54sdf7fsda" },
511
- {
512
- pattern: "*another-boring-website.com",
513
- zone_name: "some-zone.com",
514
- },
515
- { pattern: "example.com/some-route/*", zone_id: "JGHFHG654gjcj" },
516
- "more-examples.com/*",
517
- ],
518
- });
519
- await runWrangler("publish ./index");
520
- expect(std).toMatchInlineSnapshot(`
521
- Object {
522
- "debug": "",
523
- "err": "",
524
- "out": "Uploaded test-name (TIMINGS)
525
- Published test-name (TIMINGS)
526
- some-example.com/some-route/*
527
- *a-boring-website.com (zone id: 54sdf7fsda)
528
- *another-boring-website.com (zone name: some-zone.com)
529
- example.com/some-route/* (zone id: JGHFHG654gjcj)
530
- more-examples.com/*",
531
- "warn": "",
532
- }
533
- `);
534
- });
535
-
536
- it("should publish to a route with a pattern/{zone_id|zone_name} combo (service environments)", async () => {
537
- writeWranglerToml({
538
- env: {
539
- staging: {
540
- routes: [
541
- "some-example.com/some-route/*",
542
- { pattern: "*a-boring-website.com", zone_id: "54sdf7fsda" },
543
- {
544
- pattern: "*another-boring-website.com",
545
- zone_name: "some-zone.com",
546
- },
547
- { pattern: "example.com/some-route/*", zone_id: "JGHFHG654gjcj" },
548
- "more-examples.com/*",
549
- ],
550
- },
551
- },
552
- });
553
- mockSubDomainRequest();
554
- writeWorkerSource();
555
- mockUpdateWorkerRequest({
556
- enabled: false,
557
- env: "staging",
558
- legacyEnv: false,
559
- });
560
- mockUploadWorkerRequest({
561
- expectedType: "esm",
562
- env: "staging",
563
- legacyEnv: false,
564
- });
565
- mockPublishRoutesRequest({
566
- routes: [
567
- "some-example.com/some-route/*",
568
- { pattern: "*a-boring-website.com", zone_id: "54sdf7fsda" },
569
- {
570
- pattern: "*another-boring-website.com",
571
- zone_name: "some-zone.com",
572
- },
573
- { pattern: "example.com/some-route/*", zone_id: "JGHFHG654gjcj" },
574
- "more-examples.com/*",
575
- ],
576
- env: "staging",
577
- legacyEnv: false,
578
- });
579
- await runWrangler("publish ./index --legacy-env false --env staging");
580
- expect(std).toMatchInlineSnapshot(`
581
- Object {
582
- "debug": "",
583
- "err": "",
584
- "out": "Uploaded test-name (staging) (TIMINGS)
585
- Published test-name (staging) (TIMINGS)
586
- some-example.com/some-route/*
587
- *a-boring-website.com (zone id: 54sdf7fsda)
588
- *another-boring-website.com (zone name: some-zone.com)
589
- example.com/some-route/* (zone id: JGHFHG654gjcj)
590
- more-examples.com/*",
591
- "warn": "▲ [WARNING] Processing wrangler.toml configuration:
592
-
593
- - Experimental: Service environments are in beta, and their behaviour is guaranteed to change in
594
- the future. DO NOT USE IN PRODUCTION.
595
-
596
- ",
597
- }
598
- `);
599
- });
600
-
601
- it("should publish to legacy environment specific routes", async () => {
602
- writeWranglerToml({
603
- routes: ["example.com/some-route/*"],
604
- env: {
605
- dev: {
606
- routes: ["dev-example.com/some-route/*"],
607
- },
608
- },
609
- });
610
- writeWorkerSource();
611
- mockUpdateWorkerRequest({ enabled: false, legacyEnv: true, env: "dev" });
612
- mockUploadWorkerRequest({
613
- expectedType: "esm",
614
- legacyEnv: true,
615
- env: "dev",
616
- });
617
- mockPublishRoutesRequest({
618
- routes: ["dev-example.com/some-route/*"],
619
- legacyEnv: true,
620
- env: "dev",
621
- });
622
- await runWrangler("publish ./index --env dev --legacy-env true");
623
- });
624
-
625
- it("services: should publish to service environment specific routes", async () => {
626
- writeWranglerToml({
627
- routes: ["example.com/some-route/*"],
628
- env: {
629
- dev: {
630
- routes: ["dev-example.com/some-route/*"],
631
- },
632
- },
633
- });
634
- writeWorkerSource();
635
- mockUpdateWorkerRequest({ enabled: false, env: "dev" });
636
- mockUploadWorkerRequest({
637
- expectedType: "esm",
638
- env: "dev",
639
- });
640
- mockPublishRoutesRequest({
641
- routes: ["dev-example.com/some-route/*"],
642
- env: "dev",
643
- });
644
- await runWrangler("publish ./index --env dev --legacy-env false");
645
- });
646
-
647
- it.todo("should error if it's a workers.dev route");
648
- });
649
-
650
- describe("entry-points", () => {
651
- it("should be able to use `index` with no extension as the entry-point (esm)", async () => {
652
- writeWranglerToml();
653
- writeWorkerSource();
654
- mockUploadWorkerRequest({ expectedType: "esm" });
655
- mockSubDomainRequest();
656
-
657
- await runWrangler("publish ./index");
658
-
659
- expect(std.out).toMatchInlineSnapshot(`
660
- "Uploaded test-name (TIMINGS)
661
- Published test-name (TIMINGS)
662
- test-name.test-sub-domain.workers.dev"
663
- `);
664
- expect(std.err).toMatchInlineSnapshot(`""`);
665
- });
666
-
667
- it("should be able to use `index` with no extension as the entry-point (sw)", async () => {
668
- writeWranglerToml();
669
- writeWorkerSource({ type: "sw" });
670
- mockUploadWorkerRequest({ expectedType: "sw" });
671
- mockSubDomainRequest();
672
-
673
- await runWrangler("publish ./index");
674
-
675
- expect(std.out).toMatchInlineSnapshot(`
676
- "Uploaded test-name (TIMINGS)
677
- Published test-name (TIMINGS)
678
- test-name.test-sub-domain.workers.dev"
679
- `);
680
- expect(std.err).toMatchInlineSnapshot(`""`);
681
- });
682
-
683
- it("should be able to use the `main` config as the entry-point for ESM sources", async () => {
684
- writeWranglerToml({ main: "./index.js" });
685
- writeWorkerSource();
686
- mockUploadWorkerRequest();
687
- mockSubDomainRequest();
688
-
689
- await runWrangler("publish");
690
-
691
- expect(std.out).toMatchInlineSnapshot(`
692
- "Uploaded test-name (TIMINGS)
693
- Published test-name (TIMINGS)
694
- test-name.test-sub-domain.workers.dev"
695
- `);
696
- expect(std.err).toMatchInlineSnapshot(`""`);
697
- });
698
-
699
- it("should use `main` relative to the wrangler.toml not cwd", async () => {
700
- writeWranglerToml({
701
- main: "./foo/index.js",
702
- });
703
- writeWorkerSource({ basePath: "foo" });
704
- mockUploadWorkerRequest({ expectedEntry: "var foo = 100;" });
705
- mockSubDomainRequest();
706
- process.chdir("foo");
707
- await runWrangler("publish");
708
-
709
- expect(std.out).toMatchInlineSnapshot(`
710
- "Uploaded test-name (TIMINGS)
711
- Published test-name (TIMINGS)
712
- test-name.test-sub-domain.workers.dev"
713
- `);
714
- expect(std.err).toMatchInlineSnapshot(`""`);
715
- });
716
-
717
- it('should use `build.upload.main` as an entry point, where `build.upload.dir` defaults to "./dist", and log a deprecation warning', async () => {
718
- writeWranglerToml({ build: { upload: { main: "./index.js" } } });
719
- writeWorkerSource({ basePath: "./dist" });
720
- mockUploadWorkerRequest();
721
- mockSubDomainRequest();
722
-
723
- await runWrangler("publish");
724
-
725
- expect(std.out).toMatchInlineSnapshot(`
726
- "Uploaded test-name (TIMINGS)
727
- Published test-name (TIMINGS)
728
- test-name.test-sub-domain.workers.dev"
729
- `);
730
- expect(std.err).toMatchInlineSnapshot(`""`);
731
- expect(std.warn).toMatchInlineSnapshot(`
732
- "▲ [WARNING] Processing wrangler.toml configuration:
733
-
734
- - Deprecation: \\"build.upload.main\\":
735
- Delete the \`build.upload.main\` and \`build.upload.dir\` fields.
736
- Then add the top level \`main\` field to your configuration file:
737
- \`\`\`
738
- main = \\"dist/index.js\\"
739
- \`\`\`
740
-
741
- "
742
- `);
743
- });
744
-
745
- it("should use `build.upload.main` relative to `build.upload.dir`", async () => {
746
- writeWranglerToml({
747
- build: {
748
- upload: {
749
- main: "./index.js",
750
- dir: "./foo",
751
- },
752
- },
753
- });
754
- writeWorkerSource({ basePath: "./foo" });
755
- mockUploadWorkerRequest({ expectedEntry: "var foo = 100;" });
756
- mockSubDomainRequest();
757
- process.chdir("foo");
758
- await runWrangler("publish");
759
-
760
- expect(std.out).toMatchInlineSnapshot(`
761
- "Uploaded test-name (TIMINGS)
762
- Published test-name (TIMINGS)
763
- test-name.test-sub-domain.workers.dev"
764
- `);
765
- expect(std.err).toMatchInlineSnapshot(`""`);
766
- expect(std.warn).toMatchInlineSnapshot(`
767
- "▲ [WARNING] Processing ../wrangler.toml configuration:
768
-
769
- - Deprecation: \\"build.upload.main\\":
770
- Delete the \`build.upload.main\` and \`build.upload.dir\` fields.
771
- Then add the top level \`main\` field to your configuration file:
772
- \`\`\`
773
- main = \\"foo/index.js\\"
774
- \`\`\`
775
- - Deprecation: \\"build.upload.dir\\":
776
- Use the top level \\"main\\" field or a command-line argument to specify the entry-point for the
777
- Worker.
778
-
779
- "
780
- `);
781
- });
782
-
783
- it("should error when both `main` and `build.upload.main` are used", async () => {
784
- writeWranglerToml({
785
- main: "./index.js",
786
- build: {
787
- upload: {
788
- main: "./index.js",
789
- dir: "./foo",
790
- },
791
- },
792
- });
793
- await expect(runWrangler("publish")).rejects
794
- .toThrowErrorMatchingInlineSnapshot(`
795
- "Processing wrangler.toml configuration:
796
- - Don't define both the \`main\` and \`build.upload.main\` fields in your configuration.
797
- They serve the same purpose: to point to the entry-point of your worker.
798
- Delete the \`build.upload.main\` and \`build.upload.dir\` field from your config."
799
- `);
800
- });
801
-
802
- it("should be able to transpile TypeScript (esm)", async () => {
803
- writeWranglerToml();
804
- writeWorkerSource({ format: "ts" });
805
- mockUploadWorkerRequest({ expectedEntry: "var foo = 100;" });
806
- mockSubDomainRequest();
807
- await runWrangler("publish index.ts");
808
-
809
- expect(std.out).toMatchInlineSnapshot(`
810
- "Uploaded test-name (TIMINGS)
811
- Published test-name (TIMINGS)
812
- test-name.test-sub-domain.workers.dev"
813
- `);
814
- expect(std.err).toMatchInlineSnapshot(`""`);
815
- });
816
-
817
- it("should be able to transpile TypeScript (sw)", async () => {
818
- writeWranglerToml();
819
- writeWorkerSource({ format: "ts", type: "sw" });
820
- mockUploadWorkerRequest({
821
- expectedEntry: "var foo = 100;",
822
- expectedType: "sw",
823
- });
824
- mockSubDomainRequest();
825
- await runWrangler("publish index.ts");
826
-
827
- expect(std.out).toMatchInlineSnapshot(`
828
- "Uploaded test-name (TIMINGS)
829
- Published test-name (TIMINGS)
830
- test-name.test-sub-domain.workers.dev"
831
- `);
832
- expect(std.err).toMatchInlineSnapshot(`""`);
833
- });
834
-
835
- it("should add referenced text modules into the form upload", async () => {
836
- writeWranglerToml();
837
- fs.writeFileSync(
838
- "./index.js",
839
- `
840
- import txt from './textfile.txt';
841
- export default{
842
- fetch(){
843
- return new Response(txt);
844
- }
845
- }
846
- `
847
- );
848
- fs.writeFileSync("./textfile.txt", "Hello, World!");
849
- mockUploadWorkerRequest({
850
- expectedModules: {
851
- "./0a0a9f2a6772942557ab5355d76af442f8f65e01-textfile.txt":
852
- "Hello, World!",
853
- },
854
- });
855
- mockSubDomainRequest();
856
- await runWrangler("publish index.js");
857
- expect(std.out).toMatchInlineSnapshot(`
858
- "Uploaded test-name (TIMINGS)
859
- Published test-name (TIMINGS)
860
- test-name.test-sub-domain.workers.dev"
861
- `);
862
- expect(std.err).toMatchInlineSnapshot(`""`);
863
- });
864
-
865
- it("should be able to transpile entry-points in sub-directories (esm)", async () => {
866
- writeWranglerToml();
867
- writeWorkerSource({ basePath: "./src" });
868
- mockUploadWorkerRequest({ expectedEntry: "var foo = 100;" });
869
- mockSubDomainRequest();
870
-
871
- await runWrangler("publish ./src/index.js");
872
-
873
- expect(std.out).toMatchInlineSnapshot(`
874
- "Uploaded test-name (TIMINGS)
875
- Published test-name (TIMINGS)
876
- test-name.test-sub-domain.workers.dev"
877
- `);
878
- expect(std.err).toMatchInlineSnapshot(`""`);
879
- });
880
-
881
- it("should be able to transpile entry-points in sub-directories (sw)", async () => {
882
- writeWranglerToml();
883
- writeWorkerSource({ basePath: "./src", type: "sw" });
884
- mockUploadWorkerRequest({
885
- expectedEntry: "var foo = 100;",
886
- expectedType: "sw",
887
- });
888
- mockSubDomainRequest();
889
-
890
- await runWrangler("publish ./src/index.js");
891
-
892
- expect(std.out).toMatchInlineSnapshot(`
893
- "Uploaded test-name (TIMINGS)
894
- Published test-name (TIMINGS)
895
- test-name.test-sub-domain.workers.dev"
896
- `);
897
- expect(std.err).toMatchInlineSnapshot(`""`);
898
- });
899
-
900
- it('should error if a site definition doesn\'t have a "bucket" field', async () => {
901
- writeWranglerToml({
902
- // @ts-expect-error we're intentionally setting an invalid config
903
- site: {},
904
- });
905
- writeWorkerSource();
906
- mockUploadWorkerRequest();
907
- mockSubDomainRequest();
908
-
909
- await expect(runWrangler("publish ./index.js")).rejects
910
- .toThrowErrorMatchingInlineSnapshot(`
911
- "Processing wrangler.toml configuration:
912
- - \\"site.bucket\\" is a required field."
913
- `);
914
-
915
- expect(std.out).toMatchInlineSnapshot(`
916
- "
917
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
918
- `);
919
- expect(std.err).toMatchInlineSnapshot(`
920
- "X [ERROR] Processing wrangler.toml configuration:
921
-
922
- - \\"site.bucket\\" is a required field.
923
-
924
- "
925
- `);
926
- expect(normalizeSlashes(std.warn)).toMatchInlineSnapshot(`
927
- "▲ [WARNING] Processing wrangler.toml configuration:
928
-
929
- - Because you've defined a [site] configuration, we're defaulting to \\"workers-site\\" for the
930
- deprecated \`site.entry-point\`field.
931
- Add the top level \`main\` field to your configuration file:
932
- \`\`\`
933
- main = \\"workers-site/index.js\\"
934
- \`\`\`
935
-
936
- "
937
- `);
938
- });
939
-
940
- it("should warn if there is a `site.entry-point` configuration", async () => {
941
- const assets = [
942
- { filePath: "assets/file-1.txt", content: "Content of file-1" },
943
- { filePath: "assets/file-2.txt", content: "Content of file-2" },
944
- ];
945
- const kvNamespace = {
946
- title: "__test-name-workers_sites_assets",
947
- id: "__test-name-workers_sites_assets-id",
948
- };
949
-
950
- writeWranglerToml({
951
- site: {
952
- "entry-point": "./index.js",
953
- bucket: "assets",
954
- },
955
- });
956
- writeWorkerSource();
957
- writeAssets(assets);
958
- mockUploadWorkerRequest();
959
- mockSubDomainRequest();
960
- mockListKVNamespacesRequest(kvNamespace);
961
- mockKeyListRequest(kvNamespace.id, []);
962
- mockUploadAssetsToKVRequest(kvNamespace.id, assets);
963
- await runWrangler("publish ./index.js");
964
-
965
- expect(std).toMatchInlineSnapshot(`
966
- Object {
967
- "debug": "",
968
- "err": "",
969
- "out": "Reading assets/file-1.txt...
970
- Uploading as assets/file-1.2ca234f380.txt...
971
- Reading assets/file-2.txt...
972
- Uploading as assets/file-2.5938485188.txt...
973
- ↗️ Done syncing assets
974
- Uploaded test-name (TIMINGS)
975
- Published test-name (TIMINGS)
976
- test-name.test-sub-domain.workers.dev",
977
- "warn": "▲ [WARNING] Processing wrangler.toml configuration:
978
-
979
- - Deprecation: \\"site.entry-point\\":
980
- Delete the \`site.entry-point\` field, then add the top level \`main\` field to your configuration
981
- file:
982
- \`\`\`
983
- main = \\"index.js\\"
984
- \`\`\`
985
-
986
- ",
987
- }
988
- `);
989
- });
990
-
991
- it("should resolve site.entry-point relative to wrangler.toml", async () => {
992
- const assets = [
993
- { filePath: "assets/file-1.txt", content: "Content of file-1" },
994
- { filePath: "assets/file-2.txt", content: "Content of file-2" },
995
- ];
996
- const kvNamespace = {
997
- title: "__test-name-workers_sites_assets",
998
- id: "__test-name-workers_sites_assets-id",
999
- };
1000
- fs.mkdirSync("my-site");
1001
- process.chdir("my-site");
1002
- writeWranglerToml({
1003
- site: {
1004
- bucket: "assets",
1005
- "entry-point": "my-entry",
1006
- },
1007
- });
1008
- fs.mkdirSync("my-entry");
1009
- fs.writeFileSync("my-entry/index.js", "export default {}");
1010
- writeAssets(assets);
1011
- process.chdir("..");
1012
- mockUploadWorkerRequest();
1013
- mockSubDomainRequest();
1014
- mockListKVNamespacesRequest(kvNamespace);
1015
- mockKeyListRequest(kvNamespace.id, []);
1016
- mockUploadAssetsToKVRequest(kvNamespace.id, assets);
1017
- await runWrangler("publish --config ./my-site/wrangler.toml");
1018
-
1019
- expect(std.out).toMatchInlineSnapshot(`
1020
- "Reading assets/file-1.txt...
1021
- Uploading as assets/file-1.2ca234f380.txt...
1022
- Reading assets/file-2.txt...
1023
- Uploading as assets/file-2.5938485188.txt...
1024
- ↗️ Done syncing assets
1025
- Uploaded test-name (TIMINGS)
1026
- Published test-name (TIMINGS)
1027
- test-name.test-sub-domain.workers.dev"
1028
- `);
1029
- expect(std.err).toMatchInlineSnapshot(`""`);
1030
- expect(normalizeSlashes(std.warn)).toMatchInlineSnapshot(`
1031
- "▲ [WARNING] Processing my-site/wrangler.toml configuration:
1032
-
1033
- - Deprecation: \\"site.entry-point\\":
1034
- Delete the \`site.entry-point\` field, then add the top level \`main\` field to your configuration
1035
- file:
1036
- \`\`\`
1037
- main = \\"my-entry/index.js\\"
1038
- \`\`\`
1039
-
1040
- "
1041
- `);
1042
- });
1043
-
1044
- it("should error if both main and site.entry-point are specified", async () => {
1045
- writeWranglerToml({
1046
- main: "some-entry",
1047
- site: {
1048
- bucket: "some-bucket",
1049
- "entry-point": "./index.js",
1050
- },
1051
- });
1052
-
1053
- await expect(runWrangler("publish")).rejects
1054
- .toThrowErrorMatchingInlineSnapshot(`
1055
- "Processing wrangler.toml configuration:
1056
- - Don't define both the \`main\` and \`site.entry-point\` fields in your configuration.
1057
- They serve the same purpose: to point to the entry-point of your worker.
1058
- Delete the deprecated \`site.entry-point\` field from your config."
1059
- `);
1060
- });
1061
-
1062
- it("should error if there is no entry-point specified", async () => {
1063
- writeWranglerToml();
1064
- writeWorkerSource();
1065
- mockUploadWorkerRequest();
1066
- mockSubDomainRequest();
1067
- await expect(
1068
- runWrangler("publish")
1069
- ).rejects.toThrowErrorMatchingInlineSnapshot(
1070
- `"Missing entry-point: The entry-point should be specified via the command line (e.g. \`wrangler publish path/to/script\`) or the \`main\` config field."`
1071
- );
1072
-
1073
- expect(std.out).toMatchInlineSnapshot(`
1074
- "
1075
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
1076
- `);
1077
- expect(std.err).toMatchInlineSnapshot(`
1078
- "X [ERROR] Missing entry-point: The entry-point should be specified via the command line (e.g. \`wrangler publish path/to/script\`) or the \`main\` config field.
1079
-
1080
- "
1081
- `);
1082
- });
1083
- });
1084
-
1085
- describe("asset upload", () => {
1086
- it("should upload all the files in the directory specified by `config.site.bucket`", async () => {
1087
- const assets = [
1088
- { filePath: "assets/file-1.txt", content: "Content of file-1" },
1089
- { filePath: "assets/file-2.txt", content: "Content of file-2" },
1090
- ];
1091
- const kvNamespace = {
1092
- title: "__test-name-workers_sites_assets",
1093
- id: "__test-name-workers_sites_assets-id",
1094
- };
1095
- writeWranglerToml({
1096
- main: "./index.js",
1097
- site: {
1098
- bucket: "assets",
1099
- },
1100
- });
1101
- writeWorkerSource();
1102
- writeAssets(assets);
1103
- mockUploadWorkerRequest();
1104
- mockSubDomainRequest();
1105
- mockListKVNamespacesRequest(kvNamespace);
1106
- mockKeyListRequest(kvNamespace.id, []);
1107
- mockUploadAssetsToKVRequest(kvNamespace.id, assets);
1108
- await runWrangler("publish");
1109
-
1110
- expect(std.out).toMatchInlineSnapshot(`
1111
- "Reading assets/file-1.txt...
1112
- Uploading as assets/file-1.2ca234f380.txt...
1113
- Reading assets/file-2.txt...
1114
- Uploading as assets/file-2.5938485188.txt...
1115
- ↗️ Done syncing assets
1116
- Uploaded test-name (TIMINGS)
1117
- Published test-name (TIMINGS)
1118
- test-name.test-sub-domain.workers.dev"
1119
- `);
1120
- expect(std.err).toMatchInlineSnapshot(`""`);
1121
- });
1122
-
1123
- it("when using a service-worker type, it should add an asset manifest as a text_blob, and bind to a namespace", async () => {
1124
- const assets = [
1125
- { filePath: "assets/file-1.txt", content: "Content of file-1" },
1126
- { filePath: "assets/file-2.txt", content: "Content of file-2" },
1127
- ];
1128
- const kvNamespace = {
1129
- title: "__test-name-workers_sites_assets",
1130
- id: "__test-name-workers_sites_assets-id",
1131
- };
1132
- writeWranglerToml({
1133
- main: "./index.js",
1134
- site: {
1135
- bucket: "assets",
1136
- },
1137
- });
1138
- writeWorkerSource({ type: "sw" });
1139
- writeAssets(assets);
1140
- mockUploadWorkerRequest({
1141
- expectedType: "sw",
1142
- expectedModules: {
1143
- __STATIC_CONTENT_MANIFEST:
1144
- '{"file-1.txt":"assets/file-1.2ca234f380.txt","file-2.txt":"assets/file-2.5938485188.txt"}',
1145
- },
1146
- expectedBindings: [
1147
- {
1148
- name: "__STATIC_CONTENT",
1149
- namespace_id: "__test-name-workers_sites_assets-id",
1150
- type: "kv_namespace",
1151
- },
1152
- {
1153
- name: "__STATIC_CONTENT_MANIFEST",
1154
- part: "__STATIC_CONTENT_MANIFEST",
1155
- type: "text_blob",
1156
- },
1157
- ],
1158
- });
1159
- mockSubDomainRequest();
1160
- mockListKVNamespacesRequest(kvNamespace);
1161
- mockKeyListRequest(kvNamespace.id, []);
1162
- mockUploadAssetsToKVRequest(kvNamespace.id, assets);
1163
-
1164
- await runWrangler("publish");
1165
-
1166
- expect(std.out).toMatchInlineSnapshot(`
1167
- "Reading assets/file-1.txt...
1168
- Uploading as assets/file-1.2ca234f380.txt...
1169
- Reading assets/file-2.txt...
1170
- Uploading as assets/file-2.5938485188.txt...
1171
- ↗️ Done syncing assets
1172
- Uploaded test-name (TIMINGS)
1173
- Published test-name (TIMINGS)
1174
- test-name.test-sub-domain.workers.dev"
1175
- `);
1176
- expect(std.err).toMatchInlineSnapshot(`""`);
1177
- });
1178
-
1179
- it("when using a module worker type, it should add an asset manifest module, and bind to a namespace", async () => {
1180
- const assets = [
1181
- { filePath: "assets/file-1.txt", content: "Content of file-1" },
1182
- { filePath: "assets/file-2.txt", content: "Content of file-2" },
1183
- ];
1184
- const kvNamespace = {
1185
- title: "__test-name-workers_sites_assets",
1186
- id: "__test-name-workers_sites_assets-id",
1187
- };
1188
- writeWranglerToml({
1189
- main: "./index.js",
1190
- site: {
1191
- bucket: "assets",
1192
- },
1193
- });
1194
- writeWorkerSource({ type: "esm" });
1195
- writeAssets(assets);
1196
- mockUploadWorkerRequest({
1197
- expectedBindings: [
1198
- {
1199
- name: "__STATIC_CONTENT",
1200
- namespace_id: "__test-name-workers_sites_assets-id",
1201
- type: "kv_namespace",
1202
- },
1203
- ],
1204
- expectedModules: {
1205
- __STATIC_CONTENT_MANIFEST:
1206
- '{"file-1.txt":"assets/file-1.2ca234f380.txt","file-2.txt":"assets/file-2.5938485188.txt"}',
1207
- },
1208
- });
1209
- mockSubDomainRequest();
1210
- mockListKVNamespacesRequest(kvNamespace);
1211
- mockKeyListRequest(kvNamespace.id, []);
1212
- mockUploadAssetsToKVRequest(kvNamespace.id, assets);
1213
-
1214
- await runWrangler("publish");
1215
-
1216
- expect(std.out).toMatchInlineSnapshot(`
1217
- "Reading assets/file-1.txt...
1218
- Uploading as assets/file-1.2ca234f380.txt...
1219
- Reading assets/file-2.txt...
1220
- Uploading as assets/file-2.5938485188.txt...
1221
- ↗️ Done syncing assets
1222
- Uploaded test-name (TIMINGS)
1223
- Published test-name (TIMINGS)
1224
- test-name.test-sub-domain.workers.dev"
1225
- `);
1226
- expect(std.err).toMatchInlineSnapshot(`""`);
1227
- });
1228
-
1229
- it("should make environment specific kv namespace for assets, even for service envs", async () => {
1230
- // This is the same test as the one before this, but with an env arg
1231
- const assets = [
1232
- { filePath: "assets/file-1.txt", content: "Content of file-1" },
1233
- { filePath: "assets/file-2.txt", content: "Content of file-2" },
1234
- ];
1235
- const kvNamespace = {
1236
- title: "__test-name-some-env-workers_sites_assets",
1237
- id: "__test-name-some-env-workers_sites_assets-id",
1238
- };
1239
- writeWranglerToml({
1240
- main: "./index.js",
1241
- site: {
1242
- bucket: "assets",
1243
- },
1244
- env: { "some-env": {} },
1245
- });
1246
- writeWorkerSource();
1247
- writeAssets(assets);
1248
- mockUploadWorkerRequest({
1249
- env: "some-env",
1250
- expectedBindings: [
1251
- {
1252
- name: "__STATIC_CONTENT",
1253
- namespace_id: "__test-name-some-env-workers_sites_assets-id",
1254
- type: "kv_namespace",
1255
- },
1256
- ],
1257
- });
1258
- mockSubDomainRequest();
1259
- mockListKVNamespacesRequest(kvNamespace);
1260
- mockKeyListRequest(kvNamespace.id, []);
1261
- mockUploadAssetsToKVRequest(kvNamespace.id, assets);
1262
- await runWrangler("publish --env some-env --legacy-env false");
1263
-
1264
- expect(std.out).toMatchInlineSnapshot(`
1265
- "Reading assets/file-1.txt...
1266
- Uploading as assets/file-1.2ca234f380.txt...
1267
- Reading assets/file-2.txt...
1268
- Uploading as assets/file-2.5938485188.txt...
1269
- ↗️ Done syncing assets
1270
- Uploaded test-name (some-env) (TIMINGS)
1271
- Published test-name (some-env) (TIMINGS)
1272
- some-env.test-name.test-sub-domain.workers.dev"
1273
- `);
1274
- expect(std.err).toMatchInlineSnapshot(`""`);
1275
- });
1276
-
1277
- it("should make environment specific kv namespace for assets, even for legacy envs", async () => {
1278
- // And this is the same test as the one before this, but with legacyEnv:true
1279
- const assets = [
1280
- { filePath: "assets/file-1.txt", content: "Content of file-1" },
1281
- { filePath: "assets/file-2.txt", content: "Content of file-2" },
1282
- ];
1283
- const kvNamespace = {
1284
- title: "__test-name-some-env-workers_sites_assets",
1285
- id: "__test-name-some-env-workers_sites_assets-id",
1286
- };
1287
- writeWranglerToml({
1288
- main: "./index.js",
1289
- site: {
1290
- bucket: "assets",
1291
- },
1292
- env: { "some-env": {} },
1293
- });
1294
- writeWorkerSource();
1295
- writeAssets(assets);
1296
- mockUploadWorkerRequest({
1297
- legacyEnv: true,
1298
- env: "some-env",
1299
- expectedBindings: [
1300
- {
1301
- name: "__STATIC_CONTENT",
1302
- namespace_id: "__test-name-some-env-workers_sites_assets-id",
1303
- type: "kv_namespace",
1304
- },
1305
- ],
1306
- });
1307
- mockSubDomainRequest();
1308
- mockListKVNamespacesRequest(kvNamespace);
1309
- mockKeyListRequest(kvNamespace.id, []);
1310
- mockUploadAssetsToKVRequest(kvNamespace.id, assets);
1311
- await runWrangler("publish --env some-env --legacy-env true");
1312
-
1313
- expect(std.out).toMatchInlineSnapshot(`
1314
- "Reading assets/file-1.txt...
1315
- Uploading as assets/file-1.2ca234f380.txt...
1316
- Reading assets/file-2.txt...
1317
- Uploading as assets/file-2.5938485188.txt...
1318
- ↗️ Done syncing assets
1319
- Uploaded test-name-some-env (TIMINGS)
1320
- Published test-name-some-env (TIMINGS)
1321
- test-name-some-env.test-sub-domain.workers.dev"
1322
- `);
1323
- expect(std.err).toMatchInlineSnapshot(`""`);
1324
- });
1325
-
1326
- it("should only upload files that are not already in the KV namespace", async () => {
1327
- const assets = [
1328
- { filePath: "assets/file-1.txt", content: "Content of file-1" },
1329
- { filePath: "assets/file-2.txt", content: "Content of file-2" },
1330
- ];
1331
- const kvNamespace = {
1332
- title: "__test-name-workers_sites_assets",
1333
- id: "__test-name-workers_sites_assets-id",
1334
- };
1335
- writeWranglerToml({
1336
- main: "./index.js",
1337
- site: {
1338
- bucket: "assets",
1339
- },
1340
- });
1341
- writeWorkerSource();
1342
- writeAssets(assets);
1343
- mockUploadWorkerRequest();
1344
- mockSubDomainRequest();
1345
- mockListKVNamespacesRequest(kvNamespace);
1346
- // Put file-1 in the KV namespace
1347
- mockKeyListRequest(kvNamespace.id, [
1348
- { name: "assets/file-1.2ca234f380.txt" },
1349
- ]);
1350
- // Check we do not upload file-1
1351
- mockUploadAssetsToKVRequest(
1352
- kvNamespace.id,
1353
- assets.filter((a) => a.filePath !== "assets/file-1.txt")
1354
- );
1355
- await runWrangler("publish");
1356
-
1357
- expect(std.out).toMatchInlineSnapshot(`
1358
- "Reading assets/file-1.txt...
1359
- Skipping - already uploaded.
1360
- Reading assets/file-2.txt...
1361
- Uploading as assets/file-2.5938485188.txt...
1362
- ↗️ Done syncing assets
1363
- Uploaded test-name (TIMINGS)
1364
- Published test-name (TIMINGS)
1365
- test-name.test-sub-domain.workers.dev"
1366
- `);
1367
- expect(std.err).toMatchInlineSnapshot(`""`);
1368
- });
1369
-
1370
- it("should only upload files that match the `site-include` arg", async () => {
1371
- const assets = [
1372
- { filePath: "assets/file-1.txt", content: "Content of file-1" },
1373
- { filePath: "assets/file-2.txt", content: "Content of file-2" },
1374
- ];
1375
- const kvNamespace = {
1376
- title: "__test-name-workers_sites_assets",
1377
- id: "__test-name-workers_sites_assets-id",
1378
- };
1379
- writeWranglerToml({
1380
- main: "./index.js",
1381
- site: {
1382
- bucket: "assets",
1383
- },
1384
- });
1385
- writeWorkerSource();
1386
- writeAssets(assets);
1387
- mockUploadWorkerRequest();
1388
- mockSubDomainRequest();
1389
- mockListKVNamespacesRequest(kvNamespace);
1390
- mockKeyListRequest(kvNamespace.id, []);
1391
- // Check we only upload file-1
1392
- mockUploadAssetsToKVRequest(
1393
- kvNamespace.id,
1394
- assets.filter((a) => a.filePath === "assets/file-1.txt")
1395
- );
1396
- await runWrangler("publish --site-include file-1.txt");
1397
-
1398
- expect(std.out).toMatchInlineSnapshot(`
1399
- "Reading assets/file-1.txt...
1400
- Uploading as assets/file-1.2ca234f380.txt...
1401
- ↗️ Done syncing assets
1402
- Uploaded test-name (TIMINGS)
1403
- Published test-name (TIMINGS)
1404
- test-name.test-sub-domain.workers.dev"
1405
- `);
1406
- expect(std.err).toMatchInlineSnapshot(`""`);
1407
- });
1408
-
1409
- it("should not upload files that match the `site-exclude` arg", async () => {
1410
- const assets = [
1411
- { filePath: "assets/file-1.txt", content: "Content of file-1" },
1412
- { filePath: "assets/file-2.txt", content: "Content of file-2" },
1413
- ];
1414
- const kvNamespace = {
1415
- title: "__test-name-workers_sites_assets",
1416
- id: "__test-name-workers_sites_assets-id",
1417
- };
1418
- writeWranglerToml({
1419
- main: "./index.js",
1420
- site: {
1421
- bucket: "assets",
1422
- },
1423
- });
1424
- writeWorkerSource();
1425
- writeAssets(assets);
1426
- mockUploadWorkerRequest();
1427
- mockSubDomainRequest();
1428
- mockListKVNamespacesRequest(kvNamespace);
1429
- mockKeyListRequest(kvNamespace.id, []);
1430
- // Check we only upload file-1
1431
- mockUploadAssetsToKVRequest(
1432
- kvNamespace.id,
1433
- assets.filter((a) => a.filePath === "assets/file-1.txt")
1434
- );
1435
- await runWrangler("publish --site-exclude file-2.txt");
1436
-
1437
- expect(std.out).toMatchInlineSnapshot(`
1438
- "Reading assets/file-1.txt...
1439
- Uploading as assets/file-1.2ca234f380.txt...
1440
- ↗️ Done syncing assets
1441
- Uploaded test-name (TIMINGS)
1442
- Published test-name (TIMINGS)
1443
- test-name.test-sub-domain.workers.dev"
1444
- `);
1445
- expect(std.err).toMatchInlineSnapshot(`""`);
1446
- });
1447
-
1448
- it("should only upload files that match the `site.include` config", async () => {
1449
- const assets = [
1450
- { filePath: "assets/file-1.txt", content: "Content of file-1" },
1451
- { filePath: "assets/file-2.txt", content: "Content of file-2" },
1452
- ];
1453
- const kvNamespace = {
1454
- title: "__test-name-workers_sites_assets",
1455
- id: "__test-name-workers_sites_assets-id",
1456
- };
1457
- writeWranglerToml({
1458
- main: "./index.js",
1459
- site: {
1460
- bucket: "assets",
1461
- include: ["file-1.txt"],
1462
- },
1463
- });
1464
- writeWorkerSource();
1465
- writeAssets(assets);
1466
- mockUploadWorkerRequest();
1467
- mockSubDomainRequest();
1468
- mockListKVNamespacesRequest(kvNamespace);
1469
- mockKeyListRequest(kvNamespace.id, []);
1470
- // Check we only upload file-1
1471
- mockUploadAssetsToKVRequest(
1472
- kvNamespace.id,
1473
- assets.filter((a) => a.filePath === "assets/file-1.txt")
1474
- );
1475
- await runWrangler("publish");
1476
-
1477
- expect(std.out).toMatchInlineSnapshot(`
1478
- "Reading assets/file-1.txt...
1479
- Uploading as assets/file-1.2ca234f380.txt...
1480
- ↗️ Done syncing assets
1481
- Uploaded test-name (TIMINGS)
1482
- Published test-name (TIMINGS)
1483
- test-name.test-sub-domain.workers.dev"
1484
- `);
1485
- expect(std.err).toMatchInlineSnapshot(`""`);
1486
- });
1487
-
1488
- it("should not upload files that match the `site.exclude` config", async () => {
1489
- const assets = [
1490
- { filePath: "assets/file-1.txt", content: "Content of file-1" },
1491
- { filePath: "assets/file-2.txt", content: "Content of file-2" },
1492
- ];
1493
- const kvNamespace = {
1494
- title: "__test-name-workers_sites_assets",
1495
- id: "__test-name-workers_sites_assets-id",
1496
- };
1497
- writeWranglerToml({
1498
- main: "./index.js",
1499
- site: {
1500
- bucket: "assets",
1501
- exclude: ["file-2.txt"],
1502
- },
1503
- });
1504
- writeWorkerSource();
1505
- writeAssets(assets);
1506
- mockUploadWorkerRequest();
1507
- mockSubDomainRequest();
1508
- mockListKVNamespacesRequest(kvNamespace);
1509
- mockKeyListRequest(kvNamespace.id, []);
1510
- // Check we only upload file-1
1511
- mockUploadAssetsToKVRequest(
1512
- kvNamespace.id,
1513
- assets.filter((a) => a.filePath === "assets/file-1.txt")
1514
- );
1515
- await runWrangler("publish");
1516
-
1517
- expect(std.out).toMatchInlineSnapshot(`
1518
- "Reading assets/file-1.txt...
1519
- Uploading as assets/file-1.2ca234f380.txt...
1520
- ↗️ Done syncing assets
1521
- Uploaded test-name (TIMINGS)
1522
- Published test-name (TIMINGS)
1523
- test-name.test-sub-domain.workers.dev"
1524
- `);
1525
- expect(std.err).toMatchInlineSnapshot(`""`);
1526
- });
1527
-
1528
- it("should use `site-include` arg over `site.include` config", async () => {
1529
- const assets = [
1530
- { filePath: "assets/file-1.txt", content: "Content of file-1" },
1531
- { filePath: "assets/file-2.txt", content: "Content of file-2" },
1532
- ];
1533
- const kvNamespace = {
1534
- title: "__test-name-workers_sites_assets",
1535
- id: "__test-name-workers_sites_assets-id",
1536
- };
1537
- writeWranglerToml({
1538
- main: "./index.js",
1539
- site: {
1540
- bucket: "assets",
1541
- include: ["file-2.txt"],
1542
- },
1543
- });
1544
- writeWorkerSource();
1545
- writeAssets(assets);
1546
- mockUploadWorkerRequest();
1547
- mockSubDomainRequest();
1548
- mockListKVNamespacesRequest(kvNamespace);
1549
- mockKeyListRequest(kvNamespace.id, []);
1550
- // Check we only upload file-1
1551
- mockUploadAssetsToKVRequest(
1552
- kvNamespace.id,
1553
- assets.filter((a) => a.filePath === "assets/file-1.txt")
1554
- );
1555
- await runWrangler("publish --site-include file-1.txt");
1556
-
1557
- expect(std.out).toMatchInlineSnapshot(`
1558
- "Reading assets/file-1.txt...
1559
- Uploading as assets/file-1.2ca234f380.txt...
1560
- ↗️ Done syncing assets
1561
- Uploaded test-name (TIMINGS)
1562
- Published test-name (TIMINGS)
1563
- test-name.test-sub-domain.workers.dev"
1564
- `);
1565
- expect(std.err).toMatchInlineSnapshot(`""`);
1566
- });
1567
-
1568
- it("should use `site-exclude` arg over `site.exclude` config", async () => {
1569
- const assets = [
1570
- { filePath: "assets/file-1.txt", content: "Content of file-1" },
1571
- { filePath: "assets/file-2.txt", content: "Content of file-2" },
1572
- ];
1573
- const kvNamespace = {
1574
- title: "__test-name-workers_sites_assets",
1575
- id: "__test-name-workers_sites_assets-id",
1576
- };
1577
- writeWranglerToml({
1578
- main: "./index.js",
1579
- site: {
1580
- bucket: "assets",
1581
- exclude: ["assets/file-1.txt"],
1582
- },
1583
- });
1584
- writeWorkerSource();
1585
- writeAssets(assets);
1586
- mockUploadWorkerRequest();
1587
- mockSubDomainRequest();
1588
- mockListKVNamespacesRequest(kvNamespace);
1589
- mockKeyListRequest(kvNamespace.id, []);
1590
- // Check we only upload file-1
1591
- mockUploadAssetsToKVRequest(
1592
- kvNamespace.id,
1593
- assets.filter((a) => a.filePath.endsWith("assets/file-1.txt"))
1594
- );
1595
- await runWrangler("publish --site-exclude file-2.txt");
1596
-
1597
- expect(std.out).toMatchInlineSnapshot(`
1598
- "Reading assets/file-1.txt...
1599
- Uploading as assets/file-1.2ca234f380.txt...
1600
- ↗️ Done syncing assets
1601
- Uploaded test-name (TIMINGS)
1602
- Published test-name (TIMINGS)
1603
- test-name.test-sub-domain.workers.dev"
1604
- `);
1605
- expect(std.err).toMatchInlineSnapshot(`""`);
1606
- });
1607
-
1608
- it("should walk directories except node_modules", async () => {
1609
- const assets = [
1610
- {
1611
- filePath: "assets/directory-1/file-1.txt",
1612
- content: "Content of file-1",
1613
- },
1614
- {
1615
- filePath: "assets/node_modules/file-2.txt",
1616
- content: "Content of file-2",
1617
- },
1618
- ];
1619
- const kvNamespace = {
1620
- title: "__test-name-workers_sites_assets",
1621
- id: "__test-name-workers_sites_assets-id",
1622
- };
1623
- writeWranglerToml({
1624
- main: "./index.js",
1625
- site: {
1626
- bucket: "assets",
1627
- },
1628
- });
1629
- writeWorkerSource();
1630
- writeAssets(assets);
1631
- mockUploadWorkerRequest();
1632
- mockSubDomainRequest();
1633
- mockListKVNamespacesRequest(kvNamespace);
1634
- mockKeyListRequest(kvNamespace.id, []);
1635
- // Only expect file-1 to be uploaded
1636
- mockUploadAssetsToKVRequest(kvNamespace.id, assets.slice(0, 1));
1637
- await runWrangler("publish");
1638
-
1639
- expect(std.out).toMatchInlineSnapshot(`
1640
- "Reading assets/directory-1/file-1.txt...
1641
- Uploading as assets/directory-1/file-1.2ca234f380.txt...
1642
- ↗️ Done syncing assets
1643
- Uploaded test-name (TIMINGS)
1644
- Published test-name (TIMINGS)
1645
- test-name.test-sub-domain.workers.dev"
1646
- `);
1647
- expect(std.err).toMatchInlineSnapshot(`""`);
1648
- });
1649
-
1650
- it("should skip hidden files and directories except `.well-known`", async () => {
1651
- const assets = [
1652
- {
1653
- filePath: "assets/.hidden-file.txt",
1654
- content: "Content of hidden-file",
1655
- },
1656
- {
1657
- filePath: "assets/.hidden/file-1.txt",
1658
- content: "Content of file-1",
1659
- },
1660
- {
1661
- filePath: "assets/.well-known/file-2.txt",
1662
- content: "Content of file-2",
1663
- },
1664
- ];
1665
- const kvNamespace = {
1666
- title: "__test-name-workers_sites_assets",
1667
- id: "__test-name-workers_sites_assets-id",
1668
- };
1669
- writeWranglerToml({
1670
- main: "./index.js",
1671
- site: {
1672
- bucket: "assets",
1673
- },
1674
- });
1675
- writeWorkerSource();
1676
- writeAssets(assets);
1677
- mockUploadWorkerRequest();
1678
- mockSubDomainRequest();
1679
- mockListKVNamespacesRequest(kvNamespace);
1680
- mockKeyListRequest(kvNamespace.id, []);
1681
- // Only expect file-2 to be uploaded
1682
- mockUploadAssetsToKVRequest(kvNamespace.id, assets.slice(2));
1683
- await runWrangler("publish");
1684
-
1685
- expect(std.out).toMatchInlineSnapshot(`
1686
- "Reading assets/.well-known/file-2.txt...
1687
- Uploading as assets/.well-known/file-2.5938485188.txt...
1688
- ↗️ Done syncing assets
1689
- Uploaded test-name (TIMINGS)
1690
- Published test-name (TIMINGS)
1691
- test-name.test-sub-domain.workers.dev"
1692
- `);
1693
- expect(std.err).toMatchInlineSnapshot(`""`);
1694
- });
1695
-
1696
- it("should error if the asset is over 25Mb", async () => {
1697
- const assets = [
1698
- {
1699
- filePath: "assets/large-file.txt",
1700
- // This file is greater than 25MiB when base64 encoded but small enough to be uploaded.
1701
- content: "X".repeat(25 * 1024 * 1024 * 0.8 + 1),
1702
- },
1703
- {
1704
- filePath: "assets/too-large-file.txt",
1705
- content: "X".repeat(25 * 1024 * 1024 + 1),
1706
- },
1707
- ];
1708
- const kvNamespace = {
1709
- title: "__test-name-workers_sites_assets",
1710
- id: "__test-name-workers_sites_assets-id",
1711
- };
1712
- writeWranglerToml({
1713
- main: "./index.js",
1714
- site: {
1715
- bucket: "assets",
1716
- exclude: ["assets/file-1.txt"],
1717
- },
1718
- });
1719
- writeWorkerSource();
1720
- writeAssets(assets);
1721
- mockUploadWorkerRequest();
1722
- mockSubDomainRequest();
1723
- mockListKVNamespacesRequest(kvNamespace);
1724
- mockKeyListRequest(kvNamespace.id, []);
1725
-
1726
- await expect(
1727
- runWrangler("publish")
1728
- ).rejects.toThrowErrorMatchingInlineSnapshot(
1729
- `"File assets/too-large-file.txt is too big, it should be under 25 MiB. See https://developers.cloudflare.com/workers/platform/limits#kv-limits"`
1730
- );
1731
-
1732
- expect(std.out).toMatchInlineSnapshot(`
1733
- "Reading assets/large-file.txt...
1734
- Uploading as assets/large-file.0ea0637a45.txt...
1735
-
1736
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
1737
- `);
1738
- expect(std.err).toMatchInlineSnapshot(`
1739
- "X [ERROR] File assets/too-large-file.txt is too big, it should be under 25 MiB. See https://developers.cloudflare.com/workers/platform/limits#kv-limits
1740
-
1741
- "
1742
- `);
1743
- });
1744
-
1745
- it("should error if the asset key is over 512 characters", async () => {
1746
- const longFilePathAsset = {
1747
- filePath: "assets/" + "folder/".repeat(100) + "file.txt",
1748
- content: "content of file",
1749
- };
1750
- const kvNamespace = {
1751
- title: "__test-name-workers_sites_assets",
1752
- id: "__test-name-workers_sites_assets-id",
1753
- };
1754
- writeWranglerToml({
1755
- main: "./index.js",
1756
- site: {
1757
- bucket: "assets",
1758
- },
1759
- });
1760
- writeWorkerSource();
1761
- writeAssets([longFilePathAsset]);
1762
- mockUploadWorkerRequest();
1763
- mockSubDomainRequest();
1764
- mockListKVNamespacesRequest(kvNamespace);
1765
- mockKeyListRequest(kvNamespace.id, []);
1766
-
1767
- await expect(
1768
- runWrangler("publish")
1769
- ).rejects.toThrowErrorMatchingInlineSnapshot(
1770
- `"The asset path key \\"assets/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/file.3da0d0cd12.txt\\" exceeds the maximum key size limit of 512. See https://developers.cloudflare.com/workers/platform/limits#kv-limits\\","`
1771
- );
1772
-
1773
- expect(std.out).toMatchInlineSnapshot(`
1774
- "Reading assets/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/file.txt...
1775
-
1776
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
1777
- `);
1778
- expect(std.err).toMatchInlineSnapshot(`
1779
- "X [ERROR] The asset path key \\"assets/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/file.3da0d0cd12.txt\\" exceeds the maximum key size limit of 512. See https://developers.cloudflare.com/workers/platform/limits#kv-limits\\",
1780
-
1781
- "
1782
- `);
1783
- });
1784
-
1785
- it("should delete uploaded assets that aren't included anymore", async () => {
1786
- const assets = [
1787
- { filePath: "assets/file-1.txt", content: "Content of file-1" },
1788
- { filePath: "assets/file-2.txt", content: "Content of file-2" },
1789
- ];
1790
- const kvNamespace = {
1791
- title: "__test-name-workers_sites_assets",
1792
- id: "__test-name-workers_sites_assets-id",
1793
- };
1794
- writeWranglerToml({
1795
- main: "./index.js",
1796
- site: {
1797
- bucket: "assets",
1798
- },
1799
- });
1800
- writeWorkerSource();
1801
- writeAssets(assets);
1802
- mockUploadWorkerRequest();
1803
- mockSubDomainRequest();
1804
- mockListKVNamespacesRequest(kvNamespace);
1805
- mockKeyListRequest(kvNamespace.id, [
1806
- // Put file-1 in the KV namespace
1807
- { name: "assets/file-1.2ca234f380.txt" },
1808
- // As well as a couple from a previous upload
1809
- { name: "assets/file-3.somehash.txt" },
1810
- { name: "assets/file-4.anotherhash.txt" },
1811
- ]);
1812
-
1813
- // we upload only file-1.txt
1814
- mockUploadAssetsToKVRequest(kvNamespace.id, [
1815
- ...assets.filter((a) => a.filePath !== "assets/file-1.txt"),
1816
- ]);
1817
-
1818
- // and mark file-3 and file-4 for deletion
1819
- mockDeleteUnusedAssetsRequest(kvNamespace.id, [
1820
- "assets/file-3.somehash.txt",
1821
- "assets/file-4.anotherhash.txt",
1822
- ]);
1823
-
1824
- await runWrangler("publish");
1825
-
1826
- expect(std.out).toMatchInlineSnapshot(`
1827
- "Reading assets/file-1.txt...
1828
- Skipping - already uploaded.
1829
- Reading assets/file-2.txt...
1830
- Uploading as assets/file-2.5938485188.txt...
1831
- Deleting assets/file-3.somehash.txt from the asset store...
1832
- Deleting assets/file-4.anotherhash.txt from the asset store...
1833
- ↗️ Done syncing assets
1834
- Uploaded test-name (TIMINGS)
1835
- Published test-name (TIMINGS)
1836
- test-name.test-sub-domain.workers.dev"
1837
- `);
1838
- expect(std.err).toMatchInlineSnapshot(`""`);
1839
- });
1840
- });
1841
-
1842
- describe("workers_dev setting", () => {
1843
- it("should publish to a workers.dev domain if workers_dev is undefined", async () => {
1844
- writeWranglerToml();
1845
- writeWorkerSource();
1846
- mockUploadWorkerRequest();
1847
- mockSubDomainRequest();
1848
-
1849
- await runWrangler("publish ./index");
1850
-
1851
- expect(std.out).toMatchInlineSnapshot(`
1852
- "Uploaded test-name (TIMINGS)
1853
- Published test-name (TIMINGS)
1854
- test-name.test-sub-domain.workers.dev"
1855
- `);
1856
- expect(std.err).toMatchInlineSnapshot(`""`);
1857
- });
1858
-
1859
- it("should publish to the workers.dev domain if workers_dev is `true`", async () => {
1860
- writeWranglerToml({
1861
- workers_dev: true,
1862
- });
1863
- writeWorkerSource();
1864
- mockUploadWorkerRequest({ available_on_subdomain: false });
1865
- mockSubDomainRequest();
1866
- mockUpdateWorkerRequest({ enabled: true });
1867
-
1868
- await runWrangler("publish ./index");
1869
-
1870
- expect(std.out).toMatchInlineSnapshot(`
1871
- "Uploaded test-name (TIMINGS)
1872
- Published test-name (TIMINGS)
1873
- test-name.test-sub-domain.workers.dev"
1874
- `);
1875
- expect(std.err).toMatchInlineSnapshot(`""`);
1876
- });
1877
-
1878
- it("should not try to enable the workers.dev domain if it has been enabled before", async () => {
1879
- writeWranglerToml({
1880
- workers_dev: true,
1881
- });
1882
- writeWorkerSource();
1883
- mockUploadWorkerRequest({ available_on_subdomain: true });
1884
- mockSubDomainRequest();
1885
-
1886
- await runWrangler("publish ./index");
1887
-
1888
- expect(std.out).toMatchInlineSnapshot(`
1889
- "Uploaded test-name (TIMINGS)
1890
- Published test-name (TIMINGS)
1891
- test-name.test-sub-domain.workers.dev"
1892
- `);
1893
- expect(std.err).toMatchInlineSnapshot(`""`);
1894
- });
1895
-
1896
- it("should disable the workers.dev domain if workers_dev is `false`", async () => {
1897
- writeWranglerToml({
1898
- workers_dev: false,
1899
- });
1900
- writeWorkerSource();
1901
- mockUploadWorkerRequest();
1902
- mockUpdateWorkerRequest({ enabled: false });
1903
-
1904
- await runWrangler("publish ./index");
1905
-
1906
- expect(std.out).toMatchInlineSnapshot(`
1907
- "Uploaded test-name (TIMINGS)
1908
- No publish targets for test-name (TIMINGS)"
1909
- `);
1910
- expect(std.err).toMatchInlineSnapshot(`""`);
1911
- });
1912
-
1913
- it("should not try to disable the workers.dev domain if it is not already available", async () => {
1914
- writeWranglerToml({
1915
- workers_dev: false,
1916
- });
1917
- writeWorkerSource();
1918
- mockSubDomainRequest("test-sub-domain", false);
1919
- mockUploadWorkerRequest({ available_on_subdomain: false });
1920
-
1921
- // note the lack of a mock for the subdomain disable request
1922
-
1923
- await runWrangler("publish ./index");
1924
-
1925
- expect(std.out).toMatchInlineSnapshot(`
1926
- "Uploaded test-name (TIMINGS)
1927
- No publish targets for test-name (TIMINGS)"
1928
- `);
1929
- expect(std.err).toMatchInlineSnapshot(`""`);
1930
- });
1931
-
1932
- it("should disable the workers.dev domain if workers_dev is undefined but overwritten to `false` in environment", async () => {
1933
- writeWranglerToml({
1934
- env: {
1935
- dev: {
1936
- workers_dev: false,
1937
- },
1938
- },
1939
- });
1940
- writeWorkerSource();
1941
- mockUploadWorkerRequest({
1942
- env: "dev",
1943
- });
1944
- mockUpdateWorkerRequest({ enabled: false, env: "dev" });
1945
-
1946
- await runWrangler("publish ./index --env dev --legacy-env false");
1947
-
1948
- expect(std.out).toMatchInlineSnapshot(`
1949
- "Uploaded test-name (dev) (TIMINGS)
1950
- No publish targets for test-name (dev) (TIMINGS)"
1951
- `);
1952
- expect(std.err).toMatchInlineSnapshot(`""`);
1953
- });
1954
-
1955
- it("should disable the workers.dev domain if workers_dev is `true` but overwritten to `false` in environment", async () => {
1956
- writeWranglerToml({
1957
- workers_dev: true,
1958
- env: {
1959
- dev: {
1960
- workers_dev: false,
1961
- },
1962
- },
1963
- });
1964
- writeWorkerSource();
1965
- mockUploadWorkerRequest({
1966
- env: "dev",
1967
- });
1968
- mockUpdateWorkerRequest({ enabled: false, env: "dev" });
1969
-
1970
- await runWrangler("publish ./index --env dev --legacy-env false");
1971
-
1972
- expect(std.out).toMatchInlineSnapshot(`
1973
- "Uploaded test-name (dev) (TIMINGS)
1974
- No publish targets for test-name (dev) (TIMINGS)"
1975
- `);
1976
- expect(std.err).toMatchInlineSnapshot(`""`);
1977
- });
1978
-
1979
- it("should publish to a workers.dev domain if workers_dev is undefined but overwritten to `true` in environment", async () => {
1980
- writeWranglerToml({
1981
- env: {
1982
- dev: {
1983
- workers_dev: true,
1984
- },
1985
- },
1986
- });
1987
- writeWorkerSource();
1988
- mockUploadWorkerRequest({
1989
- env: "dev",
1990
- });
1991
- mockSubDomainRequest();
1992
- mockUpdateWorkerRequest({ enabled: true, env: "dev" });
1993
-
1994
- await runWrangler("publish ./index --env dev --legacy-env false");
1995
-
1996
- expect(std.out).toMatchInlineSnapshot(`
1997
- "Uploaded test-name (dev) (TIMINGS)
1998
- Published test-name (dev) (TIMINGS)
1999
- dev.test-name.test-sub-domain.workers.dev"
2000
- `);
2001
- expect(std.err).toMatchInlineSnapshot(`""`);
2002
- });
2003
-
2004
- it("should publish to a workers.dev domain if workers_dev is `false` but overwritten to `true` in environment", async () => {
2005
- writeWranglerToml({
2006
- workers_dev: false,
2007
- env: {
2008
- dev: {
2009
- workers_dev: true,
2010
- },
2011
- },
2012
- });
2013
- writeWorkerSource();
2014
- mockUploadWorkerRequest({
2015
- env: "dev",
2016
- });
2017
- mockSubDomainRequest();
2018
- mockUpdateWorkerRequest({ enabled: true, env: "dev" });
2019
-
2020
- await runWrangler("publish ./index --env dev --legacy-env false");
2021
-
2022
- expect(std.out).toMatchInlineSnapshot(`
2023
- "Uploaded test-name (dev) (TIMINGS)
2024
- Published test-name (dev) (TIMINGS)
2025
- dev.test-name.test-sub-domain.workers.dev"
2026
- `);
2027
- expect(std.err).toMatchInlineSnapshot(`""`);
2028
- });
2029
-
2030
- it("should use the global compatibility_date and compatibility_flags if they are not overwritten by the environment", async () => {
2031
- writeWranglerToml({
2032
- compatibility_date: "2022-01-12",
2033
- compatibility_flags: ["no_global_navigator"],
2034
- env: {
2035
- dev: {},
2036
- },
2037
- });
2038
- writeWorkerSource();
2039
- mockUploadWorkerRequest({
2040
- env: "dev",
2041
- expectedCompatibilityDate: "2022-01-12",
2042
- expectedCompatibilityFlags: ["no_global_navigator"],
2043
- });
2044
- mockSubDomainRequest();
2045
- mockUpdateWorkerRequest({ enabled: true, env: "dev" });
2046
-
2047
- await runWrangler("publish ./index --env dev --legacy-env false");
2048
-
2049
- expect(std.out).toMatchInlineSnapshot(`
2050
- "Uploaded test-name (dev) (TIMINGS)
2051
- Published test-name (dev) (TIMINGS)
2052
- dev.test-name.test-sub-domain.workers.dev"
2053
- `);
2054
- expect(std.err).toMatchInlineSnapshot(`""`);
2055
- });
2056
-
2057
- it("should use the environment specific compatibility_date and compatibility_flags", async () => {
2058
- writeWranglerToml({
2059
- compatibility_date: "2022-01-12",
2060
- compatibility_flags: ["no_global_navigator"],
2061
- env: {
2062
- dev: {
2063
- compatibility_date: "2022-01-13",
2064
- compatibility_flags: ["global_navigator"],
2065
- },
2066
- },
2067
- });
2068
- writeWorkerSource();
2069
- mockUploadWorkerRequest({
2070
- env: "dev",
2071
- expectedCompatibilityDate: "2022-01-13",
2072
- expectedCompatibilityFlags: ["global_navigator"],
2073
- });
2074
- mockSubDomainRequest();
2075
- mockUpdateWorkerRequest({ enabled: true, env: "dev" });
2076
-
2077
- await runWrangler("publish ./index --env dev --legacy-env false");
2078
-
2079
- expect(std.out).toMatchInlineSnapshot(`
2080
- "Uploaded test-name (dev) (TIMINGS)
2081
- Published test-name (dev) (TIMINGS)
2082
- dev.test-name.test-sub-domain.workers.dev"
2083
- `);
2084
- expect(std.err).toMatchInlineSnapshot(`""`);
2085
- });
2086
-
2087
- it("should use the command line --compatibility-date and --compatibility-flags if they are specified", async () => {
2088
- writeWranglerToml({
2089
- compatibility_date: "2022-01-12",
2090
- compatibility_flags: ["no_global_navigator"],
2091
- env: {
2092
- dev: {
2093
- compatibility_date: "2022-01-13",
2094
- compatibility_flags: ["global_navigator"],
2095
- },
2096
- },
2097
- });
2098
- writeWorkerSource();
2099
- mockUploadWorkerRequest({
2100
- env: "dev",
2101
- expectedCompatibilityDate: "2022-01-14",
2102
- expectedCompatibilityFlags: ["url_standard"],
2103
- });
2104
- mockSubDomainRequest();
2105
- mockUpdateWorkerRequest({ enabled: true, env: "dev" });
2106
-
2107
- await runWrangler(
2108
- "publish ./index --env dev --legacy-env false --compatibility-date 2022-01-14 --compatibility-flags url_standard"
2109
- );
2110
-
2111
- expect(std.out).toMatchInlineSnapshot(`
2112
- "Uploaded test-name (dev) (TIMINGS)
2113
- Published test-name (dev) (TIMINGS)
2114
- dev.test-name.test-sub-domain.workers.dev"
2115
- `);
2116
- expect(std.err).toMatchInlineSnapshot(`""`);
2117
- });
2118
-
2119
- it("should enable the workers.dev domain if workers_dev is undefined and subdomain is not already available", async () => {
2120
- writeWranglerToml();
2121
- writeWorkerSource();
2122
- mockUploadWorkerRequest({ available_on_subdomain: false });
2123
- mockSubDomainRequest();
2124
- mockUpdateWorkerRequest({ enabled: true });
2125
-
2126
- await runWrangler("publish ./index");
2127
-
2128
- expect(std.out).toMatchInlineSnapshot(`
2129
- "Uploaded test-name (TIMINGS)
2130
- Published test-name (TIMINGS)
2131
- test-name.test-sub-domain.workers.dev"
2132
- `);
2133
- expect(std.err).toMatchInlineSnapshot(`""`);
2134
- });
2135
-
2136
- it("should enable the workers.dev domain if workers_dev is true and subdomain is not already available", async () => {
2137
- writeWranglerToml({ workers_dev: true });
2138
- writeWorkerSource();
2139
- mockUploadWorkerRequest({ available_on_subdomain: false });
2140
- mockSubDomainRequest();
2141
- mockUpdateWorkerRequest({ enabled: true });
2142
-
2143
- await runWrangler("publish ./index");
2144
-
2145
- expect(std.out).toMatchInlineSnapshot(`
2146
- "Uploaded test-name (TIMINGS)
2147
- Published test-name (TIMINGS)
2148
- test-name.test-sub-domain.workers.dev"
2149
- `);
2150
- expect(std.err).toMatchInlineSnapshot(`""`);
2151
- });
2152
-
2153
- it("should error politely when publishing to workers_dev when there is no workers.dev subdomain", async () => {
2154
- writeWranglerToml({
2155
- workers_dev: true,
2156
- });
2157
- writeWorkerSource();
2158
- mockUploadWorkerRequest();
2159
- mockSubDomainRequest("does-not-exist", false);
2160
-
2161
- await expect(runWrangler("publish ./index")).rejects
2162
- .toThrowErrorMatchingInlineSnapshot(`
2163
- "Error: You need to register a workers.dev subdomain before publishing to workers.dev
2164
- You can either publish your worker to one or more routes by specifying them in wrangler.toml, or register a workers.dev subdomain here:
2165
- https://dash.cloudflare.com/some-account-id/workers/onboarding"
2166
- `);
2167
- });
2168
-
2169
- it("should not deploy to workers.dev if there are any routes defined", async () => {
2170
- writeWranglerToml({
2171
- routes: ["http://example.com/*"],
2172
- });
2173
- writeWorkerSource();
2174
- mockSubDomainRequest();
2175
- mockUploadWorkerRequest();
2176
- mockUpdateWorkerRequest({
2177
- enabled: false,
2178
- });
2179
- mockPublishRoutesRequest({ routes: ["http://example.com/*"] });
2180
- await runWrangler("publish index.js");
2181
-
2182
- expect(std.out).toMatchInlineSnapshot(`
2183
- "Uploaded test-name (TIMINGS)
2184
- Published test-name (TIMINGS)
2185
- http://example.com/*"
2186
- `);
2187
- expect(std.err).toMatchInlineSnapshot(`""`);
2188
- expect(std.warn).toMatchInlineSnapshot(`""`);
2189
- });
2190
-
2191
- it("should not deploy to workers.dev if there are any routes defined (environments)", async () => {
2192
- writeWranglerToml({
2193
- routes: ["http://example.com/*"],
2194
- env: {
2195
- production: {
2196
- routes: ["http://production.example.com/*"],
2197
- },
2198
- },
2199
- });
2200
- writeWorkerSource();
2201
- mockSubDomainRequest();
2202
- mockUploadWorkerRequest({ env: "production", legacyEnv: true });
2203
- mockUpdateWorkerRequest({
2204
- enabled: false,
2205
- env: "production",
2206
- legacyEnv: true,
2207
- });
2208
- mockPublishRoutesRequest({
2209
- routes: ["http://production.example.com/*"],
2210
- env: "production",
2211
- legacyEnv: true,
2212
- });
2213
- await runWrangler("publish index.js --env production");
2214
-
2215
- expect(std.out).toMatchInlineSnapshot(`
2216
- "Uploaded test-name-production (TIMINGS)
2217
- Published test-name-production (TIMINGS)
2218
- http://production.example.com/*"
2219
- `);
2220
- expect(std.err).toMatchInlineSnapshot(`""`);
2221
- expect(std.warn).toMatchInlineSnapshot(`""`);
2222
- });
2223
-
2224
- it("should not deploy to workers.dev if there are any routes defined (only in environments)", async () => {
2225
- writeWranglerToml({
2226
- env: {
2227
- production: {
2228
- routes: ["http://production.example.com/*"],
2229
- },
2230
- },
2231
- });
2232
- writeWorkerSource();
2233
- mockSubDomainRequest();
2234
- mockUploadWorkerRequest({ env: "production", legacyEnv: true });
2235
- mockUpdateWorkerRequest({
2236
- enabled: false,
2237
- env: "production",
2238
- legacyEnv: true,
2239
- });
2240
- mockPublishRoutesRequest({
2241
- routes: ["http://production.example.com/*"],
2242
- env: "production",
2243
- legacyEnv: true,
2244
- });
2245
- await runWrangler("publish index.js --env production");
2246
-
2247
- expect(std.out).toMatchInlineSnapshot(`
2248
- "Uploaded test-name-production (TIMINGS)
2249
- Published test-name-production (TIMINGS)
2250
- http://production.example.com/*"
2251
- `);
2252
- expect(std.err).toMatchInlineSnapshot(`""`);
2253
- expect(std.warn).toMatchInlineSnapshot(`""`);
2254
- });
2255
-
2256
- it("can deploy to both workers.dev and routes if both defined ", async () => {
2257
- writeWranglerToml({
2258
- workers_dev: true,
2259
- routes: ["http://example.com/*"],
2260
- });
2261
- writeWorkerSource();
2262
- mockSubDomainRequest();
2263
- mockUploadWorkerRequest();
2264
- mockUpdateWorkerRequest({
2265
- enabled: false,
2266
- });
2267
- mockPublishRoutesRequest({
2268
- routes: ["http://example.com/*"],
2269
- });
2270
- await runWrangler("publish index.js");
2271
-
2272
- expect(std.out).toMatchInlineSnapshot(`
2273
- "Uploaded test-name (TIMINGS)
2274
- Published test-name (TIMINGS)
2275
- test-name.test-sub-domain.workers.dev
2276
- http://example.com/*"
2277
- `);
2278
- expect(std.err).toMatchInlineSnapshot(`""`);
2279
- expect(std.warn).toMatchInlineSnapshot(`""`);
2280
- });
2281
-
2282
- it("can deploy to both workers.dev and routes if both defined (environments: 1)", async () => {
2283
- writeWranglerToml({
2284
- workers_dev: true,
2285
- env: {
2286
- production: {
2287
- routes: ["http://production.example.com/*"],
2288
- },
2289
- },
2290
- });
2291
- writeWorkerSource();
2292
- mockSubDomainRequest();
2293
- mockUploadWorkerRequest({ env: "production", legacyEnv: true });
2294
- mockUpdateWorkerRequest({
2295
- enabled: false,
2296
- env: "production",
2297
- legacyEnv: true,
2298
- });
2299
- mockPublishRoutesRequest({
2300
- routes: ["http://production.example.com/*"],
2301
- env: "production",
2302
- legacyEnv: true,
2303
- });
2304
- await runWrangler("publish index.js --env production");
2305
-
2306
- expect(std.out).toMatchInlineSnapshot(`
2307
- "Uploaded test-name-production (TIMINGS)
2308
- Published test-name-production (TIMINGS)
2309
- test-name-production.test-sub-domain.workers.dev
2310
- http://production.example.com/*"
2311
- `);
2312
- expect(std.err).toMatchInlineSnapshot(`""`);
2313
- expect(std.warn).toMatchInlineSnapshot(`""`);
2314
- });
2315
-
2316
- it("can deploy to both workers.dev and routes if both defined (environments: 2)", async () => {
2317
- writeWranglerToml({
2318
- env: {
2319
- production: {
2320
- workers_dev: true,
2321
- routes: ["http://production.example.com/*"],
2322
- },
2323
- },
2324
- });
2325
- writeWorkerSource();
2326
- mockSubDomainRequest();
2327
- mockUploadWorkerRequest({ env: "production", legacyEnv: true });
2328
- mockUpdateWorkerRequest({
2329
- enabled: false,
2330
- env: "production",
2331
- legacyEnv: true,
2332
- });
2333
- mockPublishRoutesRequest({
2334
- routes: ["http://production.example.com/*"],
2335
- env: "production",
2336
- legacyEnv: true,
2337
- });
2338
- await runWrangler("publish index.js --env production");
2339
-
2340
- expect(std.out).toMatchInlineSnapshot(`
2341
- "Uploaded test-name-production (TIMINGS)
2342
- Published test-name-production (TIMINGS)
2343
- test-name-production.test-sub-domain.workers.dev
2344
- http://production.example.com/*"
2345
- `);
2346
- expect(std.err).toMatchInlineSnapshot(`""`);
2347
- expect(std.warn).toMatchInlineSnapshot(`""`);
2348
- });
2349
-
2350
- it("will deploy only to routes when workers_dev is false (environments 1) ", async () => {
2351
- writeWranglerToml({
2352
- workers_dev: false,
2353
- env: {
2354
- production: {
2355
- routes: ["http://production.example.com/*"],
2356
- },
2357
- },
2358
- });
2359
- writeWorkerSource();
2360
- mockSubDomainRequest();
2361
- mockUploadWorkerRequest({ env: "production", legacyEnv: true });
2362
- mockUpdateWorkerRequest({
2363
- enabled: false,
2364
- env: "production",
2365
- legacyEnv: true,
2366
- });
2367
- mockPublishRoutesRequest({
2368
- routes: ["http://production.example.com/*"],
2369
- env: "production",
2370
- legacyEnv: true,
2371
- });
2372
- await runWrangler("publish index.js --env production");
2373
-
2374
- expect(std.out).toMatchInlineSnapshot(`
2375
- "Uploaded test-name-production (TIMINGS)
2376
- Published test-name-production (TIMINGS)
2377
- http://production.example.com/*"
2378
- `);
2379
- expect(std.err).toMatchInlineSnapshot(`""`);
2380
- expect(std.warn).toMatchInlineSnapshot(`""`);
2381
- });
2382
-
2383
- it("will deploy only to routes when workers_dev is false (environments 2) ", async () => {
2384
- writeWranglerToml({
2385
- env: {
2386
- production: {
2387
- workers_dev: false,
2388
- routes: ["http://production.example.com/*"],
2389
- },
2390
- },
2391
- });
2392
- writeWorkerSource();
2393
- mockSubDomainRequest();
2394
- mockUploadWorkerRequest({ env: "production", legacyEnv: true });
2395
- mockUpdateWorkerRequest({
2396
- enabled: false,
2397
- env: "production",
2398
- legacyEnv: true,
2399
- });
2400
- mockPublishRoutesRequest({
2401
- routes: ["http://production.example.com/*"],
2402
- env: "production",
2403
- legacyEnv: true,
2404
- });
2405
- await runWrangler("publish index.js --env production");
2406
-
2407
- expect(std.out).toMatchInlineSnapshot(`
2408
- "Uploaded test-name-production (TIMINGS)
2409
- Published test-name-production (TIMINGS)
2410
- http://production.example.com/*"
2411
- `);
2412
- expect(std.err).toMatchInlineSnapshot(`""`);
2413
- expect(std.warn).toMatchInlineSnapshot(`""`);
2414
- });
2415
- });
2416
-
2417
- describe("custom builds", () => {
2418
- beforeEach(() => {
2419
- // @ts-expect-error disable the mock we'd setup earlier
2420
- // or else custom builds will timeout immediately
2421
- global.setTimeout.mockRestore();
2422
- });
2423
- it("should run a custom build before publishing", async () => {
2424
- writeWranglerToml({
2425
- build: {
2426
- command: `node -e "console.log('custom build'); require('fs').writeFileSync('index.js', 'export default { fetch(){ return new Response(123) } }')"`,
2427
- },
2428
- });
2429
-
2430
- mockUploadWorkerRequest({
2431
- expectedEntry: "return new Response(123)",
2432
- });
2433
- mockSubDomainRequest();
2434
-
2435
- await runWrangler("publish index.js");
2436
- expect(std.out).toMatchInlineSnapshot(`
2437
- "Running custom build: node -e \\"console.log('custom build'); require('fs').writeFileSync('index.js', 'export default { fetch(){ return new Response(123) } }')\\"
2438
- Uploaded test-name (TIMINGS)
2439
- Published test-name (TIMINGS)
2440
- test-name.test-sub-domain.workers.dev"
2441
- `);
2442
- expect(std.err).toMatchInlineSnapshot(`""`);
2443
- expect(std.warn).toMatchInlineSnapshot(`""`);
2444
- });
2445
-
2446
- if (process.platform !== "win32") {
2447
- it("should run a custom build of multiple steps combined by && before publishing", async () => {
2448
- writeWranglerToml({
2449
- build: {
2450
- command: `echo "custom build" && echo "export default { fetch(){ return new Response(123) } }" > index.js`,
2451
- },
2452
- });
2453
-
2454
- mockUploadWorkerRequest({
2455
- expectedEntry: "return new Response(123)",
2456
- });
2457
- mockSubDomainRequest();
2458
-
2459
- await runWrangler("publish index.js");
2460
- expect(std.out).toMatchInlineSnapshot(`
2461
- "Running custom build: echo \\"custom build\\" && echo \\"export default { fetch(){ return new Response(123) } }\\" > index.js
2462
- Uploaded test-name (TIMINGS)
2463
- Published test-name (TIMINGS)
2464
- test-name.test-sub-domain.workers.dev"
2465
- `);
2466
- expect(std.err).toMatchInlineSnapshot(`""`);
2467
- expect(std.warn).toMatchInlineSnapshot(`""`);
2468
- });
2469
- }
2470
-
2471
- it("should throw an error if the entry doesn't exist after the build finishes", async () => {
2472
- writeWranglerToml({
2473
- main: "index.js",
2474
- build: {
2475
- command: `node -e "console.log('custom build');"`,
2476
- },
2477
- });
2478
-
2479
- await expect(
2480
- runWrangler("publish index.js")
2481
- ).rejects.toThrowErrorMatchingInlineSnapshot(
2482
- `"Could not resolve \\"index.js\\" after running custom build: node -e \\"console.log('custom build');\\""`
2483
- );
2484
- expect(std.out).toMatchInlineSnapshot(`
2485
- "Running custom build: node -e \\"console.log('custom build');\\"
2486
-
2487
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
2488
- `);
2489
- expect(std.err).toMatchInlineSnapshot(`
2490
- "X [ERROR] Could not resolve \\"index.js\\" after running custom build: node -e \\"console.log('custom build');\\"
2491
-
2492
- "
2493
- `);
2494
- expect(std.warn).toMatchInlineSnapshot(`""`);
2495
- });
2496
-
2497
- it("should minify the script when `--minify` is true (sw)", async () => {
2498
- writeWranglerToml({
2499
- main: "./index.js",
2500
- });
2501
- fs.writeFileSync(
2502
- "./index.js",
2503
- `export
2504
- default {
2505
- fetch() {
2506
- return new Response( "hello Cpt Picard" )
2507
- }
2508
- }
2509
- `
2510
- );
2511
-
2512
- mockUploadWorkerRequest({
2513
- expectedEntry: 'fetch(){return new Response("hello Cpt Picard")',
2514
- });
2515
-
2516
- mockSubDomainRequest();
2517
- await runWrangler("publish index.js --minify");
2518
- expect(std.out).toMatchInlineSnapshot(`
2519
- "Uploaded test-name (TIMINGS)
2520
- Published test-name (TIMINGS)
2521
- test-name.test-sub-domain.workers.dev"
2522
- `);
2523
- expect(std.err).toMatchInlineSnapshot(`""`);
2524
- });
2525
-
2526
- it("should minify the script when `minify` in config is true (esm)", async () => {
2527
- writeWranglerToml({
2528
- main: "./index.js",
2529
- legacy_env: false,
2530
- env: {
2531
- testEnv: {
2532
- minify: true,
2533
- },
2534
- },
2535
- });
2536
- fs.writeFileSync(
2537
- "./index.js",
2538
- `export
2539
- default {
2540
- fetch() {
2541
- return new Response( "hello Cpt Picard" )
2542
- }
2543
- }
2544
- `
2545
- );
2546
-
2547
- mockUploadWorkerRequest({
2548
- env: "testEnv",
2549
- expectedType: "esm",
2550
- legacyEnv: false,
2551
- expectedEntry: `fetch(){return new Response("hello Cpt Picard")`,
2552
- });
2553
-
2554
- mockSubDomainRequest();
2555
- await runWrangler("publish -e testEnv index.js");
2556
- expect(std.out).toMatchInlineSnapshot(`
2557
- "Uploaded test-name (testEnv) (TIMINGS)
2558
- Published test-name (testEnv) (TIMINGS)
2559
- testEnv.test-name.test-sub-domain.workers.dev"
2560
- `);
2561
- expect(std.err).toMatchInlineSnapshot(`""`);
2562
- });
2563
- });
2564
-
2565
- describe("durable object migrations", () => {
2566
- it("should warn when you try to publish durable objects without migrations", async () => {
2567
- writeWranglerToml({
2568
- durable_objects: {
2569
- bindings: [{ name: "SOMENAME", class_name: "SomeClass" }],
2570
- },
2571
- });
2572
- fs.writeFileSync(
2573
- "index.js",
2574
- `export class SomeClass{}; export default {};`
2575
- );
2576
- mockSubDomainRequest();
2577
- mockUploadWorkerRequest();
2578
- await runWrangler("publish index.js");
2579
- expect(std.out).toMatchInlineSnapshot(`
2580
- "Uploaded test-name (TIMINGS)
2581
- Published test-name (TIMINGS)
2582
- test-name.test-sub-domain.workers.dev"
2583
- `);
2584
- expect(std.err).toMatchInlineSnapshot(`""`);
2585
- expect(std.warn).toMatchInlineSnapshot(`
2586
- "▲ [WARNING] In wrangler.toml, you have configured [durable_objects] exported by this Worker (SomeClass), but no [migrations] for them. This may not work as expected until you add a [migrations] section to your wrangler.toml. Refer to https://developers.cloudflare.com/workers/learning/using-durable-objects/#durable-object-migrations-in-wranglertoml for more details.
2587
-
2588
- "
2589
- `);
2590
- });
2591
-
2592
- it("does not warn if all the durable object bindings are to external classes", async () => {
2593
- writeWranglerToml({
2594
- durable_objects: {
2595
- bindings: [
2596
- {
2597
- name: "SOMENAME",
2598
- class_name: "SomeClass",
2599
- script_name: "some-script",
2600
- },
2601
- ],
2602
- },
2603
- });
2604
- fs.writeFileSync(
2605
- "index.js",
2606
- `export class SomeClass{}; export default {};`
2607
- );
2608
- mockSubDomainRequest();
2609
- mockUploadWorkerRequest();
2610
- await runWrangler("publish index.js");
2611
- expect(std.out).toMatchInlineSnapshot(`
2612
- "Uploaded test-name (TIMINGS)
2613
- Published test-name (TIMINGS)
2614
- test-name.test-sub-domain.workers.dev"
2615
- `);
2616
- expect(std.err).toMatchInlineSnapshot(`""`);
2617
- expect(std.warn).toMatchInlineSnapshot(`""`);
2618
- });
2619
-
2620
- it("should publish all migrations on first publish", async () => {
2621
- writeWranglerToml({
2622
- durable_objects: {
2623
- bindings: [
2624
- { name: "SOMENAME", class_name: "SomeClass" },
2625
- { name: "SOMEOTHERNAME", class_name: "SomeOtherClass" },
2626
- ],
2627
- },
2628
- migrations: [
2629
- { tag: "v1", new_classes: ["SomeClass"] },
2630
- { tag: "v2", new_classes: ["SomeOtherClass"] },
2631
- ],
2632
- });
2633
- fs.writeFileSync(
2634
- "index.js",
2635
- `export class SomeClass{}; export class SomeOtherClass{}; export default {};`
2636
- );
2637
- mockSubDomainRequest();
2638
- mockLegacyScriptData({ scripts: [] }); // no previously uploaded scripts at all
2639
- mockUploadWorkerRequest({
2640
- expectedMigrations: {
2641
- new_tag: "v2",
2642
- steps: [
2643
- { new_classes: ["SomeClass"] },
2644
- { new_classes: ["SomeOtherClass"] },
2645
- ],
2646
- },
2647
- });
2648
-
2649
- await runWrangler("publish index.js");
2650
- expect(std.out).toMatchInlineSnapshot(`
2651
- "Uploaded test-name (TIMINGS)
2652
- Published test-name (TIMINGS)
2653
- test-name.test-sub-domain.workers.dev"
2654
- `);
2655
- expect(std.err).toMatchInlineSnapshot(`""`);
2656
- expect(std.warn).toMatchInlineSnapshot(`""`);
2657
- });
2658
-
2659
- it("should upload migrations past a previously uploaded tag", async () => {
2660
- writeWranglerToml({
2661
- durable_objects: {
2662
- bindings: [
2663
- { name: "SOMENAME", class_name: "SomeClass" },
2664
- { name: "SOMEOTHERNAME", class_name: "SomeOtherClass" },
2665
- ],
2666
- },
2667
- migrations: [
2668
- { tag: "v1", new_classes: ["SomeClass"] },
2669
- { tag: "v2", new_classes: ["SomeOtherClass"] },
2670
- ],
2671
- });
2672
- fs.writeFileSync(
2673
- "index.js",
2674
- `export class SomeClass{}; export class SomeOtherClass{}; export default {};`
2675
- );
2676
- mockSubDomainRequest();
2677
- mockLegacyScriptData({
2678
- scripts: [{ id: "test-name", migration_tag: "v1" }],
2679
- });
2680
- mockUploadWorkerRequest({
2681
- expectedMigrations: {
2682
- old_tag: "v1",
2683
- new_tag: "v2",
2684
- steps: [
2685
- {
2686
- new_classes: ["SomeOtherClass"],
2687
- },
2688
- ],
2689
- },
2690
- });
2691
-
2692
- await runWrangler("publish index.js");
2693
- expect(std).toMatchInlineSnapshot(`
2694
- Object {
2695
- "debug": "",
2696
- "err": "",
2697
- "out": "Uploaded test-name (TIMINGS)
2698
- Published test-name (TIMINGS)
2699
- test-name.test-sub-domain.workers.dev",
2700
- "warn": "",
2701
- }
2702
- `);
2703
- });
2704
-
2705
- it("should not send migrations if they've all already been sent", async () => {
2706
- writeWranglerToml({
2707
- durable_objects: {
2708
- bindings: [
2709
- { name: "SOMENAME", class_name: "SomeClass" },
2710
- { name: "SOMEOTHERNAME", class_name: "SomeOtherClass" },
2711
- ],
2712
- },
2713
- migrations: [
2714
- { tag: "v1", new_classes: ["SomeClass"] },
2715
- { tag: "v2", new_classes: ["SomeOtherClass"] },
2716
- { tag: "v3", new_classes: ["YetAnotherClass"] },
2717
- ],
2718
- });
2719
- fs.writeFileSync(
2720
- "index.js",
2721
- `export class SomeClass{}; export class SomeOtherClass{}; export class YetAnotherClass{}; export default {};`
2722
- );
2723
- mockSubDomainRequest();
2724
- mockLegacyScriptData({
2725
- scripts: [{ id: "test-name", migration_tag: "v3" }],
2726
- });
2727
- mockUploadWorkerRequest({
2728
- expectedMigrations: undefined,
2729
- });
2730
-
2731
- await runWrangler("publish index.js");
2732
- expect(std).toMatchInlineSnapshot(`
2733
- Object {
2734
- "debug": "",
2735
- "err": "",
2736
- "out": "Uploaded test-name (TIMINGS)
2737
- Published test-name (TIMINGS)
2738
- test-name.test-sub-domain.workers.dev",
2739
- "warn": "",
2740
- }
2741
- `);
2742
- });
2743
-
2744
- describe("service environments", () => {
2745
- it("should publish all migrations on first publish", async () => {
2746
- writeWranglerToml({
2747
- durable_objects: {
2748
- bindings: [
2749
- { name: "SOMENAME", class_name: "SomeClass" },
2750
- { name: "SOMEOTHERNAME", class_name: "SomeOtherClass" },
2751
- ],
2752
- },
2753
- migrations: [
2754
- { tag: "v1", new_classes: ["SomeClass"] },
2755
- { tag: "v2", new_classes: ["SomeOtherClass"] },
2756
- ],
2757
- });
2758
- fs.writeFileSync(
2759
- "index.js",
2760
- `export class SomeClass{}; export class SomeOtherClass{}; export default {};`
2761
- );
2762
- mockSubDomainRequest();
2763
- mockServiceScriptData({}); // no scripts at all
2764
- mockUploadWorkerRequest({
2765
- legacyEnv: false,
2766
- expectedMigrations: {
2767
- new_tag: "v2",
2768
- steps: [
2769
- { new_classes: ["SomeClass"] },
2770
- { new_classes: ["SomeOtherClass"] },
2771
- ],
2772
- },
2773
- });
2774
-
2775
- await runWrangler("publish index.js --legacy-env false");
2776
- expect(std.out).toMatchInlineSnapshot(`
2777
- "Uploaded test-name (TIMINGS)
2778
- Published test-name (TIMINGS)
2779
- test-name.test-sub-domain.workers.dev"
2780
- `);
2781
- expect(std.err).toMatchInlineSnapshot(`""`);
2782
- expect(std.warn).toMatchInlineSnapshot(`
2783
- "▲ [WARNING] Processing wrangler.toml configuration:
2784
-
2785
- - Experimental: Service environments are in beta, and their behaviour is guaranteed to change in
2786
- the future. DO NOT USE IN PRODUCTION.
2787
-
2788
- "
2789
- `);
2790
- });
2791
-
2792
- it("should publish all migrations on first publish (--env)", async () => {
2793
- writeWranglerToml({
2794
- durable_objects: {
2795
- bindings: [
2796
- { name: "SOMENAME", class_name: "SomeClass" },
2797
- { name: "SOMEOTHERNAME", class_name: "SomeOtherClass" },
2798
- ],
2799
- },
2800
- env: {
2801
- xyz: {
2802
- durable_objects: {
2803
- bindings: [
2804
- { name: "SOMENAME", class_name: "SomeClass" },
2805
- { name: "SOMEOTHERNAME", class_name: "SomeOtherClass" },
2806
- ],
2807
- },
2808
- },
2809
- },
2810
- migrations: [
2811
- { tag: "v1", new_classes: ["SomeClass"] },
2812
- { tag: "v2", new_classes: ["SomeOtherClass"] },
2813
- ],
2814
- });
2815
- fs.writeFileSync(
2816
- "index.js",
2817
- `export class SomeClass{}; export class SomeOtherClass{}; export default {};`
2818
- );
2819
- mockSubDomainRequest();
2820
- mockServiceScriptData({ env: "xyz" }); // no scripts at all
2821
- mockUploadWorkerRequest({
2822
- legacyEnv: false,
2823
- env: "xyz",
2824
- expectedMigrations: {
2825
- new_tag: "v2",
2826
- steps: [
2827
- { new_classes: ["SomeClass"] },
2828
- { new_classes: ["SomeOtherClass"] },
2829
- ],
2830
- },
2831
- });
2832
-
2833
- await runWrangler("publish index.js --legacy-env false --env xyz");
2834
- expect(std.out).toMatchInlineSnapshot(`
2835
- "Uploaded test-name (xyz) (TIMINGS)
2836
- Published test-name (xyz) (TIMINGS)
2837
- xyz.test-name.test-sub-domain.workers.dev"
2838
- `);
2839
- expect(std.err).toMatchInlineSnapshot(`""`);
2840
- expect(std.warn).toMatchInlineSnapshot(`
2841
- "▲ [WARNING] Processing wrangler.toml configuration:
2842
-
2843
- - Experimental: Service environments are in beta, and their behaviour is guaranteed to change in
2844
- the future. DO NOT USE IN PRODUCTION.
2845
-
2846
- "
2847
- `);
2848
- });
2849
-
2850
- it("should use a script's current migration tag when publishing migrations", async () => {
2851
- writeWranglerToml({
2852
- durable_objects: {
2853
- bindings: [
2854
- { name: "SOMENAME", class_name: "SomeClass" },
2855
- { name: "SOMEOTHERNAME", class_name: "SomeOtherClass" },
2856
- ],
2857
- },
2858
- migrations: [
2859
- { tag: "v1", new_classes: ["SomeClass"] },
2860
- { tag: "v2", new_classes: ["SomeOtherClass"] },
2861
- ],
2862
- });
2863
- fs.writeFileSync(
2864
- "index.js",
2865
- `export class SomeClass{}; export class SomeOtherClass{}; export default {};`
2866
- );
2867
- mockSubDomainRequest();
2868
- mockServiceScriptData({
2869
- script: { id: "test-name", migration_tag: "v1" },
2870
- });
2871
- mockUploadWorkerRequest({
2872
- legacyEnv: false,
2873
- expectedMigrations: {
2874
- old_tag: "v1",
2875
- new_tag: "v2",
2876
- steps: [
2877
- {
2878
- new_classes: ["SomeOtherClass"],
2879
- },
2880
- ],
2881
- },
2882
- });
2883
-
2884
- await runWrangler("publish index.js --legacy-env false");
2885
- expect(std).toMatchInlineSnapshot(`
2886
- Object {
2887
- "debug": "",
2888
- "err": "",
2889
- "out": "Uploaded test-name (TIMINGS)
2890
- Published test-name (TIMINGS)
2891
- test-name.test-sub-domain.workers.dev",
2892
- "warn": "▲ [WARNING] Processing wrangler.toml configuration:
2893
-
2894
- - Experimental: Service environments are in beta, and their behaviour is guaranteed to change in
2895
- the future. DO NOT USE IN PRODUCTION.
2896
-
2897
- ",
2898
- }
2899
- `);
2900
- });
2901
-
2902
- it("should use an environment's current migration tag when publishing migrations", async () => {
2903
- writeWranglerToml({
2904
- durable_objects: {
2905
- bindings: [
2906
- { name: "SOMENAME", class_name: "SomeClass" },
2907
- { name: "SOMEOTHERNAME", class_name: "SomeOtherClass" },
2908
- ],
2909
- },
2910
- env: {
2911
- xyz: {
2912
- durable_objects: {
2913
- bindings: [
2914
- { name: "SOMENAME", class_name: "SomeClass" },
2915
- { name: "SOMEOTHERNAME", class_name: "SomeOtherClass" },
2916
- ],
2917
- },
2918
- },
2919
- },
2920
- migrations: [
2921
- { tag: "v1", new_classes: ["SomeClass"] },
2922
- { tag: "v2", new_classes: ["SomeOtherClass"] },
2923
- ],
2924
- });
2925
- fs.writeFileSync(
2926
- "index.js",
2927
- `export class SomeClass{}; export class SomeOtherClass{}; export default {};`
2928
- );
2929
- mockSubDomainRequest();
2930
- mockServiceScriptData({
2931
- script: { id: "test-name", migration_tag: "v1" },
2932
- env: "xyz",
2933
- });
2934
- mockUploadWorkerRequest({
2935
- legacyEnv: false,
2936
- env: "xyz",
2937
- expectedMigrations: {
2938
- old_tag: "v1",
2939
- new_tag: "v2",
2940
- steps: [
2941
- {
2942
- new_classes: ["SomeOtherClass"],
2943
- },
2944
- ],
2945
- },
2946
- });
2947
-
2948
- await runWrangler("publish index.js --legacy-env false --env xyz");
2949
- expect(std).toMatchInlineSnapshot(`
2950
- Object {
2951
- "debug": "",
2952
- "err": "",
2953
- "out": "Uploaded test-name (xyz) (TIMINGS)
2954
- Published test-name (xyz) (TIMINGS)
2955
- xyz.test-name.test-sub-domain.workers.dev",
2956
- "warn": "▲ [WARNING] Processing wrangler.toml configuration:
2957
-
2958
- - Experimental: Service environments are in beta, and their behaviour is guaranteed to change in
2959
- the future. DO NOT USE IN PRODUCTION.
2960
-
2961
- ",
2962
- }
2963
- `);
2964
- });
2965
- });
2966
- });
2967
-
2968
- describe("bindings", () => {
2969
- it("should allow bindings with different names", async () => {
2970
- writeWranglerToml({
2971
- migrations: [
2972
- {
2973
- tag: "v1",
2974
- new_classes: ["SomeDurableObject", "AnotherDurableObject"],
2975
- },
2976
- ],
2977
- durable_objects: {
2978
- bindings: [
2979
- {
2980
- name: "DURABLE_OBJECT_ONE",
2981
- class_name: "SomeDurableObject",
2982
- script_name: "some-durable-object-worker",
2983
- },
2984
- {
2985
- name: "DURABLE_OBJECT_TWO",
2986
- class_name: "AnotherDurableObject",
2987
- script_name: "another-durable-object-worker",
2988
- },
2989
- ],
2990
- },
2991
- kv_namespaces: [
2992
- { binding: "KV_NAMESPACE_ONE", id: "kv-ns-one-id" },
2993
- { binding: "KV_NAMESPACE_TWO", id: "kv-ns-two-id" },
2994
- ],
2995
- r2_buckets: [
2996
- { binding: "R2_BUCKET_ONE", bucket_name: "r2-bucket-one-name" },
2997
- { binding: "R2_BUCKET_TWO", bucket_name: "r2-bucket-two-name" },
2998
- ],
2999
- text_blobs: {
3000
- TEXT_BLOB_ONE: "./my-entire-app-depends-on-this.cfg",
3001
- TEXT_BLOB_TWO: "./the-entirety-of-human-knowledge.txt",
3002
- },
3003
- unsafe: {
3004
- bindings: [
3005
- {
3006
- name: "UNSAFE_BINDING_ONE",
3007
- type: "some unsafe thing",
3008
- data: { some: { unsafe: "thing" } },
3009
- },
3010
- {
3011
- name: "UNSAFE_BINDING_TWO",
3012
- type: "another unsafe thing",
3013
- data: 1337,
3014
- },
3015
- ],
3016
- },
3017
- vars: {
3018
- ENV_VAR_ONE: 123,
3019
- ENV_VAR_TWO: "Hello, I'm an environment variable",
3020
- },
3021
- wasm_modules: {
3022
- WASM_MODULE_ONE: "./some_wasm.wasm",
3023
- WASM_MODULE_TWO: "./more_wasm.wasm",
3024
- },
3025
- data_blobs: {
3026
- DATA_BLOB_ONE: "./some-data-blob.bin",
3027
- DATA_BLOB_TWO: "./more-data-blob.bin",
3028
- },
3029
- });
3030
-
3031
- writeWorkerSource({ type: "sw" });
3032
- fs.writeFileSync("./my-entire-app-depends-on-this.cfg", "config = value");
3033
- fs.writeFileSync(
3034
- "./the-entirety-of-human-knowledge.txt",
3035
- "Everything's bigger in Texas"
3036
- );
3037
- fs.writeFileSync("./some_wasm.wasm", "some wasm");
3038
- fs.writeFileSync("./more_wasm.wasm", "more wasm");
3039
-
3040
- fs.writeFileSync("./some-data-blob.bin", "some data");
3041
- fs.writeFileSync("./more-data-blob.bin", "more data");
3042
-
3043
- mockUploadWorkerRequest({
3044
- expectedType: "sw",
3045
- expectedBindings: [
3046
- {
3047
- name: "KV_NAMESPACE_ONE",
3048
- namespace_id: "kv-ns-one-id",
3049
- type: "kv_namespace",
3050
- },
3051
- {
3052
- name: "KV_NAMESPACE_TWO",
3053
- namespace_id: "kv-ns-two-id",
3054
- type: "kv_namespace",
3055
- },
3056
- {
3057
- class_name: "SomeDurableObject",
3058
- name: "DURABLE_OBJECT_ONE",
3059
- script_name: "some-durable-object-worker",
3060
- type: "durable_object_namespace",
3061
- },
3062
- {
3063
- class_name: "AnotherDurableObject",
3064
- name: "DURABLE_OBJECT_TWO",
3065
- script_name: "another-durable-object-worker",
3066
- type: "durable_object_namespace",
3067
- },
3068
- {
3069
- bucket_name: "r2-bucket-one-name",
3070
- name: "R2_BUCKET_ONE",
3071
- type: "r2_bucket",
3072
- },
3073
- {
3074
- bucket_name: "r2-bucket-two-name",
3075
- name: "R2_BUCKET_TWO",
3076
- type: "r2_bucket",
3077
- },
3078
- { json: 123, name: "ENV_VAR_ONE", type: "json" },
3079
- {
3080
- name: "ENV_VAR_TWO",
3081
- text: "Hello, I'm an environment variable",
3082
- type: "plain_text",
3083
- },
3084
- {
3085
- name: "WASM_MODULE_ONE",
3086
- part: "WASM_MODULE_ONE",
3087
- type: "wasm_module",
3088
- },
3089
- {
3090
- name: "WASM_MODULE_TWO",
3091
- part: "WASM_MODULE_TWO",
3092
- type: "wasm_module",
3093
- },
3094
- { name: "TEXT_BLOB_ONE", part: "TEXT_BLOB_ONE", type: "text_blob" },
3095
- { name: "TEXT_BLOB_TWO", part: "TEXT_BLOB_TWO", type: "text_blob" },
3096
- { name: "DATA_BLOB_ONE", part: "DATA_BLOB_ONE", type: "data_blob" },
3097
- { name: "DATA_BLOB_TWO", part: "DATA_BLOB_TWO", type: "data_blob" },
3098
- {
3099
- data: { some: { unsafe: "thing" } },
3100
- name: "UNSAFE_BINDING_ONE",
3101
- type: "some unsafe thing",
3102
- },
3103
- {
3104
- data: 1337,
3105
- name: "UNSAFE_BINDING_TWO",
3106
- type: "another unsafe thing",
3107
- },
3108
- ],
3109
- });
3110
- mockSubDomainRequest();
3111
- mockLegacyScriptData({ scripts: [] });
3112
-
3113
- await expect(runWrangler("publish index.js")).resolves.toBeUndefined();
3114
- expect(std.out).toMatchInlineSnapshot(`
3115
- "Uploaded test-name (TIMINGS)
3116
- Published test-name (TIMINGS)
3117
- test-name.test-sub-domain.workers.dev"
3118
- `);
3119
- expect(std.err).toMatchInlineSnapshot(`""`);
3120
- expect(std.warn).toMatchInlineSnapshot(`
3121
- "▲ [WARNING] Processing wrangler.toml configuration:
3122
-
3123
- - \\"unsafe\\" fields are experimental and may change or break at any time.
3124
-
3125
- "
3126
- `);
3127
- });
3128
-
3129
- it("should error when bindings of different types have the same name", async () => {
3130
- writeWranglerToml({
3131
- durable_objects: {
3132
- bindings: [
3133
- {
3134
- name: "CONFLICTING_NAME_ONE",
3135
- class_name: "SomeDurableObject",
3136
- script_name: "some-durable-object-worker",
3137
- },
3138
- {
3139
- name: "CONFLICTING_NAME_TWO",
3140
- class_name: "AnotherDurableObject",
3141
- script_name: "another-durable-object-worker",
3142
- },
3143
- ],
3144
- },
3145
- kv_namespaces: [
3146
- { binding: "CONFLICTING_NAME_ONE", id: "kv-ns-one-id" },
3147
- { binding: "CONFLICTING_NAME_TWO", id: "kv-ns-two-id" },
3148
- ],
3149
- r2_buckets: [
3150
- {
3151
- binding: "CONFLICTING_NAME_ONE",
3152
- bucket_name: "r2-bucket-one-name",
3153
- },
3154
- {
3155
- binding: "CONFLICTING_NAME_THREE",
3156
- bucket_name: "r2-bucket-two-name",
3157
- },
3158
- ],
3159
- text_blobs: {
3160
- CONFLICTING_NAME_THREE: "./my-entire-app-depends-on-this.cfg",
3161
- CONFLICTING_NAME_FOUR: "./the-entirety-of-human-knowledge.txt",
3162
- },
3163
- unsafe: {
3164
- bindings: [
3165
- {
3166
- name: "CONFLICTING_NAME_THREE",
3167
- type: "some unsafe thing",
3168
- data: { some: { unsafe: "thing" } },
3169
- },
3170
- {
3171
- name: "CONFLICTING_NAME_FOUR",
3172
- type: "another unsafe thing",
3173
- data: 1337,
3174
- },
3175
- ],
3176
- },
3177
- vars: {
3178
- ENV_VAR_ONE: 123,
3179
- CONFLICTING_NAME_THREE: "Hello, I'm an environment variable",
3180
- },
3181
- wasm_modules: {
3182
- WASM_MODULE_ONE: "./some_wasm.wasm",
3183
- CONFLICTING_NAME_THREE: "./more_wasm.wasm",
3184
- },
3185
- data_blobs: {
3186
- DATA_BLOB_ONE: "./some_data.bin",
3187
- CONFLICTING_NAME_THREE: "./more_data.bin",
3188
- },
3189
- });
3190
-
3191
- writeWorkerSource({ type: "sw" });
3192
- fs.writeFileSync("./my-entire-app-depends-on-this.cfg", "config = value");
3193
- fs.writeFileSync(
3194
- "./the-entirety-of-human-knowledge.txt",
3195
- "Everything's bigger in Texas"
3196
- );
3197
- fs.writeFileSync("./some_wasm.wasm", "some wasm");
3198
- fs.writeFileSync("./more_wasm.wasm", "more wasm");
3199
-
3200
- await expect(runWrangler("publish index.js")).rejects
3201
- .toMatchInlineSnapshot(`
3202
- [Error: Processing wrangler.toml configuration:
3203
- - CONFLICTING_NAME_ONE assigned to Durable Object, KV Namespace, and R2 Bucket bindings.
3204
- - CONFLICTING_NAME_TWO assigned to Durable Object and KV Namespace bindings.
3205
- - CONFLICTING_NAME_THREE assigned to R2 Bucket, Text Blob, Unsafe, Environment Variable, WASM Module, and Data Blob bindings.
3206
- - CONFLICTING_NAME_FOUR assigned to Text Blob and Unsafe bindings.
3207
- - Bindings must have unique names, so that they can all be referenced in the worker.
3208
- Please change your bindings to have unique names.]
3209
- `);
3210
- expect(std.out).toMatchInlineSnapshot(`
3211
- "
3212
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
3213
- `);
3214
- expect(std.err).toMatchInlineSnapshot(`
3215
- "X [ERROR] Processing wrangler.toml configuration:
3216
-
3217
- - CONFLICTING_NAME_ONE assigned to Durable Object, KV Namespace, and R2 Bucket bindings.
3218
- - CONFLICTING_NAME_TWO assigned to Durable Object and KV Namespace bindings.
3219
- - CONFLICTING_NAME_THREE assigned to R2 Bucket, Text Blob, Unsafe, Environment Variable, WASM
3220
- Module, and Data Blob bindings.
3221
- - CONFLICTING_NAME_FOUR assigned to Text Blob and Unsafe bindings.
3222
- - Bindings must have unique names, so that they can all be referenced in the worker.
3223
- Please change your bindings to have unique names.
3224
-
3225
- "
3226
- `);
3227
- expect(std.warn).toMatchInlineSnapshot(`
3228
- "▲ [WARNING] Processing wrangler.toml configuration:
3229
-
3230
- - \\"unsafe\\" fields are experimental and may change or break at any time.
3231
-
3232
- "
3233
- `);
3234
- });
3235
-
3236
- it("should error when bindings of the same type have the same name", async () => {
3237
- writeWranglerToml({
3238
- durable_objects: {
3239
- bindings: [
3240
- {
3241
- name: "CONFLICTING_DURABLE_OBJECT_NAME",
3242
- class_name: "SomeDurableObject",
3243
- script_name: "some-durable-object-worker",
3244
- },
3245
- {
3246
- name: "CONFLICTING_DURABLE_OBJECT_NAME",
3247
- class_name: "AnotherDurableObject",
3248
- script_name: "another-durable-object-worker",
3249
- },
3250
- ],
3251
- },
3252
- kv_namespaces: [
3253
- { binding: "CONFLICTING_KV_NAMESPACE_NAME", id: "kv-ns-one-id" },
3254
- { binding: "CONFLICTING_KV_NAMESPACE_NAME", id: "kv-ns-two-id" },
3255
- ],
3256
- r2_buckets: [
3257
- {
3258
- binding: "CONFLICTING_R2_BUCKET_NAME",
3259
- bucket_name: "r2-bucket-one-name",
3260
- },
3261
- {
3262
- binding: "CONFLICTING_R2_BUCKET_NAME",
3263
- bucket_name: "r2-bucket-two-name",
3264
- },
3265
- ],
3266
- unsafe: {
3267
- bindings: [
3268
- {
3269
- name: "CONFLICTING_UNSAFE_NAME",
3270
- type: "some unsafe thing",
3271
- data: { some: { unsafe: "thing" } },
3272
- },
3273
- {
3274
- name: "CONFLICTING_UNSAFE_NAME",
3275
- type: "another unsafe thing",
3276
- data: 1337,
3277
- },
3278
- ],
3279
- },
3280
- // text_blobs, vars, wasm_modules and data_blobs are fine because they're object literals,
3281
- // and by definition cannot have two keys of the same name
3282
- //
3283
- // text_blobs: {
3284
- // CONFLICTING_TEXT_BLOB_NAME: "./my-entire-app-depends-on-this.cfg",
3285
- // CONFLICTING_TEXT_BLOB_NAME: "./the-entirety-of-human-knowledge.txt",
3286
- // },
3287
- // vars: {
3288
- // CONFLICTING_VARS_NAME: 123,
3289
- // CONFLICTING_VARS_NAME: "Hello, I'm an environment variable",
3290
- // },
3291
- // wasm_modules: {
3292
- // CONFLICTING_WASM_MODULE_NAME: "./some_wasm.wasm",
3293
- // CONFLICTING_WASM_MODULE_NAME: "./more_wasm.wasm",
3294
- // },
3295
- });
3296
-
3297
- writeWorkerSource({ type: "sw" });
3298
-
3299
- await expect(runWrangler("publish index.js")).rejects
3300
- .toMatchInlineSnapshot(`
3301
- [Error: Processing wrangler.toml configuration:
3302
- - CONFLICTING_DURABLE_OBJECT_NAME assigned to multiple Durable Object bindings.
3303
- - CONFLICTING_KV_NAMESPACE_NAME assigned to multiple KV Namespace bindings.
3304
- - CONFLICTING_R2_BUCKET_NAME assigned to multiple R2 Bucket bindings.
3305
- - CONFLICTING_UNSAFE_NAME assigned to multiple Unsafe bindings.
3306
- - Bindings must have unique names, so that they can all be referenced in the worker.
3307
- Please change your bindings to have unique names.]
3308
- `);
3309
- expect(std.out).toMatchInlineSnapshot(`
3310
- "
3311
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
3312
- `);
3313
- expect(std.err).toMatchInlineSnapshot(`
3314
- "X [ERROR] Processing wrangler.toml configuration:
3315
-
3316
- - CONFLICTING_DURABLE_OBJECT_NAME assigned to multiple Durable Object bindings.
3317
- - CONFLICTING_KV_NAMESPACE_NAME assigned to multiple KV Namespace bindings.
3318
- - CONFLICTING_R2_BUCKET_NAME assigned to multiple R2 Bucket bindings.
3319
- - CONFLICTING_UNSAFE_NAME assigned to multiple Unsafe bindings.
3320
- - Bindings must have unique names, so that they can all be referenced in the worker.
3321
- Please change your bindings to have unique names.
3322
-
3323
- "
3324
- `);
3325
- expect(std.warn).toMatchInlineSnapshot(`
3326
- "▲ [WARNING] Processing wrangler.toml configuration:
3327
-
3328
- - \\"unsafe\\" fields are experimental and may change or break at any time.
3329
-
3330
- "
3331
- `);
3332
- });
3333
-
3334
- it("should error correctly when bindings of the same and different types use the same name", async () => {
3335
- writeWranglerToml({
3336
- durable_objects: {
3337
- bindings: [
3338
- {
3339
- name: "CONFLICTING_DURABLE_OBJECT_NAME",
3340
- class_name: "SomeDurableObject",
3341
- script_name: "some-durable-object-worker",
3342
- },
3343
- {
3344
- name: "CONFLICTING_DURABLE_OBJECT_NAME",
3345
- class_name: "AnotherDurableObject",
3346
- script_name: "another-durable-object-worker",
3347
- },
3348
- ],
3349
- },
3350
- kv_namespaces: [
3351
- {
3352
- binding: "CONFLICTING_KV_NAMESPACE_NAME",
3353
- id: "kv-ns-one-id",
3354
- },
3355
- {
3356
- binding: "CONFLICTING_KV_NAMESPACE_NAME",
3357
- id: "kv-ns-two-id",
3358
- },
3359
- { binding: "CONFLICTING_NAME_ONE", id: "kv-ns-three-id" },
3360
- { binding: "CONFLICTING_NAME_TWO", id: "kv-ns-four-id" },
3361
- ],
3362
- r2_buckets: [
3363
- {
3364
- binding: "CONFLICTING_R2_BUCKET_NAME",
3365
- bucket_name: "r2-bucket-one-name",
3366
- },
3367
- {
3368
- binding: "CONFLICTING_R2_BUCKET_NAME",
3369
- bucket_name: "r2-bucket-two-name",
3370
- },
3371
- {
3372
- binding: "CONFLICTING_NAME_THREE",
3373
- bucket_name: "r2-bucket-three-name",
3374
- },
3375
- {
3376
- binding: "CONFLICTING_NAME_FOUR",
3377
- bucket_name: "r2-bucket-four-name",
3378
- },
3379
- ],
3380
- text_blobs: {
3381
- CONFLICTING_NAME_THREE: "./my-entire-app-depends-on-this.cfg",
3382
- CONFLICTING_NAME_FOUR: "./the-entirety-of-human-knowledge.txt",
3383
- },
3384
- unsafe: {
3385
- bindings: [
3386
- {
3387
- name: "CONFLICTING_UNSAFE_NAME",
3388
- type: "some unsafe thing",
3389
- data: { some: { unsafe: "thing" } },
3390
- },
3391
- {
3392
- name: "CONFLICTING_UNSAFE_NAME",
3393
- type: "another unsafe thing",
3394
- data: 1337,
3395
- },
3396
- {
3397
- name: "CONFLICTING_NAME_THREE",
3398
- type: "yet another unsafe thing",
3399
- data: "how is a string unsafe?",
3400
- },
3401
- {
3402
- name: "CONFLICTING_NAME_FOUR",
3403
- type: "a fourth unsafe thing",
3404
- data: null,
3405
- },
3406
- ],
3407
- },
3408
- vars: {
3409
- ENV_VAR_ONE: 123,
3410
- CONFLICTING_NAME_THREE: "Hello, I'm an environment variable",
3411
- },
3412
- wasm_modules: {
3413
- WASM_MODULE_ONE: "./some_wasm.wasm",
3414
- CONFLICTING_NAME_THREE: "./more_wasm.wasm",
3415
- },
3416
- data_blobs: {
3417
- DATA_BLOB_ONE: "./some_data.bin",
3418
- CONFLICTING_NAME_THREE: "./more_data.bin",
3419
- },
3420
- });
3421
-
3422
- writeWorkerSource({ type: "sw" });
3423
- fs.writeFileSync("./my-entire-app-depends-on-this.cfg", "config = value");
3424
- fs.writeFileSync(
3425
- "./the-entirety-of-human-knowledge.txt",
3426
- "Everything's bigger in Texas"
3427
- );
3428
- fs.writeFileSync("./some_wasm.wasm", "some wasm");
3429
- fs.writeFileSync("./more_wasm.wasm", "more wasm");
3430
-
3431
- await expect(runWrangler("publish index.js")).rejects
3432
- .toMatchInlineSnapshot(`
3433
- [Error: Processing wrangler.toml configuration:
3434
- - CONFLICTING_DURABLE_OBJECT_NAME assigned to multiple Durable Object bindings.
3435
- - CONFLICTING_KV_NAMESPACE_NAME assigned to multiple KV Namespace bindings.
3436
- - CONFLICTING_R2_BUCKET_NAME assigned to multiple R2 Bucket bindings.
3437
- - CONFLICTING_NAME_THREE assigned to R2 Bucket, Text Blob, Unsafe, Environment Variable, WASM Module, and Data Blob bindings.
3438
- - CONFLICTING_NAME_FOUR assigned to R2 Bucket, Text Blob, and Unsafe bindings.
3439
- - CONFLICTING_UNSAFE_NAME assigned to multiple Unsafe bindings.
3440
- - Bindings must have unique names, so that they can all be referenced in the worker.
3441
- Please change your bindings to have unique names.]
3442
- `);
3443
- expect(std.out).toMatchInlineSnapshot(`
3444
- "
3445
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
3446
- `);
3447
- expect(std.err).toMatchInlineSnapshot(`
3448
- "X [ERROR] Processing wrangler.toml configuration:
3449
-
3450
- - CONFLICTING_DURABLE_OBJECT_NAME assigned to multiple Durable Object bindings.
3451
- - CONFLICTING_KV_NAMESPACE_NAME assigned to multiple KV Namespace bindings.
3452
- - CONFLICTING_R2_BUCKET_NAME assigned to multiple R2 Bucket bindings.
3453
- - CONFLICTING_NAME_THREE assigned to R2 Bucket, Text Blob, Unsafe, Environment Variable, WASM
3454
- Module, and Data Blob bindings.
3455
- - CONFLICTING_NAME_FOUR assigned to R2 Bucket, Text Blob, and Unsafe bindings.
3456
- - CONFLICTING_UNSAFE_NAME assigned to multiple Unsafe bindings.
3457
- - Bindings must have unique names, so that they can all be referenced in the worker.
3458
- Please change your bindings to have unique names.
3459
-
3460
- "
3461
- `);
3462
- expect(std.warn).toMatchInlineSnapshot(`
3463
- "▲ [WARNING] Processing wrangler.toml configuration:
3464
-
3465
- - \\"unsafe\\" fields are experimental and may change or break at any time.
3466
-
3467
- "
3468
- `);
3469
- });
3470
-
3471
- describe("[wasm_modules]", () => {
3472
- it("should be able to define wasm modules for service-worker format workers", async () => {
3473
- writeWranglerToml({
3474
- wasm_modules: {
3475
- TESTWASMNAME: "./path/to/test.wasm",
3476
- },
3477
- });
3478
- writeWorkerSource({ type: "sw" });
3479
- fs.mkdirSync("./path/to", { recursive: true });
3480
- fs.writeFileSync("./path/to/test.wasm", "SOME WASM CONTENT");
3481
- mockUploadWorkerRequest({
3482
- expectedType: "sw",
3483
- expectedModules: { TESTWASMNAME: "SOME WASM CONTENT" },
3484
- expectedBindings: [
3485
- { name: "TESTWASMNAME", part: "TESTWASMNAME", type: "wasm_module" },
3486
- ],
3487
- });
3488
- mockSubDomainRequest();
3489
- await runWrangler("publish index.js");
3490
- expect(std.out).toMatchInlineSnapshot(`
3491
- "Uploaded test-name (TIMINGS)
3492
- Published test-name (TIMINGS)
3493
- test-name.test-sub-domain.workers.dev"
3494
- `);
3495
- expect(std.err).toMatchInlineSnapshot(`""`);
3496
- expect(std.warn).toMatchInlineSnapshot(`""`);
3497
- });
3498
-
3499
- it("should error when defining wasm modules for modules format workers", async () => {
3500
- writeWranglerToml({
3501
- wasm_modules: {
3502
- TESTWASMNAME: "./path/to/test.wasm",
3503
- },
3504
- });
3505
- writeWorkerSource({ type: "esm" });
3506
- fs.mkdirSync("./path/to", { recursive: true });
3507
- fs.writeFileSync("./path/to/test.wasm", "SOME WASM CONTENT");
3508
-
3509
- await expect(
3510
- runWrangler("publish index.js")
3511
- ).rejects.toThrowErrorMatchingInlineSnapshot(
3512
- `"You cannot configure [wasm_modules] with an ES module worker. Instead, import the .wasm module directly in your code"`
3513
- );
3514
- expect(std.out).toMatchInlineSnapshot(`
3515
- "
3516
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
3517
- `);
3518
- expect(std.err).toMatchInlineSnapshot(`
3519
- "X [ERROR] You cannot configure [wasm_modules] with an ES module worker. Instead, import the .wasm module directly in your code
3520
-
3521
- "
3522
- `);
3523
- expect(std.warn).toMatchInlineSnapshot(`""`);
3524
- });
3525
-
3526
- it("should resolve wasm modules relative to the wrangler.toml file", async () => {
3527
- fs.mkdirSync("./path/to/and/the/path/to/", { recursive: true });
3528
- fs.writeFileSync(
3529
- "./path/to/wrangler.toml",
3530
- TOML.stringify({
3531
- compatibility_date: "2022-01-12",
3532
- name: "test-name",
3533
- wasm_modules: {
3534
- TESTWASMNAME: "./and/the/path/to/test.wasm",
3535
- },
3536
- }),
3537
-
3538
- "utf-8"
3539
- );
3540
-
3541
- writeWorkerSource({ type: "sw" });
3542
- fs.writeFileSync(
3543
- "./path/to/and/the/path/to/test.wasm",
3544
- "SOME WASM CONTENT"
3545
- );
3546
- mockUploadWorkerRequest({
3547
- expectedType: "sw",
3548
- expectedModules: { TESTWASMNAME: "SOME WASM CONTENT" },
3549
- expectedBindings: [
3550
- { name: "TESTWASMNAME", part: "TESTWASMNAME", type: "wasm_module" },
3551
- ],
3552
- expectedCompatibilityDate: "2022-01-12",
3553
- });
3554
- mockSubDomainRequest();
3555
- await runWrangler("publish index.js --config ./path/to/wrangler.toml");
3556
- expect(std.out).toMatchInlineSnapshot(`
3557
- "Uploaded test-name (TIMINGS)
3558
- Published test-name (TIMINGS)
3559
- test-name.test-sub-domain.workers.dev"
3560
- `);
3561
- expect(std.err).toMatchInlineSnapshot(`""`);
3562
- expect(std.warn).toMatchInlineSnapshot(`""`);
3563
- });
3564
-
3565
- it("should be able to import .wasm modules from service-worker format workers", async () => {
3566
- writeWranglerToml();
3567
- fs.writeFileSync(
3568
- "./index.js",
3569
- "import TESTWASMNAME from './test.wasm';"
3570
- );
3571
- fs.writeFileSync("./test.wasm", "SOME WASM CONTENT");
3572
- mockUploadWorkerRequest({
3573
- expectedType: "sw",
3574
- expectedModules: {
3575
- __94b240d0d692281e6467aa42043986e5c7eea034_test_wasm:
3576
- "SOME WASM CONTENT",
3577
- },
3578
- expectedBindings: [
3579
- {
3580
- name: "__94b240d0d692281e6467aa42043986e5c7eea034_test_wasm",
3581
- part: "__94b240d0d692281e6467aa42043986e5c7eea034_test_wasm",
3582
- type: "wasm_module",
3583
- },
3584
- ],
3585
- });
3586
- mockSubDomainRequest();
3587
- await runWrangler("publish index.js");
3588
- expect(std.out).toMatchInlineSnapshot(`
3589
- "Uploaded test-name (TIMINGS)
3590
- Published test-name (TIMINGS)
3591
- test-name.test-sub-domain.workers.dev"
3592
- `);
3593
- expect(std.err).toMatchInlineSnapshot(`""`);
3594
- expect(std.warn).toMatchInlineSnapshot(`""`);
3595
- });
3596
- });
3597
-
3598
- describe("[text_blobs]", () => {
3599
- it("should be able to define text blobs for service-worker format workers", async () => {
3600
- writeWranglerToml({
3601
- text_blobs: {
3602
- TESTTEXTBLOBNAME: "./path/to/text.file",
3603
- },
3604
- });
3605
- writeWorkerSource({ type: "sw" });
3606
- fs.mkdirSync("./path/to", { recursive: true });
3607
- fs.writeFileSync("./path/to/text.file", "SOME TEXT CONTENT");
3608
- mockUploadWorkerRequest({
3609
- expectedType: "sw",
3610
- expectedModules: { TESTTEXTBLOBNAME: "SOME TEXT CONTENT" },
3611
- expectedBindings: [
3612
- {
3613
- name: "TESTTEXTBLOBNAME",
3614
- part: "TESTTEXTBLOBNAME",
3615
- type: "text_blob",
3616
- },
3617
- ],
3618
- });
3619
- mockSubDomainRequest();
3620
- await runWrangler("publish index.js");
3621
- expect(std.out).toMatchInlineSnapshot(`
3622
- "Uploaded test-name (TIMINGS)
3623
- Published test-name (TIMINGS)
3624
- test-name.test-sub-domain.workers.dev"
3625
- `);
3626
- expect(std.err).toMatchInlineSnapshot(`""`);
3627
- expect(std.warn).toMatchInlineSnapshot(`""`);
3628
- });
3629
-
3630
- it("should error when defining text blobs for modules format workers", async () => {
3631
- writeWranglerToml({
3632
- text_blobs: {
3633
- TESTTEXTBLOBNAME: "./path/to/text.file",
3634
- },
3635
- });
3636
- writeWorkerSource({ type: "esm" });
3637
- fs.mkdirSync("./path/to", { recursive: true });
3638
- fs.writeFileSync("./path/to/text.file", "SOME TEXT CONTENT");
3639
-
3640
- await expect(
3641
- runWrangler("publish index.js")
3642
- ).rejects.toThrowErrorMatchingInlineSnapshot(
3643
- `"You cannot configure [text_blobs] with an ES module worker. Instead, import the file directly in your code, and optionally configure \`[rules]\` in your wrangler.toml"`
3644
- );
3645
- expect(std.out).toMatchInlineSnapshot(`
3646
- "
3647
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
3648
- `);
3649
- expect(std.err).toMatchInlineSnapshot(`
3650
- "X [ERROR] You cannot configure [text_blobs] with an ES module worker. Instead, import the file directly in your code, and optionally configure \`[rules]\` in your wrangler.toml
3651
-
3652
- "
3653
- `);
3654
- expect(std.warn).toMatchInlineSnapshot(`""`);
3655
- });
3656
-
3657
- it("should resolve text blobs relative to the wrangler.toml file", async () => {
3658
- fs.mkdirSync("./path/to/and/the/path/to/", { recursive: true });
3659
- fs.writeFileSync(
3660
- "./path/to/wrangler.toml",
3661
- TOML.stringify({
3662
- compatibility_date: "2022-01-12",
3663
- name: "test-name",
3664
- text_blobs: {
3665
- TESTTEXTBLOBNAME: "./and/the/path/to/text.file",
3666
- },
3667
- }),
3668
-
3669
- "utf-8"
3670
- );
3671
-
3672
- writeWorkerSource({ type: "sw" });
3673
- fs.writeFileSync(
3674
- "./path/to/and/the/path/to/text.file",
3675
- "SOME TEXT CONTENT"
3676
- );
3677
- mockUploadWorkerRequest({
3678
- expectedType: "sw",
3679
- expectedModules: { TESTTEXTBLOBNAME: "SOME TEXT CONTENT" },
3680
- expectedBindings: [
3681
- {
3682
- name: "TESTTEXTBLOBNAME",
3683
- part: "TESTTEXTBLOBNAME",
3684
- type: "text_blob",
3685
- },
3686
- ],
3687
- expectedCompatibilityDate: "2022-01-12",
3688
- });
3689
- mockSubDomainRequest();
3690
- await runWrangler("publish index.js --config ./path/to/wrangler.toml");
3691
- expect(std.out).toMatchInlineSnapshot(`
3692
- "Uploaded test-name (TIMINGS)
3693
- Published test-name (TIMINGS)
3694
- test-name.test-sub-domain.workers.dev"
3695
- `);
3696
- expect(std.err).toMatchInlineSnapshot(`""`);
3697
- expect(std.warn).toMatchInlineSnapshot(`""`);
3698
- });
3699
- });
3700
-
3701
- describe("[data_blobs]", () => {
3702
- it("should be able to define data blobs for service-worker format workers", async () => {
3703
- writeWranglerToml({
3704
- data_blobs: {
3705
- TESTDATABLOBNAME: "./path/to/data.bin",
3706
- },
3707
- });
3708
- writeWorkerSource({ type: "sw" });
3709
- fs.mkdirSync("./path/to", { recursive: true });
3710
- fs.writeFileSync("./path/to/data.bin", "SOME DATA CONTENT");
3711
- mockUploadWorkerRequest({
3712
- expectedType: "sw",
3713
- expectedModules: { TESTDATABLOBNAME: "SOME DATA CONTENT" },
3714
- expectedBindings: [
3715
- {
3716
- name: "TESTDATABLOBNAME",
3717
- part: "TESTDATABLOBNAME",
3718
- type: "data_blob",
3719
- },
3720
- ],
3721
- });
3722
- mockSubDomainRequest();
3723
- await runWrangler("publish index.js");
3724
- expect(std.out).toMatchInlineSnapshot(`
3725
- "Uploaded test-name (TIMINGS)
3726
- Published test-name (TIMINGS)
3727
- test-name.test-sub-domain.workers.dev"
3728
- `);
3729
- expect(std.err).toMatchInlineSnapshot(`""`);
3730
- expect(std.warn).toMatchInlineSnapshot(`""`);
3731
- });
3732
-
3733
- it("should error when defining data blobs for modules format workers", async () => {
3734
- writeWranglerToml({
3735
- data_blobs: {
3736
- TESTDATABLOBNAME: "./path/to/data.bin",
3737
- },
3738
- });
3739
- writeWorkerSource({ type: "esm" });
3740
- fs.mkdirSync("./path/to", { recursive: true });
3741
- fs.writeFileSync("./path/to/data.bin", "SOME DATA CONTENT");
3742
-
3743
- await expect(
3744
- runWrangler("publish index.js")
3745
- ).rejects.toThrowErrorMatchingInlineSnapshot(
3746
- `"You cannot configure [data_blobs] with an ES module worker. Instead, import the file directly in your code, and optionally configure \`[rules]\` in your wrangler.toml"`
3747
- );
3748
- expect(std.out).toMatchInlineSnapshot(`
3749
- "
3750
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
3751
- `);
3752
- expect(std.err).toMatchInlineSnapshot(`
3753
- "X [ERROR] You cannot configure [data_blobs] with an ES module worker. Instead, import the file directly in your code, and optionally configure \`[rules]\` in your wrangler.toml
3754
-
3755
- "
3756
- `);
3757
- expect(std.warn).toMatchInlineSnapshot(`""`);
3758
- });
3759
-
3760
- it("should resolve data blobs relative to the wrangler.toml file", async () => {
3761
- fs.mkdirSync("./path/to/and/the/path/to/", { recursive: true });
3762
- fs.writeFileSync(
3763
- "./path/to/wrangler.toml",
3764
- TOML.stringify({
3765
- compatibility_date: "2022-01-12",
3766
- name: "test-name",
3767
- data_blobs: {
3768
- TESTDATABLOBNAME: "./and/the/path/to/data.bin",
3769
- },
3770
- }),
3771
-
3772
- "utf-8"
3773
- );
3774
-
3775
- writeWorkerSource({ type: "sw" });
3776
- fs.writeFileSync(
3777
- "./path/to/and/the/path/to/data.bin",
3778
- "SOME DATA CONTENT"
3779
- );
3780
- mockUploadWorkerRequest({
3781
- expectedType: "sw",
3782
- expectedModules: { TESTDATABLOBNAME: "SOME DATA CONTENT" },
3783
- expectedBindings: [
3784
- {
3785
- name: "TESTDATABLOBNAME",
3786
- part: "TESTDATABLOBNAME",
3787
- type: "data_blob",
3788
- },
3789
- ],
3790
- expectedCompatibilityDate: "2022-01-12",
3791
- });
3792
- mockSubDomainRequest();
3793
- await runWrangler("publish index.js --config ./path/to/wrangler.toml");
3794
- expect(std.out).toMatchInlineSnapshot(`
3795
- "Uploaded test-name (TIMINGS)
3796
- Published test-name (TIMINGS)
3797
- test-name.test-sub-domain.workers.dev"
3798
- `);
3799
- expect(std.err).toMatchInlineSnapshot(`""`);
3800
- expect(std.warn).toMatchInlineSnapshot(`""`);
3801
- });
3802
- });
3803
-
3804
- describe("[vars]", () => {
3805
- it("should support json bindings", async () => {
3806
- writeWranglerToml({
3807
- vars: {
3808
- text: "plain ol' string",
3809
- count: 1,
3810
- complex: { enabled: true, id: 123 },
3811
- },
3812
- });
3813
- writeWorkerSource();
3814
- mockSubDomainRequest();
3815
- mockUploadWorkerRequest({
3816
- expectedBindings: [
3817
- { name: "text", type: "plain_text", text: "plain ol' string" },
3818
- { name: "count", type: "json", json: 1 },
3819
- {
3820
- name: "complex",
3821
- type: "json",
3822
- json: { enabled: true, id: 123 },
3823
- },
3824
- ],
3825
- });
3826
-
3827
- await runWrangler("publish index.js");
3828
- expect(std.out).toMatchInlineSnapshot(`
3829
- "Uploaded test-name (TIMINGS)
3830
- Published test-name (TIMINGS)
3831
- test-name.test-sub-domain.workers.dev"
3832
- `);
3833
- expect(std.err).toMatchInlineSnapshot(`""`);
3834
- expect(std.warn).toMatchInlineSnapshot(`""`);
3835
- });
3836
- });
3837
-
3838
- describe("[r2_buckets]", () => {
3839
- it("should support r2 bucket bindings", async () => {
3840
- writeWranglerToml({
3841
- r2_buckets: [{ binding: "FOO", bucket_name: "foo-bucket" }],
3842
- });
3843
- writeWorkerSource();
3844
- mockSubDomainRequest();
3845
- mockUploadWorkerRequest({
3846
- expectedBindings: [
3847
- { bucket_name: "foo-bucket", name: "FOO", type: "r2_bucket" },
3848
- ],
3849
- });
3850
-
3851
- await runWrangler("publish index.js");
3852
- expect(std.out).toMatchInlineSnapshot(`
3853
- "Uploaded test-name (TIMINGS)
3854
- Published test-name (TIMINGS)
3855
- test-name.test-sub-domain.workers.dev"
3856
- `);
3857
- expect(std.err).toMatchInlineSnapshot(`""`);
3858
- expect(std.warn).toMatchInlineSnapshot(`""`);
3859
- });
3860
- });
3861
-
3862
- describe("[durable_objects]", () => {
3863
- it("should support durable object bindings", async () => {
3864
- writeWranglerToml({
3865
- durable_objects: {
3866
- bindings: [
3867
- {
3868
- name: "EXAMPLE_DO_BINDING",
3869
- class_name: "ExampleDurableObject",
3870
- },
3871
- ],
3872
- },
3873
- migrations: [{ tag: "v1", new_classes: ["ExampleDurableObject"] }],
3874
- });
3875
- fs.writeFileSync(
3876
- "index.js",
3877
- `export class ExampleDurableObject {}; export default{};`
3878
- );
3879
- mockSubDomainRequest();
3880
- mockLegacyScriptData({
3881
- scripts: [{ id: "test-name", migration_tag: "v1" }],
3882
- });
3883
- mockUploadWorkerRequest({
3884
- expectedBindings: [
3885
- {
3886
- class_name: "ExampleDurableObject",
3887
- name: "EXAMPLE_DO_BINDING",
3888
- type: "durable_object_namespace",
3889
- },
3890
- ],
3891
- });
3892
-
3893
- await runWrangler("publish index.js");
3894
- expect(std.out).toMatchInlineSnapshot(`
3895
- "Uploaded test-name (TIMINGS)
3896
- Published test-name (TIMINGS)
3897
- test-name.test-sub-domain.workers.dev"
3898
- `);
3899
- expect(std.err).toMatchInlineSnapshot(`""`);
3900
- expect(std.warn).toMatchInlineSnapshot(`""`);
3901
- });
3902
-
3903
- it("should support service-workers binding to external durable objects", async () => {
3904
- writeWranglerToml({
3905
- durable_objects: {
3906
- bindings: [
3907
- {
3908
- name: "EXAMPLE_DO_BINDING",
3909
- class_name: "ExampleDurableObject",
3910
- script_name: "example-do-binding-worker",
3911
- },
3912
- ],
3913
- },
3914
- });
3915
- writeWorkerSource({ type: "sw" });
3916
- mockSubDomainRequest();
3917
- mockUploadWorkerRequest({
3918
- expectedType: "sw",
3919
- expectedBindings: [
3920
- {
3921
- name: "EXAMPLE_DO_BINDING",
3922
- class_name: "ExampleDurableObject",
3923
- script_name: "example-do-binding-worker",
3924
- type: "durable_object_namespace",
3925
- },
3926
- ],
3927
- });
3928
-
3929
- await runWrangler("publish index.js");
3930
- expect(std.out).toMatchInlineSnapshot(`
3931
- "Uploaded test-name (TIMINGS)
3932
- Published test-name (TIMINGS)
3933
- test-name.test-sub-domain.workers.dev"
3934
- `);
3935
- expect(std.err).toMatchInlineSnapshot(`""`);
3936
- expect(std.warn).toMatchInlineSnapshot(`""`);
3937
- });
3938
-
3939
- it("should support module workers implementing durable objects", async () => {
3940
- writeWranglerToml({
3941
- durable_objects: {
3942
- bindings: [
3943
- {
3944
- name: "EXAMPLE_DO_BINDING",
3945
- class_name: "ExampleDurableObject",
3946
- },
3947
- ],
3948
- },
3949
- migrations: [{ tag: "v1", new_classes: ["ExampleDurableObject"] }],
3950
- });
3951
- fs.writeFileSync(
3952
- "index.js",
3953
- `export class ExampleDurableObject {}; export default{};`
3954
- );
3955
- mockSubDomainRequest();
3956
- mockLegacyScriptData({
3957
- scripts: [{ id: "test-name", migration_tag: "v1" }],
3958
- });
3959
- mockUploadWorkerRequest({
3960
- expectedType: "esm",
3961
- expectedBindings: [
3962
- {
3963
- name: "EXAMPLE_DO_BINDING",
3964
- class_name: "ExampleDurableObject",
3965
- type: "durable_object_namespace",
3966
- },
3967
- ],
3968
- });
3969
-
3970
- await runWrangler("publish index.js");
3971
- expect(std.out).toMatchInlineSnapshot(`
3972
- "Uploaded test-name (TIMINGS)
3973
- Published test-name (TIMINGS)
3974
- test-name.test-sub-domain.workers.dev"
3975
- `);
3976
- expect(std.err).toMatchInlineSnapshot(`""`);
3977
- expect(std.warn).toMatchInlineSnapshot(`""`);
3978
- });
3979
-
3980
- it("should error when detecting a service-worker worker implementing durable objects", async () => {
3981
- writeWranglerToml({
3982
- durable_objects: {
3983
- bindings: [
3984
- {
3985
- name: "EXAMPLE_DO_BINDING",
3986
- class_name: "ExampleDurableObject",
3987
- },
3988
- ],
3989
- },
3990
- });
3991
- writeWorkerSource({ type: "sw" });
3992
- mockSubDomainRequest();
3993
-
3994
- await expect(runWrangler("publish index.js")).rejects
3995
- .toThrowErrorMatchingInlineSnapshot(`
3996
- "You seem to be trying to use Durable Objects in a Worker written as a service-worker.
3997
- You can use Durable Objects defined in other Workers by specifying a \`script_name\` in your wrangler.toml, where \`script_name\` is the name of the Worker that implements that Durable Object. For example:
3998
- { name = EXAMPLE_DO_BINDING, class_name = ExampleDurableObject } ==> { name = EXAMPLE_DO_BINDING, class_name = ExampleDurableObject, script_name = example-do-binding-worker }
3999
- Alternatively, migrate your worker to ES Module syntax to implement a Durable Object in this Worker:
4000
- https://developers.cloudflare.com/workers/learning/migrating-to-module-workers/"
4001
- `);
4002
- });
4003
- });
4004
-
4005
- describe("[unsafe]", () => {
4006
- it("should warn if using unsafe bindings", async () => {
4007
- writeWranglerToml({
4008
- unsafe: {
4009
- bindings: [
4010
- {
4011
- name: "my-binding",
4012
- type: "binding-type",
4013
- param: "binding-param",
4014
- },
4015
- ],
4016
- },
4017
- });
4018
- writeWorkerSource();
4019
- mockSubDomainRequest();
4020
- mockUploadWorkerRequest({
4021
- expectedBindings: [
4022
- {
4023
- name: "my-binding",
4024
- type: "binding-type",
4025
- param: "binding-param",
4026
- },
4027
- ],
4028
- });
4029
-
4030
- await runWrangler("publish index.js");
4031
- expect(std.out).toMatchInlineSnapshot(`
4032
- "Uploaded test-name (TIMINGS)
4033
- Published test-name (TIMINGS)
4034
- test-name.test-sub-domain.workers.dev"
4035
- `);
4036
- expect(std.err).toMatchInlineSnapshot(`""`);
4037
- expect(std.warn).toMatchInlineSnapshot(`
4038
- "▲ [WARNING] Processing wrangler.toml configuration:
4039
-
4040
- - \\"unsafe\\" fields are experimental and may change or break at any time.
4041
-
4042
- "
4043
- `);
4044
- });
4045
- it("should warn if using unsafe bindings already handled by wrangler", async () => {
4046
- writeWranglerToml({
4047
- unsafe: {
4048
- bindings: [
4049
- {
4050
- name: "my-binding",
4051
- type: "plain_text",
4052
- text: "text",
4053
- },
4054
- ],
4055
- },
4056
- });
4057
- writeWorkerSource();
4058
- mockSubDomainRequest();
4059
- mockUploadWorkerRequest({
4060
- expectedBindings: [
4061
- {
4062
- name: "my-binding",
4063
- type: "plain_text",
4064
- text: "text",
4065
- },
4066
- ],
4067
- });
4068
-
4069
- await runWrangler("publish index.js");
4070
- expect(std.out).toMatchInlineSnapshot(`
4071
- "Uploaded test-name (TIMINGS)
4072
- Published test-name (TIMINGS)
4073
- test-name.test-sub-domain.workers.dev"
4074
- `);
4075
- expect(std.err).toMatchInlineSnapshot(`""`);
4076
- expect(std.warn).toMatchInlineSnapshot(`
4077
- "▲ [WARNING] Processing wrangler.toml configuration:
4078
-
4079
- - \\"unsafe\\" fields are experimental and may change or break at any time.
4080
- - \\"unsafe.bindings[0]\\": {\\"name\\":\\"my-binding\\",\\"type\\":\\"plain_text\\",\\"text\\":\\"text\\"}
4081
- - The binding type \\"plain_text\\" is directly supported by wrangler.
4082
- Consider migrating this unsafe binding to a format for 'plain_text' bindings that is
4083
- supported by wrangler for optimal support.
4084
- For more details, see https://developers.cloudflare.com/workers/cli-wrangler/configuration
4085
-
4086
- "
4087
- `);
4088
- });
4089
- });
4090
- });
4091
-
4092
- describe("upload rules", () => {
4093
- it("should be able to define rules for uploading non-js modules (sw)", async () => {
4094
- writeWranglerToml({
4095
- rules: [{ type: "Text", globs: ["**/*.file"], fallthrough: true }],
4096
- });
4097
- fs.writeFileSync("./index.js", `import TEXT from './text.file';`);
4098
- fs.writeFileSync("./text.file", "SOME TEXT CONTENT");
4099
- mockSubDomainRequest();
4100
- mockUploadWorkerRequest({
4101
- expectedType: "sw",
4102
- expectedBindings: [
4103
- {
4104
- name: "__2d91d1c4dd6e57d4f5432187ab7c25f45a8973f0_text_file",
4105
- part: "__2d91d1c4dd6e57d4f5432187ab7c25f45a8973f0_text_file",
4106
- type: "text_blob",
4107
- },
4108
- ],
4109
- expectedModules: {
4110
- __2d91d1c4dd6e57d4f5432187ab7c25f45a8973f0_text_file:
4111
- "SOME TEXT CONTENT",
4112
- },
4113
- });
4114
- await runWrangler("publish index.js");
4115
- expect(std.out).toMatchInlineSnapshot(`
4116
- "Uploaded test-name (TIMINGS)
4117
- Published test-name (TIMINGS)
4118
- test-name.test-sub-domain.workers.dev"
4119
- `);
4120
- expect(std.err).toMatchInlineSnapshot(`""`);
4121
- expect(std.warn).toMatchInlineSnapshot(`""`);
4122
- });
4123
-
4124
- it("should be able to define rules for uploading non-js modules (esm)", async () => {
4125
- writeWranglerToml({
4126
- rules: [{ type: "Text", globs: ["**/*.file"], fallthrough: true }],
4127
- });
4128
- fs.writeFileSync(
4129
- "./index.js",
4130
- `import TEXT from './text.file'; export default {};`
4131
- );
4132
- fs.writeFileSync("./text.file", "SOME TEXT CONTENT");
4133
- mockSubDomainRequest();
4134
- mockUploadWorkerRequest({
4135
- expectedType: "esm",
4136
- expectedBindings: [],
4137
- expectedModules: {
4138
- "./2d91d1c4dd6e57d4f5432187ab7c25f45a8973f0-text.file":
4139
- "SOME TEXT CONTENT",
4140
- },
4141
- });
4142
- await runWrangler("publish index.js");
4143
- expect(std.out).toMatchInlineSnapshot(`
4144
- "Uploaded test-name (TIMINGS)
4145
- Published test-name (TIMINGS)
4146
- test-name.test-sub-domain.workers.dev"
4147
- `);
4148
- expect(std.err).toMatchInlineSnapshot(`""`);
4149
- expect(std.warn).toMatchInlineSnapshot(`""`);
4150
- });
4151
-
4152
- it("should log a deprecation warning when using `build.upload.rules`", async () => {
4153
- writeWranglerToml({
4154
- build: {
4155
- upload: {
4156
- rules: [{ type: "Text", globs: ["**/*.file"], fallthrough: true }],
4157
- },
4158
- },
4159
- });
4160
- fs.writeFileSync(
4161
- "./index.js",
4162
- `import TEXT from './text.file'; export default {};`
4163
- );
4164
- fs.writeFileSync("./text.file", "SOME TEXT CONTENT");
4165
- mockSubDomainRequest();
4166
- mockUploadWorkerRequest({
4167
- expectedType: "esm",
4168
- expectedBindings: [],
4169
- expectedModules: {
4170
- "./2d91d1c4dd6e57d4f5432187ab7c25f45a8973f0-text.file":
4171
- "SOME TEXT CONTENT",
4172
- },
4173
- });
4174
- await runWrangler("publish index.js");
4175
- expect(std.out).toMatchInlineSnapshot(`
4176
- "Uploaded test-name (TIMINGS)
4177
- Published test-name (TIMINGS)
4178
- test-name.test-sub-domain.workers.dev"
4179
- `);
4180
- expect(std.err).toMatchInlineSnapshot(`""`);
4181
- expect(std.warn).toMatchInlineSnapshot(`
4182
- "▲ [WARNING] Processing wrangler.toml configuration:
4183
-
4184
- - Deprecation: The \`build.upload.rules\` config field is no longer used, the rules should be
4185
- specified via the \`rules\` config field. Delete the \`build.upload\` field from the configuration
4186
- file, and add this:
4187
- \`\`\`
4188
- [[rules]]
4189
- type = \\"Text\\"
4190
- globs = [ \\"**/*.file\\" ]
4191
- fallthrough = true
4192
- \`\`\`
4193
-
4194
- "
4195
- `);
4196
- });
4197
-
4198
- it("should be able to use fallthrough:true for multiple rules", async () => {
4199
- writeWranglerToml({
4200
- rules: [
4201
- { type: "Text", globs: ["**/*.file"], fallthrough: true },
4202
- { type: "Text", globs: ["**/*.other"], fallthrough: true },
4203
- ],
4204
- });
4205
- fs.writeFileSync(
4206
- "./index.js",
4207
- `import TEXT from './text.file'; import OTHER from './other.other'; export default {};`
4208
- );
4209
- fs.writeFileSync("./text.file", "SOME TEXT CONTENT");
4210
- fs.writeFileSync("./other.other", "SOME OTHER TEXT CONTENT");
4211
- mockSubDomainRequest();
4212
- mockUploadWorkerRequest({
4213
- expectedType: "esm",
4214
- expectedBindings: [],
4215
- expectedModules: {
4216
- "./2d91d1c4dd6e57d4f5432187ab7c25f45a8973f0-text.file":
4217
- "SOME TEXT CONTENT",
4218
- "./16347a01366873ed80fe45115119de3c92ab8db0-other.other":
4219
- "SOME OTHER TEXT CONTENT",
4220
- },
4221
- });
4222
- await runWrangler("publish index.js");
4223
- expect(std.out).toMatchInlineSnapshot(`
4224
- "Uploaded test-name (TIMINGS)
4225
- Published test-name (TIMINGS)
4226
- test-name.test-sub-domain.workers.dev"
4227
- `);
4228
- expect(std.err).toMatchInlineSnapshot(`""`);
4229
- expect(std.warn).toMatchInlineSnapshot(`""`);
4230
- });
4231
-
4232
- it("should be able to use fallthrough:false for multiple rules", async () => {
4233
- writeWranglerToml({
4234
- rules: [
4235
- { type: "Text", globs: ["**/*.file"], fallthrough: false },
4236
- { type: "Text", globs: ["**/*.other"] },
4237
- ],
4238
- });
4239
- fs.writeFileSync(
4240
- "./index.js",
4241
- `import TEXT from './text.file'; import OTHER from './other.other'; export default {};`
4242
- );
4243
- fs.writeFileSync("./text.file", "SOME TEXT CONTENT");
4244
- fs.writeFileSync("./other.other", "SOME OTHER TEXT CONTENT");
4245
-
4246
- // We throw an error when we come across a file that matched a rule
4247
- // but was skipped because of fallthrough = false
4248
- let err: Error | undefined;
4249
- try {
4250
- await runWrangler("publish index.js");
4251
- } catch (e) {
4252
- err = e as Error;
4253
- }
4254
- expect(err?.message).toMatch(
4255
- `The file ./other.other matched a module rule in your configuration ({"type":"Text","globs":["**/*.other"]}), but was ignored because a previous rule with the same type was not marked as \`fallthrough = true\`.`
4256
- );
4257
- });
4258
-
4259
- it("should warn when multiple rules for the same type do not have fallback defined", async () => {
4260
- writeWranglerToml({
4261
- rules: [
4262
- { type: "Text", globs: ["**/*.file"] },
4263
- { type: "Text", globs: ["**/*.other"] },
4264
- ],
4265
- });
4266
- fs.writeFileSync(
4267
- "./index.js",
4268
- `import TEXT from './text.file'; import OTHER from './other.other'; export default {};`
4269
- );
4270
- fs.writeFileSync("./text.file", "SOME TEXT CONTENT");
4271
- fs.writeFileSync("./other.other", "SOME OTHER TEXT CONTENT");
4272
-
4273
- // We throw an error when we come across a file that matched a rule
4274
- // but was skipped because of fallthrough = false
4275
- let err: Error | undefined;
4276
- try {
4277
- await runWrangler("publish index.js");
4278
- } catch (e) {
4279
- err = e as Error;
4280
- }
4281
- expect(err?.message).toMatch(
4282
- `The file ./other.other matched a module rule in your configuration ({"type":"Text","globs":["**/*.other"]}), but was ignored because a previous rule with the same type was not marked as \`fallthrough = true\`.`
4283
- );
4284
- // and the warnings because fallthrough was not explicitly set
4285
- expect(std.warn).toMatchInlineSnapshot(`
4286
- "▲ [WARNING] The module rule at position 1 ({\\"type\\":\\"Text\\",\\"globs\\":[\\"**/*.other\\"]}) has the same type as a previous rule (at position 0, {\\"type\\":\\"Text\\",\\"globs\\":[\\"**/*.file\\"]}). This rule will be ignored. To the previous rule, add \`fallthrough = true\` to allow this one to also be used, or \`fallthrough = false\` to silence this warning.
4287
-
4288
-
4289
- ▲ [WARNING] The default module rule {\\"type\\":\\"Text\\",\\"globs\\":[\\"**/*.txt\\",\\"**/*.html\\"]} has the same type as a previous rule (at position 0, {\\"type\\":\\"Text\\",\\"globs\\":[\\"**/*.file\\"]}). This rule will be ignored. To the previous rule, add \`fallthrough = true\` to allow the default one to also be used, or \`fallthrough = false\` to silence this warning.
4290
-
4291
- "
4292
- `);
4293
- });
4294
-
4295
- describe("inject process.env.NODE_ENV", () => {
4296
- let actualProcessEnvNodeEnv: string | undefined;
4297
- beforeEach(() => {
4298
- actualProcessEnvNodeEnv = process.env.NODE_ENV;
4299
- process.env.NODE_ENV = "some-node-env";
4300
- });
4301
- afterEach(() => {
4302
- process.env.NODE_ENV = actualProcessEnvNodeEnv;
4303
- });
4304
- it("should replace `process.env.NODE_ENV` in scripts", async () => {
4305
- writeWranglerToml();
4306
- fs.writeFileSync(
4307
- "./index.js",
4308
- `export default {
4309
- fetch(){
4310
- return new Response(process.env.NODE_ENV);
4311
- }
4312
- }`
4313
- );
4314
- mockSubDomainRequest();
4315
- mockUploadWorkerRequest({
4316
- expectedEntry: `return new Response("some-node-env");`,
4317
- });
4318
- await runWrangler("publish index.js");
4319
- expect(std.out).toMatchInlineSnapshot(`
4320
- "Uploaded test-name (TIMINGS)
4321
- Published test-name (TIMINGS)
4322
- test-name.test-sub-domain.workers.dev"
4323
- `);
4324
- expect(std.err).toMatchInlineSnapshot(`""`);
4325
- expect(std.warn).toMatchInlineSnapshot(`""`);
4326
- });
4327
- });
4328
- });
4329
-
4330
- describe("legacy module specifiers", () => {
4331
- it("should work with legacy module specifiers, with a deprecation warning (1)", async () => {
4332
- writeWranglerToml({
4333
- rules: [{ type: "Text", globs: ["**/*.file"], fallthrough: false }],
4334
- });
4335
- fs.writeFileSync(
4336
- "./index.js",
4337
- `import TEXT from 'text.file'; export default {};`
4338
- );
4339
- fs.writeFileSync("./text.file", "SOME TEXT CONTENT");
4340
- mockSubDomainRequest();
4341
- mockUploadWorkerRequest({
4342
- expectedModules: {
4343
- "./2d91d1c4dd6e57d4f5432187ab7c25f45a8973f0-text.file":
4344
- "SOME TEXT CONTENT",
4345
- },
4346
- });
4347
- await runWrangler("publish index.js");
4348
- expect(std.out).toMatchInlineSnapshot(`
4349
- "Uploaded test-name (TIMINGS)
4350
- Published test-name (TIMINGS)
4351
- test-name.test-sub-domain.workers.dev"
4352
- `);
4353
- expect(std.err).toMatchInlineSnapshot(`""`);
4354
- expect(std.warn).toMatchInlineSnapshot(`
4355
- "▲ [WARNING] Deprecation: detected a legacy module import in \\"./index.js\\". This will stop working in the future. Replace references to \\"text.file\\" with \\"./text.file\\";
4356
-
4357
- "
4358
- `);
4359
- });
4360
-
4361
- it("should work with legacy module specifiers, with a deprecation warning (2)", async () => {
4362
- writeWranglerToml();
4363
- fs.writeFileSync(
4364
- "./index.js",
4365
- `import WASM from 'index.wasm'; export default {};`
4366
- );
4367
- fs.writeFileSync("./index.wasm", "SOME WASM CONTENT");
4368
- mockSubDomainRequest();
4369
- mockUploadWorkerRequest({
4370
- expectedModules: {
4371
- "./94b240d0d692281e6467aa42043986e5c7eea034-index.wasm":
4372
- "SOME WASM CONTENT",
4373
- },
4374
- });
4375
- await runWrangler("publish index.js");
4376
- expect(std.out).toMatchInlineSnapshot(`
4377
- "Uploaded test-name (TIMINGS)
4378
- Published test-name (TIMINGS)
4379
- test-name.test-sub-domain.workers.dev"
4380
- `);
4381
- expect(std.err).toMatchInlineSnapshot(`""`);
4382
- expect(std.warn).toMatchInlineSnapshot(`
4383
- "▲ [WARNING] Deprecation: detected a legacy module import in \\"./index.js\\". This will stop working in the future. Replace references to \\"index.wasm\\" with \\"./index.wasm\\";
4384
-
4385
- "
4386
- `);
4387
- });
4388
-
4389
- it("should not match regular module specifiers when there aren't any possible legacy module matches", async () => {
4390
- // see https://github.com/cloudflare/wrangler2/issues/655 for bug details
4391
-
4392
- fs.writeFileSync(
4393
- "./index.js",
4394
- `import inner from './inner/index.js'; export default {};`
4395
- );
4396
- fs.mkdirSync("./inner", { recursive: true });
4397
- fs.writeFileSync("./inner/index.js", `export default 123`);
4398
- mockSubDomainRequest();
4399
- mockUploadWorkerRequest();
4400
-
4401
- await runWrangler(
4402
- "publish index.js --compatibility-date 2022-03-17 --name test-name"
4403
- );
4404
- expect(std.out).toMatchInlineSnapshot(`
4405
- "Uploaded test-name (TIMINGS)
4406
- Published test-name (TIMINGS)
4407
- test-name.test-sub-domain.workers.dev"
4408
- `);
4409
- expect(std.err).toMatchInlineSnapshot(`""`);
4410
- expect(std.warn).toMatchInlineSnapshot(`""`);
4411
- });
4412
- });
4413
-
4414
- describe("tsconfig", () => {
4415
- it("should use compilerOptions.paths to resolve modules", async () => {
4416
- writeWranglerToml({
4417
- main: "index.ts",
4418
- });
4419
- fs.writeFileSync(
4420
- "index.ts",
4421
- `import { foo } from '~lib/foo'; export default { fetch() { return new Response(foo)} }`
4422
- );
4423
- fs.mkdirSync("lib", { recursive: true });
4424
- fs.writeFileSync("lib/foo.ts", `export const foo = 123;`);
4425
- fs.writeFileSync(
4426
- "tsconfig.json",
4427
- JSON.stringify({
4428
- compilerOptions: {
4429
- baseUrl: ".",
4430
- paths: {
4431
- "~lib/*": ["lib/*"],
4432
- },
4433
- },
4434
- })
4435
- );
4436
- mockSubDomainRequest();
4437
- mockUploadWorkerRequest({
4438
- expectedEntry: "var foo = 123;", // make sure it imported the module correctly
4439
- });
4440
- await runWrangler("publish index.ts");
4441
- expect(std).toMatchInlineSnapshot(`
4442
- Object {
4443
- "debug": "",
4444
- "err": "",
4445
- "out": "Uploaded test-name (TIMINGS)
4446
- Published test-name (TIMINGS)
4447
- test-name.test-sub-domain.workers.dev",
4448
- "warn": "",
4449
- }
4450
- `);
4451
- });
4452
-
4453
- it("should output to target es2020 even if tsconfig says otherwise", async () => {
4454
- writeWranglerToml();
4455
- writeWorkerSource();
4456
- fs.writeFileSync(
4457
- "tsconfig.json",
4458
- JSON.stringify({
4459
- compilerOptions: {
4460
- target: "es5",
4461
- module: "commonjs",
4462
- },
4463
- })
4464
- );
4465
- mockSubDomainRequest();
4466
- mockUploadWorkerRequest({
4467
- expectedEntry: "export {", // just check that the export is preserved
4468
- });
4469
- await runWrangler("publish index.js"); // this would throw if we tried to compile with es5
4470
- expect(std).toMatchInlineSnapshot(`
4471
- Object {
4472
- "debug": "",
4473
- "err": "",
4474
- "out": "Uploaded test-name (TIMINGS)
4475
- Published test-name (TIMINGS)
4476
- test-name.test-sub-domain.workers.dev",
4477
- "warn": "",
4478
- }
4479
- `);
4480
- });
4481
- });
4482
-
4483
- describe("--outdir", () => {
4484
- it("should generate built assets at --outdir if specified", async () => {
4485
- writeWranglerToml();
4486
- writeWorkerSource();
4487
- mockSubDomainRequest();
4488
- mockUploadWorkerRequest();
4489
- await runWrangler("publish index.js --outdir some-dir");
4490
- expect(fs.existsSync("some-dir/index.js")).toBe(true);
4491
- expect(fs.existsSync("some-dir/index.js.map")).toBe(true);
4492
- expect(std).toMatchInlineSnapshot(`
4493
- Object {
4494
- "debug": "",
4495
- "err": "",
4496
- "out": "Uploaded test-name (TIMINGS)
4497
- Published test-name (TIMINGS)
4498
- test-name.test-sub-domain.workers.dev",
4499
- "warn": "",
4500
- }
4501
- `);
4502
- });
4503
- });
4504
-
4505
- describe("--dry-run", () => {
4506
- it("should not publish the worker if --dry-run is specified", async () => {
4507
- writeWranglerToml();
4508
- writeWorkerSource();
4509
- await runWrangler("publish index.js --dry-run");
4510
- expect(std).toMatchInlineSnapshot(`
4511
- Object {
4512
- "debug": "",
4513
- "err": "",
4514
- "out": "--dry-run: exiting now.",
4515
- "warn": "",
4516
- }
4517
- `);
4518
- });
4519
- });
4520
-
4521
- describe("--node-compat", () => {
4522
- it("should warn when using node compatibility mode", async () => {
4523
- writeWranglerToml();
4524
- writeWorkerSource();
4525
- await runWrangler("publish index.js --node-compat --dry-run");
4526
- expect(std).toMatchInlineSnapshot(`
4527
- Object {
4528
- "debug": "",
4529
- "err": "",
4530
- "out": "--dry-run: exiting now.",
4531
- "warn": "▲ [WARNING] Enabling node.js compatibility mode for builtins and globals. This is experimental and has serious tradeoffs. Please see https://github.com/ionic-team/rollup-plugin-node-polyfills/ for more details.
4532
-
4533
- ",
4534
- }
4535
- `);
4536
- });
4537
-
4538
- it("should polyfill node builtins when enabled", async () => {
4539
- writeWranglerToml();
4540
- fs.writeFileSync(
4541
- "index.js",
4542
- `
4543
- import path from 'path';
4544
- console.log(path.join("some/path/to", "a/file.txt"));
4545
- export default {}
4546
- `
4547
- );
4548
- await runWrangler("publish index.js --node-compat --dry-run"); // this would throw if node compatibility didn't exist
4549
- expect(std).toMatchInlineSnapshot(`
4550
- Object {
4551
- "debug": "",
4552
- "err": "",
4553
- "out": "--dry-run: exiting now.",
4554
- "warn": "▲ [WARNING] Enabling node.js compatibility mode for builtins and globals. This is experimental and has serious tradeoffs. Please see https://github.com/ionic-team/rollup-plugin-node-polyfills/ for more details.
4555
-
4556
- ",
4557
- }
4558
- `);
4559
- });
4560
- });
4561
- });
4562
-
4563
- /** Write mock assets to the file system so they can be uploaded. */
4564
- function writeAssets(assets: { filePath: string; content: string }[]) {
4565
- for (const asset of assets) {
4566
- fs.mkdirSync(path.dirname(asset.filePath), { recursive: true });
4567
- fs.writeFileSync(asset.filePath, asset.content);
4568
- }
4569
- }
4570
-
4571
- /** Create a mock handler for the request to upload a worker script. */
4572
- function mockUploadWorkerRequest(
4573
- options: {
4574
- available_on_subdomain?: boolean;
4575
- expectedEntry?: string;
4576
- expectedType?: "esm" | "sw";
4577
- expectedBindings?: unknown;
4578
- expectedModules?: Record<string, string>;
4579
- expectedCompatibilityDate?: string;
4580
- expectedCompatibilityFlags?: string[];
4581
- expectedMigrations?: CfWorkerInit["migrations"];
4582
- env?: string;
4583
- legacyEnv?: boolean;
4584
- } = {}
4585
- ) {
4586
- const {
4587
- available_on_subdomain = true,
4588
- expectedEntry,
4589
- expectedType = "esm",
4590
- expectedBindings,
4591
- expectedModules = {},
4592
- expectedCompatibilityDate,
4593
- expectedCompatibilityFlags,
4594
- env = undefined,
4595
- legacyEnv = false,
4596
- expectedMigrations,
4597
- } = options;
4598
- setMockResponse(
4599
- env && !legacyEnv
4600
- ? "/accounts/:accountId/workers/services/:scriptName/environments/:envName"
4601
- : "/accounts/:accountId/workers/scripts/:scriptName",
4602
- "PUT",
4603
- async ([_url, accountId, scriptName, envName], { body }, queryParams) => {
4604
- expect(accountId).toEqual("some-account-id");
4605
- expect(scriptName).toEqual(
4606
- legacyEnv && env ? `test-name-${env}` : "test-name"
4607
- );
4608
- if (!legacyEnv) {
4609
- expect(envName).toEqual(env);
4610
- }
4611
- expect(queryParams.get("include_subdomain_availability")).toEqual("true");
4612
- const formBody = body as FormData;
4613
- if (expectedEntry !== undefined) {
4614
- expect(await (formBody.get("index.js") as File).text()).toMatch(
4615
- expectedEntry
4616
- );
4617
- }
4618
- const metadata = JSON.parse(
4619
- formBody.get("metadata") as string
4620
- ) as WorkerMetadata;
4621
- if (expectedType === "esm") {
4622
- expect(metadata.main_module).toEqual("index.js");
4623
- } else {
4624
- expect(metadata.body_part).toEqual("index.js");
4625
- }
4626
- if ("expectedBindings" in options) {
4627
- expect(metadata.bindings).toEqual(expectedBindings);
4628
- }
4629
- if ("expectedCompatibilityDate" in options) {
4630
- expect(metadata.compatibility_date).toEqual(expectedCompatibilityDate);
4631
- }
4632
- if ("expectedCompatibilityFlags" in options) {
4633
- expect(metadata.compatibility_flags).toEqual(
4634
- expectedCompatibilityFlags
4635
- );
4636
- }
4637
- if ("expectedMigrations" in options) {
4638
- expect(metadata.migrations).toEqual(expectedMigrations);
4639
- }
4640
- for (const [name, content] of Object.entries(expectedModules)) {
4641
- expect(await (formBody.get(name) as File).text()).toEqual(content);
4642
- }
4643
-
4644
- return { available_on_subdomain };
4645
- }
4646
- );
4647
- }
4648
-
4649
- /** Create a mock handler for the request to get the account's subdomain. */
4650
- function mockSubDomainRequest(
4651
- subdomain = "test-sub-domain",
4652
- registeredWorkersDev = true
4653
- ) {
4654
- if (registeredWorkersDev) {
4655
- setMockResponse("/accounts/:accountId/workers/subdomain", "GET", () => {
4656
- return { subdomain };
4657
- });
4658
- } else {
4659
- setMockRawResponse("/accounts/:accountId/workers/subdomain", "GET", () => {
4660
- return createFetchResult(null, false, [
4661
- { code: 10007, message: "haven't registered workers.dev" },
4662
- ]);
4663
- });
4664
- }
4665
- }
4666
-
4667
- /** Create a mock handler to toggle a <script>.<user>.workers.dev subdomain */
4668
- function mockUpdateWorkerRequest({
4669
- env,
4670
- enabled,
4671
- legacyEnv = false,
4672
- }: {
4673
- enabled: boolean;
4674
- env?: string | undefined;
4675
- legacyEnv?: boolean | undefined;
4676
- }) {
4677
- const requests = { count: 0 };
4678
- const servicesOrScripts = env && !legacyEnv ? "services" : "scripts";
4679
- const environment = env && !legacyEnv ? "/environments/:envName" : "";
4680
- setMockResponse(
4681
- `/accounts/:accountId/workers/${servicesOrScripts}/:scriptName${environment}/subdomain`,
4682
- "POST",
4683
- ([_url, accountId, scriptName, envName], { body }) => {
4684
- expect(accountId).toEqual("some-account-id");
4685
- expect(scriptName).toEqual(
4686
- legacyEnv && env ? `test-name-${env}` : "test-name"
4687
- );
4688
- if (!legacyEnv) {
4689
- expect(envName).toEqual(env);
4690
- }
4691
- expect(JSON.parse(body as string)).toEqual({ enabled });
4692
- return null;
4693
- }
4694
- );
4695
- return requests;
4696
- }
4697
-
4698
- function mockPublishRoutesRequest({
4699
- routes = [],
4700
- env = undefined,
4701
- legacyEnv = false,
4702
- }: {
4703
- routes: Config["routes"];
4704
- env?: string | undefined;
4705
- legacyEnv?: boolean | undefined;
4706
- }) {
4707
- const servicesOrScripts = env && !legacyEnv ? "services" : "scripts";
4708
- const environment = env && !legacyEnv ? "/environments/:envName" : "";
4709
-
4710
- setMockResponse(
4711
- `/accounts/:accountId/workers/${servicesOrScripts}/:scriptName${environment}/routes`,
4712
- "PUT",
4713
- ([_url, accountId, scriptName, envName], { body }) => {
4714
- expect(accountId).toEqual("some-account-id");
4715
- expect(scriptName).toEqual(
4716
- legacyEnv && env ? `test-name-${env}` : "test-name"
4717
- );
4718
- if (!legacyEnv) {
4719
- expect(envName).toEqual(env);
4720
- }
4721
-
4722
- expect(JSON.parse(body as string)).toEqual(
4723
- routes.map((route) =>
4724
- typeof route !== "object" ? { pattern: route } : route
4725
- )
4726
- );
4727
- return null;
4728
- }
4729
- );
4730
- }
4731
-
4732
- /** Create a mock handler for the request to get a list of all KV namespaces. */
4733
- function mockListKVNamespacesRequest(...namespaces: KVNamespaceInfo[]) {
4734
- setMockResponse(
4735
- "/accounts/:accountId/storage/kv/namespaces",
4736
- "GET",
4737
- ([_url, accountId]) => {
4738
- expect(accountId).toEqual("some-account-id");
4739
- return namespaces;
4740
- }
4741
- );
4742
- }
4743
-
4744
- /** Create a mock handler for the request that tries to do a bulk upload of assets to a KV namespace. */
4745
- function mockUploadAssetsToKVRequest(
4746
- expectedNamespaceId: string,
4747
- assets: {
4748
- filePath: string;
4749
- content: string;
4750
- expiration?: number;
4751
- expiration_ttl?: number;
4752
- }[]
4753
- ) {
4754
- setMockResponse(
4755
- "/accounts/:accountId/storage/kv/namespaces/:namespaceId/bulk",
4756
- "PUT",
4757
- ([_url, accountId, namespaceId], { body }) => {
4758
- expect(accountId).toEqual("some-account-id");
4759
- expect(namespaceId).toEqual(expectedNamespaceId);
4760
- const uploads = JSON.parse(body as string);
4761
- expect(assets.length).toEqual(uploads.length);
4762
- for (let i = 0; i < uploads.length; i++) {
4763
- const asset = assets[i];
4764
- const upload = uploads[i];
4765
- // The asset key consists of:
4766
- // - the basename of the filepath
4767
- // - some hash value
4768
- // - the extension
4769
- const keyMatcher = new RegExp(
4770
- "^" +
4771
- asset.filePath
4772
- .replace(/(\.[^.]+)$/, ".[a-z0-9]+$1")
4773
- .replace(/\./g, "\\.")
4774
- );
4775
- expect(upload.key).toMatch(keyMatcher);
4776
- // The asset value is base64 encoded.
4777
- expect(upload.base64).toBe(true);
4778
- expect(Buffer.from(upload.value, "base64").toString()).toEqual(
4779
- asset.content
4780
- );
4781
- expect(upload.expiration).toEqual(asset.expiration);
4782
- expect(upload.expiration_ttl).toEqual(asset.expiration_ttl);
4783
- }
4784
- return null;
4785
- }
4786
- );
4787
- }
4788
-
4789
- /** Create a mock handler for thr request that does a bulk delete of unused assets */
4790
- function mockDeleteUnusedAssetsRequest(
4791
- expectedNamespaceId: string,
4792
- assets: string[]
4793
- ) {
4794
- setMockResponse(
4795
- "/accounts/:accountId/storage/kv/namespaces/:namespaceId/bulk",
4796
- "DELETE",
4797
- ([_url, accountId, namespaceId], { body }) => {
4798
- expect(accountId).toEqual("some-account-id");
4799
- expect(namespaceId).toEqual(expectedNamespaceId);
4800
- const deletes = JSON.parse(body as string);
4801
- expect(assets).toEqual(deletes);
4802
- return null;
4803
- }
4804
- );
4805
- }
4806
-
4807
- type LegacyScriptInfo = { id: string; migration_tag?: string };
4808
-
4809
- function mockLegacyScriptData(options: { scripts: LegacyScriptInfo[] }) {
4810
- const { scripts } = options;
4811
- setMockResponse(
4812
- "/accounts/:accountId/workers/scripts",
4813
- "GET",
4814
- ([_url, accountId]) => {
4815
- expect(accountId).toEqual("some-account-id");
4816
- return scripts;
4817
- }
4818
- );
4819
- }
4820
-
4821
- type DurableScriptInfo = { id: string; migration_tag?: string };
4822
-
4823
- function mockServiceScriptData(options: {
4824
- script?: DurableScriptInfo;
4825
- scriptName?: string;
4826
- env?: string;
4827
- }) {
4828
- const { script } = options;
4829
- if (options.env) {
4830
- if (!script) {
4831
- setMockRawResponse(
4832
- "/accounts/:accountId/workers/services/:scriptName/environments/:envName",
4833
- "GET",
4834
- () => {
4835
- return createFetchResult(null, false, [
4836
- { code: 10092, message: "workers.api.error.environment_not_found" },
4837
- ]);
4838
- }
4839
- );
4840
- return;
4841
- }
4842
- setMockResponse(
4843
- "/accounts/:accountId/workers/services/:scriptName/environments/:envName",
4844
- "GET",
4845
- ([_url, accountId, scriptName, envName]) => {
4846
- expect(accountId).toEqual("some-account-id");
4847
- expect(scriptName).toEqual(options.scriptName || "test-name");
4848
- expect(envName).toEqual(options.env);
4849
- return { script };
4850
- }
4851
- );
4852
- } else {
4853
- if (!script) {
4854
- setMockRawResponse(
4855
- "/accounts/:accountId/workers/services/:scriptName",
4856
- "GET",
4857
- () => {
4858
- return createFetchResult(null, false, [
4859
- { code: 10090, message: "workers.api.error.service_not_found" },
4860
- ]);
4861
- }
4862
- );
4863
- return;
4864
- }
4865
- setMockResponse(
4866
- "/accounts/:accountId/workers/services/:scriptName",
4867
- "GET",
4868
- ([_url, accountId, scriptName]) => {
4869
- expect(accountId).toEqual("some-account-id");
4870
- expect(scriptName).toEqual(options.scriptName || "test-name");
4871
- return { default_environment: { script } };
4872
- }
4873
- );
4874
- }
4875
- }