wrangler 0.0.0-e3cab74 → 0.0.0-e452a35

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 (102) hide show
  1. package/Cloudflare_CA.pem +18 -0
  2. package/README.md +27 -14
  3. package/bin/wrangler.js +49 -5
  4. package/miniflare-dist/index.mjs +4886 -0
  5. package/package.json +42 -28
  6. package/pages/functions/buildPlugin.ts +105 -0
  7. package/pages/functions/buildWorker.ts +93 -4
  8. package/pages/functions/filepath-routing.test.ts +136 -25
  9. package/pages/functions/filepath-routing.ts +14 -3
  10. package/pages/functions/routes.ts +6 -1
  11. package/pages/functions/template-plugin.ts +147 -0
  12. package/pages/functions/template-worker.ts +21 -8
  13. package/src/__tests__/config-cache.test.ts +36 -0
  14. package/src/__tests__/configuration.test.ts +1514 -367
  15. package/src/__tests__/dev.test.tsx +870 -84
  16. package/src/__tests__/guess-worker-format.test.ts +37 -18
  17. package/src/__tests__/helpers/mock-account-id.ts +22 -4
  18. package/src/__tests__/helpers/mock-cfetch.ts +8 -4
  19. package/src/__tests__/helpers/mock-console.ts +35 -10
  20. package/src/__tests__/helpers/mock-dialogs.ts +26 -1
  21. package/src/__tests__/helpers/mock-http-server.ts +46 -0
  22. package/src/__tests__/helpers/mock-istty.ts +27 -0
  23. package/src/__tests__/helpers/mock-kv.ts +9 -16
  24. package/src/__tests__/helpers/mock-oauth-flow.ts +255 -0
  25. package/src/__tests__/helpers/mock-stdin.ts +103 -0
  26. package/src/__tests__/helpers/run-in-tmp.ts +10 -0
  27. package/src/__tests__/helpers/write-worker-source.ts +31 -0
  28. package/src/__tests__/https-options.test.ts +130 -0
  29. package/src/__tests__/index.test.ts +137 -747
  30. package/src/__tests__/init.test.ts +1557 -0
  31. package/src/__tests__/jest.setup.ts +28 -8
  32. package/src/__tests__/kv.test.ts +598 -258
  33. package/src/__tests__/logger.test.ts +115 -0
  34. package/src/__tests__/pages.test.ts +657 -0
  35. package/src/__tests__/parse.test.ts +8 -0
  36. package/src/__tests__/publish.test.ts +4003 -941
  37. package/src/__tests__/r2.test.ts +69 -34
  38. package/src/__tests__/route.test.ts +45 -0
  39. package/src/__tests__/secret.test.ts +228 -73
  40. package/src/__tests__/tail.test.ts +146 -20
  41. package/src/__tests__/user.test.ts +131 -0
  42. package/src/__tests__/whoami.test.tsx +26 -7
  43. package/src/abort.d.ts +3 -0
  44. package/src/bundle.ts +74 -7
  45. package/src/cfetch/index.ts +21 -4
  46. package/src/cfetch/internal.ts +20 -14
  47. package/src/cli.ts +5 -11
  48. package/src/config/README.md +7 -7
  49. package/src/config/config.ts +69 -100
  50. package/src/config/environment.ts +119 -4
  51. package/src/config/index.ts +177 -4
  52. package/src/config/validation-helpers.ts +228 -23
  53. package/src/config/validation.ts +687 -174
  54. package/src/config-cache.ts +45 -0
  55. package/src/{api/preview.ts → create-worker-preview.ts} +74 -36
  56. package/src/{api/form_data.ts → create-worker-upload-form.ts} +55 -18
  57. package/src/dev/dev-vars.ts +36 -0
  58. package/src/dev/dev.tsx +422 -0
  59. package/src/dev/local.tsx +318 -0
  60. package/src/dev/remote.tsx +227 -0
  61. package/src/dev/use-esbuild.ts +118 -0
  62. package/src/dialogs.tsx +16 -6
  63. package/src/durable.ts +102 -0
  64. package/src/entry.ts +279 -14
  65. package/src/environment-variables.ts +3 -1
  66. package/src/errors.ts +11 -0
  67. package/src/https-options.ts +123 -0
  68. package/src/index.tsx +1268 -806
  69. package/src/inspect.ts +64 -11
  70. package/src/intl-polyfill.d.ts +139 -0
  71. package/src/{kv.tsx → kv.ts} +195 -62
  72. package/src/logger.ts +66 -0
  73. package/src/miniflare-cli/README.md +30 -0
  74. package/src/miniflare-cli/enum-keys.ts +17 -0
  75. package/src/miniflare-cli/index.ts +41 -0
  76. package/src/miniflare-cli/request-context.ts +40 -0
  77. package/src/module-collection.ts +94 -6
  78. package/src/open-in-browser.ts +9 -3
  79. package/src/package-manager.ts +9 -8
  80. package/src/pages.tsx +1057 -75
  81. package/src/parse.ts +24 -5
  82. package/src/proxy.ts +143 -54
  83. package/src/publish.ts +444 -229
  84. package/src/selfsigned.d.ts +29 -0
  85. package/src/sites.tsx +98 -44
  86. package/src/tail/index.ts +2 -2
  87. package/src/tail/printing.ts +13 -8
  88. package/src/update-check.ts +19 -0
  89. package/src/user.tsx +263 -109
  90. package/src/whoami.tsx +2 -1
  91. package/src/{api/worker.ts → worker.ts} +24 -30
  92. package/templates/gitignore +171 -0
  93. package/templates/new-worker.ts +16 -1
  94. package/templates/static-asset-facade.js +1 -1
  95. package/wrangler-dist/cli.js +55345 -69246
  96. package/src/__tests__/helpers/mock-user.ts +0 -27
  97. package/src/__tests__/logout.test.ts +0 -50
  98. package/src/__tests__/sentry.test.ts +0 -146
  99. package/src/dev.tsx +0 -961
  100. package/src/guess-worker-format.ts +0 -68
  101. package/src/reporting.ts +0 -115
  102. package/wrangler-dist/cli.js.map +0 -7
@@ -1,31 +1,74 @@
1
- import { render } from "ink-testing-library";
1
+ import * as fs from "node:fs";
2
+ import getPort from "get-port";
2
3
  import patchConsole from "patch-console";
3
- import React from "react";
4
- import Dev from "../dev";
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";
5
8
  import { mockConsoleMethods } from "./helpers/mock-console";
9
+ import { runInTempDir } from "./helpers/run-in-tmp";
6
10
  import { runWrangler } from "./helpers/run-wrangler";
7
11
  import writeWranglerToml from "./helpers/write-wrangler-toml";
8
- import type { DevProps } from "../dev";
9
12
 
10
- describe("Dev component", () => {
11
- let restoreConsole: ReturnType<typeof patchConsole>;
12
- beforeEach(() => (restoreConsole = patchConsole(() => {})));
13
- afterEach(() => restoreConsole());
13
+ describe("wrangler dev", () => {
14
+ mockAccountId();
15
+ mockApiToken();
16
+ runInTempDir();
14
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>.
15
45
 
16
- // This test needs to be rewritten because the error now throws asynchronously
17
- // and the Ink framework does not yet have async testing support.
18
- it.skip("should throw if format is service-worker and there is a public directory", () => {
19
- const { lastFrame } = renderDev({
20
- format: "service-worker",
21
- accountId: "some-account-id",
22
- public: "some/public/path",
23
- });
24
- expect(lastFrame()?.split("\n").slice(0, 2).join("\n"))
25
- .toMatchInlineSnapshot(`
26
- "Something went wrong:
27
- Error: You cannot use the service worker format with a \`public\` directory."
28
- `);
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
+ });
29
72
  });
30
73
 
31
74
  describe("entry-points", () => {
@@ -38,76 +81,819 @@ describe("Dev component", () => {
38
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."`
39
82
  );
40
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."
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
+ );
41
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."
471
+ `);
42
472
  expect(std.err).toMatchInlineSnapshot(`
43
- "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.
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
+ expect(std.warn).toMatchInlineSnapshot(`
703
+ "▲ [WARNING] Processing wrangler.toml configuration:
704
+
705
+ - In wrangler.toml, you have configured [durable_objects] exported by this Worker (CLASS_1,
706
+ CLASS_3), but no [migrations] for them. This may not work as expected until you add a [migrations]
707
+ section to your wrangler.toml. Add this configuration to your wrangler.toml:
708
+
709
+ \`\`\`
710
+ [[migrations]]
711
+ tag = \\"v1\\" # Should be unique for each entry
712
+ new_classes = [\\"CLASS_1\\", \\"CLASS_3\\"]
713
+ \`\`\`
714
+
715
+ Refer to
716
+ https://developers.cloudflare.com/workers/learning/using-durable-objects/#durable-object-migrations-in-wranglertoml
717
+ for more details.
718
+
719
+
720
+ ▲ [WARNING] WARNING: You have Durable Object bindings that are not defined locally in the worker being developed.
721
+
722
+ Be aware that changes to the data stored in these Durable Objects will be permanent and affect the
723
+ live instances.
724
+ Remote Durable Objects that are affected:
725
+ - {\\"name\\":\\"NAME_2\\",\\"class_name\\":\\"CLASS_2\\",\\"script_name\\":\\"SCRIPT_A\\"}
726
+ - {\\"name\\":\\"NAME_4\\",\\"class_name\\":\\"CLASS_4\\",\\"script_name\\":\\"SCRIPT_B\\"}
727
+
728
+ "
729
+ `);
730
+ expect(std.err).toMatchInlineSnapshot(`""`);
731
+ });
732
+ });
733
+
734
+ describe(".dev.vars", () => {
735
+ it("should override `vars` bindings from `wrangler.toml` with values in `.dev.vars`", async () => {
736
+ fs.writeFileSync("index.js", `export default {};`);
737
+
738
+ const localVarsEnvContent = dedent`
739
+ # Preceding comment
740
+ VAR_1="var #1 value" # End of line comment
741
+ VAR_3="var #3 value"
742
+ VAR_MULTI_LINE_1="A: line 1
743
+ line 2"
744
+ VAR_MULTI_LINE_2="B: line 1\\nline 2"
745
+ EMPTY=
746
+ UNQUOTED= unquoted value
747
+ `;
748
+ fs.writeFileSync(".dev.vars", localVarsEnvContent, "utf8");
749
+
750
+ writeWranglerToml({
751
+ main: "index.js",
752
+ vars: {
753
+ VAR_1: "original value 1",
754
+ VAR_2: "original value 2", // should not get overridden
755
+ VAR_3: "original value 3",
756
+ VAR_MULTI_LINE_1: "original multi-line 1",
757
+ VAR_MULTI_LINE_2: "original multi-line 2",
758
+ EMPTY: "original empty",
759
+ UNQUOTED: "original unquoted",
760
+ },
761
+ });
762
+ await runWrangler("dev");
763
+ const varBindings: Record<string, unknown> = (Dev as jest.Mock).mock
764
+ .calls[0][0].bindings.vars;
765
+
766
+ expect(varBindings).toEqual({
767
+ VAR_1: "var #1 value",
768
+ VAR_2: "original value 2",
769
+ VAR_3: "var #3 value",
770
+ VAR_MULTI_LINE_1: "A: line 1\nline 2",
771
+ VAR_MULTI_LINE_2: "B: line 1\nline 2",
772
+ EMPTY: "",
773
+ UNQUOTED: "unquoted value", // Note that whitespace is trimmed
774
+ });
775
+ expect(std.out).toMatchInlineSnapshot(
776
+ `"Using vars defined in .dev.vars"`
777
+ );
778
+ expect(std.warn).toMatchInlineSnapshot(`""`);
779
+ expect(std.err).toMatchInlineSnapshot(`""`);
780
+ });
781
+ });
782
+
783
+ describe("site", () => {
784
+ it("should error if --site is used with no value", async () => {
785
+ await expect(
786
+ runWrangler("dev --site")
787
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
788
+ `"Not enough arguments following: site"`
789
+ );
790
+
791
+ expect(std).toMatchInlineSnapshot(`
792
+ Object {
793
+ "debug": "",
794
+ "err": "X [ERROR] Not enough arguments following: site
795
+
796
+ ",
797
+ "out": "
798
+ wrangler dev [script]
799
+
800
+ 👂 Start a local server for developing your worker
801
+
802
+ Positionals:
803
+ script The path to an entry point for your worker [string]
804
+
805
+ Flags:
806
+ -c, --config Path to .toml configuration file [string]
807
+ -h, --help Show help [boolean]
808
+ -v, --version Show version number [boolean]
809
+
810
+ Options:
811
+ --name Name of the worker [string]
812
+ --format Choose an entry type [deprecated] [choices: \\"modules\\", \\"service-worker\\"]
813
+ -e, --env Perform on a specific environment [string]
814
+ --compatibility-date Date to use for compatibility checks [string]
815
+ --compatibility-flags, --compatibility-flag Flags to use for compatibility checks [array]
816
+ --latest Use the latest version of the worker runtime [boolean] [default: true]
817
+ --ip IP address to listen on, defaults to \`localhost\` [string]
818
+ --port Port to listen on [number]
819
+ --inspector-port Port for devtools to connect to [number]
820
+ --routes, --route Routes to upload [array]
821
+ --host Host to forward requests to, defaults to the zone of project [string]
822
+ --local-protocol Protocol to listen to requests on, defaults to http. [choices: \\"http\\", \\"https\\"]
823
+ --experimental-public Static assets to be served [string]
824
+ --site Root folder of static assets for Workers Sites [string]
825
+ --site-include Array of .gitignore-style patterns that match file or directory names from the sites directory. Only matched items will be uploaded. [array]
826
+ --site-exclude Array of .gitignore-style patterns that match file or directory names from the sites directory. Matched items will not be uploaded. [array]
827
+ --upstream-protocol Protocol to forward requests to host on, defaults to https. [choices: \\"http\\", \\"https\\"]
828
+ --jsx-factory The function that is called for each JSX element [string]
829
+ --jsx-fragment The function that is called for each JSX fragment [string]
830
+ --tsconfig Path to a custom tsconfig.json file [string]
831
+ -l, --local Run on my machine [boolean] [default: false]
832
+ --minify Minify the script [boolean]
833
+ --node-compat Enable node.js compatibility [boolean]
834
+ --experimental-enable-local-persistence Enable persistence for this session (only for local mode) [boolean]
835
+ --inspect Enable dev tools [deprecated] [boolean]",
836
+ "warn": "",
837
+ }
838
+ `);
839
+ });
840
+ });
841
+
842
+ describe("--inspect", () => {
843
+ it("should warn if --inspect is used", async () => {
844
+ fs.writeFileSync("index.js", `export default {};`);
845
+ await runWrangler("dev index.js --inspect");
846
+ expect(std).toMatchInlineSnapshot(`
847
+ Object {
848
+ "debug": "",
849
+ "err": "",
850
+ "out": "",
851
+ "warn": "▲ [WARNING] Passing --inspect is unnecessary, now you can always connect to devtools.
852
+
853
+ ",
854
+ }
855
+ `);
856
+ });
857
+ });
858
+
859
+ describe("service bindings", () => {
860
+ it("should warn when using service bindings", async () => {
861
+ writeWranglerToml({
862
+ services: [
863
+ { binding: "WorkerA", service: "A" },
864
+ { binding: "WorkerB", service: "B", environment: "staging" },
865
+ ],
866
+ });
867
+ fs.writeFileSync("index.js", `export default {};`);
868
+ await runWrangler("dev index.js");
869
+ expect(std).toMatchInlineSnapshot(`
870
+ Object {
871
+ "debug": "",
872
+ "err": "",
873
+ "out": "",
874
+ "warn": "▲ [WARNING] Processing wrangler.toml configuration:
875
+
876
+ - \\"services\\" fields are experimental and may change or break at any time.
877
+
878
+
879
+ ▲ [WARNING] This worker is bound to live services: WorkerA (A), WorkerB (B@staging)
44
880
 
45
- %s
46
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
881
+ ",
882
+ }
47
883
  `);
48
884
  });
49
885
  });
50
886
  });
51
887
 
52
- /**
53
- * Helper function to make it easier to setup and render the `Dev` component.
54
- *
55
- * All the `Dev` props are optional here, with sensible defaults for testing.
56
- */
57
- function renderDev({
58
- name,
59
- entry = { file: "some/entry.ts", directory: process.cwd() },
60
- port,
61
- inspectorPort = 9229,
62
- format,
63
- accountId,
64
- legacyEnv = true,
65
- initialMode = "local",
66
- jsxFactory,
67
- jsxFragment,
68
- rules = [],
69
- bindings = {
70
- kv_namespaces: [],
71
- vars: {},
72
- durable_objects: { bindings: [] },
73
- r2_buckets: [],
74
- wasm_modules: {},
75
- text_blobs: {},
76
- unsafe: [],
77
- },
78
- public: publicDir,
79
- assetPaths,
80
- compatibilityDate,
81
- compatibilityFlags,
82
- usageModel,
83
- buildCommand = {},
84
- enableLocalPersistence = false,
85
- env,
86
- zone,
87
- }: Partial<DevProps>) {
88
- return render(
89
- <Dev
90
- name={name}
91
- entry={entry}
92
- env={env}
93
- rules={rules}
94
- port={port}
95
- inspectorPort={inspectorPort}
96
- legacyEnv={legacyEnv}
97
- buildCommand={buildCommand}
98
- format={format}
99
- initialMode={initialMode}
100
- jsxFactory={jsxFactory}
101
- jsxFragment={jsxFragment}
102
- accountId={accountId}
103
- assetPaths={assetPaths}
104
- public={publicDir}
105
- compatibilityDate={compatibilityDate}
106
- compatibilityFlags={compatibilityFlags}
107
- usageModel={usageModel}
108
- bindings={bindings}
109
- enableLocalPersistence={enableLocalPersistence}
110
- zone={zone}
111
- />
888
+ function mockGetZones(domain: string, zones: { id: string }[] = []) {
889
+ const removeMock = setMockResponse(
890
+ "/zones",
891
+ "GET",
892
+ (_urlPieces, _init, queryParams) => {
893
+ expect([...queryParams.entries()]).toEqual([["name", domain]]);
894
+ // 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.
895
+ removeMock();
896
+ return zones;
897
+ }
112
898
  );
113
899
  }