mscdex / ssh2-streams

SSH2 and SFTP client/server protocol streams for node.js
MIT License
204 stars 143 forks source link

How to capture connection errors in the SFTP read stream #130

Open dvlato opened 5 years ago

dvlato commented 5 years ago

I have some code that can access a number of different network/file resources, including a SFTP server. This code gets a Stream from any of those different resources and then pipes it doing some processing.

Our problem is that for ssh2-sftp-client, if the connection drops the stream does not fail. We have added keepAliveInterval to have an error event when the connection drops, but that event does not make our stream fail. Are we doing anything incorrectly? We would like the stream to fail so we can capture the error, is that possible? Otherwise, what would be your recommended approach to make sure the download() method below fails?

Sample pseudocode where client is an abstraction over ssh2 sftpclient (with some other implementations):

async download() { await client.connect(params); const stream = await client.getReadStream(name); return new Promise((resolve, reject) => { stream .on('error', err => { LOG.error(Error {err}, Full stack: ${err.}); reject(err); }) .pipe(unzipper.Parse())... }

dvlato commented 5 years ago

We solved this by propagating the 'ssh level' errors to users of the 'client' object... Quite unfortunate but we haven't found any other way.

We'd still love to have the stream fail when the connection is closed/lost as it is the case for all the other libraries we are using to upload to different services.