wrangler 0.0.0-e3e3243 → 0.0.0-e4047cab

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 (310) hide show
  1. package/Cloudflare_CA.pem +18 -0
  2. package/README.md +47 -14
  3. package/bin/wrangler.js +166 -31
  4. package/kv-asset-handler.js +1 -0
  5. package/miniflare-dist/index.mjs +6263 -21
  6. package/package.json +191 -141
  7. package/src/__tests__/access.test.ts +25 -0
  8. package/src/__tests__/api-dev.test.ts +209 -0
  9. package/src/__tests__/api-devregistry.test.js +56 -0
  10. package/src/__tests__/api.test.ts +80 -0
  11. package/src/__tests__/config-cache-without-cache-dir.test.ts +38 -0
  12. package/src/__tests__/config-cache.test.ts +42 -0
  13. package/src/__tests__/configuration.test.ts +3971 -2810
  14. package/src/__tests__/d1.test.ts +80 -0
  15. package/src/__tests__/delete.test.ts +263 -0
  16. package/src/__tests__/deployments.test.ts +91 -0
  17. package/src/__tests__/dev.test.tsx +1521 -526
  18. package/src/__tests__/dialogs.test.tsx +40 -0
  19. package/src/__tests__/generate.test.ts +235 -0
  20. package/src/__tests__/get-host-from-url.test.ts +16 -0
  21. package/src/__tests__/guess-worker-format.test.ts +74 -57
  22. package/src/__tests__/helpers/cmd-shim.d.ts +6 -6
  23. package/src/__tests__/helpers/mock-account-id.ts +24 -24
  24. package/src/__tests__/helpers/mock-bin.ts +20 -20
  25. package/src/__tests__/helpers/mock-cfetch.ts +219 -93
  26. package/src/__tests__/helpers/mock-console.ts +68 -32
  27. package/src/__tests__/helpers/mock-dialogs.ts +121 -33
  28. package/src/__tests__/helpers/mock-get-zone-from-host.ts +8 -0
  29. package/src/__tests__/helpers/mock-http-server.ts +46 -0
  30. package/src/__tests__/helpers/mock-istty.ts +74 -0
  31. package/src/__tests__/helpers/mock-known-routes.ts +7 -0
  32. package/src/__tests__/helpers/mock-kv.ts +27 -27
  33. package/src/__tests__/helpers/mock-oauth-flow.ts +256 -0
  34. package/src/__tests__/helpers/mock-process.ts +39 -0
  35. package/src/__tests__/helpers/mock-stdin.ts +82 -77
  36. package/src/__tests__/helpers/mock-web-socket.ts +21 -21
  37. package/src/__tests__/helpers/msw/handlers/access.ts +13 -0
  38. package/src/__tests__/helpers/msw/handlers/deployments.ts +59 -0
  39. package/src/__tests__/helpers/msw/handlers/namespaces.ts +104 -0
  40. package/src/__tests__/helpers/msw/handlers/oauth.ts +31 -0
  41. package/src/__tests__/helpers/msw/handlers/r2.ts +80 -0
  42. package/src/__tests__/helpers/msw/handlers/script.ts +75 -0
  43. package/src/__tests__/helpers/msw/handlers/user.ts +63 -0
  44. package/src/__tests__/helpers/msw/handlers/zones.ts +22 -0
  45. package/src/__tests__/helpers/msw/index.ts +21 -0
  46. package/src/__tests__/helpers/run-in-tmp.ts +28 -22
  47. package/src/__tests__/helpers/run-wrangler.ts +8 -8
  48. package/src/__tests__/helpers/worker-scripts/child-wrangler.toml +1 -0
  49. package/src/__tests__/helpers/worker-scripts/hello-world-worker.js +5 -0
  50. package/src/__tests__/helpers/worker-scripts/hello-world-wrangler.toml +1 -0
  51. package/src/__tests__/helpers/worker-scripts/parent-worker.js +8 -0
  52. package/src/__tests__/helpers/worker-scripts/parent-wrangler.toml +5 -0
  53. package/src/__tests__/helpers/write-worker-source.ts +31 -0
  54. package/src/__tests__/helpers/write-wrangler-toml.ts +9 -9
  55. package/src/__tests__/https-options.test.ts +144 -108
  56. package/src/__tests__/index.test.ts +248 -967
  57. package/src/__tests__/init.test.ts +2900 -0
  58. package/src/__tests__/jest.setup.ts +167 -34
  59. package/src/__tests__/kv.test.ts +1661 -1366
  60. package/src/__tests__/logger.test.ts +141 -0
  61. package/src/__tests__/logout.test.ts +36 -41
  62. package/src/__tests__/metrics.test.ts +450 -0
  63. package/src/__tests__/middleware.scheduled.test.ts +135 -0
  64. package/src/__tests__/middleware.test.ts +726 -0
  65. package/src/__tests__/package-manager.test.ts +312 -165
  66. package/src/__tests__/pages-deployment-tail.test.ts +820 -0
  67. package/src/__tests__/pages.test.ts +2437 -77
  68. package/src/__tests__/parse.test.ts +253 -245
  69. package/src/__tests__/paths.test.ts +39 -0
  70. package/src/__tests__/publish.test.ts +7569 -3445
  71. package/src/__tests__/pubsub.test.ts +370 -0
  72. package/src/__tests__/queues.test.ts +522 -0
  73. package/src/__tests__/r2.test.ts +388 -193
  74. package/src/__tests__/route.test.ts +22 -22
  75. package/src/__tests__/secret.test.ts +626 -407
  76. package/src/__tests__/tail.test.ts +779 -442
  77. package/src/__tests__/test-old-node-version.js +31 -0
  78. package/src/__tests__/type-generation.test.ts +226 -0
  79. package/src/__tests__/user.test.ts +129 -0
  80. package/src/__tests__/utils-collectKeyValues.test.ts +47 -0
  81. package/src/__tests__/validate-dev-props.test.ts +56 -0
  82. package/src/__tests__/version.test.ts +35 -0
  83. package/src/__tests__/whoami.test.tsx +266 -88
  84. package/src/__tests__/worker-namespace.test.ts +326 -0
  85. package/src/abort.d.ts +3 -0
  86. package/src/api/dev.ts +210 -0
  87. package/src/api/index.ts +2 -0
  88. package/src/bundle-reporter.tsx +68 -0
  89. package/src/bundle.ts +791 -123
  90. package/src/cfetch/index.ts +123 -67
  91. package/src/cfetch/internal.ts +216 -92
  92. package/src/cli.ts +23 -14
  93. package/src/config/README.md +1 -1
  94. package/src/config/config.ts +236 -179
  95. package/src/config/diagnostics.ts +61 -61
  96. package/src/config/environment.ts +506 -316
  97. package/src/config/index.ts +310 -38
  98. package/src/config/validation-helpers.ts +385 -342
  99. package/src/config/validation.ts +1970 -1099
  100. package/src/config-cache.ts +85 -0
  101. package/src/create-worker-preview.ts +240 -124
  102. package/src/create-worker-upload-form.ts +300 -201
  103. package/src/d1/backups.tsx +223 -0
  104. package/src/d1/constants.ts +2 -0
  105. package/src/d1/create.tsx +63 -0
  106. package/src/d1/delete.tsx +56 -0
  107. package/src/d1/execute.tsx +352 -0
  108. package/src/d1/formatTimeAgo.ts +14 -0
  109. package/src/d1/index.ts +101 -0
  110. package/src/d1/list.tsx +48 -0
  111. package/src/d1/migrations/apply.tsx +184 -0
  112. package/src/d1/migrations/create.tsx +77 -0
  113. package/src/d1/migrations/helpers.ts +147 -0
  114. package/src/d1/migrations/index.tsx +3 -0
  115. package/src/d1/migrations/list.tsx +79 -0
  116. package/src/d1/migrations/options.ts +18 -0
  117. package/src/d1/options.ts +22 -0
  118. package/src/d1/types.tsx +24 -0
  119. package/src/d1/utils.ts +48 -0
  120. package/src/delete.ts +108 -0
  121. package/src/deployments.ts +77 -0
  122. package/src/deprecated/index.ts +438 -0
  123. package/src/dev/dev-vars.ts +39 -0
  124. package/src/dev/dev.tsx +550 -328
  125. package/src/dev/get-local-persistence-path.tsx +31 -0
  126. package/src/dev/local.tsx +871 -213
  127. package/src/dev/remote.tsx +592 -192
  128. package/src/dev/start-server.ts +539 -0
  129. package/src/dev/use-esbuild.ts +184 -84
  130. package/src/dev/validate-dev-props.ts +31 -0
  131. package/src/dev-registry.tsx +201 -0
  132. package/src/dev.tsx +904 -0
  133. package/src/dialogs.tsx +150 -59
  134. package/src/dispatch-namespace.ts +207 -0
  135. package/src/durable.ts +102 -0
  136. package/src/entry.ts +260 -171
  137. package/src/environment-variables.ts +40 -23
  138. package/src/errors.ts +11 -0
  139. package/src/git-client.ts +124 -0
  140. package/src/global-wrangler-config-path.ts +26 -0
  141. package/src/https-options.ts +85 -80
  142. package/src/index.tsx +639 -2341
  143. package/src/init.ts +970 -0
  144. package/src/inspect.ts +707 -557
  145. package/src/intl-polyfill.d.ts +123 -123
  146. package/src/is-ci.ts +14 -0
  147. package/src/is-interactive.ts +16 -0
  148. package/src/kv/helpers.ts +430 -0
  149. package/src/kv/index.ts +596 -0
  150. package/src/logger.ts +92 -0
  151. package/src/metrics/index.ts +5 -0
  152. package/src/metrics/metrics-config.ts +239 -0
  153. package/src/metrics/metrics-dispatcher.ts +96 -0
  154. package/src/metrics/metrics-usage-headers.ts +24 -0
  155. package/src/metrics/send-event.ts +96 -0
  156. package/src/miniflare-cli/assets.ts +220 -0
  157. package/src/miniflare-cli/index.ts +206 -30
  158. package/src/miniflare-cli/request-context.ts +40 -0
  159. package/src/module-collection.ts +222 -213
  160. package/src/open-in-browser.ts +8 -16
  161. package/src/package-manager.ts +183 -90
  162. package/src/pages/build.tsx +270 -0
  163. package/src/pages/constants.ts +18 -0
  164. package/src/pages/deployment-tails.tsx +284 -0
  165. package/src/pages/deployments.tsx +84 -0
  166. package/src/pages/dev.tsx +706 -0
  167. package/src/pages/errors.ts +66 -0
  168. package/src/pages/functions/buildPlugin.ts +107 -0
  169. package/src/pages/functions/buildWorker.ts +166 -0
  170. package/src/pages/functions/filepath-routing.test.ts +234 -0
  171. package/src/pages/functions/filepath-routing.ts +189 -0
  172. package/src/pages/functions/identifiers.ts +78 -0
  173. package/src/pages/functions/routes-consolidation.test.ts +250 -0
  174. package/src/pages/functions/routes-consolidation.ts +73 -0
  175. package/src/pages/functions/routes-transformation.test.ts +282 -0
  176. package/src/pages/functions/routes-transformation.ts +115 -0
  177. package/src/pages/functions/routes-validation.test.ts +403 -0
  178. package/src/pages/functions/routes-validation.ts +202 -0
  179. package/src/pages/functions/routes.ts +151 -0
  180. package/src/pages/functions.tsx +82 -0
  181. package/src/pages/hash.tsx +13 -0
  182. package/src/pages/index.tsx +102 -0
  183. package/src/pages/projects.tsx +169 -0
  184. package/src/pages/prompt-select-project.tsx +31 -0
  185. package/src/pages/publish.tsx +451 -0
  186. package/src/pages/types.ts +41 -0
  187. package/src/pages/upload.tsx +426 -0
  188. package/src/pages/utils.ts +23 -0
  189. package/src/parse.ts +224 -121
  190. package/src/paths.ts +53 -8
  191. package/src/proxy.ts +629 -375
  192. package/src/publish/index.ts +270 -0
  193. package/src/publish/publish.ts +1035 -0
  194. package/src/pubsub/index.ts +286 -0
  195. package/src/pubsub/pubsub-commands.tsx +624 -0
  196. package/src/queues/cli/commands/consumer/add.ts +71 -0
  197. package/src/queues/cli/commands/consumer/index.ts +22 -0
  198. package/src/queues/cli/commands/consumer/remove.ts +38 -0
  199. package/src/queues/cli/commands/create.ts +25 -0
  200. package/src/queues/cli/commands/delete.ts +26 -0
  201. package/src/queues/cli/commands/index.ts +36 -0
  202. package/src/queues/cli/commands/list.ts +25 -0
  203. package/src/queues/client.ts +135 -0
  204. package/src/queues/utils.ts +18 -0
  205. package/src/r2/constants.ts +4 -0
  206. package/src/r2/helpers.ts +131 -0
  207. package/src/r2/index.ts +289 -0
  208. package/src/routes.ts +140 -0
  209. package/src/secret/index.ts +378 -0
  210. package/src/selfsigned.d.ts +23 -23
  211. package/src/sites.tsx +283 -191
  212. package/src/tail/createTail.ts +379 -0
  213. package/src/tail/filters.ts +108 -110
  214. package/src/tail/index.ts +211 -296
  215. package/src/tail/printing.ts +92 -47
  216. package/src/type-generation.ts +173 -0
  217. package/src/update-check.ts +19 -0
  218. package/src/user/access.ts +69 -0
  219. package/src/user/choose-account.tsx +69 -0
  220. package/src/user/env-vars.ts +46 -0
  221. package/src/user/generate-auth-url.ts +33 -0
  222. package/src/user/generate-random-state.ts +16 -0
  223. package/src/user/index.ts +3 -0
  224. package/src/user/user.tsx +1198 -0
  225. package/src/utils/collectKeyValues.ts +14 -0
  226. package/src/whoami.tsx +142 -42
  227. package/src/worker.ts +182 -100
  228. package/src/yargs-types.ts +24 -0
  229. package/src/zones.ts +191 -0
  230. package/templates/__tests__/pages-dev-util.test.ts +128 -0
  231. package/templates/checked-fetch.js +17 -0
  232. package/templates/d1-beta-facade.js +205 -0
  233. package/templates/first-party-worker-module-facade.ts +18 -0
  234. package/templates/format-dev-errors.ts +32 -0
  235. package/templates/gitignore +171 -0
  236. package/templates/init-tests/test-jest-new-worker.js +25 -0
  237. package/templates/init-tests/test-vitest-new-worker.js +26 -0
  238. package/templates/middleware/common.ts +62 -0
  239. package/templates/middleware/loader-modules.ts +84 -0
  240. package/templates/middleware/loader-sw.ts +227 -0
  241. package/templates/middleware/middleware-miniflare3-json-error.ts +20 -0
  242. package/templates/middleware/middleware-pretty-error.ts +40 -0
  243. package/templates/middleware/middleware-scheduled.ts +15 -0
  244. package/templates/new-worker-scheduled.js +17 -0
  245. package/templates/new-worker-scheduled.ts +32 -0
  246. package/templates/new-worker.js +3 -3
  247. package/templates/new-worker.ts +18 -3
  248. package/templates/no-op-worker.js +10 -0
  249. package/templates/pages-dev-pipeline.ts +28 -0
  250. package/templates/pages-dev-util.ts +52 -0
  251. package/templates/pages-shim.ts +9 -0
  252. package/templates/pages-template-plugin.ts +171 -0
  253. package/templates/pages-template-worker.ts +189 -0
  254. package/templates/serve-static-assets.ts +63 -0
  255. package/templates/service-bindings-module-facade.js +54 -0
  256. package/templates/service-bindings-sw-facade.js +42 -0
  257. package/templates/tsconfig.json +96 -95
  258. package/wrangler-dist/cli.d.ts +893 -0
  259. package/wrangler-dist/cli.js +116816 -94969
  260. package/miniflare-dist/index.mjs.map +0 -7
  261. package/pages/functions/buildWorker.ts +0 -62
  262. package/pages/functions/filepath-routing.test.ts +0 -223
  263. package/pages/functions/filepath-routing.ts +0 -181
  264. package/pages/functions/identifiers.ts +0 -78
  265. package/pages/functions/routes.ts +0 -151
  266. package/pages/functions/template-worker.ts +0 -130
  267. package/src/__tests__/helpers/faye-websocket.d.ts +0 -6
  268. package/src/__tests__/sentry.test.ts +0 -146
  269. package/src/kv.ts +0 -319
  270. package/src/miniflare-cli/enum-keys.ts +0 -17
  271. package/src/pages.tsx +0 -1061
  272. package/src/publish.ts +0 -350
  273. package/src/r2.ts +0 -50
  274. package/src/reporting.ts +0 -131
  275. package/src/user.tsx +0 -1127
  276. package/templates/static-asset-facade.js +0 -47
  277. package/vendor/@cloudflare/kv-asset-handler/CHANGELOG.md +0 -332
  278. package/vendor/@cloudflare/kv-asset-handler/LICENSE_APACHE +0 -176
  279. package/vendor/@cloudflare/kv-asset-handler/LICENSE_MIT +0 -25
  280. package/vendor/@cloudflare/kv-asset-handler/README.md +0 -245
  281. package/vendor/@cloudflare/kv-asset-handler/dist/index.d.ts +0 -32
  282. package/vendor/@cloudflare/kv-asset-handler/dist/index.js +0 -354
  283. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.d.ts +0 -13
  284. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.js +0 -148
  285. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.d.ts +0 -1
  286. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.js +0 -436
  287. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.d.ts +0 -1
  288. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.js +0 -40
  289. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.d.ts +0 -1
  290. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.js +0 -42
  291. package/vendor/@cloudflare/kv-asset-handler/dist/types.d.ts +0 -26
  292. package/vendor/@cloudflare/kv-asset-handler/dist/types.js +0 -31
  293. package/vendor/@cloudflare/kv-asset-handler/package.json +0 -52
  294. package/vendor/@cloudflare/kv-asset-handler/src/index.ts +0 -296
  295. package/vendor/@cloudflare/kv-asset-handler/src/mocks.ts +0 -136
  296. package/vendor/@cloudflare/kv-asset-handler/src/test/getAssetFromKV.ts +0 -464
  297. package/vendor/@cloudflare/kv-asset-handler/src/test/mapRequestToAsset.ts +0 -33
  298. package/vendor/@cloudflare/kv-asset-handler/src/test/serveSinglePageApp.ts +0 -42
  299. package/vendor/@cloudflare/kv-asset-handler/src/types.ts +0 -39
  300. package/vendor/wrangler-mime/CHANGELOG.md +0 -289
  301. package/vendor/wrangler-mime/LICENSE +0 -21
  302. package/vendor/wrangler-mime/Mime.js +0 -97
  303. package/vendor/wrangler-mime/README.md +0 -187
  304. package/vendor/wrangler-mime/cli.js +0 -46
  305. package/vendor/wrangler-mime/index.js +0 -4
  306. package/vendor/wrangler-mime/lite.js +0 -4
  307. package/vendor/wrangler-mime/package.json +0 -52
  308. package/vendor/wrangler-mime/types/other.js +0 -1
  309. package/vendor/wrangler-mime/types/standard.js +0 -1
  310. package/wrangler-dist/cli.js.map +0 -7
package/src/dev.tsx ADDED
@@ -0,0 +1,904 @@
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
+ initialPort={
471
+ args.port ?? configParam.dev.port ?? (await getLocalPort())
472
+ }
473
+ initialIp={args.ip || configParam.dev.ip}
474
+ inspectorPort={
475
+ args.inspectorPort ??
476
+ configParam.dev.inspector_port ??
477
+ (await getInspectorPort())
478
+ }
479
+ isWorkersSite={Boolean(args.site || configParam.site)}
480
+ compatibilityDate={getDevCompatibilityDate(
481
+ configParam,
482
+ args.compatibilityDate
483
+ )}
484
+ compatibilityFlags={
485
+ args.compatibilityFlags || configParam.compatibility_flags
486
+ }
487
+ usageModel={configParam.usage_model}
488
+ bindings={bindings}
489
+ crons={configParam.triggers.crons}
490
+ queueConsumers={configParam.queues.consumers}
491
+ logPrefix={args.logPrefix}
492
+ onReady={args.onReady}
493
+ inspect={args.inspect ?? true}
494
+ showInteractiveDevSession={args.showInteractiveDevSession}
495
+ forceLocal={args.forceLocal}
496
+ enablePagesAssetsServiceBinding={args.enablePagesAssetsServiceBinding}
497
+ firstPartyWorker={configParam.first_party_worker}
498
+ sendMetrics={configParam.send_metrics}
499
+ testScheduled={args["test-scheduled"]}
500
+ experimentalLocal={args.experimentalLocal}
501
+ experimentalLocalRemoteKv={args.experimentalLocalRemoteKv}
502
+ />
503
+ );
504
+ }
505
+ const devReactElement = render(await getDevReactElement(config));
506
+ rerender = devReactElement.rerender;
507
+ return {
508
+ devReactElement,
509
+ watcher,
510
+ stop: async () => {
511
+ devReactElement.unmount();
512
+ await watcher?.close();
513
+ },
514
+ };
515
+ } finally {
516
+ await watcher?.close();
517
+ }
518
+ }
519
+
520
+ export async function startApiDev(args: StartDevOptions) {
521
+ if (args.logLevel) {
522
+ logger.loggerLevel = args.logLevel;
523
+ }
524
+ await printWranglerBanner();
525
+
526
+ const configPath =
527
+ (args.config as ConfigPath) ||
528
+ ((args.script &&
529
+ findWranglerToml(path.dirname(args.script))) as ConfigPath);
530
+ const config = readConfig(configPath, args);
531
+
532
+ const {
533
+ entry,
534
+ nodeCompat,
535
+ upstreamProtocol,
536
+ zoneId,
537
+ host,
538
+ routes,
539
+ getLocalPort,
540
+ getInspectorPort,
541
+ cliDefines,
542
+ localPersistencePath,
543
+ } = await validateDevServerSettings(args, config);
544
+
545
+ await metrics.sendMetricsEvent(
546
+ "run dev (api)",
547
+ { local: args.local },
548
+ { sendMetrics: config.send_metrics, offline: args.local }
549
+ );
550
+
551
+ // eslint-disable-next-line no-inner-declarations
552
+ async function getDevServer(configParam: Config) {
553
+ const { assetPaths, bindings } = await getBindingsAndAssetPaths(
554
+ args,
555
+ configParam
556
+ );
557
+
558
+ //if args.bundle is on, don't disable bundling
559
+ //if there's no args.bundle, and configParam.no_bundle is on, disable bundling
560
+ //otherwise, enable bundling
561
+ const enableBundling = args.bundle ?? !configParam.no_bundle;
562
+ return await startDevServer({
563
+ name: getScriptName({ name: args.name, env: args.env }, configParam),
564
+ noBundle: !enableBundling,
565
+ entry: entry,
566
+ env: args.env,
567
+ zone: zoneId,
568
+ host: host,
569
+ routes: routes,
570
+ rules: getRules(configParam),
571
+ legacyEnv: isLegacyEnv(configParam),
572
+ minify: args.minify ?? configParam.minify,
573
+ nodeCompat: nodeCompat,
574
+ build: configParam.build || {},
575
+ define: { ...config.define, ...cliDefines },
576
+ initialMode: args.local ? "local" : "remote",
577
+ jsxFactory: args["jsx-factory"] || configParam.jsx_factory,
578
+ jsxFragment: args["jsx-fragment"] || configParam.jsx_fragment,
579
+ tsconfig: args.tsconfig ?? configParam.tsconfig,
580
+ upstreamProtocol: upstreamProtocol,
581
+ localProtocol: args.localProtocol || configParam.dev.local_protocol,
582
+ localUpstream: args["local-upstream"] || host,
583
+ localPersistencePath,
584
+ liveReload: args.liveReload || false,
585
+ accountId: configParam.account_id || getAccountFromCache()?.id,
586
+ assetPaths: assetPaths,
587
+ assetsConfig: configParam.assets,
588
+ //port can be 0, which means to use a random port
589
+ initialPort: args.port ?? configParam.dev.port ?? (await getLocalPort()),
590
+ initialIp: args.ip || configParam.dev.ip,
591
+ inspectorPort:
592
+ args["inspector-port"] ??
593
+ configParam.dev.inspector_port ??
594
+ (await getInspectorPort()),
595
+ isWorkersSite: Boolean(args.site || configParam.site),
596
+ compatibilityDate: getDevCompatibilityDate(
597
+ config,
598
+ // Only `compatibilityDate` will be set when using `unstable_dev`
599
+ args["compatibility-date"] ?? args.compatibilityDate
600
+ ),
601
+ compatibilityFlags:
602
+ args["compatibility-flags"] ??
603
+ args.compatibilityFlags ??
604
+ configParam.compatibility_flags,
605
+ usageModel: configParam.usage_model,
606
+ bindings: bindings,
607
+ crons: configParam.triggers.crons,
608
+ queueConsumers: configParam.queues.consumers,
609
+ logPrefix: args.logPrefix,
610
+ onReady: args.onReady,
611
+ inspect: args.inspect ?? true,
612
+ showInteractiveDevSession: args.showInteractiveDevSession,
613
+ forceLocal: args.forceLocal,
614
+ enablePagesAssetsServiceBinding: args.enablePagesAssetsServiceBinding,
615
+ local: args.local ?? true,
616
+ firstPartyWorker: configParam.first_party_worker,
617
+ sendMetrics: configParam.send_metrics,
618
+ testScheduled: args.testScheduled,
619
+ experimentalLocal: args.experimentalLocal,
620
+ experimentalLocalRemoteKv: args.experimentalLocalRemoteKv,
621
+ });
622
+ }
623
+
624
+ const devServer = await getDevServer(config);
625
+ if (!devServer) {
626
+ throw logger.error("Failed to start dev server.");
627
+ }
628
+
629
+ return {
630
+ stop: async () => {
631
+ await devServer.stop();
632
+ },
633
+ };
634
+ }
635
+ /**
636
+ * Avoiding calling `getPort()` multiple times by memoizing the first result.
637
+ */
638
+ function memoizeGetPort(defaultPort: number) {
639
+ let portValue: number;
640
+ return async () => {
641
+ return portValue || (portValue = await getPort({ port: defaultPort }));
642
+ };
643
+ }
644
+ /**
645
+ * mask anything that was overridden in .dev.vars
646
+ * so that we don't log potential secrets into the terminal
647
+ */
648
+ function maskVars(bindings: CfWorkerInit["bindings"], configParam: Config) {
649
+ const maskedVars = { ...bindings.vars };
650
+ for (const key of Object.keys(maskedVars)) {
651
+ if (maskedVars[key] !== configParam.vars[key]) {
652
+ // This means it was overridden in .dev.vars
653
+ // so let's mask it
654
+ maskedVars[key] = "(hidden)";
655
+ }
656
+ }
657
+ return maskedVars;
658
+ }
659
+
660
+ async function getZoneIdHostAndRoutes(args: StartDevOptions, config: Config) {
661
+ // TODO: if worker_dev = false and no routes, then error (only for dev)
662
+ // Compute zone info from the `host` and `route` args and config;
663
+ let host = args.host || config.dev.host;
664
+ let zoneId: string | undefined;
665
+ const routes: Route[] | undefined =
666
+ args.routes || (config.route && [config.route]) || config.routes;
667
+
668
+ if (args.forceLocal) {
669
+ args.local = true;
670
+ }
671
+
672
+ if (!args.local) {
673
+ if (host) {
674
+ zoneId = await getZoneIdFromHost(host);
675
+ }
676
+ if (!zoneId && routes) {
677
+ const firstRoute = routes[0];
678
+ const zone = await getZoneForRoute(firstRoute);
679
+ if (zone) {
680
+ zoneId = zone.id;
681
+ host = zone.host;
682
+ }
683
+ }
684
+ } else if (!host) {
685
+ if (routes) {
686
+ const firstRoute = routes[0];
687
+ host = getHostFromRoute(firstRoute);
688
+ }
689
+ }
690
+ return { host, routes, zoneId };
691
+ }
692
+
693
+ async function validateDevServerSettings(
694
+ args: StartDevOptions,
695
+ config: Config
696
+ ) {
697
+ const entry = await getEntry(
698
+ { assets: args.assets, script: args.script },
699
+ config,
700
+ "dev"
701
+ );
702
+
703
+ const { zoneId, host, routes } = await getZoneIdHostAndRoutes(args, config);
704
+ const getLocalPort = memoizeGetPort(DEFAULT_LOCAL_PORT);
705
+ const getInspectorPort = memoizeGetPort(DEFAULT_INSPECTOR_PORT);
706
+
707
+ if (config.services && config.services.length > 0) {
708
+ logger.warn(
709
+ `This worker is bound to live services: ${config.services
710
+ .map(
711
+ (service) =>
712
+ `${service.binding} (${service.service}${
713
+ service.environment ? `@${service.environment}` : ""
714
+ })`
715
+ )
716
+ .join(", ")}`
717
+ );
718
+ }
719
+
720
+ if (args.inspect) {
721
+ //devtools are enabled by default, but we still need to disable them if the caller doesn't want them
722
+ logger.warn(
723
+ "Passing --inspect is unnecessary, now you can always connect to devtools."
724
+ );
725
+ }
726
+
727
+ if (args["experimental-public"]) {
728
+ throw new Error(
729
+ "The --experimental-public field has been renamed to --assets"
730
+ );
731
+ }
732
+
733
+ if (args.public) {
734
+ throw new Error("The --public field has been renamed to --assets");
735
+ }
736
+
737
+ if ((args.assets || config.assets) && (args.site || config.site)) {
738
+ throw new Error("Cannot use Assets and Workers Sites in the same Worker.");
739
+ }
740
+
741
+ if (args.assets) {
742
+ logger.warn(
743
+ "The --assets argument is experimental and may change or break at any time"
744
+ );
745
+ }
746
+ const upstreamProtocol =
747
+ args["upstream-protocol"] || config.dev.upstream_protocol;
748
+ if (upstreamProtocol === "http") {
749
+ logger.warn(
750
+ "Setting upstream-protocol to http is not currently implemented.\n" +
751
+ "If this is required in your project, please add your use case to the following issue:\n" +
752
+ "https://github.com/cloudflare/wrangler2/issues/583."
753
+ );
754
+ }
755
+ const nodeCompat = args.nodeCompat ?? config.node_compat;
756
+ if (nodeCompat) {
757
+ logger.warn(
758
+ "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."
759
+ );
760
+ }
761
+
762
+ if (args.experimentalEnableLocalPersistence) {
763
+ logger.warn(
764
+ `--experimental-enable-local-persistence is deprecated.\n` +
765
+ `Move any existing data to .wrangler/state and use --persist, or\n` +
766
+ `use --persist-to=./wrangler-local-state to keep using the old path.`
767
+ );
768
+ }
769
+
770
+ const localPersistencePath = getLocalPersistencePath(
771
+ args.persistTo,
772
+ Boolean(args.persist),
773
+ config.configPath
774
+ );
775
+
776
+ const cliDefines = collectKeyValues(args.define);
777
+
778
+ return {
779
+ entry,
780
+ upstreamProtocol,
781
+ nodeCompat,
782
+ getLocalPort,
783
+ getInspectorPort,
784
+ zoneId,
785
+ host,
786
+ routes,
787
+ cliDefines,
788
+ localPersistencePath,
789
+ };
790
+ }
791
+
792
+ async function getBindingsAndAssetPaths(
793
+ args: StartDevOptions,
794
+ configParam: Config
795
+ ) {
796
+ const cliVars = collectKeyValues(args.var);
797
+
798
+ // now log all available bindings into the terminal
799
+ const bindings = await getBindings(configParam, args.env, {
800
+ kv: args.kv,
801
+ vars: { ...args.vars, ...cliVars },
802
+ durableObjects: args.durableObjects,
803
+ r2: args.r2,
804
+ d1Databases: args.d1Databases,
805
+ });
806
+
807
+ const maskedVars = maskVars(bindings, configParam);
808
+
809
+ printBindings({
810
+ ...bindings,
811
+ vars: maskedVars,
812
+ });
813
+
814
+ const assetPaths =
815
+ args.assets || configParam.assets
816
+ ? getAssetPaths(configParam, args.assets)
817
+ : getSiteAssetPaths(
818
+ configParam,
819
+ args.site,
820
+ args.siteInclude,
821
+ args.siteExclude
822
+ );
823
+ return { assetPaths, bindings };
824
+ }
825
+
826
+ async function getBindings(
827
+ configParam: Config,
828
+ env: string | undefined,
829
+ args: AdditionalDevProps
830
+ ): Promise<CfWorkerInit["bindings"]> {
831
+ const bindings = {
832
+ kv_namespaces: [
833
+ ...(configParam.kv_namespaces || []).map(
834
+ ({ binding, preview_id, id: _id }) => {
835
+ // In `dev`, we make folks use a separate kv namespace called
836
+ // `preview_id` instead of `id` so that they don't
837
+ // break production data. So here we check that a `preview_id`
838
+ // has actually been configured.
839
+ // This whole block of code will be obsoleted in the future
840
+ // when we have copy-on-write for previews on edge workers.
841
+ if (!preview_id) {
842
+ // TODO: This error has to be a _lot_ better, ideally just asking
843
+ // to create a preview namespace for the user automatically
844
+ throw new Error(
845
+ `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`
846
+ ); // Ugh, I really don't like this message very much
847
+ }
848
+ return {
849
+ binding,
850
+ id: preview_id,
851
+ };
852
+ }
853
+ ),
854
+ ...(args.kv || []),
855
+ ],
856
+ // Use a copy of combinedVars since we're modifying it later
857
+ vars: {
858
+ ...getVarsForDev(configParam, env),
859
+ ...args.vars,
860
+ },
861
+ wasm_modules: configParam.wasm_modules,
862
+ text_blobs: configParam.text_blobs,
863
+ data_blobs: configParam.data_blobs,
864
+ durable_objects: {
865
+ bindings: [
866
+ ...(configParam.durable_objects || { bindings: [] }).bindings,
867
+ ...(args.durableObjects || []),
868
+ ],
869
+ },
870
+ queues: [
871
+ ...(configParam.queues.producers || []).map((queue) => {
872
+ return { binding: queue.binding, queue_name: queue.queue };
873
+ }),
874
+ ],
875
+ r2_buckets: [
876
+ ...(configParam.r2_buckets?.map(
877
+ ({ binding, preview_bucket_name, bucket_name: _bucket_name }) => {
878
+ // same idea as kv namespace preview id,
879
+ // same copy-on-write TODO
880
+ if (!preview_bucket_name) {
881
+ throw new Error(
882
+ `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`
883
+ );
884
+ }
885
+ return {
886
+ binding,
887
+ bucket_name: preview_bucket_name,
888
+ };
889
+ }
890
+ ) || []),
891
+ ...(args.r2 || []),
892
+ ],
893
+ dispatch_namespaces: configParam.dispatch_namespaces,
894
+ services: configParam.services,
895
+ unsafe: configParam.unsafe?.bindings,
896
+ logfwdr: configParam.logfwdr,
897
+ d1_databases: identifyD1BindingsAsBeta([
898
+ ...configParam.d1_databases,
899
+ ...(args.d1Databases || []),
900
+ ]),
901
+ };
902
+
903
+ return bindings;
904
+ }