Bare Docs
ReferenceBareModules

bare-stream

Streaming data for JavaScript

Documented against v2.13.4
stable

bare-stream — Streaming data for JavaScript.

Mirrors the Node.js stream module: the classic Readable/Writable/Duplex/Transform classes are backed by streamx, while bare-stream/web provides a node:stream/web-compatible implementation of the WHATWG Streams Standard.

npm i bare-stream

Usage

const { Readable } = require('bare-stream')

const stream = new Readable({
  read(size) {
    // Push data, then push `null` to signal the end
    this.push('hello')
    this.push('world')
    this.push(null)
  }
})

stream.on('data', (data) => console.log(data))

API

Stream basics

Stream._destroy(err: Error | null, cb: StreamCallback): void

Parameters

ParameterTypeDefaultDescription
errError | null—The error the stream is being destroyed with, or null for a clean destroy.
cbStreamCallback—Called with an error, or null on success.

Stream._open(cb: StreamCallback): void

Parameters

ParameterTypeDefaultDescription
cbStreamCallback—Called with an error, or null, once opening finishes.

Stream._predestroy(): void

Stream.destroy(err?: Error | null): void

Parameters

ParameterTypeDefaultDescription
err?Error | null—The error to destroy the stream with; omit or pass null for a clean destroy.

Stream.destroyed: boolean

Stream.destroying: boolean

Stream.readable: boolean

Stream.writable: boolean

Stream utilities

Stream.addAbortSignal<S extends Stream>(signal: AbortSignal, stream: S): S

Destroy stream when signal aborts, using signal.reason as the destruction error. Returns stream.

Parameters

ParameterTypeDefaultDescription
signalAbortSignal—The AbortSignal that destroys stream on abort.
streamS—The stream to destroy when signal aborts.

Stream.duplexPair(opts?: DuplexOptions): [Duplex, Duplex]

Create a pair of linked Duplex streams. Data written to a is readable from b and vice versa. options are passed to each side.

Parameters

ParameterTypeDefaultDescription
opts?DuplexOptions—Passed to both ends of the pair.

Stream.finished(stream: Stream, opts: { cleanup?: boolean }, cb: StreamCallback): () => void

Invoke cb once stream is no longer readable or writable, or has errored. Returns a function that detaches the listeners.

Overloads:

Stream.finished(stream: Stream, opts: { cleanup?: boolean }, cb: StreamCallback): () => void
Stream.finished(stream: Stream, cb: StreamCallback): () => void

Parameters

ParameterTypeDefaultDescription
streamStream—The stream to wait on.
opts{ cleanup?: boolean }—Set cleanup: true to detach the listeners automatically once cb runs.
cbStreamCallback—Called with an error, or null, once stream finishes.

Stream.getStreamError(stream: Stream, opts?: { all?: boolean }): Error | null

Return the error a stream was destroyed with, or null.

Parameters

ParameterTypeDefaultDescription
streamStream—The stream to inspect.
opts?{ all?: boolean }——

Stream.isDisturbed(stream: Stream): boolean

Parameters

ParameterTypeDefaultDescription
streamStream—The stream to test.

Stream.isEnded(stream: Stream): boolean

Parameters

ParameterTypeDefaultDescription
streamStream—The stream to test.

Stream.isErrored(stream: Stream): boolean

Parameters

ParameterTypeDefaultDescription
streamStream—The stream to test.

Stream.isFinished(stream: Stream): boolean

Parameters

ParameterTypeDefaultDescription
streamStream—The stream to test.

Stream.isReadable(stream: Stream): boolean

Parameters

ParameterTypeDefaultDescription
streamStream—The stream to test.

Stream.isStream(stream: unknown): stream is Stream

Parameters

ParameterTypeDefaultDescription
streamunknown—The stream to test.

Stream.isWritable(stream: Stream): boolean

Return true if stream is writable.

Parameters

ParameterTypeDefaultDescription
streamStream—The stream to test.

Stream.pipeline<S extends Writable>(streams: Pipeline<S>, cb?: StreamCallback): S

Pipe a series of streams together, propagating errors and cleaning up on completion. streams is a Readable source, zero or more Duplex transforms, and a Writable destination. Returns the destination stream. cb is called when the pipeline finishes or errors.

Overloads:

Stream.pipeline<S extends Writable>(streams: Pipeline<S>, cb?: StreamCallback): S
Stream.pipeline<S extends Writable>(...args: Pipeline<S>): S
Stream.pipeline<S extends Writable>(...args: [...Pipeline<S>, cb: StreamCallback]): S

Parameters

ParameterTypeDefaultDescription
streamsPipeline<S>—A Readable source, zero or more Duplex transforms, and a Writable destination.
cb?StreamCallback—Called with an error, or null, once the pipeline finishes or errors.

Readable streams

new Readable(opts?: ReadableOptions)

A readable stream.

Parameters

ParameterTypeDefaultDescription
opts?ReadableOptions——

Readable._read(size: number): void

Parameters

ParameterTypeDefaultDescription
sizenumber——

Readable.closed: boolean

true when the stream is no longer readable.

Readable.errored: Error | null

The error the stream was destroyed with, or null.

Readable.pause(): this

Readable.pipe<S extends Writable>(dest: S, cb?: StreamCallback): S

Parameters

ParameterTypeDefaultDescription
destS—The destination stream to write into.
cb?StreamCallback—Called with an error, or null on success.

Readable.push(data: unknown | null, encoding?: BufferEncoding): boolean

Push data into the stream's internal buffer. If data is a string, it is encoded to a Buffer using encoding, defaulting to 'utf8'. Push null to signal the end of the stream.

Parameters

ParameterTypeDefaultDescription
dataunknown | null—Data to add to the buffer, or null to end the stream.
encoding?BufferEncoding—Encoding used to convert a string data to a Buffer; defaults to 'utf8'.

Readable.read(): unknown | null

Readable.from

Readable.from(data: unknown | unknown[] | AsyncIterable<unknown>, opts?: ReadableOptions): Readable

Create a readable stream from data, which may be a value, an array of values, or an async iterable.

Parameters

ParameterTypeDefaultDescription
dataunknown | unknown[] | AsyncIterable<unknown>—A value, array of values, or async iterable to read from.
opts?ReadableOptions——

Readable.fromWeb(readableStream: ReadableStream, opts?: ReadableFromWebOptions): Readable

Convert a web ReadableStream into a Readable.

Parameters

ParameterTypeDefaultDescription
readableStreamReadableStream—The web ReadableStream to convert.
opts?ReadableFromWebOptions—Options for the conversion; supports encoding and signal, matching ReadableOptions.

Readable.isBackpressured(rs: Readable): boolean

Parameters

ParameterTypeDefaultDescription
rsReadable—The stream to check.

Readable.isPaused(rs: Readable): boolean

Parameters

ParameterTypeDefaultDescription
rsReadable—The stream to check.

Readable.toWeb(readable: Readable, opts?: ReadableToWebOptions): ReadableStream

Convert a Readable into a web ReadableStream.

Parameters

ParameterTypeDefaultDescription
readableReadable—The Readable to convert.
opts?ReadableToWebOptions—Options for the conversion; strategy is a custom queuing strategy passed through to the ReadableStream constructor.

Readable.resume(): this

Readable.setEncoding(encoding: BufferEncoding): void

Parameters

ParameterTypeDefaultDescription
encodingBufferEncoding—Encoding used to decode emitted data to strings.

Readable.unshift(data: unknown | null, encoding?: BufferEncoding): boolean

Like stream.push() but prepends data to the internal buffer so it is read before any already buffered data.

Parameters

ParameterTypeDefaultDescription
dataunknown | null—Data to prepend to the buffer, or null to end the stream.
encoding?BufferEncoding—Encoding used to convert a string data to a Buffer; defaults to 'utf8'.

Writable streams

new Writable(opts?: WritableOptions)

A writable stream.

Parameters

ParameterTypeDefaultDescription
opts?WritableOptions——

Writable._final(cb: StreamCallback): void

Parameters

ParameterTypeDefaultDescription
cbStreamCallback—Called with an error, or null on success.

Writable._write(data: unknown, encoding: StreamEncoding, cb: StreamCallback): void

Parameters

ParameterTypeDefaultDescription
dataunknown—The chunk to write.
encodingStreamEncoding—Encoding of data, or 'buffer' if it is not a string.
cbStreamCallback—Called with an error, or null on success.

Writable._writev(batch: { chunk: unknown; encoding: StreamEncoding }[], cb: StreamCallback): void

Parameters

ParameterTypeDefaultDescription
batch{ chunk: unknown; encoding: StreamEncoding }[]—Queued chunks to write, each with its chunk and encoding.
cbStreamCallback—Called with an error, or null on success.

Writable.closed: boolean

true when the stream is no longer writable.

Writable.cork(): void

Writable.end(cb?: StreamCallback): this

Signal that no more data will be written. If data is provided it is written first. The optional cb is called once the stream has finished.

Overloads:

end(cb?: StreamCallback): this
end(data: unknown, encoding?: BufferEncoding, cb?: StreamCallback): this
end(data: unknown, cb?: StreamCallback): this

Parameters

ParameterTypeDefaultDescription
cb?StreamCallback—Called with an error, or null, once the stream has finished.

Writable.errored: Error | null

The error the stream was destroyed with, or null.

Writable.uncork(): void

Writable.drained(ws: Writable): Promise<boolean>

Returns a promise that resolves once the stream has drained.

Parameters

ParameterTypeDefaultDescription
wsWritable—The stream to wait on.

Writable.fromWeb(writableStream: WritableStream, opts?: WritableFromWebOptions): Writable

Convert a web WritableStream into a Writable.

Parameters

ParameterTypeDefaultDescription
writableStreamWritableStream—The web WritableStream to convert.
opts?WritableFromWebOptions—Options for the conversion; supports signal, matching WritableOptions.

Writable.isBackpressured(ws: Writable): boolean

Parameters

ParameterTypeDefaultDescription
wsWritable—The stream to check.

Writable.toWeb(writable: Writable): WritableStream

Convert a Writable into a web WritableStream.

Parameters

ParameterTypeDefaultDescription
writableWritable—The Writable to convert.

Writable.write(data: unknown, encoding?: BufferEncoding, cb?: StreamCallback): boolean

Write data to the stream. If data is a string, it is encoded using encoding, defaulting to 'utf8'. Returns false if the stream is backpressured. The optional cb is called once the write has drained.

Overloads:

write(data: unknown, encoding?: BufferEncoding, cb?: StreamCallback): boolean
write(data: unknown, cb?: StreamCallback): boolean

Parameters

ParameterTypeDefaultDescription
dataunknown—Data to write. If a string, it is encoded using encoding.
encoding?BufferEncoding—Encoding used to convert a string data to a Buffer; defaults to 'utf8'.
cb?StreamCallback—Called with an error, or null, once the write has drained.

Duplex and Transform streams

new Duplex(opts?: DuplexOptions)

A stream that is both readable and writable. Accepts the combined options of Readable and Writable.

Parameters

ParameterTypeDefaultDescription
opts?DuplexOptions——

Duplex.fromWeb

Duplex.fromWeb({ readable: ReadableStream, writable: Writable }, opts?: DuplexFromWebOptions): Readable

Convert a pair of web ReadableStream and WritableStream into a Duplex.

Parameters

ParameterTypeDefaultDescription
{ readable: ReadableStream, writable: Writable }any—Options for the conversion; combines the Readable and Writable conversion options (encoding, signal).
opts?DuplexFromWebOptions—Options for the conversion; combines the Readable and Writable conversion options (encoding, signal).

Duplex.toWeb(readable: Readable, opts?: ReadableToWebOptions): ReadableStream

Parameters

ParameterTypeDefaultDescription
readableReadable——
opts?ReadableToWebOptions——

new Transform(opts?: TransformOptions)

A duplex stream where output is computed from input.

Parameters

ParameterTypeDefaultDescription
opts?TransformOptions——

_flush(cb: StreamCallback): void

Parameters

ParameterTypeDefaultDescription
cbStreamCallback—Called with an error, or null, once flushing finishes.

_transform(data: unknown, encoding: StreamEncoding, cb: StreamCallback): void

Parameters

ParameterTypeDefaultDescription
dataunknown—The chunk to transform.
encodingStreamEncoding—Encoding of data, or 'buffer' if it is not a string.
cbStreamCallback—Called with an error, or null on success.

Events

StreamEvents

interface StreamEvents {
  close: []
  error: [err: Error]
}

ReadableEvents

interface ReadableEvents {
  data: [data: unknown]
  end: []
  readable: []
  piping: [dest: Writable]
  close: []
  error: [err: Error]
}

WritableEvents

interface WritableEvents {
  drain: []
  finish: []
  pipe: [src: Readable]
  close: []
  error: [err: Error]
}

DuplexEvents

interface DuplexEvents {
  data: [data: unknown]
  end: []
  readable: []
  piping: [dest: Writable]
  close: []
  error: [err: Error]
  drain: []
  finish: []
  pipe: [src: Readable]
}

TransformEvents

interface TransformEvents {
  data: [data: unknown]
  end: []
  readable: []
  piping: [dest: Writable]
  close: []
  error: [err: Error]
  drain: []
  finish: []
  pipe: [src: Readable]
}

Options

StreamOptions

interface StreamOptions<S extends Stream = Stream> {
  eagerOpen?: boolean
  highWaterMark?: number
  signal?: AbortSignal
  open?(this: S, cb: StreamCallback): void
  predestroy?(this: S): void
  destroy?(this: S, err: Error | null, cb: StreamCallback): void
}

ReadableOptions

interface ReadableOptions<S extends Readable = Readable> {
  encoding?: BufferEncoding
  read?(this: S, size: number): void
  eagerOpen?: boolean
  highWaterMark?: number
  signal?: AbortSignal
  open?(this: S, cb: StreamCallback): void
  predestroy?(this: S): void
  destroy?(this: S, err: Error | null, cb: StreamCallback): void
}

WritableOptions

interface WritableOptions<S extends Writable = Writable> {
  write?(this: S, data: unknown, encoding: StreamEncoding, cb: StreamCallback): void
  writev?(this: S, batch: { chunk: unknown; encoding: StreamEncoding }[], cb: StreamCallback): void
  final?(this: S, cb: StreamCallback): void
  eagerOpen?: boolean
  highWaterMark?: number
  signal?: AbortSignal
  open?(this: S, cb: StreamCallback): void
  predestroy?(this: S): void
  destroy?(this: S, err: Error | null, cb: StreamCallback): void
}

DuplexOptions

interface DuplexOptions<S extends Duplex = Duplex> {
  encoding?: BufferEncoding
  read?(this: S, size: number): void
  eagerOpen?: boolean
  highWaterMark?: number
  signal?: AbortSignal
  open?(this: S, cb: StreamCallback): void
  predestroy?(this: S): void
  destroy?(this: S, err: Error | null, cb: StreamCallback): void
  write?(this: S, data: unknown, encoding: StreamEncoding, cb: StreamCallback): void
  writev?(this: S, batch: { chunk: unknown; encoding: StreamEncoding }[], cb: StreamCallback): void
  final?(this: S, cb: StreamCallback): void
}

TransformOptions

interface TransformOptions<S extends Transform = Transform> {
  transform?(this: S, data: unknown, encoding: StreamEncoding, cb: StreamCallback): void
  flush?(this: S, cb: StreamCallback): void
  encoding?: BufferEncoding
  read?(this: S, size: number): void
  eagerOpen?: boolean
  highWaterMark?: number
  signal?: AbortSignal
  open?(this: S, cb: StreamCallback): void
  predestroy?(this: S): void
  destroy?(this: S, err: Error | null, cb: StreamCallback): void
  write?(this: S, data: unknown, encoding: StreamEncoding, cb: StreamCallback): void
  writev?(this: S, batch: { chunk: unknown; encoding: StreamEncoding }[], cb: StreamCallback): void
  final?(this: S, cb: StreamCallback): void
}

Readable

Readable._destroy(err: Error | null, cb: StreamCallback): void

Parameters

ParameterTypeDefaultDescription
errError | null—The error the stream is being destroyed with, or null for a clean destroy.
cbStreamCallback—Called with an error, or null on success.

Readable._open(cb: StreamCallback): void

Parameters

ParameterTypeDefaultDescription
cbStreamCallback—Called with an error, or null, once opening finishes.

Readable._predestroy(): void

Readable.destroy(err?: Error | null): void

Parameters

ParameterTypeDefaultDescription
err?Error | null—The error to destroy the stream with; omit or pass null for a clean destroy.

Readable.destroyed: boolean

Readable.destroying: boolean

Readable.readable: boolean

Readable.writable: boolean

Writable

Writable._destroy(err: Error | null, cb: StreamCallback): void

Parameters

ParameterTypeDefaultDescription
errError | null—The error the stream is being destroyed with, or null for a clean destroy.
cbStreamCallback—Called with an error, or null on success.

Writable._open(cb: StreamCallback): void

Parameters

ParameterTypeDefaultDescription
cbStreamCallback—Called with an error, or null, once opening finishes.

Writable._predestroy(): void

Writable.destroy(err?: Error | null): void

Parameters

ParameterTypeDefaultDescription
err?Error | null—The error to destroy the stream with; omit or pass null for a clean destroy.

Writable.destroyed: boolean

Writable.destroying: boolean

Writable.readable: boolean

Writable.writable: boolean

Duplex

Duplex._destroy(err: Error | null, cb: StreamCallback): void

Parameters

ParameterTypeDefaultDescription
errError | null—The error the stream is being destroyed with, or null for a clean destroy.
cbStreamCallback—Called with an error, or null on success.

Duplex._final(cb: StreamCallback): void

Parameters

ParameterTypeDefaultDescription
cbStreamCallback—Called with an error, or null on success.

Duplex._open(cb: StreamCallback): void

Parameters

ParameterTypeDefaultDescription
cbStreamCallback—Called with an error, or null, once opening finishes.

Duplex._predestroy(): void

Duplex._read(size: number): void

Parameters

ParameterTypeDefaultDescription
sizenumber——

Duplex._write(data: unknown, encoding: StreamEncoding, cb: StreamCallback): void

Parameters

ParameterTypeDefaultDescription
dataunknown—The chunk to write.
encodingStreamEncoding—Encoding of data, or 'buffer' if it is not a string.
cbStreamCallback—Called with an error, or null on success.

Duplex._writev(batch: { chunk: unknown; encoding: StreamEncoding }[], cb: StreamCallback): void

Parameters

ParameterTypeDefaultDescription
batch{ chunk: unknown; encoding: StreamEncoding }[]—Queued chunks to write, each with its chunk and encoding.
cbStreamCallback—Called with an error, or null on success.

Duplex.closed: boolean

true when the stream is no longer readable.

Duplex.cork(): void

Duplex.destroy(err?: Error | null): void

Parameters

ParameterTypeDefaultDescription
err?Error | null—The error to destroy the stream with; omit or pass null for a clean destroy.

Duplex.destroyed: boolean

Duplex.destroying: boolean

Duplex.end(cb?: StreamCallback): this

Signal that no more data will be written. If data is provided it is written first. The optional cb is called once the stream has finished.

Overloads:

end(cb?: StreamCallback): this
end(data: unknown, encoding?: BufferEncoding, cb?: StreamCallback): this
end(data: unknown, cb?: StreamCallback): this

Parameters

ParameterTypeDefaultDescription
cb?StreamCallback—Called with an error, or null, once the stream has finished.

Duplex.errored: Error | null

The error the stream was destroyed with, or null.

Duplex.pause(): this

Duplex.pipe<S extends Writable>(dest: S, cb?: StreamCallback): S

Parameters

ParameterTypeDefaultDescription
destS—The destination stream to write into.
cb?StreamCallback—Called with an error, or null on success.

Duplex.push(data: unknown | null, encoding?: BufferEncoding): boolean

Push data into the stream's internal buffer. If data is a string, it is encoded to a Buffer using encoding, defaulting to 'utf8'. Push null to signal the end of the stream.

Parameters

ParameterTypeDefaultDescription
dataunknown | null—Data to add to the buffer, or null to end the stream.
encoding?BufferEncoding—Encoding used to convert a string data to a Buffer; defaults to 'utf8'.

Duplex.read(): unknown | null

Duplex.readable: boolean

Duplex.resume(): this

Duplex.setEncoding(encoding: BufferEncoding): void

Parameters

ParameterTypeDefaultDescription
encodingBufferEncoding—Encoding used to decode emitted data to strings.

Duplex.uncork(): void

Duplex.unshift(data: unknown | null, encoding?: BufferEncoding): boolean

Like stream.push() but prepends data to the internal buffer so it is read before any already buffered data.

Parameters

ParameterTypeDefaultDescription
dataunknown | null—Data to prepend to the buffer, or null to end the stream.
encoding?BufferEncoding—Encoding used to convert a string data to a Buffer; defaults to 'utf8'.

Duplex.writable: boolean

Duplex.write(data: unknown, encoding?: BufferEncoding, cb?: StreamCallback): boolean

Write data to the stream. If data is a string, it is encoded using encoding, defaulting to 'utf8'. Returns false if the stream is backpressured. The optional cb is called once the write has drained.

Overloads:

write(data: unknown, encoding?: BufferEncoding, cb?: StreamCallback): boolean
write(data: unknown, cb?: StreamCallback): boolean

Parameters

ParameterTypeDefaultDescription
dataunknown—Data to write. If a string, it is encoded using encoding.
encoding?BufferEncoding—Encoding used to convert a string data to a Buffer; defaults to 'utf8'.
cb?StreamCallback—Called with an error, or null, once the write has drained.

Transform

Transform._destroy(err: Error | null, cb: StreamCallback): void

Parameters

ParameterTypeDefaultDescription
errError | null—The error the stream is being destroyed with, or null for a clean destroy.
cbStreamCallback—Called with an error, or null on success.

Transform._final(cb: StreamCallback): void

Parameters

ParameterTypeDefaultDescription
cbStreamCallback—Called with an error, or null on success.

Transform._open(cb: StreamCallback): void

Parameters

ParameterTypeDefaultDescription
cbStreamCallback—Called with an error, or null, once opening finishes.

Transform._predestroy(): void

Transform._read(size: number): void

Parameters

ParameterTypeDefaultDescription
sizenumber——

Transform._write(data: unknown, encoding: StreamEncoding, cb: StreamCallback): void

Parameters

ParameterTypeDefaultDescription
dataunknown—The chunk to write.
encodingStreamEncoding—Encoding of data, or 'buffer' if it is not a string.
cbStreamCallback—Called with an error, or null on success.

Transform._writev(batch: { chunk: unknown; encoding: StreamEncoding }[], cb: StreamCallback): void

Parameters

ParameterTypeDefaultDescription
batch{ chunk: unknown; encoding: StreamEncoding }[]—Queued chunks to write, each with its chunk and encoding.
cbStreamCallback—Called with an error, or null on success.

Transform.closed: boolean

true when the stream is no longer readable.

Transform.cork(): void

Transform.destroy(err?: Error | null): void

Parameters

ParameterTypeDefaultDescription
err?Error | null—The error to destroy the stream with; omit or pass null for a clean destroy.

Transform.destroyed: boolean

Transform.destroying: boolean

Transform.end(cb?: StreamCallback): this

Signal that no more data will be written. If data is provided it is written first. The optional cb is called once the stream has finished.

Overloads:

end(cb?: StreamCallback): this
end(data: unknown, encoding?: BufferEncoding, cb?: StreamCallback): this
end(data: unknown, cb?: StreamCallback): this

Parameters

ParameterTypeDefaultDescription
cb?StreamCallback—Called with an error, or null, once the stream has finished.

Transform.errored: Error | null

The error the stream was destroyed with, or null.

Transform.pause(): this

Transform.pipe<S extends Writable>(dest: S, cb?: StreamCallback): S

Parameters

ParameterTypeDefaultDescription
destS—The destination stream to write into.
cb?StreamCallback—Called with an error, or null on success.

Transform.push(data: unknown | null, encoding?: BufferEncoding): boolean

Push data into the stream's internal buffer. If data is a string, it is encoded to a Buffer using encoding, defaulting to 'utf8'. Push null to signal the end of the stream.

Parameters

ParameterTypeDefaultDescription
dataunknown | null—Data to add to the buffer, or null to end the stream.
encoding?BufferEncoding—Encoding used to convert a string data to a Buffer; defaults to 'utf8'.

Transform.read(): unknown | null

Transform.readable: boolean

Transform.resume(): this

Transform.setEncoding(encoding: BufferEncoding): void

Parameters

ParameterTypeDefaultDescription
encodingBufferEncoding—Encoding used to decode emitted data to strings.

Transform.uncork(): void

Transform.unshift(data: unknown | null, encoding?: BufferEncoding): boolean

Like stream.push() but prepends data to the internal buffer so it is read before any already buffered data.

Parameters

ParameterTypeDefaultDescription
dataunknown | null—Data to prepend to the buffer, or null to end the stream.
encoding?BufferEncoding—Encoding used to convert a string data to a Buffer; defaults to 'utf8'.

Transform.writable: boolean

Transform.write(data: unknown, encoding?: BufferEncoding, cb?: StreamCallback): boolean

Write data to the stream. If data is a string, it is encoded using encoding, defaulting to 'utf8'. Returns false if the stream is backpressured. The optional cb is called once the write has drained.

Overloads:

write(data: unknown, encoding?: BufferEncoding, cb?: StreamCallback): boolean
write(data: unknown, cb?: StreamCallback): boolean

Parameters

ParameterTypeDefaultDescription
dataunknown—Data to write. If a string, it is encoded using encoding.
encoding?BufferEncoding—Encoding used to convert a string data to a Buffer; defaults to 'utf8'.
cb?StreamCallback—Called with an error, or null, once the write has drained.

Types

StreamCallback

interface StreamCallback {
}

ReadableFromWebOptions

interface ReadableFromWebOptions {
  encoding?: BufferEncoding
  signal?: AbortSignal
}

ReadableToWebOptions

interface ReadableToWebOptions {
  strategy?: CustomQueuingStrategy
}

WritableFromWebOptions

interface WritableFromWebOptions {
  signal?: AbortSignal
}

DuplexFromWebOptions

interface DuplexFromWebOptions {
  encoding?: BufferEncoding
  signal?: AbortSignal
}

bare-stream/web

ReadableStreamDefaultReader

new ReadableStreamDefaultReader(stream: ReadableStream)

A reader over a ReadableStream, exposing closed, read(), releaseLock(), and cancel([reason]).

Parameters

ParameterTypeDefaultDescription
streamReadableStream—The ReadableStream to read from.

ReadableStreamDefaultReader.cancel(reason?: unknown): Promise<void>

Parameters

ParameterTypeDefaultDescription
reason?unknown—Reason for the cancellation, passed to the stream's destroy(); defaults to a TypeError if omitted.

ReadableStreamDefaultReader.closed: Promise<void>

read(): Promise<{ value: unknown; done: boolean }>

Returns Promise<{ value: unknown; done: boolean }> — Resolves with the next chunk as { value, done: false }, or { value: undefined, done: true } once the stream ends; rejects with the stream's error if the stream is errored.

ReadableStreamDefaultReader.releaseLock(): void

ReadableStreamDefaultController

new ReadableStreamDefaultController(stream: ReadableStream)

The controller passed to start and pull, exposing desiredSize, enqueue(data), close(), and error([err]).

Parameters

ParameterTypeDefaultDescription
streamReadableStream—The ReadableStream the controller manages.

close(): void

ReadableStreamDefaultController.desiredSize: number

ReadableStreamDefaultController.enqueue(data: unknown): void

Parameters

ParameterTypeDefaultDescription
dataunknown—The chunk to enqueue.

ReadableStreamDefaultController.error(error?: unknown): void

Parameters

ParameterTypeDefaultDescription
error?unknown—The error to destroy the stream with.

ReadableStream

new ReadableStream(underlyingSource?: UnderlyingSource, queuingStrategy?: CustomQueuingStrategy)

A web readable stream. underlyingSource may provide start, pull, and cancel methods, or be an existing streamx stream to wrap. queuingStrategy defaults to a CountQueuingStrategy.

Parameters

ParameterTypeDefaultDescription
underlyingSource?UnderlyingSource—May provide start, pull, and cancel methods, or be an existing streamx stream to wrap.
queuingStrategy?CustomQueuingStrategy—Defaults to a CountQueuingStrategy if omitted.

ReadableStream.cancel(reason?: unknown): Promise<void>

Parameters

ParameterTypeDefaultDescription
reason?unknown—Reason for the cancellation, passed to the stream's destroy(); defaults to a TypeError if omitted.

getReader(): ReadableStreamDefaultReader

Acquire a ReadableStreamDefaultReader. Throws if the stream is already locked.

Throws

  • TypeError — thrown if the stream already has an active reader (locked is true).

ReadableStream.locked: boolean

pipeTo(destination: WritableStream): Promise<void>

Pipe the stream to a WritableStream, returning a promise that resolves once piping completes.

Parameters

ParameterTypeDefaultDescription
destinationWritableStream—The WritableStream to pipe into.

ReadableStream.from(iterable: unknown | unknown[] | AsyncIterable<unknown>): ReadableStream

Create a ReadableStream from an iterable or async iterable.

Parameters

ParameterTypeDefaultDescription
iterableunknown | unknown[] | AsyncIterable<unknown>—A value, array of values, or async iterable to read from.

tee(): [ReadableStream, ReadableStream]

Split the stream into two independent ReadableStream branches.

QueuingStrategy

new QueuingStrategy(opts?: QueuingStrategyOptions)

Parameters

ParameterTypeDefaultDescription
opts?QueuingStrategyOptions——

highWaterMark: number

size(chunk: unknown): number

Parameters

ParameterTypeDefaultDescription
chunkunknown—The chunk to measure.

WritableStreamDefaultWriter

new WritableStreamDefaultWriter(stream: WritableStream)

A writer over a WritableStream, exposing desiredSize, closed, ready, write(chunk), releaseLock(), close(), and abort([reason]).

Parameters

ParameterTypeDefaultDescription
streamWritableStream—The WritableStream to write to.

WritableStreamDefaultWriter.abort(reason?: unknown): Promise<void>

Parameters

ParameterTypeDefaultDescription
reason?unknown—Reason for the abort, passed to the stream's destroy(); defaults to a TypeError if omitted.

WritableStreamDefaultWriter.close(): Promise<void>

Returns Promise<void> — Resolves once the stream has finished closing.

WritableStreamDefaultWriter.closed: Promise<void>

WritableStreamDefaultWriter.desiredSize: number

ready: Promise<void>

WritableStreamDefaultWriter.releaseLock(): void

write(chunk: unknown): Promise<void>

Parameters

ParameterTypeDefaultDescription
chunkunknown—The chunk to write.

Returns Promise<void> — Resolves once chunk has been written and the stream has drained; rejects with the stream's error if the stream is or becomes errored.

WritableStreamDefaultController

new WritableStreamDefaultController(stream: WritableStream)

The controller passed to start and write, exposing error([err]).

Parameters

ParameterTypeDefaultDescription
streamWritableStream—The WritableStream the controller manages.

error(err?: unknown): void

Parameters

ParameterTypeDefaultDescription
err?unknown—The error to destroy the stream with.

WritableStream

new WritableStream(underlyingSink?: UnderlyingSink, queuingStrategy?: CustomQueuingStrategy)

A web writable stream. underlyingSink may provide start, write, close, and abort methods, or be an existing streamx stream to wrap.

Parameters

ParameterTypeDefaultDescription
underlyingSink?UnderlyingSink—May provide start, write, close, and abort methods, or be an existing streamx stream to wrap.
queuingStrategy?CustomQueuingStrategy——

WritableStream.abort(reason?: unknown): Promise<void>

Parameters

ParameterTypeDefaultDescription
reason?unknown—Reason for the abort, passed to the stream's destroy(); defaults to a TypeError if omitted.

WritableStream.close(): Promise<void>

Returns Promise<void> — Resolves once the stream has finished closing.

getWriter(): WritableStreamDefaultWriter

Acquire a WritableStreamDefaultWriter. Throws if the stream is already locked.

Throws

  • TypeError — thrown if the stream already has an active writer (locked is true).

WritableStream.locked: boolean

TransformStreamDefaultController

new TransformStreamDefaultController(stream: TransformStream)

The controller passed to start, transform, and flush, exposing desiredSize, enqueue(data), error([err]), and terminate().

Parameters

ParameterTypeDefaultDescription
streamTransformStream—The TransformStream the controller manages.

TransformStreamDefaultController.desiredSize: number

TransformStreamDefaultController.enqueue(data: unknown): void

Parameters

ParameterTypeDefaultDescription
dataunknown—The chunk to enqueue.

TransformStreamDefaultController.error(error?: unknown): void

Parameters

ParameterTypeDefaultDescription
error?unknown—The error to destroy the stream with.

terminate(): void

TransformStream

TransformStream

new TransformStream(transformer?: Transformer, writableStrategy?: CustomQueuingStrategy, readableStrategy?: CustomQueuingStrategy)

A web transform stream. transformer may provide start, transform, and flush methods. Exposes readable and writable properties.

Parameters

ParameterTypeDefaultDescription
transformer?Transformer—May provide start, transform, and flush methods.
writableStrategy?CustomQueuingStrategy——
readableStrategy?CustomQueuingStrategy——

readable: ReadableStream

writable: WritableStream

Functions

isReadableStream(value: unknown): value is ReadableStream

Parameters

ParameterTypeDefaultDescription
valueunknown—The value to test.

isReadableStreamErrored(stream: ReadableStream): boolean

Parameters

ParameterTypeDefaultDescription
streamReadableStream—The stream to test.

isReadableStreamDisturbed(stream: ReadableStream): boolean

Parameters

ParameterTypeDefaultDescription
streamReadableStream—The stream to test.

isWritableStream(value: unknown): value is WritableStream

Parameters

ParameterTypeDefaultDescription
valueunknown—The value to test.

isTransformStream(value: unknown): value is TransformStream

Return true if value is a web TransformStream.

Parameters

ParameterTypeDefaultDescription
valueunknown—The value to test.

Types

UnderlyingSource

interface UnderlyingSource<S extends ReadableStream = ReadableStream> {
  start?(this: S, controller: ReadableStreamDefaultController): void
  pull?(this: S, controller: ReadableStreamDefaultController): void
  cancel?(this: S, reason?: unknown): void
}

CustomQueuingStrategy

interface CustomQueuingStrategy {
  highWaterMark?: number
  size?: (chunk: unknown) => number
}

QueuingStrategyOptions

interface QueuingStrategyOptions {
  highWaterMark?: number
}

UnderlyingSink

interface UnderlyingSink<S extends WritableStream = WritableStream> {
  start?(this: S, controller: WritableStreamDefaultController): void
  write?(this: S, chunk: unknown, controller: WritableStreamDefaultController): void
  close?(this: S): void
  abort?(this: S, reason?: unknown): void
}

Transformer

interface Transformer<S extends TransformStream = TransformStream> {
  start?(this: S, controller: TransformStreamDefaultController): void
  transform?(this: S, chunk: unknown, controller: TransformStreamDefaultController): void
  flush?(this: S, controller: TransformStreamDefaultController): void
}

Classes

CountQueuingStrategy

class CountQueuingStrategy {
}

ByteLengthQueuingStrategy

class ByteLengthQueuingStrategy {
}

See also

On this page

Usage
API
Stream basics
Stream._destroy(err: Error | null, cb: StreamCallback): void
Stream._open(cb: StreamCallback): void
Stream._predestroy(): void
Stream.destroy(err?: Error | null): void
Stream.destroyed: boolean
Stream.destroying: boolean
Stream.readable: boolean
Stream.writable: boolean
Stream utilities
Stream.addAbortSignal<S extends Stream>(signal: AbortSignal, stream: S): S
Stream.duplexPair(opts?: DuplexOptions): [Duplex, Duplex]
Stream.finished(stream: Stream, opts: { cleanup?: boolean }, cb: StreamCallback): () => void
Stream.getStreamError(stream: Stream, opts?: { all?: boolean }): Error | null
Stream.isDisturbed(stream: Stream): boolean
Stream.isEnded(stream: Stream): boolean
Stream.isErrored(stream: Stream): boolean
Stream.isFinished(stream: Stream): boolean
Stream.isReadable(stream: Stream): boolean
Stream.isStream(stream: unknown): stream is Stream
Stream.isWritable(stream: Stream): boolean
Stream.pipeline<S extends Writable>(streams: Pipeline<S>, cb?: StreamCallback): S
Readable streams
new Readable(opts?: ReadableOptions)
Readable._read(size: number): void
Readable.closed: boolean
Readable.errored: Error | null
Readable.pause(): this
Readable.pipe<S extends Writable>(dest: S, cb?: StreamCallback): S
Readable.push(data: unknown | null, encoding?: BufferEncoding): boolean
Readable.read(): unknown | null
Readable.from
Readable.fromWeb(readableStream: ReadableStream, opts?: ReadableFromWebOptions): Readable
Readable.isBackpressured(rs: Readable): boolean
Readable.isPaused(rs: Readable): boolean
Readable.toWeb(readable: Readable, opts?: ReadableToWebOptions): ReadableStream
Readable.resume(): this
Readable.setEncoding(encoding: BufferEncoding): void
Readable.unshift(data: unknown | null, encoding?: BufferEncoding): boolean
Writable streams
new Writable(opts?: WritableOptions)
Writable._final(cb: StreamCallback): void
Writable._write(data: unknown, encoding: StreamEncoding, cb: StreamCallback): void
Writable._writev(batch: { chunk: unknown; encoding: StreamEncoding }[], cb: StreamCallback): void
Writable.closed: boolean
Writable.cork(): void
Writable.end(cb?: StreamCallback): this
Writable.errored: Error | null
Writable.uncork(): void
Writable.drained(ws: Writable): Promise<boolean>
Writable.fromWeb(writableStream: WritableStream, opts?: WritableFromWebOptions): Writable
Writable.isBackpressured(ws: Writable): boolean
Writable.toWeb(writable: Writable): WritableStream
Writable.write(data: unknown, encoding?: BufferEncoding, cb?: StreamCallback): boolean
Duplex and Transform streams
new Duplex(opts?: DuplexOptions)
Duplex.fromWeb
Duplex.toWeb(readable: Readable, opts?: ReadableToWebOptions): ReadableStream
new Transform(opts?: TransformOptions)
_flush(cb: StreamCallback): void
_transform(data: unknown, encoding: StreamEncoding, cb: StreamCallback): void
Events
StreamEvents
ReadableEvents
WritableEvents
DuplexEvents
TransformEvents
Options
StreamOptions
ReadableOptions
WritableOptions
DuplexOptions
TransformOptions
Readable
Readable._destroy(err: Error | null, cb: StreamCallback): void
Readable._open(cb: StreamCallback): void
Readable._predestroy(): void
Readable.destroy(err?: Error | null): void
Readable.destroyed: boolean
Readable.destroying: boolean
Readable.readable: boolean
Readable.writable: boolean
Writable
Writable._destroy(err: Error | null, cb: StreamCallback): void
Writable._open(cb: StreamCallback): void
Writable._predestroy(): void
Writable.destroy(err?: Error | null): void
Writable.destroyed: boolean
Writable.destroying: boolean
Writable.readable: boolean
Writable.writable: boolean
Duplex
Duplex._destroy(err: Error | null, cb: StreamCallback): void
Duplex._final(cb: StreamCallback): void
Duplex._open(cb: StreamCallback): void
Duplex._predestroy(): void
Duplex._read(size: number): void
Duplex._write(data: unknown, encoding: StreamEncoding, cb: StreamCallback): void
Duplex._writev(batch: { chunk: unknown; encoding: StreamEncoding }[], cb: StreamCallback): void
Duplex.closed: boolean
Duplex.cork(): void
Duplex.destroy(err?: Error | null): void
Duplex.destroyed: boolean
Duplex.destroying: boolean
Duplex.end(cb?: StreamCallback): this
Duplex.errored: Error | null
Duplex.pause(): this
Duplex.pipe<S extends Writable>(dest: S, cb?: StreamCallback): S
Duplex.push(data: unknown | null, encoding?: BufferEncoding): boolean
Duplex.read(): unknown | null
Duplex.readable: boolean
Duplex.resume(): this
Duplex.setEncoding(encoding: BufferEncoding): void
Duplex.uncork(): void
Duplex.unshift(data: unknown | null, encoding?: BufferEncoding): boolean
Duplex.writable: boolean
Duplex.write(data: unknown, encoding?: BufferEncoding, cb?: StreamCallback): boolean
Transform
Transform._destroy(err: Error | null, cb: StreamCallback): void
Transform._final(cb: StreamCallback): void
Transform._open(cb: StreamCallback): void
Transform._predestroy(): void
Transform._read(size: number): void
Transform._write(data: unknown, encoding: StreamEncoding, cb: StreamCallback): void
Transform._writev(batch: { chunk: unknown; encoding: StreamEncoding }[], cb: StreamCallback): void
Transform.closed: boolean
Transform.cork(): void
Transform.destroy(err?: Error | null): void
Transform.destroyed: boolean
Transform.destroying: boolean
Transform.end(cb?: StreamCallback): this
Transform.errored: Error | null
Transform.pause(): this
Transform.pipe<S extends Writable>(dest: S, cb?: StreamCallback): S
Transform.push(data: unknown | null, encoding?: BufferEncoding): boolean
Transform.read(): unknown | null
Transform.readable: boolean
Transform.resume(): this
Transform.setEncoding(encoding: BufferEncoding): void
Transform.uncork(): void
Transform.unshift(data: unknown | null, encoding?: BufferEncoding): boolean
Transform.writable: boolean
Transform.write(data: unknown, encoding?: BufferEncoding, cb?: StreamCallback): boolean
Types
StreamCallback
ReadableFromWebOptions
ReadableToWebOptions
WritableFromWebOptions
DuplexFromWebOptions
bare-stream/web
ReadableStreamDefaultReader
new ReadableStreamDefaultReader(stream: ReadableStream)
ReadableStreamDefaultReader.cancel(reason?: unknown): Promise<void>
ReadableStreamDefaultReader.closed: Promise<void>
read(): Promise<{ value: unknown; done: boolean }>
ReadableStreamDefaultReader.releaseLock(): void
ReadableStreamDefaultController
new ReadableStreamDefaultController(stream: ReadableStream)
close(): void
ReadableStreamDefaultController.desiredSize: number
ReadableStreamDefaultController.enqueue(data: unknown): void
ReadableStreamDefaultController.error(error?: unknown): void
ReadableStream
new ReadableStream(underlyingSource?: UnderlyingSource, queuingStrategy?: CustomQueuingStrategy)
ReadableStream.cancel(reason?: unknown): Promise<void>
getReader(): ReadableStreamDefaultReader
ReadableStream.locked: boolean
pipeTo(destination: WritableStream): Promise<void>
ReadableStream.from(iterable: unknown | unknown[] | AsyncIterable<unknown>): ReadableStream
tee(): [ReadableStream, ReadableStream]
QueuingStrategy
new QueuingStrategy(opts?: QueuingStrategyOptions)
highWaterMark: number
size(chunk: unknown): number
WritableStreamDefaultWriter
new WritableStreamDefaultWriter(stream: WritableStream)
WritableStreamDefaultWriter.abort(reason?: unknown): Promise<void>
WritableStreamDefaultWriter.close(): Promise<void>
WritableStreamDefaultWriter.closed: Promise<void>
WritableStreamDefaultWriter.desiredSize: number
ready: Promise<void>
WritableStreamDefaultWriter.releaseLock(): void
write(chunk: unknown): Promise<void>
WritableStreamDefaultController
new WritableStreamDefaultController(stream: WritableStream)
error(err?: unknown): void
WritableStream
new WritableStream(underlyingSink?: UnderlyingSink, queuingStrategy?: CustomQueuingStrategy)
WritableStream.abort(reason?: unknown): Promise<void>
WritableStream.close(): Promise<void>
getWriter(): WritableStreamDefaultWriter
WritableStream.locked: boolean
TransformStreamDefaultController
new TransformStreamDefaultController(stream: TransformStream)
TransformStreamDefaultController.desiredSize: number
TransformStreamDefaultController.enqueue(data: unknown): void
TransformStreamDefaultController.error(error?: unknown): void
terminate(): void
TransformStream
TransformStream
readable: ReadableStream
writable: WritableStream
Functions
isReadableStream(value: unknown): value is ReadableStream
isReadableStreamErrored(stream: ReadableStream): boolean
isReadableStreamDisturbed(stream: ReadableStream): boolean
isWritableStream(value: unknown): value is WritableStream
isTransformStream(value: unknown): value is TransformStream
Types
UnderlyingSource
CustomQueuingStrategy
QueuingStrategyOptions
UnderlyingSink
Transformer
Classes
CountQueuingStrategy
ByteLengthQueuingStrategy
See also