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

Function openConn

packages/server/test/host-origin.e2e.test.ts:120–151  ·  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 // Offer the fixed bearer token so the M5.1 WS auth passes; the Host/Origin
123 // cases that expect rejection use `expectRejected` (no token) instead.
124 const protocols = [...(opts?.protocols ?? []), 'kimi-code.bearer.test-token'];
125 const ws = new WebSocket(url, protocols, { headers: opts?.headers });
126 const queue: WsFrame[] = [];
127 const waiters: Array<(frame: WsFrame) => void> = [];
128 let closedResolve: (v: { code: number; reason: string }) => void;
129 const closed = new Promise<{ code: number; reason: string }>((res) => {
130 closedResolve = res;
131 });
132 ws.on('message', (data) => {
133 let parsed: WsFrame;
134 try {
135 parsed = JSON.parse(rawDataToString(data)) as WsFrame;
136 } catch {
137 return;
138 }
139 if (waiters.length > 0) {
140 waiters.shift()?.(parsed);
141 } else {
142 queue.push(parsed);
143 }
144 });
145 ws.on('close', (code, reason) => {
146 closedResolve({ code, reason: String(reason) });
147 });
148 ws.once('open', () => resolve({ ws, queue, waiters, closed }));
149 ws.once('error', (err) => reject(err));
150 });
151}
152
153function receive(conn: Conn, timeoutMs: number): Promise<WsFrame> {
154 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