Check if something is a Node.js stream
npx @tessl/cli install tessl/npm-is-stream@4.0.0is-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.
npm install is-streamimport { 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";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)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;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)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)
});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)); // trueDetects 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/**
* 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;The library correctly identifies these Node.js stream types:
Stream.Readable, Stream.Writable, Stream.Duplex, Stream.Transform, Stream.PassThroughfs.createReadStream(), fs.createWriteStream()http.IncomingMessage, http.OutgoingMessage, http.ServerResponse, http.ClientRequestnet.SocketAll functions return false for:
null or undefined valuescheckOpen: true, which is the default)