wrangler 0.0.0-e64812e → 0.0.0-e7a0cd5b

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