UNPKG

streaming

Version:

Transforms and other streaming helpers

58 lines (56 loc) 2.11 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.Mapper = void 0; var stream_1 = require("stream"); var __null = { toJSON: function () { return null; }, valueOf: function () { return null; }, }; /** Mapper transforms a stream of <T> into a stream of <R>, using a synchronous transform function. Use Transformer() instead if you need an asynchronous callback function. */ var Mapper = /** @class */ (function (_super) { __extends(Mapper, _super); function Mapper(transformFn) { var _this = _super.call(this, { objectMode: true }) || this; _this.transformFn = transformFn; _this.transformFn = transformFn.bind(_this); return _this; } Mapper.prototype._transform = function (chunk, encoding, callback) { var result = this.transformFn(chunk); if (result === null) { // We have to wrap pure JS nulls, because `push(null)` means EOF to streams. // It's kind of a hack, but it works nicely when we JSON.stringify downstream. this.push(__null); } else if (result !== undefined) { this.push(result); } // otherwise skip it; undefined denotes no output callback(); }; return Mapper; }(stream_1.Transform)); exports.Mapper = Mapper;