35 lines
775 B
JavaScript
35 lines
775 B
JavaScript
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; |