Skip to content

Commit 0c0374c

Browse files
authored
fix: allow assets in multiworker setup (#7290)
* multidev assets * assert rpc and named entrypoints do not work * fixups * fixup bindings earlier * aaaaargh * don't touch sockets
1 parent 7c05b1b commit 0c0374c

File tree

5 files changed

+391
-19
lines changed

5 files changed

+391
-19
lines changed

.changeset/poor-otters-drop.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
"miniflare": patch
3+
"wrangler": patch
4+
---
5+
6+
fix: add support for workers with assets when running multiple workers in one `wrangler dev` instance
7+
8+
https://github.com/cloudflare/workers-sdk/pull/7251 added support for running multiple Workers in one `wrangler dev`/miniflare session. e.g. `wrangler dev -c wrangler.toml -c ../worker2/wrangler.toml`, which among other things, allowed cross-service RPC to Durable Objects.
9+
10+
However this did not work in the same way as production when there was a Worker with assets - this PR should fix that.

packages/miniflare/src/index.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,14 +1130,6 @@ export class Miniflare {
11301130
innerBindings: Worker_Binding[];
11311131
}[] = [];
11321132

1133-
if (this.#workerOpts[0].assets.assets) {
1134-
// This will be the UserWorker, or the vitest pool worker wrapping the UserWorker
1135-
// The asset plugin needs this so that it can set the binding between the RouterWorker and the UserWorker
1136-
// TODO: apply this to ever this.#workerOpts, not just the first (i.e this.#workerOpts[0])
1137-
this.#workerOpts[0].assets.assets.workerName =
1138-
this.#workerOpts[0].core.name;
1139-
}
1140-
11411133
for (let i = 0; i < allWorkerOpts.length; i++) {
11421134
const previousWorkerOpts = allPreviousWorkerOpts?.[i];
11431135
const workerOpts = allWorkerOpts[i];
@@ -1152,6 +1144,12 @@ export class Miniflare {
11521144
}
11531145
}
11541146

1147+
if (workerOpts.assets.assets) {
1148+
// This will be the UserWorker, or the vitest pool worker wrapping the UserWorker
1149+
// The asset plugin needs this so that it can set the binding between the RouterWorker and the UserWorker
1150+
workerOpts.assets.assets.workerName = workerOpts.core.name;
1151+
}
1152+
11551153
// Collect all bindings from this worker
11561154
const workerBindings: Worker_Binding[] = [];
11571155
allWorkerBindings.set(workerName, workerBindings);
@@ -1202,6 +1200,20 @@ export class Miniflare {
12021200
});
12031201
}
12041202
}
1203+
if ("service" in binding) {
1204+
const targetWorkerName = binding.service?.name?.replace(
1205+
"core:user:",
1206+
""
1207+
);
1208+
const maybeAssetTargetService = allWorkerOpts.find(
1209+
(worker) =>
1210+
worker.core.name === targetWorkerName && worker.assets.assets
1211+
);
1212+
if (maybeAssetTargetService) {
1213+
assert(binding.service?.name);
1214+
binding.service.name = `${ROUTER_SERVICE_NAME}:${targetWorkerName}`;
1215+
}
1216+
}
12051217
}
12061218
}
12071219
}
@@ -1305,7 +1317,7 @@ export class Miniflare {
13051317
!this.#workerOpts[0].core.name?.startsWith(
13061318
"vitest-pool-workers-runner-"
13071319
)
1308-
? ROUTER_SERVICE_NAME
1320+
? `${ROUTER_SERVICE_NAME}:${this.#workerOpts[0].core.name}`
13091321
: getUserServiceName(this.#workerOpts[0].core.name),
13101322
loopbackPort,
13111323
log: this.#log,
@@ -1340,7 +1352,6 @@ export class Miniflare {
13401352
"Ensure wrapped bindings don't have bindings to themselves."
13411353
);
13421354
}
1343-
13441355
return { services: servicesArray, sockets, extensions };
13451356
}
13461357

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ export const ASSETS_PLUGIN: Plugin<typeof AssetsOptionsSchema> = {
3939
{
4040
// binding between User Worker and Asset Worker
4141
name: options.assets.binding,
42-
service: { name: ASSETS_SERVICE_NAME },
42+
service: {
43+
name: `${ASSETS_SERVICE_NAME}:${options.assets.workerName}`,
44+
},
4345
},
4446
];
4547
},
@@ -68,8 +70,10 @@ export const ASSETS_PLUGIN: Plugin<typeof AssetsOptionsSchema> = {
6870
options.assets.directory
6971
);
7072

73+
const id = options.assets.workerName;
74+
7175
const namespaceService: Service = {
72-
name: ASSETS_KV_SERVICE_NAME,
76+
name: `${ASSETS_KV_SERVICE_NAME}:${id}`,
7377
worker: {
7478
compatibilityDate: "2023-07-24",
7579
compatibilityFlags: ["nodejs_compat"],
@@ -93,7 +97,7 @@ export const ASSETS_PLUGIN: Plugin<typeof AssetsOptionsSchema> = {
9397
};
9498

9599
const assetService: Service = {
96-
name: ASSETS_SERVICE_NAME,
100+
name: `${ASSETS_SERVICE_NAME}:${id}`,
97101
worker: {
98102
compatibilityDate: "2024-08-01",
99103
modules: [
@@ -105,7 +109,9 @@ export const ASSETS_PLUGIN: Plugin<typeof AssetsOptionsSchema> = {
105109
bindings: [
106110
{
107111
name: "ASSETS_KV_NAMESPACE",
108-
kvNamespace: { name: ASSETS_KV_SERVICE_NAME },
112+
kvNamespace: {
113+
name: `${ASSETS_KV_SERVICE_NAME}:${id}`,
114+
},
109115
},
110116
{
111117
name: "ASSETS_MANIFEST",
@@ -120,7 +126,7 @@ export const ASSETS_PLUGIN: Plugin<typeof AssetsOptionsSchema> = {
120126
};
121127

122128
const routerService: Service = {
123-
name: ROUTER_SERVICE_NAME,
129+
name: `${ROUTER_SERVICE_NAME}:${id}`,
124130
worker: {
125131
compatibilityDate: "2024-08-01",
126132
modules: [
@@ -132,11 +138,13 @@ export const ASSETS_PLUGIN: Plugin<typeof AssetsOptionsSchema> = {
132138
bindings: [
133139
{
134140
name: "ASSET_WORKER",
135-
service: { name: ASSETS_SERVICE_NAME },
141+
service: {
142+
name: `${ASSETS_SERVICE_NAME}:${id}`,
143+
},
136144
},
137145
{
138146
name: "USER_WORKER",
139-
service: { name: getUserServiceName(options.assets.workerName) },
147+
service: { name: getUserServiceName(id) },
140148
},
141149
{
142150
name: "CONFIG",

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ function getCustomServiceDesignator(
262262
} else if (service === kCurrentWorker) {
263263
// Sets SELF binding to point to router worker instead if assets are present.
264264
serviceName = hasAssetsAndIsVitest
265-
? ROUTER_SERVICE_NAME
265+
? `${ROUTER_SERVICE_NAME}:${refererName}`
266266
: getUserServiceName(refererName);
267267
} else {
268268
// Regular user worker

0 commit comments

Comments
 (0)