Skip to content

isNodeStream

Checks if a value is a Node.js stream (has a .pipe() method).

Uses duck-typing: any object with a pipe function qualifies, covering Readable, Writable, Duplex, Transform, and custom stream-compatible objects without importing from node:stream.

Available since v2.0.3

import { isNodeStream } from '@helpers4/node';
// or, from the all-in-one package (same code, one install):
import { isNodeStream } from 'helpers4/node';
isNodeStream(value: unknown): value is object
ParameterTypeDescription
valueunknownThe value to check

value is object — true if value is a Node.js stream

Returns true for any object with a .pipe() method (Readable, Writable, Transform, etc.).

import { Readable } from 'node:stream';
isNodeStream(new Readable({ read() {} })) // => true
isNodeStream({})                          // => false
isNodeStream(null)                        // => false

Use isNodeStream to safely pipe only known streams.

import { Writable } from 'node:stream';
function pipeToOutput(source: unknown, dest: Writable): void {
  if (isNodeStream(source)) {
    source.pipe(dest);
  }
}

View source on GitHub