0

I am trying to download (get) a file from sftp server using ssh2-sftp-client, the program does not terminate and hangs even after the events end, close is triggered on the ReadableStream and the events finish and close are triggered on the WriteableStream. Below is the program and it's output. There are no errors as well, how do I debug? what is happening? Any help would be very much appreciated

const Promise = require("bluebird");
const fs = require("fs");
const Client = require("ssh2-sftp-client");
const util = require("util");


const sftp = new Client();
const path = require("path");
const openpgp = require("openpgp"); // use as CommonJS, AMD, ES6 module or via window.openpgp

openpgp.initWorker({
    path: "openpgp.worker.js"
}); // set the relative web worker path

const config = {
    host: 'xxxx',
    port: '1111',
    username: 'xxxx',
    password: 'xxxx'
};

function getFileFromSFTP() {
    console.log("Program Execution started");
    /* get files from SFTP */
    const remotePath = "remoteFile";
    const localPath = "localFile"
    sftp.connect(config).then(() => {
        sftp.get(remotePath, true, null).then((data) => {
            data.on('error', (e) => {
                console.log(e);
            });
            data.on('end', () => {
                console.log("rs end");
            });
            data.on('close', () => {
                console.log("rs closed");
            });
            data.pipe(fs.createWriteStream(localPath).on('error', (e) => {
                console.log(e);
            }).on('finish', () => {
                console.log("ws finish");
            }).on('close', () => {
                console.log("ws close");
            }));
        });
    });
}

function init() {
    getFileFromSFTP();
}

init();

output

> node index.js

Program Execution started
rs end
ws finish
ws close
rs closed
vumaasha
  • 2,765
  • 4
  • 27
  • 41

0 Answers0