A transform stream sits between a producer and a consumer, changing chunks while data is still flowing. In the Web Streams API, a TransformStream connects a writable side to a readable side through a transform function. Converting text from one encoding to another is one use. section 10.1 section 10.4.3
The two sides have roughly the same dual-buffer structure as a Node.js transform. If the readable queue fills, backpressure reaches the writable side, which stops accepting data until downstream catches up. This only protects the pipeline when callers respect the signal: application code can still push data in ways that bypass the normal pace. section 10.4.3 section 10.6.2
The failure can be cumulative. Each transform adds places for chunks to wait; several stages can turn ignored backpressure into growing buffers, garbage-collection pressure, and eventual memory exhaustion. Use pipeThrough() and pipeTo() rather than manually forwarding chunks: built-in piping carries backpressure and errors through the pipeline and handles cleanup. section 10.6.2 section 10.8
// Web Streams
const transform = new TransformStream({
async transform(chunk, controller) {
const result = processChunk(chunk); // #A
controller.enqueue(result);
}
});The transform callback processes a chunk and enqueues its result. Marking this callback async when processChunk() is synchronous adds promise work without making the computation asynchronous. section 10.7.1
TransformStream is part of the WHATWG Streams Standard, which specifies observable behavior rather than a runtime’s allocation strategy. Implementations can therefore have different allocation profiles. Web Streams are the portable fit for data already arriving through fetch(); they do not erase the per-chunk promise costs of their architecture. section 10.2 section 10.4 section 10.7.1
