wrangler 0.0.0-e3e3243 → 0.0.0-e3efd68e3

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 (175) hide show
  1. package/README.md +44 -25
  2. package/bin/wrangler.js +84 -36
  3. package/config-schema.json +3370 -0
  4. package/kv-asset-handler.js +1 -0
  5. package/package.json +143 -100
  6. package/templates/__tests__/pages-dev-util.test.ts +128 -0
  7. package/templates/__tests__/tsconfig-sanity.ts +12 -0
  8. package/templates/__tests__/tsconfig.json +8 -0
  9. package/templates/checked-fetch.js +30 -0
  10. package/templates/facade.d.ts +19 -0
  11. package/templates/gitignore +170 -0
  12. package/templates/init-tests/test-jest-new-worker.js +23 -0
  13. package/templates/init-tests/test-vitest-new-worker.js +24 -0
  14. package/templates/init-tests/test-vitest-new-worker.ts +25 -0
  15. package/templates/middleware/common.ts +67 -0
  16. package/templates/middleware/loader-modules.ts +134 -0
  17. package/templates/middleware/loader-sw.ts +229 -0
  18. package/templates/middleware/middleware-ensure-req-body-drained.ts +18 -0
  19. package/templates/middleware/middleware-miniflare3-json-error.ts +32 -0
  20. package/templates/middleware/middleware-mock-analytics-engine.d.ts +3 -0
  21. package/templates/middleware/middleware-mock-analytics-engine.ts +30 -0
  22. package/templates/middleware/middleware-patch-console-prefix.d.ts +3 -0
  23. package/templates/middleware/middleware-patch-console-prefix.ts +21 -0
  24. package/templates/middleware/middleware-pretty-error.ts +40 -0
  25. package/templates/middleware/middleware-scheduled.ts +29 -0
  26. package/templates/middleware/middleware-serve-static-assets.d.ts +6 -0
  27. package/templates/middleware/middleware-serve-static-assets.ts +56 -0
  28. package/templates/modules-watch-stub.js +4 -0
  29. package/templates/new-worker-scheduled.js +17 -0
  30. package/templates/new-worker-scheduled.ts +32 -0
  31. package/templates/new-worker.js +3 -3
  32. package/templates/new-worker.ts +22 -4
  33. package/templates/no-op-worker.js +10 -0
  34. package/templates/pages-dev-pipeline.ts +33 -0
  35. package/templates/pages-dev-util.ts +55 -0
  36. package/templates/pages-shim.ts +9 -0
  37. package/templates/pages-template-plugin.ts +190 -0
  38. package/templates/pages-template-worker.ts +198 -0
  39. package/templates/startDevWorker/InspectorProxyWorker.ts +664 -0
  40. package/templates/startDevWorker/ProxyWorker.ts +336 -0
  41. package/templates/tsconfig-sanity.ts +11 -0
  42. package/templates/tsconfig.init.json +22 -0
  43. package/templates/tsconfig.json +13 -103
  44. package/templates/tsconfig.tsbuildinfo +1 -0
  45. package/wrangler-dist/InspectorProxyWorker.js +464 -0
  46. package/wrangler-dist/InspectorProxyWorker.js.map +6 -0
  47. package/wrangler-dist/ProxyWorker.js +241 -0
  48. package/wrangler-dist/ProxyWorker.js.map +6 -0
  49. package/wrangler-dist/cli.d.ts +26745 -0
  50. package/wrangler-dist/cli.js +151572 -135868
  51. package/import_meta_url.js +0 -3
  52. package/miniflare-config-stubs/.env.empty +0 -0
  53. package/miniflare-config-stubs/package.empty.json +0 -1
  54. package/miniflare-config-stubs/wrangler.empty.toml +0 -0
  55. package/miniflare-dist/index.mjs +0 -79
  56. package/miniflare-dist/index.mjs.map +0 -7
  57. package/pages/functions/buildWorker.ts +0 -62
  58. package/pages/functions/filepath-routing.test.ts +0 -223
  59. package/pages/functions/filepath-routing.ts +0 -181
  60. package/pages/functions/identifiers.ts +0 -78
  61. package/pages/functions/routes.ts +0 -151
  62. package/pages/functions/template-worker.ts +0 -130
  63. package/src/__tests__/configuration.test.ts +0 -2820
  64. package/src/__tests__/dev.test.tsx +0 -541
  65. package/src/__tests__/guess-worker-format.test.ts +0 -68
  66. package/src/__tests__/helpers/clipboardy-mock.js +0 -4
  67. package/src/__tests__/helpers/cmd-shim.d.ts +0 -11
  68. package/src/__tests__/helpers/faye-websocket.d.ts +0 -6
  69. package/src/__tests__/helpers/mock-account-id.ts +0 -48
  70. package/src/__tests__/helpers/mock-bin.ts +0 -36
  71. package/src/__tests__/helpers/mock-cfetch.ts +0 -207
  72. package/src/__tests__/helpers/mock-console.ts +0 -70
  73. package/src/__tests__/helpers/mock-dialogs.ts +0 -65
  74. package/src/__tests__/helpers/mock-kv.ts +0 -33
  75. package/src/__tests__/helpers/mock-stdin.ts +0 -103
  76. package/src/__tests__/helpers/mock-web-socket.ts +0 -29
  77. package/src/__tests__/helpers/run-in-tmp.ts +0 -32
  78. package/src/__tests__/helpers/run-wrangler.ts +0 -16
  79. package/src/__tests__/helpers/write-wrangler-toml.ts +0 -17
  80. package/src/__tests__/https-options.test.ts +0 -127
  81. package/src/__tests__/index.test.ts +0 -983
  82. package/src/__tests__/jest.setup.ts +0 -69
  83. package/src/__tests__/kv.test.ts +0 -1380
  84. package/src/__tests__/logout.test.ts +0 -52
  85. package/src/__tests__/package-manager.test.ts +0 -206
  86. package/src/__tests__/pages.test.ts +0 -82
  87. package/src/__tests__/parse.test.ts +0 -322
  88. package/src/__tests__/publish.test.ts +0 -3509
  89. package/src/__tests__/r2.test.ts +0 -198
  90. package/src/__tests__/route.test.ts +0 -45
  91. package/src/__tests__/secret.test.ts +0 -425
  92. package/src/__tests__/sentry.test.ts +0 -146
  93. package/src/__tests__/tail.test.ts +0 -562
  94. package/src/__tests__/whoami.test.tsx +0 -100
  95. package/src/bundle.ts +0 -155
  96. package/src/cfetch/index.ts +0 -104
  97. package/src/cfetch/internal.ts +0 -139
  98. package/src/cli.ts +0 -19
  99. package/src/config/README.md +0 -107
  100. package/src/config/config.ts +0 -218
  101. package/src/config/diagnostics.ts +0 -80
  102. package/src/config/environment.ts +0 -371
  103. package/src/config/index.ts +0 -62
  104. package/src/config/validation-helpers.ts +0 -554
  105. package/src/config/validation.ts +0 -1232
  106. package/src/create-worker-preview.ts +0 -166
  107. package/src/create-worker-upload-form.ts +0 -217
  108. package/src/dev/dev.tsx +0 -392
  109. package/src/dev/local.tsx +0 -232
  110. package/src/dev/remote.tsx +0 -203
  111. package/src/dev/use-esbuild.ts +0 -104
  112. package/src/dialogs.tsx +0 -77
  113. package/src/entry.ts +0 -240
  114. package/src/environment-variables.ts +0 -33
  115. package/src/https-options.ts +0 -122
  116. package/src/index.tsx +0 -2389
  117. package/src/inspect.ts +0 -617
  118. package/src/intl-polyfill.d.ts +0 -139
  119. package/src/kv.ts +0 -319
  120. package/src/miniflare-cli/README.md +0 -30
  121. package/src/miniflare-cli/enum-keys.ts +0 -17
  122. package/src/miniflare-cli/index.ts +0 -37
  123. package/src/module-collection.ts +0 -258
  124. package/src/open-in-browser.ts +0 -25
  125. package/src/package-manager.ts +0 -126
  126. package/src/pages.tsx +0 -1061
  127. package/src/parse.ts +0 -183
  128. package/src/paths.ts +0 -26
  129. package/src/proxy.ts +0 -440
  130. package/src/publish.ts +0 -350
  131. package/src/r2.ts +0 -50
  132. package/src/reporting.ts +0 -131
  133. package/src/selfsigned.d.ts +0 -29
  134. package/src/sites.tsx +0 -280
  135. package/src/tail/filters.ts +0 -279
  136. package/src/tail/index.ts +0 -299
  137. package/src/tail/printing.ts +0 -62
  138. package/src/user.tsx +0 -1127
  139. package/src/whoami.tsx +0 -69
  140. package/src/worker.ts +0 -163
  141. package/templates/static-asset-facade.js +0 -47
  142. package/vendor/@cloudflare/kv-asset-handler/CHANGELOG.md +0 -332
  143. package/vendor/@cloudflare/kv-asset-handler/LICENSE_APACHE +0 -176
  144. package/vendor/@cloudflare/kv-asset-handler/LICENSE_MIT +0 -25
  145. package/vendor/@cloudflare/kv-asset-handler/README.md +0 -245
  146. package/vendor/@cloudflare/kv-asset-handler/dist/index.d.ts +0 -32
  147. package/vendor/@cloudflare/kv-asset-handler/dist/index.js +0 -354
  148. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.d.ts +0 -13
  149. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.js +0 -148
  150. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.d.ts +0 -1
  151. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.js +0 -436
  152. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.d.ts +0 -1
  153. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.js +0 -40
  154. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.d.ts +0 -1
  155. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.js +0 -42
  156. package/vendor/@cloudflare/kv-asset-handler/dist/types.d.ts +0 -26
  157. package/vendor/@cloudflare/kv-asset-handler/dist/types.js +0 -31
  158. package/vendor/@cloudflare/kv-asset-handler/package.json +0 -52
  159. package/vendor/@cloudflare/kv-asset-handler/src/index.ts +0 -296
  160. package/vendor/@cloudflare/kv-asset-handler/src/mocks.ts +0 -136
  161. package/vendor/@cloudflare/kv-asset-handler/src/test/getAssetFromKV.ts +0 -464
  162. package/vendor/@cloudflare/kv-asset-handler/src/test/mapRequestToAsset.ts +0 -33
  163. package/vendor/@cloudflare/kv-asset-handler/src/test/serveSinglePageApp.ts +0 -42
  164. package/vendor/@cloudflare/kv-asset-handler/src/types.ts +0 -39
  165. package/vendor/wrangler-mime/CHANGELOG.md +0 -289
  166. package/vendor/wrangler-mime/LICENSE +0 -21
  167. package/vendor/wrangler-mime/Mime.js +0 -97
  168. package/vendor/wrangler-mime/README.md +0 -187
  169. package/vendor/wrangler-mime/cli.js +0 -46
  170. package/vendor/wrangler-mime/index.js +0 -4
  171. package/vendor/wrangler-mime/lite.js +0 -4
  172. package/vendor/wrangler-mime/package.json +0 -52
  173. package/vendor/wrangler-mime/types/other.js +0 -1
  174. package/vendor/wrangler-mime/types/standard.js +0 -1
  175. package/wrangler-dist/cli.js.map +0 -7
@@ -1,62 +0,0 @@
1
- import { findUpSync } from "find-up";
2
- import { parseTOML, readFileSync } from "../parse";
3
- import { normalizeAndValidateConfig } from "./validation";
4
- import type { Config, RawConfig } from "./config";
5
-
6
- export type {
7
- Config,
8
- RawConfig,
9
- ConfigFields,
10
- DevConfig,
11
- RawDevConfig,
12
- } from "./config";
13
- export type {
14
- Environment,
15
- RawEnvironment,
16
- ConfigModuleRuleType,
17
- } from "./environment";
18
-
19
- /**
20
- * Get the Wrangler configuration; read it from the give `configPath` if available.
21
- */
22
- export function readConfig(
23
- configPath: string | undefined,
24
- args: unknown
25
- ): Config {
26
- let rawConfig: RawConfig = {};
27
- if (!configPath) {
28
- configPath = findWranglerToml();
29
- }
30
-
31
- // Load the configuration from disk if available
32
- if (configPath) {
33
- rawConfig = parseTOML(readFileSync(configPath), configPath);
34
- }
35
-
36
- // Process the top-level configuration.
37
- const { config, diagnostics } = normalizeAndValidateConfig(
38
- rawConfig,
39
- configPath,
40
- args
41
- );
42
-
43
- if (diagnostics.hasWarnings()) {
44
- console.warn(diagnostics.renderWarnings());
45
- }
46
- if (diagnostics.hasErrors()) {
47
- throw new Error(diagnostics.renderErrors());
48
- }
49
-
50
- return config;
51
- }
52
-
53
- /**
54
- * Find the wrangler.toml file by searching up the file-system
55
- * from the current working directory.
56
- */
57
- export function findWranglerToml(
58
- referencePath: string = process.cwd()
59
- ): string | undefined {
60
- const configPath = findUpSync("wrangler.toml", { cwd: referencePath });
61
- return configPath;
62
- }
@@ -1,554 +0,0 @@
1
- import type { RawConfig } from "./config";
2
- import type { Diagnostics } from "./diagnostics";
3
- import type { Environment, RawEnvironment } from "./environment";
4
-
5
- /**
6
- * Mark a field as deprecated.
7
- *
8
- * This function will add a diagnostics warning if the deprecated field is found in the `rawEnv` (or an error if it's also a breaking deprecation)
9
- * The `fieldPath` is a dot separated property path, e.g. `"build.upload.format"`.
10
- */
11
- export function deprecated<T extends object>(
12
- diagnostics: Diagnostics,
13
- config: T,
14
- fieldPath: DeepKeyOf<T>,
15
- message: string,
16
- remove: boolean,
17
- breaking = false
18
- ): void {
19
- const result = unwindPropertyPath(config, fieldPath);
20
- if (result !== undefined && result.field in result.container) {
21
- (breaking ? diagnostics.errors : diagnostics.warnings).push(
22
- `DEPRECATION: "${fieldPath}":\n${message}`
23
- );
24
- if (remove) {
25
- delete (result.container as Record<string, unknown>)[result.field];
26
- }
27
- }
28
- }
29
-
30
- /**
31
- * Mark a field as experimental.
32
- *
33
- * This function will add a diagnostics warning if the experimental field is found in the `rawEnv`.
34
- * The `fieldPath` is a dot separated property path, e.g. `"build.upload.format"`.
35
- */
36
- export function experimental<T extends object>(
37
- diagnostics: Diagnostics,
38
- config: T,
39
- fieldPath: DeepKeyOf<T>
40
- ): void {
41
- const result = unwindPropertyPath(config, fieldPath);
42
- if (result !== undefined && result.field in result.container) {
43
- diagnostics.warnings.push(
44
- `"${fieldPath}" fields are experimental and may change or break at any time.`
45
- );
46
- }
47
- }
48
-
49
- /**
50
- * Get an inheritable environment field, after computing and validating its value.
51
- *
52
- * If the field is not defined in the given environment, then fallback to the value from the top-level config,
53
- * and then the `defaultValue`.
54
- */
55
- export function inheritable<K extends keyof Environment>(
56
- diagnostics: Diagnostics,
57
- topLevelEnv: Environment | undefined,
58
- rawEnv: RawEnvironment,
59
- field: K,
60
- validate: ValidatorFn,
61
- defaultValue: Environment[K],
62
- transformFn: TransformFn<Environment[K]> = (v) => v
63
- ): Environment[K] {
64
- validate(diagnostics, field, rawEnv[field], topLevelEnv);
65
- return (
66
- (rawEnv[field] as Environment[K]) ??
67
- transformFn(topLevelEnv?.[field]) ??
68
- defaultValue
69
- );
70
- }
71
-
72
- /**
73
- * Get an inheritable environment field, but only if we are in legacy environments
74
- */
75
- export function inheritableInLegacyEnvironments<K extends keyof Environment>(
76
- diagnostics: Diagnostics,
77
- isLegacyEnv: boolean | undefined,
78
- topLevelEnv: Environment | undefined,
79
- rawEnv: RawEnvironment,
80
- field: K,
81
- validate: ValidatorFn,
82
- transformFn: TransformFn<Environment[K]>,
83
- defaultValue: Environment[K]
84
- ): Environment[K] {
85
- return topLevelEnv === undefined || isLegacyEnv === true
86
- ? inheritable(
87
- diagnostics,
88
- topLevelEnv,
89
- rawEnv,
90
- field,
91
- validate,
92
- defaultValue,
93
- transformFn
94
- )
95
- : notAllowedInNamedServiceEnvironment(
96
- diagnostics,
97
- topLevelEnv,
98
- rawEnv,
99
- field
100
- );
101
- }
102
-
103
- /**
104
- * Type of function that is used to transform an inheritable environment field.
105
- */
106
- type TransformFn<T> = (fieldValue: T | undefined) => T | undefined;
107
-
108
- /**
109
- * Transform an environment field by appending current environment name to it.
110
- */
111
- export const appendEnvName =
112
- (envName: string): TransformFn<string | undefined> =>
113
- (fieldValue) =>
114
- fieldValue ? `${fieldValue}-${envName}` : undefined;
115
-
116
- /**
117
- * Log an error if this named environment is trying to override the value in the top-level
118
- * environment, which is not allow for this field.
119
- */
120
- function notAllowedInNamedServiceEnvironment<K extends keyof Environment>(
121
- diagnostics: Diagnostics,
122
- topLevelEnv: Environment,
123
- rawEnv: RawEnvironment,
124
- field: K
125
- ): Environment[K] {
126
- if (field in rawEnv) {
127
- diagnostics.errors.push(
128
- `The "${field}" field is not allowed in named service environments.\n` +
129
- `Please remove the field from this environment.`
130
- );
131
- }
132
- return topLevelEnv[field];
133
- }
134
-
135
- /**
136
- * Get a not inheritable environment field, after computing and validating its value.
137
- *
138
- * If the field is not defined in the given environment but it is defined in the top-level config,
139
- * then log a warning and return the `defaultValue`.
140
- */
141
- export function notInheritable<K extends keyof Environment>(
142
- diagnostics: Diagnostics,
143
- topLevelEnv: Environment | undefined,
144
- rawConfig: RawConfig | undefined,
145
- rawEnv: RawEnvironment,
146
- envName: string,
147
- field: K,
148
- validate: ValidatorFn,
149
- defaultValue: Environment[K]
150
- ): Environment[K] {
151
- if (rawEnv[field] !== undefined) {
152
- validate(diagnostics, field, rawEnv[field], topLevelEnv);
153
- } else {
154
- if (rawConfig?.[field] !== undefined) {
155
- diagnostics.warnings.push(
156
- `"${field}" exists at the top level, but not on "env.${envName}".\n` +
157
- `This is not what you probably want, since "${field}" is not inherited by environments.\n` +
158
- `Please add "${field}" to "env.${envName}".`
159
- );
160
- }
161
- }
162
- return (rawEnv[field] as Environment[K]) ?? defaultValue;
163
- }
164
-
165
- // Idea taken from https://stackoverflow.com/a/66661477
166
- type DeepKeyOf<T> = (
167
- T extends object
168
- ? {
169
- [K in Exclude<keyof T, symbol>]: `${K}${DotPrefix<DeepKeyOf<T[K]>>}`;
170
- }[Exclude<keyof T, symbol>]
171
- : ""
172
- ) extends infer D
173
- ? Extract<D, string>
174
- : never;
175
-
176
- type DotPrefix<T extends string> = T extends "" ? "" : `.${T}`;
177
-
178
- /**
179
- * Return a container object and field name for the last property in a given property path.
180
- *
181
- * For example, given a path of `"build.upload.format"`) and a starting `root` object
182
- * this will return:
183
- *
184
- * ```
185
- * { container: root.build.upload, field: "format" }
186
- * ```
187
- */
188
- function unwindPropertyPath<T extends object>(
189
- root: T,
190
- path: DeepKeyOf<T>
191
- ): { container: object; field: string } | undefined {
192
- let container: object = root;
193
- const parts = (path as string).split(".");
194
- for (let i = 0; i < parts.length - 1; i++) {
195
- if (!hasProperty(container, parts[i])) {
196
- return;
197
- }
198
- container = container[parts[i]];
199
- }
200
- return { container, field: parts[parts.length - 1] };
201
- }
202
-
203
- /**
204
- * The type of a function that can be used to validate a configuration field.
205
- */
206
- export type ValidatorFn = (
207
- diagnostics: Diagnostics,
208
- field: string,
209
- value: unknown,
210
- topLevelEnv: Environment | undefined
211
- ) => boolean;
212
-
213
- /**
214
- * Validate that the field is a string.
215
- */
216
- export const isString: ValidatorFn = (diagnostics, field, value) => {
217
- if (value !== undefined && typeof value !== "string") {
218
- diagnostics.errors.push(
219
- `Expected "${field}" to be of type string but got ${JSON.stringify(
220
- value
221
- )}.`
222
- );
223
- return false;
224
- }
225
- return true;
226
- };
227
-
228
- /**
229
- * Validate that the field is an array of strings.
230
- */
231
- export const isStringArray: ValidatorFn = (diagnostics, field, value) => {
232
- if (
233
- value !== undefined &&
234
- (!Array.isArray(value) || value.some((item) => typeof item !== "string"))
235
- ) {
236
- diagnostics.errors.push(
237
- `Expected "${field}" to be of type string array but got ${JSON.stringify(
238
- value
239
- )}.`
240
- );
241
- return false;
242
- }
243
- return true;
244
- };
245
-
246
- /**
247
- * Validate that the field is an object containing the given properties.
248
- */
249
- export const isObjectWith =
250
- (...properties: string[]): ValidatorFn =>
251
- (diagnostics, field, value) => {
252
- if (
253
- value !== undefined &&
254
- (typeof value !== "object" ||
255
- value === null ||
256
- !properties.every((prop) => prop in value))
257
- ) {
258
- diagnostics.errors.push(
259
- `Expected "${field}" to be of type object, containing properties ${properties}, but got ${JSON.stringify(
260
- value
261
- )}.`
262
- );
263
- return false;
264
- }
265
- return true;
266
- };
267
-
268
- /**
269
- * Validate that the field value is one of the given choices.
270
- */
271
- export const isOneOf =
272
- (...choices: unknown[]): ValidatorFn =>
273
- (diagnostics, field, value) => {
274
- if (value !== undefined && !choices.some((choice) => value === choice)) {
275
- diagnostics.errors.push(
276
- `Expected "${field}" field to be one of ${JSON.stringify(
277
- choices
278
- )} but got ${JSON.stringify(value)}.`
279
- );
280
- return false;
281
- }
282
- return true;
283
- };
284
-
285
- /**
286
- * Aggregate multiple validator functions
287
- */
288
- export const all = (...validations: ValidatorFn[]): ValidatorFn => {
289
- return (diagnostics, field, value, config) => {
290
- let passedValidations = true;
291
-
292
- for (const validate of validations) {
293
- if (!validate(diagnostics, field, value, config)) {
294
- passedValidations = false;
295
- }
296
- }
297
-
298
- return passedValidations;
299
- };
300
- };
301
-
302
- /**
303
- * Check that the field is mutually exclusive with a list of other fields.
304
- *
305
- * @param container the container of the fields to check against.
306
- * @param fields the names of the fields to check against.
307
- */
308
- export const isMutuallyExclusiveWith = <T extends RawEnvironment | RawConfig>(
309
- container: T,
310
- ...fields: (keyof T)[]
311
- ): ValidatorFn => {
312
- return (diagnostics, field, value) => {
313
- if (value === undefined) {
314
- return true;
315
- }
316
-
317
- for (const exclusiveWith of fields) {
318
- if (container[exclusiveWith] !== undefined) {
319
- diagnostics.errors.push(
320
- `Expected exactly one of the following fields ${JSON.stringify([
321
- field,
322
- ...fields,
323
- ])}.`
324
- );
325
- return false;
326
- }
327
- }
328
-
329
- return true;
330
- };
331
- };
332
-
333
- /**
334
- * Validate that the field is a boolean.
335
- */
336
- export const isBoolean: ValidatorFn = (diagnostics, field, value) => {
337
- if (value !== undefined && typeof value !== "boolean") {
338
- diagnostics.errors.push(
339
- `Expected "${field}" to be of type boolean but got ${JSON.stringify(
340
- value
341
- )}.`
342
- );
343
- return false;
344
- }
345
- return true;
346
- };
347
-
348
- /**
349
- * Validate that the required field exists and has the expected type.
350
- */
351
- export const validateRequiredProperty = (
352
- diagnostics: Diagnostics,
353
- container: string,
354
- key: string,
355
- value: unknown,
356
- type: string,
357
- choices?: unknown[]
358
- ): boolean => {
359
- if (container) {
360
- container += ".";
361
- }
362
- if (value === undefined) {
363
- diagnostics.errors.push(`"${container}${key}" is a required field.`);
364
- return false;
365
- } else if (typeof value !== type) {
366
- diagnostics.errors.push(
367
- `Expected "${container}${key}" to be of type ${type} but got ${JSON.stringify(
368
- value
369
- )}.`
370
- );
371
- return false;
372
- } else if (choices) {
373
- if (
374
- !isOneOf(...choices)(diagnostics, `${container}${key}`, value, undefined)
375
- ) {
376
- return false;
377
- }
378
- }
379
- return true;
380
- };
381
-
382
- /**
383
- * Validate that, if the optional field exists, then it has the expected type.
384
- */
385
- export const validateOptionalProperty = (
386
- diagnostics: Diagnostics,
387
- container: string,
388
- key: string,
389
- value: unknown,
390
- type: string,
391
- choices?: unknown[]
392
- ): boolean => {
393
- if (value !== undefined) {
394
- return validateRequiredProperty(
395
- diagnostics,
396
- container,
397
- key,
398
- value,
399
- type,
400
- choices
401
- );
402
- }
403
- return true;
404
- };
405
-
406
- /**
407
- * Validate that the field is an array of elements of the given type.
408
- */
409
- export const validateTypedArray = (
410
- diagnostics: Diagnostics,
411
- container: string,
412
- value: unknown,
413
- type: string
414
- ): boolean => {
415
- let isValid = true;
416
- if (!Array.isArray(value)) {
417
- diagnostics.errors.push(
418
- `Expected "${container}" to be an array of ${type}s but got ${JSON.stringify(
419
- value
420
- )}`
421
- );
422
- isValid = false;
423
- } else {
424
- for (let i = 0; i < value.length; i++) {
425
- isValid =
426
- validateRequiredProperty(
427
- diagnostics,
428
- container,
429
- `[${i}]`,
430
- value[i],
431
- type
432
- ) && isValid;
433
- }
434
- }
435
- return isValid;
436
- };
437
-
438
- /**
439
- * Validate that, if the optional field exists, it is an array of elements of the given type.
440
- */
441
- export const validateOptionalTypedArray = (
442
- diagnostics: Diagnostics,
443
- container: string,
444
- value: unknown,
445
- type: string
446
- ) => {
447
- if (value !== undefined) {
448
- return validateTypedArray(diagnostics, container, value, type);
449
- }
450
- return true;
451
- };
452
-
453
- /**
454
- * Test to see if `obj` has the required property `prop` of type `type`.
455
- */
456
- export const isRequiredProperty = <T extends object>(
457
- obj: object,
458
- prop: keyof T,
459
- type: string,
460
- choices?: unknown[]
461
- ): obj is T =>
462
- hasProperty<T>(obj, prop) &&
463
- typeof obj[prop] === type &&
464
- (choices === undefined || choices.includes(obj[prop]));
465
-
466
- /**
467
- * Test to see if `obj` has the optional property `prop` of type `type`.
468
- */
469
- export const isOptionalProperty = <T extends object>(
470
- obj: object,
471
- prop: keyof T,
472
- type: string
473
- ): obj is T => !hasProperty<T>(obj, prop) || typeof obj[prop] === type;
474
-
475
- /**
476
- * Test to see if `obj` has the property `prop`.
477
- */
478
- export const hasProperty = <T extends object>(
479
- obj: object,
480
- property: keyof T
481
- ): obj is T => property in obj;
482
-
483
- /**
484
- * Add warning messages about any properties in the given field that are not expected to be there.
485
- */
486
- export const validateAdditionalProperties = (
487
- diagnostics: Diagnostics,
488
- fieldPath: string,
489
- restProps: Iterable<string>,
490
- knownProps: Iterable<string>
491
- ): boolean => {
492
- const restPropSet = new Set(restProps);
493
- for (const knownProp of knownProps) {
494
- restPropSet.delete(knownProp);
495
- }
496
- if (restPropSet.size > 0) {
497
- const fields = Array.from(restPropSet.keys()).map((field) => `"${field}"`);
498
- diagnostics.warnings.push(
499
- `Unexpected fields found in ${fieldPath} field: ${fields}`
500
- );
501
- return false;
502
- }
503
- return true;
504
- };
505
-
506
- /**
507
- * Get the names of the bindings collection in `value`.
508
- *
509
- * Will return an empty array if it doesn't understand the value
510
- * passed in, so another form of validation should be
511
- * performed externally.
512
- */
513
- export const getBindingNames = (value: unknown): string[] => {
514
- if (typeof value !== "object" || value === null) {
515
- return [];
516
- }
517
-
518
- if (isBindingList(value)) {
519
- return value.bindings.map(({ name }) => name);
520
- } else if (isNamespaceList(value)) {
521
- return value.map(({ binding }) => binding);
522
- } else if (isRecord(value)) {
523
- return Object.keys(value);
524
- } else {
525
- return [];
526
- }
527
- };
528
-
529
- const isBindingList = (
530
- value: unknown
531
- ): value is {
532
- bindings: {
533
- name: string;
534
- }[];
535
- } =>
536
- isRecord(value) &&
537
- "bindings" in value &&
538
- Array.isArray(value.bindings) &&
539
- value.bindings.every(
540
- (binding) =>
541
- isRecord(binding) && "name" in binding && typeof binding.name === "string"
542
- );
543
-
544
- const isNamespaceList = (value: unknown): value is { binding: string }[] =>
545
- Array.isArray(value) &&
546
- value.every(
547
- (entry) =>
548
- isRecord(entry) && "binding" in entry && typeof entry.binding === "string"
549
- );
550
-
551
- const isRecord = (
552
- value: unknown
553
- ): value is Record<string | number | symbol, unknown> =>
554
- typeof value === "object" && value !== null && !Array.isArray(value);