Pipe chains and streaming UTF-8 decoding
Quick answer
Pipe chains connect stream stages and coordinate reading, writing and normal completion. TextDecoderStream decodes UTF-8 across chunk boundaries.
How it works
readable.pipeThrough(transform) returns the transform readable side. readable.pipeTo(writable) returns a promise for completion. Default piping propagates source errors to abort the destination, destination errors to cancel the source, and source close to destination close; options can suppress particular propagation. TextDecoderStream exposes readable and writable sides and handles split multibyte sequences. Await the final pipeTo and handle rejection.
Local examples
Run in a fresh browser document. Uses synthetic local data; actual ByteMind editor support is unverified.
(async () => {const out=[];const s=new ReadableStream({start(c){c.enqueue("x");c.close();}});await s.pipeTo(new WritableStream({write(v){out.push(v);},close(){out.push("done");}}));console.log(out.join(","));})()Expected console output:
x,doneDefault piping closes the destination after source exhaustion.
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(new Uint8Array([195]));c.enqueue(new Uint8Array([169]));c.close();}});const out=[];await s.pipeThrough(new TextDecoderStream()).pipeTo(new WritableStream({write(v){out.push(v);}}));console.log(out.join(""));})()Expected console output:
éThe decoder preserves the incomplete byte sequence across chunks.
Common mistakes
Do not read from a source that is already locked by piping. Do not parse arbitrary decoded chunks as complete JSON records.
Practice
Pipe a stream of split UTF-8 bytes to a decoder and collect text in a sink.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Pipe chains and streaming UTF-8 decoding — Knowledge Check
Apply the concept and reason about local example output.
What does pipeTo return?
Which operation returns the readable side of a transform chain?
Can TextDecoderStream reconstruct a character split across chunks?
What is the expected console output of Pipe chains and streaming UTF-8 decoding example 1?
(async () => {const out=[];const s=new ReadableStream({start(c){c.enqueue("x");c.close();}});await s.pipeTo(new WritableStream({write(v){out.push(v);},close(){out.push("done");}}));console.log(out.join(","));})()What is the expected console output of Pipe chains and streaming UTF-8 decoding example 2?
(async () => {const s=new ReadableStream({start(c){c.enqueue(new Uint8Array([195]));c.enqueue(new Uint8Array([169]));c.close();}});const out=[];await s.pipeThrough(new TextDecoderStream()).pipeTo(new WritableStream({write(v){out.push(v);}}));console.log(out.join(""));})()