wrangler 0.0.0-e3cab74 → 0.0.0-e3d46ab3

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