TransformStream: composable chunk processing
Quick answer
A TransformStream connects a writable input to a readable output. A transformer can map, filter or buffer chunks before emitting results.
How it works
new TransformStream({transform(chunk,controller),flush(controller)}) defines a transformation. controller.enqueue(value) emits output; flush handles remaining buffered state when input closes successfully. The two sides coordinate flow; use a pipe chain or consume the readable side while writing to avoid waiting forever on backpressure. Transform chunks are not necessarily one-to-one.
Local examples
Run in a fresh browser document. Uses synthetic local data; actual ByteMind editor support is unverified.
(async () => {const src=new ReadableStream({start(c){c.enqueue(2);c.enqueue(3);c.close();}});const t=new TransformStream({transform(n,c){c.enqueue(n*2);}});const r=src.pipeThrough(t).getReader();const a=await r.read(),b=await r.read(),d=await r.read();r.releaseLock();console.log(a.value,b.value,d.done);})()Expected console output:
4 6 trueBoth input chunks are transformed before completion.
Run in a fresh browser document. Uses synthetic local data; actual ByteMind editor support is unverified.
(async () => {const s=new ReadableStream({start(c){c.enqueue(0);c.enqueue(4);c.close();}});const t=new TransformStream({transform(n,c){if(n>0)c.enqueue(n);}});const r=s.pipeThrough(t).getReader();console.log((await r.read()).value,(await r.read()).done);r.releaseLock();})()Expected console output:
4 trueThe zero input produces no output chunk.
Common mistakes
Do not await writes into a transform indefinitely with nobody reading the output. Do not assume chunk boundaries match record boundaries.
Practice
Build a finite transform that doubles positive numbers and drops zero or negative numbers.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
TransformStream: composable chunk processing — Knowledge Check
Apply the concept and reason about local example output.
Can a transform emit more than one output chunk per input?
Which hook handles buffered remainder at successful input completion?
Why should the readable side be consumed while writing a transform?
What is the expected console output of TransformStream: composable chunk processing example 1?
(async () => {const src=new ReadableStream({start(c){c.enqueue(2);c.enqueue(3);c.close();}});const t=new TransformStream({transform(n,c){c.enqueue(n*2);}});const r=src.pipeThrough(t).getReader();const a=await r.read(),b=await r.read(),d=await r.read();r.releaseLock();console.log(a.value,b.value,d.done);})()What is the expected console output of TransformStream: composable chunk processing example 2?
(async () => {const s=new ReadableStream({start(c){c.enqueue(0);c.enqueue(4);c.close();}});const t=new TransformStream({transform(n,c){if(n>0)c.enqueue(n);}});const r=s.pipeThrough(t).getReader();console.log((await r.read()).value,(await r.read()).done);r.releaseLock();})()