You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
15
14
16
15
<DirectoryListing />
17
16
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:
19
18
20
-
* The visitor’s 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.
22
21
23
22
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.
24
23
25
24
:::note
26
25
27
-
28
26
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.
29
27
30
-
31
28
:::
32
29
33
30
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.
37
34
38
35
```js
39
36
exportdefault {
40
-
asyncfetch(request, env, ctx) {
41
-
// Fetch from origin server.
42
-
let response =awaitfetch(request);
43
-
44
-
// ... and deliver our Response while that’s running.
45
-
returnnewResponse(response.body, response);
46
-
}
47
-
}
37
+
asyncfetch(request, env, ctx) {
38
+
// Fetch from origin server.
39
+
constresponse=awaitfetch(request);
40
+
41
+
// ... and deliver our Response while that’s running.
// ... and deliver our Response while that’s running.
114
-
returnnewResponse(readable, response);
110
+
// ... and deliver our Response while that’s running.
111
+
returnnewResponse(readable, response);
115
112
}
116
113
```
117
114
@@ -121,22 +118,20 @@ This example calls `response.body.pipeTo(writable)` but does not `await` it. Thi
121
118
122
119
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.
123
120
124
-
***
121
+
---
125
122
126
123
## Common issues
127
124
128
125
:::caution[Warning]
129
126
130
-
131
127
The Streams API is only available inside of the [Request context](/workers/runtime-apis/request/), inside the `fetch` event listener callback.
132
128
133
-
134
129
:::
135
130
136
-
***
131
+
---
137
132
138
133
## Related resources
139
134
140
-
*[MDN’s 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