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

Package detail

streamfilter

nfroidure698.4kMIT4.0.0TypeScript support: included

Filtering streams.

assert, deepEqual, neat

readme

streamfilter

Filtering streams.

GitHub license

streamfilter is a function based filter for streams inspired per gulp-filter but no limited to Gulp nor to objectMode streams.

Installation

First, install streamfilter in your project:

npm install --save streamfilter

Getting started

There are 3 common usages:

Simple filter

import { StreamFilter } from 'streamfilter';

const filter = new StreamFilter((chunk, encoding, cb) => {
  const mustBeFiltered = chunk.length() > 128;

  if (mustBeFiltered) {
    cb(true);
    return;
  }
  cb(false);
});

// Print to stdout a filtered stdin
process.stdin.pipe(filter).pipe(process.stdout);

Filter and restore

import { filterStream } from 'streamfilter';

// Here we use the functionnal help
const filter = new filterStream(
  // Here we use an async callback instead
  async (chunk, encoding) => {
    const mustBeFiltered = chunk.length() > 128;

    if (mustBeFiltered) {
      return true;
    }
    return false;
  },
  {
    restore: true,
  },
);

// Print accepted chunks in stdout
filter.pipe(process.stdout);

// Print filtered one to stderr
filter.restore.pipe(process.stderr);

Filter and restore as a passthrough stream

Let's reach total hype!

import { StreamFilter } from 'streamfilter';
import { Transform } from 'stream';

// Filter values
const filter = new StreamFilter(
  (chunk, encoding, cb) => {
    const mustBeFiltered = chunk.length() > 128;
    if (mustBeFiltered) {
      cb(true);
      return;
    }
    cb(false);
  },
  {
    restore: true,
    passthrough: true,
  },
);

// Uppercase strings
const mySuperTransformStream = new Transform({
  transform: (chunk, encoding, cb) =>
    cb(null, Buffer.from(chunk.toString(encoding).toUpperCase(), encoding)),
});

// Pipe stdin
process.stdin
  .pipe(filter)
  // Edit kept chunks
  .pipe(mySuperTransformStream)
  // Restore filtered chunks
  .pipe(filter.restore)
  // and output!
  .pipe(process.stdout);

Note that in this case, this is your responsibility to end the restore stream by piping in another stream or ending it manually.

API

Classes

StreamFilter

Filter piped in streams according to the given filterCallback.

Functions

filterStream(filterCallback, options)

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

StreamFilter

Filter piped in streams according to the given filterCallback.

Kind: global class

new StreamFilter(filterCallback, options)

Options are passed in as is in the various stream instances spawned by this module. So, to use the objectMode, simply pass in the options.objectMode value set to true.

Returns: StreamFilter - The filtering stream

Param Type Description
filterCallback function Callback applying the filters
options Object Filtering options
options.passthrough boolean Set to true, this option changes the restore stream nature from a readable stream to a passthrough one, allowing you to reuse the filtered chunks in an existing pipeline.
options.restore boolean Set to true, this option create a readable stream allowing you to use the filtered chunks elsewhere. The restore stream is exposed in the FilterStream instance as a restore named property.

filterStream(filterCallback, options) ⇒

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

Kind: global function
Returns: Stream

Param
filterCallback
options

Authors

License

MIT

changelog

4.0.0 (2024-07-17)

Code Refactoring

  • core: use es6 class instead (de678c8), closes #5

Features

  • core: allow async funtions as callbacks (affadc4)

BREAKING CHANGES

  • core: No more magic, users will have to choose between the class StreamFilter and the filterStream utility function.

3.0.0 (2018-11-10)

2.0.0 (2017-12-06)

1.0.6 (2017-12-03)

Change Log

upcoming (2017/03/04 16:13 +00:00)

  • 9a5ad65 Setting metapak/metapak-nfroidure up (@nfroidure)

v1.0.5 (2015/08/21 08:40 +00:00)

  • 0c0c8a1 1.0.5 (@nfroidure)
  • f7d8b5d Handle the restore stream with a Duplex one fix #3 (@nfroidure)
  • e747846 Factorizing backpressure management #3 (@nfroidure)

v1.0.4 (2015/08/11 09:02 +00:00)

  • 4d09fe2 1.0.4 (@nfroidure)
  • 8874bd8 Fixing the buffer overflow bug (@nfroidure)
  • c6dba12 Adding tests overflowing the internal buffers (@nfroidure)

v1.0.3 (2015/08/02 10:10 +00:00)

  • ba61e88 1.0.3 (@nfroidure)
  • efc512c Improve coveraging (@nfroidure)
  • 1baaefe Update dependencies (@nfroidure)
  • a0289a5 Add lint rules and suppress lin warnings (@nfroidure)
  • c311640 Specialize restore streams and make them backpressure aware fix #1 #2 (@nfroidure)
  • 179e858 Add encoding where usable (@nfroidure)
  • a924de7 Trying to reproduce a bug but with no luck :) (@nfroidure)
  • 0bfbbf3 Adding some tests (@nfroidure)

v1.0.2 (2015/02/12 08:00 +00:00)

  • bac10e0 1.0.2 (@nfroidure)
  • 8d10e33 Deps fix + npm ignore addition (@nfroidure)

v1.0.1 (2015/02/09 08:34 +00:00)

v1.0.0 (2015/02/09 08:23 +00:00)