wrangler 0.0.0-e41c7e41 → 0.0.0-e44f496a8

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 (272) hide show
  1. package/README.md +17 -15
  2. package/bin/wrangler.js +24 -93
  3. package/config-schema.json +3126 -0
  4. package/package.json +198 -192
  5. package/templates/__tests__/tsconfig-sanity.ts +12 -0
  6. package/templates/__tests__/tsconfig.json +8 -0
  7. package/templates/checked-fetch.js +19 -6
  8. package/templates/facade.d.ts +19 -0
  9. package/templates/gitignore +2 -3
  10. package/templates/init-tests/test-jest-new-worker.js +3 -5
  11. package/templates/init-tests/test-vitest-new-worker.js +4 -6
  12. package/templates/init-tests/test-vitest-new-worker.ts +25 -0
  13. package/templates/middleware/common.ts +9 -4
  14. package/templates/middleware/loader-modules.ts +94 -44
  15. package/templates/middleware/loader-sw.ts +6 -4
  16. package/templates/middleware/middleware-ensure-req-body-drained.ts +18 -0
  17. package/templates/middleware/middleware-miniflare3-json-error.ts +17 -5
  18. package/templates/middleware/middleware-mock-analytics-engine.d.ts +3 -0
  19. package/templates/middleware/middleware-mock-analytics-engine.ts +30 -0
  20. package/templates/middleware/middleware-scheduled.ts +15 -1
  21. package/templates/middleware/middleware-serve-static-assets.d.ts +6 -0
  22. package/templates/middleware/middleware-serve-static-assets.ts +56 -0
  23. package/templates/modules-watch-stub.js +4 -0
  24. package/templates/new-worker-scheduled.ts +1 -1
  25. package/templates/new-worker.ts +4 -1
  26. package/templates/pages-dev-pipeline.ts +6 -2
  27. package/templates/pages-dev-util.ts +4 -1
  28. package/templates/pages-shim.ts +1 -1
  29. package/templates/pages-template-plugin.ts +33 -14
  30. package/templates/pages-template-worker.ts +12 -3
  31. package/templates/startDevWorker/InspectorProxyWorker.ts +664 -0
  32. package/templates/startDevWorker/ProxyWorker.ts +336 -0
  33. package/templates/tsconfig-sanity.ts +11 -0
  34. package/templates/tsconfig.init.json +22 -0
  35. package/templates/tsconfig.json +5 -103
  36. package/wrangler-dist/InspectorProxyWorker.js +464 -0
  37. package/wrangler-dist/InspectorProxyWorker.js.map +6 -0
  38. package/wrangler-dist/ProxyWorker.js +241 -0
  39. package/wrangler-dist/ProxyWorker.js.map +6 -0
  40. package/wrangler-dist/cli.d.ts +26122 -519
  41. package/wrangler-dist/cli.js +174096 -118298
  42. package/wrangler-dist/wasm-sync.wasm +0 -0
  43. package/Cloudflare_CA.pem +0 -18
  44. package/import_meta_url.js +0 -3
  45. package/miniflare-config-stubs/.env.empty +0 -0
  46. package/miniflare-config-stubs/package.empty.json +0 -1
  47. package/miniflare-config-stubs/wrangler.empty.toml +0 -0
  48. package/miniflare-dist/index.mjs +0 -6316
  49. package/src/__tests__/access.test.ts +0 -25
  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 -3981
  56. package/src/__tests__/d1.test.ts +0 -80
  57. package/src/__tests__/delete.test.ts +0 -230
  58. package/src/__tests__/deployments.test.ts +0 -91
  59. package/src/__tests__/dev.test.tsx +0 -1536
  60. package/src/__tests__/dialogs.test.tsx +0 -40
  61. package/src/__tests__/generate.test.ts +0 -235
  62. package/src/__tests__/get-host-from-url.test.ts +0 -16
  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/mock-account-id.ts +0 -48
  67. package/src/__tests__/helpers/mock-bin.ts +0 -36
  68. package/src/__tests__/helpers/mock-cfetch.ts +0 -333
  69. package/src/__tests__/helpers/mock-console.ts +0 -106
  70. package/src/__tests__/helpers/mock-dialogs.ts +0 -153
  71. package/src/__tests__/helpers/mock-get-zone-from-host.ts +0 -8
  72. package/src/__tests__/helpers/mock-http-server.ts +0 -46
  73. package/src/__tests__/helpers/mock-istty.ts +0 -74
  74. package/src/__tests__/helpers/mock-known-routes.ts +0 -7
  75. package/src/__tests__/helpers/mock-kv.ts +0 -33
  76. package/src/__tests__/helpers/mock-oauth-flow.ts +0 -252
  77. package/src/__tests__/helpers/mock-process.ts +0 -39
  78. package/src/__tests__/helpers/mock-stdin.ts +0 -108
  79. package/src/__tests__/helpers/mock-web-socket.ts +0 -29
  80. package/src/__tests__/helpers/msw/handlers/access.ts +0 -13
  81. package/src/__tests__/helpers/msw/handlers/deployments.ts +0 -59
  82. package/src/__tests__/helpers/msw/handlers/namespaces.ts +0 -104
  83. package/src/__tests__/helpers/msw/handlers/oauth.ts +0 -36
  84. package/src/__tests__/helpers/msw/handlers/r2.ts +0 -80
  85. package/src/__tests__/helpers/msw/handlers/script.ts +0 -75
  86. package/src/__tests__/helpers/msw/handlers/user.ts +0 -63
  87. package/src/__tests__/helpers/msw/handlers/zones.ts +0 -22
  88. package/src/__tests__/helpers/msw/index.ts +0 -21
  89. package/src/__tests__/helpers/run-in-tmp.ts +0 -38
  90. package/src/__tests__/helpers/run-wrangler.ts +0 -16
  91. package/src/__tests__/helpers/worker-scripts/child-wrangler.toml +0 -1
  92. package/src/__tests__/helpers/worker-scripts/hello-world-worker.js +0 -5
  93. package/src/__tests__/helpers/worker-scripts/hello-world-wrangler.toml +0 -1
  94. package/src/__tests__/helpers/worker-scripts/parent-worker.js +0 -8
  95. package/src/__tests__/helpers/worker-scripts/parent-wrangler.toml +0 -5
  96. package/src/__tests__/helpers/write-worker-source.ts +0 -31
  97. package/src/__tests__/helpers/write-wrangler-toml.ts +0 -17
  98. package/src/__tests__/https-options.test.ts +0 -163
  99. package/src/__tests__/index.test.ts +0 -264
  100. package/src/__tests__/init.test.ts +0 -2900
  101. package/src/__tests__/jest.setup.ts +0 -202
  102. package/src/__tests__/kv.test.ts +0 -1675
  103. package/src/__tests__/logger.test.ts +0 -141
  104. package/src/__tests__/metrics.test.ts +0 -450
  105. package/src/__tests__/middleware.scheduled.test.ts +0 -135
  106. package/src/__tests__/middleware.test.ts +0 -726
  107. package/src/__tests__/package-manager.test.ts +0 -353
  108. package/src/__tests__/pages-deployment-tail.test.ts +0 -820
  109. package/src/__tests__/pages.test.ts +0 -2442
  110. package/src/__tests__/parse.test.ts +0 -330
  111. package/src/__tests__/paths.test.ts +0 -39
  112. package/src/__tests__/publish.test.ts +0 -7633
  113. package/src/__tests__/pubsub.test.ts +0 -370
  114. package/src/__tests__/queues.test.ts +0 -371
  115. package/src/__tests__/r2.test.ts +0 -393
  116. package/src/__tests__/route.test.ts +0 -45
  117. package/src/__tests__/secret.test.ts +0 -644
  118. package/src/__tests__/tail.test.ts +0 -800
  119. package/src/__tests__/test-old-node-version.js +0 -31
  120. package/src/__tests__/type-generation.test.ts +0 -226
  121. package/src/__tests__/user.test.ts +0 -155
  122. package/src/__tests__/utils-collectKeyValues.test.ts +0 -47
  123. package/src/__tests__/validate-dev-props.test.ts +0 -56
  124. package/src/__tests__/version.test.ts +0 -35
  125. package/src/__tests__/whoami.test.tsx +0 -278
  126. package/src/__tests__/worker-namespace.test.ts +0 -326
  127. package/src/abort.d.ts +0 -3
  128. package/src/api/dev.ts +0 -210
  129. package/src/api/index.ts +0 -2
  130. package/src/bundle-reporter.tsx +0 -68
  131. package/src/bundle.ts +0 -823
  132. package/src/cfetch/index.ts +0 -160
  133. package/src/cfetch/internal.ts +0 -263
  134. package/src/cli.ts +0 -28
  135. package/src/config/README.md +0 -107
  136. package/src/config/config.ts +0 -275
  137. package/src/config/diagnostics.ts +0 -80
  138. package/src/config/environment.ts +0 -561
  139. package/src/config/index.ts +0 -334
  140. package/src/config/validation-helpers.ts +0 -597
  141. package/src/config/validation.ts +0 -2103
  142. package/src/config-cache.ts +0 -85
  143. package/src/create-worker-preview.ts +0 -282
  144. package/src/create-worker-upload-form.ts +0 -316
  145. package/src/d1/backups.tsx +0 -218
  146. package/src/d1/constants.ts +0 -2
  147. package/src/d1/create.tsx +0 -63
  148. package/src/d1/delete.tsx +0 -56
  149. package/src/d1/execute.tsx +0 -352
  150. package/src/d1/formatTimeAgo.ts +0 -14
  151. package/src/d1/index.ts +0 -99
  152. package/src/d1/list.tsx +0 -48
  153. package/src/d1/migrations.tsx +0 -446
  154. package/src/d1/options.ts +0 -22
  155. package/src/d1/types.tsx +0 -24
  156. package/src/d1/utils.ts +0 -48
  157. package/src/delete.ts +0 -108
  158. package/src/deployments.ts +0 -77
  159. package/src/deprecated/index.ts +0 -438
  160. package/src/dev/dev-vars.ts +0 -39
  161. package/src/dev/dev.tsx +0 -590
  162. package/src/dev/get-local-persistence-path.tsx +0 -31
  163. package/src/dev/local.tsx +0 -886
  164. package/src/dev/remote.tsx +0 -603
  165. package/src/dev/start-server.ts +0 -538
  166. package/src/dev/use-esbuild.ts +0 -204
  167. package/src/dev/validate-dev-props.ts +0 -31
  168. package/src/dev-registry.tsx +0 -201
  169. package/src/dev.tsx +0 -905
  170. package/src/dialogs.tsx +0 -168
  171. package/src/dispatch-namespace.ts +0 -207
  172. package/src/durable.ts +0 -102
  173. package/src/entry.ts +0 -329
  174. package/src/environment-variables.ts +0 -50
  175. package/src/errors.ts +0 -11
  176. package/src/git-client.ts +0 -124
  177. package/src/global-wrangler-config-path.ts +0 -26
  178. package/src/https-options.ts +0 -127
  179. package/src/index.tsx +0 -682
  180. package/src/init.ts +0 -970
  181. package/src/inspect.ts +0 -767
  182. package/src/intl-polyfill.d.ts +0 -139
  183. package/src/is-ci.ts +0 -14
  184. package/src/is-interactive.ts +0 -16
  185. package/src/kv/helpers.ts +0 -430
  186. package/src/kv/index.ts +0 -596
  187. package/src/logger.ts +0 -92
  188. package/src/metrics/index.ts +0 -5
  189. package/src/metrics/metrics-config.ts +0 -239
  190. package/src/metrics/metrics-dispatcher.ts +0 -96
  191. package/src/metrics/metrics-usage-headers.ts +0 -24
  192. package/src/metrics/send-event.ts +0 -96
  193. package/src/miniflare-cli/README.md +0 -30
  194. package/src/miniflare-cli/assets.ts +0 -215
  195. package/src/miniflare-cli/index.ts +0 -213
  196. package/src/miniflare-cli/request-context.ts +0 -40
  197. package/src/module-collection.ts +0 -267
  198. package/src/open-in-browser.ts +0 -17
  199. package/src/package-manager.ts +0 -219
  200. package/src/pages/build.tsx +0 -270
  201. package/src/pages/constants.ts +0 -18
  202. package/src/pages/deployment-tails.tsx +0 -284
  203. package/src/pages/deployments.tsx +0 -84
  204. package/src/pages/dev.tsx +0 -708
  205. package/src/pages/errors.ts +0 -66
  206. package/src/pages/functions/buildPlugin.ts +0 -107
  207. package/src/pages/functions/buildWorker.ts +0 -166
  208. package/src/pages/functions/filepath-routing.test.ts +0 -234
  209. package/src/pages/functions/filepath-routing.ts +0 -189
  210. package/src/pages/functions/identifiers.ts +0 -78
  211. package/src/pages/functions/routes-consolidation.test.ts +0 -250
  212. package/src/pages/functions/routes-consolidation.ts +0 -73
  213. package/src/pages/functions/routes-transformation.test.ts +0 -282
  214. package/src/pages/functions/routes-transformation.ts +0 -115
  215. package/src/pages/functions/routes-validation.test.ts +0 -403
  216. package/src/pages/functions/routes-validation.ts +0 -202
  217. package/src/pages/functions/routes.ts +0 -151
  218. package/src/pages/functions.tsx +0 -82
  219. package/src/pages/hash.tsx +0 -13
  220. package/src/pages/index.tsx +0 -102
  221. package/src/pages/projects.tsx +0 -169
  222. package/src/pages/prompt-select-project.tsx +0 -31
  223. package/src/pages/publish.tsx +0 -451
  224. package/src/pages/types.ts +0 -41
  225. package/src/pages/upload.tsx +0 -426
  226. package/src/pages/utils.ts +0 -23
  227. package/src/parse.ts +0 -286
  228. package/src/paths.ts +0 -71
  229. package/src/proxy.ts +0 -689
  230. package/src/publish/index.ts +0 -270
  231. package/src/publish/publish.ts +0 -1035
  232. package/src/pubsub/index.ts +0 -286
  233. package/src/pubsub/pubsub-commands.tsx +0 -624
  234. package/src/queues/cli/commands/consumer/add.ts +0 -71
  235. package/src/queues/cli/commands/consumer/index.ts +0 -22
  236. package/src/queues/cli/commands/consumer/remove.ts +0 -38
  237. package/src/queues/cli/commands/create.ts +0 -25
  238. package/src/queues/cli/commands/delete.ts +0 -26
  239. package/src/queues/cli/commands/index.ts +0 -33
  240. package/src/queues/cli/commands/list.ts +0 -25
  241. package/src/queues/client.ts +0 -135
  242. package/src/r2/constants.ts +0 -4
  243. package/src/r2/helpers.ts +0 -131
  244. package/src/r2/index.ts +0 -289
  245. package/src/routes.ts +0 -140
  246. package/src/secret/index.ts +0 -378
  247. package/src/selfsigned.d.ts +0 -29
  248. package/src/sites.tsx +0 -372
  249. package/src/tail/createTail.ts +0 -379
  250. package/src/tail/filters.ts +0 -277
  251. package/src/tail/index.ts +0 -214
  252. package/src/tail/printing.ts +0 -107
  253. package/src/type-generation.ts +0 -173
  254. package/src/update-check.ts +0 -19
  255. package/src/user/access.ts +0 -69
  256. package/src/user/choose-account.tsx +0 -69
  257. package/src/user/env-vars.ts +0 -46
  258. package/src/user/generate-auth-url.ts +0 -33
  259. package/src/user/generate-random-state.ts +0 -16
  260. package/src/user/index.ts +0 -3
  261. package/src/user/user.tsx +0 -1192
  262. package/src/utils/collectKeyValues.ts +0 -14
  263. package/src/whoami.tsx +0 -169
  264. package/src/worker.ts +0 -245
  265. package/src/yargs-types.ts +0 -24
  266. package/src/zones.ts +0 -191
  267. package/templates/d1-beta-facade.js +0 -205
  268. package/templates/first-party-worker-module-facade.ts +0 -18
  269. package/templates/format-dev-errors.ts +0 -32
  270. package/templates/serve-static-assets.ts +0 -63
  271. package/templates/service-bindings-module-facade.js +0 -54
  272. package/templates/service-bindings-sw-facade.js +0 -42
package/src/dev.tsx DELETED
@@ -1,905 +0,0 @@
1
- import path from "node:path";
2
- import chalk from "chalk";
3
- import { watch } from "chokidar";
4
- import getPort from "get-port";
5
- import { render } from "ink";
6
- import React from "react";
7
- import { findWranglerToml, printBindings, readConfig } from "./config";
8
- import Dev from "./dev/dev";
9
- import { getVarsForDev } from "./dev/dev-vars";
10
- import { getLocalPersistencePath } from "./dev/get-local-persistence-path";
11
-
12
- import { startDevServer } from "./dev/start-server";
13
- import { getEntry } from "./entry";
14
- import { logger } from "./logger";
15
- import * as metrics from "./metrics";
16
- import { getAssetPaths, getSiteAssetPaths } from "./sites";
17
- import { getAccountFromCache } from "./user";
18
- import { collectKeyValues } from "./utils/collectKeyValues";
19
- import { identifyD1BindingsAsBeta } from "./worker";
20
- import { getHostFromRoute, getZoneForRoute, getZoneIdFromHost } from "./zones";
21
- import {
22
- type ConfigPath,
23
- DEFAULT_INSPECTOR_PORT,
24
- DEFAULT_LOCAL_PORT,
25
- getDevCompatibilityDate,
26
- getRules,
27
- getScriptName,
28
- isLegacyEnv,
29
- printWranglerBanner,
30
- } from "./index";
31
- import type { Config, Environment } from "./config";
32
- import type { Route } from "./config/environment";
33
- import type { EnablePagesAssetsServiceBindingOptions } from "./miniflare-cli";
34
- import type { CfWorkerInit } from "./worker";
35
- import type { CommonYargsOptions } from "./yargs-types";
36
- import type { Argv, ArgumentsCamelCase } from "yargs";
37
-
38
- interface DevArgs {
39
- config?: string;
40
- script?: string;
41
- name?: string;
42
- bundle?: boolean;
43
- build?: boolean;
44
- format?: string;
45
- env?: string;
46
- "compatibility-date"?: string;
47
- "compatibility-flags"?: string[];
48
- latest?: boolean;
49
- ip?: string;
50
- inspect?: boolean;
51
- port?: number;
52
- "inspector-port"?: number;
53
- routes?: string[];
54
- host?: string;
55
- "local-protocol"?: "http" | "https";
56
- "local-upstream"?: string | undefined;
57
- "experimental-public"?: string;
58
- public?: string;
59
- assets?: string;
60
- site?: string;
61
- "site-include"?: string[];
62
- "site-exclude"?: string[];
63
- "upstream-protocol"?: "http" | "https";
64
- "jsx-factory"?: string;
65
- "jsx-fragment"?: string;
66
- tsconfig?: string;
67
- local?: boolean;
68
- "experimental-local"?: boolean;
69
- "experimental-local-remote-kv"?: boolean;
70
- minify?: boolean;
71
- var?: string[];
72
- define?: string[];
73
- "node-compat"?: boolean;
74
- "experimental-enable-local-persistence"?: boolean;
75
- persist?: boolean;
76
- "persist-to"?: string;
77
- "live-reload"?: boolean;
78
- onReady?: (ip: string, port: number) => void;
79
- logLevel?: "none" | "info" | "error" | "log" | "warn" | "debug";
80
- logPrefix?: string;
81
- showInteractiveDevSession?: boolean;
82
- "test-scheduled"?: boolean;
83
- }
84
-
85
- export function devOptions(yargs: Argv<CommonYargsOptions>): Argv<DevArgs> {
86
- return (
87
- yargs
88
- .positional("script", {
89
- describe: "The path to an entry point for your worker",
90
- type: "string",
91
- })
92
- .option("name", {
93
- describe: "Name of the worker",
94
- type: "string",
95
- requiresArg: true,
96
- })
97
- // We want to have a --no-bundle flag, but yargs requires that
98
- // we also have a --bundle flag (that it adds the --no to by itself)
99
- // So we make a --bundle flag, but hide it, and then add a --no-bundle flag
100
- // that's visible to the user but doesn't "do" anything.
101
- .option("bundle", {
102
- describe: "Run wrangler's compilation step before publishing",
103
- type: "boolean",
104
- hidden: true,
105
- })
106
- .option("no-bundle", {
107
- describe: "Skip internal build steps and directly publish script",
108
- type: "boolean",
109
- default: false,
110
- })
111
- .option("format", {
112
- choices: ["modules", "service-worker"] as const,
113
- describe: "Choose an entry type",
114
- hidden: true,
115
- deprecated: true,
116
- })
117
- .option("compatibility-date", {
118
- describe: "Date to use for compatibility checks",
119
- type: "string",
120
- requiresArg: true,
121
- })
122
- .option("compatibility-flags", {
123
- describe: "Flags to use for compatibility checks",
124
- alias: "compatibility-flag",
125
- type: "string",
126
- requiresArg: true,
127
- array: true,
128
- })
129
- .option("latest", {
130
- describe: "Use the latest version of the worker runtime",
131
- type: "boolean",
132
- default: true,
133
- })
134
- .option("ip", {
135
- describe: "IP address to listen on",
136
- type: "string",
137
- })
138
- .option("port", {
139
- describe: "Port to listen on",
140
- type: "number",
141
- })
142
- .option("inspector-port", {
143
- describe: "Port for devtools to connect to",
144
- type: "number",
145
- })
146
- .option("routes", {
147
- describe: "Routes to upload",
148
- alias: "route",
149
- type: "string",
150
- requiresArg: true,
151
- array: true,
152
- })
153
- .option("host", {
154
- type: "string",
155
- requiresArg: true,
156
- describe:
157
- "Host to forward requests to, defaults to the zone of project",
158
- })
159
- .option("local-protocol", {
160
- describe: "Protocol to listen to requests on, defaults to http.",
161
- choices: ["http", "https"] as const,
162
- })
163
- .options("local-upstream", {
164
- type: "string",
165
- describe:
166
- "Host to act as origin in local mode, defaults to dev.host or route",
167
- })
168
- .option("experimental-public", {
169
- describe: "Static assets to be served",
170
- type: "string",
171
- requiresArg: true,
172
- deprecated: true,
173
- hidden: true,
174
- })
175
- .option("assets", {
176
- describe: "Static assets to be served",
177
- type: "string",
178
- requiresArg: true,
179
- })
180
- .option("site", {
181
- describe: "Root folder of static assets for Workers Sites",
182
- type: "string",
183
- requiresArg: true,
184
- })
185
- .option("site-include", {
186
- describe:
187
- "Array of .gitignore-style patterns that match file or directory names from the sites directory. Only matched items will be uploaded.",
188
- type: "string",
189
- requiresArg: true,
190
- array: true,
191
- })
192
- .option("site-exclude", {
193
- describe:
194
- "Array of .gitignore-style patterns that match file or directory names from the sites directory. Matched items will not be uploaded.",
195
- type: "string",
196
- requiresArg: true,
197
- array: true,
198
- })
199
- .option("upstream-protocol", {
200
- describe: "Protocol to forward requests to host on, defaults to https.",
201
- choices: ["http", "https"] as const,
202
- })
203
- .option("var", {
204
- describe:
205
- "A key-value pair to be injected into the script as a variable",
206
- type: "string",
207
- requiresArg: true,
208
- array: true,
209
- })
210
- .option("define", {
211
- describe: "A key-value pair to be substituted in the script",
212
- type: "string",
213
- requiresArg: true,
214
- array: true,
215
- })
216
- .option("jsx-factory", {
217
- describe: "The function that is called for each JSX element",
218
- type: "string",
219
- requiresArg: true,
220
- })
221
- .option("jsx-fragment", {
222
- describe: "The function that is called for each JSX fragment",
223
- type: "string",
224
- requiresArg: true,
225
- })
226
- .option("tsconfig", {
227
- describe: "Path to a custom tsconfig.json file",
228
- type: "string",
229
- requiresArg: true,
230
- })
231
- .option("local", {
232
- alias: "l",
233
- describe: "Run on my machine",
234
- type: "boolean",
235
- default: false, // I bet this will a point of contention. We'll revisit it.
236
- })
237
- .option("experimental-local", {
238
- describe: "Run on my machine using the Cloudflare Workers runtime",
239
- type: "boolean",
240
- default: false,
241
- })
242
- .option("experimental-local-remote-kv", {
243
- describe:
244
- "Read/write KV data from/to real namespaces on the Cloudflare network",
245
- type: "boolean",
246
- default: false,
247
- })
248
- .check((argv) => {
249
- if (argv.local && argv["experimental-local"]) {
250
- throw new Error(
251
- "--local and --experimental-local are mutually exclusive. " +
252
- "Please enable one or the other."
253
- );
254
- }
255
- if (
256
- argv["experimental-local-remote-kv"] &&
257
- !argv["experimental-local"]
258
- ) {
259
- throw new Error(
260
- "--experimental-local-remote-kv requires --experimental-local to be enabled."
261
- );
262
- }
263
- return true;
264
- })
265
- .option("minify", {
266
- describe: "Minify the script",
267
- type: "boolean",
268
- })
269
- .option("node-compat", {
270
- describe: "Enable node.js compatibility",
271
- type: "boolean",
272
- })
273
- .option("experimental-enable-local-persistence", {
274
- describe:
275
- "Enable persistence for local mode (deprecated, use --persist)",
276
- type: "boolean",
277
- deprecated: true,
278
- hidden: true,
279
- })
280
- .option("persist", {
281
- describe:
282
- "Enable persistence for local mode, using default path: .wrangler/state",
283
- type: "boolean",
284
- })
285
- .option("persist-to", {
286
- describe:
287
- "Specify directory to use for local persistence (implies --persist)",
288
- type: "string",
289
- requiresArg: true,
290
- })
291
- .option("live-reload", {
292
- describe:
293
- "Auto reload HTML pages when change is detected in local mode",
294
- type: "boolean",
295
- })
296
- .check((argv) => {
297
- const local = argv["local"] || argv["experimental-local"];
298
- if (argv["live-reload"] && !local) {
299
- throw new Error(
300
- "--live-reload is only supported in local mode. " +
301
- "Please enable either --local or --experimental-local."
302
- );
303
- }
304
- return true;
305
- })
306
- .option("inspect", {
307
- describe: "Enable dev tools",
308
- type: "boolean",
309
- deprecated: true,
310
- hidden: true,
311
- })
312
- .option("legacy-env", {
313
- type: "boolean",
314
- describe: "Use legacy environments",
315
- hidden: true,
316
- })
317
- .option("test-scheduled", {
318
- describe: "Test scheduled events by visiting /__scheduled in browser",
319
- type: "boolean",
320
- default: false,
321
- })
322
- .option("log-level", {
323
- choices: ["debug", "info", "log", "warn", "error", "none"] as const,
324
- describe: "Specify logging level",
325
- default: "log",
326
- })
327
- );
328
- }
329
-
330
- export async function devHandler(args: ArgumentsCamelCase<DevArgs>) {
331
- let watcher;
332
- try {
333
- const devInstance = await startDev(args);
334
- watcher = devInstance.watcher;
335
- const { waitUntilExit } = devInstance.devReactElement;
336
- await waitUntilExit();
337
- } finally {
338
- await watcher?.close();
339
- }
340
- }
341
-
342
- export type AdditionalDevProps = {
343
- vars?: {
344
- [key: string]: unknown;
345
- };
346
- kv?: {
347
- binding: string;
348
- id: string;
349
- preview_id?: string;
350
- }[];
351
- durableObjects?: {
352
- name: string;
353
- class_name: string;
354
- script_name?: string | undefined;
355
- environment?: string | undefined;
356
- }[];
357
- r2?: {
358
- binding: string;
359
- bucket_name: string;
360
- preview_bucket_name?: string;
361
- }[];
362
- d1Databases?: Environment["d1_databases"];
363
- };
364
-
365
- type StartDevOptions = ArgumentsCamelCase<DevArgs> &
366
- // These options can be passed in directly when called with the `wrangler.dev()` API.
367
- // They aren't exposed as CLI arguments.
368
- AdditionalDevProps & {
369
- forceLocal?: boolean;
370
- enablePagesAssetsServiceBinding?: EnablePagesAssetsServiceBindingOptions;
371
- };
372
-
373
- export async function startDev(args: StartDevOptions) {
374
- let watcher: ReturnType<typeof watch> | undefined;
375
- let rerender: (node: React.ReactNode) => void | undefined;
376
- try {
377
- if (args.logLevel) {
378
- logger.loggerLevel = args.logLevel;
379
- }
380
- await printWranglerBanner();
381
-
382
- if (args.local && process.platform !== "win32") {
383
- logger.info(
384
- chalk.magenta(
385
- `Want to try out the next version of local mode using the open-source Workers runtime?\nSwitch out --local for ${chalk.bold(
386
- "--experimental-local"
387
- )} and let us know what you think at https://discord.gg/cloudflaredev !`
388
- )
389
- );
390
- }
391
-
392
- const configPath =
393
- (args.config as ConfigPath) ||
394
- ((args.script &&
395
- findWranglerToml(path.dirname(args.script))) as ConfigPath);
396
- let config = readConfig(configPath, args);
397
-
398
- if (config.configPath) {
399
- watcher = watch(config.configPath, {
400
- persistent: true,
401
- }).on("change", async (_event) => {
402
- // TODO: Do we need to handle different `_event` types differently?
403
- // e.g. what if the file is deleted, or added?
404
- config = readConfig(configPath, args);
405
- if (config.configPath) {
406
- logger.log(`${path.basename(config.configPath)} changed...`);
407
- rerender(await getDevReactElement(config));
408
- }
409
- });
410
- }
411
-
412
- const {
413
- entry,
414
- nodeCompat,
415
- upstreamProtocol,
416
- zoneId,
417
- host,
418
- routes,
419
- getLocalPort,
420
- getInspectorPort,
421
- cliDefines,
422
- localPersistencePath,
423
- } = await validateDevServerSettings(args, config);
424
-
425
- await metrics.sendMetricsEvent(
426
- "run dev",
427
- {
428
- local: args.local,
429
- usesTypeScript: /\.tsx?$/.test(entry.file),
430
- },
431
- { sendMetrics: config.send_metrics, offline: args.local }
432
- );
433
-
434
- // eslint-disable-next-line no-inner-declarations
435
- async function getDevReactElement(configParam: Config) {
436
- const { assetPaths, bindings } = await getBindingsAndAssetPaths(
437
- args,
438
- configParam
439
- );
440
-
441
- return (
442
- <Dev
443
- name={getScriptName({ name: args.name, env: args.env }, configParam)}
444
- noBundle={!(args.bundle ?? !configParam.no_bundle)}
445
- entry={entry}
446
- env={args.env}
447
- zone={zoneId}
448
- host={host}
449
- routes={routes}
450
- rules={getRules(configParam)}
451
- legacyEnv={isLegacyEnv(configParam)}
452
- minify={args.minify ?? configParam.minify}
453
- nodeCompat={nodeCompat}
454
- build={configParam.build || {}}
455
- define={{ ...configParam.define, ...cliDefines }}
456
- initialMode={
457
- args.local || args.experimentalLocal ? "local" : "remote"
458
- }
459
- jsxFactory={args["jsx-factory"] || configParam.jsx_factory}
460
- jsxFragment={args["jsx-fragment"] || configParam.jsx_fragment}
461
- tsconfig={args.tsconfig ?? configParam.tsconfig}
462
- upstreamProtocol={upstreamProtocol}
463
- localProtocol={args.localProtocol || configParam.dev.local_protocol}
464
- localUpstream={args["local-upstream"] || host}
465
- localPersistencePath={localPersistencePath}
466
- liveReload={args.liveReload || false}
467
- accountId={configParam.account_id || getAccountFromCache()?.id}
468
- assetPaths={assetPaths}
469
- assetsConfig={configParam.assets}
470
- port={args.port || configParam.dev.port || (await getLocalPort())}
471
- ip={args.ip || configParam.dev.ip}
472
- inspectorPort={
473
- args.inspectorPort ||
474
- configParam.dev.inspector_port ||
475
- (await getInspectorPort())
476
- }
477
- isWorkersSite={Boolean(args.site || configParam.site)}
478
- compatibilityDate={getDevCompatibilityDate(
479
- configParam,
480
- args.compatibilityDate
481
- )}
482
- compatibilityFlags={
483
- args.compatibilityFlags || configParam.compatibility_flags
484
- }
485
- usageModel={configParam.usage_model}
486
- bindings={bindings}
487
- crons={configParam.triggers.crons}
488
- queueConsumers={configParam.queues.consumers}
489
- logPrefix={args.logPrefix}
490
- onReady={args.onReady}
491
- inspect={args.inspect ?? true}
492
- showInteractiveDevSession={args.showInteractiveDevSession}
493
- forceLocal={args.forceLocal}
494
- enablePagesAssetsServiceBinding={args.enablePagesAssetsServiceBinding}
495
- firstPartyWorker={configParam.first_party_worker}
496
- sendMetrics={configParam.send_metrics}
497
- testScheduled={args["test-scheduled"]}
498
- experimentalLocal={args.experimentalLocal}
499
- experimentalLocalRemoteKv={args.experimentalLocalRemoteKv}
500
- />
501
- );
502
- }
503
- const devReactElement = render(await getDevReactElement(config));
504
- rerender = devReactElement.rerender;
505
- return {
506
- devReactElement,
507
- watcher,
508
- stop: async () => {
509
- devReactElement.unmount();
510
- await watcher?.close();
511
- },
512
- };
513
- } finally {
514
- await watcher?.close();
515
- }
516
- }
517
-
518
- export async function startApiDev(args: StartDevOptions) {
519
- if (args.logLevel) {
520
- logger.loggerLevel = args.logLevel;
521
- }
522
- await printWranglerBanner();
523
-
524
- const configPath =
525
- (args.config as ConfigPath) ||
526
- ((args.script &&
527
- findWranglerToml(path.dirname(args.script))) as ConfigPath);
528
- const config = readConfig(configPath, args);
529
-
530
- const {
531
- entry,
532
- nodeCompat,
533
- upstreamProtocol,
534
- zoneId,
535
- host,
536
- routes,
537
- getLocalPort,
538
- getInspectorPort,
539
- cliDefines,
540
- localPersistencePath,
541
- } = await validateDevServerSettings(args, config);
542
-
543
- await metrics.sendMetricsEvent(
544
- "run dev (api)",
545
- { local: args.local },
546
- { sendMetrics: config.send_metrics, offline: args.local }
547
- );
548
-
549
- // eslint-disable-next-line no-inner-declarations
550
- async function getDevServer(configParam: Config) {
551
- const { assetPaths, bindings } = await getBindingsAndAssetPaths(
552
- args,
553
- configParam
554
- );
555
-
556
- //if args.bundle is on, don't disable bundling
557
- //if there's no args.bundle, and configParam.no_bundle is on, disable bundling
558
- //otherwise, enable bundling
559
- const enableBundling = args.bundle ?? !configParam.no_bundle;
560
- return await startDevServer({
561
- name: getScriptName({ name: args.name, env: args.env }, configParam),
562
- noBundle: !enableBundling,
563
- entry: entry,
564
- env: args.env,
565
- zone: zoneId,
566
- host: host,
567
- routes: routes,
568
- rules: getRules(configParam),
569
- legacyEnv: isLegacyEnv(configParam),
570
- minify: args.minify ?? configParam.minify,
571
- nodeCompat: nodeCompat,
572
- build: configParam.build || {},
573
- define: { ...config.define, ...cliDefines },
574
- initialMode: args.local ? "local" : "remote",
575
- jsxFactory: args["jsx-factory"] || configParam.jsx_factory,
576
- jsxFragment: args["jsx-fragment"] || configParam.jsx_fragment,
577
- tsconfig: args.tsconfig ?? configParam.tsconfig,
578
- upstreamProtocol: upstreamProtocol,
579
- localProtocol: args.localProtocol || configParam.dev.local_protocol,
580
- localUpstream: args["local-upstream"] || host,
581
- localPersistencePath,
582
- liveReload: args.liveReload || false,
583
- accountId: configParam.account_id || getAccountFromCache()?.id,
584
- assetPaths: assetPaths,
585
- assetsConfig: configParam.assets,
586
- //port can be 0, which means to use a random port
587
- port:
588
- args.port === 0
589
- ? args.port
590
- : args.port || configParam.dev.port || (await getLocalPort()),
591
- ip: args.ip || configParam.dev.ip,
592
- inspectorPort:
593
- args["inspector-port"] ||
594
- configParam.dev.inspector_port ||
595
- (await getInspectorPort()),
596
- isWorkersSite: Boolean(args.site || configParam.site),
597
- compatibilityDate: getDevCompatibilityDate(
598
- config,
599
- // Only `compatibilityDate` will be set when using `unstable_dev`
600
- args["compatibility-date"] ?? args.compatibilityDate
601
- ),
602
- compatibilityFlags:
603
- args["compatibility-flags"] ??
604
- args.compatibilityFlags ??
605
- configParam.compatibility_flags,
606
- usageModel: configParam.usage_model,
607
- bindings: bindings,
608
- crons: configParam.triggers.crons,
609
- queueConsumers: configParam.queues.consumers,
610
- logPrefix: args.logPrefix,
611
- onReady: args.onReady,
612
- inspect: args.inspect ?? true,
613
- showInteractiveDevSession: args.showInteractiveDevSession,
614
- forceLocal: args.forceLocal,
615
- enablePagesAssetsServiceBinding: args.enablePagesAssetsServiceBinding,
616
- local: args.local ?? true,
617
- firstPartyWorker: configParam.first_party_worker,
618
- sendMetrics: configParam.send_metrics,
619
- testScheduled: args.testScheduled,
620
- experimentalLocal: args.experimentalLocal,
621
- experimentalLocalRemoteKv: args.experimentalLocalRemoteKv,
622
- });
623
- }
624
-
625
- const devServer = await getDevServer(config);
626
- if (!devServer) {
627
- throw logger.error("Failed to start dev server.");
628
- }
629
-
630
- return {
631
- stop: async () => {
632
- await devServer.stop();
633
- },
634
- };
635
- }
636
- /**
637
- * Avoiding calling `getPort()` multiple times by memoizing the first result.
638
- */
639
- function memoizeGetPort(defaultPort: number) {
640
- let portValue: number;
641
- return async () => {
642
- return portValue || (portValue = await getPort({ port: defaultPort }));
643
- };
644
- }
645
- /**
646
- * mask anything that was overridden in .dev.vars
647
- * so that we don't log potential secrets into the terminal
648
- */
649
- function maskVars(bindings: CfWorkerInit["bindings"], configParam: Config) {
650
- const maskedVars = { ...bindings.vars };
651
- for (const key of Object.keys(maskedVars)) {
652
- if (maskedVars[key] !== configParam.vars[key]) {
653
- // This means it was overridden in .dev.vars
654
- // so let's mask it
655
- maskedVars[key] = "(hidden)";
656
- }
657
- }
658
- return maskedVars;
659
- }
660
-
661
- async function getZoneIdHostAndRoutes(args: StartDevOptions, config: Config) {
662
- // TODO: if worker_dev = false and no routes, then error (only for dev)
663
- // Compute zone info from the `host` and `route` args and config;
664
- let host = args.host || config.dev.host;
665
- let zoneId: string | undefined;
666
- const routes: Route[] | undefined =
667
- args.routes || (config.route && [config.route]) || config.routes;
668
-
669
- if (args.forceLocal) {
670
- args.local = true;
671
- }
672
-
673
- if (!args.local) {
674
- if (host) {
675
- zoneId = await getZoneIdFromHost(host);
676
- }
677
- if (!zoneId && routes) {
678
- const firstRoute = routes[0];
679
- const zone = await getZoneForRoute(firstRoute);
680
- if (zone) {
681
- zoneId = zone.id;
682
- host = zone.host;
683
- }
684
- }
685
- } else if (!host) {
686
- if (routes) {
687
- const firstRoute = routes[0];
688
- host = getHostFromRoute(firstRoute);
689
- }
690
- }
691
- return { host, routes, zoneId };
692
- }
693
-
694
- async function validateDevServerSettings(
695
- args: StartDevOptions,
696
- config: Config
697
- ) {
698
- const entry = await getEntry(
699
- { assets: args.assets, script: args.script },
700
- config,
701
- "dev"
702
- );
703
-
704
- const { zoneId, host, routes } = await getZoneIdHostAndRoutes(args, config);
705
- const getLocalPort = memoizeGetPort(DEFAULT_LOCAL_PORT);
706
- const getInspectorPort = memoizeGetPort(DEFAULT_INSPECTOR_PORT);
707
-
708
- if (config.services && config.services.length > 0) {
709
- logger.warn(
710
- `This worker is bound to live services: ${config.services
711
- .map(
712
- (service) =>
713
- `${service.binding} (${service.service}${
714
- service.environment ? `@${service.environment}` : ""
715
- })`
716
- )
717
- .join(", ")}`
718
- );
719
- }
720
-
721
- if (args.inspect) {
722
- //devtools are enabled by default, but we still need to disable them if the caller doesn't want them
723
- logger.warn(
724
- "Passing --inspect is unnecessary, now you can always connect to devtools."
725
- );
726
- }
727
-
728
- if (args["experimental-public"]) {
729
- throw new Error(
730
- "The --experimental-public field has been renamed to --assets"
731
- );
732
- }
733
-
734
- if (args.public) {
735
- throw new Error("The --public field has been renamed to --assets");
736
- }
737
-
738
- if ((args.assets || config.assets) && (args.site || config.site)) {
739
- throw new Error("Cannot use Assets and Workers Sites in the same Worker.");
740
- }
741
-
742
- if (args.assets) {
743
- logger.warn(
744
- "The --assets argument is experimental and may change or break at any time"
745
- );
746
- }
747
- const upstreamProtocol =
748
- args["upstream-protocol"] || config.dev.upstream_protocol;
749
- if (upstreamProtocol === "http") {
750
- logger.warn(
751
- "Setting upstream-protocol to http is not currently implemented.\n" +
752
- "If this is required in your project, please add your use case to the following issue:\n" +
753
- "https://github.com/cloudflare/wrangler2/issues/583."
754
- );
755
- }
756
- const nodeCompat = args.nodeCompat ?? config.node_compat;
757
- if (nodeCompat) {
758
- logger.warn(
759
- "Enabling node.js compatibility mode for built-ins and globals. This is experimental and has serious tradeoffs. Please see https://github.com/ionic-team/rollup-plugin-node-polyfills/ for more details."
760
- );
761
- }
762
-
763
- if (args.experimentalEnableLocalPersistence) {
764
- logger.warn(
765
- `--experimental-enable-local-persistence is deprecated.\n` +
766
- `Move any existing data to .wrangler/state and use --persist, or\n` +
767
- `use --persist-to=./wrangler-local-state to keep using the old path.`
768
- );
769
- }
770
-
771
- const localPersistencePath = getLocalPersistencePath(
772
- args.persistTo,
773
- Boolean(args.persist),
774
- config.configPath
775
- );
776
-
777
- const cliDefines = collectKeyValues(args.define);
778
-
779
- return {
780
- entry,
781
- upstreamProtocol,
782
- nodeCompat,
783
- getLocalPort,
784
- getInspectorPort,
785
- zoneId,
786
- host,
787
- routes,
788
- cliDefines,
789
- localPersistencePath,
790
- };
791
- }
792
-
793
- async function getBindingsAndAssetPaths(
794
- args: StartDevOptions,
795
- configParam: Config
796
- ) {
797
- const cliVars = collectKeyValues(args.var);
798
-
799
- // now log all available bindings into the terminal
800
- const bindings = await getBindings(configParam, args.env, {
801
- kv: args.kv,
802
- vars: { ...args.vars, ...cliVars },
803
- durableObjects: args.durableObjects,
804
- r2: args.r2,
805
- d1Databases: args.d1Databases,
806
- });
807
-
808
- const maskedVars = maskVars(bindings, configParam);
809
-
810
- printBindings({
811
- ...bindings,
812
- vars: maskedVars,
813
- });
814
-
815
- const assetPaths =
816
- args.assets || configParam.assets
817
- ? getAssetPaths(configParam, args.assets)
818
- : getSiteAssetPaths(
819
- configParam,
820
- args.site,
821
- args.siteInclude,
822
- args.siteExclude
823
- );
824
- return { assetPaths, bindings };
825
- }
826
-
827
- async function getBindings(
828
- configParam: Config,
829
- env: string | undefined,
830
- args: AdditionalDevProps
831
- ): Promise<CfWorkerInit["bindings"]> {
832
- const bindings = {
833
- kv_namespaces: [
834
- ...(configParam.kv_namespaces || []).map(
835
- ({ binding, preview_id, id: _id }) => {
836
- // In `dev`, we make folks use a separate kv namespace called
837
- // `preview_id` instead of `id` so that they don't
838
- // break production data. So here we check that a `preview_id`
839
- // has actually been configured.
840
- // This whole block of code will be obsoleted in the future
841
- // when we have copy-on-write for previews on edge workers.
842
- if (!preview_id) {
843
- // TODO: This error has to be a _lot_ better, ideally just asking
844
- // to create a preview namespace for the user automatically
845
- throw new Error(
846
- `In development, you should use a separate kv namespace than the one you'd use in production. Please create a new kv namespace with "wrangler kv:namespace create <name> --preview" and add its id as preview_id to the kv_namespace "${binding}" in your wrangler.toml`
847
- ); // Ugh, I really don't like this message very much
848
- }
849
- return {
850
- binding,
851
- id: preview_id,
852
- };
853
- }
854
- ),
855
- ...(args.kv || []),
856
- ],
857
- // Use a copy of combinedVars since we're modifying it later
858
- vars: {
859
- ...getVarsForDev(configParam, env),
860
- ...args.vars,
861
- },
862
- wasm_modules: configParam.wasm_modules,
863
- text_blobs: configParam.text_blobs,
864
- data_blobs: configParam.data_blobs,
865
- durable_objects: {
866
- bindings: [
867
- ...(configParam.durable_objects || { bindings: [] }).bindings,
868
- ...(args.durableObjects || []),
869
- ],
870
- },
871
- queues: [
872
- ...(configParam.queues.producers || []).map((queue) => {
873
- return { binding: queue.binding, queue_name: queue.queue };
874
- }),
875
- ],
876
- r2_buckets: [
877
- ...(configParam.r2_buckets?.map(
878
- ({ binding, preview_bucket_name, bucket_name: _bucket_name }) => {
879
- // same idea as kv namespace preview id,
880
- // same copy-on-write TODO
881
- if (!preview_bucket_name) {
882
- throw new Error(
883
- `In development, you should use a separate r2 bucket than the one you'd use in production. Please create a new r2 bucket with "wrangler r2 bucket create <name>" and add its name as preview_bucket_name to the r2_buckets "${binding}" in your wrangler.toml`
884
- );
885
- }
886
- return {
887
- binding,
888
- bucket_name: preview_bucket_name,
889
- };
890
- }
891
- ) || []),
892
- ...(args.r2 || []),
893
- ],
894
- dispatch_namespaces: configParam.dispatch_namespaces,
895
- services: configParam.services,
896
- unsafe: configParam.unsafe?.bindings,
897
- logfwdr: configParam.logfwdr,
898
- d1_databases: identifyD1BindingsAsBeta([
899
- ...configParam.d1_databases,
900
- ...(args.d1Databases || []),
901
- ]),
902
- };
903
-
904
- return bindings;
905
- }