0

我正在尝试使用 sftp 服务器下载(获取)文件,即使在ReadableStream上触发事件并且在 WriteableStream 上触发ssh2-sftp-client事件后,程序也不会终止并挂起。下面是程序及其输出。也没有错误,我该如何调试?怎么了?任何帮助将不胜感激end, closefinish and close

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();

输出

> node index.js

Program Execution started
rs end
ws finish
ws close
rs closed
4

0 回答 0