stromjs
Version:
Dependency-free streams utils for Node.js
144 lines (143 loc) • 5 kB
JavaScript
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.accumulatorBy = exports.accumulator = exports.FlushStrategy = void 0;
var stream_1 = require("stream");
var FlushStrategy;
(function (FlushStrategy) {
FlushStrategy["rolling"] = "rolling";
FlushStrategy["sliding"] = "sliding";
})(FlushStrategy = exports.FlushStrategy || (exports.FlushStrategy = {}));
function _accumulator(accumulateBy, shouldFlush, options) {
if (shouldFlush === void 0) { shouldFlush = true; }
if (options === void 0) { options = {}; }
var buffer = [];
return new stream_1.Transform(__assign(__assign({}, options), { transform: function (data, encoding, callback) {
try {
accumulateBy(data, buffer, this);
callback();
}
catch (err) {
callback(err);
}
}, flush: function (callback) {
if (shouldFlush) {
this.push(buffer);
}
callback();
} }));
}
function _sliding(windowLength, key) {
return function (event, buffer, stream) {
if (key) {
var index = 0;
if (event[key] === undefined) {
stream.emit("error", new Error("Key is missing in event: (".concat(key, ", ").concat(JSON.stringify(event), ")")));
stream.resume();
return;
}
while (index < buffer.length &&
buffer[index][key] + windowLength <= event[key]) {
index++;
}
buffer.splice(0, index);
}
else if (buffer.length === windowLength) {
buffer.shift();
}
buffer.push(event);
stream.push(__spreadArray([], buffer, true));
};
}
function _slidingByFunction(iteratee) {
return function (event, buffer, stream) {
var index = 0;
while (index < buffer.length && iteratee(event, buffer[index])) {
index++;
}
buffer.splice(0, index);
buffer.push(event);
stream.push(__spreadArray([], buffer, true));
};
}
function _rollingByFunction(iteratee) {
return function (event, buffer, stream) {
if (iteratee) {
if (buffer.length > 0 && iteratee(event, buffer[0])) {
stream.push(buffer.slice(0));
buffer.length = 0;
}
}
buffer.push(event);
};
}
function _rolling(windowLength, key) {
return function (event, buffer, stream) {
if (key) {
if (event[key] === undefined) {
stream.emit("error", new Error("Key is missing in event: (".concat(key, ", ").concat(JSON.stringify(event), ")")));
stream.resume();
return;
}
else if (buffer.length > 0 &&
buffer[0][key] + windowLength <= event[key]) {
stream.push(buffer.slice(0));
buffer.length = 0;
}
}
else if (buffer.length === windowLength) {
stream.push(buffer.slice(0));
buffer.length = 0;
}
buffer.push(event);
};
}
function accumulator(flushStrategy, batchSize, keyBy, options) {
switch (flushStrategy) {
case FlushStrategy.sliding:
return sliding(batchSize, keyBy, options);
case FlushStrategy.rolling:
return rolling(batchSize, keyBy, options);
}
}
exports.accumulator = accumulator;
function accumulatorBy(flushStrategy, iteratee, options) {
switch (flushStrategy) {
case FlushStrategy.sliding:
return slidingBy(iteratee, options);
case FlushStrategy.rolling:
return rollingBy(iteratee, options);
}
}
exports.accumulatorBy = accumulatorBy;
function sliding(windowLength, key, options) {
return _accumulator(_sliding(windowLength, key), false, options);
}
function slidingBy(iteratee, options) {
return _accumulator(_slidingByFunction(iteratee), false, options);
}
function rolling(windowLength, key, options) {
return _accumulator(_rolling(windowLength, key), true, options);
}
function rollingBy(iteratee, options) {
return _accumulator(_rollingByFunction(iteratee), true, options);
}
;