UNPKG

@gameye/sdk

Version:
55 lines 1.49 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const stream = require("stream"); 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; } } exports.SplitTransform = SplitTransform; //# sourceMappingURL=split-transform.js.map