feat(mesh): connect the teacup over a runtime-neutral WebSocket client

connectLink constructed `require('ws')` directly and drove its EventEmitter API,
which is why a teacup could only run on Node. It now goes through the neutral
WsSocket like every other consumer, over whichever client the runtime provides:
the platform global (Deno, Bun, Node >= 22) or the `ws` peer for Node 18-21,
which has no global. `ws` stays optional and is only reached for when the global
is absent.

One bridge covers both because `ws` also implements the browser-compatible event
API and delivers text frames as `event.data` strings — probed against the real
package rather than taken on faith.

Both implementations are exercised in one run: `ws` is always in the matrix, so
the Node 18-21 path cannot rot unseen on CI that only runs a modern Node, which
is how a fallback quietly stops working. connectLink takes an optional Ctor to
make that possible; it doubles as a bring-your-own-WebSocket hook for runtimes
we do not ship an adapter for.

Verified the edge bundle is unaffected: `require('ws')` tree-shakes out of the
workerd build entirely.

Refs .specs/2026-07-15-green-tea-mesh-cross-runtime-plan.md Task 4

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-07-15 15:00:04 -05:00
1 parent 71b59e7c25
commit 0ea4db3fef
3 files changed
+168 -56

No files matched your search

+44 -2
View File
@@ -41,8 +41,8 @@ export interface EventfulSocket {
send(data: string): void;
close(code?: number, reason?: string): void;
addEventListener(
type: 'message' | 'close' | 'error',
listener: (event: { data?: string | ArrayBuffer }) => void,
type: 'open' | 'message' | 'close' | 'error',
listener: (event: { data?: string | ArrayBuffer; error?: unknown }) => void,
): void;
}
@@ -107,6 +107,48 @@ export function neutralSocket(
};
}
/** Constructs a host WebSocket *client*. Both the platform global and the `ws` package satisfy it. */
export type SocketCtor = new (url: string) => EventfulSocket;
/**
* The WebSocket client implementation: the platform global where it exists (Deno, Bun, and
* Node >= 22), else the `ws` optional peer — Node 18-21, our declared floor, has no global.
*
* Both expose the same `addEventListener` API and deliver text frames as `event.data` strings,
* so one bridge covers both. That is verified rather than assumed: `ws` implements the
* browser-compatible event API alongside its EventEmitter one.
*/
export function loadWebSocketCtor(): SocketCtor {
const platform = (globalThis as { WebSocket?: SocketCtor }).WebSocket;
if (platform) return platform;
// eslint-disable-next-line @typescript-eslint/no-require-imports
return require('ws') as SocketCtor;
}
/**
* Opens a client connection and wraps it as a neutral {@link WsSocket}. `opened` resolves once
* the socket is live (the caller's cue to send its first frame) and rejects if it never opens.
*
* `Ctor` is injectable so both implementations can be exercised on one runtime — otherwise CI
* on a single Node version would silently cover only one of the two paths.
*/
export function connectSocket(
url: string,
Ctor: SocketCtor = loadWebSocketCtor(),
): { socket: WsSocket; opened: Promise<void> } {
const host = new Ctor(url);
// registered before eventSocket's own error listener, so a connect failure rejects `opened`
// with the host's real error rather than eventSocket's generic one
const opened = new Promise<void>((resolve, reject) => {
host.addEventListener('open', () => resolve());
host.addEventListener('error', (event) => reject(event?.error ?? new Error(`websocket error: ${url}`)));
});
return { socket: eventSocket(host), opened };
}
/**
* Runs the ws lifecycle, closing with 1011 if it rejects. `app.upgrade` can reject *before*
* `runWsConnection`'s own try/catch (mesh-before-listen, provider boot failure); unguarded,