Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow assets in multiworker setup #7290

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions packages/miniflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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);
Expand Down Expand Up @@ -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: {
Expand All @@ -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,
Expand Down
18 changes: 12 additions & 6 deletions packages/miniflare/src/plugins/assets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ export const ASSETS_PLUGIN: Plugin<typeof AssetsOptionsSchema> = {
{
// binding between User Worker and Asset Worker
name: options.assets.binding,
service: { name: ASSETS_SERVICE_NAME },
service: {
name: `${ASSETS_SERVICE_NAME}-${options.assets.workerName}`,
},
},
];
},
Expand Down Expand Up @@ -69,7 +71,7 @@ export const ASSETS_PLUGIN: Plugin<typeof AssetsOptionsSchema> = {
);

const namespaceService: Service = {
name: ASSETS_KV_SERVICE_NAME,
name: `${ASSETS_KV_SERVICE_NAME}-${options.assets.workerName}`,
worker: {
compatibilityDate: "2023-07-24",
compatibilityFlags: ["nodejs_compat"],
Expand All @@ -93,7 +95,7 @@ export const ASSETS_PLUGIN: Plugin<typeof AssetsOptionsSchema> = {
};

const assetService: Service = {
name: ASSETS_SERVICE_NAME,
name: `${ASSETS_SERVICE_NAME}-${options.assets.workerName}`,
worker: {
compatibilityDate: "2024-08-01",
modules: [
Expand All @@ -105,7 +107,9 @@ export const ASSETS_PLUGIN: Plugin<typeof AssetsOptionsSchema> = {
bindings: [
{
name: "ASSETS_KV_NAMESPACE",
kvNamespace: { name: ASSETS_KV_SERVICE_NAME },
kvNamespace: {
name: `${ASSETS_KV_SERVICE_NAME}-${options.assets.workerName}`,
},
},
{
name: "ASSETS_MANIFEST",
Expand All @@ -120,7 +124,7 @@ export const ASSETS_PLUGIN: Plugin<typeof AssetsOptionsSchema> = {
};

const routerService: Service = {
name: ROUTER_SERVICE_NAME,
name: `${ROUTER_SERVICE_NAME}-${options.assets.workerName}`,
worker: {
compatibilityDate: "2024-08-01",
modules: [
Expand All @@ -132,7 +136,9 @@ export const ASSETS_PLUGIN: Plugin<typeof AssetsOptionsSchema> = {
bindings: [
{
name: "ASSET_WORKER",
service: { name: ASSETS_SERVICE_NAME },
service: {
name: `${ASSETS_SERVICE_NAME}-${options.assets.workerName}`,
},
},
{
name: "USER_WORKER",
Expand Down
2 changes: 1 addition & 1 deletion packages/miniflare/src/plugins/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading