Files
pdtv26/speech/gate.js
T
dckc 65e38c23cf feat(speech): the speech capability over Cap'n Web, behind a gate
The agent's ears and voice: a server on the home server transcribes
a clip (sox, whisper.cpp) and speaks text (piper), one job at a time.
A gate hands the capability to whoever presents its key; the key comes
from systemd's credentials, not argv.

make test runs the unit tests in Nix's sandbox; make smoke starts a
real server on localhost, speaks a phrase, and hears it back.

Generated-by: Claude Opus 5.5
2026-12-02 15:27:50 -06:00

28 lines
940 B
JavaScript

/**
* @file The gate: hands a capability to whoever presents its key.
* A server listens with a gate, not the capability itself, so a connection gets nothing
* until it shows the key: open(key) returns the capability, and pipelines, so a client
* can open and call in one round trip.
*/
import { RpcTarget } from 'capnweb';
import { createHash, timingSafeEqual } from 'node:crypto';
const digest = (s) => createHash('sha256').update(String(s)).digest();
/**
* @param {string} key unguessable, from a credential file
* @param {object} cap what open(key) hands out
*/
export const makeGate = (key, cap) => {
const want = digest(key);
class Gate extends RpcTarget {
/** @param {string} k */
open(k) {
// Digests are the same length whatever k is, so the comparison takes the same time.
if (!timingSafeEqual(digest(k), want)) throw new Error('no such key');
return cap;
}
}
return new Gate();
};