Skip to content

Commit 8ba7736

Browse files
authored
fix(vite-plugin-cloudflare): ensure cross-process service bindings route through asset worker (#9556)
1 parent 502a8e0 commit 8ba7736

File tree

14 files changed

+133
-8
lines changed

14 files changed

+133
-8
lines changed

.changeset/eight-buckets-bathe.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cloudflare/vite-plugin": patch
3+
---
4+
5+
fix: cross-process service bindings no longer skip static asset serving

.changeset/icy-lands-build.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"miniflare": minor
3+
---
4+
5+
Added a `serviceName` option to `unsafeDirectSockets`
6+
7+
This allows registering the current worker in the dev registry under its own name, but routing to a different service.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is an example asset file

fixtures/dev-registry/tests/dev-registry.test.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ describe("Dev Registry: vite dev <-> vite dev", () => {
5151
it("supports module worker fetch over service binding", async ({
5252
devRegistryPath,
5353
}) => {
54-
const workerEntrypointA = await runViteDev(
55-
"vite.worker-entrypoint-a.config.ts",
54+
const workerEntrypointB = await runViteDev(
55+
"vite.worker-entrypoint-b.config.ts",
5656
devRegistryPath
5757
);
5858

@@ -62,7 +62,7 @@ describe("Dev Registry: vite dev <-> vite dev", () => {
6262
"test-service": "module-worker",
6363
"test-method": "fetch",
6464
});
65-
const response = await fetch(`${workerEntrypointA}?${searchParams}`);
65+
const response = await fetch(`${workerEntrypointB}?${searchParams}`);
6666

6767
expect(response.status).toBe(503);
6868
expect(await response.text()).toEqual(
@@ -78,13 +78,21 @@ describe("Dev Registry: vite dev <-> vite dev", () => {
7878
// Test module-worker -> worker-entrypoint
7979
await vi.waitFor(async () => {
8080
const searchParams = new URLSearchParams({
81-
"test-service": "worker-entrypoint-a",
81+
"test-service": "worker-entrypoint-b",
8282
"test-method": "fetch",
8383
});
8484
const response = await fetch(`${moduleWorker}?${searchParams}`);
8585

8686
expect(await response.text()).toBe("Hello from Worker Entrypoint!");
8787
expect(response.status).toBe(200);
88+
89+
// Test fetching asset from "worker-entrypoint-b" over service binding
90+
// Module worker has no assets, so it will hit the user worker and
91+
// forward the request to "worker-entrypoint-b" with the asset path
92+
const assetResponse = await fetch(
93+
`${moduleWorker}/example.txt?${searchParams}`
94+
);
95+
expect(await assetResponse.text()).toBe("This is an example asset file");
8896
});
8997

9098
// Test worker-entrypoint -> module-worker
@@ -93,7 +101,7 @@ describe("Dev Registry: vite dev <-> vite dev", () => {
93101
"test-service": "module-worker",
94102
"test-method": "fetch",
95103
});
96-
const response = await fetch(`${workerEntrypointA}?${searchParams}`);
104+
const response = await fetch(`${workerEntrypointB}?${searchParams}`);
97105

98106
expect(await response.text()).toEqual("Hello from Module Worker!");
99107
expect(response.status).toBe(200);

fixtures/dev-registry/vite.worker-entrypoint-b.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import { cloudflare } from "@cloudflare/vite-plugin";
22
import { defineConfig } from "vite";
33

44
export default defineConfig({
5+
// Override the default public directory to use the assets directory
6+
// so that other vite projects won't share the same assets
7+
publicDir: "./assets",
58
plugins: [
69
cloudflare({
710
configPath: "./wrangler.worker-entrypoint-b.jsonc",

fixtures/dev-registry/wrangler.service-worker.jsonc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"$schema": "node_modules/wrangler/config-schema.json",
23
"name": "service-worker",
34
"main": "./workers/service-worker.ts",
45
"compatibility_date": "2025-05-01",

fixtures/dev-registry/wrangler.worker-entrypoint-a.jsonc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"$schema": "node_modules/wrangler/config-schema.json",
23
"name": "worker-entrypoint-a",
34
"main": "./workers/worker-entrypoint.ts",
45
"compatibility_date": "2025-05-01",

fixtures/dev-registry/wrangler.worker-entrypoint-b.jsonc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
{
2+
"$schema": "node_modules/wrangler/config-schema.json",
23
"name": "worker-entrypoint-b",
34
"main": "./workers/worker-entrypoint.ts",
45
"compatibility_date": "2025-05-01",
6+
"assets": {
7+
"directory": "./assets",
8+
},
59
"services": [
610
{
711
"binding": "SERVICE_WORKER",

packages/miniflare/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1819,6 +1819,7 @@ export class Miniflare {
18191819
for (let j = 0; j < directSockets.length; j++) {
18201820
const previousDirectSocket = previousDirectSockets[j];
18211821
const directSocket = directSockets[j];
1822+
const serviceName = directSocket.serviceName ?? workerName;
18221823
const entrypoint = directSocket.entrypoint ?? "default";
18231824
const name = getDirectSocketName(i, entrypoint);
18241825
const address = this.#getSocketAddress(
@@ -1835,7 +1836,7 @@ export class Miniflare {
18351836
name: `${RPC_PROXY_SERVICE_NAME}:${workerOpts.core.name}`,
18361837
}
18371838
: {
1838-
name: getUserServiceName(workerName),
1839+
name: getUserServiceName(serviceName),
18391840
entrypoint: entrypoint === "default" ? undefined : entrypoint,
18401841
};
18411842

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ const UnusableStringSchema = z.string().transform(() => undefined);
118118
export const UnsafeDirectSocketSchema = z.object({
119119
host: z.ostring(),
120120
port: z.onumber(),
121+
serviceName: z.ostring(),
121122
entrypoint: z.ostring(),
122123
proxy: z.oboolean(),
123124
});

0 commit comments

Comments
 (0)