希望得到一些帮助。我是 Nodejs 的新手,想知道是否可以删除这个自定义事件发射器。我正在使用包“ssh2-sftp-client”,代码运行良好,但我从节点控制台收到警告,说
(node:67350) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 close listeners added to [Client]. Use emitter.setMaxListeners() to increase limit
但是我不确定在哪里清理事件,希望有人能给我一些建议。
async function executeFtpDownload() {
try {
await conn.connect({
host: params.ftpHost,
port: params.ftpPort,
user: params.ftpUser,
password: params.fptPassword,
});
const files = await conn.list("/home/bmw/autoline");
const targetFiles = files.filter((file) => {
return Object.values(targetFilesName).some(
(targetName) => targetName === file.name
);
});
const dirFiles = await Promise.all(
targetFiles.map(async (file) => {
await conn.downloadDir(
`/home/bmw/autoline/${file.name}`,
`./${params.LocalPath}/${file.name}`
);
const record = await conn.list(`/home/bmw/autoline/${file.name}`);
const dateRecord = record.reduce((obj, item) => {
const date = item.name.split("_")[0];
if (obj[date]) {
obj[date] = [...obj[date], item.name];
} else {
obj[date] = [item.name];
}
return obj;
}, {});
return {
[file.name]: dateRecord,
};
})
);
const folder_list = dirFiles.reduce((acc, item) => {
const key = Object.keys(item)[0];
const record = item[key];
return {
...acc,
[key]: record,
};
}, {});
const processUpload = await Promise.all(
Object.entries(folder_list).map((folder) => {
const folderName = folder[0];
return Promise.all(
Object.entries(folder[1]).map((subFolder) => {
const subFolderName = subFolder[0];
const files = subFolder[1];
return Promise.all(
files.map(async (file) => {
const fileContent = fs.createReadStream(
`./${params.LocalPath}/${folderName}/${file}`
);
const remove_remote = `/home/bmw/autoline/${folderName}/${file}`;
const remote = `/home/bmw/autoline/Uploaded${folderName}/${file}`;
const localPath = `./${params.LocalPath}/${folderName}/${file}`;
await s3
.putObject({
Body: fileContent,
Bucket: params.Bucket,
Key: `${folderName}/${subFolderName}/${file}`,
})
.promise();
await conn.fastPut(localPath, remote);
// await conn.delete(remove_remote);
await fs.unlink(
`./${params.LocalPath}/${folderName}/${file}`,
function (err) {
if (err) throw err;
// if no error, file has been deleted successfully
console.log(`File ${file} deleted!`);
}
);
})
);
})
);
})
);
console.log("FTPDOWNLOAD DONE");
conn.end();
return folder_list;
} catch (err) {
conn.end();
console.log("ERR executeFtpDownload:", err);
return {
statusCode: err.code,
body: err.name,
};
}
}