'use strict';
var util = require('util');
var Readable = require('stream').Readable;
util.inherits(PendingReadStream, Readable);
function PendingReadStream(buffer) {
Readable.call(this);
this._buffer = buffer;
this.unfullfilledReadCount = 0;
this._offset = 0;
this.readOffset = 0;
this._ended = false;
}
PendingReadStream.prototype._read = function _read() {
// console.log('_read', this.readOffset , this._buffer.length);
if (this.readOffset <= this._buffer.length) {
var chunk = this._buffer[this.readOffset];
this.readOffset++;
this.push(chunk);
this.unfullfilledReadCount = (this.unfullfilledReadCount > 0) ? this.unfullfilledReadCount - 1 : this.unfullfilledReadCount;
} else {
this.unfullfilledReadCount = this.unfullfilledReadCount + 1;
}
Iif (this.readOffset === this._buffer.length && this._ended) {
this.push(null);
}
};
PendingReadStream.prototype.update = function update() {
Iif (this.unfullfilledReadCount > 0) {
if (this.readOffset < this._buffer.length) {
var chunk = this._buffer[this.readOffset ];
this.readOffset++;
this.push(chunk);
this.unfullfilledReadCount = this.unfullfilledReadCount - 1;
}
}
};
PendingReadStream.prototype._end = function end() {
if (this.unfullfilledReadCount === 0) {
this.push(null);
}
this._ended = true;
};
module.exports = PendingReadStream;
|