wrangler 0.0.0-e95e5a0 → 0.0.0-e98613f8

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