Skip to content

Commit 5c413eb

Browse files
vicbpedrosousa
andauthored
[Workers] Update Stream documentation (#17506)
--------- Co-authored-by: Pedro Sousa <[email protected]>
1 parent 692e949 commit 5c413eb

File tree

1 file changed

+53
-58
lines changed
  • src/content/docs/workers/runtime-apis/streams

1 file changed

+53
-58
lines changed

src/content/docs/workers/runtime-apis/streams/index.mdx

Lines changed: 53 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,25 @@ head:
66
content: Streams - Runtime APIs
77
description: A web standard API that allows JavaScript to programmatically
88
access and process streams of data.
9-
109
---
1110

12-
import { DirectoryListing, TabItem, Tabs } from "~/components"
11+
import { DirectoryListing, TabItem, Tabs } from "~/components";
1312

1413
The [Streams API](https://developer.mozilla.org/en-US/docs/Web/API/Streams_API) is a web standard API that allows JavaScript to programmatically access and process streams of data.
1514

1615
<DirectoryListing />
1716

18-
Workers do not need to prepare an entire response body before delivering it to `event.respondWith()`. You can use [`TransformStream`](/workers/runtime-apis/streams/transformstream/) to stream a response body after sending the front matter (that is, HTTP status line and headers). This allows you to minimize:
17+
Workers do not need to prepare an entire response body before returning a `Response`. You can use a [`ReadableStream`](/workers/runtime-apis/streams/readablestream/) to stream a response body after sending the front matter (that is, HTTP status line and headers). This allows you to minimize:
1918

20-
* The visitors time-to-first-byte.
21-
* The buffering done in the Worker.
19+
- The visitor's time-to-first-byte.
20+
- The buffering done in the Worker.
2221

2322
Minimizing buffering is especially important for processing or transforming response bodies larger than the Worker's memory limit. For these cases, streaming is the only implementation strategy.
2423

2524
:::note
2625

27-
2826
By default, Cloudflare Workers is capable of streaming responses using the [Streams APIs](https://developer.mozilla.org/en-US/docs/Web/API/Streams_API). To maintain the streaming behavior, you should only modify the response body using the methods in the Streams APIs. If your Worker only forwards subrequest responses to the client verbatim without reading their body text, then its body handling is already optimal and you do not have to use these APIs.
2927

30-
3128
:::
3229

3330
The worker can create a `Response` object using a `ReadableStream` as the body. Any data provided through the
@@ -37,29 +34,29 @@ The worker can create a `Response` object using a `ReadableStream` as the body.
3734

3835
```js
3936
export default {
40-
async fetch(request, env, ctx) {
41-
// Fetch from origin server.
42-
let response = await fetch(request);
43-
44-
// ... and deliver our Response while that’s running.
45-
return new Response(response.body, response);
46-
}
47-
}
37+
async fetch(request, env, ctx) {
38+
// Fetch from origin server.
39+
const response = await fetch(request);
40+
41+
// ... and deliver our Response while that’s running.
42+
return new Response(response.body, response);
43+
},
44+
};
4845
```
4946

5047
</TabItem> <TabItem label="Service Worker" icon="seti:javascript">
5148

5249
```js
53-
addEventListener('fetch', event => {
54-
event.respondWith(fetchAndStream(event.request));
50+
addEventListener("fetch", (event) => {
51+
event.respondWith(fetchAndStream(event.request));
5552
});
5653

5754
async function fetchAndStream(request) {
58-
// Fetch from origin server.
59-
let response = await fetch(request);
55+
// Fetch from origin server.
56+
const response = await fetch(request);
6057

61-
// ... and deliver our Response while that’s running.
62-
return new Response(readable.body, response);
58+
// ... and deliver our Response while that’s running.
59+
return new Response(readable.body, response);
6360
}
6461
```
6562

@@ -71,47 +68,47 @@ A [`TransformStream`](/workers/runtime-apis/streams/transformstream/) and the [`
7168

7269
```js
7370
export default {
74-
async fetch(request, env, ctx) {
75-
// Fetch from origin server.
76-
let response = await fetch(request);
77-
78-
let { readable, writable } = new TransformStream({
79-
transform(chunk, controller) {
80-
controller.enqueue(modifyChunkSomehow(chunk));
81-
}
82-
});
83-
84-
// Start pumping the body. NOTE: No await!
85-
response.body.pipeTo(writable);
86-
87-
// ... and deliver our Response while that’s running.
88-
return new Response(readable, response);
89-
}
90-
}
71+
async fetch(request, env, ctx) {
72+
// Fetch from origin server.
73+
const response = await fetch(request);
74+
75+
const { readable, writable } = new TransformStream({
76+
transform(chunk, controller) {
77+
controller.enqueue(modifyChunkSomehow(chunk));
78+
},
79+
});
80+
81+
// Start pumping the body. NOTE: No await!
82+
response.body.pipeTo(writable);
83+
84+
// ... and deliver our Response while that’s running.
85+
return new Response(readable, response);
86+
},
87+
};
9188
```
9289

9390
</TabItem> <TabItem label="Service Worker" icon="seti:javascript">
9491

9592
```js
96-
addEventListener('fetch', event => {
97-
event.respondWith(fetchAndStream(event.request));
93+
addEventListener("fetch", (event) => {
94+
event.respondWith(fetchAndStream(event.request));
9895
});
9996

10097
async function fetchAndStream(request) {
101-
// Fetch from origin server.
102-
let response = await fetch(request);
98+
// Fetch from origin server.
99+
const response = await fetch(request);
103100

104-
let { readable, writable } = new TransformStream({
105-
transform(chunk, controller) {
106-
controller.enqueue(modifyChunkSomehow(chunk));
107-
}
108-
});
101+
const { readable, writable } = new TransformStream({
102+
transform(chunk, controller) {
103+
controller.enqueue(modifyChunkSomehow(chunk));
104+
},
105+
});
109106

110-
// Start pumping the body. NOTE: No await!
111-
response.body.pipeTo(writable);
107+
// Start pumping the body. NOTE: No await!
108+
response.body.pipeTo(writable);
112109

113-
// ... and deliver our Response while that’s running.
114-
return new Response(readable, response);
110+
// ... and deliver our Response while that’s running.
111+
return new Response(readable, response);
115112
}
116113
```
117114

@@ -121,22 +118,20 @@ This example calls `response.body.pipeTo(writable)` but does not `await` it. Thi
121118

122119
The runtime can continue running a function (`response.body.pipeTo(writable)`) after a response is returned to the client. This example pumps the subrequest response body to the final response body. However, you can use more complicated logic, such as adding a prefix or a suffix to the body or to process it somehow.
123120

124-
***
121+
---
125122

126123
## Common issues
127124

128125
:::caution[Warning]
129126

130-
131127
The Streams API is only available inside of the [Request context](/workers/runtime-apis/request/), inside the `fetch` event listener callback.
132128

133-
134129
:::
135130

136-
***
131+
---
137132

138133
## Related resources
139134

140-
* [MDNs Streams API documentation](https://developer.mozilla.org/en-US/docs/Web/API/Streams_API)
141-
* [Streams API spec](https://streams.spec.whatwg.org/)
142-
* Write your Worker code in [ES modules syntax](/workers/reference/migrate-to-module-workers/) for an optimized experience.
135+
- [MDN's Streams API documentation](https://developer.mozilla.org/en-US/docs/Web/API/Streams_API)
136+
- [Streams API spec](https://streams.spec.whatwg.org/)
137+
- Write your Worker code in [ES modules syntax](/workers/reference/migrate-to-module-workers/) for an optimized experience.

0 commit comments

Comments
 (0)