UNPKG

ranged-stream

Version:

node-ranged-stream ==================

42 lines (35 loc) 1.08 kB
var Transform = require('stream').Transform; var util = require('util'); function RangeStream(opt) { Transform.call(this, opt); this.data = ''; this._start = opt.start || 0; this._end = (opt.end != undefined ) ? opt.end : -1; this._current = 0; }; util.inherits(RangeStream, Transform); RangeStream.prototype._transform = function(data, encoding, callback) { if (this._end > -1 && this._current > this._end ) { //should we emit end ? callback(); return; } else if (this._start > this._current) { if ( this._start > this._current + data.length) { this._current += data.length; callback(); return; } data = data.slice( this._start - this._current ); this._current += this._start; } if (this._end > -1 && (this._current + data.length) >= this._end) { data = data.slice(0, this._end - this._current +1 ) ; // _end is index, not length } this._current += data.length; this.push(data); callback(); } RangeStream.prototype._flush = function(callback) { callback(); } module.exports = RangeStream;