wrangler 0.0.0-e1c615f → 0.0.0-e3cab74

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 (85) hide show
  1. package/bin/wrangler.js +2 -2
  2. package/package.json +28 -13
  3. package/pages/functions/buildWorker.ts +6 -1
  4. package/pages/functions/filepath-routing.test.ts +112 -28
  5. package/pages/functions/filepath-routing.ts +88 -131
  6. package/pages/functions/routes.ts +11 -18
  7. package/pages/functions/template-worker.ts +21 -28
  8. package/src/__tests__/configuration.test.ts +2185 -0
  9. package/src/__tests__/dev.test.tsx +56 -9
  10. package/src/__tests__/guess-worker-format.test.ts +66 -0
  11. package/src/__tests__/{clipboardy-mock.js → helpers/clipboardy-mock.js} +0 -0
  12. package/src/__tests__/helpers/cmd-shim.d.ts +11 -0
  13. package/src/__tests__/helpers/faye-websocket.d.ts +6 -0
  14. package/src/__tests__/helpers/mock-account-id.ts +30 -0
  15. package/src/__tests__/helpers/mock-bin.ts +36 -0
  16. package/src/__tests__/{mock-cfetch.ts → helpers/mock-cfetch.ts} +53 -17
  17. package/src/__tests__/helpers/mock-console.ts +62 -0
  18. package/src/__tests__/{mock-dialogs.ts → helpers/mock-dialogs.ts} +1 -1
  19. package/src/__tests__/helpers/mock-kv.ts +40 -0
  20. package/src/__tests__/helpers/mock-user.ts +27 -0
  21. package/src/__tests__/helpers/mock-web-socket.ts +29 -0
  22. package/src/__tests__/helpers/run-in-tmp.ts +29 -0
  23. package/src/__tests__/helpers/run-wrangler.ts +16 -0
  24. package/src/__tests__/helpers/write-wrangler-toml.ts +17 -0
  25. package/src/__tests__/index.test.ts +655 -76
  26. package/src/__tests__/jest.setup.ts +37 -2
  27. package/src/__tests__/kv.test.ts +441 -398
  28. package/src/__tests__/logout.test.ts +50 -0
  29. package/src/__tests__/package-manager.test.ts +206 -0
  30. package/src/__tests__/parse.test.ts +322 -0
  31. package/src/__tests__/publish.test.ts +2406 -0
  32. package/src/__tests__/r2.test.ts +198 -0
  33. package/src/__tests__/secret.test.ts +326 -0
  34. package/src/__tests__/sentry.test.ts +146 -0
  35. package/src/__tests__/tail.test.ts +564 -0
  36. package/src/__tests__/whoami.test.tsx +86 -0
  37. package/src/api/form_data.ts +112 -26
  38. package/src/api/preview.ts +34 -15
  39. package/src/api/worker.ts +58 -27
  40. package/src/bundle.ts +133 -0
  41. package/src/cfetch/index.ts +21 -19
  42. package/src/cfetch/internal.ts +85 -21
  43. package/src/cli.ts +10 -0
  44. package/src/config/README.md +107 -0
  45. package/src/config/config.ts +265 -0
  46. package/src/config/diagnostics.ts +80 -0
  47. package/src/config/environment.ts +305 -0
  48. package/src/config/index.ts +52 -0
  49. package/src/config/validation-helpers.ts +370 -0
  50. package/src/config/validation.ts +1017 -0
  51. package/src/dev.tsx +455 -368
  52. package/src/dialogs.tsx +2 -2
  53. package/src/entry.ts +57 -0
  54. package/src/environment-variables.ts +33 -0
  55. package/src/guess-worker-format.ts +68 -0
  56. package/src/index.tsx +1044 -646
  57. package/src/inspect.ts +609 -0
  58. package/src/kv.tsx +47 -39
  59. package/src/module-collection.ts +142 -36
  60. package/src/open-in-browser.ts +13 -0
  61. package/src/package-manager.ts +126 -0
  62. package/src/pages.tsx +139 -62
  63. package/src/parse.ts +183 -0
  64. package/src/paths.ts +26 -0
  65. package/src/proxy.ts +350 -77
  66. package/src/publish.ts +307 -289
  67. package/src/r2.ts +50 -0
  68. package/src/reporting.ts +115 -0
  69. package/src/sites.tsx +183 -56
  70. package/src/tail/filters.ts +279 -0
  71. package/src/tail/index.ts +299 -0
  72. package/src/tail/printing.ts +62 -0
  73. package/src/user.tsx +137 -110
  74. package/src/whoami.tsx +69 -0
  75. package/templates/new-worker.js +15 -0
  76. package/templates/new-worker.ts +15 -0
  77. package/{static-asset-facade.js → templates/static-asset-facade.js} +0 -0
  78. package/templates/tsconfig.json +105 -0
  79. package/wrangler-dist/cli.js +127861 -111486
  80. package/wrangler-dist/cli.js.map +3 -3
  81. package/src/__tests__/run-in-tmp.ts +0 -19
  82. package/src/__tests__/run-wrangler.ts +0 -32
  83. package/src/api/inspect.ts +0 -430
  84. package/src/config.ts +0 -131
  85. package/src/tail.tsx +0 -73
@@ -0,0 +1,2406 @@
1
+ import * as fs from "node:fs";
2
+ import * as path from "node:path";
3
+ import * as TOML from "@iarna/toml";
4
+ import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
5
+ import { setMockResponse, unsetAllMocks } from "./helpers/mock-cfetch";
6
+ import { mockConsoleMethods } from "./helpers/mock-console";
7
+ import { mockKeyListRequest } from "./helpers/mock-kv";
8
+ import { runInTempDir } from "./helpers/run-in-tmp";
9
+ import { runWrangler } from "./helpers/run-wrangler";
10
+ import writeWranglerToml from "./helpers/write-wrangler-toml";
11
+ import type { WorkerMetadata } from "../api/form_data";
12
+ import type { Config } from "../config";
13
+ import type { KVNamespaceInfo } from "../kv";
14
+ import type { FormData, File } from "undici";
15
+
16
+ describe("publish", () => {
17
+ beforeEach(() => {
18
+ // @ts-expect-error we're using a very simple setTimeout mock here
19
+ jest.spyOn(global, "setTimeout").mockImplementation((fn, _period) => {
20
+ fn();
21
+ });
22
+ });
23
+ mockAccountId();
24
+ mockApiToken();
25
+ runInTempDir();
26
+ const std = mockConsoleMethods();
27
+
28
+ afterEach(() => {
29
+ unsetAllMocks();
30
+ });
31
+
32
+ describe("environments", () => {
33
+ it("should use legacy environments by default", async () => {
34
+ writeWranglerToml();
35
+ writeWorkerSource();
36
+ mockSubDomainRequest();
37
+ mockUploadWorkerRequest({
38
+ env: "some-env",
39
+ legacyEnv: true,
40
+ });
41
+ await runWrangler("publish index.js --env some-env");
42
+ expect(std.out).toMatchInlineSnapshot(`
43
+ "Uploaded
44
+ test-name-some-env
45
+ (TIMINGS)
46
+ Published
47
+ test-name-some-env
48
+ (TIMINGS)
49
+
50
+ test-name-some-env.test-sub-domain.workers.dev"
51
+ `);
52
+ expect(std.err).toMatchInlineSnapshot(`""`);
53
+ expect(std.warn).toMatchInlineSnapshot(`""`);
54
+ });
55
+
56
+ describe("legacy", () => {
57
+ it("uses the script name when no environment is specified", async () => {
58
+ writeWranglerToml();
59
+ writeWorkerSource();
60
+ mockSubDomainRequest();
61
+ mockUploadWorkerRequest({
62
+ legacyEnv: true,
63
+ });
64
+ await runWrangler("publish index.js --legacy-env true");
65
+ expect(std.out).toMatchInlineSnapshot(`
66
+ "Uploaded
67
+ test-name
68
+ (TIMINGS)
69
+ Published
70
+ test-name
71
+ (TIMINGS)
72
+
73
+ test-name.test-sub-domain.workers.dev"
74
+ `);
75
+ expect(std.err).toMatchInlineSnapshot(`""`);
76
+ expect(std.warn).toMatchInlineSnapshot(`""`);
77
+ });
78
+
79
+ it("appends the environment name when provided", async () => {
80
+ writeWranglerToml();
81
+ writeWorkerSource();
82
+ mockSubDomainRequest();
83
+ mockUploadWorkerRequest({
84
+ env: "some-env",
85
+ legacyEnv: true,
86
+ });
87
+ await runWrangler("publish index.js --env some-env --legacy-env true");
88
+ expect(std.out).toMatchInlineSnapshot(`
89
+ "Uploaded
90
+ test-name-some-env
91
+ (TIMINGS)
92
+ Published
93
+ test-name-some-env
94
+ (TIMINGS)
95
+
96
+ test-name-some-env.test-sub-domain.workers.dev"
97
+ `);
98
+ expect(std.err).toMatchInlineSnapshot(`""`);
99
+ expect(std.warn).toMatchInlineSnapshot(`""`);
100
+ });
101
+ });
102
+
103
+ describe("services", () => {
104
+ it("uses the script name when no environment is specified", async () => {
105
+ writeWranglerToml();
106
+ writeWorkerSource();
107
+ mockSubDomainRequest();
108
+ mockUploadWorkerRequest({
109
+ legacyEnv: false,
110
+ });
111
+ await runWrangler("publish index.js");
112
+ expect(std.out).toMatchInlineSnapshot(`
113
+ "Uploaded
114
+ test-name
115
+ (TIMINGS)
116
+ Published
117
+ test-name
118
+ (TIMINGS)
119
+
120
+ test-name.test-sub-domain.workers.dev"
121
+ `);
122
+ expect(std.err).toMatchInlineSnapshot(`""`);
123
+ expect(std.warn).toMatchInlineSnapshot(`""`);
124
+ });
125
+
126
+ it("publishes as an environment when provided", async () => {
127
+ writeWranglerToml();
128
+ writeWorkerSource();
129
+ mockSubDomainRequest();
130
+ mockUploadWorkerRequest({
131
+ env: "some-env",
132
+ legacyEnv: false,
133
+ });
134
+ await runWrangler("publish index.js --env some-env --legacy-env false");
135
+ expect(std.out).toMatchInlineSnapshot(`
136
+ "Uploaded
137
+ test-name (some-env)
138
+ (TIMINGS)
139
+ Published
140
+ test-name (some-env)
141
+ (TIMINGS)
142
+
143
+ some-env.test-name.test-sub-domain.workers.dev"
144
+ `);
145
+ expect(std.err).toMatchInlineSnapshot(`""`);
146
+ expect(std.warn).toMatchInlineSnapshot(`""`);
147
+ });
148
+ });
149
+ });
150
+
151
+ it("should resolve wrangler.toml relative to the entrypoint", async () => {
152
+ fs.mkdirSync("./some-path/worker", { recursive: true });
153
+ fs.writeFileSync(
154
+ "./some-path/wrangler.toml",
155
+ TOML.stringify({
156
+ name: "test-name",
157
+ compatibility_date: "2022-01-12",
158
+ vars: { xyz: 123 },
159
+ }),
160
+ "utf-8"
161
+ );
162
+ writeWorkerSource({ basePath: "./some-path/worker" });
163
+ mockUploadWorkerRequest({
164
+ expectedBindings: [
165
+ {
166
+ json: 123,
167
+ name: "xyz",
168
+ type: "json",
169
+ },
170
+ ],
171
+ });
172
+ mockSubDomainRequest();
173
+ await runWrangler("publish ./some-path/worker/index.js");
174
+ expect(std.out).toMatchInlineSnapshot(`
175
+ "Uploaded
176
+ test-name
177
+ (TIMINGS)
178
+ Published
179
+ test-name
180
+ (TIMINGS)
181
+
182
+ test-name.test-sub-domain.workers.dev"
183
+ `);
184
+ expect(std.err).toMatchInlineSnapshot(`""`);
185
+ });
186
+
187
+ describe("routes", () => {
188
+ it("should publish the worker to a route", async () => {
189
+ writeWranglerToml({
190
+ routes: ["example.com/some-route/*"],
191
+ });
192
+ writeWorkerSource();
193
+ mockUpdateWorkerRequest({ enabled: false });
194
+ mockUploadWorkerRequest({ expectedType: "esm" });
195
+ mockPublishRoutesRequest({ routes: ["example.com/some-route/*"] });
196
+ await runWrangler("publish ./index");
197
+ });
198
+
199
+ it("should publish to legacy environment specific routes", async () => {
200
+ writeWranglerToml({
201
+ routes: ["example.com/some-route/*"],
202
+ env: {
203
+ dev: {
204
+ routes: ["dev-example.com/some-route/*"],
205
+ },
206
+ },
207
+ });
208
+ writeWorkerSource();
209
+ mockUpdateWorkerRequest({ enabled: false, legacyEnv: true, env: "dev" });
210
+ mockUploadWorkerRequest({
211
+ expectedType: "esm",
212
+ legacyEnv: true,
213
+ env: "dev",
214
+ });
215
+ mockPublishRoutesRequest({
216
+ routes: ["dev-example.com/some-route/*"],
217
+ legacyEnv: true,
218
+ env: "dev",
219
+ });
220
+ await runWrangler("publish ./index --env dev --legacy-env true");
221
+ });
222
+
223
+ it("services: should publish to service environment specific routes", async () => {
224
+ writeWranglerToml({
225
+ routes: ["example.com/some-route/*"],
226
+ env: {
227
+ dev: {
228
+ routes: ["dev-example.com/some-route/*"],
229
+ },
230
+ },
231
+ });
232
+ writeWorkerSource();
233
+ mockUpdateWorkerRequest({ enabled: false, env: "dev" });
234
+ mockUploadWorkerRequest({
235
+ expectedType: "esm",
236
+ env: "dev",
237
+ });
238
+ mockPublishRoutesRequest({
239
+ routes: ["dev-example.com/some-route/*"],
240
+ env: "dev",
241
+ });
242
+ await runWrangler("publish ./index --env dev --legacy-env false");
243
+ });
244
+
245
+ it.todo("should error if it's a workers.dev route");
246
+ });
247
+
248
+ describe("entry-points", () => {
249
+ it("should be able to use `index` with no extension as the entry-point (esm)", async () => {
250
+ writeWranglerToml();
251
+ writeWorkerSource();
252
+ mockUploadWorkerRequest({ expectedType: "esm" });
253
+ mockSubDomainRequest();
254
+
255
+ await runWrangler("publish ./index");
256
+
257
+ expect(std.out).toMatchInlineSnapshot(`
258
+ "Uploaded
259
+ test-name
260
+ (TIMINGS)
261
+ Published
262
+ test-name
263
+ (TIMINGS)
264
+
265
+ test-name.test-sub-domain.workers.dev"
266
+ `);
267
+ expect(std.err).toMatchInlineSnapshot(`""`);
268
+ });
269
+
270
+ it("should be able to use `index` with no extension as the entry-point (sw)", async () => {
271
+ writeWranglerToml();
272
+ writeWorkerSource({ type: "sw" });
273
+ mockUploadWorkerRequest({ expectedType: "sw" });
274
+ mockSubDomainRequest();
275
+
276
+ await runWrangler("publish ./index");
277
+
278
+ expect(std.out).toMatchInlineSnapshot(`
279
+ "Uploaded
280
+ test-name
281
+ (TIMINGS)
282
+ Published
283
+ test-name
284
+ (TIMINGS)
285
+
286
+ test-name.test-sub-domain.workers.dev"
287
+ `);
288
+ expect(std.err).toMatchInlineSnapshot(`""`);
289
+ });
290
+
291
+ it("should be able to use the `main` config as the entry-point for ESM sources", async () => {
292
+ writeWranglerToml({ main: "./index.js" });
293
+ writeWorkerSource();
294
+ mockUploadWorkerRequest();
295
+ mockSubDomainRequest();
296
+
297
+ await runWrangler("publish");
298
+
299
+ expect(std.out).toMatchInlineSnapshot(`
300
+ "Uploaded
301
+ test-name
302
+ (TIMINGS)
303
+ Published
304
+ test-name
305
+ (TIMINGS)
306
+
307
+ test-name.test-sub-domain.workers.dev"
308
+ `);
309
+ expect(std.err).toMatchInlineSnapshot(`""`);
310
+ });
311
+
312
+ it("should use `main` relative to the wrangler.toml not cwd", async () => {
313
+ writeWranglerToml({
314
+ main: "./foo/index.js",
315
+ });
316
+ writeWorkerSource({ basePath: "foo" });
317
+ mockUploadWorkerRequest({ expectedEntry: "var foo = 100;" });
318
+ mockSubDomainRequest();
319
+ process.chdir("foo");
320
+ await runWrangler("publish");
321
+
322
+ expect(std.out).toMatchInlineSnapshot(`
323
+ "Uploaded
324
+ test-name
325
+ (TIMINGS)
326
+ Published
327
+ test-name
328
+ (TIMINGS)
329
+
330
+ test-name.test-sub-domain.workers.dev"
331
+ `);
332
+ expect(std.err).toMatchInlineSnapshot(`""`);
333
+ });
334
+
335
+ it('should use `build.upload.main` as an entry point, where `build.upload.dir` defaults to "./dist", and log a deprecation warning', async () => {
336
+ writeWranglerToml({ build: { upload: { main: "./index.js" } } });
337
+ writeWorkerSource({ basePath: "./dist" });
338
+ mockUploadWorkerRequest();
339
+ mockSubDomainRequest();
340
+
341
+ await runWrangler("publish");
342
+
343
+ expect(std.out).toMatchInlineSnapshot(`
344
+ "Uploaded
345
+ test-name
346
+ (TIMINGS)
347
+ Published
348
+ test-name
349
+ (TIMINGS)
350
+
351
+ test-name.test-sub-domain.workers.dev"
352
+ `);
353
+ expect(std.err).toMatchInlineSnapshot(`""`);
354
+ expect(std.warn).toMatchInlineSnapshot(`
355
+ "Processing wrangler.toml configuration:
356
+ - DEPRECATION: \\"build.upload.main\\":
357
+ Delete the \`build.upload.main\` and \`build.upload.dir\` fields.
358
+ Then add the top level \`main\` field to your configuration file:
359
+ \`\`\`
360
+ main = \\"dist/index.js\\"
361
+ \`\`\`"
362
+ `);
363
+ });
364
+
365
+ it("should use `build.upload.main` relative to `build.upload.dir`", async () => {
366
+ writeWranglerToml({
367
+ build: {
368
+ upload: {
369
+ main: "./index.js",
370
+ dir: "./foo",
371
+ },
372
+ },
373
+ });
374
+ writeWorkerSource({ basePath: "./foo" });
375
+ mockUploadWorkerRequest({ expectedEntry: "var foo = 100;" });
376
+ mockSubDomainRequest();
377
+ process.chdir("foo");
378
+ await runWrangler("publish");
379
+
380
+ expect(std.out).toMatchInlineSnapshot(`
381
+ "Uploaded
382
+ test-name
383
+ (TIMINGS)
384
+ Published
385
+ test-name
386
+ (TIMINGS)
387
+
388
+ test-name.test-sub-domain.workers.dev"
389
+ `);
390
+ expect(std.err).toMatchInlineSnapshot(`""`);
391
+ expect(std.warn).toMatchInlineSnapshot(`
392
+ "Processing ../wrangler.toml configuration:
393
+ - DEPRECATION: \\"build.upload.main\\":
394
+ Delete the \`build.upload.main\` and \`build.upload.dir\` fields.
395
+ Then add the top level \`main\` field to your configuration file:
396
+ \`\`\`
397
+ main = \\"foo/index.js\\"
398
+ \`\`\`
399
+ - DEPRECATION: \\"build.upload.dir\\":
400
+ Use the top level \\"main\\" field or a command-line argument to specify the entry-point for the Worker."
401
+ `);
402
+ });
403
+
404
+ it("should error when both `main` and `build.upload.main` are used", async () => {
405
+ writeWranglerToml({
406
+ main: "./index.js",
407
+ build: {
408
+ upload: {
409
+ main: "./index.js",
410
+ dir: "./foo",
411
+ },
412
+ },
413
+ });
414
+ await expect(runWrangler("publish")).rejects
415
+ .toThrowErrorMatchingInlineSnapshot(`
416
+ "Processing wrangler.toml configuration:
417
+ - Don't define both the \`main\` and \`build.upload.main\` fields in your configuration.
418
+ They serve the same purpose: to point to the entry-point of your worker.
419
+ Delete the \`build.upload.main\` and \`build.upload.dir\` field from your config."
420
+ `);
421
+ });
422
+
423
+ it("should be able to transpile TypeScript (esm)", async () => {
424
+ writeWranglerToml();
425
+ writeWorkerSource({ format: "ts" });
426
+ mockUploadWorkerRequest({ expectedEntry: "var foo = 100;" });
427
+ mockSubDomainRequest();
428
+ await runWrangler("publish index.ts");
429
+
430
+ expect(std.out).toMatchInlineSnapshot(`
431
+ "Uploaded
432
+ test-name
433
+ (TIMINGS)
434
+ Published
435
+ test-name
436
+ (TIMINGS)
437
+
438
+ test-name.test-sub-domain.workers.dev"
439
+ `);
440
+ expect(std.err).toMatchInlineSnapshot(`""`);
441
+ });
442
+
443
+ it("should be able to transpile TypeScript (sw)", async () => {
444
+ writeWranglerToml();
445
+ writeWorkerSource({ format: "ts", type: "sw" });
446
+ mockUploadWorkerRequest({
447
+ expectedEntry: "var foo = 100;",
448
+ expectedType: "sw",
449
+ });
450
+ mockSubDomainRequest();
451
+ await runWrangler("publish index.ts");
452
+
453
+ expect(std.out).toMatchInlineSnapshot(`
454
+ "Uploaded
455
+ test-name
456
+ (TIMINGS)
457
+ Published
458
+ test-name
459
+ (TIMINGS)
460
+
461
+ test-name.test-sub-domain.workers.dev"
462
+ `);
463
+ expect(std.err).toMatchInlineSnapshot(`""`);
464
+ });
465
+
466
+ it("should add referenced text modules into the form upload", async () => {
467
+ writeWranglerToml();
468
+ fs.writeFileSync(
469
+ "./index.js",
470
+ `
471
+ import txt from './textfile.txt';
472
+ export default{
473
+ fetch(){
474
+ return new Response(txt);
475
+ }
476
+ }
477
+ `
478
+ );
479
+ fs.writeFileSync("./textfile.txt", "Hello, World!");
480
+ mockUploadWorkerRequest({
481
+ expectedModules: {
482
+ "./0a0a9f2a6772942557ab5355d76af442f8f65e01-textfile.txt":
483
+ "Hello, World!",
484
+ },
485
+ });
486
+ mockSubDomainRequest();
487
+ await runWrangler("publish index.js");
488
+ expect(std.out).toMatchInlineSnapshot(`
489
+ "Uploaded
490
+ test-name
491
+ (TIMINGS)
492
+ Published
493
+ test-name
494
+ (TIMINGS)
495
+
496
+ test-name.test-sub-domain.workers.dev"
497
+ `);
498
+ expect(std.err).toMatchInlineSnapshot(`""`);
499
+ });
500
+
501
+ it("should be able to transpile entry-points in sub-directories (esm)", async () => {
502
+ writeWranglerToml();
503
+ writeWorkerSource({ basePath: "./src" });
504
+ mockUploadWorkerRequest({ expectedEntry: "var foo = 100;" });
505
+ mockSubDomainRequest();
506
+
507
+ await runWrangler("publish ./src/index.js");
508
+
509
+ expect(std.out).toMatchInlineSnapshot(`
510
+ "Uploaded
511
+ test-name
512
+ (TIMINGS)
513
+ Published
514
+ test-name
515
+ (TIMINGS)
516
+
517
+ test-name.test-sub-domain.workers.dev"
518
+ `);
519
+ expect(std.err).toMatchInlineSnapshot(`""`);
520
+ });
521
+
522
+ it("should be able to transpile entry-points in sub-directories (sw)", async () => {
523
+ writeWranglerToml();
524
+ writeWorkerSource({ basePath: "./src", type: "sw" });
525
+ mockUploadWorkerRequest({
526
+ expectedEntry: "var foo = 100;",
527
+ expectedType: "sw",
528
+ });
529
+ mockSubDomainRequest();
530
+
531
+ await runWrangler("publish ./src/index.js");
532
+
533
+ expect(std.out).toMatchInlineSnapshot(`
534
+ "Uploaded
535
+ test-name
536
+ (TIMINGS)
537
+ Published
538
+ test-name
539
+ (TIMINGS)
540
+
541
+ test-name.test-sub-domain.workers.dev"
542
+ `);
543
+ expect(std.err).toMatchInlineSnapshot(`""`);
544
+ });
545
+
546
+ it('should error if a site definition doesn\'t have a "bucket" field', async () => {
547
+ writeWranglerToml({
548
+ site: {
549
+ "entry-point": "./index.js",
550
+ } as Config["site"],
551
+ });
552
+ writeWorkerSource();
553
+ mockUploadWorkerRequest();
554
+ mockSubDomainRequest();
555
+
556
+ await expect(runWrangler("publish ./index.js")).rejects
557
+ .toThrowErrorMatchingInlineSnapshot(`
558
+ "Processing wrangler.toml configuration:
559
+ - ERROR: \\"site.bucket\\" is a required field."
560
+ `);
561
+
562
+ expect(std.out).toMatchInlineSnapshot(`""`);
563
+ expect(std.err).toMatchInlineSnapshot(`
564
+ "Processing wrangler.toml configuration:
565
+ - ERROR: \\"site.bucket\\" is a required field.
566
+
567
+ %s
568
+ If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
569
+ `);
570
+ expect(std.warn).toMatchInlineSnapshot(`""`);
571
+ });
572
+
573
+ it("should warn if there is a `site.entry-point` configuration", async () => {
574
+ const assets = [
575
+ { filePath: "assets/file-1.txt", content: "Content of file-1" },
576
+ { filePath: "assets/file-2.txt", content: "Content of file-2" },
577
+ ];
578
+ const kvNamespace = {
579
+ title: "__test-name-workers_sites_assets",
580
+ id: "__test-name-workers_sites_assets-id",
581
+ };
582
+
583
+ writeWranglerToml({
584
+ site: {
585
+ "entry-point": "./index.js",
586
+ bucket: "assets",
587
+ },
588
+ });
589
+ writeWorkerSource();
590
+ writeAssets(assets);
591
+ mockUploadWorkerRequest();
592
+ mockSubDomainRequest();
593
+ mockListKVNamespacesRequest(kvNamespace);
594
+ mockKeyListRequest(kvNamespace.id, []);
595
+ mockUploadAssetsToKVRequest(kvNamespace.id, assets);
596
+
597
+ await runWrangler("publish ./index.js");
598
+
599
+ expect(std.out).toMatchInlineSnapshot(`
600
+ "reading assets/file-1.txt...
601
+ uploading as assets/file-1.2ca234f380.txt...
602
+ reading assets/file-2.txt...
603
+ uploading as assets/file-2.5938485188.txt...
604
+ Uploaded
605
+ test-name
606
+ (TIMINGS)
607
+ Published
608
+ test-name
609
+ (TIMINGS)
610
+
611
+ test-name.test-sub-domain.workers.dev"
612
+ `);
613
+ expect(std.err).toMatchInlineSnapshot(`""`);
614
+ expect(std.warn).toMatchInlineSnapshot(`
615
+ "Deprecation notice: The \`site.entry-point\` config field is no longer used.
616
+ The entry-point should be specified via the command line (e.g. \`wrangler publish path/to/script\`) or the \`main\` config field.
617
+ Please remove the \`site.entry-point\` field from the \`wrangler.toml\` file."
618
+ `);
619
+ });
620
+
621
+ it("should error if there is no entry-point specified", async () => {
622
+ writeWranglerToml();
623
+ writeWorkerSource();
624
+ mockUploadWorkerRequest();
625
+ mockSubDomainRequest();
626
+ await expect(
627
+ runWrangler("publish")
628
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
629
+ `"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."`
630
+ );
631
+
632
+ expect(std.out).toMatchInlineSnapshot(`""`);
633
+ expect(std.err).toMatchInlineSnapshot(`
634
+ "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.
635
+
636
+ %s
637
+ If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
638
+ `);
639
+ });
640
+ });
641
+
642
+ describe("asset upload", () => {
643
+ it("should upload all the files in the directory specified by `config.site.bucket`", async () => {
644
+ const assets = [
645
+ { filePath: "assets/file-1.txt", content: "Content of file-1" },
646
+ { filePath: "assets/file-2.txt", content: "Content of file-2" },
647
+ ];
648
+ const kvNamespace = {
649
+ title: "__test-name-workers_sites_assets",
650
+ id: "__test-name-workers_sites_assets-id",
651
+ };
652
+ writeWranglerToml({
653
+ main: "./index.js",
654
+ site: {
655
+ bucket: "assets",
656
+ },
657
+ });
658
+ writeWorkerSource();
659
+ writeAssets(assets);
660
+ mockUploadWorkerRequest();
661
+ mockSubDomainRequest();
662
+ mockListKVNamespacesRequest(kvNamespace);
663
+ mockKeyListRequest(kvNamespace.id, []);
664
+ mockUploadAssetsToKVRequest(kvNamespace.id, assets);
665
+ await runWrangler("publish");
666
+
667
+ expect(std.out).toMatchInlineSnapshot(`
668
+ "reading assets/file-1.txt...
669
+ uploading as assets/file-1.2ca234f380.txt...
670
+ reading assets/file-2.txt...
671
+ uploading as assets/file-2.5938485188.txt...
672
+ Uploaded
673
+ test-name
674
+ (TIMINGS)
675
+ Published
676
+ test-name
677
+ (TIMINGS)
678
+
679
+ test-name.test-sub-domain.workers.dev"
680
+ `);
681
+ expect(std.err).toMatchInlineSnapshot(`""`);
682
+ });
683
+
684
+ it("when using a service worker type, it should add an asset manifest as a text_blob, and bind to a namespace", async () => {
685
+ const assets = [
686
+ { filePath: "assets/file-1.txt", content: "Content of file-1" },
687
+ { filePath: "assets/file-2.txt", content: "Content of file-2" },
688
+ ];
689
+ const kvNamespace = {
690
+ title: "__test-name-workers_sites_assets",
691
+ id: "__test-name-workers_sites_assets-id",
692
+ };
693
+ writeWranglerToml({
694
+ main: "./index.js",
695
+ site: {
696
+ bucket: "assets",
697
+ },
698
+ });
699
+ writeWorkerSource({ type: "sw" });
700
+ writeAssets(assets);
701
+ mockUploadWorkerRequest({
702
+ expectedType: "sw",
703
+ expectedModules: {
704
+ __STATIC_CONTENT_MANIFEST:
705
+ '{"file-1.txt":"assets/file-1.2ca234f380.txt","file-2.txt":"assets/file-2.5938485188.txt"}',
706
+ },
707
+ expectedBindings: [
708
+ {
709
+ name: "__STATIC_CONTENT",
710
+ namespace_id: "__test-name-workers_sites_assets-id",
711
+ type: "kv_namespace",
712
+ },
713
+ {
714
+ name: "__STATIC_CONTENT_MANIFEST",
715
+ part: "__STATIC_CONTENT_MANIFEST",
716
+ type: "text_blob",
717
+ },
718
+ ],
719
+ });
720
+ mockSubDomainRequest();
721
+ mockListKVNamespacesRequest(kvNamespace);
722
+ mockKeyListRequest(kvNamespace.id, []);
723
+ mockUploadAssetsToKVRequest(kvNamespace.id, assets);
724
+
725
+ await runWrangler("publish");
726
+
727
+ expect(std.out).toMatchInlineSnapshot(`
728
+ "reading assets/file-1.txt...
729
+ uploading as assets/file-1.2ca234f380.txt...
730
+ reading assets/file-2.txt...
731
+ uploading as assets/file-2.5938485188.txt...
732
+ Uploaded
733
+ test-name
734
+ (TIMINGS)
735
+ Published
736
+ test-name
737
+ (TIMINGS)
738
+
739
+ test-name.test-sub-domain.workers.dev"
740
+ `);
741
+ expect(std.err).toMatchInlineSnapshot(`""`);
742
+ });
743
+
744
+ it("when using a module worker type, it should add an asset manifest module, and bind to a namespace", async () => {
745
+ const assets = [
746
+ { filePath: "assets/file-1.txt", content: "Content of file-1" },
747
+ { filePath: "assets/file-2.txt", content: "Content of file-2" },
748
+ ];
749
+ const kvNamespace = {
750
+ title: "__test-name-workers_sites_assets",
751
+ id: "__test-name-workers_sites_assets-id",
752
+ };
753
+ writeWranglerToml({
754
+ main: "./index.js",
755
+ site: {
756
+ bucket: "assets",
757
+ },
758
+ });
759
+ writeWorkerSource({ type: "esm" });
760
+ writeAssets(assets);
761
+ mockUploadWorkerRequest({
762
+ expectedBindings: [
763
+ {
764
+ name: "__STATIC_CONTENT",
765
+ namespace_id: "__test-name-workers_sites_assets-id",
766
+ type: "kv_namespace",
767
+ },
768
+ ],
769
+ expectedModules: {
770
+ __STATIC_CONTENT_MANIFEST:
771
+ '{"file-1.txt":"assets/file-1.2ca234f380.txt","file-2.txt":"assets/file-2.5938485188.txt"}',
772
+ },
773
+ });
774
+ mockSubDomainRequest();
775
+ mockListKVNamespacesRequest(kvNamespace);
776
+ mockKeyListRequest(kvNamespace.id, []);
777
+ mockUploadAssetsToKVRequest(kvNamespace.id, assets);
778
+
779
+ await runWrangler("publish");
780
+
781
+ expect(std.out).toMatchInlineSnapshot(`
782
+ "reading assets/file-1.txt...
783
+ uploading as assets/file-1.2ca234f380.txt...
784
+ reading assets/file-2.txt...
785
+ uploading as assets/file-2.5938485188.txt...
786
+ Uploaded
787
+ test-name
788
+ (TIMINGS)
789
+ Published
790
+ test-name
791
+ (TIMINGS)
792
+
793
+ test-name.test-sub-domain.workers.dev"
794
+ `);
795
+ expect(std.err).toMatchInlineSnapshot(`""`);
796
+ });
797
+
798
+ it("should make environment specific kv namespace for assets, even for service envs", async () => {
799
+ // This is the same test as the one before this, but with an env arg
800
+ const assets = [
801
+ { filePath: "assets/file-1.txt", content: "Content of file-1" },
802
+ { filePath: "assets/file-2.txt", content: "Content of file-2" },
803
+ ];
804
+ const kvNamespace = {
805
+ title: "__test-name-some-env-workers_sites_assets",
806
+ id: "__test-name-some-env-workers_sites_assets-id",
807
+ };
808
+ writeWranglerToml({
809
+ main: "./index.js",
810
+ site: {
811
+ bucket: "assets",
812
+ },
813
+ });
814
+ writeWorkerSource();
815
+ writeAssets(assets);
816
+ mockUploadWorkerRequest({
817
+ env: "some-env",
818
+ expectedBindings: [
819
+ {
820
+ name: "__STATIC_CONTENT",
821
+ namespace_id: "__test-name-some-env-workers_sites_assets-id",
822
+ type: "kv_namespace",
823
+ },
824
+ ],
825
+ });
826
+ mockSubDomainRequest();
827
+ mockListKVNamespacesRequest(kvNamespace);
828
+ mockKeyListRequest(kvNamespace.id, []);
829
+ mockUploadAssetsToKVRequest(kvNamespace.id, assets);
830
+ await runWrangler("publish --env some-env --legacy-env false");
831
+
832
+ expect(std.out).toMatchInlineSnapshot(`
833
+ "reading assets/file-1.txt...
834
+ uploading as assets/file-1.2ca234f380.txt...
835
+ reading assets/file-2.txt...
836
+ uploading as assets/file-2.5938485188.txt...
837
+ Uploaded
838
+ test-name (some-env)
839
+ (TIMINGS)
840
+ Published
841
+ test-name (some-env)
842
+ (TIMINGS)
843
+
844
+ some-env.test-name.test-sub-domain.workers.dev"
845
+ `);
846
+ expect(std.err).toMatchInlineSnapshot(`""`);
847
+ });
848
+
849
+ it("should make environment specific kv namespace for assets, even for legacy envs", async () => {
850
+ // And this is the same test as the one before this, but with legacyEnv:true
851
+ const assets = [
852
+ { filePath: "assets/file-1.txt", content: "Content of file-1" },
853
+ { filePath: "assets/file-2.txt", content: "Content of file-2" },
854
+ ];
855
+ const kvNamespace = {
856
+ title: "__test-name-some-env-workers_sites_assets",
857
+ id: "__test-name-some-env-workers_sites_assets-id",
858
+ };
859
+ writeWranglerToml({
860
+ main: "./index.js",
861
+ site: {
862
+ bucket: "assets",
863
+ },
864
+ });
865
+ writeWorkerSource();
866
+ writeAssets(assets);
867
+ mockUploadWorkerRequest({
868
+ legacyEnv: true,
869
+ env: "some-env",
870
+ expectedBindings: [
871
+ {
872
+ name: "__STATIC_CONTENT",
873
+ namespace_id: "__test-name-some-env-workers_sites_assets-id",
874
+ type: "kv_namespace",
875
+ },
876
+ ],
877
+ });
878
+ mockSubDomainRequest();
879
+ mockListKVNamespacesRequest(kvNamespace);
880
+ mockKeyListRequest(kvNamespace.id, []);
881
+ mockUploadAssetsToKVRequest(kvNamespace.id, assets);
882
+ await runWrangler("publish --env some-env --legacy-env true");
883
+
884
+ expect(std.out).toMatchInlineSnapshot(`
885
+ "reading assets/file-1.txt...
886
+ uploading as assets/file-1.2ca234f380.txt...
887
+ reading assets/file-2.txt...
888
+ uploading as assets/file-2.5938485188.txt...
889
+ Uploaded
890
+ test-name-some-env
891
+ (TIMINGS)
892
+ Published
893
+ test-name-some-env
894
+ (TIMINGS)
895
+
896
+ test-name-some-env.test-sub-domain.workers.dev"
897
+ `);
898
+ expect(std.err).toMatchInlineSnapshot(`""`);
899
+ });
900
+
901
+ it("should only upload files that are not already in the KV namespace", async () => {
902
+ const assets = [
903
+ { filePath: "assets/file-1.txt", content: "Content of file-1" },
904
+ { filePath: "assets/file-2.txt", content: "Content of file-2" },
905
+ ];
906
+ const kvNamespace = {
907
+ title: "__test-name-workers_sites_assets",
908
+ id: "__test-name-workers_sites_assets-id",
909
+ };
910
+ writeWranglerToml({
911
+ main: "./index.js",
912
+ site: {
913
+ bucket: "assets",
914
+ },
915
+ });
916
+ writeWorkerSource();
917
+ writeAssets(assets);
918
+ mockUploadWorkerRequest();
919
+ mockSubDomainRequest();
920
+ mockListKVNamespacesRequest(kvNamespace);
921
+ // Put file-1 in the KV namespace
922
+ mockKeyListRequest(kvNamespace.id, ["assets/file-1.2ca234f380.txt"]);
923
+ // Check we do not upload file-1
924
+ mockUploadAssetsToKVRequest(
925
+ kvNamespace.id,
926
+ assets.filter((a) => a.filePath !== "assets/file-1.txt")
927
+ );
928
+ await runWrangler("publish");
929
+
930
+ expect(std.out).toMatchInlineSnapshot(`
931
+ "reading assets/file-1.txt...
932
+ skipping - already uploaded
933
+ reading assets/file-2.txt...
934
+ uploading as assets/file-2.5938485188.txt...
935
+ Uploaded
936
+ test-name
937
+ (TIMINGS)
938
+ Published
939
+ test-name
940
+ (TIMINGS)
941
+
942
+ test-name.test-sub-domain.workers.dev"
943
+ `);
944
+ expect(std.err).toMatchInlineSnapshot(`""`);
945
+ });
946
+
947
+ it("should only upload files that match the `site-include` arg", async () => {
948
+ const assets = [
949
+ { filePath: "assets/file-1.txt", content: "Content of file-1" },
950
+ { filePath: "assets/file-2.txt", content: "Content of file-2" },
951
+ ];
952
+ const kvNamespace = {
953
+ title: "__test-name-workers_sites_assets",
954
+ id: "__test-name-workers_sites_assets-id",
955
+ };
956
+ writeWranglerToml({
957
+ main: "./index.js",
958
+ site: {
959
+ bucket: "assets",
960
+ },
961
+ });
962
+ writeWorkerSource();
963
+ writeAssets(assets);
964
+ mockUploadWorkerRequest();
965
+ mockSubDomainRequest();
966
+ mockListKVNamespacesRequest(kvNamespace);
967
+ mockKeyListRequest(kvNamespace.id, []);
968
+ // Check we only upload file-1
969
+ mockUploadAssetsToKVRequest(
970
+ kvNamespace.id,
971
+ assets.filter((a) => a.filePath === "assets/file-1.txt")
972
+ );
973
+ await runWrangler("publish --site-include file-1.txt");
974
+
975
+ expect(std.out).toMatchInlineSnapshot(`
976
+ "reading assets/file-1.txt...
977
+ uploading as assets/file-1.2ca234f380.txt...
978
+ Uploaded
979
+ test-name
980
+ (TIMINGS)
981
+ Published
982
+ test-name
983
+ (TIMINGS)
984
+
985
+ test-name.test-sub-domain.workers.dev"
986
+ `);
987
+ expect(std.err).toMatchInlineSnapshot(`""`);
988
+ });
989
+
990
+ it("should not upload files that match the `site-exclude` arg", async () => {
991
+ const assets = [
992
+ { filePath: "assets/file-1.txt", content: "Content of file-1" },
993
+ { filePath: "assets/file-2.txt", content: "Content of file-2" },
994
+ ];
995
+ const kvNamespace = {
996
+ title: "__test-name-workers_sites_assets",
997
+ id: "__test-name-workers_sites_assets-id",
998
+ };
999
+ writeWranglerToml({
1000
+ main: "./index.js",
1001
+ site: {
1002
+ bucket: "assets",
1003
+ },
1004
+ });
1005
+ writeWorkerSource();
1006
+ writeAssets(assets);
1007
+ mockUploadWorkerRequest();
1008
+ mockSubDomainRequest();
1009
+ mockListKVNamespacesRequest(kvNamespace);
1010
+ mockKeyListRequest(kvNamespace.id, []);
1011
+ // Check we only upload file-1
1012
+ mockUploadAssetsToKVRequest(
1013
+ kvNamespace.id,
1014
+ assets.filter((a) => a.filePath === "assets/file-1.txt")
1015
+ );
1016
+ await runWrangler("publish --site-exclude file-2.txt");
1017
+
1018
+ expect(std.out).toMatchInlineSnapshot(`
1019
+ "reading assets/file-1.txt...
1020
+ uploading as assets/file-1.2ca234f380.txt...
1021
+ Uploaded
1022
+ test-name
1023
+ (TIMINGS)
1024
+ Published
1025
+ test-name
1026
+ (TIMINGS)
1027
+
1028
+ test-name.test-sub-domain.workers.dev"
1029
+ `);
1030
+ expect(std.err).toMatchInlineSnapshot(`""`);
1031
+ });
1032
+
1033
+ it("should only upload files that match the `site.include` config", async () => {
1034
+ const assets = [
1035
+ { filePath: "assets/file-1.txt", content: "Content of file-1" },
1036
+ { filePath: "assets/file-2.txt", content: "Content of file-2" },
1037
+ ];
1038
+ const kvNamespace = {
1039
+ title: "__test-name-workers_sites_assets",
1040
+ id: "__test-name-workers_sites_assets-id",
1041
+ };
1042
+ writeWranglerToml({
1043
+ main: "./index.js",
1044
+ site: {
1045
+ bucket: "assets",
1046
+ include: ["file-1.txt"],
1047
+ },
1048
+ });
1049
+ writeWorkerSource();
1050
+ writeAssets(assets);
1051
+ mockUploadWorkerRequest();
1052
+ mockSubDomainRequest();
1053
+ mockListKVNamespacesRequest(kvNamespace);
1054
+ mockKeyListRequest(kvNamespace.id, []);
1055
+ // Check we only upload file-1
1056
+ mockUploadAssetsToKVRequest(
1057
+ kvNamespace.id,
1058
+ assets.filter((a) => a.filePath === "assets/file-1.txt")
1059
+ );
1060
+ await runWrangler("publish");
1061
+
1062
+ expect(std.out).toMatchInlineSnapshot(`
1063
+ "reading assets/file-1.txt...
1064
+ uploading as assets/file-1.2ca234f380.txt...
1065
+ Uploaded
1066
+ test-name
1067
+ (TIMINGS)
1068
+ Published
1069
+ test-name
1070
+ (TIMINGS)
1071
+
1072
+ test-name.test-sub-domain.workers.dev"
1073
+ `);
1074
+ expect(std.err).toMatchInlineSnapshot(`""`);
1075
+ });
1076
+
1077
+ it("should not upload files that match the `site.exclude` config", async () => {
1078
+ const assets = [
1079
+ { filePath: "assets/file-1.txt", content: "Content of file-1" },
1080
+ { filePath: "assets/file-2.txt", content: "Content of file-2" },
1081
+ ];
1082
+ const kvNamespace = {
1083
+ title: "__test-name-workers_sites_assets",
1084
+ id: "__test-name-workers_sites_assets-id",
1085
+ };
1086
+ writeWranglerToml({
1087
+ main: "./index.js",
1088
+ site: {
1089
+ bucket: "assets",
1090
+ exclude: ["file-2.txt"],
1091
+ },
1092
+ });
1093
+ writeWorkerSource();
1094
+ writeAssets(assets);
1095
+ mockUploadWorkerRequest();
1096
+ mockSubDomainRequest();
1097
+ mockListKVNamespacesRequest(kvNamespace);
1098
+ mockKeyListRequest(kvNamespace.id, []);
1099
+ // Check we only upload file-1
1100
+ mockUploadAssetsToKVRequest(
1101
+ kvNamespace.id,
1102
+ assets.filter((a) => a.filePath === "assets/file-1.txt")
1103
+ );
1104
+ await runWrangler("publish");
1105
+
1106
+ expect(std.out).toMatchInlineSnapshot(`
1107
+ "reading assets/file-1.txt...
1108
+ uploading as assets/file-1.2ca234f380.txt...
1109
+ Uploaded
1110
+ test-name
1111
+ (TIMINGS)
1112
+ Published
1113
+ test-name
1114
+ (TIMINGS)
1115
+
1116
+ test-name.test-sub-domain.workers.dev"
1117
+ `);
1118
+ expect(std.err).toMatchInlineSnapshot(`""`);
1119
+ });
1120
+
1121
+ it("should use `site-include` arg over `site.include` config", async () => {
1122
+ const assets = [
1123
+ { filePath: "assets/file-1.txt", content: "Content of file-1" },
1124
+ { filePath: "assets/file-2.txt", content: "Content of file-2" },
1125
+ ];
1126
+ const kvNamespace = {
1127
+ title: "__test-name-workers_sites_assets",
1128
+ id: "__test-name-workers_sites_assets-id",
1129
+ };
1130
+ writeWranglerToml({
1131
+ main: "./index.js",
1132
+ site: {
1133
+ bucket: "assets",
1134
+ include: ["file-2.txt"],
1135
+ },
1136
+ });
1137
+ writeWorkerSource();
1138
+ writeAssets(assets);
1139
+ mockUploadWorkerRequest();
1140
+ mockSubDomainRequest();
1141
+ mockListKVNamespacesRequest(kvNamespace);
1142
+ mockKeyListRequest(kvNamespace.id, []);
1143
+ // Check we only upload file-1
1144
+ mockUploadAssetsToKVRequest(
1145
+ kvNamespace.id,
1146
+ assets.filter((a) => a.filePath === "assets/file-1.txt")
1147
+ );
1148
+ await runWrangler("publish --site-include file-1.txt");
1149
+
1150
+ expect(std.out).toMatchInlineSnapshot(`
1151
+ "reading assets/file-1.txt...
1152
+ uploading as assets/file-1.2ca234f380.txt...
1153
+ Uploaded
1154
+ test-name
1155
+ (TIMINGS)
1156
+ Published
1157
+ test-name
1158
+ (TIMINGS)
1159
+
1160
+ test-name.test-sub-domain.workers.dev"
1161
+ `);
1162
+ expect(std.err).toMatchInlineSnapshot(`""`);
1163
+ });
1164
+
1165
+ it("should use `site-exclude` arg over `site.exclude` config", async () => {
1166
+ const assets = [
1167
+ { filePath: "assets/file-1.txt", content: "Content of file-1" },
1168
+ { filePath: "assets/file-2.txt", content: "Content of file-2" },
1169
+ ];
1170
+ const kvNamespace = {
1171
+ title: "__test-name-workers_sites_assets",
1172
+ id: "__test-name-workers_sites_assets-id",
1173
+ };
1174
+ writeWranglerToml({
1175
+ main: "./index.js",
1176
+ site: {
1177
+ bucket: "assets",
1178
+ exclude: ["assets/file-1.txt"],
1179
+ },
1180
+ });
1181
+ writeWorkerSource();
1182
+ writeAssets(assets);
1183
+ mockUploadWorkerRequest();
1184
+ mockSubDomainRequest();
1185
+ mockListKVNamespacesRequest(kvNamespace);
1186
+ mockKeyListRequest(kvNamespace.id, []);
1187
+ // Check we only upload file-1
1188
+ mockUploadAssetsToKVRequest(
1189
+ kvNamespace.id,
1190
+ assets.filter((a) => a.filePath.endsWith("assets/file-1.txt"))
1191
+ );
1192
+ await runWrangler("publish --site-exclude file-2.txt");
1193
+
1194
+ expect(std.out).toMatchInlineSnapshot(`
1195
+ "reading assets/file-1.txt...
1196
+ uploading as assets/file-1.2ca234f380.txt...
1197
+ Uploaded
1198
+ test-name
1199
+ (TIMINGS)
1200
+ Published
1201
+ test-name
1202
+ (TIMINGS)
1203
+
1204
+ test-name.test-sub-domain.workers.dev"
1205
+ `);
1206
+ expect(std.err).toMatchInlineSnapshot(`""`);
1207
+ });
1208
+
1209
+ it("should walk directories except node_modules", async () => {
1210
+ const assets = [
1211
+ {
1212
+ filePath: "assets/directory-1/file-1.txt",
1213
+ content: "Content of file-1",
1214
+ },
1215
+ {
1216
+ filePath: "assets/node_modules/file-2.txt",
1217
+ content: "Content of file-2",
1218
+ },
1219
+ ];
1220
+ const kvNamespace = {
1221
+ title: "__test-name-workers_sites_assets",
1222
+ id: "__test-name-workers_sites_assets-id",
1223
+ };
1224
+ writeWranglerToml({
1225
+ main: "./index.js",
1226
+ site: {
1227
+ bucket: "assets",
1228
+ },
1229
+ });
1230
+ writeWorkerSource();
1231
+ writeAssets(assets);
1232
+ mockUploadWorkerRequest();
1233
+ mockSubDomainRequest();
1234
+ mockListKVNamespacesRequest(kvNamespace);
1235
+ mockKeyListRequest(kvNamespace.id, []);
1236
+ // Only expect file-1 to be uploaded
1237
+ mockUploadAssetsToKVRequest(kvNamespace.id, assets.slice(0, 1));
1238
+ await runWrangler("publish");
1239
+
1240
+ expect(std.out).toMatchInlineSnapshot(`
1241
+ "reading assets/directory-1/file-1.txt...
1242
+ uploading as assets/directory-1/file-1.2ca234f380.txt...
1243
+ Uploaded
1244
+ test-name
1245
+ (TIMINGS)
1246
+ Published
1247
+ test-name
1248
+ (TIMINGS)
1249
+
1250
+ test-name.test-sub-domain.workers.dev"
1251
+ `);
1252
+ expect(std.err).toMatchInlineSnapshot(`""`);
1253
+ });
1254
+
1255
+ it("should skip hidden files and directories except `.well-known`", async () => {
1256
+ const assets = [
1257
+ {
1258
+ filePath: "assets/.hidden-file.txt",
1259
+ content: "Content of hidden-file",
1260
+ },
1261
+ {
1262
+ filePath: "assets/.hidden/file-1.txt",
1263
+ content: "Content of file-1",
1264
+ },
1265
+ {
1266
+ filePath: "assets/.well-known/file-2.txt",
1267
+ content: "Content of file-2",
1268
+ },
1269
+ ];
1270
+ const kvNamespace = {
1271
+ title: "__test-name-workers_sites_assets",
1272
+ id: "__test-name-workers_sites_assets-id",
1273
+ };
1274
+ writeWranglerToml({
1275
+ main: "./index.js",
1276
+ site: {
1277
+ bucket: "assets",
1278
+ },
1279
+ });
1280
+ writeWorkerSource();
1281
+ writeAssets(assets);
1282
+ mockUploadWorkerRequest();
1283
+ mockSubDomainRequest();
1284
+ mockListKVNamespacesRequest(kvNamespace);
1285
+ mockKeyListRequest(kvNamespace.id, []);
1286
+ // Only expect file-2 to be uploaded
1287
+ mockUploadAssetsToKVRequest(kvNamespace.id, assets.slice(2));
1288
+ await runWrangler("publish");
1289
+
1290
+ expect(std.out).toMatchInlineSnapshot(`
1291
+ "reading assets/.well-known/file-2.txt...
1292
+ uploading as assets/.well-known/file-2.5938485188.txt...
1293
+ Uploaded
1294
+ test-name
1295
+ (TIMINGS)
1296
+ Published
1297
+ test-name
1298
+ (TIMINGS)
1299
+
1300
+ test-name.test-sub-domain.workers.dev"
1301
+ `);
1302
+ expect(std.err).toMatchInlineSnapshot(`""`);
1303
+ });
1304
+
1305
+ it("should error if the asset is over 25Mb", async () => {
1306
+ const assets = [
1307
+ {
1308
+ filePath: "assets/large-file.txt",
1309
+ // This file is greater than 25MiB when base64 encoded but small enough to be uploaded.
1310
+ content: "X".repeat(25 * 1024 * 1024 * 0.8 + 1),
1311
+ },
1312
+ {
1313
+ filePath: "assets/too-large-file.txt",
1314
+ content: "X".repeat(25 * 1024 * 1024 + 1),
1315
+ },
1316
+ ];
1317
+ const kvNamespace = {
1318
+ title: "__test-name-workers_sites_assets",
1319
+ id: "__test-name-workers_sites_assets-id",
1320
+ };
1321
+ writeWranglerToml({
1322
+ main: "./index.js",
1323
+ site: {
1324
+ bucket: "assets",
1325
+ exclude: ["assets/file-1.txt"],
1326
+ },
1327
+ });
1328
+ writeWorkerSource();
1329
+ writeAssets(assets);
1330
+ mockUploadWorkerRequest();
1331
+ mockSubDomainRequest();
1332
+ mockListKVNamespacesRequest(kvNamespace);
1333
+ mockKeyListRequest(kvNamespace.id, []);
1334
+
1335
+ await expect(
1336
+ runWrangler("publish")
1337
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
1338
+ `"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"`
1339
+ );
1340
+
1341
+ expect(std.out).toMatchInlineSnapshot(`
1342
+ "reading assets/large-file.txt...
1343
+ uploading as assets/large-file.0ea0637a45.txt..."
1344
+ `);
1345
+ expect(std.err).toMatchInlineSnapshot(`
1346
+ "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
1347
+
1348
+ %s
1349
+ If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
1350
+ `);
1351
+ });
1352
+
1353
+ it("should error if the asset key is over 512 characters", async () => {
1354
+ const longFilePathAsset = {
1355
+ filePath: "assets/" + "folder/".repeat(100) + "file.txt",
1356
+ content: "content of file",
1357
+ };
1358
+ const kvNamespace = {
1359
+ title: "__test-name-workers_sites_assets",
1360
+ id: "__test-name-workers_sites_assets-id",
1361
+ };
1362
+ writeWranglerToml({
1363
+ main: "./index.js",
1364
+ site: {
1365
+ bucket: "assets",
1366
+ },
1367
+ });
1368
+ writeWorkerSource();
1369
+ writeAssets([longFilePathAsset]);
1370
+ mockUploadWorkerRequest();
1371
+ mockSubDomainRequest();
1372
+ mockListKVNamespacesRequest(kvNamespace);
1373
+ mockKeyListRequest(kvNamespace.id, []);
1374
+
1375
+ await expect(
1376
+ runWrangler("publish")
1377
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
1378
+ `"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\\","`
1379
+ );
1380
+
1381
+ expect(std.out).toMatchInlineSnapshot(
1382
+ `"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..."`
1383
+ );
1384
+ expect(std.err).toMatchInlineSnapshot(`
1385
+ "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\\",
1386
+
1387
+ %s
1388
+ If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
1389
+ `);
1390
+ });
1391
+ });
1392
+
1393
+ describe("workers_dev setting", () => {
1394
+ it("should publish to a workers.dev domain if workers_dev is undefined", async () => {
1395
+ writeWranglerToml();
1396
+ writeWorkerSource();
1397
+ mockUploadWorkerRequest();
1398
+ mockSubDomainRequest();
1399
+
1400
+ await runWrangler("publish ./index");
1401
+
1402
+ expect(std.out).toMatchInlineSnapshot(`
1403
+ "Uploaded
1404
+ test-name
1405
+ (TIMINGS)
1406
+ Published
1407
+ test-name
1408
+ (TIMINGS)
1409
+
1410
+ test-name.test-sub-domain.workers.dev"
1411
+ `);
1412
+ expect(std.err).toMatchInlineSnapshot(`""`);
1413
+ });
1414
+
1415
+ it("should publish to the workers.dev domain if workers_dev is `true`", async () => {
1416
+ writeWranglerToml({
1417
+ workers_dev: true,
1418
+ });
1419
+ writeWorkerSource();
1420
+ mockUploadWorkerRequest();
1421
+ mockSubDomainRequest();
1422
+
1423
+ await runWrangler("publish ./index");
1424
+
1425
+ expect(std.out).toMatchInlineSnapshot(`
1426
+ "Uploaded
1427
+ test-name
1428
+ (TIMINGS)
1429
+ Published
1430
+ test-name
1431
+ (TIMINGS)
1432
+
1433
+ test-name.test-sub-domain.workers.dev"
1434
+ `);
1435
+ expect(std.err).toMatchInlineSnapshot(`""`);
1436
+ });
1437
+
1438
+ it("should disable the workers.dev domain if workers_dev is `false`", async () => {
1439
+ writeWranglerToml({
1440
+ workers_dev: false,
1441
+ });
1442
+ writeWorkerSource();
1443
+ mockUploadWorkerRequest();
1444
+ mockSubDomainRequest();
1445
+ mockUpdateWorkerRequest({ enabled: false });
1446
+
1447
+ await runWrangler("publish ./index");
1448
+
1449
+ expect(std.out).toMatchInlineSnapshot(`
1450
+ "Uploaded
1451
+ test-name
1452
+ (TIMINGS)
1453
+ No publish targets for
1454
+ test-name
1455
+ (TIMINGS)"
1456
+ `);
1457
+ expect(std.err).toMatchInlineSnapshot(`""`);
1458
+ });
1459
+
1460
+ it("should enable the workers.dev domain if workers_dev is undefined and subdomain is not already available", async () => {
1461
+ writeWranglerToml();
1462
+ writeWorkerSource();
1463
+ mockUploadWorkerRequest({ available_on_subdomain: false });
1464
+ mockSubDomainRequest();
1465
+ mockUpdateWorkerRequest({ enabled: true });
1466
+
1467
+ await runWrangler("publish ./index");
1468
+
1469
+ expect(std.out).toMatchInlineSnapshot(`
1470
+ "Uploaded
1471
+ test-name
1472
+ (TIMINGS)
1473
+ Published
1474
+ test-name
1475
+ (TIMINGS)
1476
+
1477
+ test-name.test-sub-domain.workers.dev"
1478
+ `);
1479
+ expect(std.err).toMatchInlineSnapshot(`""`);
1480
+ });
1481
+
1482
+ it("should enable the workers.dev domain if workers_dev is true and subdomain is not already available", async () => {
1483
+ writeWranglerToml({ workers_dev: true });
1484
+ writeWorkerSource();
1485
+ mockUploadWorkerRequest({ available_on_subdomain: false });
1486
+ mockSubDomainRequest();
1487
+ mockUpdateWorkerRequest({ enabled: true });
1488
+
1489
+ await runWrangler("publish ./index");
1490
+
1491
+ expect(std.out).toMatchInlineSnapshot(`
1492
+ "Uploaded
1493
+ test-name
1494
+ (TIMINGS)
1495
+ Published
1496
+ test-name
1497
+ (TIMINGS)
1498
+
1499
+ test-name.test-sub-domain.workers.dev"
1500
+ `);
1501
+ expect(std.err).toMatchInlineSnapshot(`""`);
1502
+ });
1503
+ });
1504
+
1505
+ describe("custom builds", () => {
1506
+ beforeEach(() => {
1507
+ // @ts-expect-error disable the mock we'd setup earlier
1508
+ // or else custom builds will timeout immediately
1509
+ global.setTimeout.mockRestore();
1510
+ });
1511
+ it("should run a custom build before publishing", async () => {
1512
+ writeWranglerToml({
1513
+ build: {
1514
+ command: `node -e "console.log('custom build'); require('fs').writeFileSync('index.js', 'export default { fetch(){ return new Response(123) } }')"`,
1515
+ },
1516
+ });
1517
+
1518
+ mockUploadWorkerRequest({
1519
+ expectedEntry: "return new Response(123)",
1520
+ });
1521
+ mockSubDomainRequest();
1522
+
1523
+ await runWrangler("publish index.js");
1524
+ expect(std.out).toMatchInlineSnapshot(`
1525
+ "running:
1526
+ node -e \\"console.log('custom build'); require('fs').writeFileSync('index.js', 'export default { fetch(){ return new Response(123) } }')\\"
1527
+ Uploaded
1528
+ test-name
1529
+ (TIMINGS)
1530
+ Published
1531
+ test-name
1532
+ (TIMINGS)
1533
+
1534
+ test-name.test-sub-domain.workers.dev"
1535
+ `);
1536
+ expect(std.err).toMatchInlineSnapshot(`""`);
1537
+ expect(std.warn).toMatchInlineSnapshot(`""`);
1538
+ });
1539
+
1540
+ if (process.platform !== "win32") {
1541
+ it("should run a custom build of multiple steps combined by && before publishing", async () => {
1542
+ writeWranglerToml({
1543
+ build: {
1544
+ command: `echo "custom build" && echo "export default { fetch(){ return new Response(123) } }" > index.js`,
1545
+ },
1546
+ });
1547
+
1548
+ mockUploadWorkerRequest({
1549
+ expectedEntry: "return new Response(123)",
1550
+ });
1551
+ mockSubDomainRequest();
1552
+
1553
+ await runWrangler("publish index.js");
1554
+ expect(std.out).toMatchInlineSnapshot(`
1555
+ "running:
1556
+ echo \\"custom build\\" && echo \\"export default { fetch(){ return new Response(123) } }\\" > index.js
1557
+ Uploaded
1558
+ test-name
1559
+ (TIMINGS)
1560
+ Published
1561
+ test-name
1562
+ (TIMINGS)
1563
+
1564
+ test-name.test-sub-domain.workers.dev"
1565
+ `);
1566
+ expect(std.err).toMatchInlineSnapshot(`""`);
1567
+ expect(std.warn).toMatchInlineSnapshot(`""`);
1568
+ });
1569
+ }
1570
+ });
1571
+
1572
+ describe("[wasm_modules]", () => {
1573
+ it("should be able to define wasm modules for service-worker format workers", async () => {
1574
+ writeWranglerToml({
1575
+ wasm_modules: {
1576
+ TESTWASMNAME: "./path/to/test.wasm",
1577
+ },
1578
+ });
1579
+ writeWorkerSource({ type: "sw" });
1580
+ fs.mkdirSync("./path/to", { recursive: true });
1581
+ fs.writeFileSync("./path/to/test.wasm", "SOME WASM CONTENT");
1582
+ mockUploadWorkerRequest({
1583
+ expectedType: "sw",
1584
+ expectedModules: { TESTWASMNAME: "SOME WASM CONTENT" },
1585
+ expectedBindings: [
1586
+ { name: "TESTWASMNAME", part: "TESTWASMNAME", type: "wasm_module" },
1587
+ ],
1588
+ });
1589
+ mockSubDomainRequest();
1590
+ await runWrangler("publish index.js");
1591
+ expect(std.out).toMatchInlineSnapshot(`
1592
+ "Uploaded
1593
+ test-name
1594
+ (TIMINGS)
1595
+ Published
1596
+ test-name
1597
+ (TIMINGS)
1598
+
1599
+ test-name.test-sub-domain.workers.dev"
1600
+ `);
1601
+ expect(std.err).toMatchInlineSnapshot(`""`);
1602
+ expect(std.warn).toMatchInlineSnapshot(`""`);
1603
+ });
1604
+
1605
+ it("should error when defining wasm modules for modules format workers", async () => {
1606
+ writeWranglerToml({
1607
+ wasm_modules: {
1608
+ TESTWASMNAME: "./path/to/test.wasm",
1609
+ },
1610
+ });
1611
+ writeWorkerSource({ type: "esm" });
1612
+ fs.mkdirSync("./path/to", { recursive: true });
1613
+ fs.writeFileSync("./path/to/test.wasm", "SOME WASM CONTENT");
1614
+
1615
+ await expect(
1616
+ runWrangler("publish index.js")
1617
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
1618
+ `"You cannot configure [wasm_modules] with an ES module worker. Instead, import the .wasm module directly in your code"`
1619
+ );
1620
+ expect(std.out).toMatchInlineSnapshot(`""`);
1621
+ expect(std.err).toMatchInlineSnapshot(`
1622
+ "You cannot configure [wasm_modules] with an ES module worker. Instead, import the .wasm module directly in your code
1623
+
1624
+ %s
1625
+ If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
1626
+ `);
1627
+ expect(std.warn).toMatchInlineSnapshot(`""`);
1628
+ });
1629
+
1630
+ it("should resolve wasm modules relative to the wrangler.toml file", async () => {
1631
+ fs.mkdirSync("./path/to/and/the/path/to/", { recursive: true });
1632
+ fs.writeFileSync(
1633
+ "./path/to/wrangler.toml",
1634
+ TOML.stringify({
1635
+ compatibility_date: "2022-01-12",
1636
+ name: "test-name",
1637
+ wasm_modules: {
1638
+ TESTWASMNAME: "./and/the/path/to/test.wasm",
1639
+ },
1640
+ }),
1641
+
1642
+ "utf-8"
1643
+ );
1644
+
1645
+ writeWorkerSource({ type: "sw" });
1646
+ fs.writeFileSync(
1647
+ "./path/to/and/the/path/to/test.wasm",
1648
+ "SOME WASM CONTENT"
1649
+ );
1650
+ mockUploadWorkerRequest({
1651
+ expectedType: "sw",
1652
+ expectedModules: { TESTWASMNAME: "SOME WASM CONTENT" },
1653
+ expectedBindings: [
1654
+ { name: "TESTWASMNAME", part: "TESTWASMNAME", type: "wasm_module" },
1655
+ ],
1656
+ });
1657
+ mockSubDomainRequest();
1658
+ await runWrangler("publish index.js --config ./path/to/wrangler.toml");
1659
+ expect(std.out).toMatchInlineSnapshot(`
1660
+ "Uploaded
1661
+ test-name
1662
+ (TIMINGS)
1663
+ Published
1664
+ test-name
1665
+ (TIMINGS)
1666
+
1667
+ test-name.test-sub-domain.workers.dev"
1668
+ `);
1669
+ expect(std.err).toMatchInlineSnapshot(`""`);
1670
+ expect(std.warn).toMatchInlineSnapshot(`""`);
1671
+ });
1672
+
1673
+ it("should be able to import .wasm modules from service-worker format workers", async () => {
1674
+ writeWranglerToml();
1675
+ fs.writeFileSync("./index.js", "import TESTWASMNAME from './test.wasm';");
1676
+ fs.writeFileSync("./test.wasm", "SOME WASM CONTENT");
1677
+ mockUploadWorkerRequest({
1678
+ expectedType: "sw",
1679
+ expectedModules: {
1680
+ __94b240d0d692281e6467aa42043986e5c7eea034_test_wasm:
1681
+ "SOME WASM CONTENT",
1682
+ },
1683
+ expectedBindings: [
1684
+ {
1685
+ name: "__94b240d0d692281e6467aa42043986e5c7eea034_test_wasm",
1686
+ part: "__94b240d0d692281e6467aa42043986e5c7eea034_test_wasm",
1687
+ type: "wasm_module",
1688
+ },
1689
+ ],
1690
+ });
1691
+ mockSubDomainRequest();
1692
+ await runWrangler("publish index.js");
1693
+ expect(std.out).toMatchInlineSnapshot(`
1694
+ "Uploaded
1695
+ test-name
1696
+ (TIMINGS)
1697
+ Published
1698
+ test-name
1699
+ (TIMINGS)
1700
+
1701
+ test-name.test-sub-domain.workers.dev"
1702
+ `);
1703
+ expect(std.err).toMatchInlineSnapshot(`""`);
1704
+ expect(std.warn).toMatchInlineSnapshot(`""`);
1705
+ });
1706
+ });
1707
+
1708
+ describe("[text_blobs]", () => {
1709
+ it("should be able to define text blobs for service-worker format workers", async () => {
1710
+ writeWranglerToml({
1711
+ text_blobs: {
1712
+ TESTTEXTBLOBNAME: "./path/to/text.file",
1713
+ },
1714
+ });
1715
+ writeWorkerSource({ type: "sw" });
1716
+ fs.mkdirSync("./path/to", { recursive: true });
1717
+ fs.writeFileSync("./path/to/text.file", "SOME TEXT CONTENT");
1718
+ mockUploadWorkerRequest({
1719
+ expectedType: "sw",
1720
+ expectedModules: { TESTTEXTBLOBNAME: "SOME TEXT CONTENT" },
1721
+ expectedBindings: [
1722
+ {
1723
+ name: "TESTTEXTBLOBNAME",
1724
+ part: "TESTTEXTBLOBNAME",
1725
+ type: "text_blob",
1726
+ },
1727
+ ],
1728
+ });
1729
+ mockSubDomainRequest();
1730
+ await runWrangler("publish index.js");
1731
+ expect(std.out).toMatchInlineSnapshot(`
1732
+ "Uploaded
1733
+ test-name
1734
+ (TIMINGS)
1735
+ Published
1736
+ test-name
1737
+ (TIMINGS)
1738
+
1739
+ test-name.test-sub-domain.workers.dev"
1740
+ `);
1741
+ expect(std.err).toMatchInlineSnapshot(`""`);
1742
+ expect(std.warn).toMatchInlineSnapshot(`""`);
1743
+ });
1744
+
1745
+ it("should error when defining text blobs for modules format workers", async () => {
1746
+ writeWranglerToml({
1747
+ text_blobs: {
1748
+ TESTTEXTBLOBNAME: "./path/to/text.file",
1749
+ },
1750
+ });
1751
+ writeWorkerSource({ type: "esm" });
1752
+ fs.mkdirSync("./path/to", { recursive: true });
1753
+ fs.writeFileSync("./path/to/text.file", "SOME TEXT CONTENT");
1754
+
1755
+ await expect(
1756
+ runWrangler("publish index.js")
1757
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
1758
+ `"You cannot configure [text_blobs] with an ES module worker. Instead, import the file directly in your code, and optionally configure \`[build.upload.rules]\` in your wrangler.toml"`
1759
+ );
1760
+ expect(std.out).toMatchInlineSnapshot(`""`);
1761
+ expect(std.err).toMatchInlineSnapshot(`
1762
+ "You cannot configure [text_blobs] with an ES module worker. Instead, import the file directly in your code, and optionally configure \`[build.upload.rules]\` in your wrangler.toml
1763
+
1764
+ %s
1765
+ If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
1766
+ `);
1767
+ expect(std.warn).toMatchInlineSnapshot(`""`);
1768
+ });
1769
+
1770
+ it("should resolve text blobs relative to the wrangler.toml file", async () => {
1771
+ fs.mkdirSync("./path/to/and/the/path/to/", { recursive: true });
1772
+ fs.writeFileSync(
1773
+ "./path/to/wrangler.toml",
1774
+ TOML.stringify({
1775
+ compatibility_date: "2022-01-12",
1776
+ name: "test-name",
1777
+ text_blobs: {
1778
+ TESTTEXTBLOBNAME: "./and/the/path/to/text.file",
1779
+ },
1780
+ }),
1781
+
1782
+ "utf-8"
1783
+ );
1784
+
1785
+ writeWorkerSource({ type: "sw" });
1786
+ fs.writeFileSync(
1787
+ "./path/to/and/the/path/to/text.file",
1788
+ "SOME TEXT CONTENT"
1789
+ );
1790
+ mockUploadWorkerRequest({
1791
+ expectedType: "sw",
1792
+ expectedModules: { TESTTEXTBLOBNAME: "SOME TEXT CONTENT" },
1793
+ expectedBindings: [
1794
+ {
1795
+ name: "TESTTEXTBLOBNAME",
1796
+ part: "TESTTEXTBLOBNAME",
1797
+ type: "text_blob",
1798
+ },
1799
+ ],
1800
+ });
1801
+ mockSubDomainRequest();
1802
+ await runWrangler("publish index.js --config ./path/to/wrangler.toml");
1803
+ expect(std.out).toMatchInlineSnapshot(`
1804
+ "Uploaded
1805
+ test-name
1806
+ (TIMINGS)
1807
+ Published
1808
+ test-name
1809
+ (TIMINGS)
1810
+
1811
+ test-name.test-sub-domain.workers.dev"
1812
+ `);
1813
+ expect(std.err).toMatchInlineSnapshot(`""`);
1814
+ expect(std.warn).toMatchInlineSnapshot(`""`);
1815
+ });
1816
+ });
1817
+
1818
+ describe("vars bindings", () => {
1819
+ it("should support json bindings", async () => {
1820
+ writeWranglerToml({
1821
+ vars: {
1822
+ text: "plain ol' string",
1823
+ count: 1,
1824
+ complex: { enabled: true, id: 123 },
1825
+ },
1826
+ });
1827
+ writeWorkerSource();
1828
+ mockSubDomainRequest();
1829
+ mockUploadWorkerRequest({
1830
+ expectedBindings: [
1831
+ { name: "text", type: "plain_text", text: "plain ol' string" },
1832
+ { name: "count", type: "json", json: 1 },
1833
+ {
1834
+ name: "complex",
1835
+ type: "json",
1836
+ json: { enabled: true, id: 123 },
1837
+ },
1838
+ ],
1839
+ });
1840
+
1841
+ await runWrangler("publish index.js");
1842
+ expect(std.out).toMatchInlineSnapshot(`
1843
+ "Uploaded
1844
+ test-name
1845
+ (TIMINGS)
1846
+ Published
1847
+ test-name
1848
+ (TIMINGS)
1849
+
1850
+ test-name.test-sub-domain.workers.dev"
1851
+ `);
1852
+ expect(std.err).toMatchInlineSnapshot(`""`);
1853
+ expect(std.warn).toMatchInlineSnapshot(`""`);
1854
+ });
1855
+ });
1856
+
1857
+ describe("r2 bucket bindings", () => {
1858
+ it("should support r2 bucket bindings", async () => {
1859
+ writeWranglerToml({
1860
+ r2_buckets: [{ binding: "FOO", bucket_name: "foo-bucket" }],
1861
+ });
1862
+ writeWorkerSource();
1863
+ mockSubDomainRequest();
1864
+ mockUploadWorkerRequest({
1865
+ expectedBindings: [
1866
+ { bucket_name: "foo-bucket", name: "FOO", type: "r2_bucket" },
1867
+ ],
1868
+ });
1869
+
1870
+ await runWrangler("publish index.js");
1871
+ expect(std.out).toMatchInlineSnapshot(`
1872
+ "Uploaded
1873
+ test-name
1874
+ (TIMINGS)
1875
+ Published
1876
+ test-name
1877
+ (TIMINGS)
1878
+
1879
+ test-name.test-sub-domain.workers.dev"
1880
+ `);
1881
+ expect(std.err).toMatchInlineSnapshot(`""`);
1882
+ expect(std.warn).toMatchInlineSnapshot(`""`);
1883
+ });
1884
+ });
1885
+
1886
+ describe("unsafe bindings", () => {
1887
+ it("should warn if using unsafe bindings", async () => {
1888
+ writeWranglerToml({
1889
+ unsafe: {
1890
+ bindings: [
1891
+ {
1892
+ name: "my-binding",
1893
+ type: "binding-type",
1894
+ param: "binding-param",
1895
+ },
1896
+ ],
1897
+ },
1898
+ });
1899
+ writeWorkerSource();
1900
+ mockSubDomainRequest();
1901
+ mockUploadWorkerRequest({
1902
+ expectedBindings: [
1903
+ {
1904
+ name: "my-binding",
1905
+ type: "binding-type",
1906
+ param: "binding-param",
1907
+ },
1908
+ ],
1909
+ });
1910
+
1911
+ await runWrangler("publish index.js");
1912
+ expect(std.out).toMatchInlineSnapshot(`
1913
+ "Uploaded
1914
+ test-name
1915
+ (TIMINGS)
1916
+ Published
1917
+ test-name
1918
+ (TIMINGS)
1919
+
1920
+ test-name.test-sub-domain.workers.dev"
1921
+ `);
1922
+ expect(std.err).toMatchInlineSnapshot(`""`);
1923
+ expect(std.warn).toMatchInlineSnapshot(`
1924
+ "Processing wrangler.toml configuration:
1925
+ - \\"unsafe\\" fields are experimental and may change or break at any time."
1926
+ `);
1927
+ });
1928
+ it("should warn if using unsafe bindings already handled by wrangler", async () => {
1929
+ writeWranglerToml({
1930
+ unsafe: {
1931
+ bindings: [
1932
+ {
1933
+ name: "my-binding",
1934
+ type: "plain_text",
1935
+ text: "text",
1936
+ },
1937
+ ],
1938
+ },
1939
+ });
1940
+ writeWorkerSource();
1941
+ mockSubDomainRequest();
1942
+ mockUploadWorkerRequest({
1943
+ expectedBindings: [
1944
+ {
1945
+ name: "my-binding",
1946
+ type: "plain_text",
1947
+ text: "text",
1948
+ },
1949
+ ],
1950
+ });
1951
+
1952
+ await runWrangler("publish index.js");
1953
+ expect(std.out).toMatchInlineSnapshot(`
1954
+ "Uploaded
1955
+ test-name
1956
+ (TIMINGS)
1957
+ Published
1958
+ test-name
1959
+ (TIMINGS)
1960
+
1961
+ test-name.test-sub-domain.workers.dev"
1962
+ `);
1963
+ expect(std.err).toMatchInlineSnapshot(`""`);
1964
+ expect(std.warn).toMatchInlineSnapshot(`
1965
+ "Processing wrangler.toml configuration:
1966
+ - \\"unsafe\\" fields are experimental and may change or break at any time.
1967
+ - \\"unsafe.bindings[0]\\": {\\"name\\":\\"my-binding\\",\\"type\\":\\"plain_text\\",\\"text\\":\\"text\\"}
1968
+ - The binding type \\"plain_text\\" is directly supported by wrangler.
1969
+ Consider migrating this unsafe binding to a format for 'plain_text' bindings that is supported by wrangler for optimal support.
1970
+ For more details, see https://developers.cloudflare.com/workers/cli-wrangler/configuration"
1971
+ `);
1972
+ });
1973
+ });
1974
+
1975
+ describe("upload rules", () => {
1976
+ it("should be able to define rules for uploading non-js modules (sw)", async () => {
1977
+ writeWranglerToml({
1978
+ rules: [{ type: "Text", globs: ["**/*.file"], fallthrough: true }],
1979
+ });
1980
+ fs.writeFileSync("./index.js", `import TEXT from './text.file';`);
1981
+ fs.writeFileSync("./text.file", "SOME TEXT CONTENT");
1982
+ mockSubDomainRequest();
1983
+ mockUploadWorkerRequest({
1984
+ expectedType: "sw",
1985
+ expectedBindings: [
1986
+ {
1987
+ name: "__2d91d1c4dd6e57d4f5432187ab7c25f45a8973f0_text_file",
1988
+ part: "__2d91d1c4dd6e57d4f5432187ab7c25f45a8973f0_text_file",
1989
+ type: "text_blob",
1990
+ },
1991
+ ],
1992
+ expectedModules: {
1993
+ __2d91d1c4dd6e57d4f5432187ab7c25f45a8973f0_text_file:
1994
+ "SOME TEXT CONTENT",
1995
+ },
1996
+ });
1997
+ await runWrangler("publish index.js");
1998
+ expect(std.out).toMatchInlineSnapshot(`
1999
+ "Uploaded
2000
+ test-name
2001
+ (TIMINGS)
2002
+ Published
2003
+ test-name
2004
+ (TIMINGS)
2005
+
2006
+ test-name.test-sub-domain.workers.dev"
2007
+ `);
2008
+ expect(std.err).toMatchInlineSnapshot(`""`);
2009
+ expect(std.warn).toMatchInlineSnapshot(`""`);
2010
+ });
2011
+
2012
+ it("should be able to define rules for uploading non-js modules (esm)", async () => {
2013
+ writeWranglerToml({
2014
+ rules: [{ type: "Text", globs: ["**/*.file"], fallthrough: true }],
2015
+ });
2016
+ fs.writeFileSync(
2017
+ "./index.js",
2018
+ `import TEXT from './text.file'; export default {};`
2019
+ );
2020
+ fs.writeFileSync("./text.file", "SOME TEXT CONTENT");
2021
+ mockSubDomainRequest();
2022
+ mockUploadWorkerRequest({
2023
+ expectedType: "esm",
2024
+ expectedBindings: [],
2025
+ expectedModules: {
2026
+ "./2d91d1c4dd6e57d4f5432187ab7c25f45a8973f0-text.file":
2027
+ "SOME TEXT CONTENT",
2028
+ },
2029
+ });
2030
+ await runWrangler("publish index.js");
2031
+ expect(std.out).toMatchInlineSnapshot(`
2032
+ "Uploaded
2033
+ test-name
2034
+ (TIMINGS)
2035
+ Published
2036
+ test-name
2037
+ (TIMINGS)
2038
+
2039
+ test-name.test-sub-domain.workers.dev"
2040
+ `);
2041
+ expect(std.err).toMatchInlineSnapshot(`""`);
2042
+ expect(std.warn).toMatchInlineSnapshot(`""`);
2043
+ });
2044
+
2045
+ it("should log a deprecation warning when using `build.upload.rules`", async () => {
2046
+ writeWranglerToml({
2047
+ build: {
2048
+ upload: {
2049
+ rules: [{ type: "Text", globs: ["**/*.file"], fallthrough: true }],
2050
+ },
2051
+ },
2052
+ });
2053
+ fs.writeFileSync(
2054
+ "./index.js",
2055
+ `import TEXT from './text.file'; export default {};`
2056
+ );
2057
+ fs.writeFileSync("./text.file", "SOME TEXT CONTENT");
2058
+ mockSubDomainRequest();
2059
+ mockUploadWorkerRequest({
2060
+ expectedType: "esm",
2061
+ expectedBindings: [],
2062
+ expectedModules: {
2063
+ "./2d91d1c4dd6e57d4f5432187ab7c25f45a8973f0-text.file":
2064
+ "SOME TEXT CONTENT",
2065
+ },
2066
+ });
2067
+ await runWrangler("publish index.js");
2068
+ expect(std.out).toMatchInlineSnapshot(`
2069
+ "Uploaded
2070
+ test-name
2071
+ (TIMINGS)
2072
+ Published
2073
+ test-name
2074
+ (TIMINGS)
2075
+
2076
+ test-name.test-sub-domain.workers.dev"
2077
+ `);
2078
+ expect(std.err).toMatchInlineSnapshot(`""`);
2079
+ expect(std.warn).toMatchInlineSnapshot(`
2080
+ "Processing wrangler.toml configuration:
2081
+ - 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:
2082
+ \`\`\`
2083
+ [[rules]]
2084
+ type = \\"Text\\"
2085
+ globs = [ \\"**/*.file\\" ]
2086
+ fallthrough = true
2087
+ \`\`\`"
2088
+ `);
2089
+ });
2090
+
2091
+ it("should be able to use fallthrough:true for multiple rules", async () => {
2092
+ writeWranglerToml({
2093
+ rules: [
2094
+ { type: "Text", globs: ["**/*.file"], fallthrough: true },
2095
+ { type: "Text", globs: ["**/*.other"], fallthrough: true },
2096
+ ],
2097
+ });
2098
+ fs.writeFileSync(
2099
+ "./index.js",
2100
+ `import TEXT from './text.file'; import OTHER from './other.other'; export default {};`
2101
+ );
2102
+ fs.writeFileSync("./text.file", "SOME TEXT CONTENT");
2103
+ fs.writeFileSync("./other.other", "SOME OTHER TEXT CONTENT");
2104
+ mockSubDomainRequest();
2105
+ mockUploadWorkerRequest({
2106
+ expectedType: "esm",
2107
+ expectedBindings: [],
2108
+ expectedModules: {
2109
+ "./2d91d1c4dd6e57d4f5432187ab7c25f45a8973f0-text.file":
2110
+ "SOME TEXT CONTENT",
2111
+ "./16347a01366873ed80fe45115119de3c92ab8db0-other.other":
2112
+ "SOME OTHER TEXT CONTENT",
2113
+ },
2114
+ });
2115
+ await runWrangler("publish index.js");
2116
+ expect(std.out).toMatchInlineSnapshot(`
2117
+ "Uploaded
2118
+ test-name
2119
+ (TIMINGS)
2120
+ Published
2121
+ test-name
2122
+ (TIMINGS)
2123
+
2124
+ test-name.test-sub-domain.workers.dev"
2125
+ `);
2126
+ expect(std.err).toMatchInlineSnapshot(`""`);
2127
+ expect(std.warn).toMatchInlineSnapshot(`""`);
2128
+ });
2129
+
2130
+ it("should be able to use fallthrough:false for multiple rules", async () => {
2131
+ writeWranglerToml({
2132
+ rules: [
2133
+ { type: "Text", globs: ["**/*.file"], fallthrough: false },
2134
+ { type: "Text", globs: ["**/*.other"] },
2135
+ ],
2136
+ });
2137
+ fs.writeFileSync(
2138
+ "./index.js",
2139
+ `import TEXT from './text.file'; import OTHER from './other.other'; export default {};`
2140
+ );
2141
+ fs.writeFileSync("./text.file", "SOME TEXT CONTENT");
2142
+ fs.writeFileSync("./other.other", "SOME OTHER TEXT CONTENT");
2143
+
2144
+ // We throw an error when we come across a file that matched a rule
2145
+ // but was skipped because of fallthrough = false
2146
+ let err: Error | undefined;
2147
+ try {
2148
+ await runWrangler("publish index.js");
2149
+ } catch (e) {
2150
+ err = e as Error;
2151
+ }
2152
+ expect(err?.message).toMatch(
2153
+ `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\`.`
2154
+ );
2155
+ });
2156
+
2157
+ it("should warn when multiple rules for the same type do not have fallback defined", async () => {
2158
+ writeWranglerToml({
2159
+ rules: [
2160
+ { type: "Text", globs: ["**/*.file"] },
2161
+ { type: "Text", globs: ["**/*.other"] },
2162
+ ],
2163
+ });
2164
+ fs.writeFileSync(
2165
+ "./index.js",
2166
+ `import TEXT from './text.file'; import OTHER from './other.other'; export default {};`
2167
+ );
2168
+ fs.writeFileSync("./text.file", "SOME TEXT CONTENT");
2169
+ fs.writeFileSync("./other.other", "SOME OTHER TEXT CONTENT");
2170
+
2171
+ // We throw an error when we come across a file that matched a rule
2172
+ // but was skipped because of fallthrough = false
2173
+ let err: Error | undefined;
2174
+ try {
2175
+ await runWrangler("publish index.js");
2176
+ } catch (e) {
2177
+ err = e as Error;
2178
+ }
2179
+ expect(err?.message).toMatch(
2180
+ `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\`.`
2181
+ );
2182
+ // and the warnings because fallthrough was not explicitly set
2183
+ expect(std.warn).toMatchInlineSnapshot(`
2184
+ "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.
2185
+ 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."
2186
+ `);
2187
+ });
2188
+ });
2189
+ });
2190
+
2191
+ /** Write a mock Worker script to disk. */
2192
+ function writeWorkerSource({
2193
+ basePath = ".",
2194
+ format = "js",
2195
+ type = "esm",
2196
+ }: {
2197
+ basePath?: string;
2198
+ format?: "js" | "ts" | "jsx" | "tsx" | "mjs";
2199
+ type?: "esm" | "sw";
2200
+ } = {}) {
2201
+ if (basePath !== ".") {
2202
+ fs.mkdirSync(basePath, { recursive: true });
2203
+ }
2204
+ fs.writeFileSync(
2205
+ `${basePath}/index.${format}`,
2206
+ type === "esm"
2207
+ ? `import { foo } from "./another";
2208
+ export default {
2209
+ async fetch(request) {
2210
+ return new Response('Hello' + foo);
2211
+ },
2212
+ };`
2213
+ : `import { foo } from "./another";
2214
+ addEventListener('fetch', event => {
2215
+ event.respondWith(new Response('Hello' + foo));
2216
+ })`
2217
+ );
2218
+ fs.writeFileSync(`${basePath}/another.${format}`, `export const foo = 100;`);
2219
+ }
2220
+
2221
+ /** Write mock assets to the file system so they can be uploaded. */
2222
+ function writeAssets(assets: { filePath: string; content: string }[]) {
2223
+ for (const asset of assets) {
2224
+ fs.mkdirSync(path.dirname(asset.filePath), { recursive: true });
2225
+ fs.writeFileSync(asset.filePath, asset.content);
2226
+ }
2227
+ }
2228
+
2229
+ /** Create a mock handler for the request to upload a worker script. */
2230
+ function mockUploadWorkerRequest({
2231
+ available_on_subdomain = true,
2232
+ expectedEntry,
2233
+ expectedType = "esm",
2234
+ expectedBindings,
2235
+ expectedModules = {},
2236
+ env = undefined,
2237
+ legacyEnv = false,
2238
+ }: {
2239
+ available_on_subdomain?: boolean;
2240
+ expectedEntry?: string;
2241
+ expectedType?: "esm" | "sw";
2242
+ expectedBindings?: unknown;
2243
+ expectedModules?: Record<string, string>;
2244
+ env?: string | undefined;
2245
+ legacyEnv?: boolean | undefined;
2246
+ } = {}) {
2247
+ setMockResponse(
2248
+ env && !legacyEnv
2249
+ ? "/accounts/:accountId/workers/services/:scriptName/environments/:envName"
2250
+ : "/accounts/:accountId/workers/scripts/:scriptName",
2251
+ "PUT",
2252
+ async ([_url, accountId, scriptName, envName], { body }, queryParams) => {
2253
+ expect(accountId).toEqual("some-account-id");
2254
+ expect(scriptName).toEqual(
2255
+ legacyEnv && env ? `test-name-${env}` : "test-name"
2256
+ );
2257
+ if (!legacyEnv) {
2258
+ expect(envName).toEqual(env);
2259
+ }
2260
+ expect(queryParams.get("available_on_subdomain")).toEqual("true");
2261
+ const formBody = body as FormData;
2262
+ if (expectedEntry !== undefined) {
2263
+ expect(await (formBody.get("index.js") as File).text()).toMatch(
2264
+ expectedEntry
2265
+ );
2266
+ }
2267
+
2268
+ const metadata = JSON.parse(
2269
+ formBody.get("metadata") as string
2270
+ ) as WorkerMetadata;
2271
+ if (expectedType === "esm") {
2272
+ expect(metadata.main_module).toEqual("index.js");
2273
+ } else {
2274
+ expect(metadata.body_part).toEqual("index.js");
2275
+ }
2276
+ if (expectedBindings !== undefined) {
2277
+ expect(metadata.bindings).toEqual(expectedBindings);
2278
+ }
2279
+ for (const [name, content] of Object.entries(expectedModules)) {
2280
+ expect(await (formBody.get(name) as File).text()).toEqual(content);
2281
+ }
2282
+
2283
+ return { available_on_subdomain };
2284
+ }
2285
+ );
2286
+ }
2287
+
2288
+ /** Create a mock handler for the request to get the account's subdomain. */
2289
+ function mockSubDomainRequest(subdomain = "test-sub-domain") {
2290
+ setMockResponse("/accounts/:accountId/workers/subdomain", "GET", () => {
2291
+ return { subdomain };
2292
+ });
2293
+ }
2294
+
2295
+ /** Create a mock handler to toggle a <script>.<user>.workers.dev subdomain */
2296
+ function mockUpdateWorkerRequest({
2297
+ env,
2298
+ enabled,
2299
+ legacyEnv = false,
2300
+ }: {
2301
+ enabled: boolean;
2302
+ env?: string | undefined;
2303
+ legacyEnv?: boolean | undefined;
2304
+ }) {
2305
+ const requests = { count: 0 };
2306
+ const servicesOrScripts = env && !legacyEnv ? "services" : "scripts";
2307
+ const environment = env && !legacyEnv ? "/environments/:envName" : "";
2308
+ setMockResponse(
2309
+ `/accounts/:accountId/workers/${servicesOrScripts}/:scriptName${environment}/subdomain`,
2310
+ "POST",
2311
+ ([_url, accountId, scriptName, envName], { body }) => {
2312
+ expect(accountId).toEqual("some-account-id");
2313
+ expect(scriptName).toEqual(
2314
+ legacyEnv && env ? `test-name-${env}` : "test-name"
2315
+ );
2316
+ if (!legacyEnv) {
2317
+ expect(envName).toEqual(env);
2318
+ }
2319
+ expect(JSON.parse(body as string)).toEqual({ enabled });
2320
+ return null;
2321
+ }
2322
+ );
2323
+ return requests;
2324
+ }
2325
+
2326
+ function mockPublishRoutesRequest({
2327
+ routes,
2328
+ env = undefined,
2329
+ legacyEnv = false,
2330
+ }: {
2331
+ routes: string[];
2332
+ env?: string | undefined;
2333
+ legacyEnv?: boolean | undefined;
2334
+ }) {
2335
+ const servicesOrScripts = env && !legacyEnv ? "services" : "scripts";
2336
+ const environment = env && !legacyEnv ? "/environments/:envName" : "";
2337
+
2338
+ setMockResponse(
2339
+ `/accounts/:accountId/workers/${servicesOrScripts}/:scriptName${environment}/routes`,
2340
+ "PUT",
2341
+ ([_url, accountId, scriptName, envName], { body }) => {
2342
+ expect(accountId).toEqual("some-account-id");
2343
+ expect(scriptName).toEqual(
2344
+ legacyEnv && env ? `test-name-${env}` : "test-name"
2345
+ );
2346
+ if (!legacyEnv) {
2347
+ expect(envName).toEqual(env);
2348
+ }
2349
+
2350
+ expect(JSON.parse(body as string)).toEqual(
2351
+ routes.map((pattern) => ({ pattern }))
2352
+ );
2353
+ return null;
2354
+ }
2355
+ );
2356
+ }
2357
+
2358
+ /** Create a mock handler for the request to get a list of all KV namespaces. */
2359
+ function mockListKVNamespacesRequest(...namespaces: KVNamespaceInfo[]) {
2360
+ setMockResponse(
2361
+ "/accounts/:accountId/storage/kv/namespaces",
2362
+ "GET",
2363
+ ([_url, accountId]) => {
2364
+ expect(accountId).toEqual("some-account-id");
2365
+ return namespaces;
2366
+ }
2367
+ );
2368
+ }
2369
+
2370
+ /** Create a mock handler for the request that tries to do a bulk upload of assets to a KV namespace. */
2371
+ function mockUploadAssetsToKVRequest(
2372
+ expectedNamespaceId: string,
2373
+ assets: { filePath: string; content: string }[]
2374
+ ) {
2375
+ setMockResponse(
2376
+ "/accounts/:accountId/storage/kv/namespaces/:namespaceId/bulk",
2377
+ "PUT",
2378
+ ([_url, accountId, namespaceId], { body }) => {
2379
+ expect(accountId).toEqual("some-account-id");
2380
+ expect(namespaceId).toEqual(expectedNamespaceId);
2381
+ const uploads = JSON.parse(body as string);
2382
+ expect(assets.length).toEqual(uploads.length);
2383
+ for (let i = 0; i < uploads.length; i++) {
2384
+ const asset = assets[i];
2385
+ const upload = uploads[i];
2386
+ // The asset key consists of:
2387
+ // - the basename of the filepath
2388
+ // - some hash value
2389
+ // - the extension
2390
+ const keyMatcher = new RegExp(
2391
+ "^" +
2392
+ asset.filePath
2393
+ .replace(/(\.[^.]+)$/, ".[a-z0-9]+$1")
2394
+ .replace(/\./g, "\\.")
2395
+ );
2396
+ expect(upload.key).toMatch(keyMatcher);
2397
+ // The asset value is base64 encoded.
2398
+ expect(upload.base64).toBe(true);
2399
+ expect(Buffer.from(upload.value, "base64").toString()).toEqual(
2400
+ asset.content
2401
+ );
2402
+ }
2403
+ return null;
2404
+ }
2405
+ );
2406
+ }