Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

through2

Package Overview
Dependencies
Maintainers
2
Versions
38
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

through2

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

  • 0.4.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
34M
decreased by-13.31%
Maintainers
2
Weekly downloads
 
Created

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

Keywords

FAQs

Package last updated on 22 May 2014

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc