wrangler 0.0.0-e22f9d7 → 0.0.0-e2a26a0

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 (54) hide show
  1. package/bin/wrangler.js +2 -2
  2. package/package.json +15 -10
  3. package/pages/functions/buildWorker.ts +1 -1
  4. package/pages/functions/filepath-routing.test.ts +21 -13
  5. package/pages/functions/filepath-routing.ts +38 -28
  6. package/pages/functions/routes.ts +8 -6
  7. package/pages/functions/template-worker.ts +16 -24
  8. package/src/__tests__/dev.test.tsx +7 -3
  9. package/src/__tests__/{clipboardy-mock.js → helpers/clipboardy-mock.js} +0 -0
  10. package/src/__tests__/helpers/cmd-shim.d.ts +11 -0
  11. package/src/__tests__/helpers/mock-account-id.ts +30 -0
  12. package/src/__tests__/helpers/mock-bin.ts +33 -0
  13. package/src/__tests__/{mock-cfetch.ts → helpers/mock-cfetch.ts} +49 -13
  14. package/src/__tests__/helpers/mock-console.ts +56 -0
  15. package/src/__tests__/{mock-dialogs.ts → helpers/mock-dialogs.ts} +1 -1
  16. package/src/__tests__/helpers/mock-kv.ts +40 -0
  17. package/src/__tests__/helpers/mock-user.ts +27 -0
  18. package/src/__tests__/helpers/run-in-tmp.ts +29 -0
  19. package/src/__tests__/helpers/run-wrangler.ts +14 -0
  20. package/src/__tests__/index.test.ts +312 -56
  21. package/src/__tests__/jest.setup.ts +17 -2
  22. package/src/__tests__/kv.test.ts +344 -316
  23. package/src/__tests__/logout.test.ts +50 -0
  24. package/src/__tests__/package-manager.test.ts +206 -0
  25. package/src/__tests__/publish.test.ts +1222 -0
  26. package/src/__tests__/secret.test.ts +210 -0
  27. package/src/__tests__/whoami.test.tsx +86 -0
  28. package/src/api/form_data.ts +10 -7
  29. package/src/api/preview.ts +2 -2
  30. package/src/api/worker.ts +3 -3
  31. package/src/cfetch/index.ts +14 -14
  32. package/src/cfetch/internal.ts +40 -8
  33. package/src/config.ts +162 -8
  34. package/src/dev.tsx +103 -53
  35. package/src/dialogs.tsx +2 -2
  36. package/src/index.tsx +438 -311
  37. package/src/inspect.ts +80 -23
  38. package/src/kv.tsx +9 -1
  39. package/src/module-collection.ts +5 -6
  40. package/src/package-manager.ts +120 -0
  41. package/src/pages.tsx +41 -26
  42. package/src/paths.ts +26 -0
  43. package/src/proxy.ts +78 -10
  44. package/src/publish.ts +98 -52
  45. package/src/sites.tsx +175 -53
  46. package/src/user.tsx +51 -59
  47. package/src/whoami.tsx +69 -0
  48. package/templates/new-worker.js +15 -0
  49. package/templates/new-worker.ts +15 -0
  50. package/{static-asset-facade.js → templates/static-asset-facade.js} +0 -0
  51. package/wrangler-dist/cli.js +111479 -110187
  52. package/wrangler-dist/cli.js.map +3 -3
  53. package/src/__tests__/run-in-tmp.ts +0 -19
  54. package/src/__tests__/run-wrangler.ts +0 -32
@@ -0,0 +1,1222 @@
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 type { WorkerMetadata } from "../api/form_data";
11
+ import type { Config } from "../config";
12
+ import type { KVNamespaceInfo } from "../kv";
13
+ import type { FormData, File } from "undici";
14
+
15
+ describe("publish", () => {
16
+ mockAccountId();
17
+ mockApiToken();
18
+ runInTempDir();
19
+ const std = mockConsoleMethods();
20
+
21
+ afterEach(() => {
22
+ unsetAllMocks();
23
+ });
24
+
25
+ describe("entry-points", () => {
26
+ it("should be able to use `index` with no extension as the entry-point (esm)", async () => {
27
+ writeWranglerToml();
28
+ writeWorkerSource();
29
+ mockUploadWorkerRequest({ expectedType: "esm" });
30
+ mockSubDomainRequest();
31
+
32
+ await runWrangler("publish ./index");
33
+
34
+ expect(std.out).toMatchInlineSnapshot(`
35
+ "Uploaded
36
+ test-name
37
+ (TIMINGS)
38
+ Deployed
39
+ test-name
40
+ (TIMINGS)
41
+
42
+ test-name.test-sub-domain.workers.dev"
43
+ `);
44
+ expect(std.err).toMatchInlineSnapshot(`""`);
45
+ });
46
+ it("should be able to use `index` with no extension as the entry-point (sw)", async () => {
47
+ writeWranglerToml();
48
+ writeWorkerSource({ type: "sw" });
49
+ mockUploadWorkerRequest({ expectedType: "sw" });
50
+ mockSubDomainRequest();
51
+
52
+ await runWrangler("publish ./index");
53
+
54
+ expect(std.out).toMatchInlineSnapshot(`
55
+ "Uploaded
56
+ test-name
57
+ (TIMINGS)
58
+ Deployed
59
+ test-name
60
+ (TIMINGS)
61
+
62
+ test-name.test-sub-domain.workers.dev"
63
+ `);
64
+ expect(std.err).toMatchInlineSnapshot(`""`);
65
+ });
66
+
67
+ it("should be able to use the `build.upload.main` config as the entry-point for ESM sources", async () => {
68
+ writeWranglerToml({ build: { upload: { main: "./index.js" } } });
69
+ writeWorkerSource();
70
+ mockUploadWorkerRequest();
71
+ mockSubDomainRequest();
72
+
73
+ await runWrangler("publish");
74
+
75
+ expect(std.out).toMatchInlineSnapshot(`
76
+ "Uploaded
77
+ test-name
78
+ (TIMINGS)
79
+ Deployed
80
+ test-name
81
+ (TIMINGS)
82
+
83
+ test-name.test-sub-domain.workers.dev"
84
+ `);
85
+ expect(std.err).toMatchInlineSnapshot(`""`);
86
+ });
87
+
88
+ it("should be able to transpile TypeScript (esm)", async () => {
89
+ writeWranglerToml();
90
+ writeWorkerSource({ format: "ts" });
91
+ mockUploadWorkerRequest({ expectedEntry: "var foo = 100;" });
92
+ mockSubDomainRequest();
93
+ await runWrangler("publish index.ts");
94
+
95
+ expect(std.out).toMatchInlineSnapshot(`
96
+ "Uploaded
97
+ test-name
98
+ (TIMINGS)
99
+ Deployed
100
+ test-name
101
+ (TIMINGS)
102
+
103
+ test-name.test-sub-domain.workers.dev"
104
+ `);
105
+ expect(std.err).toMatchInlineSnapshot(`""`);
106
+ });
107
+
108
+ it("should be able to transpile TypeScript (sw)", async () => {
109
+ writeWranglerToml();
110
+ writeWorkerSource({ format: "ts", type: "sw" });
111
+ mockUploadWorkerRequest({
112
+ expectedEntry: "var foo = 100;",
113
+ expectedType: "sw",
114
+ });
115
+ mockSubDomainRequest();
116
+ await runWrangler("publish index.ts");
117
+
118
+ expect(std.out).toMatchInlineSnapshot(`
119
+ "Uploaded
120
+ test-name
121
+ (TIMINGS)
122
+ Deployed
123
+ test-name
124
+ (TIMINGS)
125
+
126
+ test-name.test-sub-domain.workers.dev"
127
+ `);
128
+ expect(std.err).toMatchInlineSnapshot(`""`);
129
+ });
130
+
131
+ it("should inline referenced text modules into the worker", async () => {
132
+ writeWranglerToml();
133
+ fs.writeFileSync(
134
+ "./index.js",
135
+ `
136
+ import txt from './textfile.txt';
137
+ export default{
138
+ fetch(){
139
+ return new Response(txt);
140
+ }
141
+ }
142
+ `
143
+ );
144
+ fs.writeFileSync("./textfile.txt", "Hello, World!");
145
+ mockUploadWorkerRequest({ expectedEntry: "Hello, World!" });
146
+ mockSubDomainRequest();
147
+ await runWrangler("publish index.js");
148
+ expect(std.out).toMatchInlineSnapshot(`
149
+ "Uploaded
150
+ test-name
151
+ (TIMINGS)
152
+ Deployed
153
+ test-name
154
+ (TIMINGS)
155
+
156
+ test-name.test-sub-domain.workers.dev"
157
+ `);
158
+ expect(std.err).toMatchInlineSnapshot(`""`);
159
+ });
160
+
161
+ it("should be able to transpile entry-points in sub-directories (esm)", async () => {
162
+ writeWranglerToml();
163
+ writeWorkerSource({ basePath: "./src" });
164
+ mockUploadWorkerRequest({ expectedEntry: "var foo = 100;" });
165
+ mockSubDomainRequest();
166
+
167
+ await runWrangler("publish ./src/index.js");
168
+
169
+ expect(std.out).toMatchInlineSnapshot(`
170
+ "Uploaded
171
+ test-name
172
+ (TIMINGS)
173
+ Deployed
174
+ test-name
175
+ (TIMINGS)
176
+
177
+ test-name.test-sub-domain.workers.dev"
178
+ `);
179
+ expect(std.err).toMatchInlineSnapshot(`""`);
180
+ });
181
+
182
+ it("should be able to transpile entry-points in sub-directories (sw)", async () => {
183
+ writeWranglerToml();
184
+ writeWorkerSource({ basePath: "./src", type: "sw" });
185
+ mockUploadWorkerRequest({
186
+ expectedEntry: "var foo = 100;",
187
+ expectedType: "sw",
188
+ });
189
+ mockSubDomainRequest();
190
+
191
+ await runWrangler("publish ./src/index.js");
192
+
193
+ expect(std.out).toMatchInlineSnapshot(`
194
+ "Uploaded
195
+ test-name
196
+ (TIMINGS)
197
+ Deployed
198
+ test-name
199
+ (TIMINGS)
200
+
201
+ test-name.test-sub-domain.workers.dev"
202
+ `);
203
+ expect(std.err).toMatchInlineSnapshot(`""`);
204
+ });
205
+
206
+ it('should error if a site definition doesn\'t have a "bucket" field', async () => {
207
+ writeWranglerToml({
208
+ // @ts-expect-error we're purposely missing the required `site.bucket` field
209
+ site: {
210
+ "entry-point": "./index.js",
211
+ },
212
+ });
213
+ writeWorkerSource();
214
+ mockUploadWorkerRequest();
215
+ mockSubDomainRequest();
216
+
217
+ await expect(
218
+ runWrangler("publish ./index.js")
219
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
220
+ `"A [site] definition requires a \`bucket\` field with a path to the site's public directory."`
221
+ );
222
+
223
+ expect(std.out).toMatchInlineSnapshot(`""`);
224
+ expect(std.err).toMatchInlineSnapshot(`
225
+ "A [site] definition requires a \`bucket\` field with a path to the site's public directory.
226
+
227
+ %s
228
+ If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
229
+ `);
230
+ expect(std.warn).toMatchInlineSnapshot(`
231
+ "Deprecation notice: The \`site.entry-point\` config field is no longer used.
232
+ The entry-point should be specified via the command line (e.g. \`wrangler publish path/to/script\`) or the \`build.upload.main\` config field.
233
+ Please remove the \`site.entry-point\` field from the \`wrangler.toml\` file."
234
+ `);
235
+ });
236
+
237
+ it("should warn if there is a `site.entry-point` configuration", async () => {
238
+ const assets = [
239
+ { filePath: "assets/file-1.txt", content: "Content of file-1" },
240
+ { filePath: "assets/file-2.txt", content: "Content of file-2" },
241
+ ];
242
+ const kvNamespace = {
243
+ title: "__test-name-workers_sites_assets",
244
+ id: "__test-name-workers_sites_assets-id",
245
+ };
246
+
247
+ writeWranglerToml({
248
+ site: {
249
+ "entry-point": "./index.js",
250
+ bucket: "assets",
251
+ },
252
+ });
253
+ writeWorkerSource();
254
+ writeAssets(assets);
255
+ mockUploadWorkerRequest();
256
+ mockSubDomainRequest();
257
+ mockListKVNamespacesRequest(kvNamespace);
258
+ mockKeyListRequest(kvNamespace.id, []);
259
+ mockUploadAssetsToKVRequest(kvNamespace.id, assets);
260
+
261
+ await runWrangler("publish ./index.js");
262
+
263
+ expect(std.out).toMatchInlineSnapshot(`
264
+ "reading assets/file-1.txt...
265
+ uploading as assets/file-1.2ca234f380.txt...
266
+ reading assets/file-2.txt...
267
+ uploading as assets/file-2.5938485188.txt...
268
+ Uploaded
269
+ test-name
270
+ (TIMINGS)
271
+ Deployed
272
+ test-name
273
+ (TIMINGS)
274
+
275
+ test-name.test-sub-domain.workers.dev"
276
+ `);
277
+ expect(std.err).toMatchInlineSnapshot(`""`);
278
+ expect(std.warn).toMatchInlineSnapshot(`
279
+ "Deprecation notice: The \`site.entry-point\` config field is no longer used.
280
+ The entry-point should be specified via the command line (e.g. \`wrangler publish path/to/script\`) or the \`build.upload.main\` config field.
281
+ Please remove the \`site.entry-point\` field from the \`wrangler.toml\` file."
282
+ `);
283
+ });
284
+
285
+ it("should error if there is no entry-point specified", async () => {
286
+ writeWranglerToml();
287
+ writeWorkerSource();
288
+ mockUploadWorkerRequest();
289
+ mockSubDomainRequest();
290
+ await expect(
291
+ runWrangler("publish")
292
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
293
+ `"Missing entry-point: The entry-point should be specified via the command line (e.g. \`wrangler publish path/to/script\`) or the \`build.upload.main\` config field."`
294
+ );
295
+
296
+ expect(std.out).toMatchInlineSnapshot(`""`);
297
+ expect(std.err).toMatchInlineSnapshot(`
298
+ "Missing entry-point: The entry-point should be specified via the command line (e.g. \`wrangler publish path/to/script\`) or the \`build.upload.main\` config field.
299
+
300
+ %s
301
+ If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
302
+ `);
303
+ });
304
+ });
305
+
306
+ describe("asset upload", () => {
307
+ it("should upload all the files in the directory specified by `config.site.bucket`", async () => {
308
+ const assets = [
309
+ { filePath: "assets/file-1.txt", content: "Content of file-1" },
310
+ { filePath: "assets/file-2.txt", content: "Content of file-2" },
311
+ ];
312
+ const kvNamespace = {
313
+ title: "__test-name-workers_sites_assets",
314
+ id: "__test-name-workers_sites_assets-id",
315
+ };
316
+ writeWranglerToml({
317
+ build: { upload: { main: "./index.js" } },
318
+ site: {
319
+ bucket: "assets",
320
+ },
321
+ });
322
+ writeWorkerSource();
323
+ writeAssets(assets);
324
+ mockUploadWorkerRequest();
325
+ mockSubDomainRequest();
326
+ mockListKVNamespacesRequest(kvNamespace);
327
+ mockKeyListRequest(kvNamespace.id, []);
328
+ mockUploadAssetsToKVRequest(kvNamespace.id, assets);
329
+ await runWrangler("publish");
330
+
331
+ expect(std.out).toMatchInlineSnapshot(`
332
+ "reading assets/file-1.txt...
333
+ uploading as assets/file-1.2ca234f380.txt...
334
+ reading assets/file-2.txt...
335
+ uploading as assets/file-2.5938485188.txt...
336
+ Uploaded
337
+ test-name
338
+ (TIMINGS)
339
+ Deployed
340
+ test-name
341
+ (TIMINGS)
342
+
343
+ test-name.test-sub-domain.workers.dev"
344
+ `);
345
+ expect(std.err).toMatchInlineSnapshot(`""`);
346
+ });
347
+
348
+ it("when using a service worker type, it should inline an asset manifest, and bind to a namespace", async () => {
349
+ const assets = [
350
+ { filePath: "assets/file-1.txt", content: "Content of file-1" },
351
+ { filePath: "assets/file-2.txt", content: "Content of file-2" },
352
+ ];
353
+ const kvNamespace = {
354
+ title: "__test-name-workers_sites_assets",
355
+ id: "__test-name-workers_sites_assets-id",
356
+ };
357
+ writeWranglerToml({
358
+ build: { upload: { main: "./index.js" } },
359
+ site: {
360
+ bucket: "assets",
361
+ },
362
+ });
363
+ writeWorkerSource({ type: "sw" });
364
+ writeAssets(assets);
365
+ mockUploadWorkerRequest({
366
+ expectedType: "sw",
367
+ expectedEntry: `const __STATIC_CONTENT_MANIFEST = {"file-1.txt":"assets/file-1.2ca234f380.txt","file-2.txt":"assets/file-2.5938485188.txt"};`,
368
+ expectedBindings: [
369
+ {
370
+ name: "__STATIC_CONTENT",
371
+ namespace_id: "__test-name-workers_sites_assets-id",
372
+ type: "kv_namespace",
373
+ },
374
+ ],
375
+ });
376
+ mockSubDomainRequest();
377
+ mockListKVNamespacesRequest(kvNamespace);
378
+ mockKeyListRequest(kvNamespace.id, []);
379
+ mockUploadAssetsToKVRequest(kvNamespace.id, assets);
380
+
381
+ await runWrangler("publish");
382
+
383
+ expect(std.out).toMatchInlineSnapshot(`
384
+ "reading assets/file-1.txt...
385
+ uploading as assets/file-1.2ca234f380.txt...
386
+ reading assets/file-2.txt...
387
+ uploading as assets/file-2.5938485188.txt...
388
+ Uploaded
389
+ test-name
390
+ (TIMINGS)
391
+ Deployed
392
+ test-name
393
+ (TIMINGS)
394
+
395
+ test-name.test-sub-domain.workers.dev"
396
+ `);
397
+ expect(std.err).toMatchInlineSnapshot(`""`);
398
+ });
399
+
400
+ it("when using a module worker type, it should add an asset manifest module, and bind to a namespace", async () => {
401
+ const assets = [
402
+ { filePath: "assets/file-1.txt", content: "Content of file-1" },
403
+ { filePath: "assets/file-2.txt", content: "Content of file-2" },
404
+ ];
405
+ const kvNamespace = {
406
+ title: "__test-name-workers_sites_assets",
407
+ id: "__test-name-workers_sites_assets-id",
408
+ };
409
+ writeWranglerToml({
410
+ build: { upload: { main: "./index.js" } },
411
+ site: {
412
+ bucket: "assets",
413
+ },
414
+ });
415
+ writeWorkerSource({ type: "esm" });
416
+ writeAssets(assets);
417
+ mockUploadWorkerRequest({
418
+ expectedBindings: [
419
+ {
420
+ name: "__STATIC_CONTENT",
421
+ namespace_id: "__test-name-workers_sites_assets-id",
422
+ type: "kv_namespace",
423
+ },
424
+ ],
425
+ expectedModules: {
426
+ __STATIC_CONTENT_MANIFEST:
427
+ '{"file-1.txt":"assets/file-1.2ca234f380.txt","file-2.txt":"assets/file-2.5938485188.txt"}',
428
+ },
429
+ });
430
+ mockSubDomainRequest();
431
+ mockListKVNamespacesRequest(kvNamespace);
432
+ mockKeyListRequest(kvNamespace.id, []);
433
+ mockUploadAssetsToKVRequest(kvNamespace.id, assets);
434
+
435
+ await runWrangler("publish");
436
+
437
+ expect(std.out).toMatchInlineSnapshot(`
438
+ "reading assets/file-1.txt...
439
+ uploading as assets/file-1.2ca234f380.txt...
440
+ reading assets/file-2.txt...
441
+ uploading as assets/file-2.5938485188.txt...
442
+ Uploaded
443
+ test-name
444
+ (TIMINGS)
445
+ Deployed
446
+ test-name
447
+ (TIMINGS)
448
+
449
+ test-name.test-sub-domain.workers.dev"
450
+ `);
451
+ expect(std.err).toMatchInlineSnapshot(`""`);
452
+ });
453
+
454
+ it("should make environment specific kv namespace for assets", async () => {
455
+ // This is the same test as the one before this, but with an env arg
456
+ const assets = [
457
+ { filePath: "assets/file-1.txt", content: "Content of file-1" },
458
+ { filePath: "assets/file-2.txt", content: "Content of file-2" },
459
+ ];
460
+ const kvNamespace = {
461
+ title: "__test-name-some-env-workers_sites_assets",
462
+ id: "__test-name-some-env-workers_sites_assets-id",
463
+ };
464
+ writeWranglerToml({
465
+ build: { upload: { main: "./index.js" } },
466
+ site: {
467
+ bucket: "assets",
468
+ },
469
+ });
470
+ writeWorkerSource();
471
+ writeAssets(assets);
472
+ mockUploadWorkerRequest({
473
+ env: "some-env",
474
+ expectedBindings: [
475
+ {
476
+ name: "__STATIC_CONTENT",
477
+ namespace_id: "__test-name-some-env-workers_sites_assets-id",
478
+ type: "kv_namespace",
479
+ },
480
+ ],
481
+ });
482
+ mockSubDomainRequest();
483
+ mockListKVNamespacesRequest(kvNamespace);
484
+ mockKeyListRequest(kvNamespace.id, []);
485
+ mockUploadAssetsToKVRequest(kvNamespace.id, assets);
486
+ await runWrangler("publish --env some-env");
487
+
488
+ expect(std.out).toMatchInlineSnapshot(`
489
+ "reading assets/file-1.txt...
490
+ uploading as assets/file-1.2ca234f380.txt...
491
+ reading assets/file-2.txt...
492
+ uploading as assets/file-2.5938485188.txt...
493
+ Uploaded
494
+ test-name (some-env)
495
+ (TIMINGS)
496
+ Deployed
497
+ test-name (some-env)
498
+ (TIMINGS)
499
+
500
+ some-env.test-name.test-sub-domain.workers.dev"
501
+ `);
502
+ expect(std.err).toMatchInlineSnapshot(`""`);
503
+ });
504
+
505
+ it("should only upload files that are not already in the KV namespace", async () => {
506
+ const assets = [
507
+ { filePath: "assets/file-1.txt", content: "Content of file-1" },
508
+ { filePath: "assets/file-2.txt", content: "Content of file-2" },
509
+ ];
510
+ const kvNamespace = {
511
+ title: "__test-name-workers_sites_assets",
512
+ id: "__test-name-workers_sites_assets-id",
513
+ };
514
+ writeWranglerToml({
515
+ build: { upload: { main: "./index.js" } },
516
+ site: {
517
+ bucket: "assets",
518
+ },
519
+ });
520
+ writeWorkerSource();
521
+ writeAssets(assets);
522
+ mockUploadWorkerRequest();
523
+ mockSubDomainRequest();
524
+ mockListKVNamespacesRequest(kvNamespace);
525
+ // Put file-1 in the KV namespace
526
+ mockKeyListRequest(kvNamespace.id, ["assets/file-1.2ca234f380.txt"]);
527
+ // Check we do not upload file-1
528
+ mockUploadAssetsToKVRequest(
529
+ kvNamespace.id,
530
+ assets.filter((a) => a.filePath !== "assets/file-1.txt")
531
+ );
532
+ await runWrangler("publish");
533
+
534
+ expect(std.out).toMatchInlineSnapshot(`
535
+ "reading assets/file-1.txt...
536
+ skipping - already uploaded
537
+ reading assets/file-2.txt...
538
+ uploading as assets/file-2.5938485188.txt...
539
+ Uploaded
540
+ test-name
541
+ (TIMINGS)
542
+ Deployed
543
+ test-name
544
+ (TIMINGS)
545
+
546
+ test-name.test-sub-domain.workers.dev"
547
+ `);
548
+ expect(std.err).toMatchInlineSnapshot(`""`);
549
+ });
550
+
551
+ it("should only upload files that match the `site-include` arg", async () => {
552
+ const assets = [
553
+ { filePath: "assets/file-1.txt", content: "Content of file-1" },
554
+ { filePath: "assets/file-2.txt", content: "Content of file-2" },
555
+ ];
556
+ const kvNamespace = {
557
+ title: "__test-name-workers_sites_assets",
558
+ id: "__test-name-workers_sites_assets-id",
559
+ };
560
+ writeWranglerToml({
561
+ build: { upload: { main: "./index.js" } },
562
+ site: {
563
+ bucket: "assets",
564
+ },
565
+ });
566
+ writeWorkerSource();
567
+ writeAssets(assets);
568
+ mockUploadWorkerRequest();
569
+ mockSubDomainRequest();
570
+ mockListKVNamespacesRequest(kvNamespace);
571
+ mockKeyListRequest(kvNamespace.id, []);
572
+ // Check we only upload file-1
573
+ mockUploadAssetsToKVRequest(
574
+ kvNamespace.id,
575
+ assets.filter((a) => a.filePath === "assets/file-1.txt")
576
+ );
577
+ await runWrangler("publish --site-include file-1.txt");
578
+
579
+ expect(std.out).toMatchInlineSnapshot(`
580
+ "reading assets/file-1.txt...
581
+ uploading as assets/file-1.2ca234f380.txt...
582
+ Uploaded
583
+ test-name
584
+ (TIMINGS)
585
+ Deployed
586
+ test-name
587
+ (TIMINGS)
588
+
589
+ test-name.test-sub-domain.workers.dev"
590
+ `);
591
+ expect(std.err).toMatchInlineSnapshot(`""`);
592
+ });
593
+
594
+ it("should not upload files that match the `site-exclude` arg", async () => {
595
+ const assets = [
596
+ { filePath: "assets/file-1.txt", content: "Content of file-1" },
597
+ { filePath: "assets/file-2.txt", content: "Content of file-2" },
598
+ ];
599
+ const kvNamespace = {
600
+ title: "__test-name-workers_sites_assets",
601
+ id: "__test-name-workers_sites_assets-id",
602
+ };
603
+ writeWranglerToml({
604
+ build: { upload: { main: "./index.js" } },
605
+ site: {
606
+ bucket: "assets",
607
+ },
608
+ });
609
+ writeWorkerSource();
610
+ writeAssets(assets);
611
+ mockUploadWorkerRequest();
612
+ mockSubDomainRequest();
613
+ mockListKVNamespacesRequest(kvNamespace);
614
+ mockKeyListRequest(kvNamespace.id, []);
615
+ // Check we only upload file-1
616
+ mockUploadAssetsToKVRequest(
617
+ kvNamespace.id,
618
+ assets.filter((a) => a.filePath === "assets/file-1.txt")
619
+ );
620
+ await runWrangler("publish --site-exclude file-2.txt");
621
+
622
+ expect(std.out).toMatchInlineSnapshot(`
623
+ "reading assets/file-1.txt...
624
+ uploading as assets/file-1.2ca234f380.txt...
625
+ Uploaded
626
+ test-name
627
+ (TIMINGS)
628
+ Deployed
629
+ test-name
630
+ (TIMINGS)
631
+
632
+ test-name.test-sub-domain.workers.dev"
633
+ `);
634
+ expect(std.err).toMatchInlineSnapshot(`""`);
635
+ });
636
+
637
+ it("should only upload files that match the `site.include` config", async () => {
638
+ const assets = [
639
+ { filePath: "assets/file-1.txt", content: "Content of file-1" },
640
+ { filePath: "assets/file-2.txt", content: "Content of file-2" },
641
+ ];
642
+ const kvNamespace = {
643
+ title: "__test-name-workers_sites_assets",
644
+ id: "__test-name-workers_sites_assets-id",
645
+ };
646
+ writeWranglerToml({
647
+ build: { upload: { main: "./index.js" } },
648
+ site: {
649
+ bucket: "assets",
650
+ include: ["file-1.txt"],
651
+ },
652
+ });
653
+ writeWorkerSource();
654
+ writeAssets(assets);
655
+ mockUploadWorkerRequest();
656
+ mockSubDomainRequest();
657
+ mockListKVNamespacesRequest(kvNamespace);
658
+ mockKeyListRequest(kvNamespace.id, []);
659
+ // Check we only upload file-1
660
+ mockUploadAssetsToKVRequest(
661
+ kvNamespace.id,
662
+ assets.filter((a) => a.filePath === "assets/file-1.txt")
663
+ );
664
+ await runWrangler("publish");
665
+
666
+ expect(std.out).toMatchInlineSnapshot(`
667
+ "reading assets/file-1.txt...
668
+ uploading as assets/file-1.2ca234f380.txt...
669
+ Uploaded
670
+ test-name
671
+ (TIMINGS)
672
+ Deployed
673
+ test-name
674
+ (TIMINGS)
675
+
676
+ test-name.test-sub-domain.workers.dev"
677
+ `);
678
+ expect(std.err).toMatchInlineSnapshot(`""`);
679
+ });
680
+
681
+ it("should not upload files that match the `site.exclude` config", async () => {
682
+ const assets = [
683
+ { filePath: "assets/file-1.txt", content: "Content of file-1" },
684
+ { filePath: "assets/file-2.txt", content: "Content of file-2" },
685
+ ];
686
+ const kvNamespace = {
687
+ title: "__test-name-workers_sites_assets",
688
+ id: "__test-name-workers_sites_assets-id",
689
+ };
690
+ writeWranglerToml({
691
+ build: { upload: { main: "./index.js" } },
692
+ site: {
693
+ bucket: "assets",
694
+ exclude: ["file-2.txt"],
695
+ },
696
+ });
697
+ writeWorkerSource();
698
+ writeAssets(assets);
699
+ mockUploadWorkerRequest();
700
+ mockSubDomainRequest();
701
+ mockListKVNamespacesRequest(kvNamespace);
702
+ mockKeyListRequest(kvNamespace.id, []);
703
+ // Check we only upload file-1
704
+ mockUploadAssetsToKVRequest(
705
+ kvNamespace.id,
706
+ assets.filter((a) => a.filePath === "assets/file-1.txt")
707
+ );
708
+ await runWrangler("publish");
709
+
710
+ expect(std.out).toMatchInlineSnapshot(`
711
+ "reading assets/file-1.txt...
712
+ uploading as assets/file-1.2ca234f380.txt...
713
+ Uploaded
714
+ test-name
715
+ (TIMINGS)
716
+ Deployed
717
+ test-name
718
+ (TIMINGS)
719
+
720
+ test-name.test-sub-domain.workers.dev"
721
+ `);
722
+ expect(std.err).toMatchInlineSnapshot(`""`);
723
+ });
724
+
725
+ it("should use `site-include` arg over `site.include` config", async () => {
726
+ const assets = [
727
+ { filePath: "assets/file-1.txt", content: "Content of file-1" },
728
+ { filePath: "assets/file-2.txt", content: "Content of file-2" },
729
+ ];
730
+ const kvNamespace = {
731
+ title: "__test-name-workers_sites_assets",
732
+ id: "__test-name-workers_sites_assets-id",
733
+ };
734
+ writeWranglerToml({
735
+ build: { upload: { main: "./index.js" } },
736
+ site: {
737
+ bucket: "assets",
738
+ include: ["file-2.txt"],
739
+ },
740
+ });
741
+ writeWorkerSource();
742
+ writeAssets(assets);
743
+ mockUploadWorkerRequest();
744
+ mockSubDomainRequest();
745
+ mockListKVNamespacesRequest(kvNamespace);
746
+ mockKeyListRequest(kvNamespace.id, []);
747
+ // Check we only upload file-1
748
+ mockUploadAssetsToKVRequest(
749
+ kvNamespace.id,
750
+ assets.filter((a) => a.filePath === "assets/file-1.txt")
751
+ );
752
+ await runWrangler("publish --site-include file-1.txt");
753
+
754
+ expect(std.out).toMatchInlineSnapshot(`
755
+ "reading assets/file-1.txt...
756
+ uploading as assets/file-1.2ca234f380.txt...
757
+ Uploaded
758
+ test-name
759
+ (TIMINGS)
760
+ Deployed
761
+ test-name
762
+ (TIMINGS)
763
+
764
+ test-name.test-sub-domain.workers.dev"
765
+ `);
766
+ expect(std.err).toMatchInlineSnapshot(`""`);
767
+ });
768
+
769
+ it("should use `site-exclude` arg over `site.exclude` config", async () => {
770
+ const assets = [
771
+ { filePath: "assets/file-1.txt", content: "Content of file-1" },
772
+ { filePath: "assets/file-2.txt", content: "Content of file-2" },
773
+ ];
774
+ const kvNamespace = {
775
+ title: "__test-name-workers_sites_assets",
776
+ id: "__test-name-workers_sites_assets-id",
777
+ };
778
+ writeWranglerToml({
779
+ build: { upload: { main: "./index.js" } },
780
+ site: {
781
+ bucket: "assets",
782
+ exclude: ["assets/file-1.txt"],
783
+ },
784
+ });
785
+ writeWorkerSource();
786
+ writeAssets(assets);
787
+ mockUploadWorkerRequest();
788
+ mockSubDomainRequest();
789
+ mockListKVNamespacesRequest(kvNamespace);
790
+ mockKeyListRequest(kvNamespace.id, []);
791
+ // Check we only upload file-1
792
+ mockUploadAssetsToKVRequest(
793
+ kvNamespace.id,
794
+ assets.filter((a) => a.filePath.endsWith("assets/file-1.txt"))
795
+ );
796
+ await runWrangler("publish --site-exclude file-2.txt");
797
+
798
+ expect(std.out).toMatchInlineSnapshot(`
799
+ "reading assets/file-1.txt...
800
+ uploading as assets/file-1.2ca234f380.txt...
801
+ Uploaded
802
+ test-name
803
+ (TIMINGS)
804
+ Deployed
805
+ test-name
806
+ (TIMINGS)
807
+
808
+ test-name.test-sub-domain.workers.dev"
809
+ `);
810
+ expect(std.err).toMatchInlineSnapshot(`""`);
811
+ });
812
+
813
+ it("should walk directories except node_modules", async () => {
814
+ const assets = [
815
+ {
816
+ filePath: "assets/directory-1/file-1.txt",
817
+ content: "Content of file-1",
818
+ },
819
+ {
820
+ filePath: "assets/node_modules/file-2.txt",
821
+ content: "Content of file-2",
822
+ },
823
+ ];
824
+ const kvNamespace = {
825
+ title: "__test-name-workers_sites_assets",
826
+ id: "__test-name-workers_sites_assets-id",
827
+ };
828
+ writeWranglerToml({
829
+ build: { upload: { main: "./index.js" } },
830
+ site: {
831
+ bucket: "assets",
832
+ },
833
+ });
834
+ writeWorkerSource();
835
+ writeAssets(assets);
836
+ mockUploadWorkerRequest();
837
+ mockSubDomainRequest();
838
+ mockListKVNamespacesRequest(kvNamespace);
839
+ mockKeyListRequest(kvNamespace.id, []);
840
+ // Only expect file-1 to be uploaded
841
+ mockUploadAssetsToKVRequest(kvNamespace.id, assets.slice(0, 1));
842
+ await runWrangler("publish");
843
+
844
+ expect(std.out).toMatchInlineSnapshot(`
845
+ "reading assets/directory-1/file-1.txt...
846
+ uploading as assets/directory-1/file-1.2ca234f380.txt...
847
+ Uploaded
848
+ test-name
849
+ (TIMINGS)
850
+ Deployed
851
+ test-name
852
+ (TIMINGS)
853
+
854
+ test-name.test-sub-domain.workers.dev"
855
+ `);
856
+ expect(std.err).toMatchInlineSnapshot(`""`);
857
+ });
858
+
859
+ it("should skip hidden files and directories except `.well-known`", async () => {
860
+ const assets = [
861
+ {
862
+ filePath: "assets/.hidden-file.txt",
863
+ content: "Content of hidden-file",
864
+ },
865
+ {
866
+ filePath: "assets/.hidden/file-1.txt",
867
+ content: "Content of file-1",
868
+ },
869
+ {
870
+ filePath: "assets/.well-known/file-2.txt",
871
+ content: "Content of file-2",
872
+ },
873
+ ];
874
+ const kvNamespace = {
875
+ title: "__test-name-workers_sites_assets",
876
+ id: "__test-name-workers_sites_assets-id",
877
+ };
878
+ writeWranglerToml({
879
+ build: { upload: { main: "./index.js" } },
880
+ site: {
881
+ bucket: "assets",
882
+ },
883
+ });
884
+ writeWorkerSource();
885
+ writeAssets(assets);
886
+ mockUploadWorkerRequest();
887
+ mockSubDomainRequest();
888
+ mockListKVNamespacesRequest(kvNamespace);
889
+ mockKeyListRequest(kvNamespace.id, []);
890
+ // Only expect file-2 to be uploaded
891
+ mockUploadAssetsToKVRequest(kvNamespace.id, assets.slice(2));
892
+ await runWrangler("publish");
893
+
894
+ expect(std.out).toMatchInlineSnapshot(`
895
+ "reading assets/.well-known/file-2.txt...
896
+ uploading as assets/.well-known/file-2.5938485188.txt...
897
+ Uploaded
898
+ test-name
899
+ (TIMINGS)
900
+ Deployed
901
+ test-name
902
+ (TIMINGS)
903
+
904
+ test-name.test-sub-domain.workers.dev"
905
+ `);
906
+ expect(std.err).toMatchInlineSnapshot(`""`);
907
+ });
908
+
909
+ it("should error if the asset is over 25Mb", async () => {
910
+ const assets = [
911
+ {
912
+ filePath: "assets/large-file.txt",
913
+ // This file is greater than 25MiB when base64 encoded but small enough to be uploaded.
914
+ content: "X".repeat(25 * 1024 * 1024 * 0.8 + 1),
915
+ },
916
+ {
917
+ filePath: "assets/too-large-file.txt",
918
+ content: "X".repeat(25 * 1024 * 1024 + 1),
919
+ },
920
+ ];
921
+ const kvNamespace = {
922
+ title: "__test-name-workers_sites_assets",
923
+ id: "__test-name-workers_sites_assets-id",
924
+ };
925
+ writeWranglerToml({
926
+ build: { upload: { main: "./index.js" } },
927
+ site: {
928
+ bucket: "assets",
929
+ exclude: ["assets/file-1.txt"],
930
+ },
931
+ });
932
+ writeWorkerSource();
933
+ writeAssets(assets);
934
+ mockUploadWorkerRequest();
935
+ mockSubDomainRequest();
936
+ mockListKVNamespacesRequest(kvNamespace);
937
+ mockKeyListRequest(kvNamespace.id, []);
938
+
939
+ await expect(
940
+ runWrangler("publish")
941
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
942
+ `"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"`
943
+ );
944
+
945
+ expect(std.out).toMatchInlineSnapshot(`
946
+ "reading assets/large-file.txt...
947
+ uploading as assets/large-file.0ea0637a45.txt..."
948
+ `);
949
+ expect(std.err).toMatchInlineSnapshot(`
950
+ "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
951
+
952
+ %s
953
+ If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
954
+ `);
955
+ });
956
+
957
+ it("should error if the asset key is over 512 characters", async () => {
958
+ const longFilePathAsset = {
959
+ filePath: "assets/" + "folder/".repeat(100) + "file.txt",
960
+ content: "content of file",
961
+ };
962
+ const kvNamespace = {
963
+ title: "__test-name-workers_sites_assets",
964
+ id: "__test-name-workers_sites_assets-id",
965
+ };
966
+ writeWranglerToml({
967
+ build: { upload: { main: "./index.js" } },
968
+ site: {
969
+ bucket: "assets",
970
+ },
971
+ });
972
+ writeWorkerSource();
973
+ writeAssets([longFilePathAsset]);
974
+ mockUploadWorkerRequest();
975
+ mockSubDomainRequest();
976
+ mockListKVNamespacesRequest(kvNamespace);
977
+ mockKeyListRequest(kvNamespace.id, []);
978
+
979
+ await expect(
980
+ runWrangler("publish")
981
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
982
+ `"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\\","`
983
+ );
984
+
985
+ expect(std.out).toMatchInlineSnapshot(
986
+ `"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..."`
987
+ );
988
+ expect(std.err).toMatchInlineSnapshot(`
989
+ "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\\",
990
+
991
+ %s
992
+ If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
993
+ `);
994
+ });
995
+ });
996
+
997
+ describe("custom builds", () => {
998
+ it("should run a custom build before publishing", async () => {
999
+ writeWranglerToml({
1000
+ build: {
1001
+ command: `node -e "console.log('custom build'); require('fs').writeFileSync('index.js', 'export default { fetch(){ return new Response(123) } }')"`,
1002
+ },
1003
+ });
1004
+
1005
+ mockUploadWorkerRequest({
1006
+ expectedEntry: "return new Response(123)",
1007
+ });
1008
+ mockSubDomainRequest();
1009
+
1010
+ await runWrangler("publish index.js");
1011
+ expect(std.out).toMatchInlineSnapshot(`
1012
+ "running:
1013
+ node -e \\"console.log('custom build'); require('fs').writeFileSync('index.js', 'export default { fetch(){ return new Response(123) } }')\\"
1014
+ Uploaded
1015
+ test-name
1016
+ (TIMINGS)
1017
+ Deployed
1018
+ test-name
1019
+ (TIMINGS)
1020
+
1021
+ test-name.test-sub-domain.workers.dev"
1022
+ `);
1023
+ expect(std.err).toMatchInlineSnapshot(`""`);
1024
+ expect(std.warn).toMatchInlineSnapshot(`""`);
1025
+ });
1026
+
1027
+ if (process.platform !== "win32") {
1028
+ it("should run a custom build of multiple steps combined by && before publishing", async () => {
1029
+ writeWranglerToml({
1030
+ build: {
1031
+ command: `echo "custom build" && echo "export default { fetch(){ return new Response(123) } }" > index.js`,
1032
+ },
1033
+ });
1034
+
1035
+ mockUploadWorkerRequest({
1036
+ expectedEntry: "return new Response(123)",
1037
+ });
1038
+ mockSubDomainRequest();
1039
+
1040
+ await runWrangler("publish index.js");
1041
+ expect(std.out).toMatchInlineSnapshot(`
1042
+ "running:
1043
+ echo \\"custom build\\" && echo \\"export default { fetch(){ return new Response(123) } }\\" > index.js
1044
+ Uploaded
1045
+ test-name
1046
+ (TIMINGS)
1047
+ Deployed
1048
+ test-name
1049
+ (TIMINGS)
1050
+
1051
+ test-name.test-sub-domain.workers.dev"
1052
+ `);
1053
+ expect(std.err).toMatchInlineSnapshot(`""`);
1054
+ expect(std.warn).toMatchInlineSnapshot(`""`);
1055
+ });
1056
+ }
1057
+ });
1058
+ });
1059
+
1060
+ /** Write a mock wrangler.toml file to disk. */
1061
+ function writeWranglerToml(config: Omit<Config, "env"> = {}) {
1062
+ // We Omit `env` from config because TOML.stringify() appears to
1063
+ // have a weird type signature that appears to fail. We'll revisit this
1064
+ // when we write tests for publishing environments
1065
+ fs.writeFileSync(
1066
+ "./wrangler.toml",
1067
+ TOML.stringify({
1068
+ compatibility_date: "2022-01-12",
1069
+ name: "test-name",
1070
+ ...config,
1071
+ }),
1072
+
1073
+ "utf-8"
1074
+ );
1075
+ }
1076
+
1077
+ /** Write a mock Worker script to disk. */
1078
+ function writeWorkerSource({
1079
+ basePath = ".",
1080
+ format = "js",
1081
+ type = "esm",
1082
+ }: {
1083
+ basePath?: string;
1084
+ format?: "js" | "ts" | "jsx" | "tsx" | "mjs";
1085
+ type?: "esm" | "sw";
1086
+ } = {}) {
1087
+ if (basePath !== ".") {
1088
+ fs.mkdirSync(basePath, { recursive: true });
1089
+ }
1090
+ fs.writeFileSync(
1091
+ `${basePath}/index.${format}`,
1092
+ type === "esm"
1093
+ ? `import { foo } from "./another";
1094
+ export default {
1095
+ async fetch(request) {
1096
+ return new Response('Hello' + foo);
1097
+ },
1098
+ };`
1099
+ : `import { foo } from "./another";
1100
+ addEventListener('fetch', event => {
1101
+ event.respondWith(new Response('Hello' + foo));
1102
+ })`
1103
+ );
1104
+ fs.writeFileSync(`${basePath}/another.${format}`, `export const foo = 100;`);
1105
+ }
1106
+
1107
+ /** Write mock assets to the file system so they can be uploaded. */
1108
+ function writeAssets(assets: { filePath: string; content: string }[]) {
1109
+ for (const asset of assets) {
1110
+ fs.mkdirSync(path.dirname(asset.filePath), { recursive: true });
1111
+ fs.writeFileSync(asset.filePath, asset.content);
1112
+ }
1113
+ }
1114
+
1115
+ /** Create a mock handler for the request to upload a worker script. */
1116
+ function mockUploadWorkerRequest({
1117
+ available_on_subdomain = true,
1118
+ expectedEntry,
1119
+ expectedType = "esm",
1120
+ expectedBindings,
1121
+ expectedModules = {},
1122
+ env = undefined,
1123
+ }: {
1124
+ available_on_subdomain?: boolean;
1125
+ expectedEntry?: string;
1126
+ expectedType?: "esm" | "sw";
1127
+ expectedBindings?: unknown;
1128
+ expectedModules?: Record<string, string>;
1129
+ env?: string | undefined;
1130
+ } = {}) {
1131
+ setMockResponse(
1132
+ env
1133
+ ? `/accounts/:accountId/workers/services/:scriptName/environments/:envName`
1134
+ : "/accounts/:accountId/workers/scripts/:scriptName",
1135
+ "PUT",
1136
+ async ([_url, accountId, scriptName], { body }, queryParams) => {
1137
+ expect(accountId).toEqual("some-account-id");
1138
+ expect(scriptName).toEqual("test-name");
1139
+ expect(queryParams.get("available_on_subdomains")).toEqual("true");
1140
+ const formBody = body as FormData;
1141
+ if (expectedEntry !== undefined) {
1142
+ expect(await (formBody.get("index.js") as File).text()).toMatch(
1143
+ expectedEntry
1144
+ );
1145
+ }
1146
+
1147
+ const metadata = JSON.parse(
1148
+ formBody.get("metadata") as string
1149
+ ) as WorkerMetadata;
1150
+ if (expectedType === "esm") {
1151
+ expect(metadata.main_module).toEqual("index.js");
1152
+ } else {
1153
+ expect(metadata.body_part).toEqual("index.js");
1154
+ }
1155
+ if (expectedBindings !== undefined) {
1156
+ expect(metadata.bindings).toEqual(expectedBindings);
1157
+ }
1158
+ for (const [name, content] of Object.entries(expectedModules)) {
1159
+ expect(await (formBody.get(name) as File).text()).toMatch(content);
1160
+ }
1161
+
1162
+ return { available_on_subdomain };
1163
+ }
1164
+ );
1165
+ }
1166
+
1167
+ /** Create a mock handler for the request to get the account's subdomain. */
1168
+ function mockSubDomainRequest(subdomain = "test-sub-domain") {
1169
+ setMockResponse("/accounts/:accountId/workers/subdomain", () => {
1170
+ return { subdomain };
1171
+ });
1172
+ }
1173
+
1174
+ /** Create a mock handler for the request to get a list of all KV namespaces. */
1175
+ function mockListKVNamespacesRequest(...namespaces: KVNamespaceInfo[]) {
1176
+ setMockResponse(
1177
+ "/accounts/:accountId/storage/kv/namespaces",
1178
+ "GET",
1179
+ ([_url, accountId]) => {
1180
+ expect(accountId).toEqual("some-account-id");
1181
+ return namespaces;
1182
+ }
1183
+ );
1184
+ }
1185
+
1186
+ /** Create a mock handler for the request that tries to do a bulk upload of assets to a KV namespace. */
1187
+ function mockUploadAssetsToKVRequest(
1188
+ expectedNamespaceId: string,
1189
+ assets: { filePath: string; content: string }[]
1190
+ ) {
1191
+ setMockResponse(
1192
+ "/accounts/:accountId/storage/kv/namespaces/:namespaceId/bulk",
1193
+ "PUT",
1194
+ ([_url, accountId, namespaceId], { body }) => {
1195
+ expect(accountId).toEqual("some-account-id");
1196
+ expect(namespaceId).toEqual(expectedNamespaceId);
1197
+ const uploads = JSON.parse(body as string);
1198
+ expect(assets.length).toEqual(uploads.length);
1199
+ for (let i = 0; i < uploads.length; i++) {
1200
+ const asset = assets[i];
1201
+ const upload = uploads[i];
1202
+ // The asset key consists of:
1203
+ // - the basename of the filepath
1204
+ // - some hash value
1205
+ // - the extension
1206
+ const keyMatcher = new RegExp(
1207
+ "^" +
1208
+ asset.filePath
1209
+ .replace(/(\.[^.]+)$/, ".[a-z0-9]+$1")
1210
+ .replace(/\./g, "\\.")
1211
+ );
1212
+ expect(upload.key).toMatch(keyMatcher);
1213
+ // The asset value is base64 encoded.
1214
+ expect(upload.base64).toBe(true);
1215
+ expect(Buffer.from(upload.value, "base64").toString()).toEqual(
1216
+ asset.content
1217
+ );
1218
+ }
1219
+ return null;
1220
+ }
1221
+ );
1222
+ }