Coder Social home page Coder Social logo

multibuffer-stream's Introduction

multibuffer-stream

NPM

A streaming version of multibuffer

This is useful for packaging your buffers to cross transport layers that may alter the stream frame boundaries (e.g. combine chunks or break in the middle of them) so they can be re-assembled into the original buffers.

var mbstream = require("multibuffer-stream")
var through2 = require("through2")
var spigot = require("stream-spigot")

spigot(["my", "dear", "aunt", "sally"])
  .pipe(mbstream.packStream()) // encode
  .pipe(through2(function (chunk, encoding, callback) {
    // Brutally chunk the stream into <= 3 byte chunks
    var len = chunk.length
    for (var i = 0; i < len; i += 3) {
      this.push(chunk.slice(i, i + 3))
    }
    callback()
  }))
  .pipe(mbstream.unpackStream()) // re-assemble into original buffers
  .pipe(through2(function (chunk, encoding, callback) {
    chunk[0] = chunk[0] - 32 // upper-case first character
    this.push(chunk)
    return callback()
  }))
  .pipe(process.stdout)

/*
MyDearAuntSally
 */

// Convert a stream into a multibuffer stream with `wrap`
// **NOTE** You **MUST** know the full length of the stream first!
var fs = require("fs")
var file = "./README.md"
var size = fs.statSync(file).size
fs.createReadStream(file)
  .pipe(mbstream.wrap(size))     // convert to multibuffer-stream
  .pipe(mbstream.unpackStream()) // convert back to regular stream

API

.packStream()

Create a stream.Transform instance that will convert buffers written to it into multibuffers

.unpackStream()

Create a stream.Transform instance that will re-assemble the original packed stream.

.wrap(byteLength)

Creates a Transform stream that will wrap a known length stream as a multibuffer (i.e. prefix the first chunk with the length).

This means it is not suitable for never-ending streams.

LICENSE

MIT

multibuffer-stream's People

Contributors

brycebaril avatar max-mapper avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

max-mapper

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.