UNPKG

@gameye/sdk

Version:
52 lines 1.38 kB
import * as stream from "stream"; export class SplitTransform extends stream.Duplex { constructor(separator = /\r?\n/) { super({ readableObjectMode: true, writableObjectMode: false, }); this.separator = separator; this.buffer = ""; this.flushing = false; } //#region Writable _write(chunk, encoding, callback) { this.buffer += String(chunk); this.flushBuffer(); callback(); } _final(callback) { this.flushBuffer(); callback(); } //#endregion //#region Readable _read(size) { this.flushing = true; this.flushBuffer(); } //#endregion //#region Duplex _destroy(destroyError, callback) { callback(destroyError); } //#endregion flushBuffer() { while (this.flushing) { const part = this.nextPart(); if (this.writable && part === null) { break; } this.flushing = this.push(part); } } nextPart() { const match = this.separator.exec(this.buffer); if (!match) return null; const part = this.buffer.substring(0, match.index); this.buffer = this.buffer.substring(match.index + match[0].length); return part; } } //# sourceMappingURL=split-transform.js.map