wrangler 0.0.0-ece06ea → 0.0.0-ed4f2ef

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