JavaScript Academy · Lesson

Transform Streams

Modify data as it flows through a pipeline.

Lesson 3 of 413 steps

Transform Streams is a free JavaScript Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, and your progress syncs across the web and the CoddyKit app. The JavaScript Academy course includes 4 lessons in total.

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.

Free to start

Learn JavaScript with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
59
Lessons
200

Frequently Asked Questions

Is the “Transform Streams” lesson free?

Yes — any 3 lessons of the JavaScript Academy learning path, including “Transform Streams”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The JavaScript Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.

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

  1. Readable Streams
  2. Streaming fetch Responses
  3. Transform Streams
  4. Backpressure and Piping
← Back to JavaScript Academy