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:
3 files changed
+168
-56
No files matched your search
+27
-1
@@ -1,6 +1,7 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { WebSocketServer } from 'ws';
|
||||
import WS, { WebSocketServer } from 'ws';
|
||||
import { connectLink } from '../../src/mesh/link';
|
||||
import { loadWebSocketCtor, type SocketCtor } from '../../src/http/ws-adapter';
|
||||
import { encode, decode, MESH_PROTOCOL_VERSION } from '../../src/mesh/protocol';
|
||||
|
||||
const V = MESH_PROTOCOL_VERSION;
|
||||
@@ -53,6 +54,31 @@ function startSkewedTeapot() {
|
||||
return { url: `ws://127.0.0.1:${port}/__mesh__/control`, close: () => wss.close() };
|
||||
}
|
||||
|
||||
/**
|
||||
* Both client implementations, exercised on whatever Node runs the suite. `ws` is always in the
|
||||
* list: it is the Node 18-21 path, and on a modern CI the platform global would otherwise be the
|
||||
* only one ever covered — the fallback would rot unnoticed until someone ran the declared floor.
|
||||
*/
|
||||
const platform = (globalThis as { WebSocket?: SocketCtor }).WebSocket;
|
||||
const CTORS: [string, SocketCtor][] = [['the ws package (Node < 22 path)', WS as unknown as SocketCtor]];
|
||||
if (platform) CTORS.push(['the platform global WebSocket', platform]);
|
||||
|
||||
describe('WebSocket client implementations', () => {
|
||||
it.each(CTORS)('handshakes and serves RPC over %s', async (_label, Ctor) => {
|
||||
const t = startTeapot();
|
||||
const link = await connectLink({ url: t.url, secret: 'good', Ctor });
|
||||
|
||||
expect(link.manifest.scopes).toEqual([{ token: 'auth', scope: 'request' }]);
|
||||
expect(await link.rpc('scope', 'auth', env)).toEqual({ got: 'auth' });
|
||||
link.close();
|
||||
t.close();
|
||||
});
|
||||
|
||||
it('prefers the platform global when it exists, else falls back to the ws package', () => {
|
||||
expect(loadWebSocketCtor()).toBe(platform ?? (WS as unknown as SocketCtor));
|
||||
});
|
||||
});
|
||||
|
||||
describe('connectLink', () => {
|
||||
it('sends its protocol version in the hello', async () => {
|
||||
const seen: any[] = [];
|
||||
|
||||
Reference in new issue
Block a user