Skip to content

Commit 70ba9fb

Browse files
authored
fix(vite-plugin-cloudflare): allow unknown entrypoint on startup (#9583)
* fix(vite-plugin-cloudflare): allow unknown entrypoint on startup * chore: simplify waitUntil implementation * add changeset * chore: add unknown named entrypoint service binding to vite worker playground
1 parent 02e2c1e commit 70ba9fb

File tree

5 files changed

+35
-26
lines changed

5 files changed

+35
-26
lines changed

.changeset/nine-facts-kneel.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@cloudflare/vite-plugin": patch
3+
---
4+
5+
fix: avoid crashing on unknown service bindings at startup
6+
7+
With Dev Registry support, the plugin no longer throws an assertion error during startup when a service binding references a named entrypoint from an unknown worker. Instead, an appropriate runtime error will be returned if the worker cannot be resolved.

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
"binding": "WORKER_ENTRYPOINT_B",
2121
"service": "worker-entrypoint-b",
2222
},
23+
{
24+
"binding": "NAMED_ENTRYPOINT",
25+
"service": "unknown-worker",
26+
"entrypoint": "UnknownEntrypoint",
27+
},
2328
],
2429
"tail_consumers": [
2530
{

packages/miniflare/test/test-shared/asserts.ts

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -63,33 +63,22 @@ export async function waitFor<T>(
6363
}
6464
}
6565

66-
export function waitUntil(
66+
export async function waitUntil(
6767
t: ExecutionContext,
6868
impl: (t: ExecutionContext) => Awaitable<void>,
69-
timeout: number = 5000,
70-
delay: number = 200
69+
timeout: number = 5000
7170
): Promise<void> {
72-
return new Promise((resolve, reject) => {
73-
const start = Date.now();
74-
const interval = setInterval(async () => {
75-
try {
76-
const result = await t.try(impl);
71+
const start = Date.now();
7772

78-
if (result.passed || Date.now() - start > timeout) {
79-
clearInterval(interval);
80-
try {
81-
result.commit();
82-
resolve();
83-
} catch (ex) {
84-
reject(ex);
85-
}
86-
return;
87-
}
73+
while (true) {
74+
const result = await t.try(impl);
8875

89-
result.discard();
90-
} catch {
91-
// Ignore errors and keep waiting
92-
}
93-
}, delay);
94-
});
76+
if (result.passed || Date.now() - start > timeout) {
77+
result.commit();
78+
return;
79+
}
80+
81+
result.discard();
82+
await sleep(100);
83+
}
9584
}

packages/vite-plugin-cloudflare/playground/worker/wrangler.jsonc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,11 @@
22
"name": "worker",
33
"main": "./src/index.ts",
44
"compatibility_date": "2024-12-30",
5+
"services": [
6+
{
7+
"binding": "TEST_IF_VITE_CRASH_WITH_UNKNOWN_NAMED_ENTRYPOINT",
8+
"service": "unknown-worker",
9+
"entrypoint": "NamedEntrypoint",
10+
},
11+
],
512
}

packages/vite-plugin-cloudflare/src/miniflare-options.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,10 @@ function getWorkerToWorkerEntrypointNamesMap(
8484
value.name === kCurrentWorker ? worker.name : value.name;
8585
const entrypointNames =
8686
workerToWorkerEntrypointNamesMap.get(targetWorkerName);
87-
assert(entrypointNames, missingWorkerErrorMessage(targetWorkerName));
8887

89-
entrypointNames.add(value.entrypoint);
88+
if (entrypointNames) {
89+
entrypointNames.add(value.entrypoint);
90+
}
9091
}
9192
}
9293
}

0 commit comments

Comments
 (0)