wrangler 0.0.0-e043479 → 0.0.0-e0489587

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