Code coverage report for streaming-cache/lib/readStream.js

Statements: 97.06% (33 / 34)      Branches: 100% (13 / 13)      Functions: 100% (5 / 5)      Lines: 97.06% (33 / 34)      Ignored: none     

All files » streaming-cache/lib/ » readStream.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58    1 1   1   1 10 10 10 10 10     1 8 8 8     1 7     1 20 1   19 8 8 8     11 11   11 9 9 9   9 9               1 12     1  
'use strict';
 
var util = require('util');
var Readable = require('stream').Readable;
 
util.inherits(ReadStream, Readable);
 
function ReadStream() {
    Readable.call(this);
    this._offset = 0;
    this.readable = false;
    this.complete = false;
    this._object = new Buffer(0);
}
 
ReadStream.prototype.setBuffer = function (_object) {
    this.complete = true;
    this._object = _object;
    this._push();
};
 
ReadStream.prototype.updateBuffer = function (_object) {
    this._object = _object;
};
 
ReadStream.prototype._push = function (size) {
    if (this.ended || !this._object.length) {
        return;
    }
    if (this.complete && this._offset === this._object.length && !this._readableState.ended) {
        this.push(null);
        this.ended = true;
        return;
    }
 
    size = size || 24 * 1024;
    size = Math.min(size, this._object.length - this._offset);
 
    if (this._offset < this._object.length) {
        var currentOffset = this._offset;
        var nextOffset = this._offset + size;
        this._offset = nextOffset;
 
        try {
            this.push(this._object.slice(currentOffset, nextOffset));
        }
        catch (err) {
            this.emit('error', err)
        }
    }
};
 
ReadStream.prototype._read = function (size) {
    this._push(size);
};
 
module.exports = ReadStream;