wrangler 0.0.0-afdb7e49 → 0.0.0-b032bf2e

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