You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
28 lines
612 B
JavaScript
28 lines
612 B
JavaScript
'use strict';
|
|
|
|
const bl = require("bl");
|
|
const through2 = require("through2");
|
|
const debug = require("debug")("through2-buffer");
|
|
|
|
module.exports = function(bufferSize) {
|
|
let buff = bl();
|
|
let flowing = false;
|
|
|
|
return through2(function(chunk, encoding, cb) {
|
|
if (flowing) {
|
|
this.push(chunk);
|
|
} else {
|
|
debug(`Buffering chunk of size ${chunk.length}`);
|
|
buff.append(chunk);
|
|
debug(`Buffer length now ${buff.length}`);
|
|
|
|
if (buff.length >= bufferSize) {
|
|
debug("Buffer limit reached; setting to flowing mode");
|
|
flowing = true;
|
|
this.push(buff.read(buff.length));
|
|
}
|
|
}
|
|
|
|
cb();
|
|
});
|
|
} |