UNPKG

stromjs

Version:

Dependency-free streams utils for Node.js

111 lines (110 loc) 4.25 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; exports.__esModule = true; exports.Compose = exports.compose = void 0; var helpers_1 = require("../helpers"); var stream_1 = require("stream"); function compose(streams, errorCallback, options) { if (streams.length < 2) { throw new Error("At least two streams are required to compose"); } return new Compose(streams, errorCallback, options); } exports.compose = compose; var Compose = /** @class */ (function (_super) { __extends(Compose, _super); function Compose(streams, errorCallback, options) { var _this = _super.call(this, options) || this; _this.first = new stream_1.PassThrough(options); var last = streams.pop(); if (last && (0, helpers_1.isReadable)(last)) { _this.last = last; } else { throw new TypeError("Invalid last stream provided, it must be readable"); } _this.streams = streams; (0, stream_1.pipeline)(__spreadArray(__spreadArray([_this.first], streams, true), [_this.last], false), errorCallback || (function (error) { if (error) { _this.emit("error", error); } })); if ((0, helpers_1.isReadable)(_this.last)) { _this.last.pipe(new stream_1.Transform(__assign(__assign({}, options), { transform: function (d, _encoding, cb) { _this.push(d); cb(); } }))); } return _this; } Compose.prototype._transform = function (chunk, encoding, cb) { this.first.write(chunk, encoding, cb); }; Compose.prototype._flush = function (cb) { if ((0, helpers_1.isWritable)(this.first)) { this.first.push(null); } this.last.once("end", function () { cb(); }); }; Compose.prototype._destroy = function (error, cb) { this.streams.forEach(function (s) { return s.destroy(); }); cb(error); }; Compose.prototype.bubble = function () { var _this = this; var events = []; for (var _i = 0; _i < arguments.length; _i++) { events[_i] = arguments[_i]; } this.streams.forEach(function (s) { events.forEach(function (e) { s.on(e, function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } return _super.prototype.emit.apply(_this, __spreadArray([e], args, false)); }); }); }); }; return Compose; }(stream_1.Transform)); exports.Compose = Compose;