File size: 8,840 Bytes
4e23b01 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | /**
* WS layer for `DaemonClient` — owns the socket, queues incoming frames so
* fast tests don't race the first `server_hello`, exposes a `waitForFrame`
* with timeouts, and routes control-message acks back to the original sender
* via `id` correlation.
*
* Frame shape is the union of WS.md §2 envelopes:
* - `event` envelope : `{type, seq, session_id, timestamp, payload}`
* - `ack` : `{type:'ack', id, code, msg, payload}`
* - `server_hello`/`ping`/`resync_required`/`error`: each carries `timestamp`
*
* We don't Zod-validate frames here — preserving forward-compat ("unknown
* fields pass through") and avoiding double-work since the server emits the
* shapes already.
*/
import { WebSocket as WsWebSocket } from 'ws';
import { recordReportEvent } from './report.js';
/** Wire frame shape — kept loose because the server adds new event types. */
export interface AnyFrame {
readonly type: string;
readonly seq?: number;
readonly session_id?: string;
readonly timestamp?: string;
readonly id?: string;
readonly code?: number;
readonly msg?: string;
readonly payload?: unknown;
}
export interface WsClientOptions {
url: string;
wsImpl: typeof WsWebSocket;
logger: (level: 'info' | 'warn' | 'error' | 'debug', msg: string, meta?: unknown) => void;
reportDir?: string;
}
type FrameWaiter = (frame: AnyFrame) => boolean;
interface PendingWaiter {
match: FrameWaiter;
resolve: (frame: AnyFrame) => void;
reject: (err: Error) => void;
timer?: NodeJS.Timeout;
}
/**
* Thin WS wrapper. Two-tier delivery:
* - All frames also fan out to subscribers added with `onFrame()`.
* - `waitForFrame(predicate)` consumes the *first matching* frame; matching
* frames already in `_queue` are dispatched immediately.
*
* Both queue and waiters are needed because the server's first `server_hello`
* can land in the same tick as `open`, before the test has a chance to
* register its first waiter (see `server/test/ws-handshake.e2e.test.ts:88-117`
* for the pattern this is ported from).
*/
export class WsClient {
private ws: WsWebSocket | null = null;
private readonly _queue: AnyFrame[] = [];
private readonly _waiters: PendingWaiter[] = [];
private readonly _subscribers = new Set<(f: AnyFrame) => void>();
private _closed = false;
private _closeReason: { code: number; reason: string } | null = null;
private _closeWaiters: Array<(v: { code: number; reason: string }) => void> = [];
constructor(private readonly opts: WsClientOptions) {}
/** Open the socket; resolves once `open` fires. */
async open(): Promise<void> {
if (this.ws) return;
await new Promise<void>((resolve, reject) => {
const ws = new this.opts.wsImpl(this.opts.url);
this.ws = ws;
ws.once('open', () => {
recordReportEvent(
{ kind: 'ws', direction: 'lifecycle', url: this.opts.url, message: 'open' },
{ reportDir: this.opts.reportDir },
);
resolve();
});
ws.once('error', (err) => {
if (this._closed) return;
recordReportEvent(
{
kind: 'ws',
direction: 'lifecycle',
url: this.opts.url,
message: 'error',
error: errorForReport(err),
},
{ reportDir: this.opts.reportDir },
);
reject(err as Error);
});
ws.on('message', (data) => this._onMessage(data));
ws.on('close', (code, reason) => this._onClose(code, String(reason ?? '')));
});
}
/** JSON-stringifies and sends a frame. */
send(frame: object): void {
if (!this.ws) throw new Error('ws not open');
this.ws.send(JSON.stringify(frame));
recordReportEvent(
{ kind: 'ws', direction: 'out', url: this.opts.url, frame },
{ reportDir: this.opts.reportDir },
);
}
/** Register a frame subscriber. Returns an unsubscribe handle. */
onFrame(handler: (f: AnyFrame) => void): () => void {
this._subscribers.add(handler);
return () => {
this._subscribers.delete(handler);
};
}
/**
* Wait for the next frame matching `predicate`. Drains queued frames first;
* the first matching frame is consumed and returned. Times out cleanly.
*/
waitForFrame(predicate: FrameWaiter, timeoutMs: number): Promise<AnyFrame> {
return new Promise((resolve, reject) => {
// Drain queue.
for (let i = 0; i < this._queue.length; i++) {
const frame = this._queue[i];
if (frame === undefined) continue;
if (predicate(frame)) {
this._queue.splice(i, 1);
resolve(frame);
return;
}
}
if (this._closed) {
reject(new Error(`ws closed before matching frame arrived (code=${this._closeReason?.code})`));
return;
}
const waiter: PendingWaiter = {
match: predicate,
resolve: (f) => {
if (waiter.timer) clearTimeout(waiter.timer);
resolve(f);
},
reject: (e) => {
if (waiter.timer) clearTimeout(waiter.timer);
reject(e);
},
};
waiter.timer = setTimeout(() => {
const idx = this._waiters.indexOf(waiter);
if (idx >= 0) this._waiters.splice(idx, 1);
reject(new Error(`waitForFrame timed out after ${timeoutMs}ms`));
}, timeoutMs);
waiter.timer.unref?.();
this._waiters.push(waiter);
});
}
/** Send a control message and wait for its `ack` (matched by `id`). */
async sendAndAwaitAck(frame: { type: string; id: string; payload: unknown }, timeoutMs: number): Promise<AnyFrame> {
this.send(frame);
return this.waitForFrame(
(f) => f.type === 'ack' && f.id === frame.id,
timeoutMs,
);
}
/** Resolves when the socket closes (or immediately if already closed). */
closed(): Promise<{ code: number; reason: string }> {
if (this._closeReason) return Promise.resolve(this._closeReason);
return new Promise((resolve) => {
this._closeWaiters.push(resolve);
});
}
/** Initiate close from the client side. */
async close(): Promise<void> {
if (!this.ws || this._closed) return;
this.ws.close();
await this.closed();
}
private _onMessage(data: unknown): void {
let frame: AnyFrame;
try {
const raw = typeof data === 'string' ? data : String(data);
frame = JSON.parse(raw) as AnyFrame;
} catch (err) {
this.opts.logger('warn', 'ws: dropped non-JSON frame', { err: String(err) });
recordReportEvent(
{
kind: 'ws',
direction: 'in',
url: this.opts.url,
message: 'dropped non-JSON frame',
error: errorForReport(err),
},
{ reportDir: this.opts.reportDir },
);
return;
}
recordReportEvent(
{ kind: 'ws', direction: 'in', url: this.opts.url, frame },
{ reportDir: this.opts.reportDir },
);
if (frame.type === 'ping') {
this.send({ type: 'pong', payload: { nonce: pingNonce(frame) } });
}
// Dispatch to subscribers first — they observe every frame, regardless of
// whether a `waitForFrame` consumed it.
for (const sub of this._subscribers) {
try {
sub(frame);
} catch (err) {
this.opts.logger('warn', 'ws: subscriber threw', { err: String(err) });
}
}
// Find the FIRST waiter whose predicate matches. A waiter is single-shot.
for (let i = 0; i < this._waiters.length; i++) {
const w = this._waiters[i];
if (w === undefined) continue;
let matches = false;
try {
matches = w.match(frame);
} catch (err) {
this.opts.logger('warn', 'ws: waiter predicate threw', { err: String(err) });
}
if (matches) {
this._waiters.splice(i, 1);
w.resolve(frame);
return;
}
}
this._queue.push(frame);
}
private _onClose(code: number, reason: string): void {
this._closed = true;
this._closeReason = { code, reason };
recordReportEvent(
{
kind: 'ws',
direction: 'lifecycle',
url: this.opts.url,
message: 'close',
frame: { code, reason },
},
{ reportDir: this.opts.reportDir },
);
for (const w of this._waiters.splice(0)) {
w.reject(new Error(`ws closed (code=${code}) before matching frame arrived`));
}
for (const w of this._closeWaiters.splice(0)) w(this._closeReason);
}
}
function pingNonce(frame: AnyFrame): string {
const payload = frame.payload as { nonce?: unknown } | undefined;
return typeof payload?.nonce === 'string' ? payload.nonce : '';
}
function errorForReport(error: unknown): unknown {
if (error instanceof Error) {
return {
name: error.name,
message: error.message,
};
}
return error;
}
|