I'm using the promise-ftp
Node.js library to download a file from an FTP server. Function ftpClient.get(...)
returns a Promise of ReadableStream, data of which should be accessible using data
and end events
, however, I encounter a weird behaviour that is not described in the documentation.
//Some code that returns a to-be-downloaded file's name
.then((fileName) => {
return ftpClient.get(fileName)
})
.then((fileStream) => {
let chunks = [];
return new Promise((resolve, reject) => {
fileStream.on('data', (chunk) => {
chunks.push(chunk);
});
fileStream.on('end', () => {
resolve(Buffer.concat(chunks).toString('utf8'));
});
});
});
This is a solution on how to read a stream into a variable that I derived from answers to this question. However, it doesn't work: the data
and end events
are just never emitted.
Comparing my code to this example in library documentation, the only difference is that the ReadableStream is piped somewhere beforehand.
Now, if I add fileStream.pipe(...)
,
//...
.then((fileStream) => {
fileStream.pipe(fs.createWriteStream('foo.txt'));
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
let chunks = [];
return new Promise((resolve, reject) => {
//...
everything works perfectly.
So my question is: what exactly is the purpose of adding a .pipe()
and how is it connected to data
and end events
?