stream.Transform
A duplex stream that transforms input chunks into output chunks.
Syntax
new Transform({ transform(chunk, enc, cb) {} }) Returns
Transform — A stream that is both readable and writable.
Examples
import { Transform } from 'node:stream';
const upper = new Transform({
transform(chunk, _e, cb) {
cb(null, chunk.toString().toUpperCase());
},
});
upper.on('data', (d) => console.log(d.toString()));
upper.end('hello');
Output
HELLO
import { Readable, Transform } from 'node:stream';
import { pipeline } from 'node:stream/promises';
const double = new Transform({
objectMode: true,
transform(n, _e, cb) { cb(null, n * 2); },
});
await pipeline(Readable.from([1, 2, 3]), double, async function (src) {
for await (const v of src) console.log(v);
});
Output
2
4
6
Notes
Call `cb(null, output)` to push transformed data, or `cb(err)` to
fail. Set `objectMode: true` to pass non-Buffer values. Implement
`flush(cb)` to emit trailing data after input ends.