wrangler 0.0.0-e5d3690 → 0.0.0-e88a54e

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