MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / readHelloLine

Function readHelloLine

src/mcp/proxy.ts:657–706  ·  view source on GitHub ↗

* Read one CRLF/LF-terminated JSON line from the socket, parse it as the * daemon hello, and return it. Bounded to MAX_HELLO_LINE_BYTES so a * malicious or broken peer can't OOM us. Times out after `timeoutMs` (3s by * default) — a healthy daemon sends hello immediately on accept.

(socket: net.Socket, timeoutMs: number = DAEMON_HELLO_TIMEOUT_MS)

Source from the content-addressed store, hash-verified

655 * default) — a healthy daemon sends hello immediately on accept.
656 */
657function readHelloLine(socket: net.Socket, timeoutMs: number = DAEMON_HELLO_TIMEOUT_MS): Promise<DaemonHello> {
658 return new Promise((resolve, reject) => {
659 let buffer = '';
660 const cleanup = () => {
661 socket.removeListener('data', onData);
662 socket.removeListener('error', onError);
663 socket.removeListener('close', onClose);
664 clearTimeout(timer);
665 };
666 const onData = (chunk: string | Buffer) => {
667 buffer += typeof chunk === 'string' ? chunk : chunk.toString('utf8');
668 const idx = buffer.indexOf('\n');
669 if (idx === -1) {
670 if (buffer.length > MAX_HELLO_LINE_BYTES) {
671 cleanup();
672 reject(new Error('daemon hello line exceeded size limit'));
673 }
674 return;
675 }
676 const line = buffer.slice(0, idx);
677 // Re-emit anything past the newline so the pipe-stage sees it.
678 const tail = buffer.slice(idx + 1);
679 cleanup();
680 if (tail.length > 0) {
681 // Push back via unshift — Node's net.Socket supports it on readable streams.
682 socket.unshift(tail);
683 }
684 try {
685 const parsed = JSON.parse(line) as DaemonHello;
686 if (typeof parsed.codegraph !== 'string' || typeof parsed.pid !== 'number') {
687 reject(new Error('daemon hello missing required fields'));
688 return;
689 }
690 resolve(parsed);
691 } catch (err) {
692 reject(new Error(`daemon hello not JSON: ${err instanceof Error ? err.message : String(err)}`));
693 }
694 };
695 const onError = (err: Error) => { cleanup(); reject(err); };
696 const onClose = () => { cleanup(); reject(new Error('daemon closed connection before hello')); };
697 const timer = setTimeout(() => {
698 cleanup();
699 reject(new Error('timed out waiting for daemon hello'));
700 }, timeoutMs);
701 timer.unref?.();
702 socket.on('data', onData);
703 socket.on('error', onError);
704 socket.on('close', onClose);
705 });
706}
707
708/**
709 * Read the daemon's answer to our client-hello's challenge: one

Callers 1

connectWithHelloFunction · 0.85

Calls 2

cleanupFunction · 0.70
onMethod · 0.65

Tested by

no test coverage detected