Bu bağlamla ilgili sorunumun biraz farklı bir çözümünü buldum. Paylaşmaya değer düşündüm.
Örneğin çoğu readStreamsdosyadan oluşturulur. Ama benim durumumda bir mesaj havuzundan readStreamgelen JSONdize oluşturulmalıdır .
var jsonStream = through2.obj(function(chunk, encoding, callback) {
this.push(JSON.stringify(chunk, null, 4) + '\n');
callback();
});
// message.value --> value/text to write in write.txt
jsonStream.write(JSON.parse(message.value));
var writeStream = sftp.createWriteStream("/path/to/write/write.txt");
//"close" event didn't work for me!
writeStream.on( 'close', function () {
console.log( "- done!" );
sftp.end();
}
);
//"finish" event didn't work for me either!
writeStream.on( 'close', function () {
console.log( "- done!"
sftp.end();
}
);
// finally this worked for me!
jsonStream.on('data', function(data) {
var toString = Object.prototype.toString.call(data);
console.log('type of data:', toString);
console.log( "- file transferred" );
});
jsonStream.pipe( writeStream );
var r = request(...).on("end",function(){/* CALLBACK */}).pipe(...);