Readable

interface Readable : StatefulStream, EventEmitter, EventTarget, ProxyObject, ProxyIterable

Node: Readable Stream.

A Readable stream is an abstraction for a source from which data is consumed; an example of a Readable stream is the response object returned by http.get(), or the input stream provided to a running program.

Examples of Readable streams include:

  • HTTP responses, on the client

  • HTTP requests, on the server

  • fs read streams

  • zlib streams

  • crypto streams

  • TCP sockets

  • child process stdout and stderr

  • process.stdin

All Readable streams implement the interface defined by the stream.Readable class.

 

Two reading modes

Readable streams effectively operate in one of two modes: flowing and paused. These modes are separate from object mode. A Readable stream can be in object mode or not, regardless of whether it is in flowing mode or paused mode.

In flowing mode, data is read from the underlying system automatically and provided to an application as quickly as possible using events via the EventEmitter interface.

In paused mode, the read method must be called explicitly to read chunks of data from the stream.

All Readable streams begin in paused mode but can be switched to flowing mode in one of the following ways:

  • Adding a 'data' event handler.

  • Calling the resume method.

  • Calling the pipe method to send the data to a Writable.

  • The Readable can switch back to paused mode using one of the following:

  • If there are no pipe destinations, by calling the pause method.

  • If there are pipe destinations, by removing all pipe destinations. Multiple pipe destinations may be removed by calling the unpipe method.

The important concept to remember is that a Readable will not generate data until a mechanism for either consuming or ignoring that data is provided. If the consuming mechanism is disabled or taken away, the Readable will attempt to stop generating the data.

For backward compatibility reasons, removing 'data' event handlers will not automatically pause the stream. Also, if there are piped destinations, then calling pause will not guarantee that the stream will remain paused once those destinations drain and ask for more data.

If a Readable is switched into flowing mode and there are no consumers available to handle the data, that data will be lost. This can occur, for instance, when the resume method is called without a listener attached to the 'data' event, or when a 'data' event handler is removed from the stream.

Adding a 'readable' event handler automatically makes the stream stop flowing, and the data has to be consumed via read. If the 'readable' event handler is removed, then the stream will start flowing again if there is a 'data' event handler.

 

Three states

The "two modes" of operation for a Readable stream are a simplified abstraction for the more complicated internal state management that is happening within the Readable stream implementation.

Specifically, at any given point in time, every Readable is in one of three possible states:

  • readable.readableFlowing === null

  • readable.readableFlowing === false

  • readable.readableFlowing === true

When Readable.readableFlowing is null, no mechanism for consuming the stream's data is provided. Therefore, the stream will not generate data. While in this state, attaching a listener for the 'data' event, calling the Readable.pipe method, or calling the Readable.resume method will switch Readable.readableFlowing to true, causing the Readable to begin actively emitting events as data is generated.

Calling Readable.pause, Readable.unpipe, or receiving backpressure will cause the Readable.readableFlowing flag to be set as false, temporarily halting the flowing of events but not halting the generation of data. While in this state, attaching a listener for the 'data' event will not switch Readable.readableFlowing to true.

 

Event: 'close'

The 'close' event is emitted when the stream and any of its underlying resources (a file descriptor, for example) have been closed. The event indicates that no more events will be emitted, and no further computation will occur.

See also

for the corresponding writable stream interface.

Inheritors

Types

Link copied to clipboard

Factory methods for creating Readable streams.

Link copied to clipboard
interface Constructors : ProxyInstantiable, ProxyObject

Describes the type layout of static methods which are made available on the Readable class.

Properties

Link copied to clipboard
abstract val closed: Boolean

Is true after 'close' has been emitted.

Link copied to clipboard
abstract val destroyed: Boolean

Is true after destroy() has been called.

Link copied to clipboard
abstract val errored: Any?

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

Link copied to clipboard
abstract val readable: Boolean

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

Link copied to clipboard
abstract val readableAborted: Boolean

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

Link copied to clipboard
abstract val readableDidRead: Boolean

Returns whether 'data' has been emitted.

Link copied to clipboard
abstract val readableEncoding: String?

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

Link copied to clipboard
abstract val readableEnded: Boolean

Becomes true when 'end' event is emitted.

Link copied to clipboard
abstract val readableFlowing: Boolean?

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

Link copied to clipboard

Returns the value of highWaterMark passed when creating this Readable.

Link copied to clipboard
abstract val readableLength: Int

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.

Link copied to clipboard

Getter for the property objectMode of a given Readable stream.

Functions

Link copied to clipboard
abstract fun addEventListener(type: String, listener: EventListener)
abstract fun addEventListener(type: String, options: AddEventListenerOptions, listener: EventListener)
abstract fun addEventListener(type: String, listener: Value, options: Value?)

Adds a new handler for the type event. Any given listener is added only once per type and per capture option value.

Link copied to clipboard
abstract fun addListener(eventName: String, listener: EventListener)
abstract fun addListener(eventName: String, listener: Value)

Alias for emitter.on(eventName, listener).

Link copied to clipboard
abstract override fun close()
Link copied to clipboard
abstract fun compose(stream: Stream): Duplex
abstract fun compose(stream: Stream, options: ReadableComposeOptions): Duplex
abstract fun compose(stream: Stream, options: Value): Duplex

Compose this stream with another one, to produce a duplex stream.

Link copied to clipboard
abstract override fun destroy()
abstract override fun destroy(error: Throwable)
abstract override fun destroy(error: Value)

Destroy the stream. Optionally emit an error event, and emit a close event (unless emitClose is set to false). After this call, the readable stream will release any internal resources and subsequent calls to push() will be ignored.

Link copied to clipboard
abstract fun dispatchEvent(event: Event): Boolean

Dispatches the event to the list of handlers for Event.type.

Link copied to clipboard
abstract fun drop(limit: Int): Readable
abstract fun drop(limit: Int, options: ReadableDropOptions): Readable
abstract fun drop(limit: Int, options: Value): Readable

Drop the first limit items from this stream.

Link copied to clipboard
abstract fun emit(eventName: String, vararg args: Any?): Boolean

Synchronously calls each of the listeners registered for the event named eventName, in the order they were registered, passing the supplied arguments to each.

Link copied to clipboard
abstract fun eventNames(): List<String>

Returns an array listing the events for which the emitter has registered listeners. The values in the array are strings or Symbols.

Link copied to clipboard
abstract fun every(cbk: () -> Boolean?): JsPromise<Boolean>
abstract fun every(cbk: Value): JsPromise<Boolean>
abstract fun every(options: ReadableEveryOptions, cbk: () -> Boolean?): JsPromise<Boolean>
abstract fun every(cbk: Value, options: Value): JsPromise<Boolean>

Check if all items in this stream match the given predicate.

Link copied to clipboard
abstract fun filter(cbk: (Value) -> Boolean): Readable
abstract fun filter(cbk: Value): Readable
abstract fun filter(options: ReadableMapOptions, cbk: (Value) -> Boolean): Readable
abstract fun filter(cbk: Value, options: Value): Readable

Filter this stream to produce results from a new Readable.

Link copied to clipboard
abstract fun find(cbk: () -> Boolean?): JsPromise<Value>
abstract fun find(cbk: Value): JsPromise<Value>
abstract fun find(options: ReadableFindOptions, cbk: () -> Boolean?): JsPromise<Value>
abstract fun find(cbk: Value, options: Value): JsPromise<Value>

Find the first item in this stream that matches the given predicate.

Link copied to clipboard
abstract fun flatMap(cbk: () -> Iterable<Value>): Readable
abstract fun flatMap(cbk: Value): Readable
abstract fun flatMap(options: ReadableFlatMapOptions, cbk: () -> Iterable<Value>): Readable
abstract fun flatMap(cbk: Value, options: Value): Readable

Map over this stream to produce a new stream of items.

Link copied to clipboard
abstract fun forEach(cbk: (Value) -> Unit): JsPromise<Unit>
abstract fun forEach(cbk: Value): JsPromise<Unit>
abstract fun forEach(options: ReadableForEachOptions, cbk: (Value) -> Unit): JsPromise<Unit>
abstract fun forEach(cbk: Value, options: Value): JsPromise<Unit>

For each item in this stream, call the given callback.

Link copied to clipboard
abstract fun getIterator(): Any
Link copied to clipboard
abstract fun getMaxListeners(): Int

Returns the current max listener value for the emitter.

Link copied to clipboard
abstract fun getMember(key: String): Any
Link copied to clipboard
open override fun getMemberKeys(): Array<String>
Link copied to clipboard
open override fun hasMember(key: String): Boolean
Link copied to clipboard
abstract fun isPaused(): Boolean

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.

Link copied to clipboard
abstract fun iterator()
abstract fun iterator(options: ReadableIteratorOptions)
abstract fun iterator(options: Value)

Get an iterator for this stream.

Link copied to clipboard
abstract fun listenerCount(eventName: String): Int

Returns the number of listeners listening to the event named eventName.

Link copied to clipboard
abstract fun listeners(eventName: String): List<EventListener>

Returns an array listing the listeners for the specified event.

Link copied to clipboard
abstract fun map(cbk: (Value) -> Value): Readable
abstract fun map(cbk: Value): Readable
abstract fun map(options: ReadableMapOptions, cbk: (Any) -> Value): Readable
abstract fun map(cbk: Value, options: Value): Readable

Map over this stream to produce results from a new Readable.

Link copied to clipboard
abstract fun off(eventName: String, listener: Value)

Alias for emitter.removeListener(eventName, listener).

Link copied to clipboard
abstract fun on(eventName: String, listener: EventListener): EventEmitter
abstract fun on(eventName: String, listener: Value): EventEmitter

Adds the listener function to the end of the listeners array for the event named eventName. No checks are made to see if the listener has already been added.

Link copied to clipboard
abstract fun once(eventName: String, listener: EventListener)
abstract fun once(eventName: String, listener: Value)

Adds a one-time listener function for the event named eventName. The next time eventName is triggered, this listener is removed and then invoked.

Link copied to clipboard
abstract fun pause(): Readable

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.

Link copied to clipboard
abstract fun pipe(destination: Writable)
abstract fun pipe(destination: Writable, options: ReadablePipeOptions)
abstract fun pipe(destination: Writable, options: Value)

The readable.pipe() method attaches a Writable stream to the readable, causing it to switch into flowing mode.

Link copied to clipboard
abstract fun prependListener(eventName: String, listener: EventListener)
abstract fun prependListener(eventName: String, listener: Value)

Adds the listener function to the beginning of the listeners array for the event named eventName. No checks are made to see if the listener has already been added.

Link copied to clipboard
abstract fun prependOnceListener(eventName: String, listener: EventListener)
abstract fun prependOnceListener(eventName: String, listener: Value)

Adds a one-time listener function for the event named eventName to the beginning of the listener array. The next time eventName is triggered, this listener is removed, and then invoked.

Link copied to clipboard
open override fun putMember(key: String?, value: Value?)
Link copied to clipboard
abstract fun rawListeners(eventName: String): List<EventListener>

Returns a copy of the array of listeners for the event named eventName, including any wrappers (such as those created by .once()).

Link copied to clipboard
abstract fun read(): StringOrBufferOrAny
abstract fun read(size: Int): StringOrBufferOrAny
abstract fun read(size: Value): StringOrBufferOrAny

The readable.read() method reads some data from the readable stream and returns it. The data will not be returned immediately if the readable stream is in flowing mode; in that case, the data will be buffered internally, and this method will return it when it is available.

Link copied to clipboard
abstract fun reduce(op: () -> Value): JsPromise<Value>
abstract fun reduce(op: Value): JsPromise<Value>
abstract fun reduce(initial: Value, op: () -> Value): JsPromise<Value>
abstract fun reduce(op: Value, initial: Value): JsPromise<Value>
abstract fun reduce(initial: Value, options: ReadableReduceOptions, op: () -> Value): JsPromise<Value>
abstract fun reduce(op: Value, initial: Value, options: Value): JsPromise<Value>

Reduce this stream to a single value.

Link copied to clipboard
abstract fun removeAllListeners()

Removes all listeners, or those of the specified eventName.

abstract fun removeAllListeners(eventName: String)

Removes all listeners, or those of the specified eventName.

Link copied to clipboard
abstract fun removeEventListener(type: String, listener: EventListener)
abstract fun removeEventListener(type: String, listener: Value)
abstract fun removeEventListener(type: String, listener: EventListener, options: RemoveEventListenerOptions)
abstract fun removeEventListener(type: String, listener: EventListener, options: Value)
abstract fun removeEventListener(type: String, listener: Value, options: Value)

Removes the listener from the list of handlers for event type.

Link copied to clipboard
abstract fun removeListener(eventName: String, listener: Value)

Removes the specified listener from the listener array for the event named eventName.

Link copied to clipboard
open override fun removeMember(key: String?): Boolean
Link copied to clipboard
abstract fun resume(): Readable

The readable.resume() method will cause a stream in paused mode to switch back into flowing mode. This will cause the stream to start emitting data events.

Link copied to clipboard
abstract fun setEncoding(encoding: String)

The readable.setEncoding() method sets the encoding for the Readable stream. This method must be called before the first 'data' event is emitted by the stream.

Link copied to clipboard
abstract fun setMaxListeners(count: Int)

Sets the maximum number of listeners for the emitter.

Link copied to clipboard
abstract fun some(cbk: () -> Boolean?): JsPromise<Boolean>
abstract fun some(cbk: Value): JsPromise<Boolean>
abstract fun some(options: ReadableSomeOptions, cbk: () -> Boolean?): JsPromise<Boolean>
abstract fun some(cbk: Value, options: Value): JsPromise<Boolean>

Check if any item in this stream matches the given predicate.

Link copied to clipboard
abstract fun take(limit: Int): Readable
abstract fun take(limit: Int, options: ReadableDropOptions): Readable
abstract fun take(limit: Int, options: Value): Readable

Take the first limit items from this stream.

Link copied to clipboard
abstract fun toArray(): JsPromise<Array<Value>>
abstract fun toArray(options: ReadableToArrayOptions): JsPromise<Array<Value>>
abstract fun toArray(options: Value): JsPromise<Array<Value>>

Collect all items in this stream into an array.

Link copied to clipboard
abstract fun unpipe()
abstract fun unpipe(destination: Writable)

The readable.unpipe() method detaches a Writable stream previously attached using readable.pipe(). The method will remove the destination from the list of destinations to which data will be written.

Link copied to clipboard
abstract fun unshift(chunk: StreamChunk)
abstract fun unshift(chunk: Value)
abstract fun unshift(chunk: StreamChunk, encoding: String)

The readable.unshift() method pushes a chunk of data back into the internal buffer for the stream.

Link copied to clipboard
abstract fun wrap(stream: Stream)

The readable.wrap() method is used to wrap a raw stream source (like a TCP socket) in a Readable stream interface.