1. Introduction
This section is non-normative.
Large swathes of the web platform are built on streaming data: that is, data that is created, processed, and consumed in an incremental fashion, without ever reading all of it into memory. The Streams Standard provides a common set of APIs for creating and interfacing with such streaming data, embodied in readable streams, writable streams, and transform streams.
These APIs have been designed to efficiently map to low-level I/O primitives, including specializations for byte streams where appropriate. They allow easy composition of multiple streams into pipe chains, or can be used directly via readers and writers. Finally, they are designed to automatically provide backpressure and queuing.
This standard provides the base stream primitives which other parts of the web platform can use to
expose their streaming data. For example, [FETCH] exposes Response
bodies as
ReadableStream
instances. More generally, the platform is full of streaming abstractions waiting
to be expressed as streams: multimedia streams, file streams, inter-global communication, and more
benefit from being able to process data incrementally instead of buffering it all into memory and
processing it in one go. By providing the foundation for these streams to be exposed to developers,
the Streams Standard enables use cases like:
-
Video effects: piping a readable video stream through a transform stream that applies effects in real time.
-
Decompression: piping a file stream through a transform stream that selectively decompresses files from a .tgz archive, turning them into
img
elements as the user scrolls through an image gallery. -
Image decoding: piping an HTTP response stream through a transform stream that decodes bytes into bitmap data, and then through another transform that translates bitmaps into PNGs. If installed inside the
fetch
hook of a service worker, this would allow developers to transparently polyfill new image formats. [SERVICE-WORKERS]
Web developers can also use the APIs described here to create their own streams, with the same APIs as those provided by the platform. Other developers can then transparently compose platform-provided streams with those supplied by libraries. In this way, the APIs described here provide unifying abstraction for all streams, encouraging an ecosystem to grow around these shared and composable interfaces.
2. Model
A chunk is a single piece of data that is written to or read from a stream. It can
be of any type; streams can even contain chunks of different types. A chunk will often not be the
most atomic unit of data for a given stream; for example a byte stream might contain chunks
consisting of 16 KiB Uint8Array
s, instead of single bytes.
2.1. Readable streams
A readable stream represents a source of data, from which you can read. In other
words, data comes
out of a readable stream. Concretely, a readable stream is an instance of the
ReadableStream
class.
Although a readable stream can be created with arbitrary behavior, most readable streams wrap a lower-level I/O source, called the underlying source. There are two types of underlying source: push sources and pull sources.
Push sources push data at you, whether or not you are listening for it. They may also provide a mechanism for pausing and resuming the flow of data. An example push source is a TCP socket, where data is constantly being pushed from the OS level, at a rate that can be controlled by changing the TCP window size.
Pull sources require you to request data from them. The data may be available synchronously, e.g. if it is held by the operating system’s in-memory buffers, or asynchronously, e.g. if it has to be read from disk. An example pull source is a file handle, where you seek to specific locations and read specific amounts.
Readable streams are designed to wrap both types of sources behind a single, unified interface. For
web developer–created streams, the implementation details of a source are provided by an object with certain methods and properties that is passed to
the ReadableStream()
constructor.
Chunks are enqueued into the stream by the stream’s underlying source. They can then be read
one at a time via the stream’s public interface, in particular by using a readable stream reader
acquired using the stream’s getReader()
method.
Code that reads from a readable stream using its public interface is known as a consumer.
Consumers also have the ability to cancel a readable
stream, using its cancel()
method. This indicates that the consumer has lost
interest in the stream, and will immediately close the stream, throw away any queued chunks, and
execute any cancellation mechanism of the underlying source.
Consumers can also tee a readable stream using its
tee()
method. This will lock the stream, making it
no longer directly usable; however, it will create two new streams, called branches, which can be consumed independently.
For streams representing bytes, an extended version of the readable stream is provided to handle
bytes efficiently, in particular by minimizing copies. The underlying source for such a readable
stream is called an underlying byte source. A readable stream whose underlying source is
an underlying byte source is sometimes called a readable byte stream. Consumers of
a readable byte stream can acquire a BYOB reader using the stream’s
getReader()
method.
2.2. Writable streams
A writable stream represents a destination for data, into which you can write. In
other words, data goes in to a writable stream. Concretely, a writable stream is an
instance of the WritableStream
class.
Analogously to readable streams, most writable streams wrap a lower-level I/O sink, called the underlying sink. Writable streams work to abstract away some of the complexity of the underlying sink, by queuing subsequent writes and only delivering them to the underlying sink one by one.
Chunks are written to the stream via its public interface, and are passed one at a time to the
stream’s underlying sink. For web developer-created streams, the implementation details of the
sink are provided by an object with certain methods that is
passed to the WritableStream()
constructor.
Code that writes into a writable stream using its public interface is known as a producer.
Producers also have the ability to abort a writable stream,
using its abort()
method. This indicates that the producer believes something has
gone wrong, and that future writes should be discontinued. It puts the stream in an errored state,
even without a signal from the underlying sink, and it discards all writes in the stream’s
internal queue.
2.3. Transform streams
A transform stream consists of a pair of streams: a writable stream, known as its writable side, and a readable stream, known as its readable side. In a manner specific to the transform stream in question, writes to the writable side result in new data being made available for reading from the readable side.
Concretely, any object with a writable
property and a readable
property
can serve as a transform stream. However, the standard TransformStream
class makes it much
easier to create such a pair that is properly entangled. It wraps a transformer, which
defines algorithms for the specific transformation to be performed. For web developer–created
streams, the implementation details of a transformer are provided by an
object with certain methods and properties that is passed to the TransformStream()
constructor. Other specifications might use the GenericTransformStream
mixin to create classes
with the same writable
/readable
property pair but other custom APIs
layered on top.
An identity transform stream is a type of transform stream which forwards all
chunks written to its writable side to its readable side, without any changes. This can
be useful in a variety of scenarios. By default, the
TransformStream
constructor will create an identity transform stream, when no
transform()
method is present on the transformer object.
Some examples of potential transform streams include:
-
A GZIP compressor, to which uncompressed bytes are written and from which compressed bytes are read;
-
A video decoder, to which encoded bytes are written and from which uncompressed video frames are read;
-
A text decoder, to which bytes are written and from which strings are read;
-
A CSV-to-JSON converter, to which strings representing lines of a CSV file are written and from which corresponding JavaScript objects are read.
2.4. Pipe chains and backpressure
Streams are primarily used by piping them to each other. A readable stream can be piped
directly to a writable stream, using its pipeTo()
method, or it can be piped
through one or more transform streams first, using its pipeThrough()
method.
A set of streams piped together in this way is referred to as a pipe chain. In a pipe chain, the original source is the underlying source of the first readable stream in the chain; the ultimate sink is the underlying sink of the final writable stream in the chain.
Once a pipe chain is constructed, it will propagate signals regarding how fast chunks should flow through it. If any step in the chain cannot yet accept chunks, it propagates a signal backwards through the pipe chain, until eventually the original source is told to stop producing chunks so fast. This process of normalizing flow from the original source according to how fast the chain can process chunks is called backpressure.
Concretely, the original source is given the
controller.desiredSize
(or
byteController.desiredSize
) value, and can then adjust
its rate of data flow accordingly. This value is derived from the
writer.desiredSize
corresponding to the ultimate sink, which gets updated as the ultimate sink finishes writing chunks. The
pipeTo()
method used to construct the chain automatically ensures this
information propagates back through the pipe chain.
When teeing a readable stream, the backpressure signals from its two branches will aggregate, such that if neither branch is read from, a backpressure signal will be sent to the underlying source of the original stream.
Piping locks the readable and writable streams, preventing them from being manipulated for the duration of the pipe operation. This allows the implementation to perform important optimizations, such as directly shuttling data from the underlying source to the underlying sink while bypassing many of the intermediate queues.
2.5. Internal queues and queuing strategies
Both readable and writable streams maintain internal queues, which they use for similar purposes. In the case of a readable stream, the internal queue contains chunks that have been enqueued by the underlying source, but not yet read by the consumer. In the case of a writable stream, the internal queue contains chunks which have been written to the stream by the producer, but not yet processed and acknowledged by the underlying sink.
A queuing strategy is an object that determines how a stream should signal backpressure based on the state of its internal queue. The queuing strategy assigns a size to each chunk, and compares the total size of all chunks in the queue to a specified number, known as the high water mark. The resulting difference, high water mark minus total size, is used to determine the desired size to fill the stream’s queue.
For readable streams, an underlying source can use this desired size as a backpressure signal, slowing down chunk generation so as to try to keep the desired size above or at zero. For writable streams, a producer can behave similarly, avoiding writes that would cause the desired size to go negative.
Concretely, a queuing strategy for web developer–created streams is given by
any JavaScript object with a highWaterMark
property. For byte streams the
highWaterMark
always has units of bytes. For other streams the default unit is
chunks, but a size()
function can be included in the strategy object
which returns the size for a given chunk. This permits the highWaterMark
to be
specified in arbitrary floating-point units.
In JavaScript, such a strategy could be written manually as
, or using the built-in CountQueuingStrategy
class, as
.
2.6. Locking
A readable stream reader, or simply reader, is an
object that allows direct reading of chunks from a readable stream. Without a reader, a
consumer can only perform high-level operations on the readable stream: canceling the stream, or piping the readable stream to a writable stream. A reader is
acquired via the stream’s getReader()
method.
A readable byte stream has the ability to vend two types of readers: default readers and BYOB readers. BYOB ("bring your
own buffer") readers allow reading into a developer-supplied buffer, thus minimizing copies. A
non-byte readable stream can only vend default readers. Default readers are instances of the
ReadableStreamDefaultReader
class, while BYOB readers are instances of
ReadableStreamBYOBReader
.
Similarly, a writable stream writer, or simply
writer, is an object that allows direct writing of chunks to a writable stream. Without a
writer, a producer can only perform the high-level operations of aborting the stream or piping a readable stream to the writable stream. Writers are
represented by the WritableStreamDefaultWriter
class.
Under the covers, these high-level operations actually use a reader or writer themselves.
A given readable or writable stream only has at most one reader or writer at a time. We say in this
case the stream is locked, and that the
reader or writer is active. This state can be
determined using the readableStream.locked
or
writableStream.locked
properties.
A reader or writer also has the capability to release its lock, which makes it no longer active, and allows further readers or
writers to be acquired. This is done via the
defaultReader.releaseLock()
,
byobReader.releaseLock()
, or
writer.releaseLock()
method, as appropriate.
3. Conventions
This specification depends on the Infra Standard. [INFRA]
This specification uses the abstract operation concept from the JavaScript specification for its internal algorithms. This includes treating their return values as completion records, and the use of ! and ? prefixes for unwrapping those completion records. [ECMASCRIPT]
This specification also uses the internal slot concept and notation from the JavaScript specification. (Although, the internal slots are on Web IDL platform objects instead of on JavaScript objects.)
The reasons for the usage of these foreign JavaScript specification conventions are largely historical. We urge you to avoid following our example when writing your own web specifications.
In this specification, all numbers are represented as double-precision 64-bit IEEE 754 floating
point values (like the JavaScript Number type or Web IDL unrestricted double
type), and all
arithmetic operations performed on them must be done in the standard way for such values. This is
particularly important for the data structure described in § 8.1 Queue-with-sizes. [IEEE-754]
4. Readable streams
4.1. Using readable streams
readableStream. pipeTo( writableStream) . then(() => console. log( "All data successfully written!" )) . catch ( e=> console. error( "Something went wrong!" , e));
readableStream. pipeTo( new WritableStream({ write( chunk) { console. log( "Chunk received" , chunk); }, close() { console. log( "All data successfully read!" ); }, abort( e) { console. error( "Something went wrong!" , e); } }));
By returning promises from your write()
implementation, you can signal
backpressure to the readable stream.
read()
method to get
successive chunks. For example, this code logs the next chunk in the stream, if available:
const reader= readableStream. getReader(); reader. read(). then( ({ value, done}) => { if ( done) { console. log( "The stream was already closed!" ); } else { console. log( value); } }, e=> console. error( "The stream became errored and cannot be read from!" , e) );
This more manual method of reading a stream is mainly useful for library authors building new high-level operations on streams, beyond the provided ones of piping and teeing.
const reader= readableStream. getReader({ mode: "byob" }); let startingAB= new ArrayBuffer( 1024 ); const buffer= await readInto( startingAB); console. log( "The first 1024 bytes: " , buffer); async function readInto( buffer) { let offset= 0 ; while ( offset< buffer. byteLength) { const { value: view, done} = await reader. read( new Uint8Array( buffer, offset, buffer. byteLength- offset)); buffer= view. buffer; if ( done) { break ; } offset+= view. byteLength; } return buffer; }
An important thing to note here is that the final buffer
value is different from the
startingAB
, but it (and all intermediate buffers) shares the same backing memory
allocation. At each step, the buffer is transferred to a new
ArrayBuffer
object. The view
is destructured from the return value of reading a
new Uint8Array
, with that ArrayBuffer
object as its buffer
property, the
offset that bytes were written to as its byteOffset
property, and the number of
bytes that were written as its byteLength
property.
Note that this example is mostly educational. For practical purposes, the
min
option of read()
provides an easier and more direct way to read an exact number of bytes:
const reader= readableStream. getReader({ mode: "byob" }); const { value: view, done} = await reader. read( new Uint8Array( 1024 ), { min: 1024 }); console. log( "The first 1024 bytes: " , view);
4.2. The ReadableStream
class
The ReadableStream
class is a concrete instance of the general readable stream concept. It
is adaptable to any chunk type, and maintains an internal queue to keep track of data supplied
by the underlying source but not yet read by any consumer.
4.2.1. Interface definition
The Web IDL definition for the ReadableStream
class is given as follows:
[Exposed=*,Transferable ]interface {
ReadableStream constructor (optional object ,
underlyingSource optional QueuingStrategy = {});
strategy static ReadableStream from (any );
asyncIterable readonly attribute boolean locked ;Promise <undefined >cancel (optional any );
reason ReadableStreamReader getReader (optional ReadableStreamGetReaderOptions = {});
options ReadableStream pipeThrough (ReadableWritablePair ,
transform optional StreamPipeOptions = {});
options Promise <undefined >pipeTo (WritableStream ,
destination optional StreamPipeOptions = {});
options sequence <ReadableStream >tee ();async_iterable <any >(optional ReadableStreamIteratorOptions = {}); };
options typedef (ReadableStreamDefaultReader or ReadableStreamBYOBReader );
ReadableStreamReader enum {
ReadableStreamReaderMode };
"byob" dictionary {
ReadableStreamGetReaderOptions ReadableStreamReaderMode ; };
mode dictionary {
ReadableStreamIteratorOptions boolean =
preventCancel false ; };dictionary {
ReadableWritablePair required ReadableStream ;
readable required WritableStream ; };
writable dictionary {
StreamPipeOptions boolean =
preventClose false ;boolean =
preventAbort false ;boolean =
preventCancel false ;AbortSignal ; };
signal
4.2.2. Internal slots
Instances of ReadableStream
are created with the internal slots described in the following
table:
Internal Slot | Description (non-normative) |
---|---|
[[controller]] | A ReadableStreamDefaultController or
ReadableByteStreamController created with the ability to control the state and queue of this
stream
|
[[Detached]] | A boolean flag set to true when the stream is transferred |
[[disturbed]] | A boolean flag set to true when the stream has been read from or canceled |
[[reader]] | A ReadableStreamDefaultReader or ReadableStreamBYOBReader
instance, if the stream is locked to a reader, or undefined if it is not
|
[[state]] | A string containing the stream’s current state, used internally; one
of "readable ", "closed ", or "errored "
|
[[storedError]] | A value indicating how the stream failed, to be given as a failure reason or exception when trying to operate on an errored stream |
4.2.3. The underlying source API
The ReadableStream()
constructor accepts as its first argument a JavaScript object representing
the underlying source. Such objects can contain any of the following properties:
dictionary {
UnderlyingSource UnderlyingSourceStartCallback start ;UnderlyingSourcePullCallback pull ;UnderlyingSourceCancelCallback cancel ;ReadableStreamType type ; [EnforceRange ]unsigned long long autoAllocateChunkSize ; };typedef (ReadableStreamDefaultController or ReadableByteStreamController );
ReadableStreamController callback =
UnderlyingSourceStartCallback any (ReadableStreamController );
controller callback =
UnderlyingSourcePullCallback Promise <undefined > (ReadableStreamController );
controller callback =
UnderlyingSourceCancelCallback Promise <undefined > (optional any );
reason enum {
ReadableStreamType "bytes" };
start(controller)
, of type UnderlyingSourceStartCallback-
A function that is called immediately during creation of the
ReadableStream
.Typically this is used to adapt a push source by setting up relevant event listeners, as in the example of § 10.1 A readable stream with an underlying push source (no backpressure support), or to acquire access to a pull source, as in § 10.4 A readable stream with an underlying pull source.
If this setup process is asynchronous, it can return a promise to signal success or failure; a rejected promise will error the stream. Any thrown exceptions will be re-thrown by the
ReadableStream()
constructor. pull(controller)
, of type UnderlyingSourcePullCallback-
A function that is called whenever the stream’s internal queue of chunks becomes not full, i.e. whenever the queue’s desired size becomes positive. Generally, it will be called repeatedly until the queue reaches its high water mark (i.e. until the desired size becomes non-positive).
For push sources, this can be used to resume a paused flow, as in § 10.2 A readable stream with an underlying push source and backpressure support. For pull sources, it is used to acquire new chunks to enqueue into the stream, as in § 10.4 A readable stream with an underlying pull source.
This function will not be called until
start()
successfully completes. Additionally, it will only be called repeatedly if it enqueues at least one chunk or fulfills a BYOB request; a no-oppull()
implementation will not be continually called.If the function returns a promise, then it will not be called again until that promise fulfills. (If the promise rejects, the stream will become errored.) This is mainly used in the case of pull sources, where the promise returned represents the process of acquiring a new chunk. Throwing an exception is treated the same as returning a rejected promise.
cancel(reason)
, of type UnderlyingSourceCancelCallback-
A function that is called whenever the consumer cancels the stream, via
stream.cancel()
orreader.cancel()
. It takes as its argument the same value as was passed to those methods by the consumer.Readable streams can additionally be canceled under certain conditions during piping; see the definition of the
pipeTo()
method for more details.For all streams, this is generally used to release access to the underlying resource; see for example § 10.1 A readable stream with an underlying push source (no backpressure support).
If the shutdown process is asynchronous, it can return a promise to signal success or failure; the result will be communicated via the return value of the
cancel()
method that was called. Throwing an exception is treated the same as returning a rejected promise.Even if the cancelation process fails, the stream will still close; it will not be put into an errored state. This is because a failure in the cancelation process doesn’t matter to the consumer’s view of the stream, once they’ve expressed disinterest in it by canceling. The failure is only communicated to the immediate caller of the corresponding method.
This is different from the behavior of the
close
andabort
options of aWritableStream
’s underlying sink, which upon failure put the correspondingWritableStream
into an errored state. Those correspond to specific actions the producer is requesting and, if those actions fail, they indicate something more persistently wrong. type
(byte streams only), of type ReadableStreamType-
Can be set to "
bytes
" to signal that the constructedReadableStream
is a readable byte stream. This ensures that the resultingReadableStream
will successfully be able to vend BYOB readers via itsgetReader()
method. It also affects the controller argument passed to thestart()
andpull()
methods; see below.For an example of how to set up a readable byte stream, including using the different controller interface, see § 10.3 A readable byte stream with an underlying push source (no backpressure support).
Setting any value other than "
bytes
" or undefined will cause theReadableStream()
constructor to throw an exception. autoAllocateChunkSize
(byte streams only), of type unsigned long long-
Can be set to a positive integer to cause the implementation to automatically allocate buffers for the underlying source code to write into. In this case, when a consumer is using a default reader, the stream implementation will automatically allocate an
ArrayBuffer
of the given size, so thatcontroller.byobRequest
is always present, as if the consumer was using a BYOB reader.This is generally used to cut down on the amount of code needed to handle consumers that use default readers, as can be seen by comparing § 10.3 A readable byte stream with an underlying push source (no backpressure support) without auto-allocation to § 10.5 A readable byte stream with an underlying pull source with auto-allocation.
The type of the controller argument passed to the start()
and
pull()
methods depends on the value of the type
option. If type
is set to undefined (including via omission), then
controller will be a ReadableStreamDefaultController
. If it’s set to
"bytes
", then controller will be a ReadableByteStreamController
.
4.2.4. Constructor, methods, and properties
stream = new
ReadableStream
(underlyingSource[, strategy])-
Creates a new
ReadableStream
wrapping the provided underlying source. See § 4.2.3 The underlying source API for more details on the underlyingSource argument.The strategy argument represents the stream’s queuing strategy, as described in § 7.1 The queuing strategy API. If it is not provided, the default behavior will be the same as a
CountQueuingStrategy
with a high water mark of 1. stream =
ReadableStream.from
(asyncIterable)-
Creates a new
ReadableStream
wrapping the provided iterable or async iterable.This can be used to adapt various kinds of objects into a readable stream, such as an array, an async generator, or a Node.js readable stream.
isLocked = stream.
locked
-
Returns whether or not the readable stream is locked to a reader.
await stream.
cancel
([ reason ])-
Cancels the stream, signaling a loss of interest in the stream by a consumer. The supplied reason argument will be given to the underlying source’s
cancel()
method, which might or might not use it.The returned promise will fulfill if the stream shuts down successfully, or reject if the underlying source signaled that there was an error doing so. Additionally, it will reject with a
TypeError
(without attempting to cancel the stream) if the stream is currently locked. reader = stream.
getReader
()-
Creates a
ReadableStreamDefaultReader
and locks the stream to the new reader. While the stream is locked, no other reader can be acquired until this one is released.This functionality is especially useful for creating abstractions that desire the ability to consume a stream in its entirety. By getting a reader for the stream, you can ensure nobody else can interleave reads with yours or cancel the stream, which would interfere with your abstraction.
reader = stream.
getReader
({mode
: "byob
" })-
Creates a
ReadableStreamBYOBReader
and locks the stream to the new reader.This call behaves the same way as the no-argument variant, except that it only works on readable byte streams, i.e. streams which were constructed specifically with the ability to handle "bring your own buffer" reading. The returned BYOB reader provides the ability to directly read individual chunks from the stream via its
read()
method, into developer-supplied buffers, allowing more precise control over allocation. readable = stream.
pipeThrough
({writable
,readable
}[, {preventClose
,preventAbort
,preventCancel
,signal
}])-
Provides a convenient, chainable way of piping this readable stream through a transform stream (or any other
{ writable, readable }
pair). It simply pipes the stream into the writable side of the supplied pair, and returns the readable side for further use.Piping a stream will lock it for the duration of the pipe, preventing any other consumer from acquiring a reader.
await stream.
pipeTo
(destination[, {preventClose
,preventAbort
,preventCancel
,signal
}])-
Pipes this readable stream to a given writable stream destination. The way in which the piping process behaves under various error conditions can be customized with a number of passed options. It returns a promise that fulfills when the piping process completes successfully, or rejects if any errors were encountered.
Piping a stream will lock it for the duration of the pipe, preventing any other consumer from acquiring a reader.
Errors and closures of the source and destination streams propagate as follows:
-
An error in this source readable stream will abort destination, unless
preventAbort
is truthy. The returned promise will be rejected with the source’s error, or with any error that occurs during aborting the destination. -
An error in destination will cancel this source readable stream, unless
preventCancel
is truthy. The returned promise will be rejected with the destination’s error, or with any error that occurs during canceling the source. -
When this source readable stream closes, destination will be closed, unless
preventClose
is truthy. The returned promise will be fulfilled once this process completes, unless an error is encountered while closing the destination, in which case it will be rejected with that error. -
If destination starts out closed or closing, this source readable stream will be canceled, unless
preventCancel
is true. The returned promise will be rejected with an error indicating piping to a closed stream failed, or with any error that occurs during canceling the source.
The
signal
option can be set to anAbortSignal
to allow aborting an ongoing pipe operation via the correspondingAbortController
. In this case, this source readable stream will be canceled, and destination aborted, unless the respective optionspreventCancel
orpreventAbort
are set. -
[branch1, branch2] = stream.
tee
()-
Tees this readable stream, returning a two-element array containing the two resulting branches as new
ReadableStream
instances.Teeing a stream will lock it, preventing any other consumer from acquiring a reader. To cancel the stream, cancel both of the resulting branches; a composite cancellation reason will then be propagated to the stream’s underlying source.
If this stream is a readable byte stream, then each branch will receive its own copy of each chunk. If not, then the chunks seen in each branch will be the same object. If the chunks are not immutable, this could allow interference between the two branches.
new ReadableStream(underlyingSource, strategy)
constructor steps are:
-
If underlyingSource is missing, set it to null.
-
Let underlyingSourceDict be underlyingSource, converted to an IDL value of type
UnderlyingSource
.We cannot declare the underlyingSource argument as having the
UnderlyingSource
type directly, because doing so would lose the reference to the original object. We need to retain the object so we can invoke the various methods on it. -
Perform ! InitializeReadableStream(this).
-
If underlyingSourceDict["
type
"] is "bytes
":-
If strategy["
size
"] exists, throw aRangeError
exception. -
Let highWaterMark be ? ExtractHighWaterMark(strategy, 0).
-
Perform ? SetUpReadableByteStreamControllerFromUnderlyingSource(this, underlyingSource, underlyingSourceDict, highWaterMark).
-
-
Otherwise,
-
Let sizeAlgorithm be ! ExtractSizeAlgorithm(strategy).
-
Let highWaterMark be ? ExtractHighWaterMark(strategy, 1).
-
Perform ? SetUpReadableStreamDefaultControllerFromUnderlyingSource(this, underlyingSource, underlyingSourceDict, highWaterMark, sizeAlgorithm).
from(asyncIterable)
method steps
are:
-
Return ? ReadableStreamFromIterable(asyncIterable).
locked
getter steps are:
-
Return ! IsReadableStreamLocked(this).
cancel(reason)
method steps are:
-
If ! IsReadableStreamLocked(this) is true, return a promise rejected with a
TypeError
exception. -
Return ! ReadableStreamCancel(this, reason).
getReader(options)
method steps
are:
-
If options["
mode
"] does not exist, return ? AcquireReadableStreamDefaultReader(this). -
Return ? AcquireReadableStreamBYOBReader(this).
function readAllChunks( readableStream) { const reader= readableStream. getReader(); const chunks= []; return pump(); function pump() { return reader. read(). then(({ value, done}) => { if ( done) { return chunks; } chunks. push( value); return pump(); }); } }
Note how the first thing it does is obtain a reader, and from then on it uses the reader exclusively. This ensures that no other consumer can interfere with the stream, either by reading chunks or by canceling the stream.
pipeThrough(transform, options)
method steps are:
-
If ! IsReadableStreamLocked(this) is true, throw a
TypeError
exception. -
If ! IsWritableStreamLocked(transform["
writable
"]) is true, throw aTypeError
exception. -
Let signal be options["
signal
"] if it exists, or undefined otherwise. -
Let promise be ! ReadableStreamPipeTo(this, transform["
writable
"], options["preventClose
"], options["preventAbort
"], options["preventCancel
"], signal). -
Set promise.[[PromiseIsHandled]] to true.
-
Return transform["
readable
"].
pipeThrough(transform, options)
would look like
httpResponseBody. pipeThrough( decompressorTransform) . pipeThrough( ignoreNonImageFilesTransform) . pipeTo( mediaGallery);
pipeTo(destination, options)
method steps are:
-
If ! IsReadableStreamLocked(this) is true, return a promise rejected with a
TypeError
exception. -
If ! IsWritableStreamLocked(destination) is true, return a promise rejected with a
TypeError
exception. -
Let signal be options["
signal
"] if it exists, or undefined otherwise. -
Return ! ReadableStreamPipeTo(this, destination, options["
preventClose
"], options["preventAbort
"], options["preventCancel
"], signal).
AbortSignal
, as follows:
const controller= new AbortController(); readable. pipeTo( writable, { signal: controller. signal}); // ... some time later ... controller. abort();
(The above omits error handling for the promise returned by pipeTo()
.
Additionally, the impact of the preventAbort
and
preventCancel
options what happens when piping is stopped are worth
considering.)
ReadableStream
being piped, while writing into
the same WritableStream
:
const controller= new AbortController(); const pipePromise= readable1. pipeTo( writable, { preventAbort: true , signal: controller. signal}); // ... some time later ... controller. abort(); // Wait for the pipe to complete before starting a new one: try { await pipePromise; } catch ( e) { // Swallow "AbortError" DOMExceptions as expected, but rethrow any unexpected failures. if ( e. name!== "AbortError" ) { throw e; } } // Start the new pipe! readable2. pipeTo( writable);
tee()
method steps are:
-
Return ? ReadableStreamTee(this, false).
cacheEntry
representing an on-disk file, and another writable stream
httpRequestBody
representing an upload to a remote server, you could pipe the same
readable stream to both destinations at once:
const [ forLocal, forRemote] = readableStream. tee(); Promise. all([ forLocal. pipeTo( cacheEntry), forRemote. pipeTo( httpRequestBody) ]) . then(() => console. log( "Saved the stream to the cache and also uploaded it!" )) . catch ( e=> console. error( "Either caching or uploading failed: " , e));
4.2.5. Asynchronous iteration
for await (const chunk of stream) { ... }
for await (const chunk of stream.values({
preventCancel
: true })) { ... }-
Asynchronously iterates over the chunks in the stream’s internal queue.
Asynchronously iterating over the stream will lock it, preventing any other consumer from acquiring a reader. The lock will be released if the async iterator’s
return()
method is called, e.g. bybreak
ing out of the loop.By default, calling the async iterator’s
return()
method will also cancel the stream. To prevent this, use the stream’svalues()
method, passing true for thepreventCancel
option.
ReadableStream
, given stream,
iterator, and args, are:
-
Let reader be ? AcquireReadableStreamDefaultReader(stream).
-
Set iterator’s reader to reader.
-
Let preventCancel be args[0]["
preventCancel
"]. -
Set iterator’s prevent cancel to preventCancel.
ReadableStream
, given stream and iterator, are:
-
Let reader be iterator’s reader.
-
Assert: reader.[[stream]] is not undefined.
-
Let promise be a new promise.
-
Let readRequest be a new read request with the following items:
- chunk steps, given chunk
-
-
Resolve promise with chunk.
-
- close steps
-
-
Perform ! ReadableStreamDefaultReaderRelease(reader).
-
Resolve promise with end of iteration.
-
- error steps, given e
-
-
Perform ! ReadableStreamDefaultReaderRelease(reader).
-
Reject promise with e.
-
-
Perform ! ReadableStreamDefaultReaderRead(this, readRequest).
-
Return promise.
ReadableStream
, given stream, iterator, and arg, are:
-
Let reader be iterator’s reader.
-
Assert: reader.[[stream]] is not undefined.
-
Assert: reader.[[readRequests]] is empty, as the async iterator machinery guarantees that any previous calls to
next()
have settled before this is called. -
If iterator’s prevent cancel is false:
-
Let result be ! ReadableStreamReaderGenericCancel(reader, arg).
-
Perform ! ReadableStreamDefaultReaderRelease(reader).
-
Return result.
-
-
Perform ! ReadableStreamDefaultReaderRelease(reader).
-
Return a promise resolved with undefined.
4.2.6. Transfer via postMessage()
destination.postMessage(rs, { transfer: [rs] });
-
Sends a
ReadableStream
to another frame, window, or worker.The transferred stream can be used exactly like the original. The original will become locked and no longer directly usable.
ReadableStream
objects are transferable objects. Their transfer steps, given value
and dataHolder, are:
-
If ! IsReadableStreamLocked(value) is true, throw a "
DataCloneError
"DOMException
. -
Let port1 be a new
MessagePort
in the current Realm. -
Let port2 be a new
MessagePort
in the current Realm. -
Entangle port1 and port2.
-
Let writable be a new
WritableStream
in the current Realm. -
Perform ! SetUpCrossRealmTransformWritable(writable, port1).
-
Let promise be ! ReadableStreamPipeTo(value, writable, false, false, false).
-
Set promise.[[PromiseIsHandled]] to true.
-
Set dataHolder.[[port]] to ! StructuredSerializeWithTransfer(port2, « port2 »).
-
Let deserializedRecord be ! StructuredDeserializeWithTransfer(dataHolder.[[port]], the current Realm).
-
Let port be deserializedRecord.[[Deserialized]].
-
Perform ! SetUpCrossRealmTransformReadable(value, port).
4.3. The ReadableStreamGenericReader
mixin
The ReadableStreamGenericReader
mixin defines common internal slots, getters and methods that
are shared between ReadableStreamDefaultReader
and ReadableStreamBYOBReader
objects.
4.3.1. Mixin definition
The Web IDL definition for the ReadableStreamGenericReader
mixin is given as follows:
interface mixin {
ReadableStreamGenericReader readonly attribute Promise <undefined >closed ;Promise <undefined >cancel (optional any ); };
reason
4.3.2. Internal slots
Instances of classes including the ReadableStreamGenericReader
mixin are created with the
internal slots described in the following table:
Internal Slot | Description (non-normative) |
---|---|
[[closedPromise]] | A promise returned by the reader’s
closed getter
|
[[stream]] | A ReadableStream instance that owns this reader
|
4.3.3. Methods and properties
closed
getter steps are:
-
Return this.[[closedPromise]].
cancel(reason)
method steps are:
-
If this.[[stream]] is undefined, return a promise rejected with a
TypeError
exception. -
Return ! ReadableStreamReaderGenericCancel(this, reason).
4.4. The ReadableStreamDefaultReader
class
The ReadableStreamDefaultReader
class represents a default reader designed to be vended by a
ReadableStream
instance.
4.4.1. Interface definition
The Web IDL definition for the ReadableStreamDefaultReader
class is given as follows:
[Exposed=*]interface {
ReadableStreamDefaultReader constructor (ReadableStream );
stream Promise <ReadableStreamReadResult >read ();undefined releaseLock (); };ReadableStreamDefaultReader includes ReadableStreamGenericReader ;dictionary {
ReadableStreamReadResult any ;
value boolean ; };
done
4.4.2. Internal slots
Instances of ReadableStreamDefaultReader
are created with the internal slots defined by
ReadableStreamGenericReader
, and those described in the following table:
Internal Slot | Description (non-normative) |
---|---|
[[readRequests]] | A list of read requests, used when a consumer requests chunks sooner than they are available |
A read request is a struct containing three algorithms to perform in reaction to filling the readable stream’s internal queue or changing its state. It has the following items:
- chunk steps
-
An algorithm taking a chunk, called when a chunk is available for reading
- close steps
-
An algorithm taking no arguments, called when no chunks are available because the stream is closed
- error steps
-
An algorithm taking a JavaScript value, called when no chunks are available because the stream is errored
4.4.3. Constructor, methods, and properties
reader = new
ReadableStreamDefaultReader
(stream)-
This is equivalent to calling
stream.
.getReader()
await reader.
closed
-
Returns a promise that will be fulfilled when the stream becomes closed, or rejected if the stream ever errors or the reader’s lock is released before the stream finishes closing.
await reader.
cancel
([ reason ])-
If the reader is active, behaves the same as
stream.
.cancel
(reason) { value, done } = await reader.
read
()-
Returns a promise that allows access to the next chunk from the stream’s internal queue, if available.
- If the chunk does become available, the promise will be fulfilled with an object of the form
.{ value: theChunk, done: false } - If the stream becomes closed, the promise will be fulfilled with an object of the form
.{ value: undefined , done: true } - If the stream becomes errored, the promise will be rejected with the relevant error.
If reading a chunk causes the queue to become empty, more data will be pulled from the underlying source.
- If the chunk does become available, the promise will be fulfilled with an object of the form
reader.
releaseLock
()-
Releases the reader’s lock on the corresponding stream. After the lock is released, the reader is no longer active. If the associated stream is errored when the lock is released, the reader will appear errored in the same way from now on; otherwise, the reader will appear closed.
If the reader’s lock is released while it still has pending read requests, then the promises returned by the reader’s
read()
method are immediately rejected with aTypeError
. Any unread chunks remain in the stream’s internal queue and can be read later by acquiring a new reader.
new ReadableStreamDefaultReader(stream)
constructor steps are:
-
Perform ? SetUpReadableStreamDefaultReader(this, stream).
read()
method steps are:
-
If this.[[stream]] is undefined, return a promise rejected with a
TypeError
exception. -
Let promise be a new promise.
-
Let readRequest be a new read request with the following items:
- chunk steps, given chunk
- close steps
- error steps, given e
-
-
Reject promise with e.
-
-
Perform ! ReadableStreamDefaultReaderRead(this, readRequest).
-
Return promise.
releaseLock()
method steps are:
-
If this.[[stream]] is undefined, return.
-
Perform ! ReadableStreamDefaultReaderRelease(this).