forked from jgthms/css-reference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
44 lines (39 loc) · 1.04 KB
/
Copy pathindex.js
File metadata and controls
44 lines (39 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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;
}