From a525edf8ecf2fc39b9fc4cca82e6c0c18d60288f Mon Sep 17 00:00:00 2001 From: Nic <162764842+Nic-Polumeyv@users.noreply.github.com> Date: Mon, 3 Aug 2026 15:43:35 -0400 Subject: [PATCH 1/2] [Workers] Document missing ReadableStream APIs --- .../runtime-apis/streams/readablestream.mdx | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/content/docs/workers/runtime-apis/streams/readablestream.mdx b/src/content/docs/workers/runtime-apis/streams/readablestream.mdx index 72512cc6f9f..ba62cd21a2f 100644 --- a/src/content/docs/workers/runtime-apis/streams/readablestream.mdx +++ b/src/content/docs/workers/runtime-apis/streams/readablestream.mdx @@ -28,6 +28,10 @@ A `ReadableStream` is returned by the `readable` property inside [`TransformStre * Pipes the readable stream to a given writable stream `destination` and returns a promise that is fulfilled when the `write` operation succeeds or rejects it if the operation fails. +* pipeThrough(transformStream, optionsPipeToOptions) : ReadableStream + + * Pipes the readable stream to the writable side of a given [`TransformStream`](/workers/runtime-apis/streams/transformstream/) and returns the transform's readable side, so that calls can be chained. `options` accepts the same values as `pipeTo()`. + * getReader(optionsObject) : ReadableStreamDefaultReader * Gets an instance of `ReadableStreamDefaultReader` and locks the `ReadableStream` to that reader instance. This method accepts an object argument indicating options. The only supported option is `mode`, which can be set to `byob` to create a [`ReadableStreamBYOBReader`](/workers/runtime-apis/streams/readablestreambyobreader/), as shown here: @@ -36,6 +40,24 @@ A `ReadableStream` is returned by the `readable` property inside [`TransformStre let reader = readable.getReader({ mode: 'byob' }); ``` +* cancel(reasonstringoptional) : Promise\ + + * Cancels the stream. `reason` is an optional human-readable string indicating the reason for cancellation. `reason` will be passed to the underlying source’s cancel algorithm. Any data not yet read is lost. + +* `tee()` : \[ReadableStream, ReadableStream] + + * Locks the stream and returns an array of two new `ReadableStream` instances, each of which reads the same data as the original stream. Chunks are shared between the two branches, not copied, and backpressure to the underlying source follows the branch with the most unread data. This avoids unbounded buffering when one branch reads more slowly than the other, as long as the underlying source responds to backpressure. Refer to [workerd's streams documentation](https://github.com/cloudflare/workerd/blob/main/src/workerd/api/streams/README.md#tee-behavior) for implementation details. + +* values(optionsObject) : AsyncIterableIterator + + * Returns an async iterator that reads and consumes the chunks of the stream. This method accepts an object argument indicating options. The only supported option is `preventCancel`, which, when `true`, prevents the stream from being canceled when the iterator exits early (for example, from a `break` statement). A `ReadableStream` is also async iterable directly: + +```js +for await (const chunk of readable) { + console.log(chunk); +} +``` + ### `PipeToOptions` @@ -50,6 +72,21 @@ let reader = readable.getReader({ mode: 'byob' }); * When `true`, errors in the source `ReadableStream` will no longer abort the destination `WritableStream`. `pipeTo` will return a rejected promise with the error from the source or any error that occurred while aborting the destination. +## Static methods + +* ReadableStream.from(asyncIterable) : ReadableStream + + * Creates a new `ReadableStream` whose chunks are the values yielded by `asyncIterable`, which may be any iterable or async iterable, including an async generator. + +```js +const stream = ReadableStream.from( + (async function* () { + yield 'hello '; + yield 'world'; + })() +); +``` + *** ## Related resources From 428273db4e429c39918eab609b2e0ab19b31f0c0 Mon Sep 17 00:00:00 2001 From: Nic <162764842+Nic-Polumeyv@users.noreply.github.com> Date: Mon, 3 Aug 2026 19:28:01 -0400 Subject: [PATCH 2/2] [Workers] Address review: tee() wording, TypeScriptExample --- .../runtime-apis/streams/readablestream.mdx | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/content/docs/workers/runtime-apis/streams/readablestream.mdx b/src/content/docs/workers/runtime-apis/streams/readablestream.mdx index ba62cd21a2f..6a934e9f43a 100644 --- a/src/content/docs/workers/runtime-apis/streams/readablestream.mdx +++ b/src/content/docs/workers/runtime-apis/streams/readablestream.mdx @@ -7,6 +7,8 @@ products: - workers --- +import { TypeScriptExample } from "~/components"; + ## Background A `ReadableStream` is returned by the `readable` property inside [`TransformStream`](/workers/runtime-apis/streams/transformstream/). @@ -46,18 +48,22 @@ let reader = readable.getReader({ mode: 'byob' }); * `tee()` : \[ReadableStream, ReadableStream] - * Locks the stream and returns an array of two new `ReadableStream` instances, each of which reads the same data as the original stream. Chunks are shared between the two branches, not copied, and backpressure to the underlying source follows the branch with the most unread data. This avoids unbounded buffering when one branch reads more slowly than the other, as long as the underlying source responds to backpressure. Refer to [workerd's streams documentation](https://github.com/cloudflare/workerd/blob/main/src/workerd/api/streams/README.md#tee-behavior) for implementation details. + * Locks the stream and returns an array of two new `ReadableStream` instances, each of which reads the same data as the original stream. Backpressure to the underlying source follows the branch with the most unread data, which avoids unbounded buffering when one branch reads more slowly than the other, as long as the underlying source responds to backpressure. Refer to [workerd's streams documentation](https://github.com/cloudflare/workerd/blob/main/src/workerd/api/streams/README.md#tee-behavior) for implementation details. * values(optionsObject) : AsyncIterableIterator * Returns an async iterator that reads and consumes the chunks of the stream. This method accepts an object argument indicating options. The only supported option is `preventCancel`, which, when `true`, prevents the stream from being canceled when the iterator exits early (for example, from a `break` statement). A `ReadableStream` is also async iterable directly: -```js + + +```ts for await (const chunk of readable) { console.log(chunk); } ``` + + ### `PipeToOptions` @@ -78,7 +84,9 @@ for await (const chunk of readable) { * Creates a new `ReadableStream` whose chunks are the values yielded by `asyncIterable`, which may be any iterable or async iterable, including an async generator. -```js + + +```ts const stream = ReadableStream.from( (async function* () { yield 'hello '; @@ -87,6 +95,8 @@ const stream = ReadableStream.from( ); ``` + + *** ## Related resources