wrangler 0.0.0-ecc8b172 → 0.0.0-ecded1af1

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 (250) hide show
  1. package/README.md +17 -19
  2. package/bin/wrangler.js +24 -93
  3. package/config-schema.json +3115 -0
  4. package/package.json +197 -190
  5. package/templates/__tests__/pages-dev-util.test.ts +128 -0
  6. package/templates/__tests__/tsconfig-sanity.ts +12 -0
  7. package/templates/__tests__/tsconfig.json +8 -0
  8. package/templates/checked-fetch.js +19 -6
  9. package/templates/facade.d.ts +19 -0
  10. package/templates/gitignore +2 -3
  11. package/templates/init-tests/test-jest-new-worker.js +3 -5
  12. package/templates/init-tests/test-vitest-new-worker.js +4 -6
  13. package/templates/init-tests/test-vitest-new-worker.ts +25 -0
  14. package/templates/middleware/common.ts +9 -4
  15. package/templates/middleware/loader-modules.ts +94 -44
  16. package/templates/middleware/loader-sw.ts +48 -32
  17. package/templates/middleware/middleware-ensure-req-body-drained.ts +18 -0
  18. package/templates/middleware/middleware-miniflare3-json-error.ts +32 -0
  19. package/templates/middleware/middleware-mock-analytics-engine.d.ts +3 -0
  20. package/templates/middleware/middleware-mock-analytics-engine.ts +30 -0
  21. package/templates/middleware/middleware-scheduled.ts +15 -1
  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.ts +1 -1
  26. package/templates/new-worker.ts +4 -1
  27. package/templates/pages-dev-pipeline.ts +10 -13
  28. package/templates/pages-dev-util.ts +55 -0
  29. package/templates/pages-shim.ts +1 -1
  30. package/templates/pages-template-plugin.ts +37 -14
  31. package/templates/pages-template-worker.ts +33 -7
  32. package/templates/startDevWorker/InspectorProxyWorker.ts +664 -0
  33. package/templates/startDevWorker/ProxyWorker.ts +336 -0
  34. package/templates/tsconfig-sanity.ts +11 -0
  35. package/templates/tsconfig.init.json +22 -0
  36. package/templates/tsconfig.json +5 -102
  37. package/wrangler-dist/InspectorProxyWorker.js +464 -0
  38. package/wrangler-dist/InspectorProxyWorker.js.map +6 -0
  39. package/wrangler-dist/ProxyWorker.js +241 -0
  40. package/wrangler-dist/ProxyWorker.js.map +6 -0
  41. package/wrangler-dist/cli.d.ts +26095 -471
  42. package/wrangler-dist/cli.js +178062 -122008
  43. package/wrangler-dist/wasm-sync.wasm +0 -0
  44. package/Cloudflare_CA.pem +0 -18
  45. package/import_meta_url.js +0 -3
  46. package/miniflare-config-stubs/.env.empty +0 -0
  47. package/miniflare-config-stubs/package.empty.json +0 -1
  48. package/miniflare-config-stubs/wrangler.empty.toml +0 -0
  49. package/miniflare-dist/index.mjs +0 -6310
  50. package/src/__tests__/api-dev.test.ts +0 -209
  51. package/src/__tests__/api-devregistry.test.js +0 -56
  52. package/src/__tests__/api.test.ts +0 -80
  53. package/src/__tests__/config-cache-without-cache-dir.test.ts +0 -38
  54. package/src/__tests__/config-cache.test.ts +0 -42
  55. package/src/__tests__/configuration.test.ts +0 -3864
  56. package/src/__tests__/d1.test.ts +0 -70
  57. package/src/__tests__/delete.test.ts +0 -116
  58. package/src/__tests__/dev.test.tsx +0 -1357
  59. package/src/__tests__/dialogs.test.tsx +0 -40
  60. package/src/__tests__/generate.test.ts +0 -235
  61. package/src/__tests__/get-host-from-url.test.ts +0 -16
  62. package/src/__tests__/guess-worker-format.test.ts +0 -85
  63. package/src/__tests__/helpers/clipboardy-mock.js +0 -4
  64. package/src/__tests__/helpers/cmd-shim.d.ts +0 -11
  65. package/src/__tests__/helpers/mock-account-id.ts +0 -48
  66. package/src/__tests__/helpers/mock-bin.ts +0 -36
  67. package/src/__tests__/helpers/mock-cfetch.ts +0 -333
  68. package/src/__tests__/helpers/mock-console.ts +0 -106
  69. package/src/__tests__/helpers/mock-dialogs.ts +0 -153
  70. package/src/__tests__/helpers/mock-get-zone-from-host.ts +0 -8
  71. package/src/__tests__/helpers/mock-http-server.ts +0 -46
  72. package/src/__tests__/helpers/mock-istty.ts +0 -74
  73. package/src/__tests__/helpers/mock-known-routes.ts +0 -7
  74. package/src/__tests__/helpers/mock-kv.ts +0 -33
  75. package/src/__tests__/helpers/mock-oauth-flow.ts +0 -252
  76. package/src/__tests__/helpers/mock-process.ts +0 -39
  77. package/src/__tests__/helpers/mock-stdin.ts +0 -108
  78. package/src/__tests__/helpers/mock-web-socket.ts +0 -29
  79. package/src/__tests__/helpers/msw/handlers/namespaces.ts +0 -104
  80. package/src/__tests__/helpers/msw/handlers/oauth.ts +0 -36
  81. package/src/__tests__/helpers/msw/handlers/r2.ts +0 -80
  82. package/src/__tests__/helpers/msw/handlers/script.ts +0 -75
  83. package/src/__tests__/helpers/msw/handlers/user.ts +0 -63
  84. package/src/__tests__/helpers/msw/index.ts +0 -15
  85. package/src/__tests__/helpers/run-in-tmp.ts +0 -38
  86. package/src/__tests__/helpers/run-wrangler.ts +0 -16
  87. package/src/__tests__/helpers/worker-scripts/child-wrangler.toml +0 -1
  88. package/src/__tests__/helpers/worker-scripts/hello-world-worker.js +0 -5
  89. package/src/__tests__/helpers/worker-scripts/hello-world-wrangler.toml +0 -1
  90. package/src/__tests__/helpers/worker-scripts/parent-worker.js +0 -8
  91. package/src/__tests__/helpers/worker-scripts/parent-wrangler.toml +0 -5
  92. package/src/__tests__/helpers/write-worker-source.ts +0 -31
  93. package/src/__tests__/helpers/write-wrangler-toml.ts +0 -17
  94. package/src/__tests__/https-options.test.ts +0 -163
  95. package/src/__tests__/index.test.ts +0 -251
  96. package/src/__tests__/init.test.ts +0 -2816
  97. package/src/__tests__/jest.setup.ts +0 -191
  98. package/src/__tests__/kv.test.ts +0 -1675
  99. package/src/__tests__/logger.test.ts +0 -141
  100. package/src/__tests__/metrics.test.ts +0 -450
  101. package/src/__tests__/middleware.scheduled.test.ts +0 -135
  102. package/src/__tests__/middleware.test.ts +0 -726
  103. package/src/__tests__/package-manager.test.ts +0 -353
  104. package/src/__tests__/pages.test.ts +0 -2111
  105. package/src/__tests__/parse.test.ts +0 -330
  106. package/src/__tests__/paths.test.ts +0 -39
  107. package/src/__tests__/publish.test.ts +0 -7119
  108. package/src/__tests__/pubsub.test.ts +0 -367
  109. package/src/__tests__/r2.test.ts +0 -371
  110. package/src/__tests__/route.test.ts +0 -45
  111. package/src/__tests__/secret.test.ts +0 -644
  112. package/src/__tests__/tail.test.ts +0 -800
  113. package/src/__tests__/test-old-node-version.js +0 -31
  114. package/src/__tests__/user.test.ts +0 -155
  115. package/src/__tests__/utils-collectKeyValues.test.ts +0 -47
  116. package/src/__tests__/validate-dev-props.test.ts +0 -56
  117. package/src/__tests__/version.test.ts +0 -35
  118. package/src/__tests__/whoami.test.tsx +0 -278
  119. package/src/__tests__/worker-namespace.test.ts +0 -321
  120. package/src/abort.d.ts +0 -3
  121. package/src/api/dev.ts +0 -208
  122. package/src/api/index.ts +0 -2
  123. package/src/bundle-reporter.tsx +0 -29
  124. package/src/bundle.ts +0 -756
  125. package/src/cfetch/index.ts +0 -160
  126. package/src/cfetch/internal.ts +0 -263
  127. package/src/cli.ts +0 -29
  128. package/src/config/README.md +0 -107
  129. package/src/config/config.ts +0 -275
  130. package/src/config/diagnostics.ts +0 -80
  131. package/src/config/environment.ts +0 -505
  132. package/src/config/index.ts +0 -293
  133. package/src/config/validation-helpers.ts +0 -597
  134. package/src/config/validation.ts +0 -1930
  135. package/src/config-cache.ts +0 -85
  136. package/src/create-worker-preview.ts +0 -275
  137. package/src/create-worker-upload-form.ts +0 -301
  138. package/src/d1/backups.tsx +0 -212
  139. package/src/d1/create.tsx +0 -54
  140. package/src/d1/delete.tsx +0 -56
  141. package/src/d1/execute.tsx +0 -294
  142. package/src/d1/formatTimeAgo.ts +0 -14
  143. package/src/d1/index.ts +0 -75
  144. package/src/d1/list.tsx +0 -48
  145. package/src/d1/options.ts +0 -12
  146. package/src/d1/types.tsx +0 -14
  147. package/src/d1/utils.ts +0 -39
  148. package/src/delete.ts +0 -86
  149. package/src/deprecated/index.ts +0 -453
  150. package/src/dev/dev-vars.ts +0 -36
  151. package/src/dev/dev.tsx +0 -575
  152. package/src/dev/get-local-persistence-path.tsx +0 -31
  153. package/src/dev/local.tsx +0 -686
  154. package/src/dev/remote.tsx +0 -601
  155. package/src/dev/start-server.ts +0 -506
  156. package/src/dev/use-esbuild.ts +0 -197
  157. package/src/dev/validate-dev-props.ts +0 -31
  158. package/src/dev-registry.tsx +0 -201
  159. package/src/dev.tsx +0 -865
  160. package/src/dialogs.tsx +0 -163
  161. package/src/dispatch-namespace.ts +0 -206
  162. package/src/durable.ts +0 -102
  163. package/src/entry.ts +0 -329
  164. package/src/environment-variables.ts +0 -50
  165. package/src/errors.ts +0 -11
  166. package/src/git-client.ts +0 -124
  167. package/src/global-wrangler-config-path.ts +0 -26
  168. package/src/https-options.ts +0 -127
  169. package/src/index.tsx +0 -582
  170. package/src/init.ts +0 -970
  171. package/src/inspect.ts +0 -747
  172. package/src/intl-polyfill.d.ts +0 -139
  173. package/src/is-ci.ts +0 -14
  174. package/src/is-interactive.ts +0 -12
  175. package/src/kv/helpers.ts +0 -430
  176. package/src/kv/index.ts +0 -645
  177. package/src/logger.ts +0 -80
  178. package/src/metrics/index.ts +0 -5
  179. package/src/metrics/metrics-config.ts +0 -239
  180. package/src/metrics/metrics-dispatcher.ts +0 -96
  181. package/src/metrics/metrics-usage-headers.ts +0 -24
  182. package/src/metrics/send-event.ts +0 -94
  183. package/src/miniflare-cli/README.md +0 -30
  184. package/src/miniflare-cli/assets.ts +0 -215
  185. package/src/miniflare-cli/index.ts +0 -208
  186. package/src/miniflare-cli/request-context.ts +0 -40
  187. package/src/module-collection.ts +0 -267
  188. package/src/open-in-browser.ts +0 -17
  189. package/src/package-manager.ts +0 -219
  190. package/src/pages/build.tsx +0 -238
  191. package/src/pages/constants.ts +0 -16
  192. package/src/pages/deployments.tsx +0 -106
  193. package/src/pages/dev.tsx +0 -689
  194. package/src/pages/errors.ts +0 -66
  195. package/src/pages/functions/buildPlugin.ts +0 -110
  196. package/src/pages/functions/buildWorker.ts +0 -156
  197. package/src/pages/functions/filepath-routing.test.ts +0 -234
  198. package/src/pages/functions/filepath-routing.ts +0 -189
  199. package/src/pages/functions/identifiers.ts +0 -78
  200. package/src/pages/functions/routes-consolidation.test.ts +0 -250
  201. package/src/pages/functions/routes-consolidation.ts +0 -73
  202. package/src/pages/functions/routes-transformation.test.ts +0 -282
  203. package/src/pages/functions/routes-transformation.ts +0 -115
  204. package/src/pages/functions/routes-validation.test.ts +0 -403
  205. package/src/pages/functions/routes-validation.ts +0 -202
  206. package/src/pages/functions/routes.ts +0 -151
  207. package/src/pages/functions.tsx +0 -82
  208. package/src/pages/hash.tsx +0 -13
  209. package/src/pages/index.tsx +0 -94
  210. package/src/pages/projects.tsx +0 -169
  211. package/src/pages/publish.tsx +0 -436
  212. package/src/pages/types.ts +0 -49
  213. package/src/pages/upload.tsx +0 -398
  214. package/src/pages/utils.ts +0 -12
  215. package/src/parse.ts +0 -286
  216. package/src/paths.ts +0 -71
  217. package/src/proxy.ts +0 -657
  218. package/src/publish/index.ts +0 -267
  219. package/src/publish/publish.ts +0 -913
  220. package/src/pubsub/index.ts +0 -286
  221. package/src/pubsub/pubsub-commands.tsx +0 -623
  222. package/src/r2/helpers.ts +0 -131
  223. package/src/r2/index.ts +0 -277
  224. package/src/routes.ts +0 -140
  225. package/src/secret/index.ts +0 -403
  226. package/src/selfsigned.d.ts +0 -29
  227. package/src/sites.tsx +0 -372
  228. package/src/tail/createTail.ts +0 -315
  229. package/src/tail/filters.ts +0 -277
  230. package/src/tail/index.ts +0 -217
  231. package/src/tail/printing.ts +0 -107
  232. package/src/update-check.ts +0 -19
  233. package/src/user/choose-account.tsx +0 -69
  234. package/src/user/env-vars.ts +0 -46
  235. package/src/user/generate-auth-url.ts +0 -33
  236. package/src/user/generate-random-state.ts +0 -16
  237. package/src/user/index.ts +0 -3
  238. package/src/user/user.tsx +0 -1192
  239. package/src/utils/collectKeyValues.ts +0 -14
  240. package/src/whoami.tsx +0 -169
  241. package/src/worker.ts +0 -234
  242. package/src/yargs-types.ts +0 -7
  243. package/src/zones.ts +0 -165
  244. package/templates/d1-beta-facade.js +0 -174
  245. package/templates/experimental-local-cache-stubs.js +0 -27
  246. package/templates/first-party-worker-module-facade.ts +0 -18
  247. package/templates/format-dev-errors.ts +0 -32
  248. package/templates/serve-static-assets.ts +0 -63
  249. package/templates/service-bindings-module-facade.js +0 -54
  250. package/templates/service-bindings-sw-facade.js +0 -42
@@ -1,1357 +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 { 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
- runInTempDir();
14
- const std = mockConsoleMethods();
15
- afterEach(() => {
16
- (Dev as jest.Mock).mockClear();
17
- patchConsole(() => {});
18
- unsetAllMocks();
19
- });
20
-
21
- describe("compatibility-date", () => {
22
- it("should not warn if there is no wrangler.toml and no compatibility-date specified", async () => {
23
- fs.writeFileSync("index.js", `export default {};`);
24
- await runWrangler("dev index.js");
25
- expect(std.out).toMatchInlineSnapshot(`""`);
26
- expect(std.warn).toMatchInlineSnapshot(`""`);
27
- expect(std.err).toMatchInlineSnapshot(`""`);
28
- });
29
-
30
- it("should warn if there is a wrangler.toml but no compatibility-date", async () => {
31
- writeWranglerToml({
32
- main: "index.js",
33
- compatibility_date: undefined,
34
- });
35
- fs.writeFileSync("index.js", `export default {};`);
36
- await runWrangler("dev");
37
- const currentDate = new Date().toISOString().substring(0, 10);
38
- expect(std.out).toMatchInlineSnapshot(`""`);
39
- expect(std.warn.replaceAll(currentDate, "<current-date>"))
40
- .toMatchInlineSnapshot(`
41
- "▲ [WARNING] No compatibility_date was specified. Using today's date: <current-date>.
42
-
43
- Add one to your wrangler.toml file:
44
- \`\`\`
45
- compatibility_date = \\"<current-date>\\"
46
- \`\`\`
47
- or pass it in your terminal:
48
- \`\`\`
49
- --compatibility-date=<current-date>
50
- \`\`\`
51
- See https://developers.cloudflare.com/workers/platform/compatibility-dates/ for more information.
52
-
53
- "
54
- `);
55
- expect(std.err).toMatchInlineSnapshot(`""`);
56
- });
57
-
58
- it("should not warn if there is a wrangler.toml but compatibility-date is specified at the command line", async () => {
59
- writeWranglerToml({
60
- main: "index.js",
61
- compatibility_date: undefined,
62
- });
63
- fs.writeFileSync("index.js", `export default {};`);
64
- await runWrangler("dev --compatibility-date=2020-01-01");
65
- expect(std.out).toMatchInlineSnapshot(`""`);
66
- expect(std.warn).toMatchInlineSnapshot(`""`);
67
- expect(std.err).toMatchInlineSnapshot(`""`);
68
- });
69
- });
70
-
71
- describe("usage-model", () => {
72
- it("should read wrangler.toml's usage_model", async () => {
73
- writeWranglerToml({
74
- main: "index.js",
75
- usage_model: "unbound",
76
- });
77
- fs.writeFileSync("index.js", `export default {};`);
78
- await runWrangler("dev");
79
- expect((Dev as jest.Mock).mock.calls[0][0].usageModel).toEqual("unbound");
80
- });
81
-
82
- it("should read wrangler.toml's usage_model in local mode", async () => {
83
- writeWranglerToml({
84
- main: "index.js",
85
- usage_model: "unbound",
86
- });
87
- fs.writeFileSync("index.js", `export default {};`);
88
- await runWrangler("dev --local");
89
- expect((Dev as jest.Mock).mock.calls[0][0].usageModel).toEqual("unbound");
90
- });
91
- });
92
-
93
- describe("entry-points", () => {
94
- it("should error if there is no entry-point specified", async () => {
95
- writeWranglerToml();
96
-
97
- await expect(
98
- runWrangler("dev")
99
- ).rejects.toThrowErrorMatchingInlineSnapshot(
100
- `"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."`
101
- );
102
-
103
- expect(std.out).toMatchInlineSnapshot(`
104
- "
105
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choose"
106
- `);
107
- expect(std.err).toMatchInlineSnapshot(`
108
- "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.
109
-
110
- "
111
- `);
112
- });
113
-
114
- it("should use `main` from the top-level environment", async () => {
115
- writeWranglerToml({
116
- main: "index.js",
117
- });
118
- fs.writeFileSync("index.js", `export default {};`);
119
- await runWrangler("dev");
120
- expect((Dev as jest.Mock).mock.calls[0][0].entry.file).toMatch(
121
- /index\.js$/
122
- );
123
- expect(std.out).toMatchInlineSnapshot(`""`);
124
- expect(std.warn).toMatchInlineSnapshot(`""`);
125
- expect(std.err).toMatchInlineSnapshot(`""`);
126
- });
127
-
128
- it("should use `main` from a named environment", async () => {
129
- writeWranglerToml({
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
- it("should use `main` from a named environment, rather than the top-level", async () => {
147
- writeWranglerToml({
148
- main: "other.js",
149
- env: {
150
- ENV1: {
151
- main: "index.js",
152
- },
153
- },
154
- });
155
- fs.writeFileSync("index.js", `export default {};`);
156
- await runWrangler("dev --env=ENV1");
157
- expect((Dev as jest.Mock).mock.calls[0][0].entry.file).toMatch(
158
- /index\.js$/
159
- );
160
- expect(std.out).toMatchInlineSnapshot(`""`);
161
- expect(std.warn).toMatchInlineSnapshot(`""`);
162
- expect(std.err).toMatchInlineSnapshot(`""`);
163
- });
164
- });
165
-
166
- describe("routes", () => {
167
- it("should pass routes to <Dev/>", async () => {
168
- fs.writeFileSync("index.js", `export default {};`);
169
-
170
- // config.routes
171
- mockGetZones("5.some-host.com", [{ id: "some-zone-id-5" }]);
172
- writeWranglerToml({
173
- main: "index.js",
174
- routes: ["http://5.some-host.com/some/path/*"],
175
- });
176
- await runWrangler("dev");
177
- expect((Dev as jest.Mock).mock.calls[0][0]).toEqual(
178
- expect.objectContaining({
179
- host: "5.some-host.com",
180
- zone: "some-zone-id-5",
181
- routes: ["http://5.some-host.com/some/path/*"],
182
- })
183
- );
184
- });
185
- });
186
- describe("host", () => {
187
- it("should resolve a host to its zone", async () => {
188
- writeWranglerToml({
189
- main: "index.js",
190
- });
191
- fs.writeFileSync("index.js", `export default {};`);
192
- mockGetZones("some-host.com", [{ id: "some-zone-id" }]);
193
- await runWrangler("dev --host some-host.com");
194
- expect((Dev as jest.Mock).mock.calls[0][0]).toEqual(
195
- expect.objectContaining({
196
- host: "some-host.com",
197
- zone: "some-zone-id",
198
- })
199
- );
200
- });
201
-
202
- it("should read wrangler.toml's dev.host", async () => {
203
- writeWranglerToml({
204
- main: "index.js",
205
- dev: {
206
- host: "some-host.com",
207
- },
208
- });
209
- fs.writeFileSync("index.js", `export default {};`);
210
- mockGetZones("some-host.com", [{ id: "some-zone-id" }]);
211
- await runWrangler("dev");
212
- expect((Dev as jest.Mock).mock.calls[0][0].host).toEqual("some-host.com");
213
- });
214
-
215
- it("should read --route", async () => {
216
- writeWranglerToml({
217
- main: "index.js",
218
- });
219
- fs.writeFileSync("index.js", `export default {};`);
220
- mockGetZones("some-host.com", [{ id: "some-zone-id" }]);
221
- await runWrangler("dev --route http://some-host.com/some/path/*");
222
- expect((Dev as jest.Mock).mock.calls[0][0].host).toEqual("some-host.com");
223
- });
224
-
225
- it("should read wrangler.toml's routes", async () => {
226
- writeWranglerToml({
227
- main: "index.js",
228
- routes: [
229
- "http://some-host.com/some/path/*",
230
- "http://some-other-host.com/path/*",
231
- ],
232
- });
233
- fs.writeFileSync("index.js", `export default {};`);
234
- mockGetZones("some-host.com", [{ id: "some-zone-id" }]);
235
- await runWrangler("dev");
236
- expect((Dev as jest.Mock).mock.calls[0][0].host).toEqual("some-host.com");
237
- });
238
-
239
- it("should read wrangler.toml's environment specific routes", async () => {
240
- writeWranglerToml({
241
- main: "index.js",
242
- routes: [
243
- "http://a-host.com/some/path/*",
244
- "http://another-host.com/path/*",
245
- ],
246
- env: {
247
- staging: {
248
- routes: [
249
- "http://some-host.com/some/path/*",
250
- "http://some-other-host.com/path/*",
251
- ],
252
- },
253
- },
254
- });
255
- fs.writeFileSync("index.js", `export default {};`);
256
- mockGetZones("some-host.com", [{ id: "some-zone-id" }]);
257
- await runWrangler("dev --env staging");
258
- expect((Dev as jest.Mock).mock.calls[0][0].host).toEqual("some-host.com");
259
- });
260
-
261
- it("should strip leading `*` from given host when deducing a zone id", async () => {
262
- writeWranglerToml({
263
- main: "index.js",
264
- route: "*some-host.com/some/path/*",
265
- });
266
- fs.writeFileSync("index.js", `export default {};`);
267
- mockGetZones("some-host.com", [{ id: "some-zone-id" }]);
268
- await runWrangler("dev");
269
- expect((Dev as jest.Mock).mock.calls[0][0].host).toEqual("some-host.com");
270
- });
271
-
272
- it("should strip leading `*.` from given host when deducing a zone id", async () => {
273
- writeWranglerToml({
274
- main: "index.js",
275
- route: "*.some-host.com/some/path/*",
276
- });
277
- fs.writeFileSync("index.js", `export default {};`);
278
- mockGetZones("some-host.com", [{ id: "some-zone-id" }]);
279
- await runWrangler("dev");
280
- expect((Dev as jest.Mock).mock.calls[0][0].host).toEqual("some-host.com");
281
- });
282
-
283
- it("should, when provided, use a configured zone_id", async () => {
284
- writeWranglerToml({
285
- main: "index.js",
286
- routes: [
287
- { pattern: "https://some-domain.com/*", zone_id: "some-zone-id" },
288
- ],
289
- });
290
- fs.writeFileSync("index.js", `export default {};`);
291
- await runWrangler("dev");
292
- expect((Dev as jest.Mock).mock.calls[0][0]).toEqual(
293
- expect.objectContaining({
294
- host: "some-domain.com",
295
- zone: "some-zone-id",
296
- })
297
- );
298
- });
299
-
300
- it("should, when provided, use a zone_name to get a zone_id", async () => {
301
- writeWranglerToml({
302
- main: "index.js",
303
- routes: [
304
- {
305
- pattern: "https://some-zone.com/*",
306
- zone_name: "some-zone.com",
307
- },
308
- ],
309
- });
310
- fs.writeFileSync("index.js", `export default {};`);
311
- mockGetZones("some-zone.com", [{ id: "a-zone-id" }]);
312
- await runWrangler("dev");
313
- expect((Dev as jest.Mock).mock.calls[0][0]).toEqual(
314
- expect.objectContaining({
315
- // note that it uses the provided zone_name as a host too
316
- host: "some-zone.com",
317
- zone: "a-zone-id",
318
- })
319
- );
320
- });
321
-
322
- it("given a long host, it should use the longest subdomain that resolves to a zone", async () => {
323
- writeWranglerToml({
324
- main: "index.js",
325
- });
326
- fs.writeFileSync("index.js", `export default {};`);
327
- mockGetZones("111.222.333.some-host.com", []);
328
- mockGetZones("222.333.some-host.com", []);
329
- mockGetZones("333.some-host.com", [{ id: "some-zone-id" }]);
330
- await runWrangler("dev --host 111.222.333.some-host.com");
331
- expect((Dev as jest.Mock).mock.calls[0][0]).toEqual(
332
- expect.objectContaining({
333
- host: "111.222.333.some-host.com",
334
- zone: "some-zone-id",
335
- })
336
- );
337
- });
338
-
339
- it("should, in order, use args.host/config.dev.host/args.routes/(config.route|config.routes)", async () => {
340
- // This test might seem like it's testing implementation details, but let's be specific and consider it a spec
341
-
342
- fs.writeFileSync("index.js", `export default {};`);
343
-
344
- // config.routes
345
- mockGetZones("5.some-host.com", [{ id: "some-zone-id-5" }]);
346
- writeWranglerToml({
347
- main: "index.js",
348
- routes: ["http://5.some-host.com/some/path/*"],
349
- });
350
- await runWrangler("dev");
351
- expect((Dev as jest.Mock).mock.calls[0][0]).toEqual(
352
- expect.objectContaining({
353
- host: "5.some-host.com",
354
- zone: "some-zone-id-5",
355
- })
356
- );
357
- (Dev as jest.Mock).mockClear();
358
-
359
- // config.route
360
- mockGetZones("4.some-host.com", [{ id: "some-zone-id-4" }]);
361
- writeWranglerToml({
362
- main: "index.js",
363
- route: "https://4.some-host.com/some/path/*",
364
- });
365
- await runWrangler("dev");
366
- expect((Dev as jest.Mock).mock.calls[0][0]).toEqual(
367
- expect.objectContaining({
368
- host: "4.some-host.com",
369
- zone: "some-zone-id-4",
370
- })
371
- );
372
- (Dev as jest.Mock).mockClear();
373
-
374
- // --routes
375
- mockGetZones("3.some-host.com", [{ id: "some-zone-id-3" }]);
376
- writeWranglerToml({
377
- main: "index.js",
378
- route: "https://4.some-host.com/some/path/*",
379
- });
380
- await runWrangler("dev --routes http://3.some-host.com/some/path/*");
381
- expect((Dev as jest.Mock).mock.calls[0][0]).toEqual(
382
- expect.objectContaining({
383
- host: "3.some-host.com",
384
- zone: "some-zone-id-3",
385
- })
386
- );
387
- (Dev as jest.Mock).mockClear();
388
-
389
- // config.dev.host
390
- mockGetZones("2.some-host.com", [{ id: "some-zone-id-2" }]);
391
- writeWranglerToml({
392
- main: "index.js",
393
- dev: {
394
- host: `2.some-host.com`,
395
- },
396
- route: "4.some-host.com/some/path/*",
397
- });
398
- await runWrangler("dev --routes http://3.some-host.com/some/path/*");
399
- expect((Dev as jest.Mock).mock.calls[0][0]).toEqual(
400
- expect.objectContaining({
401
- host: "2.some-host.com",
402
- zone: "some-zone-id-2",
403
- })
404
- );
405
- (Dev as jest.Mock).mockClear();
406
-
407
- // --host
408
- mockGetZones("1.some-host.com", [{ id: "some-zone-id-1" }]);
409
- writeWranglerToml({
410
- main: "index.js",
411
- dev: {
412
- host: `2.some-host.com`,
413
- },
414
- route: "4.some-host.com/some/path/*",
415
- });
416
- await runWrangler(
417
- "dev --routes http://3.some-host.com/some/path/* --host 1.some-host.com"
418
- );
419
- expect((Dev as jest.Mock).mock.calls[0][0]).toEqual(
420
- expect.objectContaining({
421
- host: "1.some-host.com",
422
- zone: "some-zone-id-1",
423
- })
424
- );
425
- (Dev as jest.Mock).mockClear();
426
- });
427
-
428
- it("should error if a host can't resolve to a zone", async () => {
429
- writeWranglerToml({
430
- main: "index.js",
431
- });
432
- fs.writeFileSync("index.js", `export default {};`);
433
- mockGetZones("some-host.com", []);
434
- await expect(
435
- runWrangler("dev --host some-host.com")
436
- ).rejects.toThrowErrorMatchingInlineSnapshot(
437
- `"Could not find zone for some-host.com"`
438
- );
439
- });
440
-
441
- it("should not try to resolve a zone when starting in local mode", async () => {
442
- writeWranglerToml({
443
- main: "index.js",
444
- });
445
- fs.writeFileSync("index.js", `export default {};`);
446
- await runWrangler("dev --host some-host.com --local");
447
- expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual(undefined);
448
- });
449
- });
450
-
451
- describe("local upstream", () => {
452
- it("should use dev.host from toml by default", async () => {
453
- writeWranglerToml({
454
- main: "index.js",
455
- dev: {
456
- host: `2.some-host.com`,
457
- },
458
- });
459
- fs.writeFileSync("index.js", `export default {};`);
460
- await runWrangler("dev --local");
461
- expect((Dev as jest.Mock).mock.calls[0][0].localUpstream).toEqual(
462
- "2.some-host.com"
463
- );
464
- });
465
-
466
- it("should use route from toml by default", async () => {
467
- writeWranglerToml({
468
- main: "index.js",
469
- route: "https://4.some-host.com/some/path/*",
470
- });
471
- fs.writeFileSync("index.js", `export default {};`);
472
- await runWrangler("dev --local");
473
- expect((Dev as jest.Mock).mock.calls[0][0].host).toEqual(
474
- "4.some-host.com"
475
- );
476
- });
477
-
478
- it("should respect the option when provided", async () => {
479
- writeWranglerToml({
480
- main: "index.js",
481
- route: `2.some-host.com`,
482
- });
483
- fs.writeFileSync("index.js", `export default {};`);
484
- await runWrangler("dev --local-upstream some-host.com --local");
485
- expect((Dev as jest.Mock).mock.calls[0][0].localUpstream).toEqual(
486
- "some-host.com"
487
- );
488
- });
489
- });
490
-
491
- describe("custom builds", () => {
492
- it("should run a custom build before starting `dev`", async () => {
493
- writeWranglerToml({
494
- build: {
495
- command: `node -e "console.log('custom build'); require('fs').writeFileSync('index.js', 'export default { fetch(){ return new Response(123) } }')"`,
496
- },
497
- });
498
-
499
- await runWrangler("dev index.js");
500
-
501
- expect(fs.readFileSync("index.js", "utf-8")).toMatchInlineSnapshot(
502
- `"export default { fetch(){ return new Response(123) } }"`
503
- );
504
-
505
- // and the command would pass through
506
- expect((Dev as jest.Mock).mock.calls[0][0].build).toEqual({
507
- command:
508
- "node -e \"console.log('custom build'); require('fs').writeFileSync('index.js', 'export default { fetch(){ return new Response(123) } }')\"",
509
- cwd: undefined,
510
- watch_dir: "src",
511
- });
512
- expect(std.out).toMatchInlineSnapshot(
513
- `"Running custom build: node -e \\"console.log('custom build'); require('fs').writeFileSync('index.js', 'export default { fetch(){ return new Response(123) } }')\\""`
514
- );
515
- expect(std.err).toMatchInlineSnapshot(`""`);
516
- expect(std.warn).toMatchInlineSnapshot(`""`);
517
- });
518
-
519
- if (process.platform !== "win32") {
520
- it("should run a custom build of multiple steps combined by && before starting `dev`", async () => {
521
- writeWranglerToml({
522
- build: {
523
- command: `echo "custom build" && echo "export default { fetch(){ return new Response(123) } }" > index.js`,
524
- },
525
- });
526
-
527
- await runWrangler("dev index.js");
528
-
529
- expect(fs.readFileSync("index.js", "utf-8")).toMatchInlineSnapshot(`
530
- "export default { fetch(){ return new Response(123) } }
531
- "
532
- `);
533
-
534
- expect(std.out).toMatchInlineSnapshot(
535
- `"Running custom build: echo \\"custom build\\" && echo \\"export default { fetch(){ return new Response(123) } }\\" > index.js"`
536
- );
537
- expect(std.err).toMatchInlineSnapshot(`""`);
538
- expect(std.warn).toMatchInlineSnapshot(`""`);
539
- });
540
- }
541
-
542
- it("should throw an error if the entry doesn't exist after the build finishes", async () => {
543
- writeWranglerToml({
544
- main: "index.js",
545
- build: {
546
- command: `node -e "console.log('custom build');"`,
547
- },
548
- });
549
-
550
- await expect(runWrangler("dev")).rejects
551
- .toThrowErrorMatchingInlineSnapshot(`
552
- "The expected output file at \\"index.js\\" was not found after running custom build: node -e \\"console.log('custom build');\\".
553
- The \`main\` property in wrangler.toml should point to the file generated by the custom build."
554
- `);
555
- expect(std.out).toMatchInlineSnapshot(`
556
- "Running custom build: node -e \\"console.log('custom build');\\"
557
-
558
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choose"
559
- `);
560
- expect(std.err).toMatchInlineSnapshot(`
561
- "X [ERROR] The expected output file at \\"index.js\\" was not found after running custom build: node -e \\"console.log('custom build');\\".
562
-
563
- The \`main\` property in wrangler.toml should point to the file generated by the custom build.
564
-
565
- "
566
- `);
567
- expect(std.warn).toMatchInlineSnapshot(`""`);
568
- });
569
- });
570
-
571
- describe("upstream-protocol", () => {
572
- it("should default upstream-protocol to `https`", async () => {
573
- writeWranglerToml({
574
- main: "index.js",
575
- });
576
- fs.writeFileSync("index.js", `export default {};`);
577
- await runWrangler("dev");
578
- expect((Dev as jest.Mock).mock.calls[0][0].upstreamProtocol).toEqual(
579
- "https"
580
- );
581
- expect(std.out).toMatchInlineSnapshot(`""`);
582
- expect(std.warn).toMatchInlineSnapshot(`""`);
583
- expect(std.err).toMatchInlineSnapshot(`""`);
584
- });
585
-
586
- it("should warn if `--upstream-protocol=http` is used", async () => {
587
- writeWranglerToml({
588
- main: "index.js",
589
- });
590
- fs.writeFileSync("index.js", `export default {};`);
591
- await runWrangler("dev --upstream-protocol=http");
592
- expect((Dev as jest.Mock).mock.calls[0][0].upstreamProtocol).toEqual(
593
- "http"
594
- );
595
- expect(std.out).toMatchInlineSnapshot(`""`);
596
- expect(std.warn).toMatchInlineSnapshot(`
597
- "▲ [WARNING] Setting upstream-protocol to http is not currently implemented.
598
-
599
- If this is required in your project, please add your use case to the following issue:
600
- https://github.com/cloudflare/wrangler2/issues/583.
601
-
602
- "
603
- `);
604
- expect(std.err).toMatchInlineSnapshot(`""`);
605
- });
606
- });
607
-
608
- describe("local-protocol", () => {
609
- it("should default local-protocol to `http`", async () => {
610
- writeWranglerToml({
611
- main: "index.js",
612
- });
613
- fs.writeFileSync("index.js", `export default {};`);
614
- await runWrangler("dev");
615
- expect((Dev as jest.Mock).mock.calls[0][0].localProtocol).toEqual("http");
616
- expect(std.out).toMatchInlineSnapshot(`""`);
617
- expect(std.warn).toMatchInlineSnapshot(`""`);
618
- expect(std.err).toMatchInlineSnapshot(`""`);
619
- });
620
-
621
- it("should use `local_protocol` from `wrangler.toml`, if available", async () => {
622
- writeWranglerToml({
623
- main: "index.js",
624
- dev: {
625
- local_protocol: "https",
626
- },
627
- });
628
- fs.writeFileSync("index.js", `export default {};`);
629
- await runWrangler("dev");
630
- expect((Dev as jest.Mock).mock.calls[0][0].localProtocol).toEqual(
631
- "https"
632
- );
633
- expect(std.out).toMatchInlineSnapshot(`""`);
634
- expect(std.warn).toMatchInlineSnapshot(`""`);
635
- expect(std.err).toMatchInlineSnapshot(`""`);
636
- });
637
-
638
- it("should use --local-protocol command line arg, if provided", async () => {
639
- // Here we show that the command line overrides the wrangler.toml by
640
- // setting the config to https, and then setting it back to http on the command line.
641
- writeWranglerToml({
642
- main: "index.js",
643
- dev: {
644
- local_protocol: "https",
645
- },
646
- });
647
- fs.writeFileSync("index.js", `export default {};`);
648
- await runWrangler("dev --local-protocol=http");
649
- expect((Dev as jest.Mock).mock.calls[0][0].localProtocol).toEqual("http");
650
- expect(std.out).toMatchInlineSnapshot(`""`);
651
- expect(std.warn).toMatchInlineSnapshot(`""`);
652
- expect(std.err).toMatchInlineSnapshot(`""`);
653
- });
654
- });
655
-
656
- describe("ip", () => {
657
- it("should default ip to 0.0.0.0", async () => {
658
- writeWranglerToml({
659
- main: "index.js",
660
- });
661
- fs.writeFileSync("index.js", `export default {};`);
662
- await runWrangler("dev");
663
- expect((Dev as jest.Mock).mock.calls[0][0].ip).toEqual("0.0.0.0");
664
- expect(std.out).toMatchInlineSnapshot(`""`);
665
- expect(std.warn).toMatchInlineSnapshot(`""`);
666
- expect(std.err).toMatchInlineSnapshot(`""`);
667
- });
668
-
669
- it("should use to `ip` from `wrangler.toml`, if available", async () => {
670
- writeWranglerToml({
671
- main: "index.js",
672
- dev: {
673
- ip: "1.2.3.4",
674
- },
675
- });
676
- fs.writeFileSync("index.js", `export default {};`);
677
- await runWrangler("dev");
678
- expect((Dev as jest.Mock).mock.calls[0][0].ip).toEqual("1.2.3.4");
679
- expect(std.out).toMatchInlineSnapshot(`""`);
680
- expect(std.warn).toMatchInlineSnapshot(`""`);
681
- expect(std.err).toMatchInlineSnapshot(`""`);
682
- });
683
-
684
- it("should use --ip command line arg, if provided", async () => {
685
- writeWranglerToml({
686
- main: "index.js",
687
- dev: {
688
- ip: "1.2.3.4",
689
- },
690
- });
691
- fs.writeFileSync("index.js", `export default {};`);
692
- await runWrangler("dev --ip=5.6.7.8");
693
- expect((Dev as jest.Mock).mock.calls[0][0].ip).toEqual("5.6.7.8");
694
- expect(std.out).toMatchInlineSnapshot(`""`);
695
- expect(std.warn).toMatchInlineSnapshot(`""`);
696
- expect(std.err).toMatchInlineSnapshot(`""`);
697
- });
698
- });
699
-
700
- describe("inspector port", () => {
701
- it("should use 9229 as the default port", async () => {
702
- writeWranglerToml({
703
- main: "index.js",
704
- });
705
- fs.writeFileSync("index.js", `export default {};`);
706
- await runWrangler("dev");
707
- expect((Dev as jest.Mock).mock.calls[0][0].inspectorPort).toEqual(9229);
708
- expect(std).toMatchInlineSnapshot(`
709
- Object {
710
- "debug": "",
711
- "err": "",
712
- "out": "",
713
- "warn": "",
714
- }
715
- `);
716
- });
717
-
718
- it("should read --inspector-port", async () => {
719
- writeWranglerToml({
720
- main: "index.js",
721
- });
722
- fs.writeFileSync("index.js", `export default {};`);
723
- await runWrangler("dev --inspector-port=9999");
724
- expect((Dev as jest.Mock).mock.calls[0][0].inspectorPort).toEqual(9999);
725
- expect(std).toMatchInlineSnapshot(`
726
- Object {
727
- "debug": "",
728
- "err": "",
729
- "out": "",
730
- "warn": "",
731
- }
732
- `);
733
- });
734
-
735
- it("should read dev.inspector_port from wrangler.toml", async () => {
736
- writeWranglerToml({
737
- main: "index.js",
738
- dev: {
739
- inspector_port: 9999,
740
- },
741
- });
742
- fs.writeFileSync("index.js", `export default {};`);
743
- await runWrangler("dev");
744
- expect((Dev as jest.Mock).mock.calls[0][0].inspectorPort).toEqual(9999);
745
- expect(std).toMatchInlineSnapshot(`
746
- Object {
747
- "debug": "",
748
- "err": "",
749
- "out": "",
750
- "warn": "",
751
- }
752
- `);
753
- });
754
-
755
- it("should error if a bad dev.inspector_port config is provided", async () => {
756
- writeWranglerToml({
757
- main: "index.js",
758
- dev: {
759
- // @ts-expect-error intentionally bad port
760
- inspector_port: "some string",
761
- },
762
- });
763
- fs.writeFileSync("index.js", `export default {};`);
764
- await expect(runWrangler("dev")).rejects
765
- .toThrowErrorMatchingInlineSnapshot(`
766
- "Processing wrangler.toml configuration:
767
- - Expected \\"dev.inspector_port\\" to be of type number but got \\"some string\\"."
768
- `);
769
- });
770
- });
771
-
772
- describe("port", () => {
773
- it("should default port to 8787 if it is not in use", async () => {
774
- writeWranglerToml({
775
- main: "index.js",
776
- });
777
- fs.writeFileSync("index.js", `export default {};`);
778
- await runWrangler("dev");
779
- expect((Dev as jest.Mock).mock.calls[0][0].port).toEqual(8787);
780
- expect(std.out).toMatchInlineSnapshot(`""`);
781
- expect(std.warn).toMatchInlineSnapshot(`""`);
782
- expect(std.err).toMatchInlineSnapshot(`""`);
783
- });
784
-
785
- it("should use `port` from `wrangler.toml`, if available", async () => {
786
- writeWranglerToml({
787
- main: "index.js",
788
- dev: {
789
- port: 8888,
790
- },
791
- });
792
- fs.writeFileSync("index.js", `export default {};`);
793
- // Mock `getPort()` to resolve to a completely different port.
794
- (getPort as jest.Mock).mockResolvedValue(98765);
795
-
796
- await runWrangler("dev");
797
- expect((Dev as jest.Mock).mock.calls[0][0].port).toEqual(8888);
798
- expect(std.out).toMatchInlineSnapshot(`""`);
799
- expect(std.warn).toMatchInlineSnapshot(`""`);
800
- expect(std.err).toMatchInlineSnapshot(`""`);
801
- });
802
-
803
- it("should error if a bad dev.port config is provided", async () => {
804
- writeWranglerToml({
805
- main: "index.js",
806
- dev: {
807
- // @ts-expect-error intentionally bad port
808
- port: "some string",
809
- },
810
- });
811
- fs.writeFileSync("index.js", `export default {};`);
812
- await expect(runWrangler("dev")).rejects
813
- .toThrowErrorMatchingInlineSnapshot(`
814
- "Processing wrangler.toml configuration:
815
- - Expected \\"dev.port\\" to be of type number but got \\"some string\\"."
816
- `);
817
- });
818
-
819
- it("should use --port command line arg, if provided", async () => {
820
- writeWranglerToml({
821
- main: "index.js",
822
- dev: {
823
- port: 8888,
824
- },
825
- });
826
- fs.writeFileSync("index.js", `export default {};`);
827
- // Mock `getPort()` to resolve to a completely different port.
828
- (getPort as jest.Mock).mockResolvedValue(98765);
829
-
830
- await runWrangler("dev --port=9999");
831
- expect((Dev as jest.Mock).mock.calls[0][0].port).toEqual(9999);
832
- expect(std.out).toMatchInlineSnapshot(`""`);
833
- expect(std.warn).toMatchInlineSnapshot(`""`);
834
- expect(std.err).toMatchInlineSnapshot(`""`);
835
- });
836
-
837
- it("should use a different port to the default if it is in use", async () => {
838
- writeWranglerToml({
839
- main: "index.js",
840
- });
841
- fs.writeFileSync("index.js", `export default {};`);
842
- // Mock `getPort()` to resolve to a completely different port.
843
- (getPort as jest.Mock).mockResolvedValue(98765);
844
-
845
- await runWrangler("dev");
846
- expect((Dev as jest.Mock).mock.calls[0][0].port).toEqual(98765);
847
- expect(std.out).toMatchInlineSnapshot(`""`);
848
- expect(std.warn).toMatchInlineSnapshot(`""`);
849
- expect(std.err).toMatchInlineSnapshot(`""`);
850
- });
851
- });
852
-
853
- describe("durable_objects", () => {
854
- it("should warn if there are remote Durable Objects, or missing migrations for local Durable Objects", async () => {
855
- writeWranglerToml({
856
- main: "index.js",
857
- durable_objects: {
858
- bindings: [
859
- { name: "NAME_1", class_name: "CLASS_1" },
860
- {
861
- name: "NAME_2",
862
- class_name: "CLASS_2",
863
- script_name: "SCRIPT_A",
864
- },
865
- { name: "NAME_3", class_name: "CLASS_3" },
866
- {
867
- name: "NAME_4",
868
- class_name: "CLASS_4",
869
- script_name: "SCRIPT_B",
870
- },
871
- ],
872
- },
873
- });
874
- fs.writeFileSync("index.js", `export default {};`);
875
- await runWrangler("dev");
876
- expect((Dev as jest.Mock).mock.calls[0][0].ip).toEqual("0.0.0.0");
877
- expect(std.out).toMatchInlineSnapshot(`
878
- "Your worker has access to the following bindings:
879
- - Durable Objects:
880
- - NAME_1: CLASS_1
881
- - NAME_2: CLASS_2 (defined in SCRIPT_A)
882
- - NAME_3: CLASS_3
883
- - NAME_4: CLASS_4 (defined in SCRIPT_B)"
884
- `);
885
- expect(std.warn).toMatchInlineSnapshot(`
886
- "▲ [WARNING] Processing wrangler.toml configuration:
887
-
888
- - In wrangler.toml, you have configured [durable_objects] exported by this Worker (CLASS_1,
889
- CLASS_3), but no [migrations] for them. This may not work as expected until you add a [migrations]
890
- section to your wrangler.toml. Add this configuration to your wrangler.toml:
891
-
892
- \`\`\`
893
- [[migrations]]
894
- tag = \\"v1\\" # Should be unique for each entry
895
- new_classes = [\\"CLASS_1\\", \\"CLASS_3\\"]
896
- \`\`\`
897
-
898
- Refer to
899
- https://developers.cloudflare.com/workers/learning/using-durable-objects/#durable-object-migrations-in-wranglertoml
900
- for more details.
901
-
902
-
903
- ▲ [WARNING] WARNING: You have Durable Object bindings that are not defined locally in the worker being developed.
904
-
905
- Be aware that changes to the data stored in these Durable Objects will be permanent and affect the
906
- live instances.
907
- Remote Durable Objects that are affected:
908
- - {\\"name\\":\\"NAME_2\\",\\"class_name\\":\\"CLASS_2\\",\\"script_name\\":\\"SCRIPT_A\\"}
909
- - {\\"name\\":\\"NAME_4\\",\\"class_name\\":\\"CLASS_4\\",\\"script_name\\":\\"SCRIPT_B\\"}
910
-
911
- "
912
- `);
913
- expect(std.err).toMatchInlineSnapshot(`""`);
914
- });
915
- });
916
-
917
- describe(".dev.vars", () => {
918
- it("should override `vars` bindings from `wrangler.toml` with values in `.dev.vars`", async () => {
919
- fs.writeFileSync("index.js", `export default {};`);
920
-
921
- const localVarsEnvContent = dedent`
922
- # Preceding comment
923
- VAR_1="var #1 value" # End of line comment
924
- VAR_3="var #3 value"
925
- VAR_MULTI_LINE_1="A: line 1
926
- line 2"
927
- VAR_MULTI_LINE_2="B: line 1\\nline 2"
928
- EMPTY=
929
- UNQUOTED= unquoted value
930
- `;
931
- fs.writeFileSync(".dev.vars", localVarsEnvContent, "utf8");
932
-
933
- writeWranglerToml({
934
- main: "index.js",
935
- vars: {
936
- VAR_1: "original value 1",
937
- VAR_2: "original value 2", // should not get overridden
938
- VAR_3: "original value 3",
939
- VAR_MULTI_LINE_1: "original multi-line 1",
940
- VAR_MULTI_LINE_2: "original multi-line 2",
941
- EMPTY: "original empty",
942
- UNQUOTED: "original unquoted",
943
- },
944
- });
945
- await runWrangler("dev");
946
- const varBindings: Record<string, unknown> = (Dev as jest.Mock).mock
947
- .calls[0][0].bindings.vars;
948
-
949
- expect(varBindings).toEqual({
950
- VAR_1: "var #1 value",
951
- VAR_2: "original value 2",
952
- VAR_3: "var #3 value",
953
- VAR_MULTI_LINE_1: "A: line 1\nline 2",
954
- VAR_MULTI_LINE_2: "B: line 1\nline 2",
955
- EMPTY: "",
956
- UNQUOTED: "unquoted value", // Note that whitespace is trimmed
957
- });
958
- expect(std.out).toMatchInlineSnapshot(`
959
- "Using vars defined in .dev.vars
960
- Your worker has access to the following bindings:
961
- - Vars:
962
- - VAR_1: \\"(hidden)\\"
963
- - VAR_2: \\"original value 2\\"
964
- - VAR_3: \\"(hidden)\\"
965
- - VAR_MULTI_LINE_1: \\"(hidden)\\"
966
- - VAR_MULTI_LINE_2: \\"(hidden)\\"
967
- - EMPTY: \\"(hidden)\\"
968
- - UNQUOTED: \\"(hidden)\\""
969
- `);
970
- expect(std.warn).toMatchInlineSnapshot(`""`);
971
- expect(std.err).toMatchInlineSnapshot(`""`);
972
- });
973
- });
974
-
975
- describe("serve static assets", () => {
976
- it("should error if --site is used with no value", async () => {
977
- await expect(
978
- runWrangler("dev --site")
979
- ).rejects.toThrowErrorMatchingInlineSnapshot(
980
- `"Not enough arguments following: site"`
981
- );
982
-
983
- expect(std).toMatchInlineSnapshot(`
984
- Object {
985
- "debug": "",
986
- "err": "X [ERROR] Not enough arguments following: site
987
-
988
- ",
989
- "out": "
990
- wrangler dev [script]
991
-
992
- 👂 Start a local server for developing your worker
993
-
994
- Positionals:
995
- script The path to an entry point for your worker [string]
996
-
997
- Flags:
998
- -c, --config Path to .toml configuration file [string]
999
- -h, --help Show help [boolean]
1000
- -v, --version Show version number [boolean]
1001
-
1002
- Options:
1003
- --name Name of the worker [string]
1004
- --no-bundle Skip internal build steps and directly publish script [boolean] [default: false]
1005
- -e, --env Perform on a specific environment [string]
1006
- --compatibility-date Date to use for compatibility checks [string]
1007
- --compatibility-flags, --compatibility-flag Flags to use for compatibility checks [array]
1008
- --latest Use the latest version of the worker runtime [boolean] [default: true]
1009
- --ip IP address to listen on [string]
1010
- --port Port to listen on [number]
1011
- --inspector-port Port for devtools to connect to [number]
1012
- --routes, --route Routes to upload [array]
1013
- --host Host to forward requests to, defaults to the zone of project [string]
1014
- --local-protocol Protocol to listen to requests on, defaults to http. [choices: \\"http\\", \\"https\\"]
1015
- --local-upstream Host to act as origin in local mode, defaults to dev.host or route [string]
1016
- --assets Static assets to be served [string]
1017
- --site Root folder of static assets for Workers Sites [string]
1018
- --site-include Array of .gitignore-style patterns that match file or directory names from the sites directory. Only matched items will be uploaded. [array]
1019
- --site-exclude Array of .gitignore-style patterns that match file or directory names from the sites directory. Matched items will not be uploaded. [array]
1020
- --upstream-protocol Protocol to forward requests to host on, defaults to https. [choices: \\"http\\", \\"https\\"]
1021
- --var A key-value pair to be injected into the script as a variable [array]
1022
- --define A key-value pair to be substituted in the script [array]
1023
- --jsx-factory The function that is called for each JSX element [string]
1024
- --jsx-fragment The function that is called for each JSX fragment [string]
1025
- --tsconfig Path to a custom tsconfig.json file [string]
1026
- -l, --local Run on my machine [boolean] [default: false]
1027
- --experimental-local Run on my machine using the Cloudflare Workers runtime [boolean] [default: false]
1028
- --minify Minify the script [boolean]
1029
- --node-compat Enable node.js compatibility [boolean]
1030
- --persist Enable persistence for local mode, using default path: .wrangler/state [boolean]
1031
- --persist-to Specify directory to use for local persistence (implies --persist) [string]
1032
- --test-scheduled Test scheduled events by visiting /__scheduled in browser [boolean] [default: false]
1033
- --log-level Specify logging level [choices: \\"debug\\", \\"info\\", \\"log\\", \\"warn\\", \\"error\\", \\"none\\"] [default: \\"log\\"]",
1034
- "warn": "",
1035
- }
1036
- `);
1037
- });
1038
-
1039
- it("should error if --assets and --site are used together", async () => {
1040
- writeWranglerToml({
1041
- main: "./index.js",
1042
- });
1043
- fs.writeFileSync("index.js", `export default {};`);
1044
- await expect(
1045
- runWrangler("dev --assets abc --site xyz")
1046
- ).rejects.toThrowErrorMatchingInlineSnapshot(
1047
- `"Cannot use Assets and Workers Sites in the same Worker."`
1048
- );
1049
- });
1050
-
1051
- it("should error if --assets and config.site are used together", async () => {
1052
- writeWranglerToml({
1053
- main: "./index.js",
1054
- site: {
1055
- bucket: "xyz",
1056
- },
1057
- });
1058
- fs.writeFileSync("index.js", `export default {};`);
1059
- await expect(
1060
- runWrangler("dev --assets abc")
1061
- ).rejects.toThrowErrorMatchingInlineSnapshot(
1062
- `"Cannot use Assets and Workers Sites in the same Worker."`
1063
- );
1064
- });
1065
-
1066
- it("should error if config.assets and --site are used together", async () => {
1067
- writeWranglerToml({
1068
- main: "./index.js",
1069
- // @ts-expect-error we allow string inputs here
1070
- assets: "abc",
1071
- });
1072
- fs.writeFileSync("index.js", `export default {};`);
1073
- await expect(
1074
- runWrangler("dev --site xyz")
1075
- ).rejects.toThrowErrorMatchingInlineSnapshot(
1076
- `"Cannot use Assets and Workers Sites in the same Worker."`
1077
- );
1078
- });
1079
-
1080
- it("should error if config.assets and config.site are used together", async () => {
1081
- writeWranglerToml({
1082
- main: "./index.js",
1083
- // @ts-expect-error we allow string inputs here
1084
- assets: "abc",
1085
- site: {
1086
- bucket: "xyz",
1087
- },
1088
- });
1089
- fs.writeFileSync("index.js", `export default {};`);
1090
- await expect(
1091
- runWrangler("dev --assets abc")
1092
- ).rejects.toThrowErrorMatchingInlineSnapshot(
1093
- `"Cannot use Assets and Workers Sites in the same Worker."`
1094
- );
1095
- });
1096
-
1097
- it("should indicate whether Sites is being used", async () => {
1098
- writeWranglerToml({
1099
- main: "index.js",
1100
- });
1101
- fs.writeFileSync("index.js", `export default {};`);
1102
-
1103
- await runWrangler("dev");
1104
- expect((Dev as jest.Mock).mock.calls[0][0].isWorkersSite).toEqual(false);
1105
-
1106
- await runWrangler("dev --site abc");
1107
- expect((Dev as jest.Mock).mock.calls[1][0].isWorkersSite).toEqual(true);
1108
-
1109
- await runWrangler("dev --assets abc");
1110
- expect((Dev as jest.Mock).mock.calls[2][0].isWorkersSite).toEqual(false);
1111
- });
1112
- it("should warn if --assets is used", async () => {
1113
- writeWranglerToml({
1114
- main: "./index.js",
1115
- });
1116
- fs.writeFileSync("index.js", `export default {};`);
1117
-
1118
- await runWrangler('dev --assets "./assets"');
1119
- expect(std).toMatchInlineSnapshot(`
1120
- Object {
1121
- "debug": "",
1122
- "err": "",
1123
- "out": "",
1124
- "warn": "▲ [WARNING] The --assets argument is experimental and may change or break at any time
1125
-
1126
- ",
1127
- }
1128
- `);
1129
- });
1130
-
1131
- it("should warn if config.assets is used", async () => {
1132
- writeWranglerToml({
1133
- main: "./index.js",
1134
- // @ts-expect-error we allow string inputs here
1135
- assets: "./assets",
1136
- });
1137
- fs.writeFileSync("index.js", `export default {};`);
1138
-
1139
- await runWrangler("dev");
1140
- expect(std).toMatchInlineSnapshot(`
1141
- Object {
1142
- "debug": "",
1143
- "err": "",
1144
- "out": "",
1145
- "warn": "▲ [WARNING] Processing wrangler.toml configuration:
1146
-
1147
- - \\"assets\\" fields are experimental and may change or break at any time.
1148
-
1149
- ",
1150
- }
1151
- `);
1152
- });
1153
- });
1154
-
1155
- describe("--inspect", () => {
1156
- it("should warn if --inspect is used", async () => {
1157
- fs.writeFileSync("index.js", `export default {};`);
1158
- await runWrangler("dev index.js --inspect");
1159
- expect(std).toMatchInlineSnapshot(`
1160
- Object {
1161
- "debug": "",
1162
- "err": "",
1163
- "out": "",
1164
- "warn": "▲ [WARNING] Passing --inspect is unnecessary, now you can always connect to devtools.
1165
-
1166
- ",
1167
- }
1168
- `);
1169
- });
1170
-
1171
- it("should default to true, without a warning", async () => {
1172
- fs.writeFileSync("index.js", `export default {};`);
1173
- await runWrangler("dev index.js");
1174
- expect((Dev as jest.Mock).mock.calls[0][0].inspect).toEqual(true);
1175
- expect(std).toMatchInlineSnapshot(`
1176
- Object {
1177
- "debug": "",
1178
- "err": "",
1179
- "out": "",
1180
- "warn": "",
1181
- }
1182
- `);
1183
- });
1184
-
1185
- it("should pass true, with a warning", async () => {
1186
- fs.writeFileSync("index.js", `export default {};`);
1187
- await runWrangler("dev index.js --inspect");
1188
- expect((Dev as jest.Mock).mock.calls[0][0].inspect).toEqual(true);
1189
- expect(std).toMatchInlineSnapshot(`
1190
- Object {
1191
- "debug": "",
1192
- "err": "",
1193
- "out": "",
1194
- "warn": "▲ [WARNING] Passing --inspect is unnecessary, now you can always connect to devtools.
1195
-
1196
- ",
1197
- }
1198
- `);
1199
- });
1200
-
1201
- it("should pass false, without a warning", async () => {
1202
- fs.writeFileSync("index.js", `export default {};`);
1203
- await runWrangler("dev index.js --inspect false");
1204
- expect((Dev as jest.Mock).mock.calls[0][0].inspect).toEqual(false);
1205
- expect(std).toMatchInlineSnapshot(`
1206
- Object {
1207
- "debug": "",
1208
- "err": "",
1209
- "out": "",
1210
- "warn": "",
1211
- }
1212
- `);
1213
- });
1214
- });
1215
-
1216
- describe("--log-level", () => {
1217
- it("should not output warnings with log-level 'none'", async () => {
1218
- fs.writeFileSync("index.js", `export default {};`);
1219
- await runWrangler("dev index.js --inspect --log-level none");
1220
- expect(std).toMatchInlineSnapshot(`
1221
- Object {
1222
- "debug": "",
1223
- "err": "",
1224
- "out": "",
1225
- "warn": "",
1226
- }
1227
- `);
1228
- });
1229
-
1230
- it("should output warnings with log-level 'warn'", async () => {
1231
- fs.writeFileSync("index.js", `export default {};`);
1232
- await runWrangler("dev index.js --inspect --log-level warn");
1233
- expect(std).toMatchInlineSnapshot(`
1234
- Object {
1235
- "debug": "",
1236
- "err": "",
1237
- "out": "",
1238
- "warn": "▲ [WARNING] Passing --inspect is unnecessary, now you can always connect to devtools.
1239
-
1240
- ",
1241
- }
1242
- `);
1243
- });
1244
-
1245
- it("should not output Errors with log-level error", async () => {
1246
- fs.writeFileSync("index.js", `export default {};`);
1247
- await runWrangler("dev index.js --inspect --log-level debug");
1248
- expect(std.debug.length > 1).toBe(true);
1249
- });
1250
- });
1251
-
1252
- describe("service bindings", () => {
1253
- it("should warn when using service bindings", async () => {
1254
- writeWranglerToml({
1255
- services: [
1256
- { binding: "WorkerA", service: "A" },
1257
- { binding: "WorkerB", service: "B", environment: "staging" },
1258
- ],
1259
- });
1260
- fs.writeFileSync("index.js", `export default {};`);
1261
- await runWrangler("dev index.js");
1262
- expect(std).toMatchInlineSnapshot(`
1263
- Object {
1264
- "debug": "",
1265
- "err": "",
1266
- "out": "Your worker has access to the following bindings:
1267
- - Services:
1268
- - WorkerA: A
1269
- - WorkerB: B - staging",
1270
- "warn": "▲ [WARNING] Processing wrangler.toml configuration:
1271
-
1272
- - \\"services\\" fields are experimental and may change or break at any time.
1273
-
1274
-
1275
- ▲ [WARNING] This worker is bound to live services: WorkerA (A), WorkerB (B@staging)
1276
-
1277
- ",
1278
- }
1279
- `);
1280
- });
1281
- });
1282
-
1283
- describe("print bindings", () => {
1284
- it("should print bindings", async () => {
1285
- writeWranglerToml({
1286
- services: [
1287
- { binding: "WorkerA", service: "A" },
1288
- { binding: "WorkerB", service: "B", environment: "staging" },
1289
- ],
1290
- });
1291
- fs.writeFileSync("index.js", `export default {};`);
1292
- await runWrangler("dev index.js");
1293
- expect(std).toMatchInlineSnapshot(`
1294
- Object {
1295
- "debug": "",
1296
- "err": "",
1297
- "out": "Your worker has access to the following bindings:
1298
- - Services:
1299
- - WorkerA: A
1300
- - WorkerB: B - staging",
1301
- "warn": "▲ [WARNING] Processing wrangler.toml configuration:
1302
-
1303
- - \\"services\\" fields are experimental and may change or break at any time.
1304
-
1305
-
1306
- ▲ [WARNING] This worker is bound to live services: WorkerA (A), WorkerB (B@staging)
1307
-
1308
- ",
1309
- }
1310
- `);
1311
- });
1312
-
1313
- it("should mask vars that were overriden in .dev.vars", async () => {
1314
- writeWranglerToml({
1315
- vars: {
1316
- variable: 123,
1317
- overriden: "original values",
1318
- },
1319
- });
1320
- fs.writeFileSync(
1321
- ".dev.vars",
1322
- `
1323
- SECRET = "A secret"
1324
- overriden = "overriden value"
1325
- `
1326
- );
1327
- fs.writeFileSync("index.js", `export default {};`);
1328
- await runWrangler("dev index.js");
1329
- expect(std).toMatchInlineSnapshot(`
1330
- Object {
1331
- "debug": "",
1332
- "err": "",
1333
- "out": "Using vars defined in .dev.vars
1334
- Your worker has access to the following bindings:
1335
- - Vars:
1336
- - variable: 123
1337
- - overriden: \\"(hidden)\\"
1338
- - SECRET: \\"(hidden)\\"",
1339
- "warn": "",
1340
- }
1341
- `);
1342
- });
1343
- });
1344
- });
1345
-
1346
- function mockGetZones(domain: string, zones: { id: string }[] = []) {
1347
- const removeMock = setMockResponse(
1348
- "/zones",
1349
- "GET",
1350
- (_urlPieces, _init, queryParams) => {
1351
- expect([...queryParams.entries()]).toEqual([["name", domain]]);
1352
- // 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.
1353
- removeMock();
1354
- return zones;
1355
- }
1356
- );
1357
- }