summaryrefslogtreecommitdiff
path: root/node_modules/gather-stream/index.js
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/gather-stream/index.js')
-rw-r--r--node_modules/gather-stream/index.js44
1 files changed, 44 insertions, 0 deletions
diff --git a/node_modules/gather-stream/index.js b/node_modules/gather-stream/index.js
new file mode 100644
index 00000000..cab8bda9
--- /dev/null
+++ b/node_modules/gather-stream/index.js
@@ -0,0 +1,44 @@
+var Writable = require('stream').Writable,
+ util = require('util');
+
+function Gatherer(opts) {
+ opts.decodeStrings = true;
+ Writable.call(this, opts);
+ this._length = 0;
+ this._chunks = [];
+ this._maxLength = ('maxLength' in opts) ? opts.maxLength : Infinity;
+}
+
+util.inherits(Gatherer, Writable);
+
+Gatherer.prototype._write = function (d, _enc, cb) {
+ if (this._length + d.length > this._maxLength) {
+ return cb(new Error("Maximum length exceeded!"));
+ }
+ this._chunks.push(d);
+ this._length += d.length;
+ cb();
+}
+
+Gatherer.prototype.getBuffer = function () {
+ return Buffer.concat(this._chunks, this._length);
+}
+
+module.exports = function (opts, cb) {
+ if (typeof opts === 'function') {
+ cb = opts;
+ opts = {};
+ }
+ var target = new Gatherer(opts);
+ target.on('error', cb);
+ target.on('pipe', function (source) {
+ source.on('error', cb);
+ });
+ target.on('unpipe', function (source) {
+ source.removeListener('error', cb);
+ });
+ target.on('finish', function () {
+ cb(null, target.getBuffer());
+ });
+ return target;
+}