@struktoai/mirage-core
Version:
A Unified Virtual Filesystem For AI Agents
72 lines • 1.99 kB
JavaScript
import { PipeClosed } from "../shell/errors.js";
import { materialize } from "../io/types.js";
/** Streams belong to one consumer; communicate drains both outputs concurrently. */
export class ChildProcess {
process;
stdin;
output;
cancel;
stdout;
stderr;
constructor(process, stdin, output, cancel) {
this.process = process;
this.stdin = stdin;
this.output = output;
this.cancel = cancel;
void process.join().then(() => {
stdin.stop();
output.end();
});
this.stdout = output.stdout.stream();
this.stderr = output.stderr.stream();
}
get pid() {
return this.process.info.pid;
}
closeOutput(stream) {
this.output[stream].stop();
}
poll() {
return this.process.info.exitCode;
}
/**
* Stop the child with `signal`; its status is 128 plus it.
*
* @param signal what the stop stands for, KILL (9) unless the caller
* names another (`Popen.terminate` sends TERM).
*/
terminate(signal = 9) {
this.cancel(signal);
}
wait() {
return this.process.join();
}
async communicate(data = new Uint8Array()) {
const feed = async () => {
try {
await this.stdin.write(data);
}
catch (error) {
if (!(error instanceof PipeClosed))
throw error;
}
finally {
this.stdin.close();
}
};
try {
const [, stdout, stderr, info] = await Promise.all([
feed(),
materialize(this.stdout),
materialize(this.stderr),
this.wait(),
]);
return { stdout, stderr, exitCode: info.exitCode ?? 1 };
}
catch (error) {
this.terminate();
throw error;
}
}
}
//# sourceMappingURL=child.js.map