all files / lib/ readable_streambuffer.js

100% Statements 61/61
100% Branches 32/32
100% Functions 8/8
100% Lines 61/61
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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106      13× 13×   13×   13×   13× 13× 13× 13×   13× 13×   13× 40× 40×   40× 36× 36× 36×   36×   36× 36×     40×     40× 36×           13×           13×     13×     13×         13×               13× 41×          
'use strict';
 
var stream = require('stream');
var constants = require('./constants');
var util = require('util');
 
var ReadableStreamBuffer = module.exports = function(opts) {
  var that = this;
  opts = opts || {};
 
  stream.Readable.call(this, opts);
 
  this.stopped = false;
 
  var frequency = opts.hasOwnProperty('frequency') ? opts.frequency : constants.DEFAULT_FREQUENCY;
  var chunkSize = opts.chunkSize || constants.DEFAULT_CHUNK_SIZE;
  var initialSize = opts.initialSize || constants.DEFAULT_INITIAL_SIZE;
  var incrementAmount = opts.incrementAmount || constants.DEFAULT_INCREMENT_AMOUNT;
 
  var size = 0;
  var buffer = new Buffer(initialSize);
 
  var sendData = function() {
    var amount = Math.min(chunkSize, size);
    var sendMore = false;
 
    if (amount > 0) {
      var chunk = null;
      chunk = new Buffer(amount);
      buffer.copy(chunk, 0, 0, amount);
 
      sendMore = that.push(chunk) !== false;
 
      buffer.copy(buffer, 0, amount, size);
      size -= amount;
    }
 
    if(size === 0 && that.stopped) {
      that.push(null);
    }
 
    if (sendMore) {
      sendData.timeout = setTimeout(sendData, frequency);
    }
    else {
      sendData.timeout = null;
    }
  };
 
  this.stop = function() {
    if (this.stopped) {
      throw new Error('stop() called on already stopped ReadableStreamBuffer');
    }
    this.stopped = true;
 
    if (size === 0) {
      this.push(null);
    }
  };
 
  this.size = function() {
    return size;
  };
 
  this.maxSize = function() {
    return buffer.length;
  };
 
  var increaseBufferIfNecessary = function(incomingDataSize) {
    if((buffer.length - size) < incomingDataSize) {
      var factor = Math.ceil((incomingDataSize - (buffer.length - size)) / incrementAmount);
 
      var newBuffer = new Buffer(buffer.length + (incrementAmount * factor));
      buffer.copy(newBuffer, 0, 0, size);
      buffer = newBuffer;
    }
  };
 
  this.put = function(data, encoding) {
    if (that.stopped) {
      throw new Error('Tried to write data to a stopped ReadableStreamBuffer');
    }
 
    if(Buffer.isBuffer(data)) {
      increaseBufferIfNecessary(data.length);
      data.copy(buffer, size, 0);
      size += data.length;
    }
    else {
      data = data + '';
      var dataSizeInBytes = Buffer.byteLength(data);
      increaseBufferIfNecessary(dataSizeInBytes);
      buffer.write(data, size, encoding || 'utf8');
      size += dataSizeInBytes;
    }
  };
 
  this._read = function() {
    if (!sendData.timeout) {
      sendData.timeout = setTimeout(sendData, frequency);
    }
  };
};
 
util.inherits(ReadableStreamBuffer, stream.Readable);