wrangler 0.0.0-e0b9366 → 0.0.0-e0b98fdb6

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