Socket
Socket
Sign inDemoInstall

through2

Package Overview
Dependencies
7
Maintainers
2
Versions
38
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

through2

A tiny wrapper around Node streams2 Transform to avoid explicit subclassing noise


Version published
Maintainers
2
Weekly downloads
29,610,792
decreased by-8.27%

Weekly downloads

Package description

What is through2?

The through2 npm package is a thin wrapper around Node.js streams.Transform (a standard Node.js API) that makes it easier to create transform streams. It is designed to work with streams in object mode or non-object mode, allowing for simple function-based stream transformation. This package is particularly useful when you need to create a custom stream that modifies or transforms data as it passes through the stream pipeline.

What are through2's main functionalities?

Simple Transform

This feature allows you to create a simple transform stream that converts input data to uppercase. The code sample demonstrates how to use through2 to create a stream that reads from stdin, transforms the data, and writes to stdout.

const through2 = require('through2');
const stream = through2(function(chunk, enc, callback) {
  this.push(chunk.toString().toUpperCase());
  callback();
});
process.stdin.pipe(stream).pipe(process.stdout);

Object Mode Transform

This feature allows you to work with streams in object mode, where the stream chunks are JavaScript objects. The code sample shows how to create a transform stream that modifies properties of objects and how to use it with a readable stream of objects.

const through2 = require('through2').obj;
const stream = through2(function(obj, enc, callback) {
  obj.key = obj.key.toUpperCase();
  this.push(obj);
  callback();
});

// Usage with an array of objects
const objects = [{ key: 'value' }, { key: 'data' }];
const objectStream = require('stream').Readable.from(objects);
objectStream.pipe(stream).on('data', (transformedObj) => console.log(transformedObj));

Flush Function

This feature allows you to perform an action when the stream is ending, just before it finishes. The code sample demonstrates how to append a message to the stream output right before the stream ends.

const through2 = require('through2');
const stream = through2(function(chunk, enc, callback) {
  this.push(chunk);
  callback();
}, function(callback) {
  // This function is called before the stream is finished
  this.push('Stream is ending!');
  callback();
});

process.stdin.pipe(stream).pipe(process.stdout);

Other packages similar to through2

Readme

Source

through2

NPM

A tiny wrapper around Node streams.Transform (Streams2) to avoid explicit subclassing noise

Inspired by Dominic Tarr's through in that it's so much easier to make a stream out of a function than it is to set up the prototype chain properly: through(function (chunk) { ... }).

fs.createReadStream('ex.txt')
  .pipe(through2(function (chunk, enc, callback) {

    for (var i = 0; i < chunk.length; i++)
      if (chunk[i] == 97)
        chunk[i] = 122 // swap 'a' for 'z'

    this.push(chunk)
    
    callback()

   }))
  .pipe(fs.createWriteStream('out.txt'))

Or object streams:

var all = []

fs.createReadStream('data.csv')
  .pipe(csv2())
  .pipe(through2.obj(function (chunk, enc, callback) {

    var data = {
        name    : chunk[0]
      , address : chunk[3]
      , phone   : chunk[10]
    }
    
    this.push(data)
    
    callback()

  }))
  .on('data', function (data) {
    all.push(data)
  })
  .on('end', function () {
    doSomethingSpecial(all)
  })

Note that through2.obj(fn) is a convenience wrapper around through2({ objectMode: true }, fn).

API

through2([ options, ] [ transformFunction ] [, flushFunction ])

Consult the stream.Transform documentation for the exact rules of the transformFunction (i.e. this._transform) and the optional flushFunction (i.e. this._flush).

options

The options argument is optional and is passed straight through to stream.Transform. So you can use objectMode:true if you are processing non-binary streams (or just use through2.obj()).

The options argument is first, unlike standard convention, because if I'm passing in an anonymous function then I'd prefer for the options argument to not get lost at the end of the call:

fs.createReadStream('/tmp/important.dat')
  .pipe(through2({ objectMode: true, allowHalfOpen: false }, function (chunk, enc, cb) {

    this.push(new Buffer('wut?'))
    cb()

  })
  .pipe(fs.createWriteStream('/tmp/wut.txt'))

transformFunction

The transformFunction must have the following signature: function (chunk, encoding, callback) {}. A minimal implementation should call the callback function to indicate that the transformation is done, even if that transformation means discarding the chunk.

To queue a new chunk, call this.push(chunk)—this can be called as many times as required before the callback() if you have multiple pieces to send on.

If you do not provide a transformFunction then you will get a simple simple pass-through stream.

flushFunction

The optional flushFunction is provided as the last argument (2nd or 3rd, depending on whether you've supplied options) is called just prior to the stream ending. Can be used to finish up any processing that may be in progress.

through2.ctor([ options, ] transformFunction[, flushFunction ])

Instead of returning a stream.Transform instance, through2.ctor() returns a constructor for a custom Transform. This is useful when you want to use the same transform logic in multiple instances.

var FToC = through2.ctor({objectMode: true}, function (record, encoding, callback) {
  if (record.temp != null && record.unit = "F") {
    record.temp = ( ( record.temp - 32 ) * 5 ) / 9
    record.unit = "C"
  }
  this.push(record)
  callback()
})

// Create instances of FToC like so:
var converter = new FToC()
// Or:
var converter = FToC()
// Or specify/override options when you instantiate, if you prefer:
var converter = FToC({objectMode: true})

License

through2 is Copyright (c) 2013 Rod Vagg @rvagg and licenced under the MIT licence. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE file for more details.

Keywords

FAQs

Last updated on 18 Feb 2014

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc