Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit cc246f6

Browse files
committed
Tidy up service binding name/binding warnings, ref #302
1 parent 0c53ad2 commit cc246f6

File tree

2 files changed

+47
-31
lines changed

2 files changed

+47
-31
lines changed

packages/core/src/plugins/bindings.ts

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import path from "path";
44
import {
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+
123158
export 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
};

packages/core/test/plugins/bindings.spec.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,11 @@ test("BindingsPlugin: logs warning if `name` is used instead of `binding`", asyn
191191
t.true(warning.includes(service));
192192
t.regex(
193193
warning,
194-
/Service "\w+" is declared using deprecated syntax. Key `name` should be renamed to `binding`./
194+
/^Service "\w+" declared using deprecated syntax\.\nThe `name` key should be removed and renamed to `binding`\.$/
195195
);
196196
});
197197

198-
test("BindingsPlugin: logs no warning if `name` and `binding` are both used but they are the same", async (t) => {
198+
test("BindingsPlugin: logs warning if `name` and `binding` are both used but they are the same", async (t) => {
199199
const log = new TestLog();
200200
const service = "service123";
201201
const name = "SERVICE123";
@@ -212,7 +212,14 @@ test("BindingsPlugin: logs no warning if `name` and `binding` are both used but
212212

213213
// Check no warnings logged
214214
const warnings = log.logsAtLevel(LogLevel.WARN);
215-
t.is(warnings.length, 0);
215+
t.is(warnings.length, 1);
216+
const [warning] = warnings;
217+
218+
t.true(warning.includes(service));
219+
t.regex(
220+
warning,
221+
/^Service "\w+" declared using deprecated syntax\.\nThe `name` key should be removed and renamed to `binding`\.$/
222+
);
216223
});
217224

218225
test("BindingsPlugin: throws if `name` and `binding` are both present and don't match", (t) => {

0 commit comments

Comments
 (0)