Public
Script
Readme

Response with ReadableStream example

Demonstrates our ability to handle streaming responses. We do, in practice, read the streams and then return the response - basically buffering - but with the support for the Response object we include the option to pass a ReadableStream instance as the first argument.

Note that it's required that the ReadableStream returns Uint8Array items.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
export let streamExample = () => {
const te = new TextEncoder();
let iterator = "<h1>Test</h1>".split("").map((t) => te.encode(t)).values();
const rs = new ReadableStream({
async pull(controller) {
const { value, done } = await iterator.next();
if (done) {
controller.close();
}
else {
controller.enqueue(value);
}
},
});
return new Response(rs, {
headers: {
"Content-Type": "text/html",
"x-content-type-options": "nosniff",
},
});
};
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
May 24, 2024