wrangler 0.0.0-e87ad068 → 0.0.0-e8997b87

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 (326) hide show
  1. package/README.md +11 -13
  2. package/bin/wrangler.js +13 -102
  3. package/config-schema.json +3052 -0
  4. package/package.json +197 -199
  5. package/templates/__tests__/tsconfig.json +1 -1
  6. package/templates/checked-fetch.js +11 -4
  7. package/templates/facade.d.ts +7 -2
  8. package/templates/gitignore +2 -4
  9. package/templates/init-tests/test-vitest-new-worker.js +1 -1
  10. package/templates/init-tests/test-vitest-new-worker.ts +1 -1
  11. package/templates/middleware/common.ts +9 -4
  12. package/templates/middleware/loader-modules.ts +94 -44
  13. package/templates/middleware/loader-sw.ts +6 -10
  14. package/templates/middleware/middleware-ensure-req-body-drained.ts +18 -0
  15. package/templates/middleware/middleware-serve-static-assets.d.ts +6 -0
  16. package/templates/middleware/middleware-serve-static-assets.ts +56 -0
  17. package/templates/modules-watch-stub.js +4 -0
  18. package/templates/new-worker-scheduled.ts +1 -1
  19. package/templates/new-worker.ts +1 -1
  20. package/templates/pages-dev-pipeline.ts +2 -1
  21. package/templates/pages-shim.ts +0 -3
  22. package/templates/pages-template-plugin.ts +14 -4
  23. package/templates/pages-template-worker.ts +12 -3
  24. package/templates/startDevWorker/InspectorProxyWorker.ts +664 -0
  25. package/templates/startDevWorker/ProxyWorker.ts +334 -0
  26. package/templates/tsconfig.init.json +1 -1
  27. package/templates/tsconfig.json +1 -1
  28. package/wrangler-dist/InspectorProxyWorker.js +464 -0
  29. package/wrangler-dist/InspectorProxyWorker.js.map +6 -0
  30. package/wrangler-dist/ProxyWorker.js +240 -0
  31. package/wrangler-dist/ProxyWorker.js.map +6 -0
  32. package/wrangler-dist/cli.d.ts +24940 -306
  33. package/wrangler-dist/cli.js +173787 -118025
  34. package/wrangler-dist/wasm-sync.wasm +0 -0
  35. package/Cloudflare_CA.pem +0 -18
  36. package/import_meta_url.js +0 -3
  37. package/miniflare-config-stubs/.env.empty +0 -0
  38. package/miniflare-config-stubs/package.empty.json +0 -1
  39. package/miniflare-config-stubs/wrangler.empty.toml +0 -0
  40. package/miniflare-dist/index.mjs +0 -6442
  41. package/src/__tests__/access.test.ts +0 -25
  42. package/src/__tests__/api-dev.test.ts +0 -238
  43. package/src/__tests__/api-devregistry.test.ts +0 -121
  44. package/src/__tests__/api.test.ts +0 -102
  45. package/src/__tests__/config-cache-without-cache-dir.test.ts +0 -38
  46. package/src/__tests__/config-cache.test.ts +0 -42
  47. package/src/__tests__/configuration.test.ts +0 -4727
  48. package/src/__tests__/constellation.test.ts +0 -371
  49. package/src/__tests__/d1/d1.test.ts +0 -82
  50. package/src/__tests__/d1/execute.test.ts +0 -66
  51. package/src/__tests__/d1/migrate.test.ts +0 -257
  52. package/src/__tests__/d1/splitter.test.ts +0 -255
  53. package/src/__tests__/delete.test.ts +0 -272
  54. package/src/__tests__/deployments.test.ts +0 -369
  55. package/src/__tests__/dev.test.tsx +0 -1617
  56. package/src/__tests__/generate.test.ts +0 -237
  57. package/src/__tests__/get-host-from-url.test.ts +0 -16
  58. package/src/__tests__/guess-worker-format.test.ts +0 -120
  59. package/src/__tests__/helpers/clipboardy-mock.js +0 -4
  60. package/src/__tests__/helpers/cmd-shim.d.ts +0 -11
  61. package/src/__tests__/helpers/end-event-loop.ts +0 -6
  62. package/src/__tests__/helpers/mock-account-id.ts +0 -48
  63. package/src/__tests__/helpers/mock-auth-domain.ts +0 -20
  64. package/src/__tests__/helpers/mock-bin.ts +0 -36
  65. package/src/__tests__/helpers/mock-console.ts +0 -112
  66. package/src/__tests__/helpers/mock-dialogs.ts +0 -139
  67. package/src/__tests__/helpers/mock-get-pages-upload-token.ts +0 -25
  68. package/src/__tests__/helpers/mock-get-zone-from-host.ts +0 -11
  69. package/src/__tests__/helpers/mock-http-server.ts +0 -46
  70. package/src/__tests__/helpers/mock-istty.ts +0 -74
  71. package/src/__tests__/helpers/mock-known-routes.ts +0 -12
  72. package/src/__tests__/helpers/mock-kv.ts +0 -46
  73. package/src/__tests__/helpers/mock-oauth-flow.ts +0 -263
  74. package/src/__tests__/helpers/mock-process.ts +0 -34
  75. package/src/__tests__/helpers/mock-set-timeout.ts +0 -16
  76. package/src/__tests__/helpers/mock-stdin.ts +0 -108
  77. package/src/__tests__/helpers/mock-web-socket.ts +0 -29
  78. package/src/__tests__/helpers/msw/blob-worker.cjs +0 -19
  79. package/src/__tests__/helpers/msw/handlers/access.ts +0 -13
  80. package/src/__tests__/helpers/msw/handlers/deployments.ts +0 -160
  81. package/src/__tests__/helpers/msw/handlers/namespaces.ts +0 -81
  82. package/src/__tests__/helpers/msw/handlers/oauth.ts +0 -31
  83. package/src/__tests__/helpers/msw/handlers/r2.ts +0 -60
  84. package/src/__tests__/helpers/msw/handlers/script.ts +0 -56
  85. package/src/__tests__/helpers/msw/handlers/user.ts +0 -52
  86. package/src/__tests__/helpers/msw/handlers/zones.ts +0 -20
  87. package/src/__tests__/helpers/msw/index.ts +0 -52
  88. package/src/__tests__/helpers/msw/read-file-sync.js +0 -61
  89. package/src/__tests__/helpers/run-in-tmp.ts +0 -38
  90. package/src/__tests__/helpers/run-wrangler.ts +0 -16
  91. package/src/__tests__/helpers/string-dynamic-values-matcher.ts +0 -28
  92. package/src/__tests__/helpers/worker-scripts/child-wrangler.toml +0 -1
  93. package/src/__tests__/helpers/worker-scripts/hello-world-worker.js +0 -5
  94. package/src/__tests__/helpers/worker-scripts/hello-world-wrangler.toml +0 -1
  95. package/src/__tests__/helpers/worker-scripts/parent-worker.js +0 -11
  96. package/src/__tests__/helpers/worker-scripts/parent-wrangler.toml +0 -5
  97. package/src/__tests__/helpers/write-worker-source.ts +0 -31
  98. package/src/__tests__/helpers/write-wrangler-toml.ts +0 -17
  99. package/src/__tests__/https-options.test.ts +0 -163
  100. package/src/__tests__/index.test.ts +0 -282
  101. package/src/__tests__/init.test.ts +0 -3302
  102. package/src/__tests__/jest.setup.ts +0 -179
  103. package/src/__tests__/kv.test.ts +0 -1799
  104. package/src/__tests__/logger.test.ts +0 -207
  105. package/src/__tests__/logout.test.ts +0 -47
  106. package/src/__tests__/metrics.test.ts +0 -493
  107. package/src/__tests__/middleware.scheduled.test.ts +0 -145
  108. package/src/__tests__/middleware.test.ts +0 -816
  109. package/src/__tests__/mtls-certificates.test.ts +0 -589
  110. package/src/__tests__/package-manager.test.ts +0 -353
  111. package/src/__tests__/pages/deployment-list.test.ts +0 -80
  112. package/src/__tests__/pages/functions-build.test.ts +0 -529
  113. package/src/__tests__/pages/pages.test.ts +0 -81
  114. package/src/__tests__/pages/project-create.test.ts +0 -63
  115. package/src/__tests__/pages/project-list.test.ts +0 -110
  116. package/src/__tests__/pages/project-upload.test.ts +0 -500
  117. package/src/__tests__/pages/publish.test.ts +0 -2864
  118. package/src/__tests__/pages-deployment-tail.test.ts +0 -957
  119. package/src/__tests__/parse.test.ts +0 -436
  120. package/src/__tests__/paths.test.ts +0 -39
  121. package/src/__tests__/publish.test.ts +0 -8882
  122. package/src/__tests__/pubsub.test.ts +0 -496
  123. package/src/__tests__/queues.test.ts +0 -532
  124. package/src/__tests__/r2.test.ts +0 -374
  125. package/src/__tests__/route.test.ts +0 -45
  126. package/src/__tests__/secret.test.ts +0 -693
  127. package/src/__tests__/tail.test.ts +0 -1000
  128. package/src/__tests__/test-old-node-version.js +0 -31
  129. package/src/__tests__/traverse-module-graph.test.ts +0 -220
  130. package/src/__tests__/tsconfig-sanity.ts +0 -12
  131. package/src/__tests__/tsconfig.json +0 -8
  132. package/src/__tests__/tsconfig.tsbuildinfo +0 -1
  133. package/src/__tests__/type-generation.test.ts +0 -234
  134. package/src/__tests__/unstableDev.test.ts +0 -61
  135. package/src/__tests__/user.test.ts +0 -118
  136. package/src/__tests__/utils-collectKeyValues.test.ts +0 -47
  137. package/src/__tests__/validate-dev-props.test.ts +0 -56
  138. package/src/__tests__/version.test.ts +0 -35
  139. package/src/__tests__/whoami.test.tsx +0 -172
  140. package/src/__tests__/worker-namespace.test.ts +0 -340
  141. package/src/abort.d.ts +0 -3
  142. package/src/api/dev.ts +0 -328
  143. package/src/api/index.ts +0 -11
  144. package/src/api/mtls-certificate.ts +0 -148
  145. package/src/api/pages/create-worker-bundle-contents.ts +0 -80
  146. package/src/api/pages/index.ts +0 -5
  147. package/src/api/pages/publish.tsx +0 -371
  148. package/src/bundle-reporter.ts +0 -68
  149. package/src/bundle.ts +0 -952
  150. package/src/cfetch/index.ts +0 -158
  151. package/src/cfetch/internal.ts +0 -263
  152. package/src/cli.ts +0 -28
  153. package/src/config/README.md +0 -107
  154. package/src/config/config.ts +0 -282
  155. package/src/config/diagnostics.ts +0 -80
  156. package/src/config/environment.ts +0 -659
  157. package/src/config/index.ts +0 -424
  158. package/src/config/validation-helpers.ts +0 -597
  159. package/src/config/validation.ts +0 -2524
  160. package/src/config-cache.ts +0 -85
  161. package/src/constellation/createProject.tsx +0 -51
  162. package/src/constellation/deleteProject.ts +0 -51
  163. package/src/constellation/deleteProjectModel.ts +0 -76
  164. package/src/constellation/index.ts +0 -75
  165. package/src/constellation/listCatalog.tsx +0 -35
  166. package/src/constellation/listModel.tsx +0 -49
  167. package/src/constellation/listProject.tsx +0 -28
  168. package/src/constellation/listRuntime.tsx +0 -28
  169. package/src/constellation/options.ts +0 -17
  170. package/src/constellation/types.ts +0 -17
  171. package/src/constellation/uploadModel.tsx +0 -84
  172. package/src/constellation/utils.ts +0 -90
  173. package/src/create-worker-preview.ts +0 -293
  174. package/src/create-worker-upload-form.ts +0 -384
  175. package/src/d1/backups.tsx +0 -219
  176. package/src/d1/constants.ts +0 -5
  177. package/src/d1/create.tsx +0 -102
  178. package/src/d1/delete.ts +0 -53
  179. package/src/d1/execute.tsx +0 -371
  180. package/src/d1/formatTimeAgo.ts +0 -14
  181. package/src/d1/index.ts +0 -100
  182. package/src/d1/list.tsx +0 -62
  183. package/src/d1/migrations/apply.tsx +0 -235
  184. package/src/d1/migrations/create.tsx +0 -79
  185. package/src/d1/migrations/helpers.ts +0 -171
  186. package/src/d1/migrations/index.ts +0 -3
  187. package/src/d1/migrations/list.tsx +0 -95
  188. package/src/d1/migrations/options.ts +0 -23
  189. package/src/d1/options.ts +0 -22
  190. package/src/d1/splitter.ts +0 -161
  191. package/src/d1/types.ts +0 -32
  192. package/src/d1/utils.ts +0 -49
  193. package/src/delete.ts +0 -100
  194. package/src/deployments.ts +0 -368
  195. package/src/deprecated/index.ts +0 -144
  196. package/src/dev/dev-vars.ts +0 -39
  197. package/src/dev/dev.tsx +0 -607
  198. package/src/dev/get-local-persistence-path.ts +0 -31
  199. package/src/dev/local.tsx +0 -952
  200. package/src/dev/remote.tsx +0 -636
  201. package/src/dev/start-server.ts +0 -558
  202. package/src/dev/use-esbuild.ts +0 -230
  203. package/src/dev/validate-dev-props.ts +0 -40
  204. package/src/dev-registry.ts +0 -213
  205. package/src/dev.tsx +0 -946
  206. package/src/dialogs.ts +0 -136
  207. package/src/dispatch-namespace.ts +0 -211
  208. package/src/docs/helpers.ts +0 -50
  209. package/src/docs/index.ts +0 -54
  210. package/src/durable.ts +0 -102
  211. package/src/entry.ts +0 -344
  212. package/src/environment-variables/factory.ts +0 -89
  213. package/src/environment-variables/misc-variables.ts +0 -30
  214. package/src/errors.ts +0 -11
  215. package/src/generate/index.ts +0 -303
  216. package/src/git-client.ts +0 -135
  217. package/src/global-wrangler-config-path.ts +0 -26
  218. package/src/https-options.ts +0 -127
  219. package/src/index.ts +0 -769
  220. package/src/init.ts +0 -1074
  221. package/src/inspect.ts +0 -883
  222. package/src/intl-polyfill.d.ts +0 -139
  223. package/src/is-ci.ts +0 -14
  224. package/src/is-interactive.ts +0 -16
  225. package/src/jest.d.ts +0 -4
  226. package/src/kv/helpers.ts +0 -433
  227. package/src/kv/index.ts +0 -594
  228. package/src/logger.ts +0 -123
  229. package/src/metrics/index.ts +0 -5
  230. package/src/metrics/metrics-config.ts +0 -239
  231. package/src/metrics/metrics-dispatcher.ts +0 -96
  232. package/src/metrics/metrics-usage-headers.ts +0 -24
  233. package/src/metrics/send-event.ts +0 -99
  234. package/src/miniflare-cli/README.md +0 -30
  235. package/src/miniflare-cli/assets.ts +0 -251
  236. package/src/miniflare-cli/index.ts +0 -210
  237. package/src/miniflare-cli/request-context.ts +0 -40
  238. package/src/miniflare-cli/tsconfig.json +0 -9
  239. package/src/miniflare-cli/tsconfig.tsbuildinfo +0 -1
  240. package/src/miniflare-cli/types.ts +0 -11
  241. package/src/module-collection.ts +0 -333
  242. package/src/mtls-certificate/cli.ts +0 -155
  243. package/src/open-in-browser.ts +0 -17
  244. package/src/package-manager.ts +0 -219
  245. package/src/pages/build.ts +0 -423
  246. package/src/pages/buildFunctions.ts +0 -140
  247. package/src/pages/constants.ts +0 -18
  248. package/src/pages/deployment-tails.ts +0 -281
  249. package/src/pages/deployments.tsx +0 -84
  250. package/src/pages/dev.ts +0 -759
  251. package/src/pages/errors.ts +0 -67
  252. package/src/pages/functions/buildPlugin.ts +0 -114
  253. package/src/pages/functions/buildWorker.ts +0 -381
  254. package/src/pages/functions/filepath-routing.test.ts +0 -234
  255. package/src/pages/functions/filepath-routing.ts +0 -189
  256. package/src/pages/functions/identifiers.ts +0 -78
  257. package/src/pages/functions/routes-consolidation.test.ts +0 -250
  258. package/src/pages/functions/routes-consolidation.ts +0 -73
  259. package/src/pages/functions/routes-transformation.test.ts +0 -282
  260. package/src/pages/functions/routes-transformation.ts +0 -115
  261. package/src/pages/functions/routes-validation.test.ts +0 -403
  262. package/src/pages/functions/routes-validation.ts +0 -202
  263. package/src/pages/functions/routes.ts +0 -151
  264. package/src/pages/functions/tsconfig.json +0 -8
  265. package/src/pages/functions/tsconfig.tsbuildinfo +0 -1
  266. package/src/pages/functions.ts +0 -86
  267. package/src/pages/hash.ts +0 -13
  268. package/src/pages/index.ts +0 -102
  269. package/src/pages/projects.tsx +0 -159
  270. package/src/pages/prompt-select-project.tsx +0 -31
  271. package/src/pages/publish.tsx +0 -267
  272. package/src/pages/types.ts +0 -46
  273. package/src/pages/upload.tsx +0 -469
  274. package/src/pages/utils.ts +0 -23
  275. package/src/parse.ts +0 -308
  276. package/src/paths.ts +0 -71
  277. package/src/proxy.ts +0 -694
  278. package/src/publish/index.ts +0 -274
  279. package/src/publish/publish.ts +0 -1068
  280. package/src/pubsub/index.ts +0 -286
  281. package/src/pubsub/pubsub-commands.ts +0 -623
  282. package/src/queues/cli/commands/consumer/add.ts +0 -71
  283. package/src/queues/cli/commands/consumer/index.ts +0 -19
  284. package/src/queues/cli/commands/consumer/remove.ts +0 -31
  285. package/src/queues/cli/commands/create.ts +0 -25
  286. package/src/queues/cli/commands/delete.ts +0 -26
  287. package/src/queues/cli/commands/index.ts +0 -35
  288. package/src/queues/cli/commands/list.ts +0 -25
  289. package/src/queues/client.ts +0 -136
  290. package/src/queues/utils.ts +0 -18
  291. package/src/r2/constants.ts +0 -4
  292. package/src/r2/helpers.ts +0 -132
  293. package/src/r2/index.ts +0 -289
  294. package/src/routes.ts +0 -140
  295. package/src/secret/index.ts +0 -380
  296. package/src/selfsigned.d.ts +0 -29
  297. package/src/sites.ts +0 -484
  298. package/src/tail/createTail.ts +0 -415
  299. package/src/tail/filters.ts +0 -277
  300. package/src/tail/index.ts +0 -211
  301. package/src/tail/printing.ts +0 -134
  302. package/src/traverse-module-graph.ts +0 -54
  303. package/src/tsconfig-sanity.ts +0 -16
  304. package/src/type-generation.ts +0 -187
  305. package/src/update-check.ts +0 -19
  306. package/src/user/access.ts +0 -68
  307. package/src/user/auth-variables.ts +0 -113
  308. package/src/user/choose-account.tsx +0 -39
  309. package/src/user/generate-auth-url.ts +0 -33
  310. package/src/user/generate-random-state.ts +0 -16
  311. package/src/user/index.ts +0 -2
  312. package/src/user/user.ts +0 -1234
  313. package/src/utils/collectKeyValues.ts +0 -14
  314. package/src/utils/render.ts +0 -93
  315. package/src/whoami.ts +0 -135
  316. package/src/worker.ts +0 -300
  317. package/src/yargs-types.ts +0 -37
  318. package/src/zones.ts +0 -191
  319. package/templates/__tests__/tsconfig.tsbuildinfo +0 -1
  320. package/templates/d1-beta-facade.js +0 -244
  321. package/templates/first-party-worker-module-facade.ts +0 -19
  322. package/templates/format-dev-errors.ts +0 -34
  323. package/templates/serve-static-assets.ts +0 -66
  324. package/templates/service-bindings-module-facade.js +0 -55
  325. package/templates/service-bindings-sw-facade.js +0 -42
  326. package/templates/tsconfig.tsbuildinfo +0 -1
@@ -1,2524 +0,0 @@
1
- import path from "node:path";
2
- import TOML from "@iarna/toml";
3
- import { getConstellationWarningFromEnv } from "../constellation/utils";
4
- import { Diagnostics } from "./diagnostics";
5
- import {
6
- deprecated,
7
- experimental,
8
- hasProperty,
9
- inheritable,
10
- isBoolean,
11
- isObjectWith,
12
- isOneOf,
13
- isOptionalProperty,
14
- isRequiredProperty,
15
- isString,
16
- isStringArray,
17
- validateAdditionalProperties,
18
- notInheritable,
19
- validateOptionalProperty,
20
- validateOptionalTypedArray,
21
- validateRequiredProperty,
22
- validateTypedArray,
23
- all,
24
- isMutuallyExclusiveWith,
25
- inheritableInLegacyEnvironments,
26
- appendEnvName,
27
- getBindingNames,
28
- isValidName,
29
- } from "./validation-helpers";
30
- import type { Config, DevConfig, RawConfig, RawDevConfig } from "./config";
31
- import type {
32
- RawEnvironment,
33
- DeprecatedUpload,
34
- Environment,
35
- Rule,
36
- TailConsumer,
37
- } from "./environment";
38
- import type { ValidatorFn } from "./validation-helpers";
39
-
40
- const ENGLISH = new Intl.ListFormat("en");
41
-
42
- /**
43
- * Validate the given `rawConfig` object that was loaded from `configPath`.
44
- *
45
- * The configuration is normalized, which includes using default values for missing field,
46
- * and copying over inheritable fields into named environments.
47
- *
48
- * Any errors or warnings from the validation are available in the returned `diagnostics` object.
49
- */
50
- export function normalizeAndValidateConfig(
51
- rawConfig: RawConfig,
52
- configPath: string | undefined,
53
- args: unknown
54
- ): {
55
- config: Config;
56
- diagnostics: Diagnostics;
57
- } {
58
- const diagnostics = new Diagnostics(
59
- `Processing ${
60
- configPath ? path.relative(process.cwd(), configPath) : "wrangler"
61
- } configuration:`
62
- );
63
-
64
- deprecated(
65
- diagnostics,
66
- rawConfig,
67
- "miniflare",
68
- "Wrangler does not use configuration in the `miniflare` section. Unless you are using Miniflare directly you can remove this section.",
69
- true,
70
- "😶 Ignored"
71
- );
72
-
73
- deprecated(
74
- diagnostics,
75
- rawConfig,
76
- "type",
77
- "Most common features now work out of the box with wrangler, including modules, jsx, typescript, etc. If you need anything more, use a custom build.",
78
- true,
79
- "😶 Ignored"
80
- );
81
-
82
- deprecated(
83
- diagnostics,
84
- rawConfig,
85
- "webpack_config",
86
- "Most common features now work out of the box with wrangler, including modules, jsx, typescript, etc. If you need anything more, use a custom build.",
87
- true,
88
- "😶 Ignored"
89
- );
90
-
91
- validateOptionalProperty(
92
- diagnostics,
93
- "",
94
- "legacy_env",
95
- rawConfig.legacy_env,
96
- "boolean"
97
- );
98
-
99
- validateOptionalProperty(
100
- diagnostics,
101
- "",
102
- "send_metrics",
103
- rawConfig.send_metrics,
104
- "boolean"
105
- );
106
-
107
- validateOptionalProperty(
108
- diagnostics,
109
- "",
110
- "keep_vars",
111
- rawConfig.keep_vars,
112
- "boolean"
113
- );
114
-
115
- // TODO: set the default to false to turn on service environments as the default
116
- const isLegacyEnv =
117
- (args as { "legacy-env": boolean | undefined })["legacy-env"] ??
118
- rawConfig.legacy_env ??
119
- true;
120
-
121
- // TODO: remove this once service environments goes GA.
122
- if (!isLegacyEnv) {
123
- diagnostics.warnings.push(
124
- "Experimental: Service environments are in beta, and their behaviour is guaranteed to change in the future. DO NOT USE IN PRODUCTION."
125
- );
126
- }
127
-
128
- const topLevelEnv = normalizeAndValidateEnvironment(
129
- diagnostics,
130
- configPath,
131
- rawConfig
132
- );
133
-
134
- //TODO: find a better way to define the type of Args that can be passed to the normalizeAndValidateConfig()
135
- const envName = (args as { env: string | undefined }).env;
136
-
137
- let activeEnv = topLevelEnv;
138
- if (envName !== undefined) {
139
- const envDiagnostics = new Diagnostics(
140
- `"env.${envName}" environment configuration`
141
- );
142
- const rawEnv = rawConfig.env?.[envName];
143
- if (rawEnv !== undefined) {
144
- activeEnv = normalizeAndValidateEnvironment(
145
- envDiagnostics,
146
- configPath,
147
- rawEnv,
148
- envName,
149
- topLevelEnv,
150
- isLegacyEnv,
151
- rawConfig
152
- );
153
- diagnostics.addChild(envDiagnostics);
154
- } else {
155
- // An environment was specified, but no configuration for it was found.
156
- // To cover any legacy environment cases, where the `envName` is used,
157
- // Let's create a fake active environment with the specified `envName`.
158
- activeEnv = normalizeAndValidateEnvironment(
159
- envDiagnostics,
160
- configPath,
161
- {},
162
- envName,
163
- topLevelEnv,
164
- isLegacyEnv,
165
- rawConfig
166
- );
167
- const envNames = rawConfig.env
168
- ? `The available configured environment names are: ${JSON.stringify(
169
- Object.keys(rawConfig.env)
170
- )}\n`
171
- : "";
172
- const message =
173
- `No environment found in configuration with name "${envName}".\n` +
174
- `Before using \`--env=${envName}\` there should be an equivalent environment section in the configuration.\n` +
175
- `${envNames}\n` +
176
- `Consider adding an environment configuration section to the wrangler.toml file:\n` +
177
- "```\n[env." +
178
- envName +
179
- "]\n```\n";
180
-
181
- if (envNames.length > 0) {
182
- diagnostics.errors.push(message);
183
- } else {
184
- // Only warn (rather than error) if there are not actually any environments configured in wrangler.toml.
185
- diagnostics.warnings.push(message);
186
- }
187
- }
188
- }
189
-
190
- // Process the top-level default environment configuration.
191
- const config: Config = {
192
- configPath,
193
- legacy_env: isLegacyEnv,
194
- send_metrics: rawConfig.send_metrics,
195
- keep_vars: rawConfig.keep_vars,
196
- ...activeEnv,
197
- dev: normalizeAndValidateDev(diagnostics, rawConfig.dev ?? {}),
198
- migrations: normalizeAndValidateMigrations(
199
- diagnostics,
200
- rawConfig.migrations ?? [],
201
- activeEnv.durable_objects
202
- ),
203
- site: normalizeAndValidateSite(
204
- diagnostics,
205
- configPath,
206
- rawConfig,
207
- activeEnv.main
208
- ),
209
- assets: normalizeAndValidateAssets(diagnostics, configPath, rawConfig),
210
- wasm_modules: normalizeAndValidateModulePaths(
211
- diagnostics,
212
- configPath,
213
- "wasm_modules",
214
- rawConfig.wasm_modules
215
- ),
216
- text_blobs: normalizeAndValidateModulePaths(
217
- diagnostics,
218
- configPath,
219
- "text_blobs",
220
- rawConfig.text_blobs
221
- ),
222
- data_blobs: normalizeAndValidateModulePaths(
223
- diagnostics,
224
- configPath,
225
- "data_blobs",
226
- rawConfig.data_blobs
227
- ),
228
- };
229
-
230
- validateBindingsHaveUniqueNames(diagnostics, config);
231
-
232
- validateAdditionalProperties(
233
- diagnostics,
234
- "top-level",
235
- Object.keys(rawConfig),
236
- [...Object.keys(config), "env"]
237
- );
238
-
239
- experimental(diagnostics, rawConfig, "assets");
240
-
241
- return { config, diagnostics };
242
- }
243
-
244
- /**
245
- * Validate the `build` configuration and return the normalized values.
246
- */
247
- function normalizeAndValidateBuild(
248
- diagnostics: Diagnostics,
249
- rawEnv: RawEnvironment,
250
- rawBuild: Config["build"],
251
- configPath: string | undefined
252
- ): Config["build"] & { deprecatedUpload: DeprecatedUpload } {
253
- const { command, cwd, watch_dir = "./src", upload, ...rest } = rawBuild;
254
- const deprecatedUpload: DeprecatedUpload = { ...upload };
255
- validateAdditionalProperties(diagnostics, "build", Object.keys(rest), []);
256
-
257
- validateOptionalProperty(diagnostics, "build", "command", command, "string");
258
- validateOptionalProperty(diagnostics, "build", "cwd", cwd, "string");
259
- if (Array.isArray(watch_dir)) {
260
- validateTypedArray(diagnostics, "build.watch_dir", watch_dir, "string");
261
- } else {
262
- validateOptionalProperty(
263
- diagnostics,
264
- "build",
265
- "watch_dir",
266
- watch_dir,
267
- "string"
268
- );
269
- }
270
-
271
- deprecated(
272
- diagnostics,
273
- rawEnv,
274
- "build.upload.format",
275
- "The format is inferred automatically from the code.",
276
- true
277
- );
278
-
279
- if (rawEnv.main !== undefined && rawBuild.upload?.main) {
280
- diagnostics.errors.push(
281
- `Don't define both the \`main\` and \`build.upload.main\` fields in your configuration.\n` +
282
- `They serve the same purpose: to point to the entry-point of your worker.\n` +
283
- `Delete the \`build.upload.main\` and \`build.upload.dir\` field from your config.`
284
- );
285
- } else {
286
- deprecated(
287
- diagnostics,
288
- rawEnv,
289
- "build.upload.main",
290
- `Delete the \`build.upload.main\` and \`build.upload.dir\` fields.\n` +
291
- `Then add the top level \`main\` field to your configuration file:\n` +
292
- `\`\`\`\n` +
293
- `main = "${path.join(
294
- rawBuild.upload?.dir ?? "./dist",
295
- rawBuild.upload?.main ?? "."
296
- )}"\n` +
297
- `\`\`\``,
298
- true
299
- );
300
-
301
- deprecated(
302
- diagnostics,
303
- rawEnv,
304
- "build.upload.dir",
305
- `Use the top level "main" field or a command-line argument to specify the entry-point for the Worker.`,
306
- true
307
- );
308
- }
309
-
310
- return {
311
- command,
312
- watch_dir:
313
- // - `watch_dir` only matters when `command` is defined, so we apply
314
- // a default only when `command` is defined
315
- // - `configPath` will always be defined since `build` can only
316
- // be configured in `wrangler.toml`, but who knows, that may
317
- // change in the future, so we do a check anyway
318
- command && configPath
319
- ? Array.isArray(watch_dir)
320
- ? watch_dir.map((dir) =>
321
- path.relative(
322
- process.cwd(),
323
- path.join(path.dirname(configPath), `${dir}`)
324
- )
325
- )
326
- : path.relative(
327
- process.cwd(),
328
- path.join(path.dirname(configPath), `${watch_dir}`)
329
- )
330
- : watch_dir,
331
- cwd,
332
- deprecatedUpload,
333
- };
334
- }
335
-
336
- /**
337
- * Validate the `main` field and return the normalized values.
338
- */
339
- function normalizeAndValidateMainField(
340
- configPath: string | undefined,
341
- rawMain: string | undefined,
342
- deprecatedUpload: DeprecatedUpload | undefined
343
- ): string | undefined {
344
- const configDir = path.dirname(configPath ?? "wrangler.toml");
345
- if (rawMain !== undefined) {
346
- if (typeof rawMain === "string") {
347
- const directory = path.resolve(configDir);
348
- return path.resolve(directory, rawMain);
349
- } else {
350
- return rawMain;
351
- }
352
- } else if (deprecatedUpload?.main !== undefined) {
353
- const directory = path.resolve(
354
- configDir,
355
- deprecatedUpload?.dir || "./dist"
356
- );
357
- return path.resolve(directory, deprecatedUpload.main);
358
- } else {
359
- return;
360
- }
361
- }
362
-
363
- /**
364
- * Validate the `base_dir` field and return the normalized values.
365
- */
366
- function normalizeAndValidateBaseDirField(
367
- configPath: string | undefined,
368
- rawDir: string | undefined
369
- ): string | undefined {
370
- const configDir = path.dirname(configPath ?? "wrangler.toml");
371
- if (rawDir !== undefined) {
372
- if (typeof rawDir === "string") {
373
- const directory = path.resolve(configDir);
374
- return path.resolve(directory, rawDir);
375
- } else {
376
- return rawDir;
377
- }
378
- } else {
379
- return;
380
- }
381
- }
382
-
383
- /**
384
- * Validate the `dev` configuration and return the normalized values.
385
- */
386
- function normalizeAndValidateDev(
387
- diagnostics: Diagnostics,
388
- rawDev: RawDevConfig
389
- ): DevConfig {
390
- const {
391
- ip = "0.0.0.0",
392
- port,
393
- inspector_port,
394
- local_protocol = "http",
395
- upstream_protocol = "https",
396
- host,
397
- ...rest
398
- } = rawDev;
399
- validateAdditionalProperties(diagnostics, "dev", Object.keys(rest), []);
400
-
401
- validateOptionalProperty(diagnostics, "dev", "ip", ip, "string");
402
- validateOptionalProperty(diagnostics, "dev", "port", port, "number");
403
- validateOptionalProperty(
404
- diagnostics,
405
- "dev",
406
- "inspector_port",
407
- inspector_port,
408
- "number"
409
- );
410
- validateOptionalProperty(
411
- diagnostics,
412
- "dev",
413
- "local_protocol",
414
- local_protocol,
415
- "string",
416
- ["http", "https"]
417
- );
418
- validateOptionalProperty(
419
- diagnostics,
420
- "dev",
421
- "upstream_protocol",
422
- upstream_protocol,
423
- "string",
424
- ["http", "https"]
425
- );
426
- validateOptionalProperty(diagnostics, "dev", "host", host, "string");
427
- return { ip, port, inspector_port, local_protocol, upstream_protocol, host };
428
- }
429
-
430
- /**
431
- * Validate the `migrations` configuration and return the normalized values.
432
- */
433
- function normalizeAndValidateMigrations(
434
- diagnostics: Diagnostics,
435
- rawMigrations: Config["migrations"],
436
- durableObjects: Config["durable_objects"]
437
- ): Config["migrations"] {
438
- if (!Array.isArray(rawMigrations)) {
439
- diagnostics.errors.push(
440
- `The optional "migrations" field should be an array, but got ${JSON.stringify(
441
- rawMigrations
442
- )}`
443
- );
444
- return [];
445
- } else {
446
- for (let i = 0; i < rawMigrations.length; i++) {
447
- const { tag, new_classes, renamed_classes, deleted_classes, ...rest } =
448
- rawMigrations[i];
449
-
450
- validateAdditionalProperties(
451
- diagnostics,
452
- "migrations",
453
- Object.keys(rest),
454
- []
455
- );
456
-
457
- validateRequiredProperty(
458
- diagnostics,
459
- `migrations[${i}]`,
460
- `tag`,
461
- tag,
462
- "string"
463
- );
464
- validateOptionalTypedArray(
465
- diagnostics,
466
- `migrations[${i}].new_classes`,
467
- new_classes,
468
- "string"
469
- );
470
- if (renamed_classes !== undefined) {
471
- if (!Array.isArray(renamed_classes)) {
472
- diagnostics.errors.push(
473
- `Expected "migrations[${i}].renamed_classes" to be an array of "{from: string, to: string}" objects but got ${JSON.stringify(
474
- renamed_classes
475
- )}.`
476
- );
477
- } else if (
478
- renamed_classes.some(
479
- (c) =>
480
- typeof c !== "object" ||
481
- !isRequiredProperty(c, "from", "string") ||
482
- !isRequiredProperty(c, "to", "string")
483
- )
484
- ) {
485
- diagnostics.errors.push(
486
- `Expected "migrations[${i}].renamed_classes" to be an array of "{from: string, to: string}" objects but got ${JSON.stringify(
487
- renamed_classes
488
- )}.`
489
- );
490
- }
491
- }
492
- validateOptionalTypedArray(
493
- diagnostics,
494
- `migrations[${i}].deleted_classes`,
495
- deleted_classes,
496
- "string"
497
- );
498
- }
499
-
500
- if (
501
- Array.isArray(durableObjects?.bindings) &&
502
- durableObjects.bindings.length > 0
503
- ) {
504
- // intrinsic [durable_objects] implies [migrations]
505
- const exportedDurableObjects = (durableObjects.bindings || []).filter(
506
- (binding) => !binding.script_name
507
- );
508
- if (exportedDurableObjects.length > 0 && rawMigrations.length === 0) {
509
- if (
510
- !exportedDurableObjects.some(
511
- (exportedDurableObject) =>
512
- typeof exportedDurableObject.class_name !== "string"
513
- )
514
- ) {
515
- const durableObjectClassnames = exportedDurableObjects.map(
516
- (durable) => durable.class_name
517
- );
518
-
519
- diagnostics.warnings.push(
520
- `In wrangler.toml, you have configured [durable_objects] exported by this Worker (${durableObjectClassnames.join(
521
- ", "
522
- )}), but no [migrations] for them. This may not work as expected until you add a [migrations] section to your wrangler.toml. Add this configuration to your wrangler.toml:
523
-
524
- \`\`\`
525
- [[migrations]]
526
- tag = "v1" # Should be unique for each entry
527
- new_classes = [${durableObjectClassnames
528
- .map((name) => `"${name}"`)
529
- .join(", ")}]
530
- \`\`\`
531
-
532
- Refer to https://developers.cloudflare.com/workers/learning/using-durable-objects/#durable-object-migrations-in-wranglertoml for more details.`
533
- );
534
- }
535
- }
536
- }
537
-
538
- return rawMigrations;
539
- }
540
- }
541
-
542
- /**
543
- * Validate the `site` configuration and return the normalized values.
544
- */
545
- function normalizeAndValidateSite(
546
- diagnostics: Diagnostics,
547
- configPath: string | undefined,
548
- rawConfig: RawConfig,
549
- mainEntryPoint: string | undefined
550
- ): Config["site"] {
551
- if (rawConfig?.site !== undefined) {
552
- const { bucket, include = [], exclude = [], ...rest } = rawConfig.site;
553
-
554
- validateAdditionalProperties(diagnostics, "site", Object.keys(rest), [
555
- "entry-point",
556
- ]);
557
- validateRequiredProperty(diagnostics, "site", "bucket", bucket, "string");
558
- validateTypedArray(diagnostics, "sites.include", include, "string");
559
- validateTypedArray(diagnostics, "sites.exclude", exclude, "string");
560
- validateOptionalProperty(
561
- diagnostics,
562
- "site",
563
- "entry-point",
564
- rawConfig.site["entry-point"],
565
- "string"
566
- );
567
-
568
- deprecated(
569
- diagnostics,
570
- rawConfig,
571
- `site.entry-point`,
572
- `Delete the \`site.entry-point\` field, then add the top level \`main\` field to your configuration file:\n` +
573
- `\`\`\`\n` +
574
- `main = "${path.join(
575
- String(rawConfig.site["entry-point"]) || "workers-site",
576
- path.extname(String(rawConfig.site["entry-point"]) || "workers-site")
577
- ? ""
578
- : "index.js"
579
- )}"\n` +
580
- `\`\`\``,
581
- false,
582
- undefined,
583
- "warning"
584
- );
585
-
586
- let siteEntryPoint = rawConfig.site["entry-point"];
587
-
588
- if (!mainEntryPoint && !siteEntryPoint) {
589
- // this means that we're defaulting to "workers-site"
590
- // so let's add the deprecation warning
591
- diagnostics.warnings.push(
592
- `Because you've defined a [site] configuration, we're defaulting to "workers-site" for the deprecated \`site.entry-point\`field.\n` +
593
- `Add the top level \`main\` field to your configuration file:\n` +
594
- `\`\`\`\n` +
595
- `main = "workers-site/index.js"\n` +
596
- `\`\`\``
597
- );
598
- siteEntryPoint = "workers-site";
599
- } else if (mainEntryPoint && siteEntryPoint) {
600
- diagnostics.errors.push(
601
- `Don't define both the \`main\` and \`site.entry-point\` fields in your configuration.\n` +
602
- `They serve the same purpose: to point to the entry-point of your worker.\n` +
603
- `Delete the deprecated \`site.entry-point\` field from your config.`
604
- );
605
- }
606
-
607
- if (configPath && siteEntryPoint) {
608
- // rewrite the path to be relative to the working directory
609
- siteEntryPoint = path.relative(
610
- process.cwd(),
611
- path.join(path.dirname(configPath), siteEntryPoint)
612
- );
613
- }
614
-
615
- return {
616
- bucket,
617
- "entry-point": siteEntryPoint,
618
- include,
619
- exclude,
620
- };
621
- }
622
- return undefined;
623
- }
624
-
625
- /**
626
- * Validate the `assets` configuration and return normalized values.
627
- */
628
- function normalizeAndValidateAssets(
629
- diagnostics: Diagnostics,
630
- configPath: string | undefined,
631
- rawConfig: RawConfig
632
- ): Config["assets"] {
633
- // Even though the type doesn't say it,
634
- // we allow for a string input in the config,
635
- // so let's normalise it
636
- if (typeof rawConfig?.assets === "string") {
637
- return {
638
- bucket: rawConfig.assets,
639
- include: [],
640
- exclude: [],
641
- browser_TTL: undefined,
642
- serve_single_page_app: false,
643
- };
644
- }
645
-
646
- if (rawConfig?.assets === undefined) {
647
- return undefined;
648
- }
649
-
650
- if (typeof rawConfig.assets !== "object") {
651
- diagnostics.errors.push(
652
- `Expected the \`assets\` field to be a string or an object, but got ${typeof rawConfig.assets}.`
653
- );
654
- return undefined;
655
- }
656
-
657
- const {
658
- bucket,
659
- include = [],
660
- exclude = [],
661
- browser_TTL,
662
- serve_single_page_app,
663
- ...rest
664
- } = rawConfig.assets;
665
-
666
- validateAdditionalProperties(diagnostics, "assets", Object.keys(rest), []);
667
-
668
- validateRequiredProperty(diagnostics, "assets", "bucket", bucket, "string");
669
- validateTypedArray(diagnostics, "assets.include", include, "string");
670
- validateTypedArray(diagnostics, "assets.exclude", exclude, "string");
671
-
672
- validateOptionalProperty(
673
- diagnostics,
674
- "assets",
675
- "browser_TTL",
676
- browser_TTL,
677
- "number"
678
- );
679
-
680
- validateOptionalProperty(
681
- diagnostics,
682
- "assets",
683
- "serve_single_page_app",
684
- serve_single_page_app,
685
- "boolean"
686
- );
687
-
688
- return {
689
- bucket,
690
- include,
691
- exclude,
692
- browser_TTL,
693
- serve_single_page_app,
694
- };
695
- }
696
-
697
- /**
698
- * Map the paths of the `wasm_modules`, `text_blobs` or `data_blobs` configuration to be relative to the current working directory.
699
- */
700
- function normalizeAndValidateModulePaths(
701
- diagnostics: Diagnostics,
702
- configPath: string | undefined,
703
- field: "wasm_modules" | "text_blobs" | "data_blobs",
704
- rawMapping: Record<string, string> | undefined
705
- ): Record<string, string> | undefined {
706
- if (rawMapping === undefined) {
707
- return undefined;
708
- }
709
- const mapping: Record<string, string> = {};
710
- // Rewrite paths to be relative to the cwd, rather than the config path.
711
- for (const [name, filePath] of Object.entries(rawMapping)) {
712
- if (isString(diagnostics, `${field}['${name}']`, filePath, undefined)) {
713
- if (configPath) {
714
- mapping[name] = configPath
715
- ? path.relative(
716
- process.cwd(),
717
- path.join(path.dirname(configPath), filePath)
718
- )
719
- : filePath;
720
- }
721
- }
722
- }
723
- return mapping;
724
- }
725
-
726
- /**
727
- * Check whether a value has the shape of a route, which can be a string
728
- * or an object that looks like {pattern: string, zone_id: string }
729
- */
730
- function isValidRouteValue(item: unknown): boolean {
731
- if (!item) {
732
- return false;
733
- }
734
- if (typeof item === "string") {
735
- return true;
736
- }
737
- if (typeof item === "object") {
738
- if (!hasProperty(item, "pattern") || typeof item.pattern !== "string") {
739
- return false;
740
- }
741
-
742
- const otherKeys = Object.keys(item).length - 1; // minus one to subtract "pattern"
743
-
744
- const hasZoneId =
745
- hasProperty(item, "zone_id") && typeof item.zone_id === "string";
746
- const hasZoneName =
747
- hasProperty(item, "zone_name") && typeof item.zone_name === "string";
748
- const hasCustomDomainFlag =
749
- hasProperty(item, "custom_domain") &&
750
- typeof item.custom_domain === "boolean";
751
-
752
- if (otherKeys === 2 && hasCustomDomainFlag && (hasZoneId || hasZoneName)) {
753
- return true;
754
- } else if (
755
- otherKeys === 1 &&
756
- (hasZoneId || hasZoneName || hasCustomDomainFlag)
757
- ) {
758
- return true;
759
- }
760
- }
761
- return false;
762
- }
763
-
764
- /**
765
- * If account_id has been passed as an empty string, normalise it to undefined.
766
- * This is to workaround older Wrangler v1-era templates that have account_id = '',
767
- * which isn't a valid value anyway
768
- */
769
- function mutateEmptyStringAccountIDValue(
770
- diagnostics: Diagnostics,
771
- rawEnv: RawEnvironment
772
- ) {
773
- if (rawEnv.account_id === "") {
774
- diagnostics.warnings.push(
775
- `The "account_id" field in your configuration is an empty string and will be ignored.\n` +
776
- `Please remove the "account_id" field from your configuration.`
777
- );
778
- rawEnv.account_id = undefined;
779
- }
780
- return rawEnv;
781
- }
782
-
783
- /**
784
- * Normalize empty string to `undefined` by mutating rawEnv.route value.
785
- * As part of backward compatibility with Wrangler v1 converting empty string to `undefined`
786
- */
787
- function mutateEmptyStringRouteValue(
788
- diagnostics: Diagnostics,
789
- rawEnv: RawEnvironment
790
- ): RawEnvironment {
791
- if (rawEnv["route"] === "") {
792
- diagnostics.warnings.push(
793
- `The "route" field in your configuration is an empty string and will be ignored.\n` +
794
- `Please remove the "route" field from your configuration.`
795
- );
796
- rawEnv["route"] = undefined;
797
- }
798
-
799
- return rawEnv;
800
- }
801
-
802
- /**
803
- * Validate that the field is a route.
804
- */
805
- const isRoute: ValidatorFn = (diagnostics, field, value) => {
806
- if (value !== undefined && !isValidRouteValue(value)) {
807
- diagnostics.errors.push(
808
- `Expected "${field}" to be either a string, or an object with shape { pattern, custom_domain, zone_id | zone_name }, but got ${JSON.stringify(
809
- value
810
- )}.`
811
- );
812
- return false;
813
- }
814
- return true;
815
- };
816
-
817
- /**
818
- * Validate that the field is an array of routes.
819
- */
820
- const isRouteArray: ValidatorFn = (diagnostics, field, value) => {
821
- if (value === undefined) {
822
- return true;
823
- }
824
- if (!Array.isArray(value)) {
825
- diagnostics.errors.push(
826
- `Expected "${field}" to be an array but got ${JSON.stringify(value)}.`
827
- );
828
- return false;
829
- }
830
- const invalidRoutes = [];
831
- for (const item of value) {
832
- if (!isValidRouteValue(item)) {
833
- invalidRoutes.push(item);
834
- }
835
- }
836
- if (invalidRoutes.length > 0) {
837
- diagnostics.errors.push(
838
- `Expected "${field}" to be an array of either strings or objects with the shape { pattern, custom_domain, zone_id | zone_name }, but these weren't valid: ${JSON.stringify(
839
- invalidRoutes,
840
- null,
841
- 2
842
- )}.`
843
- );
844
- }
845
- return invalidRoutes.length === 0;
846
- };
847
-
848
- function normalizeAndValidateRoute(
849
- diagnostics: Diagnostics,
850
- topLevelEnv: Environment | undefined,
851
- rawEnv: RawEnvironment
852
- ): Config["route"] {
853
- return inheritable(
854
- diagnostics,
855
- topLevelEnv,
856
- mutateEmptyStringRouteValue(diagnostics, rawEnv),
857
- "route",
858
- isRoute,
859
- undefined
860
- );
861
- }
862
-
863
- function validateRoutes(
864
- diagnostics: Diagnostics,
865
- topLevelEnv: Environment | undefined,
866
- rawEnv: RawEnvironment
867
- ): Config["routes"] {
868
- return inheritable(
869
- diagnostics,
870
- topLevelEnv,
871
- rawEnv,
872
- "routes",
873
- all(isRouteArray, isMutuallyExclusiveWith(rawEnv, "route")),
874
- undefined
875
- );
876
- }
877
-
878
- function normalizeAndValidatePlacement(
879
- diagnostics: Diagnostics,
880
- topLevelEnv: Environment | undefined,
881
- rawEnv: RawEnvironment
882
- ): Config["placement"] {
883
- if (rawEnv.placement) {
884
- validateRequiredProperty(
885
- diagnostics,
886
- "placement",
887
- "mode",
888
- rawEnv.placement.mode,
889
- "string",
890
- ["off", "smart"]
891
- );
892
- }
893
-
894
- return inheritable(
895
- diagnostics,
896
- topLevelEnv,
897
- rawEnv,
898
- "placement",
899
- () => true,
900
- undefined
901
- );
902
- }
903
-
904
- function validateTailConsumer(
905
- diagnostics: Diagnostics,
906
- field: string,
907
- value: TailConsumer
908
- ) {
909
- if (typeof value !== "object" || value === null) {
910
- diagnostics.errors.push(
911
- `"${field}" should be an object but got ${JSON.stringify(value)}.`
912
- );
913
- return false;
914
- }
915
-
916
- let isValid = true;
917
-
918
- isValid =
919
- isValid &&
920
- validateRequiredProperty(
921
- diagnostics,
922
- field,
923
- "service",
924
- value.service,
925
- "string"
926
- );
927
- isValid =
928
- isValid &&
929
- validateOptionalProperty(
930
- diagnostics,
931
- field,
932
- "environment",
933
- value.environment,
934
- "string"
935
- );
936
-
937
- return isValid;
938
- }
939
-
940
- const validateTailConsumers: ValidatorFn = (diagnostics, field, value) => {
941
- if (!value) {
942
- return true;
943
- }
944
- if (!Array.isArray(value)) {
945
- diagnostics.errors.push(
946
- `Expected "${field}" to be an array but got ${JSON.stringify(value)}.`
947
- );
948
- return false;
949
- }
950
-
951
- let isValid = true;
952
- for (let i = 0; i < value.length; i++) {
953
- isValid =
954
- validateTailConsumer(diagnostics, `${field}[${i}]`, value[i]) && isValid;
955
- }
956
-
957
- return isValid;
958
- };
959
-
960
- /**
961
- * Validate top-level environment configuration and return the normalized values.
962
- */
963
- function normalizeAndValidateEnvironment(
964
- diagnostics: Diagnostics,
965
- configPath: string | undefined,
966
- topLevelEnv: RawEnvironment
967
- ): Environment;
968
- /**
969
- * Validate the named environment configuration and return the normalized values.
970
- */
971
- function normalizeAndValidateEnvironment(
972
- diagnostics: Diagnostics,
973
- configPath: string | undefined,
974
- rawEnv: RawEnvironment,
975
- envName: string,
976
- topLevelEnv: Environment,
977
- isLegacyEnv: boolean,
978
- rawConfig: RawConfig
979
- ): Environment;
980
- function normalizeAndValidateEnvironment(
981
- diagnostics: Diagnostics,
982
- configPath: string | undefined,
983
- rawEnv: RawEnvironment,
984
- envName = "top level",
985
- topLevelEnv?: Environment | undefined,
986
- isLegacyEnv?: boolean,
987
- rawConfig?: RawConfig | undefined
988
- ): Environment {
989
- deprecated(
990
- diagnostics,
991
- rawEnv,
992
- "kv-namespaces",
993
- `The "kv-namespaces" field is no longer supported, please rename to "kv_namespaces"`,
994
- true
995
- );
996
- deprecated(
997
- diagnostics,
998
- rawEnv,
999
- "zone_id",
1000
- "This is unnecessary since we can deduce this from routes directly.",
1001
- false // We need to leave this in-place for the moment since `route` commands might use it.
1002
- );
1003
-
1004
- // The field "experimental_services" doesn't exist anymore in the config, but we still want to error about any older usage.
1005
-
1006
- deprecated(
1007
- diagnostics,
1008
- rawEnv,
1009
- "experimental_services",
1010
- `The "experimental_services" field is no longer supported. Simply rename the [experimental_services] field to [services].`,
1011
- true
1012
- );
1013
-
1014
- experimental(diagnostics, rawEnv, "unsafe");
1015
- experimental(diagnostics, rawEnv, "services");
1016
-
1017
- const route = normalizeAndValidateRoute(diagnostics, topLevelEnv, rawEnv);
1018
-
1019
- const account_id = inheritableInLegacyEnvironments(
1020
- diagnostics,
1021
- isLegacyEnv,
1022
- topLevelEnv,
1023
- mutateEmptyStringAccountIDValue(diagnostics, rawEnv),
1024
- "account_id",
1025
- isString,
1026
- undefined,
1027
- undefined
1028
- );
1029
-
1030
- const routes = validateRoutes(diagnostics, topLevelEnv, rawEnv);
1031
-
1032
- const workers_dev = inheritable(
1033
- diagnostics,
1034
- topLevelEnv,
1035
- rawEnv,
1036
- "workers_dev",
1037
- isBoolean,
1038
- undefined
1039
- );
1040
-
1041
- const { deprecatedUpload, ...build } = normalizeAndValidateBuild(
1042
- diagnostics,
1043
- rawEnv,
1044
- rawEnv.build ?? topLevelEnv?.build ?? {},
1045
- configPath
1046
- );
1047
-
1048
- const environment: Environment = {
1049
- // Inherited fields
1050
- account_id,
1051
- compatibility_date: inheritable(
1052
- diagnostics,
1053
- topLevelEnv,
1054
- rawEnv,
1055
- "compatibility_date",
1056
- isString,
1057
- undefined
1058
- ),
1059
- compatibility_flags: inheritable(
1060
- diagnostics,
1061
- topLevelEnv,
1062
- rawEnv,
1063
- "compatibility_flags",
1064
- isStringArray,
1065
- []
1066
- ),
1067
- jsx_factory: inheritable(
1068
- diagnostics,
1069
- topLevelEnv,
1070
- rawEnv,
1071
- "jsx_factory",
1072
- isString,
1073
- "React.createElement"
1074
- ),
1075
- jsx_fragment: inheritable(
1076
- diagnostics,
1077
- topLevelEnv,
1078
- rawEnv,
1079
- "jsx_fragment",
1080
- isString,
1081
- "React.Fragment"
1082
- ),
1083
- tsconfig: validateAndNormalizeTsconfig(
1084
- diagnostics,
1085
- topLevelEnv,
1086
- rawEnv,
1087
- configPath
1088
- ),
1089
- rules: validateAndNormalizeRules(
1090
- diagnostics,
1091
- topLevelEnv,
1092
- rawEnv,
1093
- deprecatedUpload?.rules,
1094
- envName
1095
- ),
1096
- name: inheritableInLegacyEnvironments(
1097
- diagnostics,
1098
- isLegacyEnv,
1099
- topLevelEnv,
1100
- rawEnv,
1101
- "name",
1102
- isValidName,
1103
- appendEnvName(envName),
1104
- undefined
1105
- ),
1106
- main: normalizeAndValidateMainField(
1107
- configPath,
1108
- inheritable(
1109
- diagnostics,
1110
- topLevelEnv,
1111
- rawEnv,
1112
- "main",
1113
- isString,
1114
- undefined
1115
- ),
1116
- deprecatedUpload
1117
- ),
1118
- base_dir: normalizeAndValidateBaseDirField(
1119
- configPath,
1120
- inheritable(
1121
- diagnostics,
1122
- topLevelEnv,
1123
- rawEnv,
1124
- "base_dir",
1125
- isString,
1126
- undefined
1127
- )
1128
- ),
1129
- route,
1130
- routes,
1131
- triggers: inheritable(
1132
- diagnostics,
1133
- topLevelEnv,
1134
- rawEnv,
1135
- "triggers",
1136
- isObjectWith("crons"),
1137
- { crons: [] }
1138
- ),
1139
- usage_model: inheritable(
1140
- diagnostics,
1141
- topLevelEnv,
1142
- rawEnv,
1143
- "usage_model",
1144
- isOneOf("bundled", "unbound"),
1145
- undefined
1146
- ),
1147
- placement: normalizeAndValidatePlacement(diagnostics, topLevelEnv, rawEnv),
1148
- build,
1149
- workers_dev,
1150
- // Not inherited fields
1151
- vars: notInheritable(
1152
- diagnostics,
1153
- topLevelEnv,
1154
- rawConfig,
1155
- rawEnv,
1156
- envName,
1157
- "vars",
1158
- validateVars(envName),
1159
- {}
1160
- ),
1161
- define: notInheritable(
1162
- diagnostics,
1163
- topLevelEnv,
1164
- rawConfig,
1165
- rawEnv,
1166
- envName,
1167
- "define",
1168
- validateDefines(envName),
1169
- {}
1170
- ),
1171
- durable_objects: notInheritable(
1172
- diagnostics,
1173
- topLevelEnv,
1174
- rawConfig,
1175
- rawEnv,
1176
- envName,
1177
- "durable_objects",
1178
- validateBindingsProperty(envName, validateDurableObjectBinding),
1179
- {
1180
- bindings: [],
1181
- }
1182
- ),
1183
- kv_namespaces: notInheritable(
1184
- diagnostics,
1185
- topLevelEnv,
1186
- rawConfig,
1187
- rawEnv,
1188
- envName,
1189
- "kv_namespaces",
1190
- validateBindingArray(envName, validateKVBinding),
1191
- []
1192
- ),
1193
- send_email: notInheritable(
1194
- diagnostics,
1195
- topLevelEnv,
1196
- rawConfig,
1197
- rawEnv,
1198
- envName,
1199
- "send_email",
1200
- validateBindingArray(envName, validateSendEmailBinding),
1201
- []
1202
- ),
1203
- queues: notInheritable(
1204
- diagnostics,
1205
- topLevelEnv,
1206
- rawConfig,
1207
- rawEnv,
1208
- envName,
1209
- "queues",
1210
- validateQueues(envName),
1211
- { producers: [], consumers: [] }
1212
- ),
1213
- r2_buckets: notInheritable(
1214
- diagnostics,
1215
- topLevelEnv,
1216
- rawConfig,
1217
- rawEnv,
1218
- envName,
1219
- "r2_buckets",
1220
- validateBindingArray(envName, validateR2Binding),
1221
- []
1222
- ),
1223
- d1_databases: notInheritable(
1224
- diagnostics,
1225
- topLevelEnv,
1226
- rawConfig,
1227
- rawEnv,
1228
- envName,
1229
- "d1_databases",
1230
- validateBindingArray(envName, validateD1Binding),
1231
- []
1232
- ),
1233
- constellation: notInheritable(
1234
- diagnostics,
1235
- topLevelEnv,
1236
- rawConfig,
1237
- rawEnv,
1238
- envName,
1239
- "constellation",
1240
- validateBindingArray(envName, validateConstellationBinding),
1241
- []
1242
- ),
1243
- services: notInheritable(
1244
- diagnostics,
1245
- topLevelEnv,
1246
- rawConfig,
1247
- rawEnv,
1248
- envName,
1249
- "services",
1250
- validateBindingArray(envName, validateServiceBinding),
1251
- []
1252
- ),
1253
- analytics_engine_datasets: notInheritable(
1254
- diagnostics,
1255
- topLevelEnv,
1256
- rawConfig,
1257
- rawEnv,
1258
- envName,
1259
- "analytics_engine_datasets",
1260
- validateBindingArray(envName, validateAnalyticsEngineBinding),
1261
- []
1262
- ),
1263
- dispatch_namespaces: notInheritable(
1264
- diagnostics,
1265
- topLevelEnv,
1266
- rawConfig,
1267
- rawEnv,
1268
- envName,
1269
- "dispatch_namespaces",
1270
- validateBindingArray(envName, validateWorkerNamespaceBinding),
1271
- []
1272
- ),
1273
- mtls_certificates: notInheritable(
1274
- diagnostics,
1275
- topLevelEnv,
1276
- rawConfig,
1277
- rawEnv,
1278
- envName,
1279
- "mtls_certificates",
1280
- validateBindingArray(envName, validateMTlsCertificateBinding),
1281
- []
1282
- ),
1283
- tail_consumers: notInheritable(
1284
- diagnostics,
1285
- topLevelEnv,
1286
- rawConfig,
1287
- rawEnv,
1288
- envName,
1289
- "tail_consumers",
1290
- validateTailConsumers,
1291
- undefined
1292
- ),
1293
- logfwdr: inheritable(
1294
- diagnostics,
1295
- topLevelEnv,
1296
- rawEnv,
1297
- "logfwdr",
1298
- validateBindingsProperty(envName, validateCflogfwdrBinding),
1299
- {
1300
- schema: undefined,
1301
- bindings: [],
1302
- }
1303
- ),
1304
- unsafe: notInheritable(
1305
- diagnostics,
1306
- topLevelEnv,
1307
- rawConfig,
1308
- rawEnv,
1309
- envName,
1310
- "unsafe",
1311
- validateUnsafeSettings(envName),
1312
- {}
1313
- ),
1314
- browser: notInheritable(
1315
- diagnostics,
1316
- topLevelEnv,
1317
- rawConfig,
1318
- rawEnv,
1319
- envName,
1320
- "browser",
1321
- validateBrowserBinding(envName),
1322
- undefined
1323
- ),
1324
- zone_id: rawEnv.zone_id,
1325
- no_bundle: inheritable(
1326
- diagnostics,
1327
- topLevelEnv,
1328
- rawEnv,
1329
- "no_bundle",
1330
- isBoolean,
1331
- undefined
1332
- ),
1333
- minify: inheritable(
1334
- diagnostics,
1335
- topLevelEnv,
1336
- rawEnv,
1337
- "minify",
1338
- isBoolean,
1339
- undefined
1340
- ),
1341
- node_compat: inheritable(
1342
- diagnostics,
1343
- topLevelEnv,
1344
- rawEnv,
1345
- "node_compat",
1346
- isBoolean,
1347
- undefined
1348
- ),
1349
- first_party_worker: inheritable(
1350
- diagnostics,
1351
- topLevelEnv,
1352
- rawEnv,
1353
- "first_party_worker",
1354
- isBoolean,
1355
- undefined
1356
- ),
1357
- logpush: inheritable(
1358
- diagnostics,
1359
- topLevelEnv,
1360
- rawEnv,
1361
- "logpush",
1362
- isBoolean,
1363
- undefined
1364
- ),
1365
- };
1366
-
1367
- return environment;
1368
- }
1369
-
1370
- function validateAndNormalizeTsconfig(
1371
- diagnostics: Diagnostics,
1372
- topLevelEnv: Environment | undefined,
1373
- rawEnv: RawEnvironment,
1374
- configPath: string | undefined
1375
- ) {
1376
- const tsconfig = inheritable(
1377
- diagnostics,
1378
- topLevelEnv,
1379
- rawEnv,
1380
- "tsconfig",
1381
- isString,
1382
- undefined
1383
- );
1384
-
1385
- return configPath && tsconfig
1386
- ? path.relative(
1387
- process.cwd(),
1388
- path.join(path.dirname(configPath), tsconfig)
1389
- )
1390
- : tsconfig;
1391
- }
1392
-
1393
- const validateAndNormalizeRules = (
1394
- diagnostics: Diagnostics,
1395
- topLevelEnv: Environment | undefined,
1396
- rawEnv: RawEnvironment,
1397
- deprecatedRules: Rule[] | undefined,
1398
- envName: string
1399
- ): Rule[] => {
1400
- if (topLevelEnv === undefined) {
1401
- // Only create errors/warnings for the top-level environment
1402
- if (rawEnv.rules && deprecatedRules) {
1403
- diagnostics.errors.push(
1404
- `You cannot configure both [rules] and [build.upload.rules] in your wrangler.toml. Delete the \`build.upload\` section.`
1405
- );
1406
- } else if (deprecatedRules) {
1407
- diagnostics.warnings.push(
1408
- `Deprecation: The \`build.upload.rules\` config field is no longer used, the rules should be specified via the \`rules\` config field. Delete the \`build.upload\` field from the configuration file, and add this:\n` +
1409
- "```\n" +
1410
- TOML.stringify({ rules: deprecatedRules }) +
1411
- "```"
1412
- );
1413
- }
1414
- }
1415
-
1416
- return inheritable(
1417
- diagnostics,
1418
- topLevelEnv,
1419
- rawEnv,
1420
- "rules",
1421
- validateRules(envName),
1422
- deprecatedRules ?? []
1423
- );
1424
- };
1425
-
1426
- const validateRules =
1427
- (envName: string): ValidatorFn =>
1428
- (diagnostics, field, envValue, config) => {
1429
- if (!envValue) {
1430
- return true;
1431
- }
1432
- const fieldPath =
1433
- config === undefined ? `${field}` : `env.${envName}.${field}`;
1434
- if (!Array.isArray(envValue)) {
1435
- diagnostics.errors.push(
1436
- `The field "${fieldPath}" should be an array but got ${JSON.stringify(
1437
- envValue
1438
- )}.`
1439
- );
1440
- return false;
1441
- }
1442
-
1443
- let isValid = true;
1444
- for (let i = 0; i < envValue.length; i++) {
1445
- isValid =
1446
- validateRule(diagnostics, `${fieldPath}[${i}]`, envValue[i], config) &&
1447
- isValid;
1448
- }
1449
- return isValid;
1450
- };
1451
-
1452
- const validateRule: ValidatorFn = (diagnostics, field, value) => {
1453
- if (typeof value !== "object" || value === null) {
1454
- diagnostics.errors.push(
1455
- `"${field}" should be an object but got ${JSON.stringify(value)}.`
1456
- );
1457
- return false;
1458
- }
1459
- // Rules must have a type string and glob string array, and optionally a fallthrough boolean.
1460
- let isValid = true;
1461
- const rule = value as Rule;
1462
-
1463
- if (
1464
- !isRequiredProperty(rule, "type", "string", [
1465
- "ESModule",
1466
- "CommonJS",
1467
- "CompiledWasm",
1468
- "Text",
1469
- "Data",
1470
- ])
1471
- ) {
1472
- diagnostics.errors.push(
1473
- `bindings should have a string "type" field, which contains one of "ESModule", "CommonJS", "CompiledWasm", "Text", or "Data".`
1474
- );
1475
- isValid = false;
1476
- }
1477
-
1478
- isValid =
1479
- validateTypedArray(diagnostics, `${field}.globs`, rule.globs, "string") &&
1480
- isValid;
1481
-
1482
- if (!isOptionalProperty(rule, "fallthrough", "boolean")) {
1483
- diagnostics.errors.push(
1484
- `the field "fallthrough", when present, should be a boolean.`
1485
- );
1486
- isValid = false;
1487
- }
1488
-
1489
- return isValid;
1490
- };
1491
-
1492
- const validateDefines =
1493
- (envName: string): ValidatorFn =>
1494
- (diagnostics, field, value, config) => {
1495
- let isValid = true;
1496
- const fieldPath =
1497
- config === undefined ? `${field}` : `env.${envName}.${field}`;
1498
-
1499
- if (typeof value === "object" && value !== null) {
1500
- for (const varName in value) {
1501
- // some casting here to appease typescript
1502
- // even though the value might not match the type
1503
- if (typeof (value as Record<string, string>)[varName] !== "string") {
1504
- diagnostics.errors.push(
1505
- `The field "${fieldPath}.${varName}" should be a string but got ${JSON.stringify(
1506
- (value as Record<string, string>)[varName]
1507
- )}.`
1508
- );
1509
- isValid = false;
1510
- }
1511
- }
1512
- } else {
1513
- if (value !== undefined) {
1514
- diagnostics.errors.push(
1515
- `The field "${fieldPath}" should be an object but got ${JSON.stringify(
1516
- value
1517
- )}.\n`
1518
- );
1519
- isValid = false;
1520
- }
1521
- }
1522
-
1523
- const configDefines = Object.keys(config?.define ?? {});
1524
-
1525
- // If there are no top level vars then there is nothing to do here.
1526
- if (configDefines.length > 0) {
1527
- if (typeof value === "object" && value !== null) {
1528
- const configEnvDefines = config === undefined ? [] : Object.keys(value);
1529
-
1530
- for (const varName of configDefines) {
1531
- if (!(varName in value)) {
1532
- diagnostics.warnings.push(
1533
- `"define.${varName}" exists at the top level, but not on "${fieldPath}".\n` +
1534
- `This is not what you probably want, since "define" configuration is not inherited by environments.\n` +
1535
- `Please add "define.${varName}" to "env.${envName}".`
1536
- );
1537
- }
1538
- }
1539
- for (const varName of configEnvDefines) {
1540
- if (!configDefines.includes(varName)) {
1541
- diagnostics.warnings.push(
1542
- `"${varName}" exists on "env.${envName}", but not on the top level.\n` +
1543
- `This is not what you probably want, since "define" configuration within environments can only override existing top level "define" configuration\n` +
1544
- `Please remove "${fieldPath}.${varName}", or add "define.${varName}".`
1545
- );
1546
- }
1547
- }
1548
- }
1549
- }
1550
-
1551
- return isValid;
1552
- };
1553
-
1554
- const validateVars =
1555
- (envName: string): ValidatorFn =>
1556
- (diagnostics, field, value, config) => {
1557
- let isValid = true;
1558
- const fieldPath =
1559
- config === undefined ? `${field}` : `env.${envName}.${field}`;
1560
- const configVars = Object.keys(config?.vars ?? {});
1561
- // If there are no top level vars then there is nothing to do here.
1562
- if (configVars.length > 0) {
1563
- if (typeof value !== "object" || value === null) {
1564
- diagnostics.errors.push(
1565
- `The field "${fieldPath}" should be an object but got ${JSON.stringify(
1566
- value
1567
- )}.\n`
1568
- );
1569
- isValid = false;
1570
- } else {
1571
- for (const varName of configVars) {
1572
- if (!(varName in value)) {
1573
- diagnostics.warnings.push(
1574
- `"vars.${varName}" exists at the top level, but not on "${fieldPath}".\n` +
1575
- `This is not what you probably want, since "vars" configuration is not inherited by environments.\n` +
1576
- `Please add "vars.${varName}" to "env.${envName}".`
1577
- );
1578
- }
1579
- }
1580
- }
1581
- }
1582
- return isValid;
1583
- };
1584
-
1585
- const validateBindingsProperty =
1586
- (envName: string, validateBinding: ValidatorFn): ValidatorFn =>
1587
- (diagnostics, field, value, config) => {
1588
- let isValid = true;
1589
- const fieldPath =
1590
- config === undefined ? `${field}` : `env.${envName}.${field}`;
1591
-
1592
- if (value !== undefined) {
1593
- // Check the validity of the `value` as a bindings container.
1594
- if (typeof value !== "object" || value === null || Array.isArray(value)) {
1595
- diagnostics.errors.push(
1596
- `The field "${fieldPath}" should be an object but got ${JSON.stringify(
1597
- value
1598
- )}.`
1599
- );
1600
- isValid = false;
1601
- } else if (!hasProperty(value, "bindings")) {
1602
- diagnostics.errors.push(
1603
- `The field "${fieldPath}" is missing the required "bindings" property.`
1604
- );
1605
- isValid = false;
1606
- } else if (!Array.isArray(value.bindings)) {
1607
- diagnostics.errors.push(
1608
- `The field "${fieldPath}.bindings" should be an array but got ${JSON.stringify(
1609
- value.bindings
1610
- )}.`
1611
- );
1612
- isValid = false;
1613
- } else {
1614
- for (let i = 0; i < value.bindings.length; i++) {
1615
- const binding = value.bindings[i];
1616
- const bindingDiagnostics = new Diagnostics(
1617
- `"${fieldPath}.bindings[${i}]": ${JSON.stringify(binding)}`
1618
- );
1619
- isValid =
1620
- validateBinding(
1621
- bindingDiagnostics,
1622
- `${fieldPath}.bindings[${i}]`,
1623
- binding,
1624
- config
1625
- ) && isValid;
1626
- diagnostics.addChild(bindingDiagnostics);
1627
- }
1628
- }
1629
-
1630
- const configBindingNames = getBindingNames(
1631
- config?.[field as keyof Environment]
1632
- );
1633
- if (isValid && configBindingNames.length > 0) {
1634
- // If there are top level bindings then check that they all appear in the environment.
1635
- const envBindingNames = new Set(getBindingNames(value));
1636
- const missingBindings = configBindingNames.filter(
1637
- (name) => !envBindingNames.has(name)
1638
- );
1639
- if (missingBindings.length > 0) {
1640
- diagnostics.warnings.push(
1641
- `The following bindings are at the top level, but not on "env.${envName}".\n` +
1642
- `This is not what you probably want, since "${field}" configuration is not inherited by environments.\n` +
1643
- `Please add a binding for each to "${fieldPath}.bindings":\n` +
1644
- missingBindings.map((name) => `- ${name}`).join("\n")
1645
- );
1646
- }
1647
- }
1648
- }
1649
- return isValid;
1650
- };
1651
-
1652
- const validateUnsafeSettings =
1653
- (envName: string): ValidatorFn =>
1654
- (diagnostics, field, value, config) => {
1655
- const fieldPath =
1656
- config === undefined ? `${field}` : `env.${envName}.${field}`;
1657
-
1658
- if (typeof value !== "object" || value === null || Array.isArray(value)) {
1659
- diagnostics.errors.push(
1660
- `The field "${fieldPath}" should be an object but got ${JSON.stringify(
1661
- value
1662
- )}.`
1663
- );
1664
- return false;
1665
- }
1666
-
1667
- // At least one of bindings and metadata must exist
1668
- if (!hasProperty(value, "bindings") && !hasProperty(value, "metadata")) {
1669
- diagnostics.errors.push(
1670
- `The field "${fieldPath}" should contain at least one of "bindings" or "metadata" properties but got ${JSON.stringify(
1671
- value
1672
- )}.`
1673
- );
1674
- return false;
1675
- }
1676
-
1677
- // unsafe.bindings
1678
- if (hasProperty(value, "bindings") && value.bindings !== undefined) {
1679
- const validateBindingsFn = validateBindingsProperty(
1680
- envName,
1681
- validateUnsafeBinding
1682
- );
1683
- const valid = validateBindingsFn(diagnostics, field, value, config);
1684
- if (!valid) {
1685
- return false;
1686
- }
1687
- }
1688
-
1689
- // unsafe.metadata
1690
- if (
1691
- hasProperty(value, "metadata") &&
1692
- value.metadata !== undefined &&
1693
- (typeof value.metadata !== "object" ||
1694
- value.metadata === null ||
1695
- Array.isArray(value.metadata))
1696
- ) {
1697
- diagnostics.errors.push(
1698
- `The field "${fieldPath}.metadata" should be an object but got ${JSON.stringify(
1699
- value.metadata
1700
- )}.`
1701
- );
1702
- return false;
1703
- }
1704
-
1705
- return true;
1706
- };
1707
-
1708
- /**
1709
- * Check that the given field is a valid "durable_object" binding object.
1710
- */
1711
- const validateDurableObjectBinding: ValidatorFn = (
1712
- diagnostics,
1713
- field,
1714
- value
1715
- ) => {
1716
- if (typeof value !== "object" || value === null) {
1717
- diagnostics.errors.push(
1718
- `Expected "${field}" to be an object but got ${JSON.stringify(value)}`
1719
- );
1720
- return false;
1721
- }
1722
-
1723
- // Durable Object bindings must have a name and class_name, and optionally a script_name and an environment.
1724
- let isValid = true;
1725
- if (!isRequiredProperty(value, "name", "string")) {
1726
- diagnostics.errors.push(`binding should have a string "name" field.`);
1727
- isValid = false;
1728
- }
1729
- if (!isRequiredProperty(value, "class_name", "string")) {
1730
- diagnostics.errors.push(`binding should have a string "class_name" field.`);
1731
- isValid = false;
1732
- }
1733
- if (!isOptionalProperty(value, "script_name", "string")) {
1734
- diagnostics.errors.push(
1735
- `the field "script_name", when present, should be a string.`
1736
- );
1737
- isValid = false;
1738
- }
1739
- // environment requires a script_name
1740
- if (!isOptionalProperty(value, "environment", "string")) {
1741
- diagnostics.errors.push(
1742
- `the field "environment", when present, should be a string.`
1743
- );
1744
- isValid = false;
1745
- }
1746
-
1747
- if ("environment" in value && !("script_name" in value)) {
1748
- diagnostics.errors.push(
1749
- `binding should have a "script_name" field if "environment" is present.`
1750
- );
1751
- isValid = false;
1752
- }
1753
-
1754
- return isValid;
1755
- };
1756
-
1757
- const validateCflogfwdrBinding: ValidatorFn = (diagnostics, field, value) => {
1758
- if (typeof value !== "object" || value === null) {
1759
- diagnostics.errors.push(
1760
- `Expected "${field}" to be an object but got ${JSON.stringify(value)}`
1761
- );
1762
- return false;
1763
- }
1764
-
1765
- let isValid = true;
1766
- if (!isRequiredProperty(value, "name", "string")) {
1767
- diagnostics.errors.push(`binding should have a string "name" field.`);
1768
- isValid = false;
1769
- }
1770
-
1771
- if (!isRequiredProperty(value, "destination", "string")) {
1772
- diagnostics.errors.push(
1773
- `binding should have a string "destination" field.`
1774
- );
1775
- isValid = false;
1776
- }
1777
-
1778
- return isValid;
1779
- };
1780
-
1781
- const validateBrowserBinding =
1782
- (envName: string): ValidatorFn =>
1783
- (diagnostics, field, value, config) => {
1784
- const fieldPath =
1785
- config === undefined ? `${field}` : `env.${envName}.${field}`;
1786
-
1787
- if (typeof value !== "object" || value === null || Array.isArray(value)) {
1788
- diagnostics.errors.push(
1789
- `The field "${fieldPath}" should be an object but got ${JSON.stringify(
1790
- value
1791
- )}.`
1792
- );
1793
- return false;
1794
- }
1795
-
1796
- let isValid = true;
1797
- if (!isRequiredProperty(value, "binding", "string")) {
1798
- diagnostics.errors.push(`binding should have a string "binding" field.`);
1799
- isValid = false;
1800
- }
1801
-
1802
- return isValid;
1803
- };
1804
-
1805
- /**
1806
- * Check that the given field is a valid "unsafe" binding object.
1807
- *
1808
- * TODO: further validation of known unsafe bindings.
1809
- */
1810
- const validateUnsafeBinding: ValidatorFn = (diagnostics, field, value) => {
1811
- if (typeof value !== "object" || value === null) {
1812
- diagnostics.errors.push(
1813
- `Expected ${field} to be an object but got ${JSON.stringify(value)}.`
1814
- );
1815
- return false;
1816
- }
1817
-
1818
- let isValid = true;
1819
- // Unsafe bindings must have a name and type.
1820
- if (!isRequiredProperty(value, "name", "string")) {
1821
- diagnostics.errors.push(`binding should have a string "name" field.`);
1822
- isValid = false;
1823
- }
1824
- if (isRequiredProperty(value, "type", "string")) {
1825
- const safeBindings = [
1826
- "plain_text",
1827
- "json",
1828
- "wasm_module",
1829
- "data_blob",
1830
- "text_blob",
1831
- "browser",
1832
- "kv_namespace",
1833
- "durable_object_namespace",
1834
- "d1_database",
1835
- "constellation",
1836
- "r2_bucket",
1837
- "service",
1838
- "logfwdr",
1839
- "mtls_certificate",
1840
- ];
1841
-
1842
- if (safeBindings.includes(value.type)) {
1843
- diagnostics.warnings.push(
1844
- `The binding type "${value.type}" is directly supported by wrangler.\n` +
1845
- `Consider migrating this unsafe binding to a format for '${value.type}' bindings that is supported by wrangler for optimal support.\n` +
1846
- "For more details, see https://developers.cloudflare.com/workers/cli-wrangler/configuration"
1847
- );
1848
- }
1849
- } else {
1850
- diagnostics.errors.push(`binding should have a string "type" field.`);
1851
- isValid = false;
1852
- }
1853
- return isValid;
1854
- };
1855
-
1856
- /**
1857
- * Check that the given environment field is a valid array of bindings.
1858
- */
1859
- const validateBindingArray =
1860
- (envName: string, validateBinding: ValidatorFn): ValidatorFn =>
1861
- (diagnostics, field, envValue, config) => {
1862
- if (envValue === undefined) {
1863
- return true;
1864
- }
1865
-
1866
- const fieldPath =
1867
- config === undefined ? `${field}` : `env.${envName}.${field}`;
1868
- if (!Array.isArray(envValue)) {
1869
- diagnostics.errors.push(
1870
- `The field "${fieldPath}" should be an array but got ${JSON.stringify(
1871
- envValue
1872
- )}.`
1873
- );
1874
- return false;
1875
- }
1876
-
1877
- let isValid = true;
1878
- for (let i = 0; i < envValue.length; i++) {
1879
- isValid =
1880
- validateBinding(
1881
- diagnostics,
1882
- `${fieldPath}[${i}]`,
1883
- envValue[i],
1884
- config
1885
- ) && isValid;
1886
- }
1887
- const configValue = config?.[field as keyof Environment] as {
1888
- binding: unknown;
1889
- }[];
1890
- if (Array.isArray(configValue)) {
1891
- const configBindingNames = configValue.map((value) => value.binding);
1892
- // If there are no top level bindings then there is nothing to do here.
1893
- if (configBindingNames.length > 0) {
1894
- const envBindingNames = new Set(envValue.map((value) => value.binding));
1895
- for (const configBindingName of configBindingNames) {
1896
- if (!envBindingNames.has(configBindingName)) {
1897
- diagnostics.warnings.push(
1898
- `There is a ${field} binding with name "${configBindingName}" at the top level, but not on "env.${envName}".\n` +
1899
- `This is not what you probably want, since "${field}" configuration is not inherited by environments.\n` +
1900
- `Please add a binding for "${configBindingName}" to "env.${envName}.${field}.bindings".`
1901
- );
1902
- }
1903
- }
1904
- }
1905
- }
1906
- return isValid;
1907
- };
1908
-
1909
- const validateKVBinding: ValidatorFn = (diagnostics, field, value) => {
1910
- if (typeof value !== "object" || value === null) {
1911
- diagnostics.errors.push(
1912
- `"kv_namespaces" bindings should be objects, but got ${JSON.stringify(
1913
- value
1914
- )}`
1915
- );
1916
- return false;
1917
- }
1918
- let isValid = true;
1919
- // KV bindings must have a binding and id.
1920
- if (!isRequiredProperty(value, "binding", "string")) {
1921
- diagnostics.errors.push(
1922
- `"${field}" bindings should have a string "binding" field but got ${JSON.stringify(
1923
- value
1924
- )}.`
1925
- );
1926
- isValid = false;
1927
- }
1928
- if (
1929
- !isRequiredProperty(value, "id", "string") ||
1930
- (value as { id: string }).id.length === 0
1931
- ) {
1932
- diagnostics.errors.push(
1933
- `"${field}" bindings should have a string "id" field but got ${JSON.stringify(
1934
- value
1935
- )}.`
1936
- );
1937
- isValid = false;
1938
- }
1939
- if (!isOptionalProperty(value, "preview_id", "string")) {
1940
- diagnostics.errors.push(
1941
- `"${field}" bindings should, optionally, have a string "preview_id" field but got ${JSON.stringify(
1942
- value
1943
- )}.`
1944
- );
1945
- isValid = false;
1946
- }
1947
- return isValid;
1948
- };
1949
-
1950
- const validateSendEmailBinding: ValidatorFn = (diagnostics, field, value) => {
1951
- if (typeof value !== "object" || value === null) {
1952
- diagnostics.errors.push(
1953
- `"send_email" bindings should be objects, but got ${JSON.stringify(
1954
- value
1955
- )}`
1956
- );
1957
- return false;
1958
- }
1959
- let isValid = true;
1960
- // send email bindings must have a name.
1961
- if (!isRequiredProperty(value, "name", "string")) {
1962
- diagnostics.errors.push(
1963
- `"${field}" bindings should have a string "name" field but got ${JSON.stringify(
1964
- value
1965
- )}.`
1966
- );
1967
- isValid = false;
1968
- }
1969
- if (!isOptionalProperty(value, "destination_address", "string")) {
1970
- diagnostics.errors.push(
1971
- `"${field}" bindings should, optionally, have a string "destination_address" field but got ${JSON.stringify(
1972
- value
1973
- )}.`
1974
- );
1975
- isValid = false;
1976
- }
1977
- if (!isOptionalProperty(value, "allowed_destination_addresses", "object")) {
1978
- diagnostics.errors.push(
1979
- `"${field}" bindings should, optionally, have a []string "allowed_destination_addresses" field but got ${JSON.stringify(
1980
- value
1981
- )}.`
1982
- );
1983
- isValid = false;
1984
- }
1985
- if (
1986
- "destination_address" in value &&
1987
- "allowed_destination_addresses" in value
1988
- ) {
1989
- diagnostics.errors.push(
1990
- `"${field}" bindings should have either a "destination_address" or "allowed_destination_addresses" field, but not both.`
1991
- );
1992
- isValid = false;
1993
- }
1994
- return isValid;
1995
- };
1996
-
1997
- const validateQueueBinding: ValidatorFn = (diagnostics, field, value) => {
1998
- if (typeof value !== "object" || value === null) {
1999
- diagnostics.errors.push(
2000
- `"queue" bindings should be objects, but got ${JSON.stringify(value)}`
2001
- );
2002
- return false;
2003
- }
2004
-
2005
- if (
2006
- !validateAdditionalProperties(diagnostics, field, Object.keys(value), [
2007
- "binding",
2008
- "queue",
2009
- ])
2010
- ) {
2011
- return false;
2012
- }
2013
-
2014
- // Queue bindings must have a binding and queue.
2015
- let isValid = true;
2016
- if (!isRequiredProperty(value, "binding", "string")) {
2017
- diagnostics.errors.push(
2018
- `"${field}" bindings should have a string "binding" field but got ${JSON.stringify(
2019
- value
2020
- )}.`
2021
- );
2022
- isValid = false;
2023
- }
2024
- if (
2025
- !isRequiredProperty(value, "queue", "string") ||
2026
- (value as { queue: string }).queue.length === 0
2027
- ) {
2028
- diagnostics.errors.push(
2029
- `"${field}" bindings should have a string "queue" field but got ${JSON.stringify(
2030
- value
2031
- )}.`
2032
- );
2033
- isValid = false;
2034
- }
2035
- return isValid;
2036
- };
2037
-
2038
- const validateR2Binding: ValidatorFn = (diagnostics, field, value) => {
2039
- if (typeof value !== "object" || value === null) {
2040
- diagnostics.errors.push(
2041
- `"r2_buckets" bindings should be objects, but got ${JSON.stringify(
2042
- value
2043
- )}`
2044
- );
2045
- return false;
2046
- }
2047
- let isValid = true;
2048
- // R2 bindings must have a binding and bucket_name.
2049
- if (!isRequiredProperty(value, "binding", "string")) {
2050
- diagnostics.errors.push(
2051
- `"${field}" bindings should have a string "binding" field but got ${JSON.stringify(
2052
- value
2053
- )}.`
2054
- );
2055
- isValid = false;
2056
- }
2057
- if (
2058
- !isRequiredProperty(value, "bucket_name", "string") ||
2059
- (value as { bucket_name: string }).bucket_name.length === 0
2060
- ) {
2061
- diagnostics.errors.push(
2062
- `"${field}" bindings should have a string "bucket_name" field but got ${JSON.stringify(
2063
- value
2064
- )}.`
2065
- );
2066
- isValid = false;
2067
- }
2068
- if (!isOptionalProperty(value, "preview_bucket_name", "string")) {
2069
- diagnostics.errors.push(
2070
- `"${field}" bindings should, optionally, have a string "preview_bucket_name" field but got ${JSON.stringify(
2071
- value
2072
- )}.`
2073
- );
2074
- isValid = false;
2075
- }
2076
- return isValid;
2077
- };
2078
-
2079
- const validateD1Binding: ValidatorFn = (diagnostics, field, value) => {
2080
- if (typeof value !== "object" || value === null) {
2081
- diagnostics.errors.push(
2082
- `"d1_databases" bindings should be objects, but got ${JSON.stringify(
2083
- value
2084
- )}`
2085
- );
2086
- return false;
2087
- }
2088
- let isValid = true;
2089
- // D1 databases must have a binding and either a database_name or database_id.
2090
- if (!isRequiredProperty(value, "binding", "string")) {
2091
- diagnostics.errors.push(
2092
- `"${field}" bindings should have a string "binding" field but got ${JSON.stringify(
2093
- value
2094
- )}.`
2095
- );
2096
- isValid = false;
2097
- }
2098
- if (
2099
- // TODO: allow name only, where we look up the ID dynamically
2100
- // !isOptionalProperty(value, "database_name", "string") &&
2101
- !isRequiredProperty(value, "database_id", "string")
2102
- ) {
2103
- diagnostics.errors.push(
2104
- `"${field}" bindings must have a "database_id" field but got ${JSON.stringify(
2105
- value
2106
- )}.`
2107
- );
2108
- isValid = false;
2109
- }
2110
- if (!isOptionalProperty(value, "preview_database_id", "string")) {
2111
- diagnostics.errors.push(
2112
- `"${field}" bindings should, optionally, have a string "preview_database_id" field but got ${JSON.stringify(
2113
- value
2114
- )}.`
2115
- );
2116
- isValid = false;
2117
- }
2118
- if (isValid && !process.env.NO_D1_WARNING) {
2119
- diagnostics.warnings.push(
2120
- "D1 Bindings are currently in alpha to allow the API to evolve before general availability.\nPlease report any issues to https://github.com/cloudflare/workers-sdk/issues/new/choose\nNote: Run this command with the environment variable NO_D1_WARNING=true to hide this message\n\nFor example: `export NO_D1_WARNING=true && wrangler <YOUR COMMAND HERE>`"
2121
- );
2122
- }
2123
- return isValid;
2124
- };
2125
-
2126
- const validateConstellationBinding: ValidatorFn = (
2127
- diagnostics,
2128
- field,
2129
- value
2130
- ) => {
2131
- if (typeof value !== "object" || value === null) {
2132
- diagnostics.errors.push(
2133
- `"constellation" bindings should be objects, but got ${JSON.stringify(
2134
- value
2135
- )}`
2136
- );
2137
- return false;
2138
- }
2139
- let isValid = true;
2140
- // Constellation bindings must have a binding and a project.
2141
- if (!isRequiredProperty(value, "binding", "string")) {
2142
- diagnostics.errors.push(
2143
- `"${field}" bindings should have a string "binding" field but got ${JSON.stringify(
2144
- value
2145
- )}.`
2146
- );
2147
- isValid = false;
2148
- }
2149
- if (!isRequiredProperty(value, "project_id", "string")) {
2150
- diagnostics.errors.push(
2151
- `"${field}" bindings must have a "project_id" field but got ${JSON.stringify(
2152
- value
2153
- )}.`
2154
- );
2155
- isValid = false;
2156
- }
2157
- if (isValid && getConstellationWarningFromEnv() === undefined) {
2158
- diagnostics.warnings.push(
2159
- "Constellation Bindings are currently in beta to allow the API to evolve before general availability.\nPlease report any issues to https://github.com/cloudflare/workers-sdk/issues/new/choose\nNote: Run this command with the environment variable NO_CONSTELLATION_WARNING=true to hide this message\n\nFor example: `export NO_CONSTELLATION_WARNING=true && wrangler <YOUR COMMAND HERE>`"
2160
- );
2161
- }
2162
- return isValid;
2163
- };
2164
-
2165
- /**
2166
- * Check that bindings whose names might conflict, don't.
2167
- *
2168
- * We don't want to have, for example, a KV namespace named "DATA"
2169
- * and a Durable Object also named "DATA". Then it would be ambiguous
2170
- * what exactly would live at `env.DATA` (or in the case of service-workers,
2171
- * the `DATA` global).
2172
- */
2173
- const validateBindingsHaveUniqueNames = (
2174
- diagnostics: Diagnostics,
2175
- {
2176
- durable_objects,
2177
- kv_namespaces,
2178
- r2_buckets,
2179
- analytics_engine_datasets,
2180
- text_blobs,
2181
- browser,
2182
- unsafe,
2183
- vars,
2184
- define,
2185
- wasm_modules,
2186
- data_blobs,
2187
- }: Partial<Config>
2188
- ): boolean => {
2189
- let hasDuplicates = false;
2190
-
2191
- const bindingsGroupedByType = {
2192
- "Durable Object": getBindingNames(durable_objects),
2193
- "KV Namespace": getBindingNames(kv_namespaces),
2194
- "R2 Bucket": getBindingNames(r2_buckets),
2195
- "Analytics Engine Dataset": getBindingNames(analytics_engine_datasets),
2196
- "Text Blob": getBindingNames(text_blobs),
2197
- Browser: getBindingNames(browser),
2198
- Unsafe: getBindingNames(unsafe),
2199
- "Environment Variable": getBindingNames(vars),
2200
- Definition: getBindingNames(define),
2201
- "WASM Module": getBindingNames(wasm_modules),
2202
- "Data Blob": getBindingNames(data_blobs),
2203
- } as Record<string, string[]>;
2204
-
2205
- const bindingsGroupedByName: Record<string, string[]> = {};
2206
-
2207
- for (const bindingType in bindingsGroupedByType) {
2208
- const bindingNames = bindingsGroupedByType[bindingType];
2209
-
2210
- for (const bindingName of bindingNames) {
2211
- if (!(bindingName in bindingsGroupedByName)) {
2212
- bindingsGroupedByName[bindingName] = [];
2213
- }
2214
-
2215
- bindingsGroupedByName[bindingName].push(bindingType);
2216
- }
2217
- }
2218
-
2219
- for (const bindingName in bindingsGroupedByName) {
2220
- const bindingTypes = bindingsGroupedByName[bindingName];
2221
- if (bindingTypes.length < 2) {
2222
- // there's only one (or zero) binding(s) with this name, which is fine, actually
2223
- continue;
2224
- }
2225
-
2226
- hasDuplicates = true;
2227
-
2228
- // there's two types of duplicates we want to look for:
2229
- // - bindings with the same name of the same type (e.g. two Durable Objects both named "OBJ")
2230
- // - bindings with the same name of different types (a KV namespace and DO both named "DATA")
2231
-
2232
- const sameType = bindingTypes
2233
- // filter once to find duplicate binding types
2234
- .filter((type, i) => bindingTypes.indexOf(type) !== i)
2235
- // filter twice to only get _unique_ duplicate binding types
2236
- .filter(
2237
- (type, i, duplicateBindingTypes) =>
2238
- duplicateBindingTypes.indexOf(type) === i
2239
- );
2240
-
2241
- const differentTypes = bindingTypes.filter(
2242
- (type, i) => bindingTypes.indexOf(type) === i
2243
- );
2244
-
2245
- if (differentTypes.length > 1) {
2246
- // we have multiple different types using the same name
2247
- diagnostics.errors.push(
2248
- `${bindingName} assigned to ${ENGLISH.format(differentTypes)} bindings.`
2249
- );
2250
- }
2251
-
2252
- sameType.forEach((bindingType) => {
2253
- diagnostics.errors.push(
2254
- `${bindingName} assigned to multiple ${bindingType} bindings.`
2255
- );
2256
- });
2257
- }
2258
-
2259
- if (hasDuplicates) {
2260
- const problem =
2261
- "Bindings must have unique names, so that they can all be referenced in the worker.";
2262
- const resolution = "Please change your bindings to have unique names.";
2263
- diagnostics.errors.push(`${problem}\n${resolution}`);
2264
- }
2265
-
2266
- return !hasDuplicates;
2267
- };
2268
-
2269
- const validateServiceBinding: ValidatorFn = (diagnostics, field, value) => {
2270
- if (typeof value !== "object" || value === null) {
2271
- diagnostics.errors.push(
2272
- `"services" bindings should be objects, but got ${JSON.stringify(value)}`
2273
- );
2274
- return false;
2275
- }
2276
- let isValid = true;
2277
- // Service bindings must have a binding, service, and environment.
2278
- if (!isRequiredProperty(value, "binding", "string")) {
2279
- diagnostics.errors.push(
2280
- `"${field}" bindings should have a string "binding" field but got ${JSON.stringify(
2281
- value
2282
- )}.`
2283
- );
2284
- isValid = false;
2285
- }
2286
- if (!isRequiredProperty(value, "service", "string")) {
2287
- diagnostics.errors.push(
2288
- `"${field}" bindings should have a string "service" field but got ${JSON.stringify(
2289
- value
2290
- )}.`
2291
- );
2292
- isValid = false;
2293
- }
2294
- if (!isOptionalProperty(value, "environment", "string")) {
2295
- diagnostics.errors.push(
2296
- `"${field}" bindings should have a string "environment" field but got ${JSON.stringify(
2297
- value
2298
- )}.`
2299
- );
2300
- isValid = false;
2301
- }
2302
- return isValid;
2303
- };
2304
-
2305
- const validateAnalyticsEngineBinding: ValidatorFn = (
2306
- diagnostics,
2307
- field,
2308
- value
2309
- ) => {
2310
- if (typeof value !== "object" || value === null) {
2311
- diagnostics.errors.push(
2312
- `"analytics_engine" bindings should be objects, but got ${JSON.stringify(
2313
- value
2314
- )}`
2315
- );
2316
- return false;
2317
- }
2318
- let isValid = true;
2319
- // Service bindings must have a binding and optional dataset.
2320
- if (!isRequiredProperty(value, "binding", "string")) {
2321
- diagnostics.errors.push(
2322
- `"${field}" bindings should have a string "binding" field but got ${JSON.stringify(
2323
- value
2324
- )}.`
2325
- );
2326
- isValid = false;
2327
- }
2328
- if (
2329
- !isOptionalProperty(value, "dataset", "string") ||
2330
- (value as { dataset: string }).dataset?.length === 0
2331
- ) {
2332
- diagnostics.errors.push(
2333
- `"${field}" bindings should, optionally, have a string "dataset" field but got ${JSON.stringify(
2334
- value
2335
- )}.`
2336
- );
2337
- isValid = false;
2338
- }
2339
- return isValid;
2340
- };
2341
-
2342
- const validateWorkerNamespaceBinding: ValidatorFn = (
2343
- diagnostics,
2344
- field,
2345
- value
2346
- ) => {
2347
- if (typeof value !== "object" || value === null) {
2348
- diagnostics.errors.push(
2349
- `"${field}" binding should be objects, but got ${JSON.stringify(value)}`
2350
- );
2351
- return false;
2352
- }
2353
- let isValid = true;
2354
- // Worker namespace bindings must have a binding, and a namespace.
2355
- if (!isRequiredProperty(value, "binding", "string")) {
2356
- diagnostics.errors.push(
2357
- `"${field}" should have a string "binding" field but got ${JSON.stringify(
2358
- value
2359
- )}.`
2360
- );
2361
- isValid = false;
2362
- }
2363
- if (!isRequiredProperty(value, "namespace", "string")) {
2364
- diagnostics.errors.push(
2365
- `"${field}" should have a string "namespace" field but got ${JSON.stringify(
2366
- value
2367
- )}.`
2368
- );
2369
- isValid = false;
2370
- }
2371
- return isValid;
2372
- };
2373
-
2374
- const validateMTlsCertificateBinding: ValidatorFn = (
2375
- diagnostics,
2376
- field,
2377
- value
2378
- ) => {
2379
- if (typeof value !== "object" || value === null) {
2380
- diagnostics.errors.push(
2381
- `"mtls_certificates" bindings should be objects, but got ${JSON.stringify(
2382
- value
2383
- )}`
2384
- );
2385
- return false;
2386
- }
2387
- let isValid = true;
2388
- if (!isRequiredProperty(value, "binding", "string")) {
2389
- diagnostics.errors.push(
2390
- `"${field}" bindings should have a string "binding" field but got ${JSON.stringify(
2391
- value
2392
- )}.`
2393
- );
2394
- isValid = false;
2395
- }
2396
- if (
2397
- !isRequiredProperty(value, "certificate_id", "string") ||
2398
- (value as { certificate_id: string }).certificate_id.length === 0
2399
- ) {
2400
- diagnostics.errors.push(
2401
- `"${field}" bindings should have a string "certificate_id" field but got ${JSON.stringify(
2402
- value
2403
- )}.`
2404
- );
2405
- isValid = false;
2406
- }
2407
- return isValid;
2408
- };
2409
-
2410
- function validateQueues(envName: string): ValidatorFn {
2411
- return (diagnostics, field, value, config) => {
2412
- const fieldPath =
2413
- config === undefined ? `${field}` : `env.${envName}.${field}`;
2414
-
2415
- if (typeof value !== "object" || Array.isArray(value) || value === null) {
2416
- diagnostics.errors.push(
2417
- `The field "${fieldPath}" should be an object but got ${JSON.stringify(
2418
- value
2419
- )}.`
2420
- );
2421
- return false;
2422
- }
2423
-
2424
- let isValid = true;
2425
- if (
2426
- !validateAdditionalProperties(
2427
- diagnostics,
2428
- fieldPath,
2429
- Object.keys(value),
2430
- ["consumers", "producers"]
2431
- )
2432
- ) {
2433
- isValid = false;
2434
- }
2435
-
2436
- if (hasProperty(value, "consumers")) {
2437
- const consumers = value.consumers;
2438
- if (!Array.isArray(consumers)) {
2439
- diagnostics.errors.push(
2440
- `The field "${fieldPath}.consumers" should be an array but got ${JSON.stringify(
2441
- consumers
2442
- )}.`
2443
- );
2444
- isValid = false;
2445
- }
2446
-
2447
- for (let i = 0; i < consumers.length; i++) {
2448
- const consumer = consumers[i];
2449
- const consumerPath = `${fieldPath}.consumers[${i}]`;
2450
- if (!validateConsumer(diagnostics, consumerPath, consumer, config)) {
2451
- isValid = false;
2452
- }
2453
- }
2454
- }
2455
-
2456
- if (hasProperty(value, "producers")) {
2457
- if (
2458
- !validateBindingArray(envName, validateQueueBinding)(
2459
- diagnostics,
2460
- `${field}.producers`,
2461
- value.producers,
2462
- config
2463
- )
2464
- ) {
2465
- isValid = false;
2466
- }
2467
- }
2468
- return isValid;
2469
- };
2470
- }
2471
-
2472
- const validateConsumer: ValidatorFn = (diagnostics, field, value, _config) => {
2473
- if (typeof value !== "object" || value === null) {
2474
- diagnostics.errors.push(
2475
- `"${field}" should be a objects, but got ${JSON.stringify(value)}`
2476
- );
2477
- return false;
2478
- }
2479
-
2480
- let isValid = true;
2481
- if (
2482
- !validateAdditionalProperties(diagnostics, field, Object.keys(value), [
2483
- "queue",
2484
- "max_batch_size",
2485
- "max_batch_timeout",
2486
- "max_retries",
2487
- "dead_letter_queue",
2488
- "max_concurrency",
2489
- ])
2490
- ) {
2491
- isValid = false;
2492
- }
2493
-
2494
- if (!isRequiredProperty(value, "queue", "string")) {
2495
- diagnostics.errors.push(
2496
- `"${field}" should have a string "queue" field but got ${JSON.stringify(
2497
- value
2498
- )}.`
2499
- );
2500
- }
2501
-
2502
- const options: {
2503
- key: string;
2504
- type: "number" | "string" | "boolean";
2505
- }[] = [
2506
- { key: "max_batch_size", type: "number" },
2507
- { key: "max_batch_timeout", type: "number" },
2508
- { key: "max_retries", type: "number" },
2509
- { key: "dead_letter_queue", type: "string" },
2510
- { key: "max_concurrency", type: "number" },
2511
- ];
2512
- for (const optionalOpt of options) {
2513
- if (!isOptionalProperty(value, optionalOpt.key, optionalOpt.type)) {
2514
- diagnostics.errors.push(
2515
- `"${field}" should, optionally, have a ${optionalOpt.type} "${
2516
- optionalOpt.key
2517
- }" field but got ${JSON.stringify(value)}.`
2518
- );
2519
- isValid = false;
2520
- }
2521
- }
2522
-
2523
- return isValid;
2524
- };