A tester binds with a process proof from the bus's own tester-cli.mjs when the business var bus.tester is true. It may send [mos test] messages to a role, receive replies on the tester address and use the read verbs; every other verb refuses. Messages store from_role tester and write no human.input event. turnRequest() marks them as never Jason's instruction. human.mjs is unchanged. Candidate manifest bdaf7f476abd0b2b5095ab2f7f43e38540e6ba48ee14a02a4f4995a1469b79df (Darkwing round 2, packet9f1563ec1), BASEbadfa52b9. Approved by Filbert (comment 28360) and Sage (comment 28361). Sage gate rerun passed. Co-Authored-By: Claude Opus 5.5 <[email protected]>
50 lines
1.8 KiB
JavaScript
50 lines
1.8 KiB
JavaScript
import { connect } from 'node:net';
|
|
import { BusError } from './broker.mjs';
|
|
export class Client {
|
|
constructor({ path, cap, human, tester, timeout = 5000 }) {
|
|
this.path = path;
|
|
this.cap = cap;
|
|
this.human = human;
|
|
this.tester = tester;
|
|
this.timeout = timeout;
|
|
}
|
|
call(verb, args = {}) {
|
|
const request =
|
|
JSON.stringify({
|
|
...(this.human ? { human: this.human } : this.tester ? { tester: this.tester } : { cap: this.cap }),
|
|
verb,
|
|
args,
|
|
}) + '\n';
|
|
if (Buffer.byteLength(request) > 65536) return Promise.reject(new BusError('request-too-large'));
|
|
return new Promise((resolve, reject) => {
|
|
const socket = connect(this.path);
|
|
let input = Buffer.alloc(0),
|
|
settled = false;
|
|
const end = (error, value) => {
|
|
if (settled) return;
|
|
settled = true;
|
|
socket.destroy();
|
|
error ? reject(error) : resolve(value);
|
|
};
|
|
socket.setTimeout(this.timeout, () => end(new BusError('outcome-unknown')));
|
|
socket.on('connect', () => socket.write(request));
|
|
socket.on('error', () => end(new BusError('outcome-unknown')));
|
|
socket.on('end', () => end(new BusError('outcome-unknown')));
|
|
socket.on('data', (b) => {
|
|
input = Buffer.concat([input, b]);
|
|
if (input.length > 4 * 1024 * 1024) return end(new BusError('response-too-large'));
|
|
if (!input.includes(10)) return;
|
|
try {
|
|
const r = JSON.parse(input.toString('utf8'));
|
|
if (r.ok === true) end(null, r.result);
|
|
else if (r.ok === false && typeof r.error === 'string' && /^[a-z-]{1,64}$/.test(r.error))
|
|
end(new BusError(r.error));
|
|
else end(new BusError('invalid-response'));
|
|
} catch {
|
|
end(new BusError('invalid-response'));
|
|
}
|
|
});
|
|
});
|
|
}
|
|
}
|