wrangler 0.0.0-ece06ea → 0.0.0-ef13735

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 (63) hide show
  1. package/bin/wrangler.js +2 -2
  2. package/package.json +35 -21
  3. package/pages/functions/buildWorker.ts +62 -0
  4. package/pages/functions/filepath-routing.test.ts +66 -0
  5. package/pages/functions/filepath-routing.ts +219 -0
  6. package/pages/functions/identifiers.ts +78 -0
  7. package/pages/functions/routes.ts +151 -0
  8. package/pages/functions/template-worker.ts +130 -0
  9. package/src/__tests__/dev.test.tsx +73 -0
  10. package/src/__tests__/guess-worker-format.test.ts +51 -0
  11. package/src/__tests__/{clipboardy-mock.js → helpers/clipboardy-mock.js} +0 -0
  12. package/src/__tests__/helpers/cmd-shim.d.ts +11 -0
  13. package/src/__tests__/helpers/mock-account-id.ts +30 -0
  14. package/src/__tests__/helpers/mock-bin.ts +33 -0
  15. package/src/__tests__/helpers/mock-cfetch.ts +207 -0
  16. package/src/__tests__/helpers/mock-console.ts +56 -0
  17. package/src/__tests__/helpers/mock-dialogs.ts +65 -0
  18. package/src/__tests__/helpers/mock-kv.ts +40 -0
  19. package/src/__tests__/helpers/mock-user.ts +27 -0
  20. package/src/__tests__/helpers/run-in-tmp.ts +29 -0
  21. package/src/__tests__/helpers/run-wrangler.ts +14 -0
  22. package/src/__tests__/index.test.ts +715 -119
  23. package/src/__tests__/jest.setup.ts +37 -0
  24. package/src/__tests__/kv.test.ts +1139 -0
  25. package/src/__tests__/logout.test.ts +50 -0
  26. package/src/__tests__/package-manager.test.ts +206 -0
  27. package/src/__tests__/publish.test.ts +1381 -0
  28. package/src/__tests__/secret.test.ts +210 -0
  29. package/src/__tests__/whoami.test.tsx +86 -0
  30. package/src/api/form_data.ts +93 -100
  31. package/src/api/preview.ts +29 -24
  32. package/src/api/worker.ts +55 -47
  33. package/src/cfetch/index.ts +102 -0
  34. package/src/cfetch/internal.ts +101 -0
  35. package/src/cli.ts +6 -7
  36. package/src/config.ts +641 -112
  37. package/src/dev.tsx +476 -355
  38. package/src/dialogs.tsx +6 -14
  39. package/src/guess-worker-format.ts +66 -0
  40. package/src/index.tsx +828 -496
  41. package/src/inspect.ts +596 -0
  42. package/src/kv.tsx +118 -45
  43. package/src/module-collection.ts +63 -0
  44. package/src/open-in-browser.ts +13 -0
  45. package/src/package-manager.ts +120 -0
  46. package/src/pages.tsx +915 -215
  47. package/src/paths.ts +26 -0
  48. package/src/proxy.ts +370 -0
  49. package/src/publish.ts +349 -290
  50. package/src/sites.tsx +184 -55
  51. package/src/tail.tsx +5 -3
  52. package/src/user.tsx +81 -91
  53. package/src/whoami.tsx +69 -0
  54. package/templates/new-worker.js +15 -0
  55. package/templates/new-worker.ts +15 -0
  56. package/{static-asset-facade.js → templates/static-asset-facade.js} +0 -0
  57. package/wrangler-dist/cli.js +119231 -130682
  58. package/wrangler-dist/cli.js.map +3 -3
  59. package/src/__tests__/fixtures/init/.gitkeep +0 -0
  60. package/src/__tests__/mock-cfetch.js +0 -46
  61. package/src/api/inspect.ts +0 -441
  62. package/src/cfetch.ts +0 -72
  63. package/src/util/fetch.ts +0 -74
package/src/config.ts CHANGED
@@ -1,124 +1,653 @@
1
- // we're going to manually write both the type definition AND
2
- // the validator for the config, so that we can give better error messages
3
-
4
- type DOMigration = {
5
- tag: string;
6
- new_classes?: string[];
7
- renamed_classes?: string[];
8
- deleted_classes?: string[];
9
- };
1
+ import assert from "node:assert";
10
2
 
11
- type Project = "webpack" | "javascript" | "rust";
3
+ /**
4
+ * This is the static type definition for the configuration object.
5
+ * It reflects the configuration that you can write in wrangler.toml,
6
+ * and optionally augment with arguments passed directly to wrangler.
7
+ * The type definition doesn't fully reflect the constraints applied
8
+ * to the configuration, but it is a good starting point. Later, we
9
+ * also defined a validator function that will validate the configuration
10
+ * with the same rules as the type definition, as well as the extra
11
+ * constraints. The type definition is good for asserting correctness
12
+ * in the wrangler codebase, whereas the validator function is useful
13
+ * for signalling errors in the configuration to a user of wrangler.
14
+ *
15
+ * For more information about the configuration object, see the
16
+ * documentation at https://developers.cloudflare.com/workers/cli-wrangler/configuration
17
+ *
18
+ * Legend for the annotations:
19
+ *
20
+ * *:optional means providing a value isn't mandatory
21
+ * *:deprecated means the field itself isn't necessary anymore in wrangler.toml
22
+ * *:breaking means the deprecation/optionality is a breaking change from wrangler 1
23
+ * *:todo means there's more work to be done (with details attached)
24
+ * *:inherited means the field is copied to all environments
25
+ */
26
+ export type Config = {
27
+ /**
28
+ * The name of your worker. Alphanumeric + dashes only.
29
+ *
30
+ * @optional
31
+ * @inherited
32
+ */
33
+ name?: string;
12
34
 
13
- type Site = {
14
- // inherited
15
- bucket: string;
16
- "entry-point": string;
17
- include?: string[];
18
- exclude?: string[];
19
- };
35
+ /**
36
+ * The entrypoint/path to the JavaScript file that will be executed.
37
+ *
38
+ * @optional
39
+ * @inherited
40
+ * @todo this needs to be implemented!
41
+ */
42
+ entry?: string;
20
43
 
21
- type Dev = {
22
- ip?: string;
23
- port?: number;
24
- local_protocol?: string;
25
- upstream_protocol?: string;
26
- };
44
+ /**
45
+ * This is the ID of the account associated with your zone.
46
+ * You might have more than one account, so make sure to use
47
+ * the ID of the account associated with the zone/route you
48
+ * provide, if you provide one. It can also be specified through
49
+ * the CF_ACCOUNT_ID environment variable.
50
+ *
51
+ * @optional
52
+ * @inherited
53
+ */
54
+ account_id?: string;
27
55
 
28
- type Vars = { [key: string]: string };
56
+ /**
57
+ * The project "type". A holdover from wrangler 1.x.
58
+ * Valid values were "webpack", "javascript", and "rust".
59
+ *
60
+ * @deprecated 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
+ * @optional
62
+ * @inherited
63
+ * @breaking
64
+ */
65
+ type?: "webpack" | "javascript" | "rust";
29
66
 
30
- type Cron = string; // TODO: we should be able to parse a cron pattern with ts
67
+ /**
68
+ * A date in the form yyyy-mm-dd, which will be used to determine
69
+ * which version of the Workers runtime is used. More details at
70
+ * https://developers.cloudflare.com/workers/platform/compatibility-dates
71
+ * @optional true for `dev`, false for `publish`
72
+ * @inherited
73
+ */
74
+ compatibility_date?: string;
31
75
 
32
- type KVNamespace = {
33
- binding?: string;
34
- preview_id: string;
35
- id: string;
36
- };
76
+ /**
77
+ * A list of flags that enable features from upcoming features of
78
+ * the Workers runtime, usually used together with compatibility_flags.
79
+ * More details at
80
+ * https://developers.cloudflare.com/workers/platform/compatibility-dates
81
+ *
82
+ * @optional
83
+ * @inherited
84
+ */
85
+ compatibility_flags?: string[];
86
+
87
+ /**
88
+ * Whether we use <name>.<subdomain>.workers.dev to
89
+ * test and deploy your worker.
90
+ *
91
+ * @default `true` (This is a breaking change from wrangler 1)
92
+ * @optional
93
+ * @inherited
94
+ * @breaking
95
+ */
96
+ workers_dev?: boolean;
97
+
98
+ /**
99
+ * The zone ID of the zone you want to deploy to. You can find this
100
+ * in your domain page on the dashboard.
101
+ *
102
+ * @deprecated This is unnecessary since we can deduce this from routes directly.
103
+ * @optional
104
+ * @inherited
105
+ */
106
+ zone_id?: string;
107
+
108
+ /**
109
+ * A list of routes that your worker should be deployed to.
110
+ * Only one of `routes` or `route` is required.
111
+ *
112
+ * @optional false only when workers_dev is false, and there's no scheduled worker
113
+ * @inherited
114
+ */
115
+ routes?: string[];
116
+
117
+ /**
118
+ * A route that your worker should be deployed to. Literally
119
+ * the same as routes, but only one.
120
+ * Only one of `routes` or `route` is required.
121
+ *
122
+ * @optional false only when workers_dev is false, and there's no scheduled worker
123
+ * @inherited
124
+ */
125
+ route?: string;
126
+
127
+ /**
128
+ * Path to the webpack config to use when building your worker.
129
+ * A holdover from wrangler 1.x, used with `type: "webpack"`.
130
+ *
131
+ * @deprecated 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.
132
+ * @inherited
133
+ * @breaking
134
+ */
135
+ webpack_config?: string;
136
+
137
+ /**
138
+ * The function to use to replace jsx syntax.
139
+ *
140
+ * @default `"React.createElement"`
141
+ * @optional
142
+ * @inherited
143
+ */
144
+ jsx_factory?: string;
145
+
146
+ /**
147
+ * The function to use to replace jsx fragment syntax.
148
+ *
149
+ * @default `"React.Fragment"`
150
+ * @optional
151
+ * @inherited
152
+ */
153
+ jsx_fragment?: string;
154
+
155
+ /**
156
+ * A map of environment variables to set when deploying your worker.
157
+ * Of note, they can only be strings. Which is unfortunate, really.
158
+ * (TODO: verify that they can only be strings?)
159
+ * NB: these are not inherited, and HAVE to be duplicated across all environments.
160
+ *
161
+ * @default `{}`
162
+ * @optional
163
+ * @inherited false
164
+ */
165
+ vars?: { [key: string]: string };
166
+
167
+ /**
168
+ * A list of durable objects that your worker should be bound to.
169
+ * For more information about Durable Objects, see the documentation at
170
+ * https://developers.cloudflare.com/workers/learning/using-durable-objects
171
+ * NB: these are not inherited, and HAVE to be duplicated across all environments.
172
+ *
173
+ * @default `{ bindings: [] }`
174
+ * @optional
175
+ * @inherited false
176
+ */
177
+ durable_objects?: {
178
+ bindings: {
179
+ /** The name of the binding used to refer to the Durable Object */
180
+ name: string;
181
+ /** The exported class name of the Durable Object */
182
+ class_name: string;
183
+ /** The script where the Durable Object is defined (if it's external to this worker) */
184
+ script_name?: string;
185
+ }[];
186
+ };
187
+
188
+ /**
189
+ * These specify any Workers KV Namespaces you want to
190
+ * access from inside your Worker. To learn more about KV Namespaces,
191
+ * see the documentation at https://developers.cloudflare.com/workers/learning/how-kv-works
192
+ * NB: these are not inherited, and HAVE to be duplicated across all environments.
193
+ *
194
+ * @default `[]`
195
+ * @optional
196
+ * @inherited false
197
+ */
198
+ kv_namespaces?: {
199
+ /** The binding name used to refer to the KV Namespace */
200
+ binding: string;
201
+ /** The ID of the KV namespace */
202
+ id: string;
203
+ /** The ID of the KV namespace used during `wrangler dev` */
204
+ preview_id?: string;
205
+ }[];
206
+
207
+ /**
208
+ * A list of services that your worker should be bound to.
209
+ * NB: these are not inherited, and HAVE to be duplicated across all environments.
210
+ *
211
+ * @default `[]`
212
+ * @optional
213
+ * @inherited false
214
+ */
215
+ experimental_services?: {
216
+ /** The binding name used to refer to the Service */
217
+ name: string;
218
+ /** The name of the Service being bound */
219
+ service: string;
220
+ /** The Service's environment */
221
+ environment: string;
222
+ }[];
223
+
224
+ /**
225
+ * A list of wasm modules that your worker should be bound to. This is
226
+ * the "legacy" way of binding to a wasm module. ES Module workers should
227
+ * do proper module imports.
228
+ * NB: these ARE NOT inherited, and SHOULD NOT be duplicated across all environments.
229
+ */
230
+ wasm_modules?: {
231
+ [key: string]: string;
232
+ };
233
+
234
+ /**
235
+ * A list of migrations that should be uploaded with your Worker.
236
+ * These define changes in your Durable Object declarations.
237
+ * More details at https://developers.cloudflare.com/workers/learning/using-durable-objects#configuring-durable-object-classes-with-migrations
238
+ * NB: these ARE NOT inherited, and SHOULD NOT be duplicated across all environments.
239
+ *
240
+ * @default `[]`
241
+ * @optional
242
+ * @inherited true
243
+ */
244
+ migrations?: {
245
+ /** A unique identifier for this migration. */
246
+ tag: string;
247
+ /** The new Durable Objects being defined. */
248
+ new_classes?: string[];
249
+ /** The Durable Objects being renamed. */
250
+ renamed_classes?: {
251
+ from: string;
252
+ to: string;
253
+ }[];
254
+ /** The Durable Objects being removed. */
255
+ deleted_classes?: string[];
256
+ }[];
257
+
258
+ /**
259
+ * The definition of a Worker Site, a feature that lets you upload
260
+ * static assets with your Worker.
261
+ * More details at https://developers.cloudflare.com/workers/platform/sites
262
+ * NB: This IS NOT inherited, and SHOULD NOT be duplicated across all environments.
263
+ *
264
+ * @default `undefined`
265
+ * @optional
266
+ * @inherited true
267
+ */
268
+ site?: {
269
+ /**
270
+ * The directory containing your static assets. It must be
271
+ * a path relative to your wrangler.toml file.
272
+ * Example: bucket = "./public"
273
+ *
274
+ * optional false
275
+ */
276
+ bucket: string;
277
+
278
+ /**
279
+ * The location of your Worker script.
280
+ *
281
+ * @deprecated DO NOT use this (it's a holdover from wrangler 1.x). Either use the top level `entry` field, or pass the path to your entry file as a command line argument.
282
+ * @breaking
283
+ */
284
+ "entry-point"?: string;
285
+
286
+ /**
287
+ * An exclusive list of .gitignore-style patterns that match file
288
+ * or directory names from your bucket location. Only matched
289
+ * items will be uploaded. Example: include = ["upload_dir"]
290
+ *
291
+ * @optional
292
+ * @default `[]`
293
+ */
294
+ include?: string[];
295
+
296
+ /**
297
+ * A list of .gitignore-style patterns that match files or
298
+ * directories in your bucket that should be excluded from
299
+ * uploads. Example: exclude = ["ignore_dir"]
300
+ *
301
+ * @optional
302
+ * @default `[]`
303
+ */
304
+ exclude?: string[];
305
+ };
306
+
307
+ /**
308
+ * "Cron" definitions to trigger a worker's "scheduled" function.
309
+ * Lets you call workers periodically, much like a cron job.
310
+ * More details here https://developers.cloudflare.com/workers/platform/cron-triggers
311
+ *
312
+ * @inherited
313
+ * @default `{ crons: [] }`
314
+ * @optional
315
+ */
316
+ triggers?: { crons: string[] };
317
+
318
+ /**
319
+ * Options to configure the development server that your worker will use.
320
+ * NB: This is NOT inherited, and SHOULD NOT be duplicated across all environments.
321
+ *
322
+ * @default `{}`
323
+ * @optional
324
+ * @inherited false
325
+ */
326
+ dev?: {
327
+ /**
328
+ * IP address for the local dev server to listen on,
329
+ *
330
+ * @default `127.0.0.1`
331
+ * @todo this needs to be implemented
332
+ */
333
+ ip?: string;
334
+
335
+ /**
336
+ * Port for the local dev server to listen on
337
+ *
338
+ * @default `8787`
339
+ */
340
+ port?: number;
341
+
342
+ /**
343
+ * Protocol that local wrangler dev server listens to requests on.
344
+ *
345
+ * @default `http`
346
+ * @todo this needs to be implemented
347
+ */
348
+ local_protocol?: string;
349
+
350
+ /**
351
+ * Protocol that wrangler dev forwards requests on
352
+ *
353
+ * @default `https`
354
+ * @todo this needs to be implemented
355
+ */
356
+ upstream_protocol?: string;
357
+ };
37
358
 
38
- type DurableObject = {
39
- name: string;
40
- class_name: string;
41
- script_name?: string;
359
+ /**
360
+ * Specifies the Usage Model for your Worker. There are two options -
361
+ * [bundled](https://developers.cloudflare.com/workers/platform/limits#bundled-usage-model) and
362
+ * [unbound](https://developers.cloudflare.com/workers/platform/limits#unbound-usage-model).
363
+ * For newly created Workers, if the Usage Model is omitted
364
+ * it will be set to the [default Usage Model set on the account](https://dash.cloudflare.com/?account=workers/default-usage-model).
365
+ * For existing Workers, if the Usage Model is omitted, it will be
366
+ * set to the Usage Model configured in the dashboard for that Worker.
367
+ */
368
+ usage_model?: undefined | "bundled" | "unbound";
369
+
370
+ /**
371
+ * Configures a custom build step to be run by Wrangler when
372
+ * building your Worker. Refer to the [custom builds documentation](https://developers.cloudflare.com/workers/cli-wrangler/configuration#build)
373
+ * for more details.
374
+ *
375
+ * @default `undefined`
376
+ * @optional
377
+ * @inherited false
378
+ */
379
+ build?: {
380
+ /** The command used to build your Worker. On Linux and macOS, the command is executed in the `sh` shell and the `cmd` shell for Windows. The `&&` and `||` shell operators may be used. */
381
+ command?: string;
382
+ /** The directory in which the command is executed. */
383
+ cwd?: string;
384
+ /** The directory to watch for changes while using wrangler dev, defaults to the current working directory */
385
+ watch_dir?: string;
386
+ } & /**
387
+ * Much of the rest of this configuration isn't necessary anymore
388
+ * in wrangler2. We infer the format automatically, and we can pass
389
+ * the path to the script either in the CLI (or, @todo, as the top level
390
+ * `entry` property).
391
+ */ (
392
+ | {
393
+ upload?: {
394
+ /**
395
+ * The format of the Worker script, must be "service-worker".
396
+ *
397
+ * @deprecated We infer the format automatically now.
398
+ */
399
+ format?: "service-worker";
400
+
401
+ /**
402
+ * The path to the Worker script. This should be replaced
403
+ * by the top level `entry' property.
404
+ *
405
+ * @deprecated This will be replaced by the top level `entry' property.
406
+ */
407
+ main: string;
408
+ };
409
+ }
410
+ | {
411
+ /**
412
+ * When we use the module format, we only really
413
+ * need to specify the entry point. The format is deduced
414
+ * automatically in wrangler2.
415
+ */
416
+ upload?: {
417
+ /**
418
+ * The format of the Worker script, must be "modules".
419
+ *
420
+ * @deprecated We infer the format automatically now.
421
+ */
422
+ format?: "modules";
423
+
424
+ /**
425
+ * The directory you wish to upload your modules from,
426
+ * relative to the wrangler.toml file.
427
+ *
428
+ * Defaults to the directory containing the wrangler.toml file.
429
+ *
430
+ * @deprecated
431
+ * @breaking
432
+ */
433
+ dir?: string;
434
+
435
+ /**
436
+ * The path to the Worker script, relative to `upload.dir`.
437
+ *
438
+ * @deprecated This will be replaced by a command line argument.
439
+ */
440
+ main?: string;
441
+
442
+ /**
443
+ * An ordered list of rules that define which modules to import,
444
+ * and what type to import them as. You will need to specify rules
445
+ * to use Text, Data, and CompiledWasm modules, or when you wish to
446
+ * have a .js file be treated as an ESModule instead of CommonJS.
447
+ *
448
+ * @deprecated These are now inferred automatically for major file types, but you can still specify them manually.
449
+ * @todo this needs to be implemented!
450
+ * @breaking
451
+ */
452
+ rules?: {
453
+ type: "ESModule" | "CommonJS" | "Text" | "Data" | "CompiledWasm";
454
+ globs: string[];
455
+ fallthrough?: boolean;
456
+ };
457
+ };
458
+ }
459
+ );
460
+
461
+ /**
462
+ * The `env` section defines overrides for the configuration for
463
+ * different environments. Most fields can be overridden, while
464
+ * some have to be specifically duplicated in every environment.
465
+ * For more information, see the documentation at https://developers.cloudflare.com/workers/cli-wrangler/configuration#environments
466
+ */
467
+ env?: {
468
+ [envName: string]:
469
+ | undefined
470
+ | Omit<Config, "env" | "wasm_modules" | "migrations" | "site" | "dev">;
471
+ };
42
472
  };
43
473
 
44
- type Build = {
45
- command?: string;
46
- cwd?: string;
47
- watch_dir?: string;
48
- } & (
49
- | {
50
- upload?: {
51
- format: "service-worker";
52
- main: string;
53
- };
474
+ type ValidationResults = (
475
+ | { key: string; info: string }
476
+ | { key: string; error: string }
477
+ | { key: string; warning: string }
478
+ )[];
479
+
480
+ /**
481
+ * We also define a validation function that manually validates
482
+ * every field in the configuration as per the type definitions,
483
+ * as well as extra constraints we apply to some fields, as well
484
+ * as some constraints on combinations of fields. This is useful for
485
+ * presenting errors and messages to the user. Eventually, we will
486
+ * combine this with some automatic config rewriting tools.
487
+ *
488
+ */
489
+ export async function validateConfig(
490
+ _config: Partial<Config>
491
+ ): Promise<ValidationResults> {
492
+ const results: ValidationResults = [];
493
+
494
+ return results;
495
+ }
496
+
497
+ /**
498
+ * Process the environments (`env`) specified in the `config`.
499
+ *
500
+ * The environments configuration is complicated since each environment is a customized version of the main config.
501
+ * Some of the configuration can be inherited from the main config, while other configuration must replace what is in the main config.
502
+ *
503
+ * This function ensures that each environment is set up correctly with inherited configuration, as necessary.
504
+ * It will log a warning if an environment is missing required configuration.
505
+ */
506
+ export function normaliseAndValidateEnvironmentsConfig(config: Config) {
507
+ if (config.env == undefined) {
508
+ // There are no environments specified so there is nothing to do here.
509
+ return;
510
+ }
511
+
512
+ const environments = config.env;
513
+
514
+ for (const envKey of Object.keys(environments)) {
515
+ const environment = environments[envKey];
516
+
517
+ // Given how TOML works, there should never be an environment containing nothing.
518
+ // I.e. if there is a section in a TOML file, then the parser will create an object for it.
519
+ // But it may be possible in the future if we change how the configuration is stored.
520
+ assert(
521
+ environment,
522
+ `Environment ${envKey} is specified in the config but not defined.`
523
+ );
524
+
525
+ // Fall back on "inherited fields" from the config, if not specified in the environment.
526
+ const inheritedFields = [
527
+ "name",
528
+ "account_id",
529
+ "workers_dev",
530
+ "compatibility_date",
531
+ "compatibility_flags",
532
+ "zone_id",
533
+ "routes",
534
+ "route",
535
+ "jsx_factory",
536
+ "jsx_fragment",
537
+ "site",
538
+ "triggers",
539
+ "usage_model",
540
+ ];
541
+
542
+ for (const inheritedField of inheritedFields) {
543
+ if (config[inheritedField] !== undefined) {
544
+ if (environment[inheritedField] === undefined) {
545
+ environment[inheritedField] = config[inheritedField]; // TODO: - shallow or deep copy?
546
+ }
547
+ }
54
548
  }
55
- | {
56
- upload?: {
57
- format: "modules";
58
- dir?: string;
59
- main?: string;
60
- rules?: {
61
- type: "ESModule" | "CommonJS" | "Text" | "Data" | "CompiledWasm";
62
- globs: string[]; // can we use typescript for these patterns?
63
- fallthrough?: boolean;
64
- };
65
- };
549
+
550
+ // Warn if there is a "required" field in the top level config that has not been specified specified in the environment.
551
+ // These required fields are `vars`, `durable_objects`, `kv_namespaces` and `experimental_services`.
552
+ // Each of them has different characteristics that need to be checked.
553
+
554
+ // `vars` is just an object
555
+ if (config.vars !== undefined) {
556
+ if (environment.vars === undefined) {
557
+ console.warn(
558
+ `In your configuration, "vars" exists at the top level, but not on "env.${envKey}".\n` +
559
+ `This is not what you probably want, since "vars" is not inherited by environments.\n` +
560
+ `Please add "vars" to "env.${envKey}".`
561
+ );
562
+ } else {
563
+ for (const varField of Object.keys(config.vars)) {
564
+ if (!(varField in environment.vars)) {
565
+ console.warn(
566
+ `In your configuration, "vars.${varField}" exists at the top level, but not on "env.${envKey}".\n` +
567
+ `This is not what you probably want, since "vars" is not inherited by environments.\n` +
568
+ `Please add "vars.${varField}" to "env.${envKey}".`
569
+ );
570
+ }
571
+ }
572
+ }
66
573
  }
67
- );
68
-
69
- type UsageModel = "bundled" | "unbound";
70
-
71
- type Env = {
72
- name?: string; // inherited
73
- account_id?: string; // inherited
74
- workers_dev?: boolean; // inherited
75
- compatibility_date?: string; // inherited
76
- compatibility_flags?: string[]; // inherited
77
- zone_id?: string; // inherited
78
- routes?: string[]; // inherited
79
- route?: string; // inherited
80
- webpack_config?: string; // inherited
81
- site?: Site;
82
- jsx_factory?: string; // inherited
83
- jsx_fragment?: string; // inherited
84
- polyfill_node?: boolean; //inherited
85
- // we should use typescript to parse cron patterns
86
- triggers?: { crons: Cron[] }; // inherited
87
- vars?: Vars;
88
- durable_objects?: { bindings: DurableObject[] };
89
- kv_namespaces?: KVNamespace[];
90
- usage_model?: UsageModel; // inherited
91
- };
92
574
 
93
- export type Config = {
94
- name?: string; // inherited
95
- account_id?: string; // inherited
96
- // @deprecated Don't use this
97
- type?: Project; // top level
98
- compatibility_date?: string; // inherited
99
- compatibility_flags?: string[]; // inherited
100
- // -- there's some mutually exclusive logic for this next block,
101
- // but I didn't bother for now
102
- workers_dev?: boolean; // inherited
103
- zone_id?: string; // inherited
104
- routes?: string[]; // inherited
105
- route?: string; // inherited
106
- // -- end mutually exclusive stuff
107
- // @deprecated Don't use this
108
- webpack_config?: string; // inherited
109
- jsx_factory?: string; // inherited
110
- jsx_fragment?: string; // inherited
111
- polyfill_node?: boolean; //inherited
112
- vars?: Vars;
113
- migrations?: DOMigration[];
114
- durable_objects?: { bindings: DurableObject[] };
115
- kv_namespaces?: KVNamespace[];
116
- site?: Site; // inherited
117
- // we should use typescript to parse cron patterns
118
- triggers?: { crons: Cron[] }; // inherited
119
- dev?: Dev;
120
- usage_model?: UsageModel; // inherited
121
- // top level
122
- build?: Build;
123
- env?: { [envName: string]: void | Env };
124
- };
575
+ // `durable_objects` is an object containing a `bindings` array
576
+ if (config.durable_objects !== undefined) {
577
+ if (environment.durable_objects === undefined) {
578
+ console.warn(
579
+ `In your configuration, "durable_objects.bindings" exists at the top level, but not on "env.${envKey}".\n` +
580
+ `This is not what you probably want, since "durable_objects" is not inherited by environments.\n` +
581
+ `Please add "durable_objects.bindings" to "env.${envKey}".`
582
+ );
583
+ } else {
584
+ const envBindingNames = new Set(
585
+ environment.durable_objects.bindings.map((b) => b.name)
586
+ );
587
+ for (const bindingName of config.durable_objects.bindings.map(
588
+ (b) => b.name
589
+ )) {
590
+ if (!envBindingNames.has(bindingName)) {
591
+ console.warn(
592
+ `In your configuration, there is a durable_objects binding with name "${bindingName}" at the top level, but not on "env.${envKey}".\n` +
593
+ `This is not what you probably want, since "durable_objects" is not inherited by environments.\n` +
594
+ `Please add a binding for "${bindingName}" to "env.${envKey}.durable_objects.bindings".`
595
+ );
596
+ }
597
+ }
598
+ }
599
+ }
600
+
601
+ // `kv_namespaces` contains an array of namespace bindings
602
+ if (config.kv_namespaces !== undefined) {
603
+ if (environment.kv_namespaces === undefined) {
604
+ console.warn(
605
+ `In your configuration, "kv_namespaces" exists at the top level, but not on "env.${envKey}".\n` +
606
+ `This is not what you probably want, since "kv_namespaces" is not inherited by environments.\n` +
607
+ `Please add "kv_namespaces" to "env.${envKey}".`
608
+ );
609
+ } else {
610
+ const envBindings = new Set(
611
+ environment.kv_namespaces.map((kvNamespace) => kvNamespace.binding)
612
+ );
613
+ for (const bindingName of config.kv_namespaces.map(
614
+ (kvNamespace) => kvNamespace.binding
615
+ )) {
616
+ if (!envBindings.has(bindingName)) {
617
+ console.warn(
618
+ `In your configuration, there is a kv_namespaces with binding "${bindingName}" at the top level, but not on "env.${envKey}".\n` +
619
+ `This is not what you probably want, since "kv_namespaces" is not inherited by environments.\n` +
620
+ `Please add a binding for "${bindingName}" to "env.${envKey}.kv_namespaces".`
621
+ );
622
+ }
623
+ }
624
+ }
625
+ }
626
+
627
+ // `experimental_services` contains an array of namespace bindings
628
+ if (config.experimental_services !== undefined) {
629
+ if (environment.experimental_services === undefined) {
630
+ console.warn(
631
+ `In your configuration, "experimental_services" exists at the top level, but not on "env.${envKey}".\n` +
632
+ `This is not what you probably want, since "experimental_services" is not inherited by environments.\n` +
633
+ `Please add "experimental_services" to "env.${envKey}".`
634
+ );
635
+ } else {
636
+ const envBindingNames = new Set(
637
+ environment.experimental_services.map((service) => service.name)
638
+ );
639
+ for (const bindingName of config.experimental_services.map(
640
+ (service) => service.name
641
+ )) {
642
+ if (!envBindingNames.has(bindingName)) {
643
+ console.warn(
644
+ `In your configuration, there is a experimental_services with binding name "${bindingName}" at the top level, but not on "env.${envKey}".\n` +
645
+ `This is not what you probably want, since "experimental_services" is not inherited by environments.\n` +
646
+ `Please add a service for "${bindingName}" to "env.${envKey}.experimental_services".`
647
+ );
648
+ }
649
+ }
650
+ }
651
+ }
652
+ }
653
+ }