Skip to content

Commit ac08e68

Browse files
fix incorrect bindings remote deduplication logic (#9854)
1 parent c79a527 commit ac08e68

File tree

16 files changed

+259
-29
lines changed

16 files changed

+259
-29
lines changed

.changeset/cruel-owls-watch.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"miniflare": patch
3+
---
4+
5+
fix incorrect bindings remote deduplication logic
6+
7+
when bindings are registered deduplication logic is applied to make sure that the same binding is not unnecessarily registered multiple times, the changes here fix the fact that such deduplication logic doesn't currently take into account whether bindings are used or not in remote mode (which is problematic when the same binding is used both in remote and local mode)

packages/miniflare/src/plugins/ai/index.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import assert from "node:assert";
22
import { z } from "zod";
33
import {
4+
getUserBindingServiceName,
45
Plugin,
56
ProxyNodeBinding,
67
remoteProxyClientWorker,
@@ -38,7 +39,13 @@ export const AI_PLUGIN: Plugin<typeof AIOptionsSchema> = {
3839
innerBindings: [
3940
{
4041
name: "fetcher",
41-
service: { name: `${AI_PLUGIN_NAME}:${options.ai.binding}` },
42+
service: {
43+
name: getUserBindingServiceName(
44+
AI_PLUGIN_NAME,
45+
options.ai.binding,
46+
options.ai.remoteProxyConnectionString
47+
),
48+
},
4249
},
4350
],
4451
},
@@ -60,7 +67,11 @@ export const AI_PLUGIN: Plugin<typeof AIOptionsSchema> = {
6067

6168
return [
6269
{
63-
name: `${AI_PLUGIN_NAME}:${options.ai.binding}`,
70+
name: getUserBindingServiceName(
71+
AI_PLUGIN_NAME,
72+
options.ai.binding,
73+
options.ai.remoteProxyConnectionString
74+
),
6475
worker: remoteProxyClientWorker(
6576
options.ai.remoteProxyConnectionString,
6677
options.ai.binding

packages/miniflare/src/plugins/browser-rendering/index.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import BROWSER_RENDERING_WORKER from "worker:browser-rendering/binding";
22
import { z } from "zod";
33
import { kVoid } from "../../runtime";
44
import {
5+
getUserBindingServiceName,
56
Plugin,
67
ProxyNodeBinding,
78
remoteProxyClientWorker,
@@ -35,7 +36,11 @@ export const BROWSER_RENDERING_PLUGIN: Plugin<
3536
{
3637
name: options.browserRendering.binding,
3738
service: {
38-
name: `${BROWSER_RENDERING_PLUGIN_NAME}:${options.browserRendering.binding}`,
39+
name: getUserBindingServiceName(
40+
BROWSER_RENDERING_PLUGIN_NAME,
41+
options.browserRendering.binding,
42+
options.browserRendering.remoteProxyConnectionString
43+
),
3944
},
4045
},
4146
];
@@ -55,7 +60,11 @@ export const BROWSER_RENDERING_PLUGIN: Plugin<
5560

5661
return [
5762
{
58-
name: `${BROWSER_RENDERING_PLUGIN_NAME}:${options.browserRendering.binding}`,
63+
name: getUserBindingServiceName(
64+
BROWSER_RENDERING_PLUGIN_NAME,
65+
options.browserRendering.binding,
66+
options.browserRendering.remoteProxyConnectionString
67+
),
5968
worker: options.browserRendering.remoteProxyConnectionString
6069
? remoteProxyClientWorker(
6170
options.browserRendering.remoteProxyConnectionString,

packages/miniflare/src/plugins/d1/index.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { SharedBindings } from "../../workers";
1111
import {
1212
getMiniflareObjectBindings,
1313
getPersistPath,
14+
getUserBindingServiceName,
1415
migrateDatabase,
1516
namespaceEntries,
1617
namespaceKeys,
@@ -67,10 +68,18 @@ export const D1_PLUGIN: Plugin<
6768
"Alpha D1 Databases cannot run remotely"
6869
);
6970

71+
const serviceName = getUserBindingServiceName(
72+
D1_DATABASE_SERVICE_PREFIX,
73+
id,
74+
remoteProxyConnectionString
75+
);
76+
7077
const binding = name.startsWith("__D1_BETA__")
7178
? // Used before Wrangler 3.3
7279
{
73-
service: { name: `${D1_DATABASE_SERVICE_PREFIX}:${id}` },
80+
service: {
81+
name: serviceName,
82+
},
7483
}
7584
: // Used after Wrangler 3.3
7685
{
@@ -79,7 +88,9 @@ export const D1_PLUGIN: Plugin<
7988
innerBindings: [
8089
{
8190
name: "fetcher",
82-
service: { name: `${D1_DATABASE_SERVICE_PREFIX}:${id}` },
91+
service: {
92+
name: serviceName,
93+
},
8394
},
8495
],
8596
},
@@ -107,7 +118,11 @@ export const D1_PLUGIN: Plugin<
107118
const databases = namespaceEntries(options.d1Databases);
108119
const services = databases.map<Service>(
109120
([name, { id, remoteProxyConnectionString }]) => ({
110-
name: `${D1_DATABASE_SERVICE_PREFIX}:${id}`,
121+
name: getUserBindingServiceName(
122+
D1_DATABASE_SERVICE_PREFIX,
123+
id,
124+
remoteProxyConnectionString
125+
),
111126
worker: remoteProxyConnectionString
112127
? remoteProxyClientWorker(remoteProxyConnectionString, name)
113128
: objectEntryWorker(D1_DATABASE_OBJECT, id),

packages/miniflare/src/plugins/dispatch-namespace/index.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import LOCAL_DISPATCH_NAMESPACE from "worker:dispatch-namespace/dispatch-namespa
33
import { z } from "zod";
44
import { Worker_Binding } from "../../runtime";
55
import {
6+
getUserBindingServiceName,
67
Plugin,
78
ProxyNodeBinding,
89
remoteProxyClientWorker,
@@ -42,7 +43,11 @@ export const DISPATCH_NAMESPACE_PLUGIN: Plugin<
4243
{
4344
name: "fetcher",
4445
service: {
45-
name: `${DISPATCH_NAMESPACE_PLUGIN_NAME}:ns:${config.namespace}`,
46+
name: getUserBindingServiceName(
47+
DISPATCH_NAMESPACE_PLUGIN_NAME,
48+
config.namespace,
49+
config.remoteProxyConnectionString
50+
),
4651
},
4752
},
4853
],
@@ -73,7 +78,11 @@ export const DISPATCH_NAMESPACE_PLUGIN: Plugin<
7378
"Dispatch Namespace bindings only support running remotely"
7479
);
7580
return {
76-
name: `${DISPATCH_NAMESPACE_PLUGIN_NAME}:ns:${config.namespace}`,
81+
name: getUserBindingServiceName(
82+
DISPATCH_NAMESPACE_PLUGIN_NAME,
83+
config.namespace,
84+
config.remoteProxyConnectionString
85+
),
7786
worker: remoteProxyClientWorker(
7887
config.remoteProxyConnectionString,
7988
name

packages/miniflare/src/plugins/email/index.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import EMAIL_MESSAGE from "worker:email/email";
22
import SEND_EMAIL_BINDING from "worker:email/send_email";
33
import { z } from "zod";
44
import { Service, Worker_Binding } from "../../runtime";
5-
import { Plugin, WORKER_BINDING_SERVICE_LOOPBACK } from "../shared";
5+
import {
6+
getUserBindingServiceName,
7+
Plugin,
8+
WORKER_BINDING_SERVICE_LOOPBACK,
9+
} from "../shared";
610

711
// Define the mutually exclusive schema
812
const EmailBindingOptionsSchema = z
@@ -53,7 +57,7 @@ export const EMAIL_PLUGIN: Plugin<typeof EmailOptionsSchema> = {
5357
name,
5458
service: {
5559
entrypoint: "SendEmailBinding",
56-
name: `${SERVICE_SEND_EMAIL_WORKER_PREFIX}:${name}`,
60+
name: getUserBindingServiceName(SERVICE_SEND_EMAIL_WORKER_PREFIX, name),
5761
},
5862
}));
5963
},
@@ -65,7 +69,7 @@ export const EMAIL_PLUGIN: Plugin<typeof EmailOptionsSchema> = {
6569

6670
for (const { name, ...config } of args.options.email?.send_email ?? []) {
6771
services.push({
68-
name: `${SERVICE_SEND_EMAIL_WORKER_PREFIX}:${name}`,
72+
name: getUserBindingServiceName(SERVICE_SEND_EMAIL_WORKER_PREFIX, name),
6973
worker: {
7074
compatibilityDate: "2025-03-17",
7175
modules: [

packages/miniflare/src/plugins/images/index.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { z } from "zod";
22
import { CoreBindings, CoreHeaders } from "../../workers";
33
import {
4+
getUserBindingServiceName,
45
Plugin,
56
ProxyNodeBinding,
67
remoteProxyClientWorker,
@@ -48,7 +49,11 @@ export const IMAGES_PLUGIN: Plugin<typeof ImagesOptionsSchema> = {
4849
{
4950
name: "fetcher",
5051
service: {
51-
name: `${IMAGES_PLUGIN_NAME}:${options.images.binding}`,
52+
name: getUserBindingServiceName(
53+
IMAGES_PLUGIN_NAME,
54+
options.images.binding,
55+
options.images.remoteProxyConnectionString
56+
),
5257
},
5358
},
5459
],
@@ -71,7 +76,11 @@ export const IMAGES_PLUGIN: Plugin<typeof ImagesOptionsSchema> = {
7176

7277
return [
7378
{
74-
name: `${IMAGES_PLUGIN_NAME}:${options.images.binding}`,
79+
name: getUserBindingServiceName(
80+
IMAGES_PLUGIN_NAME,
81+
options.images.binding,
82+
options.images.remoteProxyConnectionString
83+
),
7584
worker: options.images.remoteProxyConnectionString
7685
? remoteProxyClientWorker(
7786
options.images.remoteProxyConnectionString,

packages/miniflare/src/plugins/kv/index.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { SharedBindings } from "../../workers";
1111
import {
1212
getMiniflareObjectBindings,
1313
getPersistPath,
14+
getUserBindingServiceName,
1415
migrateDatabase,
1516
namespaceEntries,
1617
namespaceKeys,
@@ -77,9 +78,15 @@ export const KV_PLUGIN: Plugin<
7778
sharedOptions: KVSharedOptionsSchema,
7879
async getBindings(options) {
7980
const namespaces = namespaceEntries(options.kvNamespaces);
80-
const bindings = namespaces.map<Worker_Binding>(([name, { id }]) => ({
81+
const bindings = namespaces.map<Worker_Binding>(([name, namespace]) => ({
8182
name,
82-
kvNamespace: { name: `${SERVICE_NAMESPACE_PREFIX}:${id}` },
83+
kvNamespace: {
84+
name: getUserBindingServiceName(
85+
SERVICE_NAMESPACE_PREFIX,
86+
namespace.id,
87+
namespace.remoteProxyConnectionString
88+
),
89+
},
8390
}));
8491

8592
if (isWorkersSitesEnabled(options)) {
@@ -114,7 +121,11 @@ export const KV_PLUGIN: Plugin<
114121
const namespaces = namespaceEntries(options.kvNamespaces);
115122
const services = namespaces.map<Service>(
116123
([name, { id, remoteProxyConnectionString }]) => ({
117-
name: `${SERVICE_NAMESPACE_PREFIX}:${id}`,
124+
name: getUserBindingServiceName(
125+
SERVICE_NAMESPACE_PREFIX,
126+
id,
127+
remoteProxyConnectionString
128+
),
118129
worker: remoteProxyConnectionString
119130
? remoteProxyClientWorker(remoteProxyConnectionString, name)
120131
: objectEntryWorker(KV_NAMESPACE_OBJECT, id),

packages/miniflare/src/plugins/mtls/index.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import assert from "node:assert";
22
import { z } from "zod";
33
import {
4+
getUserBindingServiceName,
45
Plugin,
56
ProxyNodeBinding,
67
remoteProxyClientWorker,
@@ -36,7 +37,11 @@ export const MTLS_PLUGIN: Plugin<typeof MtlsOptionsSchema> = {
3637
name,
3738

3839
service: {
39-
name: `${MTLS_PLUGIN_NAME}:${certificate_id}`,
40+
name: getUserBindingServiceName(
41+
MTLS_PLUGIN_NAME,
42+
certificate_id,
43+
remoteProxyConnectionString
44+
),
4045
},
4146
};
4247
}
@@ -66,7 +71,11 @@ export const MTLS_PLUGIN: Plugin<typeof MtlsOptionsSchema> = {
6671
);
6772

6873
return {
69-
name: `${MTLS_PLUGIN_NAME}:${certificate_id}`,
74+
name: getUserBindingServiceName(
75+
MTLS_PLUGIN_NAME,
76+
certificate_id,
77+
remoteProxyConnectionString
78+
),
7079
worker: remoteProxyClientWorker(remoteProxyConnectionString, name),
7180
};
7281
}

packages/miniflare/src/plugins/r2/index.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { SharedBindings } from "../../workers";
1010
import {
1111
getMiniflareObjectBindings,
1212
getPersistPath,
13+
getUserBindingServiceName,
1314
migrateDatabase,
1415
namespaceEntries,
1516
namespaceKeys,
@@ -59,9 +60,15 @@ export const R2_PLUGIN: Plugin<
5960
sharedOptions: R2SharedOptionsSchema,
6061
getBindings(options) {
6162
const buckets = namespaceEntries(options.r2Buckets);
62-
return buckets.map<Worker_Binding>(([name, { id }]) => ({
63+
return buckets.map<Worker_Binding>(([name, bucket]) => ({
6364
name,
64-
r2Bucket: { name: `${R2_BUCKET_SERVICE_PREFIX}:${id}` },
65+
r2Bucket: {
66+
name: getUserBindingServiceName(
67+
R2_BUCKET_SERVICE_PREFIX,
68+
bucket.id,
69+
bucket.remoteProxyConnectionString
70+
),
71+
},
6572
}));
6673
},
6774
getNodeBindings(options) {
@@ -82,7 +89,11 @@ export const R2_PLUGIN: Plugin<
8289
const buckets = namespaceEntries(options.r2Buckets);
8390
const services = buckets.map<Service>(
8491
([name, { id, remoteProxyConnectionString }]) => ({
85-
name: `${R2_BUCKET_SERVICE_PREFIX}:${id}`,
92+
name: getUserBindingServiceName(
93+
R2_BUCKET_SERVICE_PREFIX,
94+
id,
95+
remoteProxyConnectionString
96+
),
8697
worker: remoteProxyConnectionString
8798
? remoteProxyClientWorker(remoteProxyConnectionString, name)
8899
: objectEntryWorker(R2_BUCKET_OBJECT, id),

0 commit comments

Comments
 (0)