@@ -4,6 +4,7 @@ import path from "path";
44import {
55 Awaitable ,
66 Context ,
7+ Log ,
78 Mount ,
89 Option ,
910 OptionType ,
@@ -120,6 +121,40 @@ export class Fetcher {
120121 }
121122}
122123
124+ function getServiceBindingName (
125+ log : Log ,
126+ service : string ,
127+ { name, binding } : Partial < WranglerServiceConfig >
128+ ) {
129+ // 1. Make sure name and binding match if both defined
130+ if ( name !== undefined && binding !== undefined && name !== binding ) {
131+ throw new MiniflareCoreError (
132+ "ERR_SERVICE_NAME_MISMATCH" ,
133+ `Service "${ service } " declared with \`name\`="${ name } " and \`binding\`="${ binding } ".
134+ The \`binding\` key should be used to define binding names.`
135+ ) ;
136+ }
137+
138+ // 2. If name is defined, use it but log a warning
139+ if ( name !== undefined ) {
140+ log . warn (
141+ `Service "${ service } " declared using deprecated syntax.
142+ The \`name\` key should be removed and renamed to \`binding\`.`
143+ ) ;
144+ return name ;
145+ }
146+
147+ // 3. If binding is defined, use it
148+ if ( binding !== undefined ) return binding ;
149+
150+ // 4. Otherwise, neither `name` nor `binding` defined, so throw
151+ throw new MiniflareCoreError (
152+ "ERR_SERVICE_NO_NAME" ,
153+ `Service "${ service } " declared with neither \`binding\` nor \`name\` keys.
154+ The \`binding\` key should be used to define binding names.`
155+ ) ;
156+ }
157+
123158export class BindingsPlugin
124159 extends Plugin < BindingsOptions >
125160 implements BindingsOptions
@@ -236,42 +271,16 @@ export class BindingsPlugin
236271 fromWrangler : ( { services, experimental_services } , configDir , log ) => {
237272 if ( experimental_services ) {
238273 log . warn (
239- "Using `experimental_services` in `wrangler.toml` is deprecated. " +
274+ "Using the `experimental_services` key is deprecated. " +
240275 "This key should be renamed to `services`."
241276 ) ;
242277 }
243278 const allServices : WranglerServiceConfig [ ] = [ ] ;
244279 if ( services ) allServices . push ( ...services ) ;
245280 if ( experimental_services ) allServices . push ( ...experimental_services ) ;
246- const getBindingName = (
247- service : string ,
248- { name, binding } : Partial < WranglerServiceConfig >
249- ) : string => {
250- if ( name && binding && name !== binding ) {
251- throw new MiniflareCoreError (
252- "ERR_SERVICE_NAME_MISMATCH" ,
253- `Service "${ service } " declared with name=${ name } and binding=${ binding } .
254- \`binding\` key should be used to define binding names.`
255- ) ;
256- } else if ( binding === undefined ) {
257- if ( name ) {
258- log . warn (
259- `Service "${ service } " is declared using deprecated syntax. Key \`name\` should be renamed to \`binding\`.`
260- ) ;
261- return name ;
262- }
263- throw new MiniflareCoreError (
264- "ERR_SERVICE_NO_NAME" ,
265- `Service "${ service } " declared with neither \`bindding\` nor \`name\`.
266- \`binding\` key should be used to define binding names.`
267- ) ;
268- }
269-
270- return binding ;
271- } ;
272281 return allServices ?. reduce (
273282 ( services , { name, binding, service, environment } ) => {
274- services [ getBindingName ( service , { name, binding } ) ] = {
283+ services [ getServiceBindingName ( log , service , { name, binding } ) ] = {
275284 service,
276285 environment,
277286 } ;
0 commit comments