Seeing data loss when serving ReadableStream from worker

I have a generator which returns fixed-size chunks (tested, eg., 16kB and 32kB chunks), and I take those chunks and pass them out via controller.enqueue() calls. Some of those chunks then vanish before reaching the other end of the connection.

No errors in the connection itself. It looks like the controller object just misplaces them.

Am I doing this right?
        const generator = pageGenerator(hash, url.pathname);
        const stream = new ReadableStream({
            async pull(controller) {
                const { value, done } = generator.next();
                if (done) {
                    controller.close();
                } else {
                    controller.enqueue(value);
                }
            },
            cancel() {
                generator.return(undefined);
            }
        });
        return new Response(stream, { headers: html_headers });
Was this page helpful?