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

Function openConn

packages/server/test/ws-auth.e2e.test.ts:120–148  ·  view source on GitHub ↗
(url: string, opts?: ConnectOptions)

Source from the content-addressed store, hash-verified

118}
119
120function openConn(url: string, opts?: ConnectOptions): Promise<Conn> {
121 return new Promise((resolve, reject) => {
122 const ws = new WebSocket(url, opts?.protocols, { headers: opts?.headers });
123 const queue: WsFrame[] = [];
124 const waiters: Array<(frame: WsFrame) => void> = [];
125 let closedResolve: (v: { code: number; reason: string }) => void;
126 const closed = new Promise<{ code: number; reason: string }>((res) => {
127 closedResolve = res;
128 });
129 ws.on('message', (data) => {
130 let parsed: WsFrame;
131 try {
132 parsed = JSON.parse(rawDataToString(data)) as WsFrame;
133 } catch {
134 return;
135 }
136 if (waiters.length > 0) {
137 waiters.shift()?.(parsed);
138 } else {
139 queue.push(parsed);
140 }
141 });
142 ws.on('close', (code, reason) => {
143 closedResolve({ code, reason: String(reason) });
144 });
145 ws.once('open', () => resolve({ ws, queue, waiters, closed }));
146 ws.once('error', (err) => reject(err));
147 });
148}
149
150function receive(conn: Conn, timeoutMs: number): Promise<WsFrame> {
151 return new Promise((resolve, reject) => {

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