Sha256: 6b64bbc9ba8c2c74dd0623e838a4e980bc01c8ea279e20c7ce7a069153476d03

Contents?: true

Size: 1.82 KB

Versions: 13

Compression:

Stored size: 1.82 KB

Contents

'use strict';

var StreamReader = function() {
  this._queue     = [];
  this._queueSize = 0;
  this._offset    = 0;
};

StreamReader.prototype.put = function(buffer) {
  if (!buffer || buffer.length === 0) return;
  if (!buffer.copy) buffer = new Buffer(buffer);
  this._queue.push(buffer);
  this._queueSize += buffer.length;
};

StreamReader.prototype.read = function(length) {
  if (length > this._queueSize) return null;
  if (length === 0) return new Buffer(0);

  this._queueSize -= length;

  var queue  = this._queue,
      remain = length,
      first  = queue[0],
      buffers, buffer;

  if (first.length >= length) {
    if (first.length === length) {
      return queue.shift();
    } else {
      buffer = first.slice(0, length);
      queue[0] = first.slice(length);
      return buffer;
    }
  }

  for (var i = 0, n = queue.length; i < n; i++) {
    if (remain < queue[i].length) break;
    remain -= queue[i].length;
  }
  buffers = queue.splice(0, i);

  if (remain > 0 && queue.length > 0) {
    buffers.push(queue[0].slice(0, remain));
    queue[0] = queue[0].slice(remain);
  }
  return this._concat(buffers, length);
};

StreamReader.prototype.eachByte = function(callback, context) {
  var buffer, n, index;

  while (this._queue.length > 0) {
    buffer = this._queue[0];
    n = buffer.length;

    while (this._offset < n) {
      index = this._offset;
      this._offset += 1;
      callback.call(context, buffer[index]);
    }
    this._offset = 0;
    this._queue.shift();
  }
};

StreamReader.prototype._concat = function(buffers, length) {
  if (Buffer.concat) return Buffer.concat(buffers, length);

  var buffer = new Buffer(length),
      offset = 0;

  for (var i = 0, n = buffers.length; i < n; i++) {
    buffers[i].copy(buffer, offset);
    offset += buffers[i].length;
  }
  return buffer;
};

module.exports = StreamReader;

Version data entries

13 entries across 13 versions & 7 rubygems

Version Path
tang-0.2.1 spec/tang_app/node_modules/sockjs/node_modules/websocket-driver/lib/websocket/driver/stream_reader.js
tang-0.2.0 spec/tang_app/node_modules/sockjs/node_modules/websocket-driver/lib/websocket/driver/stream_reader.js
tang-0.1.0 spec/tang_app/node_modules/sockjs/node_modules/websocket-driver/lib/websocket/driver/stream_reader.js
tang-0.0.9 spec/tang_app/node_modules/sockjs/node_modules/websocket-driver/lib/websocket/driver/stream_reader.js
blsk-sevcore-0.0.2 blsk_old/bluesky-doc/bluesky-doc/node_modules/websocket-driver/lib/websocket/driver/stream_reader.js
optimacms-0.4.3 spec/dummy/node_modules/websocket-driver/lib/websocket/driver/stream_reader.js
optimacms-0.4.2 spec/dummy/node_modules/websocket-driver/lib/websocket/driver/stream_reader.js
cortex-0.1.3 spec/dummy/node_modules/websocket-driver/lib/websocket/driver/stream_reader.js
dragonfly_puppeteer-0.1.0 node_modules/websocket-driver/lib/websocket/driver/stream_reader.js
lanes-0.8.0 node_modules/websocket-driver/lib/websocket/driver/stream_reader.js
gulp_assets-1.0.0.pre.5 template/node_modules/gulp-livereload/node_modules/tiny-lr/node_modules/faye-websocket/node_modules/websocket-driver/lib/websocket/driver/stream_reader.js
gulp_assets-1.0.0.pre.4 template/node_modules/gulp-livereload/node_modules/tiny-lr/node_modules/faye-websocket/node_modules/websocket-driver/lib/websocket/driver/stream_reader.js
gulp_assets-1.0.0.pre.3 template/node_modules/gulp-livereload/node_modules/tiny-lr/node_modules/faye-websocket/node_modules/websocket-driver/lib/websocket/driver/stream_reader.js