Commit Detail
Commit 6b58166
Fixup TransformStream backpressure
```
export default {
async fetch(req, env, ctx) {
async function sleep(ns) {
await new Promise((res) => setTimeout(res, ns));
}
const strategy = new ByteLengthQueuingStrategy({ highWaterMark: 2024 });
const { writable, readable } = new TransformStream(undefined, strategy);
let writes = 0, reads = 0;
// Start writing chunks to the stream
async function write(writable) {
const writer = writable.getWriter();
for (let n = 0; n < 10000; n++) {
console.log('waiting on backpressure', writer.ready, writer.desiredSize);
await writer.ready;
console.log('backpressure cleared. writing a chunk');
writer.write(crypto.getRandomValues(new Uint8Array(1024)));
console.log('chunk written', ++writes, writer.desiredSize);
}
await writer.close();
console.log('done writing');
}
async function read(readable) {
console.log('reading pending...');
await sleep(5_000);
// const reader = readable.getReader();
// console.log(await reader.read());
console.log('reading....');
for await (const chunk of readable) {
console.log(`read ${chunk.length}`, ++reads);
await sleep(100);
}
console.log('done reading');
}
await Promise.all([
write(writable),
read(readable)
]);
return new Response("Hello World\n");
}
};
```
Files changed
2 files changed~2 modified