wrangler 0.0.0-e492c10 → 0.0.0-e4abed3e

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 (182) hide show
  1. package/README.md +30 -12
  2. package/bin/wrangler.js +90 -39
  3. package/config-schema.json +3059 -0
  4. package/kv-asset-handler.js +1 -0
  5. package/package.json +132 -86
  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 +19 -0
  11. package/templates/gitignore +2 -3
  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 +134 -0
  17. package/templates/middleware/loader-sw.ts +229 -0
  18. package/templates/middleware/middleware-ensure-req-body-drained.ts +18 -0
  19. package/templates/middleware/middleware-miniflare3-json-error.ts +32 -0
  20. package/templates/middleware/middleware-pretty-error.ts +40 -0
  21. package/templates/middleware/middleware-scheduled.ts +15 -0
  22. package/templates/middleware/middleware-serve-static-assets.d.ts +6 -0
  23. package/templates/middleware/middleware-serve-static-assets.ts +56 -0
  24. package/templates/modules-watch-stub.js +4 -0
  25. package/templates/new-worker-scheduled.js +17 -0
  26. package/templates/new-worker-scheduled.ts +32 -0
  27. package/templates/new-worker.js +3 -3
  28. package/templates/new-worker.ts +22 -4
  29. package/templates/no-op-worker.js +10 -0
  30. package/templates/pages-dev-pipeline.ts +32 -0
  31. package/templates/pages-dev-util.ts +55 -0
  32. package/templates/pages-shim.ts +9 -0
  33. package/templates/pages-template-plugin.ts +190 -0
  34. package/templates/pages-template-worker.ts +198 -0
  35. package/templates/startDevWorker/InspectorProxyWorker.ts +664 -0
  36. package/templates/startDevWorker/ProxyWorker.ts +334 -0
  37. package/templates/tsconfig-sanity.ts +11 -0
  38. package/templates/tsconfig.init.json +22 -0
  39. package/templates/tsconfig.json +6 -103
  40. package/wrangler-dist/InspectorProxyWorker.js +464 -0
  41. package/wrangler-dist/InspectorProxyWorker.js.map +6 -0
  42. package/wrangler-dist/ProxyWorker.js +240 -0
  43. package/wrangler-dist/ProxyWorker.js.map +6 -0
  44. package/wrangler-dist/cli.d.ts +26390 -0
  45. package/wrangler-dist/cli.js +201375 -103384
  46. package/wrangler-dist/wasm-sync.wasm +0 -0
  47. package/import_meta_url.js +0 -3
  48. package/miniflare-config-stubs/.env.empty +0 -0
  49. package/miniflare-config-stubs/package.empty.json +0 -1
  50. package/miniflare-config-stubs/wrangler.empty.toml +0 -0
  51. package/miniflare-dist/index.mjs +0 -4886
  52. package/pages/functions/buildPlugin.ts +0 -92
  53. package/pages/functions/buildWorker.ts +0 -138
  54. package/pages/functions/filepath-routing.test.ts +0 -234
  55. package/pages/functions/filepath-routing.ts +0 -189
  56. package/pages/functions/identifiers.ts +0 -78
  57. package/pages/functions/routes.ts +0 -156
  58. package/pages/functions/template-plugin.ts +0 -146
  59. package/pages/functions/template-worker.ts +0 -143
  60. package/src/__tests__/config-cache.test.ts +0 -36
  61. package/src/__tests__/configuration.test.ts +0 -3125
  62. package/src/__tests__/dev.test.tsx +0 -827
  63. package/src/__tests__/guess-worker-format.test.ts +0 -85
  64. package/src/__tests__/helpers/clipboardy-mock.js +0 -4
  65. package/src/__tests__/helpers/cmd-shim.d.ts +0 -11
  66. package/src/__tests__/helpers/faye-websocket.d.ts +0 -6
  67. package/src/__tests__/helpers/mock-account-id.ts +0 -48
  68. package/src/__tests__/helpers/mock-bin.ts +0 -36
  69. package/src/__tests__/helpers/mock-cfetch.ts +0 -211
  70. package/src/__tests__/helpers/mock-console.ts +0 -87
  71. package/src/__tests__/helpers/mock-dialogs.ts +0 -90
  72. package/src/__tests__/helpers/mock-http-server.ts +0 -46
  73. package/src/__tests__/helpers/mock-istty.ts +0 -27
  74. package/src/__tests__/helpers/mock-kv.ts +0 -33
  75. package/src/__tests__/helpers/mock-oauth-flow.ts +0 -255
  76. package/src/__tests__/helpers/mock-stdin.ts +0 -103
  77. package/src/__tests__/helpers/mock-web-socket.ts +0 -29
  78. package/src/__tests__/helpers/run-in-tmp.ts +0 -39
  79. package/src/__tests__/helpers/run-wrangler.ts +0 -16
  80. package/src/__tests__/helpers/write-worker-source.ts +0 -31
  81. package/src/__tests__/helpers/write-wrangler-toml.ts +0 -17
  82. package/src/__tests__/https-options.test.ts +0 -130
  83. package/src/__tests__/index.test.ts +0 -242
  84. package/src/__tests__/init.test.ts +0 -1516
  85. package/src/__tests__/jest.setup.ts +0 -77
  86. package/src/__tests__/kv.test.ts +0 -1442
  87. package/src/__tests__/logger.test.ts +0 -115
  88. package/src/__tests__/package-manager.test.ts +0 -206
  89. package/src/__tests__/pages.test.ts +0 -327
  90. package/src/__tests__/parse.test.ts +0 -326
  91. package/src/__tests__/publish.test.ts +0 -4948
  92. package/src/__tests__/r2.test.ts +0 -210
  93. package/src/__tests__/route.test.ts +0 -45
  94. package/src/__tests__/secret.test.ts +0 -446
  95. package/src/__tests__/tail.test.ts +0 -690
  96. package/src/__tests__/user.test.ts +0 -131
  97. package/src/__tests__/whoami.test.tsx +0 -105
  98. package/src/bundle.ts +0 -169
  99. package/src/cfetch/index.ts +0 -104
  100. package/src/cfetch/internal.ts +0 -137
  101. package/src/cli.ts +0 -13
  102. package/src/config/README.md +0 -107
  103. package/src/config/config.ts +0 -234
  104. package/src/config/diagnostics.ts +0 -80
  105. package/src/config/environment.ts +0 -394
  106. package/src/config/index.ts +0 -63
  107. package/src/config/validation-helpers.ts +0 -575
  108. package/src/config/validation.ts +0 -1424
  109. package/src/config-cache.ts +0 -45
  110. package/src/create-worker-preview.ts +0 -175
  111. package/src/create-worker-upload-form.ts +0 -240
  112. package/src/dev/dev-vars.ts +0 -36
  113. package/src/dev/dev.tsx +0 -418
  114. package/src/dev/local.tsx +0 -312
  115. package/src/dev/remote.tsx +0 -213
  116. package/src/dev/use-esbuild.ts +0 -118
  117. package/src/dialogs.tsx +0 -87
  118. package/src/entry.ts +0 -267
  119. package/src/environment-variables.ts +0 -35
  120. package/src/errors.ts +0 -11
  121. package/src/https-options.ts +0 -123
  122. package/src/index.tsx +0 -2698
  123. package/src/inspect.ts +0 -623
  124. package/src/intl-polyfill.d.ts +0 -139
  125. package/src/kv.ts +0 -319
  126. package/src/logger.ts +0 -66
  127. package/src/miniflare-cli/README.md +0 -30
  128. package/src/miniflare-cli/enum-keys.ts +0 -17
  129. package/src/miniflare-cli/index.ts +0 -41
  130. package/src/miniflare-cli/request-context.ts +0 -40
  131. package/src/module-collection.ts +0 -258
  132. package/src/open-in-browser.ts +0 -26
  133. package/src/package-manager.ts +0 -127
  134. package/src/pages.tsx +0 -1875
  135. package/src/parse.ts +0 -185
  136. package/src/paths.ts +0 -26
  137. package/src/proxy.ts +0 -449
  138. package/src/publish.ts +0 -469
  139. package/src/r2.ts +0 -50
  140. package/src/selfsigned.d.ts +0 -29
  141. package/src/sites.tsx +0 -293
  142. package/src/tail/filters.ts +0 -279
  143. package/src/tail/index.ts +0 -299
  144. package/src/tail/printing.ts +0 -67
  145. package/src/update-check.ts +0 -19
  146. package/src/user.tsx +0 -1203
  147. package/src/whoami.tsx +0 -70
  148. package/src/worker.ts +0 -172
  149. package/templates/static-asset-facade.js +0 -47
  150. package/vendor/@cloudflare/kv-asset-handler/CHANGELOG.md +0 -332
  151. package/vendor/@cloudflare/kv-asset-handler/LICENSE_APACHE +0 -176
  152. package/vendor/@cloudflare/kv-asset-handler/LICENSE_MIT +0 -25
  153. package/vendor/@cloudflare/kv-asset-handler/README.md +0 -245
  154. package/vendor/@cloudflare/kv-asset-handler/dist/index.d.ts +0 -32
  155. package/vendor/@cloudflare/kv-asset-handler/dist/index.js +0 -354
  156. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.d.ts +0 -13
  157. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.js +0 -148
  158. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.d.ts +0 -1
  159. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.js +0 -436
  160. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.d.ts +0 -1
  161. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.js +0 -40
  162. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.d.ts +0 -1
  163. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.js +0 -42
  164. package/vendor/@cloudflare/kv-asset-handler/dist/types.d.ts +0 -26
  165. package/vendor/@cloudflare/kv-asset-handler/dist/types.js +0 -31
  166. package/vendor/@cloudflare/kv-asset-handler/package.json +0 -52
  167. package/vendor/@cloudflare/kv-asset-handler/src/index.ts +0 -296
  168. package/vendor/@cloudflare/kv-asset-handler/src/mocks.ts +0 -136
  169. package/vendor/@cloudflare/kv-asset-handler/src/test/getAssetFromKV.ts +0 -464
  170. package/vendor/@cloudflare/kv-asset-handler/src/test/mapRequestToAsset.ts +0 -33
  171. package/vendor/@cloudflare/kv-asset-handler/src/test/serveSinglePageApp.ts +0 -42
  172. package/vendor/@cloudflare/kv-asset-handler/src/types.ts +0 -39
  173. package/vendor/wrangler-mime/CHANGELOG.md +0 -289
  174. package/vendor/wrangler-mime/LICENSE +0 -21
  175. package/vendor/wrangler-mime/Mime.js +0 -97
  176. package/vendor/wrangler-mime/README.md +0 -187
  177. package/vendor/wrangler-mime/cli.js +0 -46
  178. package/vendor/wrangler-mime/index.js +0 -4
  179. package/vendor/wrangler-mime/lite.js +0 -4
  180. package/vendor/wrangler-mime/package.json +0 -52
  181. package/vendor/wrangler-mime/types/other.js +0 -1
  182. 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
- }