UNPKG

@struktoai/mirage-core

Version:

A Unified Virtual Filesystem For AI Agents

54 lines • 1.52 kB
import { BytePipe } from "./pipe.js"; import { PipeClosed } from "./errors.js"; /** Feed one consuming write through a bounded pipe; finish waits for commit. */ export class ByteWriter { pipe = new BytePipe(); task; failed = false; failure; constructor(consume) { this.task = Promise.resolve() .then(() => consume(this.pipe.stream())) .catch((error) => { this.failed = true; this.failure = error; }) .finally(() => { this.pipe.closeReader(); }); } async write(data) { try { await this.pipe.write(data); } catch (error) { if (error instanceof PipeClosed) { await this.task; if (this.failed) throw this.failure; } throw error; } } /** Wait until the consumer asks for its first bytes; one that ended first, as a refused open does, throws its error. */ async opened() { await this.pipe.asked(); if (!this.pipe.closedReader) return; await this.task; if (this.failed) throw this.failure; } async finish() { this.pipe.end(); await this.task; if (this.failed) throw this.failure; } async abort() { this.pipe.end(new Error('Write aborted')); await this.task; this.pipe.closeReader(); } } //# sourceMappingURL=writer.js.map