Attaching a transform stream via .pipe doesn't seem to handle all the events automatically. This example:
const stream = require('stream');
const readable1 = stream.Readable.from([1, 2, 3])
.on('data', (data) => console.log(data))
.on('end', () => console.log("readable1 is ended"))
.on('close', () => console.log("readable1 is closed"));
const readable2 = stream.Readable.from([4, 5, 6])
.pipe(new stream.Transform({
objectMode: true,
transform(item, encoding, callback) {
console.debug(`transform ${item}`);
callback();
}
}))
.on('end', () => console.log("transform is ended"))
.on('close', () => console.log("transform is closed"));
would produce
1
2
3
transform 4
transform 5
transform 6
readable1 is ended
readable1 is closed
There are no traces of transform stream end or close events.
However if i add this code at the end
readable2
.on('data', (data) => console.log(data)) // for some reason it doesn't work without 'data' event handler
.on('end', () => console.log("readable2 is ended"))
.on('close', () => console.log("readable2 is closed"));
suddenly i get all the log lines
1
2
3
transform 4
transform 5
transform 6
readable1 is ended
transform is ended
readable2 is ended
transform is closed
readable2 is closed
readable1 is closed
Obviously i'm missing something quite important here. Couldn't find an answer in Node.js documentation. The main question is how to implement Transform stream properly so that it handles all the events?