Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
/**
* MsgHeaderStream is a `Writable` that concatenates strings or `Buffer`
* and invokes a callback with an `Buffer` prepended by its length.
*
* @param {object} opts options for determining how the data is written
* @param {function} callback is invoked when all data has been written
* @constructor
*/
function MsgHeaderStream(opts, callback) {
// Initializes the base class with the buffer
this.opts = opts || {};
ConcatStream.call(this, { encoding: 'buffer' }, callback);
this.size = 0;
}
MsgHeaderStream.prototype = Object.create(
ConcatStream.prototype, {constructor: {value: MsgHeaderStream }});
/**
* Overrides ConcatStream._write to count the size of data in bytes.
*/
MsgHeaderStream.prototype._write = function(chunk, enc, next) {
this.body.push(chunk);
this.size += chunk.length;
next();
};
/**
* Overrides `ConcatStream.getBody` to return the body prefixed with the
* size.
*
* @returns {external:Buffer} containing the data written to this stream
* prefixed with its length