Why does a Transform stream stop writing after frist write?

544 Views Asked by At

In the example below it writes "foo" to process.stdout once. Why doesn't it write "foo" 10 times?

const { Transform } = require('stream');

class Stream extends Transform {

    constructor(){
        super({autoDestroy : false, emitClose :false});
    }

    _transform(chunk, encoding, callback){
        this.push(chunk);
    }
}

let stream = new Stream();

stream.pipe(process.stdout, {end:false});

for (let i = 0; i < 10; i++) {
    stream.write('foo');
}
1

There are 1 best solutions below

0
stackhatter On

The reason it doesn't print "foo" 10 times is because I didn't carefully read the documentation:

The callback function must be called only when the current chunk is completely consumed. The first argument passed to the callback must be an Error object if an error occurred while processing the input or null otherwise. If a second argument is passed to the callback, it will be forwarded on to the readable.push()...

_transform(chunk, encoding, callback){
    this.push(chunk);
    callback();
}

fixes it.