Skip to content

ReadStream

Defined in: src/node/streams.ts:19

A ReadStream implementation that wraps an underlying global ReadableStream.

  • Readable
  • ReadStream

new ReadStream(opts?, handleOrPromise): ReadStream

Defined in: src/node/streams.ts:26

CreateReadStreamOptions = {}

FileHandle | Promise<FileHandle>

ReadStream

Readable.constructor

_events: Listeners

Defined in: node_modules/@zenfs/streams/dist/events.d.ts:28

Readable._events


_eventsCount: number

Defined in: node_modules/@zenfs/streams/dist/events.d.ts:29

Readable._eventsCount


optional _maxListeners?: number

Defined in: node_modules/@zenfs/streams/dist/events.d.ts:30

Readable._maxListeners


_readableState: ReadableState

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:99

Readable._readableState


optional _writableState?: WritableState

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:101

Present on a Duplex; undefined on a plain Readable.

Readable._writableState


protected [kCapture]: boolean

Defined in: node_modules/@zenfs/streams/dist/events.d.ts:31

Readable.[kCapture]


pending: boolean = true

Defined in: src/node/streams.ts:20

This property is true if the underlying file has not been opened yet, i.e. before the 'ready' event is emitted.

v11.2.0, v10.16.0

fs.ReadStream.pending


static _fromList: (n, state) => any

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:163

Exposed for tests.

Takes n bytes (or one object) off the front of the buffer.

number

ReadableState

any

Readable._fromList


static captureRejections: boolean

Defined in: node_modules/@zenfs/streams/dist/events.d.ts:25

The default for the captureRejections option of newly created emitters.

Readable.captureRejections


readonly static captureRejectionSymbol: typeof captureRejectionSymbol

Defined in: node_modules/@zenfs/streams/dist/events.d.ts:26

Readable.captureRejectionSymbol


static defaultMaxListeners: number

Defined in: node_modules/@zenfs/streams/dist/events.d.ts:23

Readable.defaultMaxListeners


readonly static errorMonitor: typeof errorMonitor

Defined in: node_modules/@zenfs/streams/dist/events.d.ts:27

Readable.errorMonitor


readonly static ReadableState: typeof ReadableState

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:161

Readable.ReadableState

get bytesRead(): number

Defined in: src/node/streams.ts:77

The number of bytes that have been read so far.

v6.4.0

number

fs.ReadStream.bytesRead


get closed(): boolean

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:149

Is true after 'close' has been emitted.

v18.0.0

boolean

fs.ReadStream.closed

Readable.closed


get destroyed(): boolean

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:150

Is true after readable.destroy() has been called.

v8.0.0

boolean

set destroyed(value): void

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:151

Is true after readable.destroy() has been called.

v8.0.0

boolean

void

fs.ReadStream.destroyed

Readable.destroyed


get errored(): Error | null

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:148

Returns error if the stream has been destroyed with an error.

v18.0.0

Error | null

fs.ReadStream.errored

Readable.errored


get path(): string

Defined in: src/node/streams.ts:73

The path to the file the stream is reading from as specified in the first argument to fs.createReadStream(). If path is passed as a string, thenreadStream.path will be a string. If path is passed as a Buffer, thenreadStream.path will be a Buffer. If fd is specified, thenreadStream.path will be undefined.

v0.1.93

string

fs.ReadStream.path


get readable(): boolean

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:137

Is true if it is safe to call read, which means the stream has not been destroyed or emitted 'error' or 'end'.

v11.4.0

boolean

set readable(value): void

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:138

Is true if it is safe to call read, which means the stream has not been destroyed or emitted 'error' or 'end'.

v11.4.0

boolean

void

fs.ReadStream.readable

Readable.readable


get readableAborted(): boolean

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:140

Returns whether the stream was destroyed or errored before emitting 'end'.

v16.8.0

boolean

fs.ReadStream.readableAborted

Readable.readableAborted


get readableBuffer(): any[] | undefined

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:142

any[] | undefined

Readable.readableBuffer


get readableDidRead(): boolean

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:139

Returns whether 'data' has been emitted.

v16.7.0, v14.18.0

boolean

fs.ReadStream.readableDidRead

Readable.readableDidRead


get readableEncoding(): BufferEncoding | null

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:147

Getter for the property encoding of a given Readable stream. The encoding property can be set using the setEncoding method.

v12.7.0

BufferEncoding | null

fs.ReadStream.readableEncoding

Readable.readableEncoding


get readableEnded(): boolean

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:152

Becomes true when 'end' event is emitted.

v12.9.0

boolean

fs.ReadStream.readableEnded

Readable.readableEnded


get readableFlowing(): boolean | null

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:143

This property reflects the current state of a Readable stream as described in the Three states section.

v9.4.0

boolean | null

set readableFlowing(value): void

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:144

This property reflects the current state of a Readable stream as described in the Three states section.

v9.4.0

boolean | null

void

fs.ReadStream.readableFlowing

Readable.readableFlowing


get readableHighWaterMark(): number

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:141

Returns the value of highWaterMark passed when creating this Readable.

v9.3.0

number

fs.ReadStream.readableHighWaterMark

Readable.readableHighWaterMark


get readableLength(): number

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:145

This property contains the number of bytes (or objects) in the queue ready to be read. The value provides introspection data regarding the status of the highWaterMark.

v9.4.0

number

fs.ReadStream.readableLength

Readable.readableLength


get readableObjectMode(): boolean

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:146

Getter for the property objectMode of a given Readable stream.

v12.3.0

boolean

fs.ReadStream.readableObjectMode

Readable.readableObjectMode

optional _construct(callback): void

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:106

Callback

void

fs.ReadStream._construct

Readable._construct


_destroy(error, callback): void

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:105

Error | null

Callback

void

fs.ReadStream._destroy

Readable._destroy


_read(): Promise<void>

Defined in: src/node/streams.ts:47

Promise<void>

fs.ReadStream._read

Readable._read


_undestroy(): void

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:104

void

Readable._undestroy


[asyncDispose](): Promise<void>

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:108

Promise<void>

fs.ReadStream.[asyncDispose]

Readable.[asyncDispose]


[asyncIterator](): AsyncIterator<any>

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:135

AsyncIterator<any>

fs.ReadStream.[asyncIterator]

Readable.[asyncIterator]


[captureRejectionSymbol](err): void

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:107

Error

void

Readable.[captureRejectionSymbol]


[toAsyncStreamable](): AsyncGenerator<Uint8Array<ArrayBufferLike>[], any, any>

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:164

AsyncGenerator<Uint8Array<ArrayBufferLike>[], any, any>

fs.ReadStream.[toAsyncStreamable]

Readable.[toAsyncStreamable]


addListener(ev, fn): this

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:126

EventKey

Listener

this

fs.ReadStream.addListener

Readable.addListener


close(callback?): void

Defined in: src/node/streams.ts:68

Like Node, the callback is invoked once the stream is closed. Closing before the stream has finished reports ERR_STREAM_PREMATURE_CLOSE.

Callback<[void]> = ...

void

fs.ReadStream.close


compose(stream, options?): Duplex

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:199

Available once @zenfs/streams (or @zenfs/streams/compose) has been imported.

any

AbortSignal

Duplex

fs.ReadStream.compose

Readable.compose


destroy(error?, callback?): this

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:103

Error | null

Callback

this

fs.ReadStream.destroy

Readable.destroy


drop(limit, options?): Readable

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:180

number

ReadableOperatorOptions

Readable

fs.ReadStream.drop

Readable.drop


emit(type, …args): boolean

Defined in: node_modules/@zenfs/streams/dist/events.d.ts:35

EventKey

…any[]

boolean

fs.ReadStream.emit

Readable.emit


eventNames(): EventKey[]

Defined in: node_modules/@zenfs/streams/dist/events.d.ts:47

EventKey[]

fs.ReadStream.eventNames

Readable.eventNames


every(fn, options?): Promise<boolean>

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:182

(value, options) => any

ReadableOperatorOptions

Promise<boolean>

fs.ReadStream.every

Readable.every


filter(fn, options?): Readable

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:174

(value, options) => any

ReadableOperatorOptions

Readable

fs.ReadStream.filter

Readable.filter


find(fn, options?): Promise<any>

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:188

(value, options) => any

ReadableOperatorOptions

Promise<any>

fs.ReadStream.find

Readable.find


flatMap(fn, options?): Readable

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:177

(value, options) => any

ReadableOperatorOptions

Readable

fs.ReadStream.flatMap

Readable.flatMap


forEach(fn, options?): Promise<void>

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:191

(value, options) => any

ReadableOperatorOptions

Promise<void>

fs.ReadStream.forEach

Readable.forEach


getMaxListeners(): number

Defined in: node_modules/@zenfs/streams/dist/events.d.ts:34

number

fs.ReadStream.getMaxListeners

Readable.getMaxListeners


isPaused(): boolean

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:116

The readable.isPaused() method returns the current operating state of the Readable. This is used primarily by the mechanism that underlies the readable.pipe() method. In most typical cases, there will be no reason to use this method directly.

const readable = new stream.Readable();
readable.isPaused(); // === false
readable.pause();
readable.isPaused(); // === true
readable.resume();
readable.isPaused(); // === false

boolean

v0.11.14

fs.ReadStream.isPaused

Readable.isPaused


iterator(options?): AsyncIterator<any>

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:136

The iterator created by this method gives users the option to cancel the destruction of the stream if the for await...of loop is exited by return, break, or throw, or if the iterator should destroy the stream if the stream emitted an error during iteration.

ReadableIteratorOptions

AsyncIterator<any>

v16.3.0

fs.ReadStream.iterator

Readable.iterator


listenerCount(type, listener?): number

Defined in: node_modules/@zenfs/streams/dist/events.d.ts:46

EventKey

Listener

number

fs.ReadStream.listenerCount

Readable.listenerCount


listeners(type): Listener[]

Defined in: node_modules/@zenfs/streams/dist/events.d.ts:44

EventKey

Listener[]

fs.ReadStream.listeners

Readable.listeners


map(fn, options?): Readable

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:171

(value, options) => any

ReadableOperatorOptions

Readable

fs.ReadStream.map

Readable.map


off(ev, fn): this

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:128

EventKey

Listener

this

fs.ReadStream.off

Readable.off


on(ev, fn): this

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:125

EventKey

Listener

this

fs.ReadStream.on

Readable.on


once(type, listener): this

Defined in: node_modules/@zenfs/streams/dist/events.d.ts:39

EventKey

Listener

this

fs.ReadStream.once

Readable.once


pause(): this

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:132

The readable.pause() method will cause a stream in flowing mode to stop emitting 'data' events, switching out of flowing mode. Any data that becomes available will remain in the internal buffer.

const readable = getReadableStreamSomehow();
readable.on('data', (chunk) => {
console.log(`Received ${chunk.length} bytes of data.`);
readable.pause();
console.log('There will be no additional data for 1 second.');
setTimeout(() => {
console.log('Now data will start flowing again.');
readable.resume();
}, 1000);
});

The readable.pause() method has no effect if there is a 'readable' event listener.

this

v0.9.4

fs.ReadStream.pause

Readable.pause


pipe<T>(dest, pipeOpts?): T

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:121

T extends PipeDestination

T

boolean

T

fs.ReadStream.pipe

Readable.pipe


prependListener(type, listener): this

Defined in: node_modules/@zenfs/streams/dist/events.d.ts:38

EventKey

Listener

this

fs.ReadStream.prependListener

Readable.prependListener


prependOnceListener(type, listener): this

Defined in: node_modules/@zenfs/streams/dist/events.d.ts:40

EventKey

Listener

this

fs.ReadStream.prependOnceListener

Readable.prependOnceListener


push(chunk, encoding?): boolean

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:113

Adds a chunk to the read buffer, or ends the stream when passed null. Returns false once the high water mark is reached.

any

BufferEncoding

boolean

fs.ReadStream.push

Readable.push


rawListeners(type): Listener[]

Defined in: node_modules/@zenfs/streams/dist/events.d.ts:45

EventKey

Listener[]

fs.ReadStream.rawListeners

Readable.rawListeners


read(n?): any

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:119

The readable.read() method reads data out of the internal buffer and returns it. If no data is available to be read, null is returned. By default, the data is returned as a Buffer object unless an encoding has been specified using the readable.setEncoding() method or the stream is operating in object mode.

The optional size argument specifies a specific number of bytes to read. If size bytes are not available to be read, null will be returned unless the stream has ended, in which case all of the data remaining in the internal buffer will be returned.

If the size argument is not specified, all of the data contained in the internal buffer will be returned.

The size argument must be less than or equal to 1 GiB.

The readable.read() method should only be called on Readable streams operating in paused mode. In flowing mode, readable.read() is called automatically until the internal buffer is fully drained.

const readable = getReadableStreamSomehow();
// 'readable' may be triggered multiple times as data is buffered in
readable.on('readable', () => {
let chunk;
console.log('Stream is readable (new data received in buffer)');
// Use a loop to make sure we read all currently available data
while (null !== (chunk = readable.read())) {
console.log(`Read ${chunk.length} bytes of data...`);
}
});
// 'end' will be triggered once when there is no more data available
readable.on('end', () => {
console.log('Reached end of stream.');
});

Each call to readable.read() returns a chunk of data, or null. The chunks are not concatenated. A while loop is necessary to consume all data currently in the buffer. When reading a large file .read() may return null, having consumed all buffered content so far, but there is still more data to come not yet buffered. In this case a new 'readable' event will be emitted when there is more data in the buffer. Finally the 'end' event will be emitted when there is no more data to come.

Therefore to read a file’s whole contents from a readable, it is necessary to collect chunks across multiple 'readable' events:

const chunks = [];
readable.on('readable', () => {
let chunk;
while (null !== (chunk = readable.read())) {
chunks.push(chunk);
}
});
readable.on('end', () => {
const content = chunks.join('');
});

A Readable stream in object mode will always return a single item from a call to readable.read(size), regardless of the value of the size argument.

If the readable.read() method returns a chunk of data, a 'data' event will also be emitted.

Calling read after the 'end' event has been emitted will return null. No runtime error will be raised.

number | null

any

v0.9.4

fs.ReadStream.read

Readable.read


reduce(reducer, initialValue?, options?): Promise<any>

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:195

(previous, value, options) => any

any

ReadableOperatorOptions

Promise<any>

fs.ReadStream.reduce

Readable.reduce


removeAllListeners(ev?): this

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:129

EventKey

this

fs.ReadStream.removeAllListeners

Readable.removeAllListeners


removeListener(ev, fn): this

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:127

EventKey

Listener

this

fs.ReadStream.removeListener

Readable.removeListener


resume(): this

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:131

Switches the stream into flowing mode.

this

fs.ReadStream.resume

Readable.resume


setEncoding(encoding): this

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:118

Makes the stream emit strings in encoding rather than Buffers, without splitting characters.

BufferEncoding

this

fs.ReadStream.setEncoding

Readable.setEncoding


setMaxListeners(n): this

Defined in: node_modules/@zenfs/streams/dist/events.d.ts:33

number

this

fs.ReadStream.setMaxListeners

Readable.setMaxListeners


some(fn, options?): Promise<boolean>

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:185

(value, options) => any

ReadableOperatorOptions

Promise<boolean>

fs.ReadStream.some

Readable.some


take(limit, options?): Readable

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:181

number

ReadableOperatorOptions

Readable

fs.ReadStream.take

Readable.take


toArray(options?): Promise<any[]>

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:194

ReadableOperatorOptions

Promise<any[]>

fs.ReadStream.toArray

Readable.toArray


unpipe(dest?): this

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:124

The readable.unpipe() method detaches a Writable stream previously attached using the pipe method.

If the destination is not specified, then all pipes are detached.

If the destination is specified, but no pipe is set up for it, then the method does nothing.

import fs from 'node:fs';
const readable = getReadableStreamSomehow();
const writable = fs.createWriteStream('file.txt');
// All the data from readable goes into 'file.txt',
// but only for the first second.
readable.pipe(writable);
setTimeout(() => {
console.log('Stop writing to file.txt.');
readable.unpipe(writable);
console.log('Manually close the file stream.');
writable.end();
}, 1000);

PipeDestination

this

v0.9.4

fs.ReadStream.unpipe

Readable.unpipe


unshift(chunk, encoding?): boolean

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:115

Returns a chunk to the front of the read buffer. Should only ever be a chunk that came out of read().

any

BufferEncoding

boolean

fs.ReadStream.unshift

Readable.unshift


wrap(oldStream): this

Defined in: src/node/streams.ts:81

Uses an old-style (pre-streams2) stream as this stream’s data source.

ReadableStream

this

fs.ReadStream.wrap

Readable.wrap


static from(iterable, options?): Readable

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:154

Builds a Readable from an iterable, async iterable, string or Buffer.

string | Buffer<ArrayBufferLike> | Iterable<unknown, any, any> | AsyncIterable<unknown, any, any>

ReadableOptions<Readable>

Readable

Readable.from


static fromWeb(readableStream, options?): Readable

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:158

Wraps a WHATWG ReadableStream.

ReadableStream

ReadableOptions<Readable>

Readable

Readable.fromWeb


static toWeb(streamReadable, options?): ReadableStream

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:160

Exposes this stream as a WHATWG ReadableStream.

Readable

ReadableToWebOptions

ReadableStream

Readable.toWeb


static wrap(src, options?): Readable

Defined in: node_modules/@zenfs/streams/dist/readable.d.ts:156

Wraps an old-style stream, or any object with a compatible data/end event surface.

any

ReadableOptions<Readable>

Readable

Readable.wrap