'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(); }); }