wrangler 0.0.0-ece06ea → 0.0.0-ed08df4

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 (121) hide show
  1. package/Cloudflare_CA.pem +18 -0
  2. package/README.md +27 -14
  3. package/bin/wrangler.js +34 -2
  4. package/miniflare-dist/index.mjs +4886 -0
  5. package/package.json +71 -36
  6. package/pages/functions/buildPlugin.ts +92 -0
  7. package/pages/functions/buildWorker.ts +138 -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 +3125 -0
  16. package/src/__tests__/dev.test.tsx +829 -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 +90 -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 +221 -132
  38. package/src/__tests__/init.test.ts +1516 -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 +206 -0
  43. package/src/__tests__/pages.test.ts +327 -0
  44. package/src/__tests__/parse.test.ts +326 -0
  45. package/src/__tests__/publish.test.ts +4996 -0
  46. package/src/__tests__/r2.test.ts +210 -0
  47. package/src/__tests__/route.test.ts +45 -0
  48. package/src/__tests__/secret.test.ts +446 -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/bundle.ts +169 -0
  53. package/src/cfetch/index.ts +104 -0
  54. package/src/cfetch/internal.ts +140 -0
  55. package/src/cli.ts +10 -7
  56. package/src/config/README.md +107 -0
  57. package/src/config/config.ts +234 -0
  58. package/src/config/diagnostics.ts +80 -0
  59. package/src/config/environment.ts +394 -0
  60. package/src/config/index.ts +63 -0
  61. package/src/config/validation-helpers.ts +575 -0
  62. package/src/config/validation.ts +1424 -0
  63. package/src/config-cache.ts +45 -0
  64. package/src/create-worker-preview.ts +175 -0
  65. package/src/create-worker-upload-form.ts +240 -0
  66. package/src/dev/dev-vars.ts +36 -0
  67. package/src/dev/dev.tsx +421 -0
  68. package/src/dev/local.tsx +312 -0
  69. package/src/dev/remote.tsx +213 -0
  70. package/src/dev/use-esbuild.ts +118 -0
  71. package/src/dialogs.tsx +22 -20
  72. package/src/entry.ts +322 -0
  73. package/src/environment-variables.ts +35 -0
  74. package/src/errors.ts +11 -0
  75. package/src/https-options.ts +123 -0
  76. package/src/index.tsx +1869 -1020
  77. package/src/inspect.ts +623 -0
  78. package/src/intl-polyfill.d.ts +139 -0
  79. package/src/kv.ts +344 -0
  80. package/src/logger.ts +66 -0
  81. package/src/miniflare-cli/README.md +30 -0
  82. package/src/miniflare-cli/enum-keys.ts +17 -0
  83. package/src/miniflare-cli/index.ts +41 -0
  84. package/src/miniflare-cli/request-context.ts +40 -0
  85. package/src/module-collection.ts +258 -0
  86. package/src/open-in-browser.ts +26 -0
  87. package/src/package-manager.ts +127 -0
  88. package/src/pages.tsx +1756 -218
  89. package/src/parse.ts +185 -0
  90. package/src/paths.ts +26 -0
  91. package/src/proxy.ts +449 -0
  92. package/src/publish.ts +377 -262
  93. package/src/r2.ts +50 -0
  94. package/src/selfsigned.d.ts +29 -0
  95. package/src/sites.tsx +237 -59
  96. package/src/tail/filters.ts +279 -0
  97. package/src/tail/index.ts +299 -0
  98. package/src/tail/printing.ts +67 -0
  99. package/src/update-check.ts +19 -0
  100. package/src/user.tsx +395 -221
  101. package/src/whoami.tsx +70 -0
  102. package/src/worker.ts +172 -0
  103. package/templates/gitignore +171 -0
  104. package/templates/new-worker.js +15 -0
  105. package/templates/new-worker.ts +15 -0
  106. package/{static-asset-facade.js → templates/static-asset-facade.js} +1 -1
  107. package/templates/tsconfig.json +105 -0
  108. package/wrangler-dist/cli.js +108921 -132528
  109. package/src/__tests__/fixtures/init/.gitkeep +0 -0
  110. package/src/__tests__/mock-cfetch.js +0 -46
  111. package/src/api/form_data.ts +0 -158
  112. package/src/api/inspect.ts +0 -441
  113. package/src/api/preview.ts +0 -123
  114. package/src/api/worker.ts +0 -161
  115. package/src/cfetch.ts +0 -72
  116. package/src/config.ts +0 -124
  117. package/src/dev.tsx +0 -736
  118. package/src/kv.tsx +0 -211
  119. package/src/tail.tsx +0 -71
  120. package/src/util/fetch.ts +0 -74
  121. package/wrangler-dist/cli.js.map +0 -7
@@ -0,0 +1,829 @@
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."
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, when provided, use a configured zone_id", async () => {
229
+ writeWranglerToml({
230
+ main: "index.js",
231
+ routes: [
232
+ { pattern: "https://some-domain.com/*", zone_id: "some-zone-id" },
233
+ ],
234
+ });
235
+ fs.writeFileSync("index.js", `export default {};`);
236
+ await runWrangler("dev");
237
+ expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
238
+ host: "some-domain.com",
239
+ id: "some-zone-id",
240
+ });
241
+ });
242
+
243
+ it("should, when provided, use a zone_name to get a zone_id", async () => {
244
+ writeWranglerToml({
245
+ main: "index.js",
246
+ routes: [
247
+ { pattern: "https://some-domain.com/*", zone_name: "some-zone.com" },
248
+ ],
249
+ });
250
+ fs.writeFileSync("index.js", `export default {};`);
251
+ mockGetZones("some-zone.com", [{ id: "a-zone-id" }]);
252
+ await runWrangler("dev");
253
+ expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
254
+ // note that it uses the provided zone_name as a host too
255
+ host: "some-zone.com",
256
+ id: "a-zone-id",
257
+ });
258
+ });
259
+
260
+ it("given a long host, it should use the longest subdomain that resolves to a zone", async () => {
261
+ writeWranglerToml({
262
+ main: "index.js",
263
+ });
264
+ fs.writeFileSync("index.js", `export default {};`);
265
+ mockGetZones("111.222.333.some-host.com", []);
266
+ mockGetZones("222.333.some-host.com", []);
267
+ mockGetZones("333.some-host.com", [{ id: "some-zone-id" }]);
268
+ await runWrangler("dev --host 111.222.333.some-host.com");
269
+ expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
270
+ host: "111.222.333.some-host.com",
271
+ id: "some-zone-id",
272
+ });
273
+ });
274
+
275
+ it("should, in order, use args.host/config.dev.host/args.routes/(config.route|config.routes)", async () => {
276
+ // This test might seem like it's testing implementation details, but let's be specific and consider it a spec
277
+
278
+ fs.writeFileSync("index.js", `export default {};`);
279
+
280
+ // config.routes
281
+ mockGetZones("5.some-host.com", [{ id: "some-zone-id-5" }]);
282
+ writeWranglerToml({
283
+ main: "index.js",
284
+ routes: ["http://5.some-host.com/some/path/*"],
285
+ });
286
+ await runWrangler("dev");
287
+ expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
288
+ host: "5.some-host.com",
289
+ id: "some-zone-id-5",
290
+ });
291
+ (Dev as jest.Mock).mockClear();
292
+
293
+ // config.route
294
+ mockGetZones("4.some-host.com", [{ id: "some-zone-id-4" }]);
295
+ writeWranglerToml({
296
+ main: "index.js",
297
+ route: "https://4.some-host.com/some/path/*",
298
+ });
299
+ await runWrangler("dev");
300
+ expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
301
+ host: "4.some-host.com",
302
+ id: "some-zone-id-4",
303
+ });
304
+ (Dev as jest.Mock).mockClear();
305
+
306
+ // --routes
307
+ mockGetZones("3.some-host.com", [{ id: "some-zone-id-3" }]);
308
+ writeWranglerToml({
309
+ main: "index.js",
310
+ route: "https://4.some-host.com/some/path/*",
311
+ });
312
+ await runWrangler("dev --routes http://3.some-host.com/some/path/*");
313
+ expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
314
+ host: "3.some-host.com",
315
+ id: "some-zone-id-3",
316
+ });
317
+ (Dev as jest.Mock).mockClear();
318
+
319
+ // config.dev.host
320
+ mockGetZones("2.some-host.com", [{ id: "some-zone-id-2" }]);
321
+ writeWranglerToml({
322
+ main: "index.js",
323
+ dev: {
324
+ host: `2.some-host.com`,
325
+ },
326
+ route: "4.some-host.com/some/path/*",
327
+ });
328
+ await runWrangler("dev --routes http://3.some-host.com/some/path/*");
329
+ expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
330
+ host: "2.some-host.com",
331
+ id: "some-zone-id-2",
332
+ });
333
+ (Dev as jest.Mock).mockClear();
334
+
335
+ // --host
336
+ mockGetZones("1.some-host.com", [{ id: "some-zone-id-1" }]);
337
+ writeWranglerToml({
338
+ main: "index.js",
339
+ dev: {
340
+ host: `2.some-host.com`,
341
+ },
342
+ route: "4.some-host.com/some/path/*",
343
+ });
344
+ await runWrangler(
345
+ "dev --routes http://3.some-host.com/some/path/* --host 1.some-host.com"
346
+ );
347
+ expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
348
+ host: "1.some-host.com",
349
+ id: "some-zone-id-1",
350
+ });
351
+ (Dev as jest.Mock).mockClear();
352
+ });
353
+
354
+ it("should error if a host can't resolve to a zone", async () => {
355
+ writeWranglerToml({
356
+ main: "index.js",
357
+ });
358
+ fs.writeFileSync("index.js", `export default {};`);
359
+ mockGetZones("some-host.com", []);
360
+ await expect(
361
+ runWrangler("dev --host some-host.com")
362
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
363
+ `"Could not find zone for some-host.com"`
364
+ );
365
+ });
366
+
367
+ it("should not try to resolve a zone when starting in local mode", async () => {
368
+ writeWranglerToml({
369
+ main: "index.js",
370
+ });
371
+ fs.writeFileSync("index.js", `export default {};`);
372
+ await runWrangler("dev --host some-host.com --local");
373
+ expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual(undefined);
374
+ });
375
+ });
376
+
377
+ describe("custom builds", () => {
378
+ it("should run a custom build before starting `dev`", async () => {
379
+ writeWranglerToml({
380
+ build: {
381
+ command: `node -e "console.log('custom build'); require('fs').writeFileSync('index.js', 'export default { fetch(){ return new Response(123) } }')"`,
382
+ },
383
+ });
384
+
385
+ await runWrangler("dev index.js");
386
+
387
+ expect(fs.readFileSync("index.js", "utf-8")).toMatchInlineSnapshot(
388
+ `"export default { fetch(){ return new Response(123) } }"`
389
+ );
390
+
391
+ // and the command would pass through
392
+ expect((Dev as jest.Mock).mock.calls[0][0].build).toEqual({
393
+ command:
394
+ "node -e \"console.log('custom build'); require('fs').writeFileSync('index.js', 'export default { fetch(){ return new Response(123) } }')\"",
395
+ cwd: undefined,
396
+ watch_dir: "src",
397
+ });
398
+ expect(std.out).toMatchInlineSnapshot(
399
+ `"Running custom build: node -e \\"console.log('custom build'); require('fs').writeFileSync('index.js', 'export default { fetch(){ return new Response(123) } }')\\""`
400
+ );
401
+ expect(std.err).toMatchInlineSnapshot(`""`);
402
+ expect(std.warn).toMatchInlineSnapshot(`""`);
403
+ });
404
+
405
+ if (process.platform !== "win32") {
406
+ it("should run a custom build of multiple steps combined by && before starting `dev`", async () => {
407
+ writeWranglerToml({
408
+ build: {
409
+ command: `echo "custom build" && echo "export default { fetch(){ return new Response(123) } }" > index.js`,
410
+ },
411
+ });
412
+
413
+ await runWrangler("dev index.js");
414
+
415
+ expect(fs.readFileSync("index.js", "utf-8")).toMatchInlineSnapshot(`
416
+ "export default { fetch(){ return new Response(123) } }
417
+ "
418
+ `);
419
+
420
+ expect(std.out).toMatchInlineSnapshot(
421
+ `"Running custom build: echo \\"custom build\\" && echo \\"export default { fetch(){ return new Response(123) } }\\" > index.js"`
422
+ );
423
+ expect(std.err).toMatchInlineSnapshot(`""`);
424
+ expect(std.warn).toMatchInlineSnapshot(`""`);
425
+ });
426
+ }
427
+
428
+ it("should throw an error if the entry doesn't exist after the build finishes", async () => {
429
+ writeWranglerToml({
430
+ main: "index.js",
431
+ build: {
432
+ command: `node -e "console.log('custom build');"`,
433
+ },
434
+ });
435
+
436
+ await expect(runWrangler("dev")).rejects
437
+ .toThrowErrorMatchingInlineSnapshot(`
438
+ "The expected output file at \\"index.js\\" was not found after running custom build: node -e \\"console.log('custom build');\\".
439
+ The \`main\` property in wrangler.toml should point to the file generated by the custom build."
440
+ `);
441
+ expect(std.out).toMatchInlineSnapshot(`
442
+ "Running custom build: node -e \\"console.log('custom build');\\"
443
+
444
+ If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
445
+ `);
446
+ expect(std.err).toMatchInlineSnapshot(`
447
+ "X [ERROR] The expected output file at \\"index.js\\" was not found after running custom build: node -e \\"console.log('custom build');\\".
448
+
449
+ The \`main\` property in wrangler.toml should point to the file generated by the custom build.
450
+
451
+ "
452
+ `);
453
+ expect(std.warn).toMatchInlineSnapshot(`""`);
454
+ });
455
+ });
456
+
457
+ describe("upstream-protocol", () => {
458
+ it("should default upstream-protocol to `https`", async () => {
459
+ writeWranglerToml({
460
+ main: "index.js",
461
+ });
462
+ fs.writeFileSync("index.js", `export default {};`);
463
+ await runWrangler("dev");
464
+ expect((Dev as jest.Mock).mock.calls[0][0].upstreamProtocol).toEqual(
465
+ "https"
466
+ );
467
+ expect(std.out).toMatchInlineSnapshot(`""`);
468
+ expect(std.warn).toMatchInlineSnapshot(`""`);
469
+ expect(std.err).toMatchInlineSnapshot(`""`);
470
+ });
471
+
472
+ it("should warn if `--upstream-protocol=http` is used", async () => {
473
+ writeWranglerToml({
474
+ main: "index.js",
475
+ });
476
+ fs.writeFileSync("index.js", `export default {};`);
477
+ await runWrangler("dev --upstream-protocol=http");
478
+ expect((Dev as jest.Mock).mock.calls[0][0].upstreamProtocol).toEqual(
479
+ "http"
480
+ );
481
+ expect(std.out).toMatchInlineSnapshot(`""`);
482
+ expect(std.warn).toMatchInlineSnapshot(`
483
+ "â–² [WARNING] Setting upstream-protocol to http is not currently implemented.
484
+
485
+ If this is required in your project, please add your use case to the following issue:
486
+ https://github.com/cloudflare/wrangler2/issues/583.
487
+
488
+ "
489
+ `);
490
+ expect(std.err).toMatchInlineSnapshot(`""`);
491
+ });
492
+ });
493
+
494
+ describe("local-protocol", () => {
495
+ it("should default local-protocol to `http`", async () => {
496
+ writeWranglerToml({
497
+ main: "index.js",
498
+ });
499
+ fs.writeFileSync("index.js", `export default {};`);
500
+ await runWrangler("dev");
501
+ expect((Dev as jest.Mock).mock.calls[0][0].localProtocol).toEqual("http");
502
+ expect(std.out).toMatchInlineSnapshot(`""`);
503
+ expect(std.warn).toMatchInlineSnapshot(`""`);
504
+ expect(std.err).toMatchInlineSnapshot(`""`);
505
+ });
506
+
507
+ it("should use `local_protocol` from `wrangler.toml`, if available", async () => {
508
+ writeWranglerToml({
509
+ main: "index.js",
510
+ dev: {
511
+ local_protocol: "https",
512
+ },
513
+ });
514
+ fs.writeFileSync("index.js", `export default {};`);
515
+ await runWrangler("dev");
516
+ expect((Dev as jest.Mock).mock.calls[0][0].localProtocol).toEqual(
517
+ "https"
518
+ );
519
+ expect(std.out).toMatchInlineSnapshot(`""`);
520
+ expect(std.warn).toMatchInlineSnapshot(`""`);
521
+ expect(std.err).toMatchInlineSnapshot(`""`);
522
+ });
523
+
524
+ it("should use --local-protocol command line arg, if provided", async () => {
525
+ // Here we show that the command line overrides the wrangler.toml by
526
+ // setting the config to https, and then setting it back to http on the command line.
527
+ writeWranglerToml({
528
+ main: "index.js",
529
+ dev: {
530
+ local_protocol: "https",
531
+ },
532
+ });
533
+ fs.writeFileSync("index.js", `export default {};`);
534
+ await runWrangler("dev --local-protocol=http");
535
+ expect((Dev as jest.Mock).mock.calls[0][0].localProtocol).toEqual("http");
536
+ expect(std.out).toMatchInlineSnapshot(`""`);
537
+ expect(std.warn).toMatchInlineSnapshot(`""`);
538
+ expect(std.err).toMatchInlineSnapshot(`""`);
539
+ });
540
+ });
541
+
542
+ describe("ip", () => {
543
+ it("should default ip to localhost", async () => {
544
+ writeWranglerToml({
545
+ main: "index.js",
546
+ });
547
+ fs.writeFileSync("index.js", `export default {};`);
548
+ await runWrangler("dev");
549
+ expect((Dev as jest.Mock).mock.calls[0][0].ip).toEqual("localhost");
550
+ expect(std.out).toMatchInlineSnapshot(`""`);
551
+ expect(std.warn).toMatchInlineSnapshot(`""`);
552
+ expect(std.err).toMatchInlineSnapshot(`""`);
553
+ });
554
+
555
+ it("should use to `ip` from `wrangler.toml`, if available", async () => {
556
+ writeWranglerToml({
557
+ main: "index.js",
558
+ dev: {
559
+ ip: "0.0.0.0",
560
+ },
561
+ });
562
+ fs.writeFileSync("index.js", `export default {};`);
563
+ await runWrangler("dev");
564
+ expect((Dev as jest.Mock).mock.calls[0][0].ip).toEqual("0.0.0.0");
565
+ expect(std.out).toMatchInlineSnapshot(`""`);
566
+ expect(std.warn).toMatchInlineSnapshot(`""`);
567
+ expect(std.err).toMatchInlineSnapshot(`""`);
568
+ });
569
+
570
+ it("should use --ip command line arg, if provided", async () => {
571
+ writeWranglerToml({
572
+ main: "index.js",
573
+ dev: {
574
+ ip: "1.1.1.1",
575
+ },
576
+ });
577
+ fs.writeFileSync("index.js", `export default {};`);
578
+ await runWrangler("dev --ip=0.0.0.0");
579
+ expect((Dev as jest.Mock).mock.calls[0][0].ip).toEqual("0.0.0.0");
580
+ expect(std.out).toMatchInlineSnapshot(`""`);
581
+ expect(std.warn).toMatchInlineSnapshot(`""`);
582
+ expect(std.err).toMatchInlineSnapshot(`""`);
583
+ });
584
+ });
585
+
586
+ describe("port", () => {
587
+ it("should default port to 8787 if it is not in use", async () => {
588
+ writeWranglerToml({
589
+ main: "index.js",
590
+ });
591
+ fs.writeFileSync("index.js", `export default {};`);
592
+ await runWrangler("dev");
593
+ expect((Dev as jest.Mock).mock.calls[0][0].port).toEqual(8787);
594
+ expect(std.out).toMatchInlineSnapshot(`""`);
595
+ expect(std.warn).toMatchInlineSnapshot(`""`);
596
+ expect(std.err).toMatchInlineSnapshot(`""`);
597
+ });
598
+
599
+ it("should use to `port` from `wrangler.toml`, if available", async () => {
600
+ writeWranglerToml({
601
+ main: "index.js",
602
+ dev: {
603
+ port: 8888,
604
+ },
605
+ });
606
+ fs.writeFileSync("index.js", `export default {};`);
607
+ // Mock `getPort()` to resolve to a completely different port.
608
+ (getPort as jest.Mock).mockResolvedValue(98765);
609
+
610
+ await runWrangler("dev");
611
+ expect((Dev as jest.Mock).mock.calls[0][0].port).toEqual(8888);
612
+ expect(std.out).toMatchInlineSnapshot(`""`);
613
+ expect(std.warn).toMatchInlineSnapshot(`""`);
614
+ expect(std.err).toMatchInlineSnapshot(`""`);
615
+ });
616
+
617
+ it("should use --port command line arg, if provided", async () => {
618
+ writeWranglerToml({
619
+ main: "index.js",
620
+ dev: {
621
+ port: 8888,
622
+ },
623
+ });
624
+ fs.writeFileSync("index.js", `export default {};`);
625
+ // Mock `getPort()` to resolve to a completely different port.
626
+ (getPort as jest.Mock).mockResolvedValue(98765);
627
+
628
+ await runWrangler("dev --port=9999");
629
+ expect((Dev as jest.Mock).mock.calls[0][0].port).toEqual(9999);
630
+ expect(std.out).toMatchInlineSnapshot(`""`);
631
+ expect(std.warn).toMatchInlineSnapshot(`""`);
632
+ expect(std.err).toMatchInlineSnapshot(`""`);
633
+ });
634
+
635
+ it("should use a different port to the default if it is in use", async () => {
636
+ writeWranglerToml({
637
+ main: "index.js",
638
+ });
639
+ fs.writeFileSync("index.js", `export default {};`);
640
+ // Mock `getPort()` to resolve to a completely different port.
641
+ (getPort as jest.Mock).mockResolvedValue(98765);
642
+
643
+ await runWrangler("dev");
644
+ expect((Dev as jest.Mock).mock.calls[0][0].port).toEqual(98765);
645
+ expect(std.out).toMatchInlineSnapshot(`""`);
646
+ expect(std.warn).toMatchInlineSnapshot(`""`);
647
+ expect(std.err).toMatchInlineSnapshot(`""`);
648
+ });
649
+ });
650
+
651
+ describe("durable_objects", () => {
652
+ it("should warn if there is one or more remote Durable Object", async () => {
653
+ writeWranglerToml({
654
+ main: "index.js",
655
+ durable_objects: {
656
+ bindings: [
657
+ { name: "NAME_1", class_name: "CLASS_1" },
658
+ {
659
+ name: "NAME_2",
660
+ class_name: "CLASS_2",
661
+ script_name: "SCRIPT_A",
662
+ },
663
+ { name: "NAME_3", class_name: "CLASS_3" },
664
+ {
665
+ name: "NAME_4",
666
+ class_name: "CLASS_4",
667
+ script_name: "SCRIPT_B",
668
+ },
669
+ ],
670
+ },
671
+ });
672
+ fs.writeFileSync("index.js", `export default {};`);
673
+ await runWrangler("dev");
674
+ expect((Dev as jest.Mock).mock.calls[0][0].ip).toEqual("localhost");
675
+ expect(std.out).toMatchInlineSnapshot(`""`);
676
+ expect(std.warn).toMatchInlineSnapshot(`
677
+ "â–² [WARNING] WARNING: You have Durable Object bindings that are not defined locally in the worker being developed.
678
+
679
+ Be aware that changes to the data stored in these Durable Objects will be permanent and affect the
680
+ live instances.
681
+ Remote Durable Objects that are affected:
682
+ - {\\"name\\":\\"NAME_2\\",\\"class_name\\":\\"CLASS_2\\",\\"script_name\\":\\"SCRIPT_A\\"}
683
+ - {\\"name\\":\\"NAME_4\\",\\"class_name\\":\\"CLASS_4\\",\\"script_name\\":\\"SCRIPT_B\\"}
684
+
685
+ "
686
+ `);
687
+ expect(std.err).toMatchInlineSnapshot(`""`);
688
+ });
689
+ });
690
+
691
+ describe(".dev.vars", () => {
692
+ it("should override `vars` bindings from `wrangler.toml` with values in `.dev.vars`", async () => {
693
+ fs.writeFileSync("index.js", `export default {};`);
694
+
695
+ const localVarsEnvContent = dedent`
696
+ # Preceding comment
697
+ VAR_1="var #1 value" # End of line comment
698
+ VAR_3="var #3 value"
699
+ VAR_MULTI_LINE_1="A: line 1
700
+ line 2"
701
+ VAR_MULTI_LINE_2="B: line 1\\nline 2"
702
+ EMPTY=
703
+ UNQUOTED= unquoted value
704
+ `;
705
+ fs.writeFileSync(".dev.vars", localVarsEnvContent, "utf8");
706
+
707
+ writeWranglerToml({
708
+ main: "index.js",
709
+ vars: {
710
+ VAR_1: "original value 1",
711
+ VAR_2: "original value 2", // should not get overridden
712
+ VAR_3: "original value 3",
713
+ VAR_MULTI_LINE_1: "original multi-line 1",
714
+ VAR_MULTI_LINE_2: "original multi-line 2",
715
+ EMPTY: "original empty",
716
+ UNQUOTED: "original unquoted",
717
+ },
718
+ });
719
+ await runWrangler("dev");
720
+ const varBindings: Record<string, unknown> = (Dev as jest.Mock).mock
721
+ .calls[0][0].bindings.vars;
722
+
723
+ expect(varBindings).toEqual({
724
+ VAR_1: "var #1 value",
725
+ VAR_2: "original value 2",
726
+ VAR_3: "var #3 value",
727
+ VAR_MULTI_LINE_1: "A: line 1\nline 2",
728
+ VAR_MULTI_LINE_2: "B: line 1\nline 2",
729
+ EMPTY: "",
730
+ UNQUOTED: "unquoted value", // Note that whitespace is trimmed
731
+ });
732
+ expect(std.out).toMatchInlineSnapshot(
733
+ `"Using vars defined in .dev.vars"`
734
+ );
735
+ expect(std.warn).toMatchInlineSnapshot(`""`);
736
+ expect(std.err).toMatchInlineSnapshot(`""`);
737
+ });
738
+ });
739
+
740
+ describe("site", () => {
741
+ it("should error if --site is used with no value", async () => {
742
+ await expect(
743
+ runWrangler("dev --site")
744
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
745
+ `"Not enough arguments following: site"`
746
+ );
747
+
748
+ expect(std).toMatchInlineSnapshot(`
749
+ Object {
750
+ "debug": "",
751
+ "err": "wrangler dev [script]
752
+
753
+ 👂 Start a local server for developing your worker
754
+
755
+ Positionals:
756
+ script The path to an entry point for your worker [string]
757
+
758
+ Flags:
759
+ -c, --config Path to .toml configuration file [string]
760
+ -h, --help Show help [boolean]
761
+ -v, --version Show version number [boolean]
762
+
763
+ Options:
764
+ --name Name of the worker [string]
765
+ --format Choose an entry type [deprecated] [choices: \\"modules\\", \\"service-worker\\"]
766
+ -e, --env Perform on a specific environment [string]
767
+ --compatibility-date Date to use for compatibility checks [string]
768
+ --compatibility-flags, --compatibility-flag Flags to use for compatibility checks [array]
769
+ --latest Use the latest version of the worker runtime [boolean] [default: true]
770
+ --ip IP address to listen on, defaults to \`localhost\` [string]
771
+ --port Port to listen on [number]
772
+ --inspector-port Port for devtools to connect to [number]
773
+ --routes, --route Routes to upload [array]
774
+ --host Host to forward requests to, defaults to the zone of project [string]
775
+ --local-protocol Protocol to listen to requests on, defaults to http. [choices: \\"http\\", \\"https\\"]
776
+ --experimental-public Static assets to be served [string]
777
+ --site Root folder of static assets for Workers Sites [string]
778
+ --site-include Array of .gitignore-style patterns that match file or directory names from the sites directory. Only matched items will be uploaded. [array]
779
+ --site-exclude Array of .gitignore-style patterns that match file or directory names from the sites directory. Matched items will not be uploaded. [array]
780
+ --upstream-protocol Protocol to forward requests to host on, defaults to https. [choices: \\"http\\", \\"https\\"]
781
+ --jsx-factory The function that is called for each JSX element [string]
782
+ --jsx-fragment The function that is called for each JSX fragment [string]
783
+ --tsconfig Path to a custom tsconfig.json file [string]
784
+ -l, --local Run on my machine [boolean] [default: false]
785
+ --minify Minify the script [boolean]
786
+ --node-compat Enable node.js compatibility [boolean]
787
+ --experimental-enable-local-persistence Enable persistence for this session (only for local mode) [boolean]
788
+ --inspect Enable dev tools [deprecated] [boolean]
789
+ X [ERROR] Not enough arguments following: site
790
+
791
+ ",
792
+ "out": "
793
+ ",
794
+ "warn": "",
795
+ }
796
+ `);
797
+ });
798
+ });
799
+
800
+ describe("--inspect", () => {
801
+ it("should warn if --inspect is used", async () => {
802
+ fs.writeFileSync("index.js", `export default {};`);
803
+ await runWrangler("dev index.js --inspect");
804
+ expect(std).toMatchInlineSnapshot(`
805
+ Object {
806
+ "debug": "",
807
+ "err": "",
808
+ "out": "",
809
+ "warn": "â–² [WARNING] Passing --inspect is unnecessary, now you can always connect to devtools.
810
+
811
+ ",
812
+ }
813
+ `);
814
+ });
815
+ });
816
+ });
817
+
818
+ function mockGetZones(domain: string, zones: { id: string }[] = []) {
819
+ const removeMock = setMockResponse(
820
+ "/zones",
821
+ "GET",
822
+ (_urlPieces, _init, queryParams) => {
823
+ expect([...queryParams.entries()]).toEqual([["name", domain]]);
824
+ // 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.
825
+ removeMock();
826
+ return zones;
827
+ }
828
+ );
829
+ }