Table of Contents

Class ReadableStream

Namespace
CSharpToJavaScript.APIs.JS
Assembly
CSharpToJavaScript.dll

The ReadableStream interface of the Streams API represents a readable stream of byte data. The Fetch API offers a concrete instance of a ReadableStream through the Response.Body property of a Response object.

[Value("ReadableStream")]
public class ReadableStream
Inheritance
ReadableStream
Derived
Inherited Members

Remarks

Constructors

ReadableStream()

public ReadableStream()

ReadableStream(Object, QueuingStrategy)

The ReadableStream() constructor creates and returns a readable stream object from the given handlers.

public ReadableStream(Object underlyingSource = null, QueuingStrategy strategy = null)

Parameters

underlyingSource Object
strategy QueuingStrategy

Remarks

Note that while all parameters are technically optional, omitting the underlyingSource will result in a stream that has no source, and that can't be read from (readers return a promise that will never be resolved).

-ReadableStream
-ReadableByteStreamController
-ReadableStreamDefaultController
-Using readable streams

See also on MDN

Properties

this[int]

public dynamic this[int i] { get; set; }

Parameters

i int

Property Value

dynamic

Locked

The locked read-only property of the ReadableStream interface returns whether or not the readable stream is locked to a reader.

[Value("locked")]
public bool Locked { get; }

Property Value

bool

A boolean value indicating whether or not the readable stream is locked.

Remarks

A readable stream can have at most one active reader at a time, and is locked to that reader until it is released.
A reader might be obtained using ReadableStream.getReader() and released using the reader's releaseLock() method.

-ReadableStream(Object, QueuingStrategy) constructor
-Using readable streams

See also on MDN

Methods

Cancel(dynamic)

The cancel() method of the ReadableStream interface returns a {{jsxref("Promise")}} that resolves when the stream is canceled.

[Value("cancel")]
public Task<GlobalObject.Undefined> Cancel(dynamic reason = null)

Parameters

reason dynamic

Returns

Task<GlobalObject.Undefined>

A Promise, which fulfills with undefined value.

Remarks

Cancel is used when you&apos;ve completely finished with the stream and don&apos;t need any more data from it, even if there are chunks enqueued waiting to be read.
That data is lost after cancel is called, and the stream is not readable any more.
To read those chunks still and not completely get rid of the stream, you'd use Close().

-ReadableStream(Object, QueuingStrategy) constructor
-Using readable streams

See also on MDN

From(dynamic)

NOTE
Experimental
The ReadableStream.from() static method returns a ReadableStream from a provided iterable or async iterable object.
[Value("from")]
public static ReadableStream From(dynamic asyncIterable)

Parameters

asyncIterable dynamic

Returns

ReadableStream

A ReadableStream.

Remarks

The method can be used to wrap iterable and async iterable objects as readable streams, including arrays, sets, arrays of promises, async generators, ReadableStreams, Node.js readable streams, and so on.

-ReadableStream
-Using readable streams

See also on MDN

GetReader(ReadableStreamGetReaderOptions)

The getReader() method of the ReadableStream interface creates a reader and locks the stream to it.
While the stream is locked, no other reader can be acquired until this one is released.

[Value("getReader")]
public Union143 GetReader(ReadableStreamGetReaderOptions options = null)

Parameters

options ReadableStreamGetReaderOptions

Returns

Union143

A ReadableStreamDefaultReader or ReadableStreamBYOBReader object instance, depending on the mode value.

Remarks

PipeThrough(ReadableWritablePair, StreamPipeOptions)

The pipeThrough() method of the ReadableStream interface provides a chainable way of piping the current stream through a transform stream or any other writable/readable pair.

[Value("pipeThrough")]
public ReadableStream PipeThrough(ReadableWritablePair transform, StreamPipeOptions options = null)

Parameters

transform ReadableWritablePair
options StreamPipeOptions

Returns

ReadableStream

The readable side of the transformStream.

Remarks

Piping a stream will generally lock it for the duration of the pipe, preventing other readers from locking it.

-ReadableStream(Object, QueuingStrategy) constructor
-Pipe chains

See also on MDN

PipeTo(WritableStream, StreamPipeOptions)

The pipeTo() method of the ReadableStream interface pipes the current ReadableStream to a given WritableStream and returns a {{jsxref("Promise")}} that fulfills when the piping process completes successfully, or rejects if any errors were encountered.

[Value("pipeTo")]
public Task<GlobalObject.Undefined> PipeTo(WritableStream destination, StreamPipeOptions options = null)

Parameters

destination WritableStream
options StreamPipeOptions

Returns

Task<GlobalObject.Undefined>

A Promise that resolves when the piping process has completed.

Remarks

Piping a stream will generally lock it for the duration of the pipe, preventing other readers from locking it.

-ReadableStream(Object, QueuingStrategy) constructor
-Pipe chains

See also on MDN

Tee()

The tee() method of the
ReadableStream interface tees the current readable stream, returning a
two-element array containing the two resulting branches as
new ReadableStream instances.

[Value("tee")]
public List<ReadableStream> Tee()

Returns

List<ReadableStream>

An {{jsxref("Array")}} containing two ReadableStream instances.

Remarks

This is useful for allowing two readers to read a stream sequentially or simultaneously,
perhaps at different speeds.
For example, you might do this in a ServiceWorker if you want to fetch
a response from the server and stream it to the browser, but also stream it to the
ServiceWorker cache. Since a response body cannot be consumed more than once, you&apos;d need
two copies to do this.

A teed stream will partially signal backpressure at the rate of the faster consumer
of the two ReadableStream branches,
and unread data is enqueued internally on the slower consumed ReadableStream
without any limit or backpressure.
That is, when both branches have an unread element in their internal queue,
then the original ReadableStream&apos;s controller&apos;s internal queue will start to fill up,
and once its DesiredSize ≤ 0
or byte stream controller DesiredSize ≤ 0,
then the controller will stop calling pull(controller) on the
underlying source passed to ReadableStream(Object, QueuingStrategy).
If only one branch is consumed, then the entire body will be enqueued in memory.
Therefore, you should not use the built-in tee() to read very large streams
in parallel at different speeds.
Instead, search for an implementation that fully backpressures
to the speed of the slower consumed branch.

To cancel the stream you then need to cancel both resulting branches. Teeing a stream
will generally lock it for the duration, preventing other readers from locking it.

-ReadableStream(Object, QueuingStrategy) constructor
-Teeing a stream

See also on MDN