You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
775 B

const stream = require('stream')
const util = require('util')
const Transform = stream.Transform
function Take(options) {
// allow use without new
if (!(this instanceof Take)) {
return new Take(options);
}
this._toTake = options.take || undefined
// init Transform
Transform.call(this, options);
}
util.inherits(Take, Transform);
Take.prototype._transform = function (chunk, enc, cb) {
if ( typeof this._toTake == 'undefined' ) {
this.push(chunk)
}
else if (this._toTake > chunk.length) {
this._toTake -= chunk.length;
this.push(chunk)
} else {
if (this._toTake !== chunk.length) this.push(chunk.slice(0, this._toTake))
this._toTake = 0;
}
cb();
};
module.exports = Take;