through2

  • Version 5.0.1
  • Published
  • 68.3 kB
  • 1 dependency
  • MIT license

Install

npm i through2
yarn add through2
pnpm add through2

Overview

Tiny utilities for inserting transformation logic into Node.js stream and Web Streams pipelines

Index

Variables

variable through2

const through2: ((
options?: any,
transformFn?: TransformFn,
flushFn?: FlushFn
) => any) & { obj: typeof objectTransform; ctor: typeof transformer };
  • Default export: transform with legacy .obj / .ctor for v4 back-compatibility. {typeof transform & { obj: typeof objectTransform, ctor: typeof transformer }}

Functions

function objectTransform

objectTransform: (
options?: TransformOptions | TransformFn,
transformFn?: TransformFn,
flushFn?: FlushFn
) => any;
  • Like transform, with objectMode: true by default.

    Parameter options

    Parameter transformFn

    Parameter flushFn

    Returns

    {import('readable-stream').Transform}

    Example 1

    Filter objectTransform(async (item) => predicate(item) ? item : undefined)

    Example 2

    Batch objectTransform(async function * (source) { let batch = [] for await (const item of source) { batch.push(item) if (batch.length >= 100) { yield batch; batch = [] } } if (batch.length) yield batch })

function transform

transform: (
options?: TransformOptions | TransformFn,
transformFn?: TransformFn,
flushFn?: FlushFn
) => any;
  • Build a Transform around a transformation function (classic / async / async generator; auto-dispatched).

    Parameter options

    Parameter transformFn

    Parameter flushFn

    Returns

    {import('readable-stream').Transform}

    Example 1

    Classic transform(function (chunk, _enc, cb) { this.push(chunk); cb() })

    Example 2

    Async transform(async (chunk) => chunk.toString().toUpperCase())

    Example 3

    Async generator (1-to-many; cross-chunk state is just a local var) transform(async function * (source) { let buf = '' for await (const chunk of source) { buf += chunk.toString() const lines = buf.split('\n') buf = lines.pop() for (const line of lines) yield line } if (buf) yield buf })

function transformer

transformer: (
options?: TransformOptions | TransformFn,
transformFn?: TransformFn,
flushFn?: FlushFn
) => (
override?: any
) => import('readable-stream').Transform & { options: TransformOptions };
  • Build a reusable factory. The returned function works with or without new; per-call options merge over the configured defaults and are exposed as this.options inside the transform.

    Note: in v5 this returns a Transform instance directly, not a true constructor; instanceof FactoryFn no longer holds (instanceof Transform still does).

    Parameter options

    Parameter transformFn

    Parameter flushFn

    Returns

    {(override?: TransformOptions) => import('readable-stream').Transform & { options: TransformOptions }}

    Example 1

    const Counter = transformer({ objectMode: true }, function (chunk, _enc, cb) { this.count = (this.count || 0) + 1 this.push(chunk); cb() }) const a = Counter() const b = new Counter({ highWaterMark: 32 })

Type Aliases

type AsyncFlushFn

type AsyncFlushFn = () => any | Promise<any>;

    type AsyncGenTransformFn

    type AsyncGenTransformFn = (
    source: AsyncIterable<any>
    ) => AsyncGenerator<any, void, void>;

      type AsyncTransformFn

      type AsyncTransformFn = (chunk: any, encoding: BufferEncoding) => any | Promise<any>;

        type ClassicFlushFn

        type ClassicFlushFn = (callback: (err?: Error | null) => void) => void;

          type ClassicTransformFn

          type ClassicTransformFn = (
          chunk: any,
          encoding: BufferEncoding,
          callback: (err?: Error | null, data?: any) => void
          ) => void;

            type FlushFn

            type FlushFn = ClassicFlushFn | AsyncFlushFn;

              type TransformFn

              type TransformFn = ClassicTransformFn | AsyncTransformFn | AsyncGenTransformFn;

                type TransformOptions

                type TransformOptions = import('readable-stream').TransformOptions;

                  Package Files (1)

                  Dependencies (1)

                  Dev Dependencies (12)

                  Peer Dependencies (0)

                  No peer dependencies.

                  Badge

                  To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

                  You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/through2.

                  • Markdown
                    [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/through2)
                  • HTML
                    <a href="https://www.jsdocs.io/package/through2"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>