wrangler 0.0.0-e88a54e → 0.0.0-e8df68ee

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