import { FanoutWriteStream, Future, type TxtEnDecoder, TxtEnDecoderSingleton } from "@adviser/cement"; export class LogWriteStream implements WritableStreamDefaultWriter { private readonly _bufferArr: Uint8Array[]; constructor(bufferArr: Uint8Array[]) { this._bufferArr = bufferArr; } readonly _resolveClosed: Future = new Future(); readonly closed: Promise = this._resolveClosed.asPromise(); readonly desiredSize: number | null = null; readonly ready: Promise = Promise.resolve() as Promise; // eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/no-explicit-any abort(reason?: any): Promise { throw new Error("Method not implemented."); } async close(): Promise { await this.closed; return Promise.resolve(); } releaseLock(): void { // do nothing } async write(chunk?: Uint8Array): Promise { if (chunk) { this._bufferArr.push(chunk); } return Promise.resolve(); } } export class LogCollector implements WritableStream { readonly locked: boolean = false; private _writer?: FanoutWriteStream; private readonly _pass?: WritableStreamDefaultWriter; private readonly _bufferArr: Uint8Array[] = []; private readonly _txtEnDe: TxtEnDecoder; constructor(pass?: WritableStreamDefaultWriter, txtEnDe?: TxtEnDecoder) { this._pass = pass; this._txtEnDe = txtEnDe || TxtEnDecoderSingleton(); } // eslint-disable-next-line @typescript-eslint/no-unused-vars abort(reason?: Uint8Array): Promise { throw new Error("Method not implemented."); } async close(): Promise { if (this._writer) { const ret = await this._writer.close(); this._writer = undefined; return ret; } return Promise.resolve(); } getWriter(): WritableStreamDefaultWriter { if (!this._writer) { const dests: WritableStreamDefaultWriter[] = [new LogWriteStream(this._bufferArr)]; if (this._pass) { dests.push(this._pass); } this._writer = new FanoutWriteStream(dests); } return this._writer; } // eslint-disable-next-line @typescript-eslint/no-explicit-any Logs(notJsonLine = false): any[] { if (!this._writer) { return []; } const jsonNlStr = this._txtEnDe.decode( new Uint8Array( (function* (res: Uint8Array[]): Generator { for (const x of res) { yield* x; } })(this._bufferArr), ), ); if (!notJsonLine) { const splitStr = jsonNlStr.split("\n"); const filterStr = splitStr.filter((a) => a.length); // eslint-disable-next-line @typescript-eslint/no-unsafe-return const mapStr = filterStr.map((a) => JSON.parse(a)); return mapStr; } return jsonNlStr.split("\n").filter((a) => a.length); } }