wrangler 0.0.0-e928f94 → 0.0.0-e978986

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 (124) hide show
  1. package/Cloudflare_CA.pem +18 -0
  2. package/README.md +27 -14
  3. package/bin/wrangler.js +48 -4
  4. package/miniflare-dist/index.mjs +4886 -0
  5. package/package.json +74 -34
  6. package/pages/functions/buildPlugin.ts +105 -0
  7. package/pages/functions/buildWorker.ts +151 -0
  8. package/pages/functions/filepath-routing.test.ts +234 -0
  9. package/pages/functions/filepath-routing.ts +189 -0
  10. package/pages/functions/identifiers.ts +78 -0
  11. package/pages/functions/routes.ts +156 -0
  12. package/pages/functions/template-plugin.ts +147 -0
  13. package/pages/functions/template-worker.ts +143 -0
  14. package/src/__tests__/config-cache.test.ts +36 -0
  15. package/src/__tests__/configuration.test.ts +3374 -0
  16. package/src/__tests__/dev.test.tsx +980 -0
  17. package/src/__tests__/guess-worker-format.test.ts +85 -0
  18. package/src/__tests__/{clipboardy-mock.js → helpers/clipboardy-mock.js} +0 -0
  19. package/src/__tests__/helpers/cmd-shim.d.ts +11 -0
  20. package/src/__tests__/helpers/faye-websocket.d.ts +6 -0
  21. package/src/__tests__/helpers/mock-account-id.ts +48 -0
  22. package/src/__tests__/helpers/mock-bin.ts +36 -0
  23. package/src/__tests__/helpers/mock-cfetch.ts +211 -0
  24. package/src/__tests__/helpers/mock-console.ts +87 -0
  25. package/src/__tests__/helpers/mock-dialogs.ts +130 -0
  26. package/src/__tests__/helpers/mock-http-server.ts +46 -0
  27. package/src/__tests__/helpers/mock-istty.ts +27 -0
  28. package/src/__tests__/helpers/mock-kv.ts +33 -0
  29. package/src/__tests__/helpers/mock-oauth-flow.ts +255 -0
  30. package/src/__tests__/helpers/mock-stdin.ts +103 -0
  31. package/src/__tests__/helpers/mock-web-socket.ts +29 -0
  32. package/src/__tests__/helpers/run-in-tmp.ts +39 -0
  33. package/src/__tests__/helpers/run-wrangler.ts +16 -0
  34. package/src/__tests__/helpers/write-worker-source.ts +31 -0
  35. package/src/__tests__/helpers/write-wrangler-toml.ts +17 -0
  36. package/src/__tests__/https-options.test.ts +130 -0
  37. package/src/__tests__/index.test.ts +233 -130
  38. package/src/__tests__/init.test.ts +1567 -0
  39. package/src/__tests__/jest.setup.ts +77 -0
  40. package/src/__tests__/kv.test.ts +1481 -0
  41. package/src/__tests__/logger.test.ts +115 -0
  42. package/src/__tests__/package-manager.test.ts +353 -0
  43. package/src/__tests__/pages.test.ts +657 -0
  44. package/src/__tests__/parse.test.ts +330 -0
  45. package/src/__tests__/publish.test.ts +5468 -0
  46. package/src/__tests__/r2.test.ts +233 -0
  47. package/src/__tests__/route.test.ts +45 -0
  48. package/src/__tests__/secret.test.ts +481 -0
  49. package/src/__tests__/tail.test.ts +690 -0
  50. package/src/__tests__/user.test.ts +131 -0
  51. package/src/__tests__/whoami.test.tsx +105 -0
  52. package/src/abort.d.ts +3 -0
  53. package/src/bundle.ts +200 -0
  54. package/src/cfetch/index.ts +121 -0
  55. package/src/cfetch/internal.ts +145 -0
  56. package/src/cli.ts +10 -7
  57. package/src/config/README.md +107 -0
  58. package/src/config/config.ts +234 -0
  59. package/src/config/diagnostics.ts +80 -0
  60. package/src/config/environment.ts +420 -0
  61. package/src/config/index.ts +225 -0
  62. package/src/config/validation-helpers.ts +575 -0
  63. package/src/config/validation.ts +1539 -0
  64. package/src/config-cache.ts +45 -0
  65. package/src/create-worker-preview.ts +185 -0
  66. package/src/create-worker-upload-form.ts +254 -0
  67. package/src/dev/dev-vars.ts +36 -0
  68. package/src/dev/dev.tsx +419 -0
  69. package/src/dev/local.tsx +318 -0
  70. package/src/dev/remote.tsx +227 -0
  71. package/src/dev/use-esbuild.ts +118 -0
  72. package/src/dialogs.tsx +67 -17
  73. package/src/durable.ts +102 -0
  74. package/src/entry.ts +322 -0
  75. package/src/environment-variables.ts +35 -0
  76. package/src/errors.ts +11 -0
  77. package/src/https-options.ts +123 -0
  78. package/src/index.tsx +2031 -1041
  79. package/src/inspect.ts +662 -0
  80. package/src/intl-polyfill.d.ts +139 -0
  81. package/src/kv.ts +408 -0
  82. package/src/logger.ts +66 -0
  83. package/src/miniflare-cli/README.md +30 -0
  84. package/src/miniflare-cli/enum-keys.ts +17 -0
  85. package/src/miniflare-cli/index.ts +41 -0
  86. package/src/miniflare-cli/request-context.ts +40 -0
  87. package/src/module-collection.ts +229 -35
  88. package/src/open-in-browser.ts +19 -0
  89. package/src/package-manager.ts +174 -0
  90. package/src/pages.tsx +1444 -260
  91. package/src/parse.ts +202 -0
  92. package/src/paths.ts +26 -0
  93. package/src/proxy.ts +465 -0
  94. package/src/publish.ts +498 -261
  95. package/src/r2.ts +50 -0
  96. package/src/selfsigned.d.ts +29 -0
  97. package/src/sites.tsx +240 -60
  98. package/src/tail/filters.ts +279 -0
  99. package/src/tail/index.ts +299 -0
  100. package/src/tail/printing.ts +67 -0
  101. package/src/update-check.ts +19 -0
  102. package/src/user.tsx +395 -221
  103. package/src/whoami.tsx +70 -0
  104. package/src/worker.ts +180 -0
  105. package/templates/gitignore +171 -0
  106. package/templates/new-worker-scheduled.js +17 -0
  107. package/templates/new-worker-scheduled.ts +32 -0
  108. package/templates/new-worker.js +15 -0
  109. package/templates/new-worker.ts +30 -0
  110. package/{static-asset-facade.js → templates/static-asset-facade.js} +1 -1
  111. package/templates/tsconfig.json +105 -0
  112. package/wrangler-dist/cli.js +118298 -116056
  113. package/src/__tests__/fixtures/init/.gitkeep +0 -0
  114. package/src/__tests__/mock-cfetch.js +0 -46
  115. package/src/api/form_data.ts +0 -141
  116. package/src/api/inspect.ts +0 -441
  117. package/src/api/preview.ts +0 -128
  118. package/src/api/worker.ts +0 -161
  119. package/src/cfetch.ts +0 -72
  120. package/src/config.ts +0 -122
  121. package/src/dev.tsx +0 -724
  122. package/src/kv.tsx +0 -211
  123. package/src/tail.tsx +0 -71
  124. package/wrangler-dist/cli.js.map +0 -7
@@ -0,0 +1,980 @@
1
+ import * as fs from "node:fs";
2
+ import getPort from "get-port";
3
+ import patchConsole from "patch-console";
4
+ import dedent from "ts-dedent";
5
+ import Dev from "../dev/dev";
6
+ import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
7
+ import { setMockResponse, unsetAllMocks } from "./helpers/mock-cfetch";
8
+ import { mockConsoleMethods } from "./helpers/mock-console";
9
+ import { runInTempDir } from "./helpers/run-in-tmp";
10
+ import { runWrangler } from "./helpers/run-wrangler";
11
+ import writeWranglerToml from "./helpers/write-wrangler-toml";
12
+
13
+ describe("wrangler dev", () => {
14
+ mockAccountId();
15
+ mockApiToken();
16
+ runInTempDir();
17
+ const std = mockConsoleMethods();
18
+ afterEach(() => {
19
+ (Dev as jest.Mock).mockClear();
20
+ patchConsole(() => {});
21
+ unsetAllMocks();
22
+ });
23
+
24
+ describe("compatibility-date", () => {
25
+ it("should not warn if there is no wrangler.toml and no compatibility-date specified", async () => {
26
+ fs.writeFileSync("index.js", `export default {};`);
27
+ await runWrangler("dev index.js");
28
+ expect(std.out).toMatchInlineSnapshot(`""`);
29
+ expect(std.warn).toMatchInlineSnapshot(`""`);
30
+ expect(std.err).toMatchInlineSnapshot(`""`);
31
+ });
32
+
33
+ it("should warn if there is a wrangler.toml but no compatibility-date", async () => {
34
+ writeWranglerToml({
35
+ main: "index.js",
36
+ compatibility_date: undefined,
37
+ });
38
+ fs.writeFileSync("index.js", `export default {};`);
39
+ await runWrangler("dev");
40
+ const currentDate = new Date().toISOString().substring(0, 10);
41
+ expect(std.out).toMatchInlineSnapshot(`""`);
42
+ expect(std.warn.replaceAll(currentDate, "<current-date>"))
43
+ .toMatchInlineSnapshot(`
44
+ "▲ [WARNING] No compatibility_date was specified. Using today's date: <current-date>.
45
+
46
+ Add one to your wrangler.toml file:
47
+ \`\`\`
48
+ compatibility_date = \\"<current-date>\\"
49
+ \`\`\`
50
+ or pass it in your terminal:
51
+ \`\`\`
52
+ --compatibility-date=<current-date>
53
+ \`\`\`
54
+ See https://developers.cloudflare.com/workers/platform/compatibility-dates for more information.
55
+
56
+ "
57
+ `);
58
+ expect(std.err).toMatchInlineSnapshot(`""`);
59
+ });
60
+
61
+ it("should not warn if there is a wrangler.toml but compatibility-date is specified at the command line", async () => {
62
+ writeWranglerToml({
63
+ main: "index.js",
64
+ compatibility_date: undefined,
65
+ });
66
+ fs.writeFileSync("index.js", `export default {};`);
67
+ await runWrangler("dev --compatibility-date=2020-01-01");
68
+ expect(std.out).toMatchInlineSnapshot(`""`);
69
+ expect(std.warn).toMatchInlineSnapshot(`""`);
70
+ expect(std.err).toMatchInlineSnapshot(`""`);
71
+ });
72
+ });
73
+
74
+ describe("entry-points", () => {
75
+ it("should error if there is no entry-point specified", async () => {
76
+ writeWranglerToml();
77
+
78
+ await expect(
79
+ runWrangler("dev")
80
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
81
+ `"Missing entry-point: The entry-point should be specified via the command line (e.g. \`wrangler dev path/to/script\`) or the \`main\` config field."`
82
+ );
83
+
84
+ expect(std.out).toMatchInlineSnapshot(`
85
+ "
86
+ If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choose"
87
+ `);
88
+ expect(std.err).toMatchInlineSnapshot(`
89
+ "X [ERROR] Missing entry-point: The entry-point should be specified via the command line (e.g. \`wrangler dev path/to/script\`) or the \`main\` config field.
90
+
91
+ "
92
+ `);
93
+ });
94
+
95
+ it("should use `main` from the top-level environment", async () => {
96
+ writeWranglerToml({
97
+ main: "index.js",
98
+ });
99
+ fs.writeFileSync("index.js", `export default {};`);
100
+ await runWrangler("dev");
101
+ expect((Dev as jest.Mock).mock.calls[0][0].entry.file).toMatch(
102
+ /index\.js$/
103
+ );
104
+ expect(std.out).toMatchInlineSnapshot(`""`);
105
+ expect(std.warn).toMatchInlineSnapshot(`""`);
106
+ expect(std.err).toMatchInlineSnapshot(`""`);
107
+ });
108
+
109
+ it("should use `main` from a named environment", async () => {
110
+ writeWranglerToml({
111
+ env: {
112
+ ENV1: {
113
+ main: "index.js",
114
+ },
115
+ },
116
+ });
117
+ fs.writeFileSync("index.js", `export default {};`);
118
+ await runWrangler("dev --env=ENV1");
119
+ expect((Dev as jest.Mock).mock.calls[0][0].entry.file).toMatch(
120
+ /index\.js$/
121
+ );
122
+ expect(std.out).toMatchInlineSnapshot(`""`);
123
+ expect(std.warn).toMatchInlineSnapshot(`""`);
124
+ expect(std.err).toMatchInlineSnapshot(`""`);
125
+ });
126
+
127
+ it("should use `main` from a named environment, rather than the top-level", async () => {
128
+ writeWranglerToml({
129
+ main: "other.js",
130
+ env: {
131
+ ENV1: {
132
+ main: "index.js",
133
+ },
134
+ },
135
+ });
136
+ fs.writeFileSync("index.js", `export default {};`);
137
+ await runWrangler("dev --env=ENV1");
138
+ expect((Dev as jest.Mock).mock.calls[0][0].entry.file).toMatch(
139
+ /index\.js$/
140
+ );
141
+ expect(std.out).toMatchInlineSnapshot(`""`);
142
+ expect(std.warn).toMatchInlineSnapshot(`""`);
143
+ expect(std.err).toMatchInlineSnapshot(`""`);
144
+ });
145
+ });
146
+
147
+ describe("host", () => {
148
+ it("should resolve a host to its zone", async () => {
149
+ writeWranglerToml({
150
+ main: "index.js",
151
+ });
152
+ fs.writeFileSync("index.js", `export default {};`);
153
+ mockGetZones("some-host.com", [{ id: "some-zone-id" }]);
154
+ await runWrangler("dev --host some-host.com");
155
+ expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
156
+ host: "some-host.com",
157
+ id: "some-zone-id",
158
+ });
159
+ });
160
+
161
+ it("should read wrangler.toml's dev.host", async () => {
162
+ writeWranglerToml({
163
+ main: "index.js",
164
+ dev: {
165
+ host: "some-host.com",
166
+ },
167
+ });
168
+ fs.writeFileSync("index.js", `export default {};`);
169
+ mockGetZones("some-host.com", [{ id: "some-zone-id" }]);
170
+ await runWrangler("dev");
171
+ expect((Dev as jest.Mock).mock.calls[0][0].zone.host).toEqual(
172
+ "some-host.com"
173
+ );
174
+ });
175
+
176
+ it("should read --route", async () => {
177
+ writeWranglerToml({
178
+ main: "index.js",
179
+ });
180
+ fs.writeFileSync("index.js", `export default {};`);
181
+ mockGetZones("some-host.com", [{ id: "some-zone-id" }]);
182
+ await runWrangler("dev --route http://some-host.com/some/path/*");
183
+ expect((Dev as jest.Mock).mock.calls[0][0].zone.host).toEqual(
184
+ "some-host.com"
185
+ );
186
+ });
187
+
188
+ it("should read wrangler.toml's routes", async () => {
189
+ writeWranglerToml({
190
+ main: "index.js",
191
+ routes: [
192
+ "http://some-host.com/some/path/*",
193
+ "http://some-other-host.com/path/*",
194
+ ],
195
+ });
196
+ fs.writeFileSync("index.js", `export default {};`);
197
+ mockGetZones("some-host.com", [{ id: "some-zone-id" }]);
198
+ await runWrangler("dev");
199
+ expect((Dev as jest.Mock).mock.calls[0][0].zone.host).toEqual(
200
+ "some-host.com"
201
+ );
202
+ });
203
+
204
+ it("should read wrangler.toml's environment specific routes", async () => {
205
+ writeWranglerToml({
206
+ main: "index.js",
207
+ routes: [
208
+ "http://a-host.com/some/path/*",
209
+ "http://another-host.com/path/*",
210
+ ],
211
+ env: {
212
+ staging: {
213
+ routes: [
214
+ "http://some-host.com/some/path/*",
215
+ "http://some-other-host.com/path/*",
216
+ ],
217
+ },
218
+ },
219
+ });
220
+ fs.writeFileSync("index.js", `export default {};`);
221
+ mockGetZones("some-host.com", [{ id: "some-zone-id" }]);
222
+ await runWrangler("dev --env staging");
223
+ expect((Dev as jest.Mock).mock.calls[0][0].zone.host).toEqual(
224
+ "some-host.com"
225
+ );
226
+ });
227
+
228
+ it("should strip leading `*` from given host when deducing a zone id", async () => {
229
+ writeWranglerToml({
230
+ main: "index.js",
231
+ route: "*some-host.com/some/path/*",
232
+ });
233
+ fs.writeFileSync("index.js", `export default {};`);
234
+ mockGetZones("some-host.com", [{ id: "some-zone-id" }]);
235
+ await runWrangler("dev");
236
+ expect((Dev as jest.Mock).mock.calls[0][0].zone.host).toEqual(
237
+ "some-host.com"
238
+ );
239
+ });
240
+
241
+ it("should strip leading `*.` from given host when deducing a zone id", async () => {
242
+ writeWranglerToml({
243
+ main: "index.js",
244
+ route: "*.some-host.com/some/path/*",
245
+ });
246
+ fs.writeFileSync("index.js", `export default {};`);
247
+ mockGetZones("some-host.com", [{ id: "some-zone-id" }]);
248
+ await runWrangler("dev");
249
+ expect((Dev as jest.Mock).mock.calls[0][0].zone.host).toEqual(
250
+ "some-host.com"
251
+ );
252
+ });
253
+
254
+ it("should, when provided, use a configured zone_id", async () => {
255
+ writeWranglerToml({
256
+ main: "index.js",
257
+ routes: [
258
+ { pattern: "https://some-domain.com/*", zone_id: "some-zone-id" },
259
+ ],
260
+ });
261
+ fs.writeFileSync("index.js", `export default {};`);
262
+ await runWrangler("dev");
263
+ expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
264
+ host: "some-domain.com",
265
+ id: "some-zone-id",
266
+ });
267
+ });
268
+
269
+ it("should, when provided, use a zone_name to get a zone_id", async () => {
270
+ writeWranglerToml({
271
+ main: "index.js",
272
+ routes: [
273
+ { pattern: "https://some-domain.com/*", zone_name: "some-zone.com" },
274
+ ],
275
+ });
276
+ fs.writeFileSync("index.js", `export default {};`);
277
+ mockGetZones("some-zone.com", [{ id: "a-zone-id" }]);
278
+ await runWrangler("dev");
279
+ expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
280
+ // note that it uses the provided zone_name as a host too
281
+ host: "some-zone.com",
282
+ id: "a-zone-id",
283
+ });
284
+ });
285
+
286
+ it("given a long host, it should use the longest subdomain that resolves to a zone", async () => {
287
+ writeWranglerToml({
288
+ main: "index.js",
289
+ });
290
+ fs.writeFileSync("index.js", `export default {};`);
291
+ mockGetZones("111.222.333.some-host.com", []);
292
+ mockGetZones("222.333.some-host.com", []);
293
+ mockGetZones("333.some-host.com", [{ id: "some-zone-id" }]);
294
+ await runWrangler("dev --host 111.222.333.some-host.com");
295
+ expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
296
+ host: "111.222.333.some-host.com",
297
+ id: "some-zone-id",
298
+ });
299
+ });
300
+
301
+ it("should, in order, use args.host/config.dev.host/args.routes/(config.route|config.routes)", async () => {
302
+ // This test might seem like it's testing implementation details, but let's be specific and consider it a spec
303
+
304
+ fs.writeFileSync("index.js", `export default {};`);
305
+
306
+ // config.routes
307
+ mockGetZones("5.some-host.com", [{ id: "some-zone-id-5" }]);
308
+ writeWranglerToml({
309
+ main: "index.js",
310
+ routes: ["http://5.some-host.com/some/path/*"],
311
+ });
312
+ await runWrangler("dev");
313
+ expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
314
+ host: "5.some-host.com",
315
+ id: "some-zone-id-5",
316
+ });
317
+ (Dev as jest.Mock).mockClear();
318
+
319
+ // config.route
320
+ mockGetZones("4.some-host.com", [{ id: "some-zone-id-4" }]);
321
+ writeWranglerToml({
322
+ main: "index.js",
323
+ route: "https://4.some-host.com/some/path/*",
324
+ });
325
+ await runWrangler("dev");
326
+ expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
327
+ host: "4.some-host.com",
328
+ id: "some-zone-id-4",
329
+ });
330
+ (Dev as jest.Mock).mockClear();
331
+
332
+ // --routes
333
+ mockGetZones("3.some-host.com", [{ id: "some-zone-id-3" }]);
334
+ writeWranglerToml({
335
+ main: "index.js",
336
+ route: "https://4.some-host.com/some/path/*",
337
+ });
338
+ await runWrangler("dev --routes http://3.some-host.com/some/path/*");
339
+ expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
340
+ host: "3.some-host.com",
341
+ id: "some-zone-id-3",
342
+ });
343
+ (Dev as jest.Mock).mockClear();
344
+
345
+ // config.dev.host
346
+ mockGetZones("2.some-host.com", [{ id: "some-zone-id-2" }]);
347
+ writeWranglerToml({
348
+ main: "index.js",
349
+ dev: {
350
+ host: `2.some-host.com`,
351
+ },
352
+ route: "4.some-host.com/some/path/*",
353
+ });
354
+ await runWrangler("dev --routes http://3.some-host.com/some/path/*");
355
+ expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
356
+ host: "2.some-host.com",
357
+ id: "some-zone-id-2",
358
+ });
359
+ (Dev as jest.Mock).mockClear();
360
+
361
+ // --host
362
+ mockGetZones("1.some-host.com", [{ id: "some-zone-id-1" }]);
363
+ writeWranglerToml({
364
+ main: "index.js",
365
+ dev: {
366
+ host: `2.some-host.com`,
367
+ },
368
+ route: "4.some-host.com/some/path/*",
369
+ });
370
+ await runWrangler(
371
+ "dev --routes http://3.some-host.com/some/path/* --host 1.some-host.com"
372
+ );
373
+ expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
374
+ host: "1.some-host.com",
375
+ id: "some-zone-id-1",
376
+ });
377
+ (Dev as jest.Mock).mockClear();
378
+ });
379
+
380
+ it("should error if a host can't resolve to a zone", async () => {
381
+ writeWranglerToml({
382
+ main: "index.js",
383
+ });
384
+ fs.writeFileSync("index.js", `export default {};`);
385
+ mockGetZones("some-host.com", []);
386
+ await expect(
387
+ runWrangler("dev --host some-host.com")
388
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
389
+ `"Could not find zone for some-host.com"`
390
+ );
391
+ });
392
+
393
+ it("should not try to resolve a zone when starting in local mode", async () => {
394
+ writeWranglerToml({
395
+ main: "index.js",
396
+ });
397
+ fs.writeFileSync("index.js", `export default {};`);
398
+ await runWrangler("dev --host some-host.com --local");
399
+ expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual(undefined);
400
+ });
401
+ });
402
+
403
+ describe("custom builds", () => {
404
+ it("should run a custom build before starting `dev`", async () => {
405
+ writeWranglerToml({
406
+ build: {
407
+ command: `node -e "console.log('custom build'); require('fs').writeFileSync('index.js', 'export default { fetch(){ return new Response(123) } }')"`,
408
+ },
409
+ });
410
+
411
+ await runWrangler("dev index.js");
412
+
413
+ expect(fs.readFileSync("index.js", "utf-8")).toMatchInlineSnapshot(
414
+ `"export default { fetch(){ return new Response(123) } }"`
415
+ );
416
+
417
+ // and the command would pass through
418
+ expect((Dev as jest.Mock).mock.calls[0][0].build).toEqual({
419
+ command:
420
+ "node -e \"console.log('custom build'); require('fs').writeFileSync('index.js', 'export default { fetch(){ return new Response(123) } }')\"",
421
+ cwd: undefined,
422
+ watch_dir: "src",
423
+ });
424
+ expect(std.out).toMatchInlineSnapshot(
425
+ `"Running custom build: node -e \\"console.log('custom build'); require('fs').writeFileSync('index.js', 'export default { fetch(){ return new Response(123) } }')\\""`
426
+ );
427
+ expect(std.err).toMatchInlineSnapshot(`""`);
428
+ expect(std.warn).toMatchInlineSnapshot(`""`);
429
+ });
430
+
431
+ if (process.platform !== "win32") {
432
+ it("should run a custom build of multiple steps combined by && before starting `dev`", async () => {
433
+ writeWranglerToml({
434
+ build: {
435
+ command: `echo "custom build" && echo "export default { fetch(){ return new Response(123) } }" > index.js`,
436
+ },
437
+ });
438
+
439
+ await runWrangler("dev index.js");
440
+
441
+ expect(fs.readFileSync("index.js", "utf-8")).toMatchInlineSnapshot(`
442
+ "export default { fetch(){ return new Response(123) } }
443
+ "
444
+ `);
445
+
446
+ expect(std.out).toMatchInlineSnapshot(
447
+ `"Running custom build: echo \\"custom build\\" && echo \\"export default { fetch(){ return new Response(123) } }\\" > index.js"`
448
+ );
449
+ expect(std.err).toMatchInlineSnapshot(`""`);
450
+ expect(std.warn).toMatchInlineSnapshot(`""`);
451
+ });
452
+ }
453
+
454
+ it("should throw an error if the entry doesn't exist after the build finishes", async () => {
455
+ writeWranglerToml({
456
+ main: "index.js",
457
+ build: {
458
+ command: `node -e "console.log('custom build');"`,
459
+ },
460
+ });
461
+
462
+ await expect(runWrangler("dev")).rejects
463
+ .toThrowErrorMatchingInlineSnapshot(`
464
+ "The expected output file at \\"index.js\\" was not found after running custom build: node -e \\"console.log('custom build');\\".
465
+ The \`main\` property in wrangler.toml should point to the file generated by the custom build."
466
+ `);
467
+ expect(std.out).toMatchInlineSnapshot(`
468
+ "Running custom build: node -e \\"console.log('custom build');\\"
469
+
470
+ If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choose"
471
+ `);
472
+ expect(std.err).toMatchInlineSnapshot(`
473
+ "X [ERROR] The expected output file at \\"index.js\\" was not found after running custom build: node -e \\"console.log('custom build');\\".
474
+
475
+ The \`main\` property in wrangler.toml should point to the file generated by the custom build.
476
+
477
+ "
478
+ `);
479
+ expect(std.warn).toMatchInlineSnapshot(`""`);
480
+ });
481
+ });
482
+
483
+ describe("upstream-protocol", () => {
484
+ it("should default upstream-protocol to `https`", async () => {
485
+ writeWranglerToml({
486
+ main: "index.js",
487
+ });
488
+ fs.writeFileSync("index.js", `export default {};`);
489
+ await runWrangler("dev");
490
+ expect((Dev as jest.Mock).mock.calls[0][0].upstreamProtocol).toEqual(
491
+ "https"
492
+ );
493
+ expect(std.out).toMatchInlineSnapshot(`""`);
494
+ expect(std.warn).toMatchInlineSnapshot(`""`);
495
+ expect(std.err).toMatchInlineSnapshot(`""`);
496
+ });
497
+
498
+ it("should warn if `--upstream-protocol=http` is used", async () => {
499
+ writeWranglerToml({
500
+ main: "index.js",
501
+ });
502
+ fs.writeFileSync("index.js", `export default {};`);
503
+ await runWrangler("dev --upstream-protocol=http");
504
+ expect((Dev as jest.Mock).mock.calls[0][0].upstreamProtocol).toEqual(
505
+ "http"
506
+ );
507
+ expect(std.out).toMatchInlineSnapshot(`""`);
508
+ expect(std.warn).toMatchInlineSnapshot(`
509
+ "▲ [WARNING] Setting upstream-protocol to http is not currently implemented.
510
+
511
+ If this is required in your project, please add your use case to the following issue:
512
+ https://github.com/cloudflare/wrangler2/issues/583.
513
+
514
+ "
515
+ `);
516
+ expect(std.err).toMatchInlineSnapshot(`""`);
517
+ });
518
+ });
519
+
520
+ describe("local-protocol", () => {
521
+ it("should default local-protocol to `http`", async () => {
522
+ writeWranglerToml({
523
+ main: "index.js",
524
+ });
525
+ fs.writeFileSync("index.js", `export default {};`);
526
+ await runWrangler("dev");
527
+ expect((Dev as jest.Mock).mock.calls[0][0].localProtocol).toEqual("http");
528
+ expect(std.out).toMatchInlineSnapshot(`""`);
529
+ expect(std.warn).toMatchInlineSnapshot(`""`);
530
+ expect(std.err).toMatchInlineSnapshot(`""`);
531
+ });
532
+
533
+ it("should use `local_protocol` from `wrangler.toml`, if available", async () => {
534
+ writeWranglerToml({
535
+ main: "index.js",
536
+ dev: {
537
+ local_protocol: "https",
538
+ },
539
+ });
540
+ fs.writeFileSync("index.js", `export default {};`);
541
+ await runWrangler("dev");
542
+ expect((Dev as jest.Mock).mock.calls[0][0].localProtocol).toEqual(
543
+ "https"
544
+ );
545
+ expect(std.out).toMatchInlineSnapshot(`""`);
546
+ expect(std.warn).toMatchInlineSnapshot(`""`);
547
+ expect(std.err).toMatchInlineSnapshot(`""`);
548
+ });
549
+
550
+ it("should use --local-protocol command line arg, if provided", async () => {
551
+ // Here we show that the command line overrides the wrangler.toml by
552
+ // setting the config to https, and then setting it back to http on the command line.
553
+ writeWranglerToml({
554
+ main: "index.js",
555
+ dev: {
556
+ local_protocol: "https",
557
+ },
558
+ });
559
+ fs.writeFileSync("index.js", `export default {};`);
560
+ await runWrangler("dev --local-protocol=http");
561
+ expect((Dev as jest.Mock).mock.calls[0][0].localProtocol).toEqual("http");
562
+ expect(std.out).toMatchInlineSnapshot(`""`);
563
+ expect(std.warn).toMatchInlineSnapshot(`""`);
564
+ expect(std.err).toMatchInlineSnapshot(`""`);
565
+ });
566
+ });
567
+
568
+ describe("ip", () => {
569
+ it("should default ip to localhost", async () => {
570
+ writeWranglerToml({
571
+ main: "index.js",
572
+ });
573
+ fs.writeFileSync("index.js", `export default {};`);
574
+ await runWrangler("dev");
575
+ expect((Dev as jest.Mock).mock.calls[0][0].ip).toEqual("localhost");
576
+ expect(std.out).toMatchInlineSnapshot(`""`);
577
+ expect(std.warn).toMatchInlineSnapshot(`""`);
578
+ expect(std.err).toMatchInlineSnapshot(`""`);
579
+ });
580
+
581
+ it("should use to `ip` from `wrangler.toml`, if available", async () => {
582
+ writeWranglerToml({
583
+ main: "index.js",
584
+ dev: {
585
+ ip: "0.0.0.0",
586
+ },
587
+ });
588
+ fs.writeFileSync("index.js", `export default {};`);
589
+ await runWrangler("dev");
590
+ expect((Dev as jest.Mock).mock.calls[0][0].ip).toEqual("0.0.0.0");
591
+ expect(std.out).toMatchInlineSnapshot(`""`);
592
+ expect(std.warn).toMatchInlineSnapshot(`""`);
593
+ expect(std.err).toMatchInlineSnapshot(`""`);
594
+ });
595
+
596
+ it("should use --ip command line arg, if provided", async () => {
597
+ writeWranglerToml({
598
+ main: "index.js",
599
+ dev: {
600
+ ip: "1.1.1.1",
601
+ },
602
+ });
603
+ fs.writeFileSync("index.js", `export default {};`);
604
+ await runWrangler("dev --ip=0.0.0.0");
605
+ expect((Dev as jest.Mock).mock.calls[0][0].ip).toEqual("0.0.0.0");
606
+ expect(std.out).toMatchInlineSnapshot(`""`);
607
+ expect(std.warn).toMatchInlineSnapshot(`""`);
608
+ expect(std.err).toMatchInlineSnapshot(`""`);
609
+ });
610
+ });
611
+
612
+ describe("port", () => {
613
+ it("should default port to 8787 if it is not in use", async () => {
614
+ writeWranglerToml({
615
+ main: "index.js",
616
+ });
617
+ fs.writeFileSync("index.js", `export default {};`);
618
+ await runWrangler("dev");
619
+ expect((Dev as jest.Mock).mock.calls[0][0].port).toEqual(8787);
620
+ expect(std.out).toMatchInlineSnapshot(`""`);
621
+ expect(std.warn).toMatchInlineSnapshot(`""`);
622
+ expect(std.err).toMatchInlineSnapshot(`""`);
623
+ });
624
+
625
+ it("should use to `port` from `wrangler.toml`, if available", async () => {
626
+ writeWranglerToml({
627
+ main: "index.js",
628
+ dev: {
629
+ port: 8888,
630
+ },
631
+ });
632
+ fs.writeFileSync("index.js", `export default {};`);
633
+ // Mock `getPort()` to resolve to a completely different port.
634
+ (getPort as jest.Mock).mockResolvedValue(98765);
635
+
636
+ await runWrangler("dev");
637
+ expect((Dev as jest.Mock).mock.calls[0][0].port).toEqual(8888);
638
+ expect(std.out).toMatchInlineSnapshot(`""`);
639
+ expect(std.warn).toMatchInlineSnapshot(`""`);
640
+ expect(std.err).toMatchInlineSnapshot(`""`);
641
+ });
642
+
643
+ it("should use --port command line arg, if provided", async () => {
644
+ writeWranglerToml({
645
+ main: "index.js",
646
+ dev: {
647
+ port: 8888,
648
+ },
649
+ });
650
+ fs.writeFileSync("index.js", `export default {};`);
651
+ // Mock `getPort()` to resolve to a completely different port.
652
+ (getPort as jest.Mock).mockResolvedValue(98765);
653
+
654
+ await runWrangler("dev --port=9999");
655
+ expect((Dev as jest.Mock).mock.calls[0][0].port).toEqual(9999);
656
+ expect(std.out).toMatchInlineSnapshot(`""`);
657
+ expect(std.warn).toMatchInlineSnapshot(`""`);
658
+ expect(std.err).toMatchInlineSnapshot(`""`);
659
+ });
660
+
661
+ it("should use a different port to the default if it is in use", async () => {
662
+ writeWranglerToml({
663
+ main: "index.js",
664
+ });
665
+ fs.writeFileSync("index.js", `export default {};`);
666
+ // Mock `getPort()` to resolve to a completely different port.
667
+ (getPort as jest.Mock).mockResolvedValue(98765);
668
+
669
+ await runWrangler("dev");
670
+ expect((Dev as jest.Mock).mock.calls[0][0].port).toEqual(98765);
671
+ expect(std.out).toMatchInlineSnapshot(`""`);
672
+ expect(std.warn).toMatchInlineSnapshot(`""`);
673
+ expect(std.err).toMatchInlineSnapshot(`""`);
674
+ });
675
+ });
676
+
677
+ describe("durable_objects", () => {
678
+ it("should warn if there are remote Durable Objects, or missing migrations for local Durable Objects", async () => {
679
+ writeWranglerToml({
680
+ main: "index.js",
681
+ durable_objects: {
682
+ bindings: [
683
+ { name: "NAME_1", class_name: "CLASS_1" },
684
+ {
685
+ name: "NAME_2",
686
+ class_name: "CLASS_2",
687
+ script_name: "SCRIPT_A",
688
+ },
689
+ { name: "NAME_3", class_name: "CLASS_3" },
690
+ {
691
+ name: "NAME_4",
692
+ class_name: "CLASS_4",
693
+ script_name: "SCRIPT_B",
694
+ },
695
+ ],
696
+ },
697
+ });
698
+ fs.writeFileSync("index.js", `export default {};`);
699
+ await runWrangler("dev");
700
+ expect((Dev as jest.Mock).mock.calls[0][0].ip).toEqual("localhost");
701
+ expect(std.out).toMatchInlineSnapshot(`
702
+ "Your worker has access to the following bindings:
703
+ - Durable Objects:
704
+ - NAME_1: CLASS_1
705
+ - NAME_2: CLASS_2 (defined in SCRIPT_A)
706
+ - NAME_3: CLASS_3
707
+ - NAME_4: CLASS_4 (defined in SCRIPT_B)"
708
+ `);
709
+ expect(std.warn).toMatchInlineSnapshot(`
710
+ "▲ [WARNING] Processing wrangler.toml configuration:
711
+
712
+ - In wrangler.toml, you have configured [durable_objects] exported by this Worker (CLASS_1,
713
+ CLASS_3), but no [migrations] for them. This may not work as expected until you add a [migrations]
714
+ section to your wrangler.toml. Add this configuration to your wrangler.toml:
715
+
716
+ \`\`\`
717
+ [[migrations]]
718
+ tag = \\"v1\\" # Should be unique for each entry
719
+ new_classes = [\\"CLASS_1\\", \\"CLASS_3\\"]
720
+ \`\`\`
721
+
722
+ Refer to
723
+ https://developers.cloudflare.com/workers/learning/using-durable-objects/#durable-object-migrations-in-wranglertoml
724
+ for more details.
725
+
726
+
727
+ ▲ [WARNING] WARNING: You have Durable Object bindings that are not defined locally in the worker being developed.
728
+
729
+ Be aware that changes to the data stored in these Durable Objects will be permanent and affect the
730
+ live instances.
731
+ Remote Durable Objects that are affected:
732
+ - {\\"name\\":\\"NAME_2\\",\\"class_name\\":\\"CLASS_2\\",\\"script_name\\":\\"SCRIPT_A\\"}
733
+ - {\\"name\\":\\"NAME_4\\",\\"class_name\\":\\"CLASS_4\\",\\"script_name\\":\\"SCRIPT_B\\"}
734
+
735
+ "
736
+ `);
737
+ expect(std.err).toMatchInlineSnapshot(`""`);
738
+ });
739
+ });
740
+
741
+ describe(".dev.vars", () => {
742
+ it("should override `vars` bindings from `wrangler.toml` with values in `.dev.vars`", async () => {
743
+ fs.writeFileSync("index.js", `export default {};`);
744
+
745
+ const localVarsEnvContent = dedent`
746
+ # Preceding comment
747
+ VAR_1="var #1 value" # End of line comment
748
+ VAR_3="var #3 value"
749
+ VAR_MULTI_LINE_1="A: line 1
750
+ line 2"
751
+ VAR_MULTI_LINE_2="B: line 1\\nline 2"
752
+ EMPTY=
753
+ UNQUOTED= unquoted value
754
+ `;
755
+ fs.writeFileSync(".dev.vars", localVarsEnvContent, "utf8");
756
+
757
+ writeWranglerToml({
758
+ main: "index.js",
759
+ vars: {
760
+ VAR_1: "original value 1",
761
+ VAR_2: "original value 2", // should not get overridden
762
+ VAR_3: "original value 3",
763
+ VAR_MULTI_LINE_1: "original multi-line 1",
764
+ VAR_MULTI_LINE_2: "original multi-line 2",
765
+ EMPTY: "original empty",
766
+ UNQUOTED: "original unquoted",
767
+ },
768
+ });
769
+ await runWrangler("dev");
770
+ const varBindings: Record<string, unknown> = (Dev as jest.Mock).mock
771
+ .calls[0][0].bindings.vars;
772
+
773
+ expect(varBindings).toEqual({
774
+ VAR_1: "var #1 value",
775
+ VAR_2: "original value 2",
776
+ VAR_3: "var #3 value",
777
+ VAR_MULTI_LINE_1: "A: line 1\nline 2",
778
+ VAR_MULTI_LINE_2: "B: line 1\nline 2",
779
+ EMPTY: "",
780
+ UNQUOTED: "unquoted value", // Note that whitespace is trimmed
781
+ });
782
+ expect(std.out).toMatchInlineSnapshot(`
783
+ "Using vars defined in .dev.vars
784
+ Your worker has access to the following bindings:
785
+ - Vars:
786
+ - VAR_1: \\"(hidden)\\"
787
+ - VAR_2: \\"original value 2\\"
788
+ - VAR_3: \\"(hidden)\\"
789
+ - VAR_MULTI_LINE_1: \\"(hidden)\\"
790
+ - VAR_MULTI_LINE_2: \\"(hidden)\\"
791
+ - EMPTY: \\"(hidden)\\"
792
+ - UNQUOTED: \\"(hidden)\\""
793
+ `);
794
+ expect(std.warn).toMatchInlineSnapshot(`""`);
795
+ expect(std.err).toMatchInlineSnapshot(`""`);
796
+ });
797
+ });
798
+
799
+ describe("site", () => {
800
+ it("should error if --site is used with no value", async () => {
801
+ await expect(
802
+ runWrangler("dev --site")
803
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
804
+ `"Not enough arguments following: site"`
805
+ );
806
+
807
+ expect(std).toMatchInlineSnapshot(`
808
+ Object {
809
+ "debug": "",
810
+ "err": "X [ERROR] Not enough arguments following: site
811
+
812
+ ",
813
+ "out": "
814
+ wrangler dev [script]
815
+
816
+ 👂 Start a local server for developing your worker
817
+
818
+ Positionals:
819
+ script The path to an entry point for your worker [string]
820
+
821
+ Flags:
822
+ -c, --config Path to .toml configuration file [string]
823
+ -h, --help Show help [boolean]
824
+ -v, --version Show version number [boolean]
825
+
826
+ Options:
827
+ --name Name of the worker [string]
828
+ --format Choose an entry type [deprecated] [choices: \\"modules\\", \\"service-worker\\"]
829
+ -e, --env Perform on a specific environment [string]
830
+ --compatibility-date Date to use for compatibility checks [string]
831
+ --compatibility-flags, --compatibility-flag Flags to use for compatibility checks [array]
832
+ --latest Use the latest version of the worker runtime [boolean] [default: true]
833
+ --ip IP address to listen on, defaults to \`localhost\` [string]
834
+ --port Port to listen on [number]
835
+ --inspector-port Port for devtools to connect to [number]
836
+ --routes, --route Routes to upload [array]
837
+ --host Host to forward requests to, defaults to the zone of project [string]
838
+ --local-protocol Protocol to listen to requests on, defaults to http. [choices: \\"http\\", \\"https\\"]
839
+ --experimental-public Static assets to be served [string]
840
+ --site Root folder of static assets for Workers Sites [string]
841
+ --site-include Array of .gitignore-style patterns that match file or directory names from the sites directory. Only matched items will be uploaded. [array]
842
+ --site-exclude Array of .gitignore-style patterns that match file or directory names from the sites directory. Matched items will not be uploaded. [array]
843
+ --upstream-protocol Protocol to forward requests to host on, defaults to https. [choices: \\"http\\", \\"https\\"]
844
+ --jsx-factory The function that is called for each JSX element [string]
845
+ --jsx-fragment The function that is called for each JSX fragment [string]
846
+ --tsconfig Path to a custom tsconfig.json file [string]
847
+ -l, --local Run on my machine [boolean] [default: false]
848
+ --minify Minify the script [boolean]
849
+ --node-compat Enable node.js compatibility [boolean]
850
+ --experimental-enable-local-persistence Enable persistence for this session (only for local mode) [boolean]
851
+ --inspect Enable dev tools [deprecated] [boolean]",
852
+ "warn": "",
853
+ }
854
+ `);
855
+ });
856
+ });
857
+
858
+ describe("--inspect", () => {
859
+ it("should warn if --inspect is used", async () => {
860
+ fs.writeFileSync("index.js", `export default {};`);
861
+ await runWrangler("dev index.js --inspect");
862
+ expect(std).toMatchInlineSnapshot(`
863
+ Object {
864
+ "debug": "",
865
+ "err": "",
866
+ "out": "",
867
+ "warn": "▲ [WARNING] Passing --inspect is unnecessary, now you can always connect to devtools.
868
+
869
+ ",
870
+ }
871
+ `);
872
+ });
873
+ });
874
+
875
+ describe("service bindings", () => {
876
+ it("should warn when using service bindings", async () => {
877
+ writeWranglerToml({
878
+ services: [
879
+ { binding: "WorkerA", service: "A" },
880
+ { binding: "WorkerB", service: "B", environment: "staging" },
881
+ ],
882
+ });
883
+ fs.writeFileSync("index.js", `export default {};`);
884
+ await runWrangler("dev index.js");
885
+ expect(std).toMatchInlineSnapshot(`
886
+ Object {
887
+ "debug": "",
888
+ "err": "",
889
+ "out": "Your worker has access to the following bindings:
890
+ - Services:
891
+ - WorkerA: A
892
+ - WorkerB: B - staging",
893
+ "warn": "▲ [WARNING] Processing wrangler.toml configuration:
894
+
895
+ - \\"services\\" fields are experimental and may change or break at any time.
896
+
897
+
898
+ ▲ [WARNING] This worker is bound to live services: WorkerA (A), WorkerB (B@staging)
899
+
900
+ ",
901
+ }
902
+ `);
903
+ });
904
+ });
905
+
906
+ describe("print bindings", () => {
907
+ it("should print bindings", async () => {
908
+ writeWranglerToml({
909
+ services: [
910
+ { binding: "WorkerA", service: "A" },
911
+ { binding: "WorkerB", service: "B", environment: "staging" },
912
+ ],
913
+ });
914
+ fs.writeFileSync("index.js", `export default {};`);
915
+ await runWrangler("dev index.js");
916
+ expect(std).toMatchInlineSnapshot(`
917
+ Object {
918
+ "debug": "",
919
+ "err": "",
920
+ "out": "Your worker has access to the following bindings:
921
+ - Services:
922
+ - WorkerA: A
923
+ - WorkerB: B - staging",
924
+ "warn": "▲ [WARNING] Processing wrangler.toml configuration:
925
+
926
+ - \\"services\\" fields are experimental and may change or break at any time.
927
+
928
+
929
+ ▲ [WARNING] This worker is bound to live services: WorkerA (A), WorkerB (B@staging)
930
+
931
+ ",
932
+ }
933
+ `);
934
+ });
935
+
936
+ it("should mask vars that were overriden in .dev.vars", async () => {
937
+ writeWranglerToml({
938
+ vars: {
939
+ variable: 123,
940
+ overriden: "original values",
941
+ },
942
+ });
943
+ fs.writeFileSync(
944
+ ".dev.vars",
945
+ `
946
+ SECRET = "A secret"
947
+ overriden = "overriden value"
948
+ `
949
+ );
950
+ fs.writeFileSync("index.js", `export default {};`);
951
+ await runWrangler("dev index.js");
952
+ expect(std).toMatchInlineSnapshot(`
953
+ Object {
954
+ "debug": "",
955
+ "err": "",
956
+ "out": "Using vars defined in .dev.vars
957
+ Your worker has access to the following bindings:
958
+ - Vars:
959
+ - variable: \\"123\\"
960
+ - overriden: \\"(hidden)\\"
961
+ - SECRET: \\"(hidden)\\"",
962
+ "warn": "",
963
+ }
964
+ `);
965
+ });
966
+ });
967
+ });
968
+
969
+ function mockGetZones(domain: string, zones: { id: string }[] = []) {
970
+ const removeMock = setMockResponse(
971
+ "/zones",
972
+ "GET",
973
+ (_urlPieces, _init, queryParams) => {
974
+ expect([...queryParams.entries()]).toEqual([["name", domain]]);
975
+ // Because the API URL `/zones` is the same for each request, we can get into a situation where earlier mocks get triggered for later requests. So, we simply clear the mock on every trigger.
976
+ removeMock();
977
+ return zones;
978
+ }
979
+ );
980
+ }