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

Cannot create writeStream #75

Closed ScottRandallWay closed 2 years ago

ScottRandallWay commented 2 years ago

We had existing code that used ssh2 to write a string to a file and it worked for several years. We upgraded to ssh2-promise now it seems we are not getting a valid writestream. We just keep getting writestream.write is not a function. Are we doing something wrong?

                let conn = new SSH2Promise(this.connSettings);
                var sftp = conn.sftp();
                var writeStream = sftp.createWriteStream(targetFile).then(() => {
                    writeStream.write(dataContent, {mode: 0o777});
                    writeStream.end();
                    writeStream.on('close', () => {
                        conn.close();
                        resolve(targetFile);
                    })
                }).catch ((err) => {
                    logger.error({
                        object: 'Scp',
                        op: 'writeData',
                        targetFile: targetFile,
                        err: err
                    }, 'op: Scp.writeData, targetFile: ' + targetFile + ', error: ' + err);
                    reject('WriteStream Failed');
                });
ScottRandallWay commented 2 years ago

Additionally we figured out this code seems to work fine on windows (our dev workstations), but is failing on Centos 7 which is our dev and test servers.

CRAKZOR commented 2 years ago

Additionally we figured out this code seems to work fine on windows (our dev workstations), but is failing on Centos 7 which is our dev and test servers.

use node version <= 17.8.0

sanketbajoria commented 2 years ago

@ScottRandallWay Actually you are doing little bit wrong. We can access WriteStream in below manner, if using promise (thenable)

sftp.createWriteStream(targetFile).then((writeStream) => {
                    writeStream.write(dataContent, {mode: 0o777});
});

I will update readme around it.

For example: https://github.com/sanketbajoria/ssh2-promise/blob/master/spec/sftp-spec.js#L73