diff --git a/packages/miniflare/src/index.ts b/packages/miniflare/src/index.ts index 2c5987a4c216..3575688f31ac 100644 --- a/packages/miniflare/src/index.ts +++ b/packages/miniflare/src/index.ts @@ -1131,14 +1131,6 @@ export class Miniflare { innerBindings: Worker_Binding[]; }[] = []; - if (this.#workerOpts[0].assets.assets) { - // This will be the UserWorker, or the vitest pool worker wrapping the UserWorker - // The asset plugin needs this so that it can set the binding between the RouterWorker and the UserWorker - // TODO: apply this to ever this.#workerOpts, not just the first (i.e this.#workerOpts[0]) - this.#workerOpts[0].assets.assets.workerName = - this.#workerOpts[0].core.name; - } - for (let i = 0; i < allWorkerOpts.length; i++) { const previousWorkerOpts = allPreviousWorkerOpts?.[i]; const workerOpts = allWorkerOpts[i]; @@ -1153,6 +1145,13 @@ export class Miniflare { } } + if (workerOpts.assets.assets) { + // This will be the UserWorker, or the vitest pool worker wrapping the UserWorker + // The asset plugin needs this so that it can set the binding between the RouterWorker and the UserWorker + // TODO: apply this to ever this.#workerOpts, not just the first (i.e this.#workerOpts[0]) + workerOpts.assets.assets.workerName = workerOpts.core.name; + } + // Collect all bindings from this worker const workerBindings: Worker_Binding[] = []; allWorkerBindings.set(workerName, workerBindings); @@ -1284,7 +1283,9 @@ export class Miniflare { name, address, service: { - name: getUserServiceName(workerName), + name: workerOpts.assets.assets + ? `${ROUTER_SERVICE_NAME}-${workerName}` + : getUserServiceName(workerName), entrypoint: entrypoint === "default" ? undefined : entrypoint, }, http: { @@ -1306,7 +1307,7 @@ export class Miniflare { !this.#workerOpts[0].core.name?.startsWith( "vitest-pool-workers-runner-" ) - ? ROUTER_SERVICE_NAME + ? `${ROUTER_SERVICE_NAME}-${this.#workerOpts[0].core.name}` : getUserServiceName(this.#workerOpts[0].core.name), loopbackPort, log: this.#log, diff --git a/packages/miniflare/src/plugins/assets/index.ts b/packages/miniflare/src/plugins/assets/index.ts index e25c369e1e53..0faf54ad25e7 100644 --- a/packages/miniflare/src/plugins/assets/index.ts +++ b/packages/miniflare/src/plugins/assets/index.ts @@ -39,7 +39,9 @@ export const ASSETS_PLUGIN: Plugin = { { // binding between User Worker and Asset Worker name: options.assets.binding, - service: { name: ASSETS_SERVICE_NAME }, + service: { + name: `${ASSETS_SERVICE_NAME}-${options.assets.workerName}`, + }, }, ]; }, @@ -69,7 +71,7 @@ export const ASSETS_PLUGIN: Plugin = { ); const namespaceService: Service = { - name: ASSETS_KV_SERVICE_NAME, + name: `${ASSETS_KV_SERVICE_NAME}-${options.assets.workerName}`, worker: { compatibilityDate: "2023-07-24", compatibilityFlags: ["nodejs_compat"], @@ -93,7 +95,7 @@ export const ASSETS_PLUGIN: Plugin = { }; const assetService: Service = { - name: ASSETS_SERVICE_NAME, + name: `${ASSETS_SERVICE_NAME}-${options.assets.workerName}`, worker: { compatibilityDate: "2024-08-01", modules: [ @@ -105,7 +107,9 @@ export const ASSETS_PLUGIN: Plugin = { bindings: [ { name: "ASSETS_KV_NAMESPACE", - kvNamespace: { name: ASSETS_KV_SERVICE_NAME }, + kvNamespace: { + name: `${ASSETS_KV_SERVICE_NAME}-${options.assets.workerName}`, + }, }, { name: "ASSETS_MANIFEST", @@ -120,7 +124,7 @@ export const ASSETS_PLUGIN: Plugin = { }; const routerService: Service = { - name: ROUTER_SERVICE_NAME, + name: `${ROUTER_SERVICE_NAME}-${options.assets.workerName}`, worker: { compatibilityDate: "2024-08-01", modules: [ @@ -132,7 +136,9 @@ export const ASSETS_PLUGIN: Plugin = { bindings: [ { name: "ASSET_WORKER", - service: { name: ASSETS_SERVICE_NAME }, + service: { + name: `${ASSETS_SERVICE_NAME}-${options.assets.workerName}`, + }, }, { name: "USER_WORKER", diff --git a/packages/miniflare/src/plugins/core/index.ts b/packages/miniflare/src/plugins/core/index.ts index 721508b6290f..4f0bfebf76ea 100644 --- a/packages/miniflare/src/plugins/core/index.ts +++ b/packages/miniflare/src/plugins/core/index.ts @@ -262,7 +262,7 @@ function getCustomServiceDesignator( } else if (service === kCurrentWorker) { // Sets SELF binding to point to router worker instead if assets are present. serviceName = hasAssetsAndIsVitest - ? ROUTER_SERVICE_NAME + ? `${ROUTER_SERVICE_NAME}-${refererName}` : getUserServiceName(refererName); } else { // Regular user worker