From 965a1d03ba82de5e24d67e9190439708c4cd1065 Mon Sep 17 00:00:00 2001 From: lyswhut Date: Wed, 13 Dec 2023 01:19:17 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=8B=E8=BD=BD=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E5=9C=A8=E6=95=B0=E6=8D=AE=E5=86=99=E5=85=A5=E9=80=9F?= =?UTF-8?q?=E5=BA=A6=E8=BE=83=E6=85=A2=E7=9A=84=E6=83=85=E5=86=B5=E4=B8=8B?= =?UTF-8?q?=E5=87=BA=E7=8E=B0=E4=BB=BB=E5=8A=A1=E5=8F=8A=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- publish/changeLog.md | 1 + src/common/utils/download/Downloader.ts | 60 ++++++++++++------------- 2 files changed, 31 insertions(+), 30 deletions(-) diff --git a/publish/changeLog.md b/publish/changeLog.md index 08c00eeb..a33b8ae4 100644 --- a/publish/changeLog.md +++ b/publish/changeLog.md @@ -27,6 +27,7 @@ - Windows、MacOS平台下的字体列表取消使用原生方式获取以修复某些字体应用后无效的问题(#1596) - 修复亮暗主题自动切换功能无效的问题(#1697) - 修复 MacOS 平台在 Finder 打开文件或目录时应用卡死的问题(#1684) +- 修复下载模块在数据写入速度较慢的情况下出现任务及文件异常的问题 ### 其他 diff --git a/src/common/utils/download/Downloader.ts b/src/common/utils/download/Downloader.ts index 920198d8..3957c9b0 100644 --- a/src/common/utils/download/Downloader.ts +++ b/src/common/utils/download/Downloader.ts @@ -39,6 +39,8 @@ class Task extends EventEmitter { requestInstance: http.ClientRequest | null = null maxRedirectNum = 2 private redirectNum = 0 + private dataWriteQueueLength = 0 + private closeWaiting = false constructor(url: string, savePath: string, filename: string, options: Partial = {}) { @@ -65,6 +67,8 @@ class Task extends EventEmitter { this.progress.downloaded = 0 this.progress.progress = 0 this.progress.speed = 0 + this.dataWriteQueueLength = 0 + this.closeWaiting = false if (startByte) this.requestOptions.headers!.range = `bytes=${startByte}-${endByte}` if (!path) return @@ -202,6 +206,7 @@ class Task extends EventEmitter { this.ws = fs.createWriteStream(this.chunkInfo.path, options) this.ws.on('finish', () => { + if (this.closeWaiting) return void this.__closeWriteStream() }) this.ws.on('error', err => { @@ -245,16 +250,21 @@ class Task extends EventEmitter { return } // console.log('close write stream') - this.ws.close(err => { - if (err) { - this.status = STATUS.error - this.emit('error', err) - reject(err) - return - } - this.ws = null - resolve() - }) + if (this.closeWaiting || this.dataWriteQueueLength) { + this.closeWaiting ||= true + this.ws.on('close', resolve) + } else { + this.ws.close(err => { + if (err) { + this.status = STATUS.error + this.emit('error', err) + reject(err) + return + } + this.ws = null + resolve() + }) + } }) } @@ -293,12 +303,16 @@ class Task extends EventEmitter { console.log('cancel write') return } + this.dataWriteQueueLength++ this.__calculateProgress(chunk.length) this.ws.write(chunk, err => { - if (!err) return - console.log(err) - this.__handleError(err) - void this.stop() + this.dataWriteQueueLength-- + if (err) { + console.log(err) + this.__handleError(err) + void this.stop() + } + if (this.closeWaiting && !this.dataWriteQueueLength) this.ws?.close() }) } @@ -322,22 +336,8 @@ class Task extends EventEmitter { } async __handleStop() { - return new Promise((resolve, reject) => { - this.__closeRequest() - if (this.ws) { - this.ws.close(err => { - if (err) { - reject(err) - this.emit('error', err) - return - } - this.ws = null - resolve() - }) - } else { - resolve() - } - }) + this.__closeRequest() + return this.__closeWriteStream() } __calculateProgress(receivedBytes: number) {