Transform Streams
Modify data as it flows through a pipeline.
Transform Streams is a free JavaScript Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is a TransformStream?
A TransformStream sits between a readable and a writable side. Chunks written in are transformed and emitted out — a pipe stage that maps, filters, or reshapes data on the fly.
Anatomy
It has two ends: writable (where data goes in) and readable (where transformed data comes out).
const ts = new TransformStream();
console.log(ts.writable, ts.readable);The transform Method
Define behavior with a transform(chunk, controller) function. Call controller.enqueue() to emit output.
const upper = new TransformStream({
transform(chunk, controller) {
controller.enqueue(chunk.toUpperCase());
}
});One In, Many Out
A transform may emit zero, one, or several chunks per input — perfect for splitting or expanding data.
const splitter = new TransformStream({
transform(line, controller) {
for (const word of line.split(" ")) controller.enqueue(word);
}
});Filtering with Transforms
Simply do not enqueue chunks you want to drop.
const onlyEven = new TransformStream({
transform(n, controller) {
if (n % 2 === 0) controller.enqueue(n);
}
});The flush Method
flush(controller) runs once when input ends — ideal for emitting buffered leftovers or a final chunk.
const ts = new TransformStream({
transform(c, ctrl) { ctrl.enqueue(c); },
flush(ctrl) { ctrl.enqueue("[END]"); }
});Buffering Across Chunks
Keep state in the closure to join partial chunks, such as assembling complete lines from arbitrary byte boundaries.
let buffer = "";
new TransformStream({
transform(chunk, ctrl) {
buffer += chunk;
const parts = buffer.split("\n");
buffer = parts.pop();
for (const line of parts) ctrl.enqueue(line);
},
flush(ctrl) { if (buffer) ctrl.enqueue(buffer); }
});Built-in Transform Streams
The platform ships TextDecoderStream, TextEncoderStream, and CompressionStream/DecompressionStream — ready-made transforms.
const gzip = new CompressionStream("gzip");
const compressed = inputStream.pipeThrough(gzip);Connecting with pipeThrough
readable.pipeThrough(transform) wires a source into a transform and returns the transform's readable output, ready for the next stage.
const out = sourceStream
.pipeThrough(new TextDecoderStream())
.pipeThrough(upper);Async Transforms
transform may be async — await inside it to call APIs or do async work per chunk. The stream waits for the promise.
new TransformStream({
async transform(chunk, ctrl) {
const result = await process(chunk);
ctrl.enqueue(result);
}
});Error Propagation
Throwing inside transform errors the stream; downstream consumers see the error and the pipeline tears down.
new TransformStream({
transform(chunk, ctrl) {
if (!valid(chunk)) throw new Error("bad chunk");
ctrl.enqueue(chunk);
}
});Quick Check
Test transform streams.
Recap: Transform Streams
You built transforms with transform and flush, mapped/filtered/expanded chunks, buffered across chunk boundaries, used built-ins like CompressionStream, and wired stages with pipeThrough. Next: backpressure and piping.
Frequently asked questions
Is the “Transform Streams” lesson free?
Yes — the full text of “Transform Streams” is free to read here on the web, and the JavaScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the JavaScript Academy course, upgrade to CoddyKit PRO.
What will I learn in “Transform Streams”?
Modify data as it flows through a pipeline. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start JavaScript Academy?
No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Transform Streams” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this JavaScript Academy lesson?
Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Readable Streams
- Streaming fetch Responses
- Transform Streams
- Backpressure and Piping