wrangler 0.0.0-e61907d → 0.0.0-e95bea2

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