Sha256: c2862db8470626836b41d498a1eb1d07c8fa0ef8a11e69d06d888825f58c997d

Contents?: true

Size: 1.57 KB

Versions: 36

Compression:

Stored size: 1.57 KB

Contents

'use strict';

var Buffer = require('safe-buffer').Buffer;

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

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

StreamReader.prototype.read = function(length) {
  if (length > this._queueSize) return null;
  if (length === 0) return Buffer.alloc(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 Buffer.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();
  }
};

module.exports = StreamReader;

Version data entries

36 entries across 28 versions & 9 rubygems

Version Path
optimacms-0.1.61 spec/dummy/node_modules/websocket-driver/lib/websocket/driver/stream_reader.js
disco_app-0.18.0 test/dummy/node_modules/websocket-driver/lib/websocket/driver/stream_reader.js
disco_app-0.18.2 test/dummy/node_modules/websocket-driver/lib/websocket/driver/stream_reader.js
disco_app-0.16.1 test/dummy/node_modules/faye-websocket/node_modules/websocket-driver/lib/websocket/driver/stream_reader.js
disco_app-0.16.1 test/dummy/node_modules/websocket-driver/lib/websocket/driver/stream_reader.js
disco_app-0.15.2 test/dummy/node_modules/websocket-driver/lib/websocket/driver/stream_reader.js
disco_app-0.15.2 test/dummy/node_modules/faye-websocket/node_modules/websocket-driver/lib/websocket/driver/stream_reader.js
disco_app-0.18.4 test/dummy/node_modules/websocket-driver/lib/websocket/driver/stream_reader.js
disco_app-0.18.4 test/dummy/node_modules/faye-websocket/node_modules/websocket-driver/lib/websocket/driver/stream_reader.js
disco_app-0.18.1 test/dummy/node_modules/faye-websocket/node_modules/websocket-driver/lib/websocket/driver/stream_reader.js
disco_app-0.18.1 test/dummy/node_modules/websocket-driver/lib/websocket/driver/stream_reader.js
disco_app-0.12.7.pre.puma.pre.3 test/dummy/node_modules/websocket-driver/lib/websocket/driver/stream_reader.js
disco_app-0.12.7.pre.puma.pre.3 test/dummy/node_modules/faye-websocket/node_modules/websocket-driver/lib/websocket/driver/stream_reader.js
disco_app-0.14.0 test/dummy/node_modules/faye-websocket/node_modules/websocket-driver/lib/websocket/driver/stream_reader.js
disco_app-0.14.0 test/dummy/node_modules/websocket-driver/lib/websocket/driver/stream_reader.js
disco_app-0.13.6.pre.puma.pre.3 test/dummy/node_modules/faye-websocket/node_modules/websocket-driver/lib/websocket/driver/stream_reader.js
disco_app-0.13.6.pre.puma.pre.3 test/dummy/node_modules/websocket-driver/lib/websocket/driver/stream_reader.js
tang-0.2.1 spec/tang_app/node_modules/websocket-driver/lib/websocket/driver/stream_reader.js
groonga-client-model-6.0.0 test/apps/rails6.0.3.5/node_modules/websocket-driver/lib/websocket/driver/stream_reader.js
groonga-client-model-6.0.0 test/apps/rails6.1.3/node_modules/websocket-driver/lib/websocket/driver/stream_reader.js