wrangler 0.0.0-ece06ea → 0.0.0-ed4f2ef

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 (109) hide show
  1. package/README.md +3 -3
  2. package/bin/wrangler.js +12 -3
  3. package/miniflare-dist/index.mjs +79 -0
  4. package/miniflare-dist/index.mjs.map +7 -0
  5. package/package.json +49 -22
  6. package/pages/functions/buildWorker.ts +62 -0
  7. package/pages/functions/filepath-routing.test.ts +223 -0
  8. package/pages/functions/filepath-routing.ts +181 -0
  9. package/pages/functions/identifiers.ts +78 -0
  10. package/pages/functions/routes.ts +151 -0
  11. package/pages/functions/template-worker.ts +130 -0
  12. package/src/__tests__/configuration.test.ts +2874 -0
  13. package/src/__tests__/dev.test.tsx +588 -0
  14. package/src/__tests__/guess-worker-format.test.ts +83 -0
  15. package/src/__tests__/{clipboardy-mock.js → helpers/clipboardy-mock.js} +0 -0
  16. package/src/__tests__/helpers/cmd-shim.d.ts +11 -0
  17. package/src/__tests__/helpers/faye-websocket.d.ts +6 -0
  18. package/src/__tests__/helpers/mock-account-id.ts +48 -0
  19. package/src/__tests__/helpers/mock-bin.ts +36 -0
  20. package/src/__tests__/helpers/mock-cfetch.ts +207 -0
  21. package/src/__tests__/helpers/mock-console.ts +70 -0
  22. package/src/__tests__/helpers/mock-dialogs.ts +65 -0
  23. package/src/__tests__/helpers/mock-kv.ts +33 -0
  24. package/src/__tests__/helpers/mock-oauth-flow.ts +178 -0
  25. package/src/__tests__/helpers/mock-stdin.ts +103 -0
  26. package/src/__tests__/helpers/mock-web-socket.ts +29 -0
  27. package/src/__tests__/helpers/run-in-tmp.ts +32 -0
  28. package/src/__tests__/helpers/run-wrangler.ts +16 -0
  29. package/src/__tests__/helpers/write-wrangler-toml.ts +17 -0
  30. package/src/__tests__/https-options.test.ts +127 -0
  31. package/src/__tests__/index.test.ts +953 -119
  32. package/src/__tests__/jest.setup.ts +74 -0
  33. package/src/__tests__/kv.test.ts +1380 -0
  34. package/src/__tests__/package-manager.test.ts +206 -0
  35. package/src/__tests__/pages.test.ts +180 -0
  36. package/src/__tests__/parse.test.ts +322 -0
  37. package/src/__tests__/publish.test.ts +3946 -0
  38. package/src/__tests__/r2.test.ts +198 -0
  39. package/src/__tests__/route.test.ts +45 -0
  40. package/src/__tests__/secret.test.ts +425 -0
  41. package/src/__tests__/sentry.test.ts +146 -0
  42. package/src/__tests__/tail.test.ts +638 -0
  43. package/src/__tests__/user.test.ts +83 -0
  44. package/src/__tests__/whoami.test.tsx +95 -0
  45. package/src/bundle.ts +155 -0
  46. package/src/cfetch/index.ts +104 -0
  47. package/src/cfetch/internal.ts +139 -0
  48. package/src/cli.ts +18 -7
  49. package/src/config/README.md +107 -0
  50. package/src/config/config.ts +229 -0
  51. package/src/config/diagnostics.ts +80 -0
  52. package/src/config/environment.ts +371 -0
  53. package/src/config/index.ts +62 -0
  54. package/src/config/validation-helpers.ts +557 -0
  55. package/src/config/validation.ts +1240 -0
  56. package/src/create-worker-preview.ts +173 -0
  57. package/src/create-worker-upload-form.ts +240 -0
  58. package/src/dev/dev.tsx +413 -0
  59. package/src/dev/local.tsx +287 -0
  60. package/src/dev/remote.tsx +209 -0
  61. package/src/dev/use-esbuild.ts +109 -0
  62. package/src/dialogs.tsx +6 -14
  63. package/src/entry.ts +256 -0
  64. package/src/environment-variables.ts +33 -0
  65. package/src/errors.ts +11 -0
  66. package/src/https-options.ts +122 -0
  67. package/src/index.tsx +1503 -959
  68. package/src/inspect.ts +624 -0
  69. package/src/intl-polyfill.d.ts +139 -0
  70. package/src/kv.ts +319 -0
  71. package/src/miniflare-cli/README.md +30 -0
  72. package/src/miniflare-cli/enum-keys.ts +17 -0
  73. package/src/miniflare-cli/index.ts +38 -0
  74. package/src/module-collection.ts +257 -0
  75. package/src/open-in-browser.ts +25 -0
  76. package/src/package-manager.ts +126 -0
  77. package/src/pages.tsx +1074 -212
  78. package/src/parse.ts +183 -0
  79. package/src/paths.ts +26 -0
  80. package/src/proxy.ts +455 -0
  81. package/src/publish.ts +326 -295
  82. package/src/r2.ts +50 -0
  83. package/src/reporting.ts +131 -0
  84. package/src/selfsigned.d.ts +29 -0
  85. package/src/sites.tsx +224 -59
  86. package/src/tail/filters.ts +279 -0
  87. package/src/tail/index.ts +299 -0
  88. package/src/tail/printing.ts +62 -0
  89. package/src/user.tsx +340 -203
  90. package/src/whoami.tsx +69 -0
  91. package/src/worker.ts +172 -0
  92. package/templates/new-worker.js +15 -0
  93. package/templates/new-worker.ts +15 -0
  94. package/{static-asset-facade.js → templates/static-asset-facade.js} +1 -1
  95. package/templates/tsconfig.json +105 -0
  96. package/wrangler-dist/cli.js +136988 -131174
  97. package/wrangler-dist/cli.js.map +3 -3
  98. package/src/__tests__/fixtures/init/.gitkeep +0 -0
  99. package/src/__tests__/mock-cfetch.js +0 -46
  100. package/src/api/form_data.ts +0 -158
  101. package/src/api/inspect.ts +0 -441
  102. package/src/api/preview.ts +0 -123
  103. package/src/api/worker.ts +0 -161
  104. package/src/cfetch.ts +0 -72
  105. package/src/config.ts +0 -124
  106. package/src/dev.tsx +0 -736
  107. package/src/kv.tsx +0 -211
  108. package/src/tail.tsx +0 -71
  109. package/src/util/fetch.ts +0 -74
@@ -0,0 +1,588 @@
1
+ import * as fs from "node:fs";
2
+ import { readFileSync } from "node:fs";
3
+ import patchConsole from "patch-console";
4
+ import Dev from "../dev/dev";
5
+ import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
6
+ import { setMockResponse, unsetAllMocks } from "./helpers/mock-cfetch";
7
+ import { mockConsoleMethods } from "./helpers/mock-console";
8
+ import { runInTempDir } from "./helpers/run-in-tmp";
9
+ import { runWrangler } from "./helpers/run-wrangler";
10
+ import writeWranglerToml from "./helpers/write-wrangler-toml";
11
+
12
+ describe("wrangler dev", () => {
13
+ mockAccountId();
14
+ mockApiToken();
15
+ runInTempDir();
16
+ const std = mockConsoleMethods();
17
+ afterEach(() => {
18
+ (Dev as jest.Mock).mockClear();
19
+ patchConsole(() => {});
20
+ unsetAllMocks();
21
+ });
22
+
23
+ describe("compatibility-date", () => {
24
+ it("should not warn if there is no wrangler.toml and no compatibility-date specified", async () => {
25
+ fs.writeFileSync("index.js", `export default {};`);
26
+ await runWrangler("dev index.js");
27
+ expect(std.out).toMatchInlineSnapshot(`""`);
28
+ expect(std.warn).toMatchInlineSnapshot(`""`);
29
+ expect(std.err).toMatchInlineSnapshot(`""`);
30
+ });
31
+
32
+ it("should warn if there is a wrangler.toml but no compatibility-date", async () => {
33
+ writeWranglerToml({
34
+ main: "index.js",
35
+ compatibility_date: undefined,
36
+ });
37
+ fs.writeFileSync("index.js", `export default {};`);
38
+ await runWrangler("dev");
39
+ const currentDate = new Date().toISOString().substring(0, 10);
40
+ expect(std.out).toMatchInlineSnapshot(`""`);
41
+ expect(std.warn.replaceAll(currentDate, "<current-date>"))
42
+ .toMatchInlineSnapshot(`
43
+ "No compatibility_date was specified. Using today's date: <current-date>.
44
+ Add one to your wrangler.toml file:
45
+ \`\`\`
46
+ compatibility_date = \\"<current-date>\\"
47
+ \`\`\`
48
+ or pass it in your terminal:
49
+ \`\`\`
50
+ --compatibility-date=<current-date>
51
+ \`\`\`
52
+ See https://developers.cloudflare.com/workers/platform/compatibility-dates for more information."
53
+ `);
54
+ expect(std.err).toMatchInlineSnapshot(`""`);
55
+ });
56
+
57
+ it("should not warn if there is a wrangler.toml but compatibility-date is specified at the command line", async () => {
58
+ writeWranglerToml({
59
+ main: "index.js",
60
+ compatibility_date: undefined,
61
+ });
62
+ fs.writeFileSync("index.js", `export default {};`);
63
+ await runWrangler("dev --compatibility-date=2020-01-01");
64
+ expect(std.out).toMatchInlineSnapshot(`""`);
65
+ expect(std.warn).toMatchInlineSnapshot(`""`);
66
+ expect(std.err).toMatchInlineSnapshot(`""`);
67
+ });
68
+ });
69
+
70
+ describe("entry-points", () => {
71
+ it("should error if there is no entry-point specified", async () => {
72
+ writeWranglerToml();
73
+
74
+ await expect(
75
+ runWrangler("dev")
76
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
77
+ `"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."`
78
+ );
79
+
80
+ expect(std.out).toMatchInlineSnapshot(`""`);
81
+ expect(std.err).toMatchInlineSnapshot(`
82
+ "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.
83
+
84
+ %s If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
85
+ `);
86
+ });
87
+
88
+ it("should use `main` from the top-level environment", async () => {
89
+ writeWranglerToml({
90
+ main: "index.js",
91
+ });
92
+ fs.writeFileSync("index.js", `export default {};`);
93
+ await runWrangler("dev");
94
+ expect((Dev as jest.Mock).mock.calls[0][0].entry.file).toMatch(
95
+ /index\.js$/
96
+ );
97
+ expect(std.out).toMatchInlineSnapshot(`""`);
98
+ expect(std.warn).toMatchInlineSnapshot(`""`);
99
+ expect(std.err).toMatchInlineSnapshot(`""`);
100
+ });
101
+
102
+ it("should use `main` from a named environment", async () => {
103
+ writeWranglerToml({
104
+ env: {
105
+ ENV1: {
106
+ main: "index.js",
107
+ },
108
+ },
109
+ });
110
+ fs.writeFileSync("index.js", `export default {};`);
111
+ await runWrangler("dev --env=ENV1");
112
+ expect((Dev as jest.Mock).mock.calls[0][0].entry.file).toMatch(
113
+ /index\.js$/
114
+ );
115
+ expect(std.out).toMatchInlineSnapshot(`""`);
116
+ expect(std.warn).toMatchInlineSnapshot(`""`);
117
+ expect(std.err).toMatchInlineSnapshot(`""`);
118
+ });
119
+
120
+ it("should use `main` from a named environment, rather than the top-level", async () => {
121
+ writeWranglerToml({
122
+ main: "other.js",
123
+ env: {
124
+ ENV1: {
125
+ main: "index.js",
126
+ },
127
+ },
128
+ });
129
+ fs.writeFileSync("index.js", `export default {};`);
130
+ await runWrangler("dev --env=ENV1");
131
+ expect((Dev as jest.Mock).mock.calls[0][0].entry.file).toMatch(
132
+ /index\.js$/
133
+ );
134
+ expect(std.out).toMatchInlineSnapshot(`""`);
135
+ expect(std.warn).toMatchInlineSnapshot(`""`);
136
+ expect(std.err).toMatchInlineSnapshot(`""`);
137
+ });
138
+ });
139
+
140
+ describe("host", () => {
141
+ it("should resolve a host to its zone", async () => {
142
+ writeWranglerToml({
143
+ main: "index.js",
144
+ });
145
+ fs.writeFileSync("index.js", `export default {};`);
146
+ mockGetZones("some-host.com", [{ id: "some-zone-id" }]);
147
+ await runWrangler("dev --host some-host.com");
148
+ expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
149
+ host: "some-host.com",
150
+ id: "some-zone-id",
151
+ });
152
+ });
153
+
154
+ it("should read wrangler.toml's dev.host", async () => {
155
+ writeWranglerToml({
156
+ main: "index.js",
157
+ dev: {
158
+ host: "some-host.com",
159
+ },
160
+ });
161
+ fs.writeFileSync("index.js", `export default {};`);
162
+ mockGetZones("some-host.com", [{ id: "some-zone-id" }]);
163
+ await runWrangler("dev");
164
+ expect((Dev as jest.Mock).mock.calls[0][0].zone.host).toEqual(
165
+ "some-host.com"
166
+ );
167
+ });
168
+
169
+ it("should read --route", async () => {
170
+ writeWranglerToml({
171
+ main: "index.js",
172
+ });
173
+ fs.writeFileSync("index.js", `export default {};`);
174
+ mockGetZones("some-host.com", [{ id: "some-zone-id" }]);
175
+ await runWrangler("dev --route http://some-host.com/some/path/*");
176
+ expect((Dev as jest.Mock).mock.calls[0][0].zone.host).toEqual(
177
+ "some-host.com"
178
+ );
179
+ });
180
+
181
+ it("should read wrangler.toml's routes", async () => {
182
+ writeWranglerToml({
183
+ main: "index.js",
184
+ routes: [
185
+ "http://some-host.com/some/path/*",
186
+ "http://some-other-host.com/path/*",
187
+ ],
188
+ });
189
+ fs.writeFileSync("index.js", `export default {};`);
190
+ mockGetZones("some-host.com", [{ id: "some-zone-id" }]);
191
+ await runWrangler("dev");
192
+ expect((Dev as jest.Mock).mock.calls[0][0].zone.host).toEqual(
193
+ "some-host.com"
194
+ );
195
+ });
196
+
197
+ it("should read wrangler.toml's environment specific routes", async () => {
198
+ writeWranglerToml({
199
+ main: "index.js",
200
+ routes: [
201
+ "http://a-host.com/some/path/*",
202
+ "http://another-host.com/path/*",
203
+ ],
204
+ env: {
205
+ staging: {
206
+ routes: [
207
+ "http://some-host.com/some/path/*",
208
+ "http://some-other-host.com/path/*",
209
+ ],
210
+ },
211
+ },
212
+ });
213
+ fs.writeFileSync("index.js", `export default {};`);
214
+ mockGetZones("some-host.com", [{ id: "some-zone-id" }]);
215
+ await runWrangler("dev --env staging");
216
+ expect((Dev as jest.Mock).mock.calls[0][0].zone.host).toEqual(
217
+ "some-host.com"
218
+ );
219
+ });
220
+
221
+ it("given a long host, it should use the longest subdomain that resolves to a zone", async () => {
222
+ writeWranglerToml({
223
+ main: "index.js",
224
+ });
225
+ fs.writeFileSync("index.js", `export default {};`);
226
+ mockGetZones("111.222.333.some-host.com", []);
227
+ mockGetZones("222.333.some-host.com", []);
228
+ mockGetZones("333.some-host.com", [{ id: "some-zone-id" }]);
229
+ await runWrangler("dev --host 111.222.333.some-host.com");
230
+ expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
231
+ host: "111.222.333.some-host.com",
232
+ id: "some-zone-id",
233
+ });
234
+ });
235
+
236
+ it("should, in order, use args.host/config.dev.host/args.routes/(config.route|config.routes)", async () => {
237
+ // This test might seem like it's testing implementation details, but let's be specific and consider it a spec
238
+
239
+ fs.writeFileSync("index.js", `export default {};`);
240
+
241
+ // config.routes
242
+ mockGetZones("5.some-host.com", [{ id: "some-zone-id-5" }]);
243
+ writeWranglerToml({
244
+ main: "index.js",
245
+ routes: ["http://5.some-host.com/some/path/*"],
246
+ });
247
+ await runWrangler("dev");
248
+ expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
249
+ host: "5.some-host.com",
250
+ id: "some-zone-id-5",
251
+ });
252
+ (Dev as jest.Mock).mockClear();
253
+
254
+ // config.route
255
+ mockGetZones("4.some-host.com", [{ id: "some-zone-id-4" }]);
256
+ writeWranglerToml({
257
+ main: "index.js",
258
+ route: "https://4.some-host.com/some/path/*",
259
+ });
260
+ await runWrangler("dev");
261
+ expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
262
+ host: "4.some-host.com",
263
+ id: "some-zone-id-4",
264
+ });
265
+ (Dev as jest.Mock).mockClear();
266
+
267
+ // --routes
268
+ mockGetZones("3.some-host.com", [{ id: "some-zone-id-3" }]);
269
+ writeWranglerToml({
270
+ main: "index.js",
271
+ route: "https://4.some-host.com/some/path/*",
272
+ });
273
+ await runWrangler("dev --routes http://3.some-host.com/some/path/*");
274
+ expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
275
+ host: "3.some-host.com",
276
+ id: "some-zone-id-3",
277
+ });
278
+ (Dev as jest.Mock).mockClear();
279
+
280
+ // config.dev.host
281
+ mockGetZones("2.some-host.com", [{ id: "some-zone-id-2" }]);
282
+ writeWranglerToml({
283
+ main: "index.js",
284
+ dev: {
285
+ host: `2.some-host.com`,
286
+ },
287
+ route: "4.some-host.com/some/path/*",
288
+ });
289
+ await runWrangler("dev --routes http://3.some-host.com/some/path/*");
290
+ expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
291
+ host: "2.some-host.com",
292
+ id: "some-zone-id-2",
293
+ });
294
+ (Dev as jest.Mock).mockClear();
295
+
296
+ // --host
297
+ mockGetZones("1.some-host.com", [{ id: "some-zone-id-1" }]);
298
+ writeWranglerToml({
299
+ main: "index.js",
300
+ dev: {
301
+ host: `2.some-host.com`,
302
+ },
303
+ route: "4.some-host.com/some/path/*",
304
+ });
305
+ await runWrangler(
306
+ "dev --routes http://3.some-host.com/some/path/* --host 1.some-host.com"
307
+ );
308
+ expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
309
+ host: "1.some-host.com",
310
+ id: "some-zone-id-1",
311
+ });
312
+ (Dev as jest.Mock).mockClear();
313
+ });
314
+
315
+ it("should error if a host can't resolve to a zone", async () => {
316
+ writeWranglerToml({
317
+ main: "index.js",
318
+ });
319
+ fs.writeFileSync("index.js", `export default {};`);
320
+ mockGetZones("some-host.com", []);
321
+ await expect(
322
+ runWrangler("dev --host some-host.com")
323
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
324
+ `"Could not find zone for some-host.com"`
325
+ );
326
+ });
327
+
328
+ it("should not try to resolve a zone when starting in local mode", async () => {
329
+ writeWranglerToml({
330
+ main: "index.js",
331
+ });
332
+ fs.writeFileSync("index.js", `export default {};`);
333
+ await runWrangler("dev --host some-host.com --local");
334
+ expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual(undefined);
335
+ });
336
+ });
337
+
338
+ describe("custom builds", () => {
339
+ it("should run a custom build before starting `dev`", async () => {
340
+ writeWranglerToml({
341
+ build: {
342
+ command: `node -e "console.log('custom build'); require('fs').writeFileSync('index.js', 'export default { fetch(){ return new Response(123) } }')"`,
343
+ },
344
+ });
345
+
346
+ await runWrangler("dev index.js");
347
+
348
+ expect(readFileSync("index.js", "utf-8")).toMatchInlineSnapshot(
349
+ `"export default { fetch(){ return new Response(123) } }"`
350
+ );
351
+
352
+ // and the command would pass through
353
+ expect((Dev as jest.Mock).mock.calls[0][0].build).toEqual({
354
+ command:
355
+ "node -e \"console.log('custom build'); require('fs').writeFileSync('index.js', 'export default { fetch(){ return new Response(123) } }')\"",
356
+ cwd: undefined,
357
+ watch_dir: "src",
358
+ });
359
+ expect(std.out).toMatchInlineSnapshot(
360
+ `"running: node -e \\"console.log('custom build'); require('fs').writeFileSync('index.js', 'export default { fetch(){ return new Response(123) } }')\\""`
361
+ );
362
+ expect(std.err).toMatchInlineSnapshot(`""`);
363
+ expect(std.warn).toMatchInlineSnapshot(`""`);
364
+ });
365
+
366
+ if (process.platform !== "win32") {
367
+ it("should run a custom build of multiple steps combined by && before starting `dev`", async () => {
368
+ writeWranglerToml({
369
+ build: {
370
+ command: `echo "custom build" && echo "export default { fetch(){ return new Response(123) } }" > index.js`,
371
+ },
372
+ });
373
+
374
+ await runWrangler("dev index.js");
375
+
376
+ expect(readFileSync("index.js", "utf-8")).toMatchInlineSnapshot(`
377
+ "export default { fetch(){ return new Response(123) } }
378
+ "
379
+ `);
380
+
381
+ expect(std.out).toMatchInlineSnapshot(
382
+ `"running: echo \\"custom build\\" && echo \\"export default { fetch(){ return new Response(123) } }\\" > index.js"`
383
+ );
384
+ expect(std.err).toMatchInlineSnapshot(`""`);
385
+ expect(std.warn).toMatchInlineSnapshot(`""`);
386
+ });
387
+ }
388
+
389
+ it("should throw an error if the entry doesn't exist after the build finishes", async () => {
390
+ writeWranglerToml({
391
+ main: "index.js",
392
+ build: {
393
+ command: `node -e "console.log('custom build');"`,
394
+ },
395
+ });
396
+
397
+ await expect(
398
+ runWrangler("dev")
399
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
400
+ `"Could not resolve \\"index.js\\" after running custom build: node -e \\"console.log('custom build');\\""`
401
+ );
402
+ expect(std.out).toMatchInlineSnapshot(
403
+ `"running: node -e \\"console.log('custom build');\\""`
404
+ );
405
+ expect(std.err).toMatchInlineSnapshot(`
406
+ "Could not resolve \\"index.js\\" after running custom build: node -e \\"console.log('custom build');\\"
407
+
408
+ %s If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
409
+ `);
410
+ expect(std.warn).toMatchInlineSnapshot(`""`);
411
+ });
412
+ });
413
+
414
+ describe("upstream-protocol", () => {
415
+ it("should default upstream-protocol to `https`", async () => {
416
+ writeWranglerToml({
417
+ main: "index.js",
418
+ });
419
+ fs.writeFileSync("index.js", `export default {};`);
420
+ await runWrangler("dev");
421
+ expect((Dev as jest.Mock).mock.calls[0][0].upstreamProtocol).toEqual(
422
+ "https"
423
+ );
424
+ expect(std.out).toMatchInlineSnapshot(`""`);
425
+ expect(std.warn).toMatchInlineSnapshot(`""`);
426
+ expect(std.err).toMatchInlineSnapshot(`""`);
427
+ });
428
+
429
+ it("should warn if `--upstream-protocol=http` is used", async () => {
430
+ writeWranglerToml({
431
+ main: "index.js",
432
+ });
433
+ fs.writeFileSync("index.js", `export default {};`);
434
+ await runWrangler("dev --upstream-protocol=http");
435
+ expect((Dev as jest.Mock).mock.calls[0][0].upstreamProtocol).toEqual(
436
+ "http"
437
+ );
438
+ expect(std.out).toMatchInlineSnapshot(`""`);
439
+ expect(std.warn).toMatchInlineSnapshot(`
440
+ "Setting upstream-protocol to http is not currently implemented.
441
+ If this is required in your project, please add your use case to the following issue:
442
+ https://github.com/cloudflare/wrangler2/issues/583."
443
+ `);
444
+ expect(std.err).toMatchInlineSnapshot(`""`);
445
+ });
446
+ });
447
+
448
+ describe("local-protocol", () => {
449
+ it("should default local-protocol to `http`", async () => {
450
+ writeWranglerToml({
451
+ main: "index.js",
452
+ });
453
+ fs.writeFileSync("index.js", `export default {};`);
454
+ await runWrangler("dev");
455
+ expect((Dev as jest.Mock).mock.calls[0][0].localProtocol).toEqual("http");
456
+ expect(std.out).toMatchInlineSnapshot(`""`);
457
+ expect(std.warn).toMatchInlineSnapshot(`""`);
458
+ expect(std.err).toMatchInlineSnapshot(`""`);
459
+ });
460
+
461
+ it("should use `local_protocol` from `wrangler.toml`, if available", async () => {
462
+ writeWranglerToml({
463
+ main: "index.js",
464
+ dev: {
465
+ local_protocol: "https",
466
+ },
467
+ });
468
+ fs.writeFileSync("index.js", `export default {};`);
469
+ await runWrangler("dev");
470
+ expect((Dev as jest.Mock).mock.calls[0][0].localProtocol).toEqual(
471
+ "https"
472
+ );
473
+ expect(std.out).toMatchInlineSnapshot(`""`);
474
+ expect(std.warn).toMatchInlineSnapshot(`""`);
475
+ expect(std.err).toMatchInlineSnapshot(`""`);
476
+ });
477
+
478
+ it("should use --local-protocol command line arg, if provided", async () => {
479
+ // Here we show that the command line overrides the wrangler.toml by
480
+ // setting the config to https, and then setting it back to http on the command line.
481
+ writeWranglerToml({
482
+ main: "index.js",
483
+ dev: {
484
+ local_protocol: "https",
485
+ },
486
+ });
487
+ fs.writeFileSync("index.js", `export default {};`);
488
+ await runWrangler("dev --local-protocol=http");
489
+ expect((Dev as jest.Mock).mock.calls[0][0].localProtocol).toEqual("http");
490
+ expect(std.out).toMatchInlineSnapshot(`""`);
491
+ expect(std.warn).toMatchInlineSnapshot(`""`);
492
+ expect(std.err).toMatchInlineSnapshot(`""`);
493
+ });
494
+ });
495
+
496
+ describe("ip", () => {
497
+ it("should default ip to localhost", async () => {
498
+ writeWranglerToml({
499
+ main: "index.js",
500
+ });
501
+ fs.writeFileSync("index.js", `export default {};`);
502
+ await runWrangler("dev");
503
+ expect((Dev as jest.Mock).mock.calls[0][0].ip).toEqual("localhost");
504
+ expect(std.out).toMatchInlineSnapshot(`""`);
505
+ expect(std.warn).toMatchInlineSnapshot(`""`);
506
+ expect(std.err).toMatchInlineSnapshot(`""`);
507
+ });
508
+
509
+ it("should use to `ip` from `wrangler.toml`, if available", async () => {
510
+ writeWranglerToml({
511
+ main: "index.js",
512
+ dev: {
513
+ ip: "0.0.0.0",
514
+ },
515
+ });
516
+ fs.writeFileSync("index.js", `export default {};`);
517
+ await runWrangler("dev");
518
+ expect((Dev as jest.Mock).mock.calls[0][0].ip).toEqual("0.0.0.0");
519
+ expect(std.out).toMatchInlineSnapshot(`""`);
520
+ expect(std.warn).toMatchInlineSnapshot(`""`);
521
+ expect(std.err).toMatchInlineSnapshot(`""`);
522
+ });
523
+
524
+ it("should use --ip command line arg, if provided", async () => {
525
+ writeWranglerToml({
526
+ main: "index.js",
527
+ dev: {
528
+ ip: "1.1.1.1",
529
+ },
530
+ });
531
+ fs.writeFileSync("index.js", `export default {};`);
532
+ await runWrangler("dev --ip=0.0.0.0");
533
+ expect((Dev as jest.Mock).mock.calls[0][0].ip).toEqual("0.0.0.0");
534
+ expect(std.out).toMatchInlineSnapshot(`""`);
535
+ expect(std.warn).toMatchInlineSnapshot(`""`);
536
+ expect(std.err).toMatchInlineSnapshot(`""`);
537
+ });
538
+ });
539
+
540
+ describe("durable_objects", () => {
541
+ it("should warn if there is one or more remote Durable Object", async () => {
542
+ writeWranglerToml({
543
+ main: "index.js",
544
+ durable_objects: {
545
+ bindings: [
546
+ { name: "NAME_1", class_name: "CLASS_1" },
547
+ {
548
+ name: "NAME_2",
549
+ class_name: "CLASS_2",
550
+ script_name: "SCRIPT_A",
551
+ },
552
+ { name: "NAME_3", class_name: "CLASS_3" },
553
+ {
554
+ name: "NAME_4",
555
+ class_name: "CLASS_4",
556
+ script_name: "SCRIPT_B",
557
+ },
558
+ ],
559
+ },
560
+ });
561
+ fs.writeFileSync("index.js", `export default {};`);
562
+ await runWrangler("dev");
563
+ expect((Dev as jest.Mock).mock.calls[0][0].ip).toEqual("localhost");
564
+ expect(std.out).toMatchInlineSnapshot(`""`);
565
+ expect(std.warn).toMatchInlineSnapshot(`
566
+ "WARNING: You have Durable Object bindings, which are not defined locally in the worker being developed.
567
+ Be aware that changes to the data stored in these Durable Objects will be permanent and affect the live instances.
568
+ Remote Durable Objects that are affected:
569
+ - {\\"name\\":\\"NAME_2\\",\\"class_name\\":\\"CLASS_2\\",\\"script_name\\":\\"SCRIPT_A\\"}
570
+ - {\\"name\\":\\"NAME_4\\",\\"class_name\\":\\"CLASS_4\\",\\"script_name\\":\\"SCRIPT_B\\"}"
571
+ `);
572
+ expect(std.err).toMatchInlineSnapshot(`""`);
573
+ });
574
+ });
575
+ });
576
+
577
+ function mockGetZones(domain: string, zones: { id: string }[] = []) {
578
+ const removeMock = setMockResponse(
579
+ "/zones",
580
+ "GET",
581
+ (_urlPieces, _init, queryParams) => {
582
+ expect([...queryParams.entries()]).toEqual([["name", domain]]);
583
+ // 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.
584
+ removeMock();
585
+ return zones;
586
+ }
587
+ );
588
+ }
@@ -0,0 +1,83 @@
1
+ import { writeFile } from "fs/promises";
2
+ import path from "path";
3
+ import guessWorkerFormat from "../entry";
4
+ import { mockConsoleMethods } from "./helpers/mock-console";
5
+ import { runInTempDir } from "./helpers/run-in-tmp";
6
+
7
+ describe("guess worker format", () => {
8
+ runInTempDir();
9
+ const std = mockConsoleMethods();
10
+ it('should detect a "modules" worker', async () => {
11
+ await writeFile("./index.ts", "export default {};");
12
+ // Note that this isn't actually a valid worker, because it's missing
13
+ // a fetch handler. Regardless, our heuristic is simply to check for exports.
14
+ const guess = await guessWorkerFormat(
15
+ path.join(process.cwd(), "./index.ts"),
16
+ process.cwd(),
17
+ undefined
18
+ );
19
+ expect(guess).toBe("modules");
20
+ });
21
+
22
+ it('should detect a "service-worker" worker', async () => {
23
+ await writeFile("./index.ts", "");
24
+ // Note that this isn't actually a valid worker, because it's missing
25
+ // a fetch listener. Regardless, our heuristic is simply to check for
26
+ // the lack of exports.
27
+ const guess = await guessWorkerFormat(
28
+ path.join(process.cwd(), "./index.ts"),
29
+ process.cwd(),
30
+ undefined
31
+ );
32
+ expect(guess).toBe("service-worker");
33
+ });
34
+
35
+ it("should throw an error when the hint doesn't match the guess (modules - service-worker)", async () => {
36
+ await writeFile("./index.ts", "export default {};");
37
+ await expect(
38
+ guessWorkerFormat(
39
+ path.join(process.cwd(), "./index.ts"),
40
+ process.cwd(),
41
+ "service-worker"
42
+ )
43
+ ).rejects.toThrow(
44
+ "You configured this worker to be a 'service-worker', but the file you are trying to build appears to have a `default` export like a module worker. Please pass `--format modules`, or simply remove the configuration."
45
+ );
46
+ });
47
+
48
+ it("should throw an error when the hint doesn't match the guess (service-worker - modules)", async () => {
49
+ await writeFile("./index.ts", "");
50
+ await expect(
51
+ guessWorkerFormat(
52
+ path.join(process.cwd(), "./index.ts"),
53
+ process.cwd(),
54
+ "modules"
55
+ )
56
+ ).rejects.toThrow(
57
+ "You configured this worker to be 'modules', but the file you are trying to build doesn't export a handler. Please pass `--format service-worker`, or simply remove the configuration."
58
+ );
59
+ });
60
+
61
+ it("should not error if a .js entry point has jsx", async () => {
62
+ await writeFile("./index.js", "console.log(<div/>)");
63
+ const guess = await guessWorkerFormat(
64
+ path.join(process.cwd(), "./index.js"),
65
+ process.cwd(),
66
+ undefined
67
+ );
68
+ expect(guess).toBe("service-worker");
69
+ });
70
+
71
+ it("logs a warning when a worker has exports, but not a default one", async () => {
72
+ await writeFile("./index.ts", "export const foo = 1;");
73
+ const guess = await guessWorkerFormat(
74
+ path.join(process.cwd(), "./index.ts"),
75
+ process.cwd(),
76
+ undefined
77
+ );
78
+ expect(guess).toBe("service-worker");
79
+ expect(std.warn).toMatchInlineSnapshot(
80
+ `"The entrypoint index.ts has exports like an ES Module, but hasn't defined a default export like a module worker normally would. Building the worker using \\"service-worker\\" format..."`
81
+ );
82
+ });
83
+ });
@@ -0,0 +1,11 @@
1
+ /**
2
+ * The typings file available at `@types/cmd-shim` are out of date.
3
+ */
4
+ module "cmd-shim" {
5
+ /**
6
+ *
7
+ * Create a cmd shim at `to` for the command line program at `from`.
8
+ *
9
+ */
10
+ export default function cmdShim(from: string, to: string): Promise<void>;
11
+ }
@@ -0,0 +1,6 @@
1
+ module "faye-websocket" {
2
+ /**
3
+ * Standards-compliant WebSocket client and server.
4
+ */
5
+ export default WebSocket;
6
+ }