Skip to content

Commit 4418341

Browse files
committed
add tests and changeset
1 parent 7a47644 commit 4418341

File tree

8 files changed

+242
-9
lines changed

8 files changed

+242
-9
lines changed

.changeset/short-humans-nail.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
refactor: switch `getPlatformProxy()` to use Miniflare's dev registry implementation
6+
7+
Updated `getPlatformProxy()` to use Miniflare's dev registry instead of Wrangler's implementation. Previously, you had to start a wrangler or vite dev session before accessing the proxy bindings to connect to those workers. Now the order doesn't matter.

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

Lines changed: 168 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as fs from "node:fs/promises";
22
import * as os from "node:os";
33
import * as path from "node:path";
44
import { resolve } from "node:path";
5-
import { describe, expect, test, vi } from "vitest";
5+
import { describe, expect, onTestFinished, test, vi } from "vitest";
66
import {
77
runLongLived,
88
waitForReady,
@@ -44,9 +44,29 @@ async function runWranglerDev(
4444
{ WRANGLER_REGISTRY_PATH: devRegistryPath }
4545
);
4646

47+
onTestFinished(() => session.stop());
48+
4749
return `http://${session.ip}:${session.port}`;
4850
}
4951

52+
async function setupPlatformProxy(config: string, devRegistryPath?: string) {
53+
vi.stubEnv("WRANGLER_REGISTRY_PATH", devRegistryPath);
54+
try {
55+
// @ts-expect-error It works
56+
const wrangler = await import("wrangler");
57+
const proxy = await wrangler.getPlatformProxy<Record<string, any>>({
58+
configPath: config,
59+
});
60+
61+
onTestFinished(() => proxy.dispose());
62+
63+
return proxy;
64+
} finally {
65+
// Unset the environment variable to avoid affecting other tests
66+
vi.stubEnv("WRANGLER_REGISTRY_PATH", undefined);
67+
}
68+
}
69+
5070
describe("Dev Registry: vite dev <-> vite dev", () => {
5171
it("supports module worker fetch over service binding", async ({
5272
devRegistryPath,
@@ -432,3 +452,150 @@ describe("Dev Registry: vite dev <-> wrangler dev", () => {
432452
});
433453
});
434454
});
455+
456+
describe("Dev Registry: getPlatformProxy -> wrangler / vite dev", () => {
457+
it("supports fetch over service binding", async ({ devRegistryPath }) => {
458+
const { env } = await setupPlatformProxy(
459+
"wrangler.worker-entrypoint-a.jsonc",
460+
devRegistryPath
461+
);
462+
463+
await vi.waitFor(async () => {
464+
const response = await env.WORKER_ENTRYPOINT_B.fetch("http://localhost");
465+
466+
expect(response.status).toBe(503);
467+
expect(await response.text()).toEqual(
468+
`Couldn't find a local dev session for the "default" entrypoint of service "worker-entrypoint-b" to proxy to`
469+
);
470+
});
471+
472+
await vi.waitFor(async () => {
473+
const response = await env.MODULE_WORKER.fetch("http://localhost");
474+
475+
expect(response.status).toBe(503);
476+
expect(await response.text()).toEqual(
477+
`Couldn't find a local dev session for the "default" entrypoint of service "module-worker" to proxy to`
478+
);
479+
});
480+
481+
await runViteDev("vite.worker-entrypoint-b.config.ts", devRegistryPath);
482+
483+
await vi.waitFor(async () => {
484+
const response = await env.WORKER_ENTRYPOINT_B.fetch("http://localhost");
485+
486+
expect(await response.text()).toEqual("Hello from Worker Entrypoint!");
487+
expect(response.status).toBe(200);
488+
});
489+
490+
await vi.waitFor(async () => {
491+
const response = await env.MODULE_WORKER.fetch("http://localhost");
492+
493+
expect(response.status).toBe(503);
494+
expect(await response.text()).toEqual(
495+
`Couldn't find a local dev session for the "default" entrypoint of service "module-worker" to proxy to`
496+
);
497+
});
498+
499+
await runWranglerDev("wrangler.module-worker.jsonc", devRegistryPath);
500+
501+
await vi.waitFor(async () => {
502+
const response = await env.MODULE_WORKER.fetch("http://localhost");
503+
504+
expect(await response.text()).toEqual("Hello from Module Worker!");
505+
expect(response.status).toBe(200);
506+
});
507+
508+
await vi.waitFor(async () => {
509+
const response = await env.WORKER_ENTRYPOINT_B.fetch("http://localhost");
510+
511+
expect(await response.text()).toEqual("Hello from Worker Entrypoint!");
512+
expect(response.status).toBe(200);
513+
});
514+
});
515+
516+
it("supports RPC over service binding", async ({ devRegistryPath }) => {
517+
const { env } = await setupPlatformProxy(
518+
"wrangler.module-worker.jsonc",
519+
devRegistryPath
520+
);
521+
522+
expect(() =>
523+
env.WORKER_ENTRYPOINT_A.ping()
524+
).toThrowErrorMatchingInlineSnapshot(
525+
`[Error: Cannot access "ping" as we couldn't find a local dev session for the "default" entrypoint of service "worker-entrypoint-a" to proxy to.]`
526+
);
527+
528+
expect(() =>
529+
env.WORKER_ENTRYPOINT_B.ping()
530+
).toThrowErrorMatchingInlineSnapshot(
531+
`[Error: Cannot access "ping" as we couldn't find a local dev session for the "default" entrypoint of service "worker-entrypoint-b" to proxy to.]`
532+
);
533+
534+
await runViteDev("vite.worker-entrypoint-a.config.ts", devRegistryPath);
535+
536+
await vi.waitFor(async () => {
537+
const result = await env.WORKER_ENTRYPOINT_A.ping();
538+
expect(result).toBe("Pong");
539+
});
540+
541+
await runWranglerDev("wrangler.worker-entrypoint-b.jsonc", devRegistryPath);
542+
543+
await vi.waitFor(async () => {
544+
const result = await env.WORKER_ENTRYPOINT_B.ping();
545+
546+
expect(result).toBe("Pong");
547+
});
548+
});
549+
550+
it("supports fetch over durable object binding", async ({
551+
devRegistryPath,
552+
}) => {
553+
const { env } = await setupPlatformProxy(
554+
"wrangler.external-durable-object.jsonc",
555+
devRegistryPath
556+
);
557+
const id = env.DURABLE_OBJECT.newUniqueId();
558+
const stub = env.DURABLE_OBJECT.get(id);
559+
560+
await vi.waitFor(async () => {
561+
const response = await stub.fetch("http://localhost");
562+
expect(response.status).toBe(503);
563+
expect(await response.text()).toEqual("Service Unavailable");
564+
});
565+
566+
await runWranglerDev(
567+
"wrangler.internal-durable-object.jsonc",
568+
devRegistryPath
569+
);
570+
571+
await vi.waitFor(async () => {
572+
const response = await stub.fetch("http://localhost");
573+
574+
expect(response.status).toBe(200);
575+
expect(await response.text()).toEqual("Hello from Durable Object!");
576+
});
577+
});
578+
579+
it("supports RPC over durable object binding", async ({
580+
devRegistryPath,
581+
}) => {
582+
const { env } = await setupPlatformProxy(
583+
"wrangler.external-durable-object.jsonc",
584+
devRegistryPath
585+
);
586+
const id = env.DURABLE_OBJECT.newUniqueId();
587+
const stub = env.DURABLE_OBJECT.get(id);
588+
589+
expect(() => stub.ping()).toThrowErrorMatchingInlineSnapshot(
590+
`[Error: Cannot access "ping" as Durable Object RPC is not yet supported between multiple dev sessions.]`
591+
);
592+
await runWranglerDev(
593+
"wrangler.internal-durable-object.jsonc",
594+
devRegistryPath
595+
);
596+
597+
expect(() => stub.ping()).toThrowErrorMatchingInlineSnapshot(
598+
`[Error: Cannot access "ping" as Durable Object RPC is not yet supported between multiple dev sessions.]`
599+
);
600+
});
601+
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export default defineConfig({
99
persistState: false,
1010
auxiliaryWorkers: [
1111
{
12-
configPath: "./wrangler.durable-object.jsonc",
12+
configPath: "./wrangler.internal-durable-object.jsonc",
1313
},
1414
],
1515
}),

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export default defineConfig({
1212
persistState: false,
1313
auxiliaryWorkers: [
1414
{
15-
configPath: "./wrangler.durable-object.jsonc",
15+
configPath: "./wrangler.internal-durable-object.jsonc",
1616
},
1717
],
1818
}),

fixtures/dev-registry/workers/worker-entrypoint.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export default class Worker extends WorkerEntrypoint<{
77
MODULE_WORKER: Fetcher;
88
WORKER_ENTRYPOINT_A: Fetcher;
99
WORKER_ENTRYPOINT_B: Fetcher;
10+
DURABLE_OBJECT: DurableObjectNamespace;
1011
}> {
1112
ping() {
1213
return "Pong";
@@ -41,6 +42,12 @@ export default class Worker extends WorkerEntrypoint<{
4142
service = this.env.WORKER_ENTRYPOINT_B;
4243
break;
4344
}
45+
case "durable-object": {
46+
const id = this.env.DURABLE_OBJECT.newUniqueId();
47+
const stub = this.env.DURABLE_OBJECT.get(id);
48+
service = stub;
49+
break;
50+
}
4451
}
4552

4653
if (service && testMethod === "rpc") {

fixtures/dev-registry/wrangler.durable-object.jsonc

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"$schema": "node_modules/wrangler/config-schema.json",
3+
"name": "external-durable-object",
4+
"main": "./workers/worker-entrypoint.ts",
5+
"compatibility_date": "2025-05-01",
6+
"services": [
7+
{
8+
"binding": "SERVICE_WORKER",
9+
"service": "service-worker",
10+
},
11+
{
12+
"binding": "MODULE_WORKER",
13+
"service": "module-worker",
14+
},
15+
{
16+
"binding": "WORKER_ENTRYPOINT_A",
17+
"service": "worker-entrypoint-a",
18+
},
19+
{
20+
"binding": "WORKER_ENTRYPOINT_B",
21+
"service": "worker-entrypoint-b",
22+
},
23+
],
24+
"durable_objects": {
25+
"bindings": [
26+
{
27+
"name": "DURABLE_OBJECT",
28+
"class_name": "TestObject",
29+
"script_name": "internal-durable-object",
30+
},
31+
],
32+
},
33+
"tail_consumers": [
34+
{
35+
"service": "module-worker",
36+
},
37+
],
38+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"$schema": "node_modules/wrangler/config-schema.json",
3+
"name": "internal-durable-object",
4+
"main": "./workers/durable-object.ts",
5+
"compatibility_date": "2025-05-01",
6+
"durable_objects": {
7+
"bindings": [
8+
{
9+
"name": "DURABLE_OBJECT",
10+
"class_name": "TestObject",
11+
},
12+
],
13+
},
14+
"migrations": [
15+
{
16+
"tag": "v1",
17+
"new_sqlite_classes": ["TestObject"],
18+
},
19+
],
20+
}

0 commit comments

Comments
 (0)