UNPKG

streaming

Version:

Transforms and other streaming helpers

54 lines (53 loc) 2.34 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 __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.Timeout = void 0; var stream_1 = require("stream"); /** Timeout is mostly a PassThrough (identity) stream, but will throw an error if a period of `timeoutMilliseconds` elapses without seeing any new data. */ var Timeout = /** @class */ (function (_super) { __extends(Timeout, _super); function Timeout(timeoutSeconds, options) { var _this = _super.call(this, options) || this; // if (timeoutSeconds === undefined) { // throw new Error('new Timeout(timeoutSeconds): timeoutSeconds is a required parameter.'); // } _this.timeoutMilliseconds = timeoutSeconds * 1000; setInterval(function () { return _this._check(); }, _this.timeoutMilliseconds); return _this; } Timeout.prototype._check = function () { // millisecondsSinceLast: milliseconds since we last saw some incoming data var millisecondsSinceLast = (new Date()).getTime() - this.lastChunkReceived; if (millisecondsSinceLast > this.timeoutMilliseconds) { this.emit('error', new Error("Timed out: ".concat(millisecondsSinceLast, "ms"))); } }; Timeout.prototype._transform = function (chunk, encoding, callback) { this.lastChunkReceived = (new Date()).getTime(); this.push(chunk); callback(); }; Timeout.prototype._flush = function (callback) { // don't clear the interval callback(); }; return Timeout; }(stream_1.Transform)); exports.Timeout = Timeout;