@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
61 lines • 2.34 kB
JavaScript
import { randomBytes, randomUUID, verify } from "node:crypto";
import { isRecord } from "../../core/extensions/builtin/mcp/modern/form-schema.js";
import { attachJsonlLineReader } from "./jsonl.js";
import { PRESENTER_EXCHANGE_MS, PRESENTER_PARKING_MS } from "./mcp-presenter-bootstrap.js";
export function signed(value, transcript, key) {
if (!isRecord(value) ||
Object.keys(value).length !== 2 ||
value.type !== "proof" ||
typeof value.signature !== "string" ||
!/^[a-f0-9]{128}$/.test(value.signature))
return false;
return verify(null, Buffer.from(transcript), key, Buffer.from(value.signature, "hex"));
}
/** The control challenge each signed command proves; a fresh nonce pair binds it to one exchange. */
export function challengeTranscript(hostInstance, backendGeneration, command) {
return JSON.stringify([
"mcp-presenter-control-v1",
hostInstance,
backendGeneration,
randomUUID(),
randomBytes(32).toString("hex"),
command,
]);
}
export function send(stream, value) {
if (!stream.write(`${JSON.stringify(value)}\n`))
stream.destroy();
}
/**
* Bound before parsing. Neither malformed bodies nor parser exceptions enter diagnostics. `"parked"`: the channel
* waits up to `PRESENTER_PARKING_MS` for its first frame, and the exchange window starts with that frame.
*/
export function privateFrames(stream, receive, deadline = true) {
let parked = deadline === "parked";
let timer = deadline === false
? undefined
: setTimeout(() => stream.destroy(), parked ? PRESENTER_PARKING_MS : PRESENTER_EXCHANGE_MS);
const detach = attachJsonlLineReader(stream, (line) => {
if (parked) {
parked = false;
clearTimeout(timer);
timer = setTimeout(() => stream.destroy(), PRESENTER_EXCHANGE_MS);
}
try {
receive(JSON.parse(line));
}
catch {
stream.destroy();
}
}, { maxLineLength: 16 * 1024, onOversizedLine: () => stream.destroy() });
stream.on("error", () => stream.destroy());
stream.once("close", () => {
clearTimeout(timer);
detach();
});
return () => {
clearTimeout(timer);
detach();
};
}
//# sourceMappingURL=mcp-presenter-wire.js.map