UNPKG

multi-readable

Version:

Serial connecting readable streams, strictly following the back pressure and highWaterMark

176 lines (175 loc) 7.44 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __asyncValues = (this && this.__asyncValues) || function (o) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var m = o[Symbol.asyncIterator], i; return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i); function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; } function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.MultiObjectReadable = exports.MultiBufferReadable = void 0; const stream_1 = require("stream"); class BaseMultiReadable extends stream_1.Readable { constructor(streams, opts) { super(opts); const readableObjectMode = !!(opts === null || opts === void 0 ? void 0 : opts.objectMode); const isReadable = stream_1.Readable.isReadable ? stream_1.Readable.isReadable : (stream) => stream instanceof stream_1.Readable; if (Array.isArray(streams) && streams.every((stream) => { if (!isReadable(stream)) { return false; } if (stream.readableObjectMode !== readableObjectMode) { return false; } return true; })) { this.__streams = streams; this.__start().catch((reason) => { this.destroy(reason); }); } else { throw new Error("arguments type error"); } } _read(size) { var _a; (_a = this.__read) === null || _a === void 0 ? void 0 : _a.call(this, size); } _destroy(error, callback) { var _a; for (const stream of this.__streams) { if (!stream.destroyed) { stream.destroy(); } } (_a = this.__read) === null || _a === void 0 ? void 0 : _a.call(this, -1); callback(error); } __get_size() { return __awaiter(this, void 0, void 0, function* () { try { return yield new Promise((resolve) => { this.__read = resolve; }); } finally { this.__read = null; } }); } } class MultiBufferReadable extends BaseMultiReadable { constructor(streams, opts) { super(streams, Object.assign(Object.assign({}, opts), { objectMode: false })); } __start() { var e_1, _a; return __awaiter(this, void 0, void 0, function* () { let size = 0; let size_promise = this.__get_size(); let cache; const clear = () => { size = 0; size_promise = this.__get_size(); cache = null; }; for (const stream of this.__streams) { try { for (var stream_2 = (e_1 = void 0, __asyncValues(stream)), stream_2_1; stream_2_1 = yield stream_2.next(), !stream_2_1.done;) { const chunk = stream_2_1.value; if (!Buffer.isBuffer(chunk)) { throw new Error("chunk is not a buffer"); } if (size === 0) { size = yield size_promise; } cache = cache ? Buffer.concat([cache, chunk]) : chunk; let cache_len = cache.length; if (cache_len > size) { do { this.push(cache.slice(0, size)); cache = cache.slice(size); cache_len = cache.length; size = yield this.__get_size(); if (cache_len < size) { break; } else if (cache_len === size) { this.push(cache); clear(); break; } } while (true); } else if (cache_len === size) { this.push(cache); clear(); } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (stream_2_1 && !stream_2_1.done && (_a = stream_2.return)) yield _a.call(stream_2); } finally { if (e_1) throw e_1.error; } } } if (cache) { this.push(cache); cache = null; } this.push(null); }); } } exports.MultiBufferReadable = MultiBufferReadable; class MultiObjectReadable extends BaseMultiReadable { constructor(streams, opts) { super(streams, Object.assign(Object.assign({}, opts), { objectMode: true })); } __start() { var e_2, _a; return __awaiter(this, void 0, void 0, function* () { let size = 0; let size_promise = this.__get_size(); for (const stream of this.__streams) { try { for (var stream_3 = (e_2 = void 0, __asyncValues(stream)), stream_3_1; stream_3_1 = yield stream_3.next(), !stream_3_1.done;) { const object = stream_3_1.value; if (size === 0) { size = yield size_promise; } const r = this.push(object); if (!r) { size = 0; size_promise = this.__get_size(); } } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (stream_3_1 && !stream_3_1.done && (_a = stream_3.return)) yield _a.call(stream_3); } finally { if (e_2) throw e_2.error; } } } this.push(null); }); } } exports.MultiObjectReadable = MultiObjectReadable;