Skip to content

Commit 6c1bfb0

Browse files
committed
fix(file-uplaod): 解决MaxListenersExceededWarning: Possible EventEmitter memory leak detected的警告
1 parent a3a4d24 commit 6c1bfb0

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
# demo
2-
前端学习中的一些小实现
1+
# 前端学习中的一些小功能实现
2+

file-upload/server/controller.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ const pipeStream = (path, writeStream, chunkSize) =>
2323
highWaterMark: chunkSize
2424
});
2525
readStream.on('end', () => {
26-
// fse.unlinkSync(path);
27-
readStream.unpipe();
26+
fse.unlinkSync(path);
2827
resolve();
2928
});
3029
readStream.pipe(writeStream, { end: false });
@@ -37,14 +36,16 @@ const mergeFileChunk = async (fileHash, fileName, chunkSize) => {
3736
const chunkPaths = await fse.readdir(chunkDir);
3837
chunkPaths.sort((a, b) => a.slice(a.lastIndexOf('-') + 1) - b.slice(b.lastIndexOf('-') + 1));
3938
const writeStream = fse.createWriteStream(filePath);
39+
// 避免MaxListenersExceededWarning的警告
40+
writeStream.setMaxListeners(0);
4041
await Promise.all(chunkPaths.map((chunkPath, index) =>
4142
pipeStream(path.resolve(chunkDir, chunkPath), writeStream, chunkSize)
4243
)).then(() => {
4344
// close the stream to prevent memory leaks
4445
writeStream.close();
4546
return Promise.resolve(filePath);
4647
})
47-
// fse.rmdirSync(chunkDir); // 合并完成后删除切片目录
48+
fse.rmdirSync(chunkDir); // 合并完成后删除切片目录
4849
};
4950

5051
module.exports = {

0 commit comments

Comments
 (0)