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

Function onData

src/mcp/hook-client.ts:136–161  ·  view source on GitHub ↗
(chunk: string | Buffer)

Source from the content-addressed store, hash-verified

134 };
135 const timer = setTimeout(() => finish(false), timeoutMs);
136 const onData = (chunk: string | Buffer): void => {
137 buffer += typeof chunk === 'string' ? chunk : chunk.toString('utf8');
138 if (buffer.length > MAX_HANDSHAKE_CHARS) return finish(false);
139 let newline: number;
140 while ((newline = buffer.indexOf('\n')) !== -1) {
141 const line = buffer.slice(0, newline);
142 buffer = buffer.slice(newline + 1);
143 let message: Record<string, unknown> | null;
144 try {
145 message = JSON.parse(line);
146 } catch {
147 return finish(false);
148 }
149 if (!message || typeof message !== 'object') return finish(false);
150 if (challenge === null) {
151 // The daemon's hello. Another version gets no prompt: the hook explores itself.
152 if (message.protocol !== 1 || message.codegraph !== CodeGraphPackageVersion) return finish(false);
153 challenge = newDaemonChallenge();
154 socket.write(JSON.stringify({ codegraph_client: 1, pid: process.pid, hostPid: null, challenge }) + '\n');
155 } else {
156 // Read after the proof arrives: a daemon answering the first challenge makes the key.
157 const key = readDaemonKey();
158 return finish(key !== null && daemonProofMatches(key, challenge, socketPath, message.codegraph_proof));
159 }
160 }
161 };
162 socket.setEncoding('utf8');
163 // For the socket's whole life: an 'error' with no listener would throw.
164 socket.on('error', () => finish(false));

Callers

nothing calls this directly

Calls 6

newDaemonChallengeFunction · 0.90
readDaemonKeyFunction · 0.90
daemonProofMatchesFunction · 0.90
parseMethod · 0.80
finishFunction · 0.70
writeMethod · 0.65

Tested by

no test coverage detected