Pipeline and cancellation
Local practice: Node.js 22.16.0 on Linux x64; proposed 26.13.1 baseline remains untested. Save each example separately as main.mjs in a disposable directory. No packages or external services required. HTTP examples bind only to loopback and shut down after a finite scenario. Static Node.js code, not verified ByteMind execution.
Decode a split UTF-8 character
import { Readable, Writable, Transform } from 'node:stream';import { pipeline, finished } from 'node:stream/promises';const bytes = Buffer.from('café');async function* decode(source){ const decoder = new TextDecoder('utf-8',{fatal:true}); for await(const chunk of source) yield decoder.decode(chunk,{stream:true}); yield decoder.decode();}let text='';await pipeline(Readable.from([bytes.subarray(0,4),bytes.subarray(4)]),decode,new Writable({write(c,e,cb){text+=c.toString();cb();}}));console.log(text);Run:
node main.mjsExpected stdout:
caféExit status: 0. Expected stderr: empty.
How it works
Use await pipeline(...) so the enclosing operation observes success or failure. In the first example, an async generator owns its decoding state through TextDecoder and emits text fragments while preserving a split UTF-8 sequence. This is not a CSV parser: quoting and line grammar need a separate parser. The second fixture cancels during a write, and verifies the destination is destroyed. Abort is cooperative for custom async work; pass the signal into any separately awaited operations and do not assume external side effects are rolled back.
Abort a bounded transfer
import { Readable, Writable, Transform } from 'node:stream';import { pipeline, finished } from 'node:stream/promises';const controller = new AbortController();const sink = new Writable({objectMode:true,write(n,e,cb){controller.abort();cb();}});try {await pipeline(Readable.from([1,2,3]),sink,{signal:controller.signal});}catch(e){console.log(e.name,sink.destroyed);}Run:
node main.mjsExpected stdout:
AbortError trueExit status: 0. Expected stderr: empty.
Common mistake
Do not treat cancellation as successful completion or assume that a partial destination is automatically deleted. For filesystem output, write to a temporary file and commit only after success, with appropriate cleanup.
Practice
Split the encoded character at a different byte offset and confirm the decoded result. Then explain which resources a canceled file transfer would need to release.
Further reading: Authoritative source 1. Internal reference routes remain proposed until repository discovery.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Pipeline and cancellation — knowledge check
Apply the concepts and predict the behavior taught in this lesson.
How do you pass cancellation to the promise pipeline?
Why use streaming TextDecoder?
Does abort undo bytes already written to disk?