1 | # Stream Response
|
2 | Fetch API `Response` objects made from async generators. Build streaming HTML responses or SSE with JS sugar.
|
3 |
|
4 | Example:
|
5 |
|
6 | ```js
|
7 | async function* generate() {
|
8 | for await (const row of iterAllRows()) {
|
9 | yield `${row.join(',')}\n`
|
10 | }
|
11 | }
|
12 |
|
13 | router.get('/large.csv', () => new StreamResponse(generate(), {
|
14 | headers: [['content-type', 'text/csv']]
|
15 | }))
|
16 | ```
|
17 |
|
18 | Creating a SSE endpoint works much the same way:
|
19 |
|
20 | ```js
|
21 | async function* sse() {
|
22 | while (true) {
|
23 | await new Promise(r => setTimeout(r, 1000));
|
24 | yield 'data: hello\n\n';
|
25 | }
|
26 | }
|
27 |
|
28 | router.get('/sse', contentType(['text/event-stream']), (req, { type }) => {
|
29 | return new StreamResponse(sse(), { headers: [['content-type', type]] })
|
30 | })
|
31 | ```
|
32 |
|
33 |
|
34 | *[SSE]: Server Sent Events
|
35 |
|
36 | --------
|
37 |
|
38 | <p align="center"><a href="https://workers.tools"><img src="https://workers.tools/assets/img/logo.svg" width="100" height="100" /></a>
|
39 | <p align="center">This module is part of the Worker Tools collection<br/>β
|
40 |
|
41 | [Worker Tools](https://workers.tools) are a collection of TypeScript libraries for writing web servers in [Worker Runtimes](https://workers.js.org) such as Cloudflare Workers, Deno Deploy and Service Workers in the browser.
|
42 |
|
43 | If you liked this module, you might also like:
|
44 |
|
45 | - π§ [__Worker Router__][router] --- Complete routing solution that works across CF Workers, Deno and Service Workers
|
46 | - π [__Worker Middleware__][middleware] --- A suite of standalone HTTP server-side middleware with TypeScript support
|
47 | - π [__Worker HTML__][html] --- HTML templating and streaming response library
|
48 | - π¦ [__Storage Area__][kv-storage] --- Key-value store abstraction across [Cloudflare KV][cloudflare-kv-storage], [Deno][deno-kv-storage] and browsers.
|
49 | - π [__Response Creators__][response-creators] --- Factory functions for responses with pre-filled status and status text
|
50 | - π [__Stream Response__][stream-response] --- Use async generators to build streaming responses for SSE, etc...
|
51 | - π₯ [__JSON Fetch__][json-fetch] --- Drop-in replacements for Fetch API classes with first class support for JSON.
|
52 | - π¦ [__JSON Stream__][json-stream] --- Streaming JSON parser/stingifier with first class support for web streams.
|
53 |
|
54 | Worker Tools also includes a number of polyfills that help bridge the gap between Worker Runtimes:
|
55 | - βοΈ [__HTML Rewriter__][html-rewriter] --- Cloudflare's HTML Rewriter for use in Deno, browsers, etc...
|
56 | - π [__Location Polyfill__][location-polyfill] --- A `Location` polyfill for Cloudflare Workers.
|
57 | - π¦ [__Deno Fetch Event Adapter__][deno-fetch-event-adapter] --- Dispatches global `fetch` events using Denoβs native HTTP server.
|
58 |
|
59 | [router]: https://workers.tools/router
|
60 | [middleware]: https://workers.tools/middleware
|
61 | [html]: https://workers.tools/html
|
62 | [kv-storage]: https://workers.tools/kv-storage
|
63 | [cloudflare-kv-storage]: https://workers.tools/cloudflare-kv-storage
|
64 | [deno-kv-storage]: https://workers.tools/deno-kv-storage
|
65 | [kv-storage-polyfill]: https://workers.tools/kv-storage-polyfill
|
66 | [response-creators]: https://workers.tools/response-creators
|
67 | [stream-response]: https://workers.tools/stream-response
|
68 | [json-fetch]: https://workers.tools/json-fetch
|
69 | [json-stream]: https://workers.tools/json-stream
|
70 | [request-cookie-store]: https://workers.tools/request-cookie-store
|
71 | [extendable-promise]: https://workers.tools/extendable-promise
|
72 | [html-rewriter]: https://workers.tools/html-rewriter
|
73 | [location-polyfill]: https://workers.tools/location-polyfill
|
74 | [deno-fetch-event-adapter]: https://workers.tools/deno-fetch-event-adapter
|
75 |
|
76 | Fore more visit [workers.tools](https://workers.tools).
|