wrangler 0.0.0-e2b7352 → 0.0.0-e2f5756c2

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 (174) hide show
  1. package/README.md +44 -25
  2. package/bin/wrangler.js +90 -36
  3. package/config-schema.json +3324 -0
  4. package/kv-asset-handler.js +1 -0
  5. package/package.json +139 -100
  6. package/templates/__tests__/pages-dev-util.test.ts +128 -0
  7. package/templates/__tests__/tsconfig-sanity.ts +12 -0
  8. package/templates/__tests__/tsconfig.json +8 -0
  9. package/templates/checked-fetch.js +30 -0
  10. package/templates/facade.d.ts +19 -0
  11. package/templates/gitignore +170 -0
  12. package/templates/init-tests/test-jest-new-worker.js +23 -0
  13. package/templates/init-tests/test-vitest-new-worker.js +24 -0
  14. package/templates/init-tests/test-vitest-new-worker.ts +25 -0
  15. package/templates/middleware/common.ts +67 -0
  16. package/templates/middleware/loader-modules.ts +134 -0
  17. package/templates/middleware/loader-sw.ts +229 -0
  18. package/templates/middleware/middleware-ensure-req-body-drained.ts +18 -0
  19. package/templates/middleware/middleware-miniflare3-json-error.ts +32 -0
  20. package/templates/middleware/middleware-mock-analytics-engine.d.ts +3 -0
  21. package/templates/middleware/middleware-mock-analytics-engine.ts +30 -0
  22. package/templates/middleware/middleware-patch-console-prefix.d.ts +3 -0
  23. package/templates/middleware/middleware-patch-console-prefix.ts +21 -0
  24. package/templates/middleware/middleware-pretty-error.ts +40 -0
  25. package/templates/middleware/middleware-scheduled.ts +29 -0
  26. package/templates/middleware/middleware-serve-static-assets.d.ts +6 -0
  27. package/templates/middleware/middleware-serve-static-assets.ts +56 -0
  28. package/templates/modules-watch-stub.js +4 -0
  29. package/templates/new-worker-scheduled.js +17 -0
  30. package/templates/new-worker-scheduled.ts +32 -0
  31. package/templates/new-worker.js +3 -3
  32. package/templates/new-worker.ts +22 -4
  33. package/templates/no-op-worker.js +10 -0
  34. package/templates/pages-dev-pipeline.ts +32 -0
  35. package/templates/pages-dev-util.ts +55 -0
  36. package/templates/pages-shim.ts +9 -0
  37. package/templates/pages-template-plugin.ts +190 -0
  38. package/templates/pages-template-worker.ts +198 -0
  39. package/templates/startDevWorker/InspectorProxyWorker.ts +664 -0
  40. package/templates/startDevWorker/ProxyWorker.ts +336 -0
  41. package/templates/tsconfig-sanity.ts +11 -0
  42. package/templates/tsconfig.init.json +22 -0
  43. package/templates/tsconfig.json +6 -103
  44. package/wrangler-dist/InspectorProxyWorker.js +464 -0
  45. package/wrangler-dist/InspectorProxyWorker.js.map +6 -0
  46. package/wrangler-dist/ProxyWorker.js +241 -0
  47. package/wrangler-dist/ProxyWorker.js.map +6 -0
  48. package/wrangler-dist/cli.d.ts +26702 -0
  49. package/wrangler-dist/cli.js +164220 -135144
  50. package/import_meta_url.js +0 -3
  51. package/miniflare-config-stubs/.env.empty +0 -0
  52. package/miniflare-config-stubs/package.empty.json +0 -1
  53. package/miniflare-config-stubs/wrangler.empty.toml +0 -0
  54. package/miniflare-dist/index.mjs +0 -79
  55. package/miniflare-dist/index.mjs.map +0 -7
  56. package/pages/functions/buildWorker.ts +0 -62
  57. package/pages/functions/filepath-routing.test.ts +0 -223
  58. package/pages/functions/filepath-routing.ts +0 -181
  59. package/pages/functions/identifiers.ts +0 -78
  60. package/pages/functions/routes.ts +0 -151
  61. package/pages/functions/template-worker.ts +0 -130
  62. package/src/__tests__/configuration.test.ts +0 -2820
  63. package/src/__tests__/dev.test.tsx +0 -541
  64. package/src/__tests__/guess-worker-format.test.ts +0 -68
  65. package/src/__tests__/helpers/clipboardy-mock.js +0 -4
  66. package/src/__tests__/helpers/cmd-shim.d.ts +0 -11
  67. package/src/__tests__/helpers/faye-websocket.d.ts +0 -6
  68. package/src/__tests__/helpers/mock-account-id.ts +0 -48
  69. package/src/__tests__/helpers/mock-bin.ts +0 -36
  70. package/src/__tests__/helpers/mock-cfetch.ts +0 -207
  71. package/src/__tests__/helpers/mock-console.ts +0 -70
  72. package/src/__tests__/helpers/mock-dialogs.ts +0 -65
  73. package/src/__tests__/helpers/mock-kv.ts +0 -33
  74. package/src/__tests__/helpers/mock-stdin.ts +0 -103
  75. package/src/__tests__/helpers/mock-web-socket.ts +0 -29
  76. package/src/__tests__/helpers/run-in-tmp.ts +0 -32
  77. package/src/__tests__/helpers/run-wrangler.ts +0 -16
  78. package/src/__tests__/helpers/write-wrangler-toml.ts +0 -17
  79. package/src/__tests__/https-options.test.ts +0 -127
  80. package/src/__tests__/index.test.ts +0 -983
  81. package/src/__tests__/jest.setup.ts +0 -69
  82. package/src/__tests__/kv.test.ts +0 -1380
  83. package/src/__tests__/logout.test.ts +0 -52
  84. package/src/__tests__/package-manager.test.ts +0 -206
  85. package/src/__tests__/pages.test.ts +0 -82
  86. package/src/__tests__/parse.test.ts +0 -322
  87. package/src/__tests__/publish.test.ts +0 -3509
  88. package/src/__tests__/r2.test.ts +0 -198
  89. package/src/__tests__/route.test.ts +0 -45
  90. package/src/__tests__/secret.test.ts +0 -425
  91. package/src/__tests__/sentry.test.ts +0 -146
  92. package/src/__tests__/tail.test.ts +0 -638
  93. package/src/__tests__/whoami.test.tsx +0 -100
  94. package/src/bundle.ts +0 -155
  95. package/src/cfetch/index.ts +0 -104
  96. package/src/cfetch/internal.ts +0 -139
  97. package/src/cli.ts +0 -19
  98. package/src/config/README.md +0 -107
  99. package/src/config/config.ts +0 -218
  100. package/src/config/diagnostics.ts +0 -80
  101. package/src/config/environment.ts +0 -371
  102. package/src/config/index.ts +0 -62
  103. package/src/config/validation-helpers.ts +0 -554
  104. package/src/config/validation.ts +0 -1232
  105. package/src/create-worker-preview.ts +0 -166
  106. package/src/create-worker-upload-form.ts +0 -217
  107. package/src/dev/dev.tsx +0 -392
  108. package/src/dev/local.tsx +0 -232
  109. package/src/dev/remote.tsx +0 -203
  110. package/src/dev/use-esbuild.ts +0 -104
  111. package/src/dialogs.tsx +0 -77
  112. package/src/entry.ts +0 -240
  113. package/src/environment-variables.ts +0 -33
  114. package/src/https-options.ts +0 -122
  115. package/src/index.tsx +0 -2389
  116. package/src/inspect.ts +0 -617
  117. package/src/intl-polyfill.d.ts +0 -139
  118. package/src/kv.ts +0 -319
  119. package/src/miniflare-cli/README.md +0 -30
  120. package/src/miniflare-cli/enum-keys.ts +0 -17
  121. package/src/miniflare-cli/index.ts +0 -37
  122. package/src/module-collection.ts +0 -258
  123. package/src/open-in-browser.ts +0 -25
  124. package/src/package-manager.ts +0 -126
  125. package/src/pages.tsx +0 -1061
  126. package/src/parse.ts +0 -183
  127. package/src/paths.ts +0 -26
  128. package/src/proxy.ts +0 -440
  129. package/src/publish.ts +0 -350
  130. package/src/r2.ts +0 -50
  131. package/src/reporting.ts +0 -131
  132. package/src/selfsigned.d.ts +0 -29
  133. package/src/sites.tsx +0 -280
  134. package/src/tail/filters.ts +0 -279
  135. package/src/tail/index.ts +0 -299
  136. package/src/tail/printing.ts +0 -62
  137. package/src/user.tsx +0 -1127
  138. package/src/whoami.tsx +0 -69
  139. package/src/worker.ts +0 -163
  140. package/templates/static-asset-facade.js +0 -47
  141. package/vendor/@cloudflare/kv-asset-handler/CHANGELOG.md +0 -332
  142. package/vendor/@cloudflare/kv-asset-handler/LICENSE_APACHE +0 -176
  143. package/vendor/@cloudflare/kv-asset-handler/LICENSE_MIT +0 -25
  144. package/vendor/@cloudflare/kv-asset-handler/README.md +0 -245
  145. package/vendor/@cloudflare/kv-asset-handler/dist/index.d.ts +0 -32
  146. package/vendor/@cloudflare/kv-asset-handler/dist/index.js +0 -354
  147. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.d.ts +0 -13
  148. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.js +0 -148
  149. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.d.ts +0 -1
  150. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.js +0 -436
  151. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.d.ts +0 -1
  152. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.js +0 -40
  153. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.d.ts +0 -1
  154. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.js +0 -42
  155. package/vendor/@cloudflare/kv-asset-handler/dist/types.d.ts +0 -26
  156. package/vendor/@cloudflare/kv-asset-handler/dist/types.js +0 -31
  157. package/vendor/@cloudflare/kv-asset-handler/package.json +0 -52
  158. package/vendor/@cloudflare/kv-asset-handler/src/index.ts +0 -296
  159. package/vendor/@cloudflare/kv-asset-handler/src/mocks.ts +0 -136
  160. package/vendor/@cloudflare/kv-asset-handler/src/test/getAssetFromKV.ts +0 -464
  161. package/vendor/@cloudflare/kv-asset-handler/src/test/mapRequestToAsset.ts +0 -33
  162. package/vendor/@cloudflare/kv-asset-handler/src/test/serveSinglePageApp.ts +0 -42
  163. package/vendor/@cloudflare/kv-asset-handler/src/types.ts +0 -39
  164. package/vendor/wrangler-mime/CHANGELOG.md +0 -289
  165. package/vendor/wrangler-mime/LICENSE +0 -21
  166. package/vendor/wrangler-mime/Mime.js +0 -97
  167. package/vendor/wrangler-mime/README.md +0 -187
  168. package/vendor/wrangler-mime/cli.js +0 -46
  169. package/vendor/wrangler-mime/index.js +0 -4
  170. package/vendor/wrangler-mime/lite.js +0 -4
  171. package/vendor/wrangler-mime/package.json +0 -52
  172. package/vendor/wrangler-mime/types/other.js +0 -1
  173. package/vendor/wrangler-mime/types/standard.js +0 -1
  174. package/wrangler-dist/cli.js.map +0 -7
@@ -1,1232 +0,0 @@
1
- import path from "node:path";
2
- import TOML from "@iarna/toml";
3
- import { Diagnostics } from "./diagnostics";
4
- import {
5
- deprecated,
6
- experimental,
7
- hasProperty,
8
- inheritable,
9
- isBoolean,
10
- isObjectWith,
11
- isOneOf,
12
- isOptionalProperty,
13
- isRequiredProperty,
14
- isString,
15
- isStringArray,
16
- validateAdditionalProperties,
17
- notInheritable,
18
- validateOptionalProperty,
19
- validateOptionalTypedArray,
20
- validateRequiredProperty,
21
- validateTypedArray,
22
- all,
23
- isMutuallyExclusiveWith,
24
- inheritableInLegacyEnvironments,
25
- appendEnvName,
26
- getBindingNames,
27
- } from "./validation-helpers";
28
- import type { Config, DevConfig, RawConfig, RawDevConfig } from "./config";
29
- import type {
30
- RawEnvironment,
31
- DeprecatedUpload,
32
- Environment,
33
- Rule,
34
- } from "./environment";
35
- import type { ValidatorFn } from "./validation-helpers";
36
-
37
- const ENGLISH = new Intl.ListFormat("en");
38
-
39
- /**
40
- * Validate the given `rawConfig` object that was loaded from `configPath`.
41
- *
42
- * The configuration is normalized, which includes using default values for missing field,
43
- * and copying over inheritable fields into named environments.
44
- *
45
- * Any errors or warnings from the validation are available in the returned `diagnostics` object.
46
- */
47
- export function normalizeAndValidateConfig(
48
- rawConfig: RawConfig,
49
- configPath: string | undefined,
50
- args: unknown
51
- ): {
52
- config: Config;
53
- diagnostics: Diagnostics;
54
- } {
55
- const diagnostics = new Diagnostics(
56
- `Processing ${
57
- configPath ? path.relative(process.cwd(), configPath) : "wrangler"
58
- } configuration:`
59
- );
60
-
61
- deprecated(
62
- diagnostics,
63
- rawConfig,
64
- "type",
65
- "DO NOT USE THIS. 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.",
66
- true
67
- );
68
-
69
- deprecated(
70
- diagnostics,
71
- rawConfig,
72
- "webpack_config",
73
- "DO NOT USE THIS. 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.",
74
- true
75
- );
76
-
77
- deprecated(
78
- diagnostics,
79
- rawConfig,
80
- `site.entry-point`,
81
- `The \`site.entry-point\` config field is no longer used.\nThe entry-point should be specified via the command line or the \`main\` config field.`,
82
- false,
83
- true
84
- );
85
-
86
- validateOptionalProperty(
87
- diagnostics,
88
- "",
89
- "legacy_env",
90
- rawConfig.legacy_env,
91
- "boolean"
92
- );
93
-
94
- // TODO: set the default to false to turn on service environments as the default
95
- const isLegacyEnv =
96
- (args as { "legacy-env": boolean | undefined })["legacy-env"] ??
97
- rawConfig.legacy_env ??
98
- true;
99
-
100
- const topLevelEnv = normalizeAndValidateEnvironment(
101
- diagnostics,
102
- configPath,
103
- rawConfig
104
- );
105
-
106
- //TODO: find a better way to define the type of Args that can be passed to the normalizeAndValidateConfig()
107
- const envName = (args as { env: string | undefined }).env;
108
-
109
- let activeEnv = topLevelEnv;
110
- if (envName !== undefined) {
111
- const envDiagnostics = new Diagnostics(
112
- `"env.${envName}" environment configuration`
113
- );
114
- const rawEnv = rawConfig.env?.[envName];
115
- if (rawEnv !== undefined) {
116
- activeEnv = normalizeAndValidateEnvironment(
117
- envDiagnostics,
118
- configPath,
119
- rawEnv,
120
- envName,
121
- topLevelEnv,
122
- isLegacyEnv,
123
- rawConfig
124
- );
125
- diagnostics.addChild(envDiagnostics);
126
- } else {
127
- // An environment was specified, but no configuration for it was found.
128
- // To cover any legacy environment cases, where the `envName` is used,
129
- // Let's create a fake active environment with the specified `envName`.
130
- activeEnv = normalizeAndValidateEnvironment(
131
- envDiagnostics,
132
- configPath,
133
- {},
134
- envName,
135
- topLevelEnv,
136
- isLegacyEnv,
137
- rawConfig
138
- );
139
- const envNames = rawConfig.env
140
- ? `The available configured environment names are: ${JSON.stringify(
141
- Object.keys(rawConfig.env)
142
- )}\n`
143
- : "";
144
- const message =
145
- `No environment found in configuration with name "${envName}".\n` +
146
- `Before using \`--env=${envName}\` there should be an equivalent environment section in the configuration.\n` +
147
- `${envNames}\n` +
148
- `Consider adding an environment configuration section to the wrangler.toml file:\n` +
149
- "```\n[env." +
150
- envName +
151
- "]\n```\n";
152
-
153
- if (envNames.length > 0) {
154
- diagnostics.errors.push(message);
155
- } else {
156
- // Only warn (rather than error) if there are not actually any environments configured in wrangler.toml.
157
- diagnostics.warnings.push(message);
158
- }
159
- }
160
- }
161
-
162
- // Process the top-level default environment configuration.
163
- const config: Config = {
164
- configPath,
165
- legacy_env: isLegacyEnv,
166
- ...activeEnv,
167
- dev: normalizeAndValidateDev(diagnostics, rawConfig.dev ?? {}),
168
- migrations: normalizeAndValidateMigrations(
169
- diagnostics,
170
- rawConfig.migrations ?? []
171
- ),
172
- site: normalizeAndValidateSite(diagnostics, rawConfig.site),
173
- wasm_modules: normalizeAndValidateModulePaths(
174
- diagnostics,
175
- configPath,
176
- "wasm_modules",
177
- rawConfig.wasm_modules
178
- ),
179
- text_blobs: normalizeAndValidateModulePaths(
180
- diagnostics,
181
- configPath,
182
- "text_blobs",
183
- rawConfig.text_blobs
184
- ),
185
- };
186
-
187
- validateBindingsHaveUniqueNames(diagnostics, config);
188
-
189
- validateAdditionalProperties(
190
- diagnostics,
191
- "top-level",
192
- Object.keys(rawConfig),
193
- [...Object.keys(config), "env", "miniflare"]
194
- );
195
-
196
- return { config, diagnostics };
197
- }
198
-
199
- /**
200
- * Validate the `build` configuration and return the normalized values.
201
- */
202
- function normalizeAndValidateBuild(
203
- diagnostics: Diagnostics,
204
- rawEnv: RawEnvironment,
205
- rawBuild: Config["build"],
206
- configPath: string | undefined
207
- ): Config["build"] & { deprecatedUpload: DeprecatedUpload } {
208
- const { command, cwd, watch_dir, upload, ...rest } = rawBuild;
209
- const deprecatedUpload: DeprecatedUpload = { ...upload };
210
- validateAdditionalProperties(diagnostics, "build", Object.keys(rest), []);
211
-
212
- validateOptionalProperty(diagnostics, "build", "command", command, "string");
213
- validateOptionalProperty(diagnostics, "build", "cwd", cwd, "string");
214
- validateOptionalProperty(
215
- diagnostics,
216
- "build",
217
- "watch_dir",
218
- watch_dir,
219
- "string"
220
- );
221
-
222
- deprecated(
223
- diagnostics,
224
- rawEnv,
225
- "build.upload.format",
226
- "The format is inferred automatically from the code.",
227
- true
228
- );
229
-
230
- if (rawEnv.main !== undefined && rawBuild.upload?.main) {
231
- diagnostics.errors.push(
232
- `Don't define both the \`main\` and \`build.upload.main\` fields in your configuration.\n` +
233
- `They serve the same purpose: to point to the entry-point of your worker.\n` +
234
- `Delete the \`build.upload.main\` and \`build.upload.dir\` field from your config.`
235
- );
236
- } else {
237
- deprecated(
238
- diagnostics,
239
- rawEnv,
240
- "build.upload.main",
241
- `Delete the \`build.upload.main\` and \`build.upload.dir\` fields.\n` +
242
- `Then add the top level \`main\` field to your configuration file:\n` +
243
- `\`\`\`\n` +
244
- `main = "${path.join(
245
- rawBuild.upload?.dir ?? "./dist",
246
- rawBuild.upload?.main ?? "."
247
- )}"\n` +
248
- `\`\`\``,
249
- true
250
- );
251
-
252
- deprecated(
253
- diagnostics,
254
- rawEnv,
255
- "build.upload.dir",
256
- `Use the top level "main" field or a command-line argument to specify the entry-point for the Worker.`,
257
- true
258
- );
259
- }
260
-
261
- return {
262
- command,
263
- watch_dir:
264
- // - `watch_dir` only matters when `command` is defined, so we apply
265
- // a default only when `command` is defined
266
- // - `configPath` will always be defined since `build` can only
267
- // be configured in `wrangler.toml`, but who knows, that may
268
- // change in the future, so we do a check anyway
269
- command
270
- ? configPath
271
- ? path.relative(
272
- process.cwd(),
273
- path.join(path.dirname(configPath), watch_dir || "./src")
274
- )
275
- : watch_dir || "./src"
276
- : watch_dir,
277
- cwd,
278
- deprecatedUpload,
279
- };
280
- }
281
-
282
- /**
283
- * Validate the `main` field and return the normalized values.
284
- */
285
- function normalizeAndValidateMainField(
286
- configPath: string | undefined,
287
- rawMain: string | undefined,
288
- deprecatedUpload: DeprecatedUpload | undefined
289
- ): string | undefined {
290
- const configDir = path.dirname(configPath ?? "wrangler.toml");
291
- if (rawMain !== undefined) {
292
- if (typeof rawMain === "string") {
293
- const directory = path.resolve(configDir);
294
- return path.resolve(directory, rawMain);
295
- } else {
296
- return rawMain;
297
- }
298
- } else if (deprecatedUpload?.main !== undefined) {
299
- const directory = path.resolve(
300
- configDir,
301
- deprecatedUpload?.dir || "./dist"
302
- );
303
- return path.resolve(directory, deprecatedUpload.main);
304
- } else {
305
- return;
306
- }
307
- }
308
-
309
- /**
310
- * Validate the `dev` configuration and return the normalized values.
311
- */
312
- function normalizeAndValidateDev(
313
- diagnostics: Diagnostics,
314
- rawDev: RawDevConfig
315
- ): DevConfig {
316
- const {
317
- ip = "localhost",
318
- port = 8787,
319
- local_protocol = "http",
320
- upstream_protocol = "https",
321
- host,
322
- ...rest
323
- } = rawDev;
324
- validateAdditionalProperties(diagnostics, "dev", Object.keys(rest), []);
325
-
326
- validateOptionalProperty(diagnostics, "dev", "ip", ip, "string");
327
- validateOptionalProperty(diagnostics, "dev", "port", port, "number");
328
- validateOptionalProperty(
329
- diagnostics,
330
- "dev",
331
- "local_protocol",
332
- local_protocol,
333
- "string",
334
- ["http", "https"]
335
- );
336
- validateOptionalProperty(
337
- diagnostics,
338
- "dev",
339
- "upstream_protocol",
340
- upstream_protocol,
341
- "string",
342
- ["http", "https"]
343
- );
344
- validateOptionalProperty(diagnostics, "dev", "host", host, "string");
345
- return { ip, port, local_protocol, upstream_protocol, host };
346
- }
347
-
348
- /**
349
- * Validate the `migrations` configuration and return the normalized values.
350
- */
351
- function normalizeAndValidateMigrations(
352
- diagnostics: Diagnostics,
353
- rawMigrations: Config["migrations"]
354
- ): Config["migrations"] {
355
- if (!Array.isArray(rawMigrations)) {
356
- diagnostics.errors.push(
357
- `The optional "migrations" field should be an array, but got ${JSON.stringify(
358
- rawMigrations
359
- )}`
360
- );
361
- return [];
362
- } else {
363
- for (let i = 0; i < rawMigrations.length; i++) {
364
- const migration = rawMigrations[i];
365
- validateRequiredProperty(
366
- diagnostics,
367
- `migrations[${i}]`,
368
- `tag`,
369
- migration.tag,
370
- "string"
371
- );
372
- validateOptionalTypedArray(
373
- diagnostics,
374
- `migrations[${i}].new_classes`,
375
- migration.new_classes,
376
- "string"
377
- );
378
- if (migration.renamed_classes !== undefined) {
379
- if (!Array.isArray(migration.renamed_classes)) {
380
- diagnostics.errors.push(
381
- `Expected "migrations[${i}].renamed_classes" to be an array of "{from: string, to: string}" objects but got ${JSON.stringify(
382
- migration.renamed_classes
383
- )}.`
384
- );
385
- } else if (
386
- migration.renamed_classes.some(
387
- (c) =>
388
- typeof c !== "object" ||
389
- !isRequiredProperty(c, "from", "string") ||
390
- !isRequiredProperty(c, "to", "string")
391
- )
392
- ) {
393
- diagnostics.errors.push(
394
- `Expected "migrations[${i}].renamed_classes" to be an array of "{from: string, to: string}" objects but got ${JSON.stringify(
395
- migration.renamed_classes
396
- )}.`
397
- );
398
- }
399
- }
400
- validateOptionalTypedArray(
401
- diagnostics,
402
- `migrations[${i}].deleted_classes`,
403
- migration.deleted_classes,
404
- "string"
405
- );
406
- }
407
- return rawMigrations;
408
- }
409
- }
410
-
411
- /**
412
- * Validate the `site` configuration and return the normalized values.
413
- */
414
- function normalizeAndValidateSite(
415
- diagnostics: Diagnostics,
416
- rawSite: Config["site"]
417
- ): Config["site"] {
418
- if (rawSite !== undefined) {
419
- const { bucket, include = [], exclude = [], ...rest } = rawSite;
420
- validateAdditionalProperties(diagnostics, "site", Object.keys(rest), []);
421
- validateRequiredProperty(diagnostics, "site", "bucket", bucket, "string");
422
- validateTypedArray(diagnostics, "sites.include", include, "string");
423
- validateTypedArray(diagnostics, "sites.exclude", exclude, "string");
424
- return { bucket, include, exclude };
425
- }
426
- return undefined;
427
- }
428
-
429
- /**
430
- * Map the paths of the `wasm_modules` or `text_blobs` configuration to be relative to the current working directory.
431
- */
432
- function normalizeAndValidateModulePaths(
433
- diagnostics: Diagnostics,
434
- configPath: string | undefined,
435
- field: "wasm_modules" | "text_blobs",
436
- rawMapping: Record<string, string> | undefined
437
- ): Record<string, string> | undefined {
438
- if (rawMapping === undefined) {
439
- return undefined;
440
- }
441
- const mapping: Record<string, string> = {};
442
- // Rewrite paths to be relative to the cwd, rather than the config path.
443
- for (const [name, filePath] of Object.entries(rawMapping)) {
444
- if (isString(diagnostics, `${field}['${name}']`, filePath, undefined)) {
445
- if (configPath) {
446
- mapping[name] = configPath
447
- ? path.relative(
448
- process.cwd(),
449
- path.join(path.dirname(configPath), filePath)
450
- )
451
- : filePath;
452
- }
453
- }
454
- }
455
- return mapping;
456
- }
457
-
458
- /**
459
- * Validate top-level environment configuration and return the normalized values.
460
- */
461
- function normalizeAndValidateEnvironment(
462
- diagnostics: Diagnostics,
463
- configPath: string | undefined,
464
- topLevelEnv: RawEnvironment
465
- ): Environment;
466
- /**
467
- * Validate the named environment configuration and return the normalized values.
468
- */
469
- function normalizeAndValidateEnvironment(
470
- diagnostics: Diagnostics,
471
- configPath: string | undefined,
472
- rawEnv: RawEnvironment,
473
- envName: string,
474
- topLevelEnv: Environment,
475
- isLegacyEnv: boolean,
476
- rawConfig: RawConfig
477
- ): Environment;
478
- function normalizeAndValidateEnvironment(
479
- diagnostics: Diagnostics,
480
- configPath: string | undefined,
481
- rawEnv: RawEnvironment,
482
- envName = "top level",
483
- topLevelEnv?: Environment | undefined,
484
- isLegacyEnv?: boolean,
485
- rawConfig?: RawConfig | undefined
486
- ): Environment {
487
- deprecated(
488
- diagnostics,
489
- rawEnv,
490
- "zone_id",
491
- "This is unnecessary since we can deduce this from routes directly.",
492
- false // We need to leave this in-place for the moment since `route` commands might use it.
493
- );
494
-
495
- // The field "experimental_services" doesn't exist anymore in the config, but we still want to error about any older usage.
496
- // TODO: remove this before GA.
497
- deprecated(
498
- diagnostics,
499
- rawEnv,
500
- "experimental_services",
501
- `The "experimental_services" field is no longer supported. Instead, use [[unsafe.bindings]] to enable experimental features. Add this to your wrangler.toml:\n` +
502
- "```\n" +
503
- TOML.stringify({
504
- unsafe: {
505
- bindings: (rawEnv?.experimental_services || []).map(
506
- (serviceDefinition) => {
507
- return {
508
- name: serviceDefinition.name,
509
- type: "service",
510
- service: serviceDefinition.service,
511
- environment: serviceDefinition.environment,
512
- };
513
- }
514
- ),
515
- },
516
- }) +
517
- "```",
518
- true
519
- );
520
-
521
- experimental(diagnostics, rawEnv, "unsafe");
522
-
523
- const route = inheritable(
524
- diagnostics,
525
- topLevelEnv,
526
- rawEnv,
527
- "route",
528
- isString,
529
- undefined
530
- );
531
- const routes = inheritable(
532
- diagnostics,
533
- topLevelEnv,
534
- rawEnv,
535
- "routes",
536
- all(isStringArray, isMutuallyExclusiveWith(rawEnv, "route")),
537
- undefined
538
- );
539
- const workers_dev = inheritable(
540
- diagnostics,
541
- topLevelEnv,
542
- rawEnv,
543
- "workers_dev",
544
- isBoolean,
545
- undefined
546
- );
547
-
548
- const { deprecatedUpload, ...build } = normalizeAndValidateBuild(
549
- diagnostics,
550
- rawEnv,
551
- rawEnv.build ?? topLevelEnv?.build ?? {},
552
- configPath
553
- );
554
-
555
- const environment: Environment = {
556
- // Inherited fields
557
- account_id: inheritable(
558
- diagnostics,
559
- topLevelEnv,
560
- rawEnv,
561
- "account_id",
562
- isString,
563
- undefined
564
- ),
565
- compatibility_date: inheritable(
566
- diagnostics,
567
- topLevelEnv,
568
- rawEnv,
569
- "compatibility_date",
570
- isString,
571
- undefined
572
- ),
573
- compatibility_flags: inheritable(
574
- diagnostics,
575
- topLevelEnv,
576
- rawEnv,
577
- "compatibility_flags",
578
- isStringArray,
579
- []
580
- ),
581
- jsx_factory: inheritable(
582
- diagnostics,
583
- topLevelEnv,
584
- rawEnv,
585
- "jsx_factory",
586
- isString,
587
- "React.createElement"
588
- ),
589
- jsx_fragment: inheritable(
590
- diagnostics,
591
- topLevelEnv,
592
- rawEnv,
593
- "jsx_fragment",
594
- isString,
595
- "React.Fragment"
596
- ),
597
- tsconfig: validateAndNormalizeTsconfig(
598
- diagnostics,
599
- topLevelEnv,
600
- rawEnv,
601
- configPath
602
- ),
603
- rules: validateAndNormalizeRules(
604
- diagnostics,
605
- topLevelEnv,
606
- rawEnv,
607
- deprecatedUpload?.rules,
608
- envName
609
- ),
610
- name: inheritableInLegacyEnvironments(
611
- diagnostics,
612
- isLegacyEnv,
613
- topLevelEnv,
614
- rawEnv,
615
- "name",
616
- isString,
617
- appendEnvName(envName),
618
- undefined
619
- ),
620
- main: normalizeAndValidateMainField(
621
- configPath,
622
- inheritable(
623
- diagnostics,
624
- topLevelEnv,
625
- rawEnv,
626
- "main",
627
- isString,
628
- undefined
629
- ),
630
- deprecatedUpload
631
- ),
632
- route,
633
- routes,
634
- triggers: inheritable(
635
- diagnostics,
636
- topLevelEnv,
637
- rawEnv,
638
- "triggers",
639
- isObjectWith("crons"),
640
- { crons: [] }
641
- ),
642
- usage_model: inheritable(
643
- diagnostics,
644
- topLevelEnv,
645
- rawEnv,
646
- "usage_model",
647
- isOneOf("bundled", "unbound"),
648
- undefined
649
- ),
650
- build,
651
- workers_dev,
652
- // Not inherited fields
653
- vars: notInheritable(
654
- diagnostics,
655
- topLevelEnv,
656
- rawConfig,
657
- rawEnv,
658
- envName,
659
- "vars",
660
- validateVars(envName),
661
- {}
662
- ),
663
- durable_objects: notInheritable(
664
- diagnostics,
665
- topLevelEnv,
666
- rawConfig,
667
- rawEnv,
668
- envName,
669
- "durable_objects",
670
- validateBindingsProperty(envName, validateDurableObjectBinding),
671
- {
672
- bindings: [],
673
- }
674
- ),
675
- kv_namespaces: notInheritable(
676
- diagnostics,
677
- topLevelEnv,
678
- rawConfig,
679
- rawEnv,
680
- envName,
681
- "kv_namespaces",
682
- validateBindingArray(envName, validateKVBinding),
683
- []
684
- ),
685
- r2_buckets: notInheritable(
686
- diagnostics,
687
- topLevelEnv,
688
- rawConfig,
689
- rawEnv,
690
- envName,
691
- "r2_buckets",
692
- validateBindingArray(envName, validateR2Binding),
693
- []
694
- ),
695
- unsafe: notInheritable(
696
- diagnostics,
697
- topLevelEnv,
698
- rawConfig,
699
- rawEnv,
700
- envName,
701
- "unsafe",
702
- validateBindingsProperty(envName, validateUnsafeBinding),
703
- {
704
- bindings: [],
705
- }
706
- ),
707
- zone_id: rawEnv.zone_id,
708
- };
709
-
710
- return environment;
711
- }
712
-
713
- function validateAndNormalizeTsconfig(
714
- diagnostics: Diagnostics,
715
- topLevelEnv: Environment | undefined,
716
- rawEnv: RawEnvironment,
717
- configPath: string | undefined
718
- ) {
719
- const tsconfig = inheritable(
720
- diagnostics,
721
- topLevelEnv,
722
- rawEnv,
723
- "tsconfig",
724
- isString,
725
- undefined
726
- );
727
-
728
- return configPath && tsconfig
729
- ? path.relative(
730
- process.cwd(),
731
- path.join(path.dirname(configPath), tsconfig)
732
- )
733
- : tsconfig;
734
- }
735
-
736
- const validateAndNormalizeRules = (
737
- diagnostics: Diagnostics,
738
- topLevelEnv: Environment | undefined,
739
- rawEnv: RawEnvironment,
740
- deprecatedRules: Rule[] | undefined,
741
- envName: string
742
- ): Rule[] => {
743
- if (topLevelEnv === undefined) {
744
- // Only create errors/warnings for the top-level environment
745
- if (rawEnv.rules && deprecatedRules) {
746
- diagnostics.errors.push(
747
- `You cannot configure both [rules] and [build.upload.rules] in your wrangler.toml. Delete the \`build.upload\` section.`
748
- );
749
- } else if (deprecatedRules) {
750
- diagnostics.warnings.push(
751
- `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` +
752
- "```\n" +
753
- TOML.stringify({ rules: deprecatedRules }) +
754
- "```"
755
- );
756
- }
757
- }
758
-
759
- return inheritable(
760
- diagnostics,
761
- topLevelEnv,
762
- rawEnv,
763
- "rules",
764
- validateRules(envName),
765
- deprecatedRules ?? []
766
- );
767
- };
768
-
769
- const validateRules =
770
- (envName: string): ValidatorFn =>
771
- (diagnostics, field, envValue, config) => {
772
- if (!envValue) {
773
- return true;
774
- }
775
- const fieldPath =
776
- config === undefined ? `${field}` : `env.${envName}.${field}`;
777
- if (!Array.isArray(envValue)) {
778
- diagnostics.errors.push(
779
- `The field "${fieldPath}" should be an array but got ${JSON.stringify(
780
- envValue
781
- )}.`
782
- );
783
- return false;
784
- }
785
-
786
- let isValid = true;
787
- for (let i = 0; i < envValue.length; i++) {
788
- isValid =
789
- validateRule(diagnostics, `${fieldPath}[${i}]`, envValue[i], config) &&
790
- isValid;
791
- }
792
- return isValid;
793
- };
794
-
795
- const validateRule: ValidatorFn = (diagnostics, field, value) => {
796
- if (typeof value !== "object" || value === null) {
797
- diagnostics.errors.push(
798
- `"${field}" should be an object but got ${JSON.stringify(value)}.`
799
- );
800
- return false;
801
- }
802
- // Rules must have a type string and glob string array, and optionally a fallthrough boolean.
803
- let isValid = true;
804
- const rule = value as Rule;
805
-
806
- if (
807
- !isRequiredProperty(rule, "type", "string", [
808
- "ESModule",
809
- "CommonJS",
810
- "CompiledWasm",
811
- "Text",
812
- "Data",
813
- ])
814
- ) {
815
- diagnostics.errors.push(
816
- `bindings should have a string "type" field, which contains one of "ESModule", "CommonJS", "CompiledWasm", "Text", or "Data".`
817
- );
818
- isValid = false;
819
- }
820
-
821
- isValid =
822
- validateTypedArray(diagnostics, `${field}.globs`, rule.globs, "string") &&
823
- isValid;
824
-
825
- if (!isOptionalProperty(rule, "fallthrough", "boolean")) {
826
- diagnostics.errors.push(
827
- `binding should, optionally, have a boolean "fallthrough" field.`
828
- );
829
- isValid = false;
830
- }
831
-
832
- return isValid;
833
- };
834
-
835
- const validateVars =
836
- (envName: string): ValidatorFn =>
837
- (diagnostics, field, value, config) => {
838
- let isValid = true;
839
- const fieldPath =
840
- config === undefined ? `${field}` : `env.${envName}.${field}`;
841
- const configVars = Object.keys(config?.vars ?? {});
842
- // If there are no top level vars then there is nothing to do here.
843
- if (configVars.length > 0) {
844
- if (typeof value !== "object" || value === null) {
845
- diagnostics.errors.push(
846
- `The field "${fieldPath}" should be an object but got ${JSON.stringify(
847
- value
848
- )}.\n`
849
- );
850
- isValid = false;
851
- } else {
852
- for (const varName of configVars) {
853
- if (!(varName in value)) {
854
- diagnostics.warnings.push(
855
- `"vars.${varName}" exists at the top level, but not on "${fieldPath}".\n` +
856
- `This is not what you probably want, since "vars" configuration is not inherited by environments.\n` +
857
- `Please add "vars.${varName}" to "env.${envName}".`
858
- );
859
- }
860
- }
861
- }
862
- }
863
- return isValid;
864
- };
865
-
866
- const validateBindingsProperty =
867
- (envName: string, validateBinding: ValidatorFn): ValidatorFn =>
868
- (diagnostics, field, value, config) => {
869
- let isValid = true;
870
- const fieldPath =
871
- config === undefined ? `${field}` : `env.${envName}.${field}`;
872
-
873
- if (value !== undefined) {
874
- // Check the validity of the `value` as a bindings container.
875
- if (typeof value !== "object" || value === null || Array.isArray(value)) {
876
- diagnostics.errors.push(
877
- `The field "${fieldPath}" should be an object but got ${JSON.stringify(
878
- value
879
- )}.`
880
- );
881
- isValid = false;
882
- } else if (!hasProperty(value, "bindings")) {
883
- diagnostics.errors.push(
884
- `The field "${fieldPath}" is missing the required "bindings" property.`
885
- );
886
- isValid = false;
887
- } else if (!Array.isArray(value.bindings)) {
888
- diagnostics.errors.push(
889
- `The field "${fieldPath}.bindings" should be an array but got ${JSON.stringify(
890
- value.bindings
891
- )}.`
892
- );
893
- isValid = false;
894
- } else {
895
- for (let i = 0; i < value.bindings.length; i++) {
896
- const binding = value.bindings[i];
897
- const bindingDiagnostics = new Diagnostics(
898
- `"${fieldPath}.bindings[${i}]": ${JSON.stringify(binding)}`
899
- );
900
- isValid =
901
- validateBinding(
902
- bindingDiagnostics,
903
- `${fieldPath}.bindings[${i}]`,
904
- binding,
905
- config
906
- ) && isValid;
907
- diagnostics.addChild(bindingDiagnostics);
908
- }
909
- }
910
-
911
- const configBindingNames = getBindingNames(
912
- config?.[field as keyof Environment]
913
- );
914
- if (isValid && configBindingNames.length > 0) {
915
- // If there are top level bindings then check that they all appear in the environment.
916
- const envBindingNames = new Set(getBindingNames(value));
917
- const missingBindings = configBindingNames.filter(
918
- (name) => !envBindingNames.has(name)
919
- );
920
- if (missingBindings.length > 0) {
921
- diagnostics.warnings.push(
922
- `The following bindings are at the top level, but not on "env.${envName}".\n` +
923
- `This is not what you probably want, since "${field}" configuration is not inherited by environments.\n` +
924
- `Please add a binding for each to "${fieldPath}.bindings".` +
925
- missingBindings.map((name) => `- ${name}`).join("\n")
926
- );
927
- }
928
- }
929
- }
930
- return isValid;
931
- };
932
-
933
- /**
934
- * Check that the given field is a valid "durable_object" binding object.
935
- */
936
- const validateDurableObjectBinding: ValidatorFn = (
937
- diagnostics,
938
- field,
939
- value
940
- ) => {
941
- if (typeof value !== "object" || value === null) {
942
- diagnostics.errors.push(
943
- `Expected "${field}" to be an object but got ${JSON.stringify(value)}`
944
- );
945
- return false;
946
- }
947
-
948
- // Durable Object bindings must have a name and class_name, and optionally a script_name.
949
- let isValid = true;
950
- if (!isRequiredProperty(value, "name", "string")) {
951
- diagnostics.errors.push(`binding should have a string "name" field.`);
952
- isValid = false;
953
- }
954
- if (!isRequiredProperty(value, "class_name", "string")) {
955
- diagnostics.errors.push(`binding should have a string "class_name" field.`);
956
- isValid = false;
957
- }
958
- if (!isOptionalProperty(value, "script_name", "string")) {
959
- diagnostics.errors.push(
960
- `binding should, optionally, have a string "script_name" field.`
961
- );
962
- isValid = false;
963
- }
964
-
965
- return isValid;
966
- };
967
-
968
- /**
969
- * Check that the given field is a valid "unsafe" binding object.
970
- *
971
- * TODO: further validation of known unsafe bindings.
972
- */
973
- const validateUnsafeBinding: ValidatorFn = (diagnostics, field, value) => {
974
- if (typeof value !== "object" || value === null) {
975
- diagnostics.errors.push(
976
- `Expected ${field} to be an object but got ${JSON.stringify(value)}.`
977
- );
978
- return false;
979
- }
980
-
981
- let isValid = true;
982
- // Unsafe bindings must have a name and type.
983
- if (!isRequiredProperty(value, "name", "string")) {
984
- diagnostics.errors.push(`binding should have a string "name" field.`);
985
- isValid = false;
986
- }
987
- if (isRequiredProperty(value, "type", "string")) {
988
- const safeBindings = [
989
- "plain_text",
990
- "json",
991
- "kv_namespace",
992
- "durable_object_namespace",
993
- ];
994
-
995
- if (safeBindings.includes(value.type)) {
996
- diagnostics.warnings.push(
997
- `The binding type "${value.type}" is directly supported by wrangler.\n` +
998
- `Consider migrating this unsafe binding to a format for '${value.type}' bindings that is supported by wrangler for optimal support.\n` +
999
- "For more details, see https://developers.cloudflare.com/workers/cli-wrangler/configuration"
1000
- );
1001
- }
1002
- } else {
1003
- diagnostics.errors.push(`binding should have a string "type" field.`);
1004
- isValid = false;
1005
- }
1006
- return isValid;
1007
- };
1008
-
1009
- /**
1010
- * Check that the given environment field is a valid array of bindings.
1011
- */
1012
- const validateBindingArray =
1013
- (envName: string, validateBinding: ValidatorFn): ValidatorFn =>
1014
- (diagnostics, field, envValue, config) => {
1015
- if (envValue === undefined) {
1016
- return true;
1017
- }
1018
-
1019
- const fieldPath =
1020
- config === undefined ? `${field}` : `env.${envName}.${field}`;
1021
- if (!Array.isArray(envValue)) {
1022
- diagnostics.errors.push(
1023
- `The field "${fieldPath}" should be an array but got ${JSON.stringify(
1024
- envValue
1025
- )}.`
1026
- );
1027
- return false;
1028
- }
1029
-
1030
- let isValid = true;
1031
- for (let i = 0; i < envValue.length; i++) {
1032
- isValid =
1033
- validateBinding(
1034
- diagnostics,
1035
- `${fieldPath}[${i}]`,
1036
- envValue[i],
1037
- config
1038
- ) && isValid;
1039
- }
1040
- const configValue = config?.[field as keyof Environment] as {
1041
- binding: unknown;
1042
- }[];
1043
- if (Array.isArray(configValue)) {
1044
- const configBindingNames = configValue.map((value) => value.binding);
1045
- // If there are no top level bindings then there is nothing to do here.
1046
- if (configBindingNames.length > 0) {
1047
- const envBindingNames = new Set(envValue.map((value) => value.binding));
1048
- for (const configBindingName of configBindingNames) {
1049
- if (!envBindingNames.has(configBindingName)) {
1050
- diagnostics.warnings.push(
1051
- `There is a ${field} binding with name "${configBindingName}" at the top level, but not on "env.${envName}".\n` +
1052
- `This is not what you probably want, since "${field}" configuration is not inherited by environments.\n` +
1053
- `Please add a binding for "${configBindingName}" to "env.${envName}.${field}.bindings".`
1054
- );
1055
- }
1056
- }
1057
- }
1058
- }
1059
- return isValid;
1060
- };
1061
-
1062
- const validateKVBinding: ValidatorFn = (diagnostics, field, value) => {
1063
- if (typeof value !== "object" || value === null) {
1064
- diagnostics.errors.push(
1065
- `"kv_namespaces" bindings should be objects, but got ${JSON.stringify(
1066
- value
1067
- )}`
1068
- );
1069
- return false;
1070
- }
1071
- let isValid = true;
1072
- // KV bindings must have a binding and id.
1073
- if (!isRequiredProperty(value, "binding", "string")) {
1074
- diagnostics.errors.push(
1075
- `"${field}" bindings should have a string "binding" field but got ${JSON.stringify(
1076
- value
1077
- )}.`
1078
- );
1079
- isValid = false;
1080
- }
1081
- if (!isRequiredProperty(value, "id", "string")) {
1082
- diagnostics.errors.push(
1083
- `"${field}" bindings should have a string "id" field but got ${JSON.stringify(
1084
- value
1085
- )}.`
1086
- );
1087
- isValid = false;
1088
- }
1089
- if (!isOptionalProperty(value, "preview_id", "string")) {
1090
- diagnostics.errors.push(
1091
- `"${field}" bindings should, optionally, have a string "preview_id" field but got ${JSON.stringify(
1092
- value
1093
- )}.`
1094
- );
1095
- isValid = false;
1096
- }
1097
- return isValid;
1098
- };
1099
-
1100
- const validateR2Binding: ValidatorFn = (diagnostics, field, value) => {
1101
- if (typeof value !== "object" || value === null) {
1102
- diagnostics.errors.push(
1103
- `"kv_namespaces" bindings should be objects, but got ${JSON.stringify(
1104
- value
1105
- )}`
1106
- );
1107
- return false;
1108
- }
1109
- let isValid = true;
1110
- // R2 bindings must have a binding and bucket_name.
1111
- if (!isRequiredProperty(value, "binding", "string")) {
1112
- diagnostics.errors.push(
1113
- `"${field}" bindings should have a string "binding" field but got ${JSON.stringify(
1114
- value
1115
- )}.`
1116
- );
1117
- isValid = false;
1118
- }
1119
- if (!isRequiredProperty(value, "bucket_name", "string")) {
1120
- diagnostics.errors.push(
1121
- `"${field}" bindings should have a string "bucket_name" field but got ${JSON.stringify(
1122
- value
1123
- )}.`
1124
- );
1125
- isValid = false;
1126
- }
1127
- if (!isOptionalProperty(value, "preview_bucket_name", "string")) {
1128
- diagnostics.errors.push(
1129
- `"${field}" bindings should, optionally, have a string "preview_bucket_name" field but got ${JSON.stringify(
1130
- value
1131
- )}.`
1132
- );
1133
- isValid = false;
1134
- }
1135
- return isValid;
1136
- };
1137
-
1138
- /**
1139
- * Check that bindings whose names might conflict, don't.
1140
- *
1141
- * We don't want to have, for example, a KV namespace named "DATA"
1142
- * and a Durable Object also named "DATA". Then it would be ambiguous
1143
- * what exactly would live at `env.DATA` (or in the case of service workers,
1144
- * the `DATA` global).
1145
- */
1146
- const validateBindingsHaveUniqueNames = (
1147
- diagnostics: Diagnostics,
1148
- {
1149
- durable_objects,
1150
- kv_namespaces,
1151
- r2_buckets,
1152
- text_blobs,
1153
- unsafe,
1154
- vars,
1155
- wasm_modules,
1156
- }: Partial<Config>
1157
- ): boolean => {
1158
- let hasDuplicates = false;
1159
-
1160
- const bindingsGroupedByType = {
1161
- "Durable Object": getBindingNames(durable_objects),
1162
- "KV Namespace": getBindingNames(kv_namespaces),
1163
- "R2 Bucket": getBindingNames(r2_buckets),
1164
- "Text Blob": getBindingNames(text_blobs),
1165
- Unsafe: getBindingNames(unsafe),
1166
- "Environment Variable": getBindingNames(vars),
1167
- "WASM Module": getBindingNames(wasm_modules),
1168
- } as Record<string, string[]>;
1169
-
1170
- const bindingsGroupedByName: Record<string, string[]> = {};
1171
-
1172
- for (const bindingType in bindingsGroupedByType) {
1173
- const bindingNames = bindingsGroupedByType[bindingType];
1174
-
1175
- for (const bindingName of bindingNames) {
1176
- if (!(bindingName in bindingsGroupedByName)) {
1177
- bindingsGroupedByName[bindingName] = [];
1178
- }
1179
-
1180
- bindingsGroupedByName[bindingName].push(bindingType);
1181
- }
1182
- }
1183
-
1184
- for (const bindingName in bindingsGroupedByName) {
1185
- const bindingTypes = bindingsGroupedByName[bindingName];
1186
- if (bindingTypes.length < 2) {
1187
- // there's only one (or zero) binding(s) with this name, which is fine, actually
1188
- continue;
1189
- }
1190
-
1191
- hasDuplicates = true;
1192
-
1193
- // there's two types of duplicates we want to look for:
1194
- // - bindings with the same name of the same type (e.g. two Durable Objects both named "OBJ")
1195
- // - bindings with the same name of different types (a KV namespace and DO both named "DATA")
1196
-
1197
- const sameType = bindingTypes
1198
- // filter once to find duplicate binding types
1199
- .filter((type, i) => bindingTypes.indexOf(type) !== i)
1200
- // filter twice to only get _unique_ duplicate binding types
1201
- .filter(
1202
- (type, i, duplicateBindingTypes) =>
1203
- duplicateBindingTypes.indexOf(type) === i
1204
- );
1205
-
1206
- const differentTypes = bindingTypes.filter(
1207
- (type, i) => bindingTypes.indexOf(type) === i
1208
- );
1209
-
1210
- if (differentTypes.length > 1) {
1211
- // we have multiple different types using the same name
1212
- diagnostics.errors.push(
1213
- `${bindingName} assigned to ${ENGLISH.format(differentTypes)} bindings.`
1214
- );
1215
- }
1216
-
1217
- sameType.forEach((bindingType) => {
1218
- diagnostics.errors.push(
1219
- `${bindingName} assigned to multiple ${bindingType} bindings.`
1220
- );
1221
- });
1222
- }
1223
-
1224
- if (hasDuplicates) {
1225
- const problem =
1226
- "Bindings must have unique names, so that they can all be referenced in the worker.";
1227
- const resolution = "Please change your bindings to have unique names.";
1228
- diagnostics.errors.push(`${problem}\n${resolution}`);
1229
- }
1230
-
1231
- return !hasDuplicates;
1232
- };