sanketbajoria / ssh2-promise

ssh with promise/async await and typescript support
https://www.npmjs.com/package/ssh2-promise
MIT License
148 stars 25 forks source link

`exec` - Rejected promises because of `stderr` output only provide the first chunk of data encountered #76

Closed tjbrockmeyer closed 2 years ago

tjbrockmeyer commented 2 years ago

With a command such as the following:

mkdir abc && mkdir abc

I would expect to see the full error message - something along the lines of:

mkdir: cannot create directory 'abc': File exists

However, the error that is seen is simply this:

mkdir:

And on top of that, the data type of the error is Buffer instead of string as one would expect.

The issue arises because of the way stderr output is handled. Any time that stdout output is received, it is put into a 'buffer', which is actually just a string, and then when the stream is closed, the buffer is returned. With stderr output, the code immediately rejects the promise with the received chunk of data (which is of type Buffer - not string), and so the output is of an inconsistent type, and it is also incomplete in content. I propose the following fix:

Mark the operation as a failure upon receiving the first bit of output from stderr, but do not reject immediately. Instead, push the output into a second buffer which is for error output only. After the connection closes, we can call reject(errorBuffer) if any stderr output came through to reject the promise with the full output instead of only partial output. Additionally, this will fix the issue of inconsistent data types, because the error buffer will always be a string.

I am willing to fork and write a PR for this change, as I've already explored the code for it.

sanketbajoria commented 2 years ago

@tjbrockmeyer awesome.. please feel free to raise a PR.