MCPcopy Create free account
hub / github.com/MoonshotAI/kimi-code / openConn

Function openConn

packages/server/test/ws-handshake.e2e.test.ts:125–154  ·  view source on GitHub ↗
(url: string)

Source from the content-addressed store, hash-verified

123}
124
125function openConn(url: string): Promise<Conn> {
126 return new Promise((resolve, reject) => {
127 const ws = new WebSocket(url, ['kimi-code.bearer.test-token']);
128 const queue: WsFrame[] = [];
129 const waiters: Array<(frame: WsFrame) => void> = [];
130 let closedResolve: (v: { code: number; reason: string }) => void;
131 const closed = new Promise<{ code: number; reason: string }>((res) => {
132 closedResolve = res;
133 });
134 ws.on('message', (data) => {
135 let parsed: WsFrame;
136 try {
137 parsed = JSON.parse(rawDataToString(data)) as WsFrame;
138 } catch {
139 return;
140 }
141 if (waiters.length > 0) {
142 const w = waiters.shift();
143 w?.(parsed);
144 } else {
145 queue.push(parsed);
146 }
147 });
148 ws.on('close', (code, reason) => {
149 closedResolve({ code, reason: String(reason) });
150 });
151 ws.once('open', () => resolve({ ws, queue, waiters, closed }));
152 ws.once('error', (err) => reject(err));
153 });
154}
155
156/** Pop the next frame (queued or yet-to-arrive). Rejects on timeout. */
157function receive(conn: Conn, timeoutMs: number): Promise<WsFrame> {

Callers 1

Calls 6

rawDataToStringFunction · 0.90
rejectFunction · 0.85
onMethod · 0.65
onceMethod · 0.65
resolveFunction · 0.50
pushMethod · 0.45

Tested by

no test coverage detected