Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

bufferstreams

nfroidure1.7mMIT4.0.0TypeScript support: included

Abstract streams to deal with the whole buffered contents.

buffer, streaming, stream, async, abstract

readme

bufferstreams

Abstract streams to deal with the whole buffered contents.

GitHub license Coverage Status

bufferstreams abstracts streams to allow you to deal with their whole content in a single buffer when it becomes necessary (by example: a legacy library that do not support streams).

It is not a good practice (dealing with the whole stream content means you need to keep the whole stream content in memory which is probably not what you intent by using streams), just some glue. Using bufferstreams means:

  • there is no library dealing with streams for your needs
  • you filled an issue to the wrapped library to support streams

bufferstreams can also be used to control the whole stream content in a single point of a streaming pipeline for testing purposes.

Usage

Install the npm module:

npm install bufferstreams --save

Then, in your scripts:

import fs from 'fs';
import { BufferStream } from 'bufferstreams';

fs.createReadStream('input.txt')
  .pipe(
    new BufferStream((err, buf, cb) => {
      // err will be filled with an error if the piped in stream emits one.
      if (err) {
        throw err;
      }

      // buf will contain the whole piped in stream contents
      buf = Buffer.from(buf.toString('utf-8').replace('foo', 'bar'));

      // cb is a callback to pass the result back to the piped out stream
      // first argument is an error that will be emitted if any
      // the second argument is the modified buffer
      cb(null, buf);
    }),
  )
  .pipe(fs.createWriteStream('output.txt'));

Note that you can use bufferstreams with the objectMode option. In this case, the given buffer will be an array containing the streamed objects:

new BufferStreams(myCallback, { objectMode: true });

bufferstreams exposes a utility function for functional programming:

import { streamBuffer } from 'bufferstreams';

process.stdin.pipe(streamBuffer(myCallback)).pipe(process.stdout);

Finally bufferstreams exposes another function for objects mode buffering:

import { bufferObjects } from 'bufferstreams';

process.stdin.pipe(bufferObjects(myCallback)).pipe(process.stdout);

Contributing

Feel free to contribute with your code if you agree with publishing it under the MIT license.

API

Classes

BufferStream

Buffer the stream content and bring it into the provided callback

Functions

bufferStream(bufferCallback, options)

Utility function if you prefer a functional way of using this lib

bufferObjects(bufferCallback, options)

Utility function to buffer objet mode streams

BufferStream

Buffer the stream content and bring it into the provided callback

Kind: global class

new BufferStream(bufferCallback, options)

Param Type Description
bufferCallback function A function to handle the buffered content.
options Object inherits of Stream.Duplex, the options are passed to the parent constructor so you can use it's options too.
options.objectMode boolean Use if piped in streams are in object mode. In this case, an array of the buffered will be transmitted to the callback function.

bufferStream(bufferCallback, options) ⇒

Utility function if you prefer a functional way of using this lib

Kind: global function
Returns: Stream

Param
bufferCallback
options

bufferObjects(bufferCallback, options) ⇒

Utility function to buffer objet mode streams

Kind: global function
Returns: Stream

Param
bufferCallback
options

Authors

License

MIT

changelog

4.0.0 (2024-07-17)

Code Refactoring

  • api: typescript rewrite (9bdffa4), closes #5

BREAKING CHANGES

  • api: Require Node20+

3.0.0 (2019-11-15)

2.0.1 (2018-04-23)

Bug Fixes

  • Remove debug dependency (2719573)

2.0.0 (2017-12-06)

build

  • metapak-nfroidure: Add metapak-nfroidure (ed104f6)

BREAKING CHANGES

  • metapak-nfroidure: Removing support for versions minors to the last LTS

Change Log

upcoming (2017/03/05 07:36 +00:00)

  • f1bea6b Add Node 4-6-7 (@nfroidure)

v1.1.1 (2016/06/28 11:20 +00:00)

  • acd1875 1.1.1 (@shinnn)
  • 4fc64b9 Use wider badges to fit modern Github design (@shinnn)
  • 2b28391 Fix invalid license statement in package.json (@shinnn)
  • e330151 Fix file mode (@shinnn)
  • #4 Use files field instead of .npmignore (@shinnn)
  • d7435e5 Use files field instead of .npmignore (@shinnn)

v1.1.0 (2015/08/04 09:48 +00:00)

  • 8b3f905 1.1.0 (@nfroidure)
  • e7a4854 Better doc (@nfroidure)
  • 0567b10 Also build on Node 0.12 (@nfroidure)
  • 92cd9de Dependencies update (@nfroidure)
  • 5bc3649 Better handling of errors (@nfroidure)
  • 1ca6222 Code cleanup and backward compatibility (@nfroidure)
  • 999b805 Using Duplex stream instead of Transform one (@nfroidure)
  • 77051e3 Add tests for callback error emitting (@nfroidure)
  • 6475b82 Adding linter and suppress warnings (@nfroidure)
  • 8d24a90 Improving the README file (@nfroidure)
  • d5ab44e Add stats (@nfroidure)

v1.0.2 (2015/06/21 07:11 +00:00)

  • 8aaf7a5 1.0.2 (@nfroidure)
  • fe52a06 Fix trinity script (@nfroidure)
  • d512161 Dependencies update (@nfroidure)
  • 48687ed Merge branch 'master' of github.com:nfroidure/bufferstreams (@nfroidure)
  • #3 Use SVG-based badges (@shinnn)
  • 88eccc5 Use SVG-based badges (@shinnn)
  • a2875b3 Simplify test scripts (@shinnn)

v1.0.1 (2015/02/09 17:57 +00:00)

v1.0.0 (2015/02/09 17:49 +00:00)

  • 438812d 1.0.0 (@nfroidure)
  • 01859b0 Documenting objectMode (@nfroidure)
  • d3cac8d Fixing tests (@nfroidure)
  • d7ae39a Adding code climate metrics badge (@nfroidure)
  • 677f394 Allowing objectMode, adding a better test lib and 100% coveraging (@nfroidure)
  • 1bf9b40 New version 0.0.2 (@nfroidure)
  • 17723e2 Added readable-stream closes#1 (@nfroidure)
  • 51545c3 Accept forgiving new (@nfroidure)
  • 8d87f57 Dependencies update (@nfroidure)
  • 3a1408f Added deps (@nfroidure)
  • 38f0690 Added coverage tests (@nfroidure)
  • 2a631d6 Typo fix (@nfroidure)
  • 7ce57c9 Fixing npm badge (@nfroidure)
  • c6d7b48 Adding npm badge (@nfroidure)
  • bfbca40 Adding instructions, fixing naming (@nfroidure)
  • 1a531c7 Readme updated (@nfroidure)
  • b0e1be8 First commit (@nfroidure)