wrangler 0.0.0-e9ab55a → 0.0.0-e9c2b04a

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