UNPKG

memstreams

Version:

Memory Streams library for JavaScript based on readable-stream

46 lines 1.37 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BufferWriter = void 0; const readable_stream_1 = require("readable-stream"); const utils_1 = require("./utils"); class BufferWriter extends readable_stream_1.Writable { constructor(options = {}) { options.objectMode = false; super(options); this.init(); } init() { this._queue = []; } get queue() { return this._queue; } get data() { if (!this._data || this._data.length < this._queue.reduce((pre, curr) => pre + curr.length, 0)) { this._data = Buffer.concat(this._queue); } return this._data; } _write(chunk, encoding, callback) { const data = (0, utils_1.chunk2buf)(chunk, encoding); if (this.listenerCount('write')) { this.emit('write', data); } else { this._queue.push(data); } callback(); } forward(destination, options) { const forward = (data) => destination.push(data); this.on('write', forward); if (!options || options.end) { this.once('finish', () => this.removeListener('data', forward)); } return destination; } } exports.BufferWriter = BufferWriter; //# sourceMappingURL=buffer-writer.js.map