wrangler 0.0.0-e492c10 → 0.0.0-e4bb5f36

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 (177) hide show
  1. package/Cloudflare_CA.pem +18 -0
  2. package/README.md +30 -12
  3. package/bin/wrangler.js +113 -39
  4. package/kv-asset-handler.js +1 -0
  5. package/package.json +194 -149
  6. package/templates/__tests__/pages-dev-util.test.ts +128 -0
  7. package/templates/__tests__/tsconfig-sanity.ts +12 -0
  8. package/templates/__tests__/tsconfig.json +8 -0
  9. package/templates/checked-fetch.js +30 -0
  10. package/templates/facade.d.ts +17 -0
  11. package/templates/gitignore +1 -0
  12. package/templates/init-tests/test-jest-new-worker.js +23 -0
  13. package/templates/init-tests/test-vitest-new-worker.js +24 -0
  14. package/templates/init-tests/test-vitest-new-worker.ts +25 -0
  15. package/templates/middleware/common.ts +67 -0
  16. package/templates/middleware/loader-modules.ts +137 -0
  17. package/templates/middleware/loader-sw.ts +228 -0
  18. package/templates/middleware/middleware-d1-beta.d.ts +3 -0
  19. package/templates/middleware/middleware-miniflare3-json-error.ts +33 -0
  20. package/templates/middleware/middleware-multiworker-dev.d.ts +4 -0
  21. package/templates/middleware/middleware-multiworker-dev.ts +59 -0
  22. package/templates/middleware/middleware-pretty-error.ts +40 -0
  23. package/templates/middleware/middleware-scheduled.ts +15 -0
  24. package/templates/middleware/middleware-serve-static-assets.d.ts +6 -0
  25. package/templates/middleware/middleware-serve-static-assets.ts +56 -0
  26. package/templates/new-worker-scheduled.js +17 -0
  27. package/templates/new-worker-scheduled.ts +32 -0
  28. package/templates/new-worker.js +3 -3
  29. package/templates/new-worker.ts +22 -4
  30. package/templates/no-op-worker.js +10 -0
  31. package/templates/pages-dev-pipeline.ts +31 -0
  32. package/templates/pages-dev-util.ts +55 -0
  33. package/templates/pages-shim.ts +9 -0
  34. package/templates/pages-template-plugin.ts +190 -0
  35. package/templates/pages-template-worker.ts +198 -0
  36. package/templates/tsconfig-sanity.ts +11 -0
  37. package/templates/tsconfig.init.json +105 -0
  38. package/templates/tsconfig.json +6 -103
  39. package/wrangler-dist/cli.d.ts +1732 -0
  40. package/wrangler-dist/cli.js +138795 -97365
  41. package/wrangler-dist/wasm-sync.wasm +0 -0
  42. package/import_meta_url.js +0 -3
  43. package/miniflare-config-stubs/.env.empty +0 -0
  44. package/miniflare-config-stubs/package.empty.json +0 -1
  45. package/miniflare-config-stubs/wrangler.empty.toml +0 -0
  46. package/miniflare-dist/index.mjs +0 -4886
  47. package/pages/functions/buildPlugin.ts +0 -92
  48. package/pages/functions/buildWorker.ts +0 -138
  49. package/pages/functions/filepath-routing.test.ts +0 -234
  50. package/pages/functions/filepath-routing.ts +0 -189
  51. package/pages/functions/identifiers.ts +0 -78
  52. package/pages/functions/routes.ts +0 -156
  53. package/pages/functions/template-plugin.ts +0 -146
  54. package/pages/functions/template-worker.ts +0 -143
  55. package/src/__tests__/config-cache.test.ts +0 -36
  56. package/src/__tests__/configuration.test.ts +0 -3125
  57. package/src/__tests__/dev.test.tsx +0 -827
  58. package/src/__tests__/guess-worker-format.test.ts +0 -85
  59. package/src/__tests__/helpers/clipboardy-mock.js +0 -4
  60. package/src/__tests__/helpers/cmd-shim.d.ts +0 -11
  61. package/src/__tests__/helpers/faye-websocket.d.ts +0 -6
  62. package/src/__tests__/helpers/mock-account-id.ts +0 -48
  63. package/src/__tests__/helpers/mock-bin.ts +0 -36
  64. package/src/__tests__/helpers/mock-cfetch.ts +0 -211
  65. package/src/__tests__/helpers/mock-console.ts +0 -87
  66. package/src/__tests__/helpers/mock-dialogs.ts +0 -90
  67. package/src/__tests__/helpers/mock-http-server.ts +0 -46
  68. package/src/__tests__/helpers/mock-istty.ts +0 -27
  69. package/src/__tests__/helpers/mock-kv.ts +0 -33
  70. package/src/__tests__/helpers/mock-oauth-flow.ts +0 -255
  71. package/src/__tests__/helpers/mock-stdin.ts +0 -103
  72. package/src/__tests__/helpers/mock-web-socket.ts +0 -29
  73. package/src/__tests__/helpers/run-in-tmp.ts +0 -39
  74. package/src/__tests__/helpers/run-wrangler.ts +0 -16
  75. package/src/__tests__/helpers/write-worker-source.ts +0 -31
  76. package/src/__tests__/helpers/write-wrangler-toml.ts +0 -17
  77. package/src/__tests__/https-options.test.ts +0 -130
  78. package/src/__tests__/index.test.ts +0 -242
  79. package/src/__tests__/init.test.ts +0 -1516
  80. package/src/__tests__/jest.setup.ts +0 -77
  81. package/src/__tests__/kv.test.ts +0 -1442
  82. package/src/__tests__/logger.test.ts +0 -115
  83. package/src/__tests__/package-manager.test.ts +0 -206
  84. package/src/__tests__/pages.test.ts +0 -327
  85. package/src/__tests__/parse.test.ts +0 -326
  86. package/src/__tests__/publish.test.ts +0 -4948
  87. package/src/__tests__/r2.test.ts +0 -210
  88. package/src/__tests__/route.test.ts +0 -45
  89. package/src/__tests__/secret.test.ts +0 -446
  90. package/src/__tests__/tail.test.ts +0 -690
  91. package/src/__tests__/user.test.ts +0 -131
  92. package/src/__tests__/whoami.test.tsx +0 -105
  93. package/src/bundle.ts +0 -169
  94. package/src/cfetch/index.ts +0 -104
  95. package/src/cfetch/internal.ts +0 -137
  96. package/src/cli.ts +0 -13
  97. package/src/config/README.md +0 -107
  98. package/src/config/config.ts +0 -234
  99. package/src/config/diagnostics.ts +0 -80
  100. package/src/config/environment.ts +0 -394
  101. package/src/config/index.ts +0 -63
  102. package/src/config/validation-helpers.ts +0 -575
  103. package/src/config/validation.ts +0 -1424
  104. package/src/config-cache.ts +0 -45
  105. package/src/create-worker-preview.ts +0 -175
  106. package/src/create-worker-upload-form.ts +0 -240
  107. package/src/dev/dev-vars.ts +0 -36
  108. package/src/dev/dev.tsx +0 -418
  109. package/src/dev/local.tsx +0 -312
  110. package/src/dev/remote.tsx +0 -213
  111. package/src/dev/use-esbuild.ts +0 -118
  112. package/src/dialogs.tsx +0 -87
  113. package/src/entry.ts +0 -267
  114. package/src/environment-variables.ts +0 -35
  115. package/src/errors.ts +0 -11
  116. package/src/https-options.ts +0 -123
  117. package/src/index.tsx +0 -2698
  118. package/src/inspect.ts +0 -623
  119. package/src/intl-polyfill.d.ts +0 -139
  120. package/src/kv.ts +0 -319
  121. package/src/logger.ts +0 -66
  122. package/src/miniflare-cli/README.md +0 -30
  123. package/src/miniflare-cli/enum-keys.ts +0 -17
  124. package/src/miniflare-cli/index.ts +0 -41
  125. package/src/miniflare-cli/request-context.ts +0 -40
  126. package/src/module-collection.ts +0 -258
  127. package/src/open-in-browser.ts +0 -26
  128. package/src/package-manager.ts +0 -127
  129. package/src/pages.tsx +0 -1875
  130. package/src/parse.ts +0 -185
  131. package/src/paths.ts +0 -26
  132. package/src/proxy.ts +0 -449
  133. package/src/publish.ts +0 -469
  134. package/src/r2.ts +0 -50
  135. package/src/selfsigned.d.ts +0 -29
  136. package/src/sites.tsx +0 -293
  137. package/src/tail/filters.ts +0 -279
  138. package/src/tail/index.ts +0 -299
  139. package/src/tail/printing.ts +0 -67
  140. package/src/update-check.ts +0 -19
  141. package/src/user.tsx +0 -1203
  142. package/src/whoami.tsx +0 -70
  143. package/src/worker.ts +0 -172
  144. package/templates/static-asset-facade.js +0 -47
  145. package/vendor/@cloudflare/kv-asset-handler/CHANGELOG.md +0 -332
  146. package/vendor/@cloudflare/kv-asset-handler/LICENSE_APACHE +0 -176
  147. package/vendor/@cloudflare/kv-asset-handler/LICENSE_MIT +0 -25
  148. package/vendor/@cloudflare/kv-asset-handler/README.md +0 -245
  149. package/vendor/@cloudflare/kv-asset-handler/dist/index.d.ts +0 -32
  150. package/vendor/@cloudflare/kv-asset-handler/dist/index.js +0 -354
  151. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.d.ts +0 -13
  152. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.js +0 -148
  153. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.d.ts +0 -1
  154. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.js +0 -436
  155. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.d.ts +0 -1
  156. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.js +0 -40
  157. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.d.ts +0 -1
  158. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.js +0 -42
  159. package/vendor/@cloudflare/kv-asset-handler/dist/types.d.ts +0 -26
  160. package/vendor/@cloudflare/kv-asset-handler/dist/types.js +0 -31
  161. package/vendor/@cloudflare/kv-asset-handler/package.json +0 -52
  162. package/vendor/@cloudflare/kv-asset-handler/src/index.ts +0 -296
  163. package/vendor/@cloudflare/kv-asset-handler/src/mocks.ts +0 -136
  164. package/vendor/@cloudflare/kv-asset-handler/src/test/getAssetFromKV.ts +0 -464
  165. package/vendor/@cloudflare/kv-asset-handler/src/test/mapRequestToAsset.ts +0 -33
  166. package/vendor/@cloudflare/kv-asset-handler/src/test/serveSinglePageApp.ts +0 -42
  167. package/vendor/@cloudflare/kv-asset-handler/src/types.ts +0 -39
  168. package/vendor/wrangler-mime/CHANGELOG.md +0 -289
  169. package/vendor/wrangler-mime/LICENSE +0 -21
  170. package/vendor/wrangler-mime/Mime.js +0 -97
  171. package/vendor/wrangler-mime/README.md +0 -187
  172. package/vendor/wrangler-mime/cli.js +0 -46
  173. package/vendor/wrangler-mime/index.js +0 -4
  174. package/vendor/wrangler-mime/lite.js +0 -4
  175. package/vendor/wrangler-mime/package.json +0 -52
  176. package/vendor/wrangler-mime/types/other.js +0 -1
  177. package/vendor/wrangler-mime/types/standard.js +0 -1
@@ -1,827 +0,0 @@
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(
437
- runWrangler("dev")
438
- ).rejects.toThrowErrorMatchingInlineSnapshot(
439
- `"Could not resolve \\"index.js\\" after running custom build: node -e \\"console.log('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] Could not resolve \\"index.js\\" after running custom build: node -e \\"console.log('custom build');\\"
448
-
449
- "
450
- `);
451
- expect(std.warn).toMatchInlineSnapshot(`""`);
452
- });
453
- });
454
-
455
- describe("upstream-protocol", () => {
456
- it("should default upstream-protocol to `https`", async () => {
457
- writeWranglerToml({
458
- main: "index.js",
459
- });
460
- fs.writeFileSync("index.js", `export default {};`);
461
- await runWrangler("dev");
462
- expect((Dev as jest.Mock).mock.calls[0][0].upstreamProtocol).toEqual(
463
- "https"
464
- );
465
- expect(std.out).toMatchInlineSnapshot(`""`);
466
- expect(std.warn).toMatchInlineSnapshot(`""`);
467
- expect(std.err).toMatchInlineSnapshot(`""`);
468
- });
469
-
470
- it("should warn if `--upstream-protocol=http` is used", async () => {
471
- writeWranglerToml({
472
- main: "index.js",
473
- });
474
- fs.writeFileSync("index.js", `export default {};`);
475
- await runWrangler("dev --upstream-protocol=http");
476
- expect((Dev as jest.Mock).mock.calls[0][0].upstreamProtocol).toEqual(
477
- "http"
478
- );
479
- expect(std.out).toMatchInlineSnapshot(`""`);
480
- expect(std.warn).toMatchInlineSnapshot(`
481
- "â–² [WARNING] Setting upstream-protocol to http is not currently implemented.
482
-
483
- If this is required in your project, please add your use case to the following issue:
484
- https://github.com/cloudflare/wrangler2/issues/583.
485
-
486
- "
487
- `);
488
- expect(std.err).toMatchInlineSnapshot(`""`);
489
- });
490
- });
491
-
492
- describe("local-protocol", () => {
493
- it("should default local-protocol to `http`", async () => {
494
- writeWranglerToml({
495
- main: "index.js",
496
- });
497
- fs.writeFileSync("index.js", `export default {};`);
498
- await runWrangler("dev");
499
- expect((Dev as jest.Mock).mock.calls[0][0].localProtocol).toEqual("http");
500
- expect(std.out).toMatchInlineSnapshot(`""`);
501
- expect(std.warn).toMatchInlineSnapshot(`""`);
502
- expect(std.err).toMatchInlineSnapshot(`""`);
503
- });
504
-
505
- it("should use `local_protocol` from `wrangler.toml`, if available", async () => {
506
- writeWranglerToml({
507
- main: "index.js",
508
- dev: {
509
- local_protocol: "https",
510
- },
511
- });
512
- fs.writeFileSync("index.js", `export default {};`);
513
- await runWrangler("dev");
514
- expect((Dev as jest.Mock).mock.calls[0][0].localProtocol).toEqual(
515
- "https"
516
- );
517
- expect(std.out).toMatchInlineSnapshot(`""`);
518
- expect(std.warn).toMatchInlineSnapshot(`""`);
519
- expect(std.err).toMatchInlineSnapshot(`""`);
520
- });
521
-
522
- it("should use --local-protocol command line arg, if provided", async () => {
523
- // Here we show that the command line overrides the wrangler.toml by
524
- // setting the config to https, and then setting it back to http on the command line.
525
- writeWranglerToml({
526
- main: "index.js",
527
- dev: {
528
- local_protocol: "https",
529
- },
530
- });
531
- fs.writeFileSync("index.js", `export default {};`);
532
- await runWrangler("dev --local-protocol=http");
533
- expect((Dev as jest.Mock).mock.calls[0][0].localProtocol).toEqual("http");
534
- expect(std.out).toMatchInlineSnapshot(`""`);
535
- expect(std.warn).toMatchInlineSnapshot(`""`);
536
- expect(std.err).toMatchInlineSnapshot(`""`);
537
- });
538
- });
539
-
540
- describe("ip", () => {
541
- it("should default ip to localhost", async () => {
542
- writeWranglerToml({
543
- main: "index.js",
544
- });
545
- fs.writeFileSync("index.js", `export default {};`);
546
- await runWrangler("dev");
547
- expect((Dev as jest.Mock).mock.calls[0][0].ip).toEqual("localhost");
548
- expect(std.out).toMatchInlineSnapshot(`""`);
549
- expect(std.warn).toMatchInlineSnapshot(`""`);
550
- expect(std.err).toMatchInlineSnapshot(`""`);
551
- });
552
-
553
- it("should use to `ip` from `wrangler.toml`, if available", async () => {
554
- writeWranglerToml({
555
- main: "index.js",
556
- dev: {
557
- ip: "0.0.0.0",
558
- },
559
- });
560
- fs.writeFileSync("index.js", `export default {};`);
561
- await runWrangler("dev");
562
- expect((Dev as jest.Mock).mock.calls[0][0].ip).toEqual("0.0.0.0");
563
- expect(std.out).toMatchInlineSnapshot(`""`);
564
- expect(std.warn).toMatchInlineSnapshot(`""`);
565
- expect(std.err).toMatchInlineSnapshot(`""`);
566
- });
567
-
568
- it("should use --ip command line arg, if provided", async () => {
569
- writeWranglerToml({
570
- main: "index.js",
571
- dev: {
572
- ip: "1.1.1.1",
573
- },
574
- });
575
- fs.writeFileSync("index.js", `export default {};`);
576
- await runWrangler("dev --ip=0.0.0.0");
577
- expect((Dev as jest.Mock).mock.calls[0][0].ip).toEqual("0.0.0.0");
578
- expect(std.out).toMatchInlineSnapshot(`""`);
579
- expect(std.warn).toMatchInlineSnapshot(`""`);
580
- expect(std.err).toMatchInlineSnapshot(`""`);
581
- });
582
- });
583
-
584
- describe("port", () => {
585
- it("should default port to 8787 if it is not in use", async () => {
586
- writeWranglerToml({
587
- main: "index.js",
588
- });
589
- fs.writeFileSync("index.js", `export default {};`);
590
- await runWrangler("dev");
591
- expect((Dev as jest.Mock).mock.calls[0][0].port).toEqual(8787);
592
- expect(std.out).toMatchInlineSnapshot(`""`);
593
- expect(std.warn).toMatchInlineSnapshot(`""`);
594
- expect(std.err).toMatchInlineSnapshot(`""`);
595
- });
596
-
597
- it("should use to `port` from `wrangler.toml`, if available", async () => {
598
- writeWranglerToml({
599
- main: "index.js",
600
- dev: {
601
- port: 8888,
602
- },
603
- });
604
- fs.writeFileSync("index.js", `export default {};`);
605
- // Mock `getPort()` to resolve to a completely different port.
606
- (getPort as jest.Mock).mockResolvedValue(98765);
607
-
608
- await runWrangler("dev");
609
- expect((Dev as jest.Mock).mock.calls[0][0].port).toEqual(8888);
610
- expect(std.out).toMatchInlineSnapshot(`""`);
611
- expect(std.warn).toMatchInlineSnapshot(`""`);
612
- expect(std.err).toMatchInlineSnapshot(`""`);
613
- });
614
-
615
- it("should use --port command line arg, if provided", async () => {
616
- writeWranglerToml({
617
- main: "index.js",
618
- dev: {
619
- port: 8888,
620
- },
621
- });
622
- fs.writeFileSync("index.js", `export default {};`);
623
- // Mock `getPort()` to resolve to a completely different port.
624
- (getPort as jest.Mock).mockResolvedValue(98765);
625
-
626
- await runWrangler("dev --port=9999");
627
- expect((Dev as jest.Mock).mock.calls[0][0].port).toEqual(9999);
628
- expect(std.out).toMatchInlineSnapshot(`""`);
629
- expect(std.warn).toMatchInlineSnapshot(`""`);
630
- expect(std.err).toMatchInlineSnapshot(`""`);
631
- });
632
-
633
- it("should use a different port to the default if it is in use", async () => {
634
- writeWranglerToml({
635
- main: "index.js",
636
- });
637
- fs.writeFileSync("index.js", `export default {};`);
638
- // Mock `getPort()` to resolve to a completely different port.
639
- (getPort as jest.Mock).mockResolvedValue(98765);
640
-
641
- await runWrangler("dev");
642
- expect((Dev as jest.Mock).mock.calls[0][0].port).toEqual(98765);
643
- expect(std.out).toMatchInlineSnapshot(`""`);
644
- expect(std.warn).toMatchInlineSnapshot(`""`);
645
- expect(std.err).toMatchInlineSnapshot(`""`);
646
- });
647
- });
648
-
649
- describe("durable_objects", () => {
650
- it("should warn if there is one or more remote Durable Object", async () => {
651
- writeWranglerToml({
652
- main: "index.js",
653
- durable_objects: {
654
- bindings: [
655
- { name: "NAME_1", class_name: "CLASS_1" },
656
- {
657
- name: "NAME_2",
658
- class_name: "CLASS_2",
659
- script_name: "SCRIPT_A",
660
- },
661
- { name: "NAME_3", class_name: "CLASS_3" },
662
- {
663
- name: "NAME_4",
664
- class_name: "CLASS_4",
665
- script_name: "SCRIPT_B",
666
- },
667
- ],
668
- },
669
- });
670
- fs.writeFileSync("index.js", `export default {};`);
671
- await runWrangler("dev");
672
- expect((Dev as jest.Mock).mock.calls[0][0].ip).toEqual("localhost");
673
- expect(std.out).toMatchInlineSnapshot(`""`);
674
- expect(std.warn).toMatchInlineSnapshot(`
675
- "â–² [WARNING] WARNING: You have Durable Object bindings that are not defined locally in the worker being developed.
676
-
677
- Be aware that changes to the data stored in these Durable Objects will be permanent and affect the
678
- live instances.
679
- Remote Durable Objects that are affected:
680
- - {\\"name\\":\\"NAME_2\\",\\"class_name\\":\\"CLASS_2\\",\\"script_name\\":\\"SCRIPT_A\\"}
681
- - {\\"name\\":\\"NAME_4\\",\\"class_name\\":\\"CLASS_4\\",\\"script_name\\":\\"SCRIPT_B\\"}
682
-
683
- "
684
- `);
685
- expect(std.err).toMatchInlineSnapshot(`""`);
686
- });
687
- });
688
-
689
- describe(".dev.vars", () => {
690
- it("should override `vars` bindings from `wrangler.toml` with values in `.dev.vars`", async () => {
691
- fs.writeFileSync("index.js", `export default {};`);
692
-
693
- const localVarsEnvContent = dedent`
694
- # Preceding comment
695
- VAR_1="var #1 value" # End of line comment
696
- VAR_3="var #3 value"
697
- VAR_MULTI_LINE_1="A: line 1
698
- line 2"
699
- VAR_MULTI_LINE_2="B: line 1\\nline 2"
700
- EMPTY=
701
- UNQUOTED= unquoted value
702
- `;
703
- fs.writeFileSync(".dev.vars", localVarsEnvContent, "utf8");
704
-
705
- writeWranglerToml({
706
- main: "index.js",
707
- vars: {
708
- VAR_1: "original value 1",
709
- VAR_2: "original value 2", // should not get overridden
710
- VAR_3: "original value 3",
711
- VAR_MULTI_LINE_1: "original multi-line 1",
712
- VAR_MULTI_LINE_2: "original multi-line 2",
713
- EMPTY: "original empty",
714
- UNQUOTED: "original unquoted",
715
- },
716
- });
717
- await runWrangler("dev");
718
- const varBindings: Record<string, unknown> = (Dev as jest.Mock).mock
719
- .calls[0][0].bindings.vars;
720
-
721
- expect(varBindings).toEqual({
722
- VAR_1: "var #1 value",
723
- VAR_2: "original value 2",
724
- VAR_3: "var #3 value",
725
- VAR_MULTI_LINE_1: "A: line 1\nline 2",
726
- VAR_MULTI_LINE_2: "B: line 1\nline 2",
727
- EMPTY: "",
728
- UNQUOTED: "unquoted value", // Note that whitespace is trimmed
729
- });
730
- expect(std.out).toMatchInlineSnapshot(
731
- `"Using vars defined in .dev.vars"`
732
- );
733
- expect(std.warn).toMatchInlineSnapshot(`""`);
734
- expect(std.err).toMatchInlineSnapshot(`""`);
735
- });
736
- });
737
-
738
- describe("site", () => {
739
- it("should error if --site is used with no value", async () => {
740
- await expect(
741
- runWrangler("dev --site")
742
- ).rejects.toThrowErrorMatchingInlineSnapshot(
743
- `"Not enough arguments following: site"`
744
- );
745
-
746
- expect(std).toMatchInlineSnapshot(`
747
- Object {
748
- "debug": "",
749
- "err": "wrangler dev [script]
750
-
751
- 👂 Start a local server for developing your worker
752
-
753
- Positionals:
754
- script The path to an entry point for your worker [string]
755
-
756
- Flags:
757
- -c, --config Path to .toml configuration file [string]
758
- -h, --help Show help [boolean]
759
- -v, --version Show version number [boolean]
760
-
761
- Options:
762
- --name Name of the worker [string]
763
- --format Choose an entry type [deprecated] [choices: \\"modules\\", \\"service-worker\\"]
764
- -e, --env Perform on a specific environment [string]
765
- --compatibility-date Date to use for compatibility checks [string]
766
- --compatibility-flags, --compatibility-flag Flags to use for compatibility checks [array]
767
- --latest Use the latest version of the worker runtime [boolean] [default: true]
768
- --ip IP address to listen on, defaults to \`localhost\` [string]
769
- --port Port to listen on [number]
770
- --inspector-port Port for devtools to connect to [number]
771
- --routes, --route Routes to upload [array]
772
- --host Host to forward requests to, defaults to the zone of project [string]
773
- --local-protocol Protocol to listen to requests on, defaults to http. [choices: \\"http\\", \\"https\\"]
774
- --experimental-public Static assets to be served [string]
775
- --site Root folder of static assets for Workers Sites [string]
776
- --site-include Array of .gitignore-style patterns that match file or directory names from the sites directory. Only matched items will be uploaded. [array]
777
- --site-exclude Array of .gitignore-style patterns that match file or directory names from the sites directory. Matched items will not be uploaded. [array]
778
- --upstream-protocol Protocol to forward requests to host on, defaults to https. [choices: \\"http\\", \\"https\\"]
779
- --jsx-factory The function that is called for each JSX element [string]
780
- --jsx-fragment The function that is called for each JSX fragment [string]
781
- --tsconfig Path to a custom tsconfig.json file [string]
782
- -l, --local Run on my machine [boolean] [default: false]
783
- --minify Minify the script [boolean]
784
- --node-compat Enable node.js compatibility [boolean]
785
- --experimental-enable-local-persistence Enable persistence for this session (only for local mode) [boolean]
786
- --inspect Enable dev tools [deprecated] [boolean]
787
- X [ERROR] Not enough arguments following: site
788
-
789
- ",
790
- "out": "
791
- ",
792
- "warn": "",
793
- }
794
- `);
795
- });
796
- });
797
-
798
- describe("--inspect", () => {
799
- it("should warn if --inspect is used", async () => {
800
- fs.writeFileSync("index.js", `export default {};`);
801
- await runWrangler("dev index.js --inspect");
802
- expect(std).toMatchInlineSnapshot(`
803
- Object {
804
- "debug": "",
805
- "err": "",
806
- "out": "",
807
- "warn": "â–² [WARNING] Passing --inspect is unnecessary, now you can always connect to devtools.
808
-
809
- ",
810
- }
811
- `);
812
- });
813
- });
814
- });
815
-
816
- function mockGetZones(domain: string, zones: { id: string }[] = []) {
817
- const removeMock = setMockResponse(
818
- "/zones",
819
- "GET",
820
- (_urlPieces, _init, queryParams) => {
821
- expect([...queryParams.entries()]).toEqual([["name", domain]]);
822
- // 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.
823
- removeMock();
824
- return zones;
825
- }
826
- );
827
- }