Repository URL to install this package:
|
Version:
1.2.18 ▾
|
"use strict";
// https://github.com/JacksonTian/bufferhelper
let BufferHelper = function BufferHelper() {
this.buffers = [];
this.size = 0;
Object.defineProperty(this, 'length', {
get() {
return this.size;
}
});
};
BufferHelper.prototype.concat = function (buffer) {
this.buffers.push(buffer);
this.size += buffer.length;
return this;
};
BufferHelper.prototype.empty = function () {
this.buffers = [];
this.size = 0;
return this;
};
BufferHelper.prototype.toBuffer = function () {
return Buffer.concat(this.buffers, this.size);
};
BufferHelper.prototype.toString = function (encoding) {
return this.toBuffer().toString(encoding);
};
BufferHelper.prototype.load = function (stream, callback) {
let that = this;
stream.on('data', function (trunk) {
that.concat(trunk);
});
stream.on('end', function () {
callback(null, that.toBuffer());
});
stream.once('error', callback);
};
module.exports = BufferHelper;