wrangler 0.0.0-e61907d → 0.0.0-e95bea2

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 (93) hide show
  1. package/README.md +51 -0
  2. package/bin/wrangler.js +36 -2
  3. package/import_meta_url.js +3 -0
  4. package/package.json +79 -21
  5. package/pages/functions/buildWorker.ts +62 -0
  6. package/pages/functions/filepath-routing.test.ts +123 -0
  7. package/pages/functions/filepath-routing.ts +178 -0
  8. package/pages/functions/identifiers.ts +78 -0
  9. package/pages/functions/routes.ts +151 -0
  10. package/pages/functions/template-worker.ts +130 -0
  11. package/src/__tests__/configuration.test.ts +2217 -0
  12. package/src/__tests__/dev.test.tsx +405 -0
  13. package/src/__tests__/dev2.test.tsx +152 -0
  14. package/src/__tests__/guess-worker-format.test.ts +58 -0
  15. package/src/__tests__/helpers/clipboardy-mock.js +4 -0
  16. package/src/__tests__/helpers/cmd-shim.d.ts +11 -0
  17. package/src/__tests__/helpers/faye-websocket.d.ts +6 -0
  18. package/src/__tests__/helpers/mock-account-id.ts +44 -0
  19. package/src/__tests__/helpers/mock-bin.ts +36 -0
  20. package/src/__tests__/helpers/mock-cfetch.ts +207 -0
  21. package/src/__tests__/helpers/mock-console.ts +70 -0
  22. package/src/__tests__/helpers/mock-dialogs.ts +65 -0
  23. package/src/__tests__/helpers/mock-kv.ts +40 -0
  24. package/src/__tests__/helpers/mock-stdin.ts +103 -0
  25. package/src/__tests__/helpers/mock-user.ts +27 -0
  26. package/src/__tests__/helpers/mock-web-socket.ts +29 -0
  27. package/src/__tests__/helpers/run-in-tmp.ts +29 -0
  28. package/src/__tests__/helpers/run-wrangler.ts +16 -0
  29. package/src/__tests__/helpers/write-wrangler-toml.ts +17 -0
  30. package/src/__tests__/https-options.test.ts +127 -0
  31. package/src/__tests__/index.test.ts +991 -0
  32. package/src/__tests__/jest.setup.ts +69 -0
  33. package/src/__tests__/kv.test.ts +1408 -0
  34. package/src/__tests__/logout.test.ts +50 -0
  35. package/src/__tests__/package-manager.test.ts +206 -0
  36. package/src/__tests__/pages.test.ts +44 -0
  37. package/src/__tests__/parse.test.ts +322 -0
  38. package/src/__tests__/publish.test.ts +2453 -0
  39. package/src/__tests__/r2.test.ts +198 -0
  40. package/src/__tests__/route.test.ts +45 -0
  41. package/src/__tests__/secret.test.ts +425 -0
  42. package/src/__tests__/sentry.test.ts +146 -0
  43. package/src/__tests__/tail.test.ts +564 -0
  44. package/src/__tests__/whoami.test.tsx +86 -0
  45. package/src/bundle.ts +142 -0
  46. package/src/cfetch/index.ts +104 -0
  47. package/src/cfetch/internal.ts +135 -0
  48. package/src/cli.ts +19 -0
  49. package/src/config/README.md +107 -0
  50. package/src/config/config.ts +279 -0
  51. package/src/config/diagnostics.ts +80 -0
  52. package/src/config/environment.ts +305 -0
  53. package/src/config/index.ts +52 -0
  54. package/src/config/validation-helpers.ts +388 -0
  55. package/src/config/validation.ts +1027 -0
  56. package/src/create-worker-preview.ts +166 -0
  57. package/src/create-worker-upload-form.ts +217 -0
  58. package/src/dev/dev.tsx +357 -0
  59. package/src/dev/local.tsx +241 -0
  60. package/src/dev/remote.tsx +201 -0
  61. package/src/dev/use-esbuild.ts +100 -0
  62. package/src/dialogs.tsx +77 -0
  63. package/src/entry.ts +159 -0
  64. package/src/environment-variables.ts +33 -0
  65. package/src/https-options.ts +122 -0
  66. package/src/index.tsx +2329 -0
  67. package/src/inspect.ts +617 -0
  68. package/src/kv/index.tsx +339 -0
  69. package/src/module-collection.ts +253 -0
  70. package/src/open-in-browser.ts +25 -0
  71. package/src/package-manager.ts +126 -0
  72. package/src/pages.tsx +1045 -0
  73. package/src/parse.ts +183 -0
  74. package/src/paths.ts +26 -0
  75. package/src/proxy.ts +438 -0
  76. package/src/publish.ts +354 -0
  77. package/src/r2.ts +50 -0
  78. package/src/reporting.ts +131 -0
  79. package/src/selfsigned.d.ts +29 -0
  80. package/src/sites.tsx +238 -0
  81. package/src/tail/filters.ts +279 -0
  82. package/src/tail/index.ts +299 -0
  83. package/src/tail/printing.ts +62 -0
  84. package/src/user.tsx +1065 -0
  85. package/src/whoami.tsx +69 -0
  86. package/src/worker.ts +163 -0
  87. package/templates/new-worker.js +15 -0
  88. package/templates/new-worker.ts +15 -0
  89. package/{static-asset-facade.js → templates/static-asset-facade.js} +0 -0
  90. package/templates/tsconfig.json +105 -0
  91. package/wrangler-dist/{index_node.js → cli.js} +105532 -67432
  92. package/wrangler-dist/cli.js.map +7 -0
  93. package/wrangler-dist/index_node.js.map +0 -7
@@ -0,0 +1,388 @@
1
+ import type { Config, 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
+ config: Config | undefined,
58
+ rawEnv: RawEnvironment,
59
+ field: K,
60
+ validate: ValidatorFn,
61
+ defaultValue: Environment[K]
62
+ ): Environment[K] {
63
+ validate(diagnostics, field, rawEnv[field], config);
64
+ return (rawEnv[field] as Environment[K]) ?? config?.[field] ?? defaultValue;
65
+ }
66
+
67
+ /**
68
+ * Get a not inheritable environment field, after computing and validating its value.
69
+ *
70
+ * If the field is not defined in the given environment but it is defined in the top-level config,
71
+ * then log a warning and return the `defaultValue`.
72
+ */
73
+ export function notInheritable<K extends keyof Environment>(
74
+ diagnostics: Diagnostics,
75
+ config: Config | undefined,
76
+ rawConfig: RawConfig | undefined,
77
+ rawEnv: RawEnvironment,
78
+ envName: string,
79
+ field: K,
80
+ validate: ValidatorFn,
81
+ defaultValue: Environment[K]
82
+ ): Environment[K] {
83
+ if (rawEnv[field] !== undefined) {
84
+ validate(diagnostics, field, rawEnv[field], config);
85
+ } else {
86
+ if (rawConfig?.[field] !== undefined) {
87
+ diagnostics.warnings.push(
88
+ `"${field}" exists at the top level, but not on "env.${envName}".\n` +
89
+ `This is not what you probably want, since "${field}" is not inherited by environments.\n` +
90
+ `Please add "${field}" to "env.${envName}".`
91
+ );
92
+ }
93
+ }
94
+ return (rawEnv[field] as Environment[K]) ?? defaultValue;
95
+ }
96
+
97
+ // Idea taken from https://stackoverflow.com/a/66661477
98
+ type DeepKeyOf<T> = (
99
+ T extends object
100
+ ? {
101
+ [K in Exclude<keyof T, symbol>]: `${K}${DotPrefix<DeepKeyOf<T[K]>>}`;
102
+ }[Exclude<keyof T, symbol>]
103
+ : ""
104
+ ) extends infer D
105
+ ? Extract<D, string>
106
+ : never;
107
+
108
+ type DotPrefix<T extends string> = T extends "" ? "" : `.${T}`;
109
+
110
+ /**
111
+ * Return a container object and field name for the last property in a given property path.
112
+ *
113
+ * For example, given a path of `"build.upload.format"`) and a starting `root` object
114
+ * this will return:
115
+ *
116
+ * ```
117
+ * { container: root.build.upload, field: "format" }
118
+ * ```
119
+ */
120
+ function unwindPropertyPath<T extends object>(
121
+ root: T,
122
+ path: DeepKeyOf<T>
123
+ ): { container: object; field: string } | undefined {
124
+ let container: object = root;
125
+ const parts = (path as string).split(".");
126
+ for (let i = 0; i < parts.length - 1; i++) {
127
+ if (!hasProperty(container, parts[i])) {
128
+ return;
129
+ }
130
+ container = container[parts[i]];
131
+ }
132
+ return { container, field: parts[parts.length - 1] };
133
+ }
134
+
135
+ /**
136
+ * The type of a function that can be used to validate a configuration field.
137
+ */
138
+ export type ValidatorFn = (
139
+ diagnostics: Diagnostics,
140
+ field: string,
141
+ value: unknown,
142
+ config: Config | undefined
143
+ ) => boolean;
144
+
145
+ /**
146
+ * Validate that the field is a string.
147
+ */
148
+ export const isString: ValidatorFn = (diagnostics, field, value) => {
149
+ if (value !== undefined && typeof value !== "string") {
150
+ diagnostics.errors.push(
151
+ `Expected "${field}" field to be a string but got ${JSON.stringify(
152
+ value
153
+ )}.`
154
+ );
155
+ return false;
156
+ }
157
+ return true;
158
+ };
159
+
160
+ /**
161
+ * Validate that the field is an array of strings.
162
+ */
163
+ export const isStringArray: ValidatorFn = (diagnostics, field, value) => {
164
+ if (
165
+ value !== undefined &&
166
+ (!Array.isArray(value) || value.some((item) => typeof item !== "string"))
167
+ ) {
168
+ diagnostics.errors.push(
169
+ `Expected "${field}" field to be an array of strings but got ${JSON.stringify(
170
+ value
171
+ )}.`
172
+ );
173
+ return false;
174
+ }
175
+ return true;
176
+ };
177
+
178
+ /**
179
+ * Validate that the field is an object containing the given properties.
180
+ */
181
+ export const isObjectWith =
182
+ (...properties: string[]): ValidatorFn =>
183
+ (diagnostics, field, value) => {
184
+ if (
185
+ value !== undefined &&
186
+ (typeof value !== "object" ||
187
+ value === null ||
188
+ !properties.every((prop) => prop in value))
189
+ ) {
190
+ diagnostics.errors.push(
191
+ `Expected "${field}" field to be an object containing properties ${properties} but got ${JSON.stringify(
192
+ value
193
+ )}.`
194
+ );
195
+ return false;
196
+ }
197
+ return true;
198
+ };
199
+
200
+ /**
201
+ * Validate that the field value is one of the given choices.
202
+ */
203
+ export const isOneOf =
204
+ (...choices: unknown[]): ValidatorFn =>
205
+ (diagnostics, field, value) => {
206
+ if (value !== undefined && !choices.some((choice) => value === choice)) {
207
+ diagnostics.errors.push(
208
+ `Expected "${field}" field to be one of ${JSON.stringify(
209
+ choices
210
+ )} but got ${JSON.stringify(value)}.`
211
+ );
212
+ return false;
213
+ }
214
+ return true;
215
+ };
216
+
217
+ /**
218
+ * Validate that the field is a boolean.
219
+ */
220
+ export const isBoolean: ValidatorFn = (diagnostics, field, value) => {
221
+ if (value !== undefined && typeof value !== "boolean") {
222
+ diagnostics.errors.push(
223
+ `Expected "${field}" field to be a boolean but got ${JSON.stringify(
224
+ value
225
+ )}.`
226
+ );
227
+ return false;
228
+ }
229
+ return true;
230
+ };
231
+
232
+ /**
233
+ * Validate that the required field exists and has the expected type.
234
+ */
235
+ export const validateRequiredProperty = (
236
+ diagnostics: Diagnostics,
237
+ container: string,
238
+ key: string,
239
+ value: unknown,
240
+ type: string,
241
+ choices?: unknown[]
242
+ ): boolean => {
243
+ if (container) {
244
+ container += ".";
245
+ }
246
+ if (value === undefined) {
247
+ diagnostics.errors.push(`"${container}${key}" is a required field.`);
248
+ return false;
249
+ } else if (typeof value !== type) {
250
+ diagnostics.errors.push(
251
+ `Expected "${container}${key}" to be of type ${type} but got ${JSON.stringify(
252
+ value
253
+ )}.`
254
+ );
255
+ return false;
256
+ } else if (choices) {
257
+ if (
258
+ !isOneOf(...choices)(diagnostics, `${container}${key}`, value, undefined)
259
+ ) {
260
+ return false;
261
+ }
262
+ }
263
+ return true;
264
+ };
265
+
266
+ /**
267
+ * Validate that, if the optional field exists, then it has the expected type.
268
+ */
269
+ export const validateOptionalProperty = (
270
+ diagnostics: Diagnostics,
271
+ container: string,
272
+ key: string,
273
+ value: unknown,
274
+ type: string,
275
+ choices?: unknown[]
276
+ ): boolean => {
277
+ if (value !== undefined) {
278
+ return validateRequiredProperty(
279
+ diagnostics,
280
+ container,
281
+ key,
282
+ value,
283
+ type,
284
+ choices
285
+ );
286
+ }
287
+ return true;
288
+ };
289
+
290
+ /**
291
+ * Validate that the field is an array of elements of the given type.
292
+ */
293
+ export const validateTypedArray = (
294
+ diagnostics: Diagnostics,
295
+ container: string,
296
+ value: unknown,
297
+ type: string
298
+ ): boolean => {
299
+ let isValid = true;
300
+ if (!Array.isArray(value)) {
301
+ diagnostics.errors.push(
302
+ `Expected "${container}" to be an array of ${type}s but got ${JSON.stringify(
303
+ value
304
+ )}`
305
+ );
306
+ isValid = false;
307
+ } else {
308
+ for (let i = 0; i < value.length; i++) {
309
+ isValid =
310
+ validateRequiredProperty(
311
+ diagnostics,
312
+ container,
313
+ `[${i}]`,
314
+ value[i],
315
+ type
316
+ ) && isValid;
317
+ }
318
+ }
319
+ return isValid;
320
+ };
321
+
322
+ /**
323
+ * Validate that, if the optional field exists, it is an array of elements of the given type.
324
+ */
325
+ export const validateOptionalTypedArray = (
326
+ diagnostics: Diagnostics,
327
+ container: string,
328
+ value: unknown,
329
+ type: string
330
+ ) => {
331
+ if (value !== undefined) {
332
+ return validateTypedArray(diagnostics, container, value, type);
333
+ }
334
+ return true;
335
+ };
336
+
337
+ /**
338
+ * Test to see if `obj` has the required property `prop` of type `type`.
339
+ */
340
+ export const isRequiredProperty = <T extends object>(
341
+ obj: object,
342
+ prop: keyof T,
343
+ type: string,
344
+ choices?: unknown[]
345
+ ): obj is T =>
346
+ hasProperty<T>(obj, prop) &&
347
+ typeof obj[prop] === type &&
348
+ (choices === undefined || choices.includes(obj[prop]));
349
+
350
+ /**
351
+ * Test to see if `obj` has the optional property `prop` of type `type`.
352
+ */
353
+ export const isOptionalProperty = <T extends object>(
354
+ obj: object,
355
+ prop: keyof T,
356
+ type: string
357
+ ): obj is T => !hasProperty<T>(obj, prop) || typeof obj[prop] === type;
358
+
359
+ /**
360
+ * Test to see if `obj` has the property `prop`.
361
+ */
362
+ export const hasProperty = <T extends object>(
363
+ obj: object,
364
+ property: keyof T
365
+ ): obj is T => property in obj;
366
+
367
+ /**
368
+ * Add warning messages about any properties in the given field that are not expected to be there.
369
+ */
370
+ export const validateAdditionalProperties = (
371
+ diagnostics: Diagnostics,
372
+ fieldPath: string,
373
+ restProps: Iterable<string>,
374
+ knownProps: Iterable<string>
375
+ ): boolean => {
376
+ const restPropSet = new Set(restProps);
377
+ for (const knownProp of knownProps) {
378
+ restPropSet.delete(knownProp);
379
+ }
380
+ if (restPropSet.size > 0) {
381
+ const fields = Array.from(restPropSet.keys()).map((field) => `"${field}"`);
382
+ diagnostics.warnings.push(
383
+ `Unexpected fields found in ${fieldPath} field: ${fields}`
384
+ );
385
+ return false;
386
+ }
387
+ return true;
388
+ };