Git Product home page Git Product logo

buffer-queue's Introduction

buffer-queue

Build Status Build Status

A fast buffer store, for queueing up buffer chunks to later drain as a full buffer for streams. Faster than Buffer.concat on every chunk.

Install

npm install buffer-queue

Usage

Example of a transform stream that only writes downstream 1MB at a time, storing up buffers in the buffer store.

var Transform = require("stream").Transform;
var util = require("util");
var Store = require("buffer-queue");

/**
 * Transform stream that limits writes to 1MB at a time
 */
function ThrottleStream (limit) {
  this.limit = limit || 1024 * 1024;
  this.store = new Store();
  Transform.call(this);
}
util.inherits(ThrottleStream, Transform);

ThrottleStream.prototype._transform = function (chunk, enc, callback) {
  this.store.push(chunk);
  var data;

  // While we have more in our store than the limit, then push
  // out a chunk
  while (this.store.length() > this.limit && data = this.store.shift(this.limit)) {
    this.push(data);
  }
  callback();
};

ThrottleStream.prototype._flush = function (callback) {
  var data;
  while (data = this.store.shift(this.limit)) {
    this.push(data);
  }
  callback();
};

API

BufferStore()

Constructor for a buffer store.

store.push(buffer)

Pushes a buffer to the store.

store.shift(n)

Returns the first n bytes of the entire buffer store and removes from the store.

store.empty()

Empties the entire internal buffer store.

store.length()

Returns the length of the internal buffer store in bytes.

store.drain()

Empties and returns the internal buffer store. Use store.empty() if you are not interested in the return value.

Testing

npm test

License

MIT License, Copyright (c) 2015 Jordan Santell

buffer-queue's People

Contributors

jsantell avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.