or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-is-stream

Check if something is a Node.js stream

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/is-stream@4.0.x

To install, run

npx @tessl/cli install tessl/npm-is-stream@4.0.0

index.mddocs/

is-stream

is-stream is a Node.js utility library that provides comprehensive stream type detection and validation capabilities. It offers runtime type checking functions that determine whether an object is a stream and what specific stream operations it supports, with configurable options for checking stream state.

Package Information

  • Package Name: is-stream
  • Package Type: npm
  • Language: JavaScript (ES modules with TypeScript definitions)
  • Node.js Version: >=18
  • Installation: npm install is-stream

Core Imports

import { isStream, isWritableStream, isReadableStream, isDuplexStream, isTransformStream } from "is-stream";

Dynamic import (for CommonJS or conditional loading):

const { isStream, isWritableStream, isReadableStream, isDuplexStream, isTransformStream } = await import("is-stream");

With TypeScript (including types):

import { isStream, isWritableStream, isReadableStream, isDuplexStream, isTransformStream, type Options } from "is-stream";

Basic Usage

import fs from 'node:fs';
import { isStream, isWritableStream, isReadableStream } from 'is-stream';

// Check if something is any type of stream
const readStream = fs.createReadStream('file.txt');
console.log(isStream(readStream)); // true

// Check for specific stream types
const writeStream = fs.createWriteStream('output.txt');
console.log(isWritableStream(writeStream)); // true
console.log(isReadableStream(writeStream)); // false

// Configure stream state checking
console.log(isStream(closedStream, { checkOpen: false })); // true (ignores closed state)
console.log(isStream(closedStream, { checkOpen: true })); // false (default: checks if open)

Capabilities

Stream Detection

Detects if an object is any type of Node.js stream.

/**
 * Check if something is a Node.js Stream
 * @param stream - The object to check
 * @param options - Optional configuration
 * @returns Whether the object is a stream
 */
function isStream(stream: unknown, options?: Options): stream is Stream;

Writable Stream Detection

Detects if an object is a writable stream (stream.Writable, http.OutgoingMessage, http.ServerResponse, or http.ClientRequest).

/**
 * Check if something is a writable stream
 * @param stream - The object to check
 * @param options - Optional configuration
 * @returns Whether the object is a writable stream
 */
function isWritableStream(stream: unknown, options?: Options): stream is WritableStream;

Usage Example:

import fs from 'node:fs';
import http from 'node:http';
import { isWritableStream } from 'is-stream';

// File streams
console.log(isWritableStream(fs.createWriteStream('output.txt'))); // true
console.log(isWritableStream(fs.createReadStream('input.txt'))); // false

// HTTP streams
const server = http.createServer((req, res) => {
  console.log(isWritableStream(res)); // true (ServerResponse)
  console.log(isWritableStream(req)); // false (IncomingMessage)
});

// Network streams
import net from 'node:net';
const socket = new net.Socket();
console.log(isWritableStream(socket)); // true (Socket is duplex)

Readable Stream Detection

Detects if an object is a readable stream (stream.Readable or http.IncomingMessage).

/**
 * Check if something is a readable stream
 * @param stream - The object to check
 * @param options - Optional configuration
 * @returns Whether the object is a readable stream
 */
function isReadableStream(stream: unknown, options?: Options): stream is ReadableStream;

Usage Example:

import fs from 'node:fs';
import http from 'node:http';
import { isReadableStream } from 'is-stream';

// File streams
console.log(isReadableStream(fs.createReadStream('input.txt'))); // true
console.log(isReadableStream(fs.createWriteStream('output.txt'))); // false

// HTTP streams
const server = http.createServer((req, res) => {
  console.log(isReadableStream(req)); // true (IncomingMessage)
  console.log(isReadableStream(res)); // false (ServerResponse)
});

Duplex Stream Detection

Detects if an object is a duplex stream (stream.Duplex) that supports both reading and writing.

/**
 * Check if something is a duplex stream
 * @param stream - The object to check
 * @param options - Optional configuration
 * @returns Whether the object is a duplex stream
 */
function isDuplexStream(stream: unknown, options?: Options): stream is DuplexStream;

Usage Example:

import { Duplex } from 'node:stream';
import net from 'node:net';
import { isDuplexStream } from 'is-stream';

// Native duplex streams
const duplex = new Duplex({
  write(chunk, encoding, callback) { callback(); },
  read() { this.push(null); }
});
console.log(isDuplexStream(duplex)); // true

// Network streams (inherently duplex)
const socket = new net.Socket();
console.log(isDuplexStream(socket)); // true

Transform Stream Detection

Detects if an object is a transform stream (stream.Transform) that can modify data as it passes through.

/**
 * Check if something is a transform stream
 * @param stream - The object to check
 * @param options - Optional configuration
 * @returns Whether the object is a transform stream
 */
function isTransformStream(stream: unknown, options?: Options): stream is TransformStream;

Usage Example:

import { Transform, PassThrough } from 'node:stream';
import { isTransformStream } from 'is-stream';

// Transform stream that uppercases text
const uppercaseTransform = new Transform({
  transform(chunk, encoding, callback) {
    callback(null, chunk.toString().toUpperCase());
  }
});
console.log(isTransformStream(uppercaseTransform)); // true

// PassThrough is also a transform stream
const passthrough = new PassThrough();
console.log(isTransformStream(passthrough)); // true

Types

/**
 * Configuration options for stream checking functions
 */
interface Options {
  /**
   * When this option is true, the method returns false if the stream has already been closed.
   * @default true
   */
  checkOpen?: boolean;
}

/**
 * Stream types (from Node.js 'node:stream' module)
 * These are the base types that is-stream can detect
 */
type Stream = import('node:stream').Stream;
type WritableStream = import('node:stream').Writable;
type ReadableStream = import('node:stream').Readable;
type DuplexStream = import('node:stream').Duplex;
type TransformStream = import('node:stream').Transform;

Supported Stream Types

The library correctly identifies these Node.js stream types:

  • Core streams: Stream.Readable, Stream.Writable, Stream.Duplex, Stream.Transform, Stream.PassThrough
  • File streams: fs.createReadStream(), fs.createWriteStream()
  • HTTP streams: http.IncomingMessage, http.OutgoingMessage, http.ServerResponse, http.ClientRequest
  • Network streams: net.Socket
  • Custom streams: Any object implementing the Node.js stream interface

Error Handling

All functions return false for:

  • null or undefined values
  • Non-object types (strings, numbers, booleans)
  • Objects that don't implement the stream interface
  • Closed streams (when checkOpen: true, which is the default)