diff --git a/package.json b/package.json index ddc9ca89..70af5be1 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "qiniu-js", "jsName": "qiniu", - "version": "3.4.0", + "version": "3.4.1", "private": false, "description": "Javascript SDK for Qiniu Resource (Cloud) Storage AP", "main": "lib/index.js", diff --git a/site/package.json b/site/package.json index 077ed38c..fcca0430 100644 --- a/site/package.json +++ b/site/package.json @@ -43,7 +43,6 @@ "byte-size": "^7.0.1", "create-hmac": "^1.1.7", "js-base64": "^3.7.2", - "qiniu-js": "^3.4.0", "react": "^17.0.2", "react-dom": "^17.0.2" } diff --git a/site/src/upload.ts b/site/src/upload.ts index f1ca00fd..626f756a 100644 --- a/site/src/upload.ts +++ b/site/src/upload.ts @@ -1,6 +1,7 @@ import * as React from 'react' -import { upload } from 'qiniu-js' -import { UploadProgress } from 'qiniu-js/esm/upload' + +import { upload } from '../../src' +import { UploadProgress } from '../../src/upload' import { generateUploadToken, loadSetting } from './utils' diff --git a/site/yarn.lock b/site/yarn.lock index 6ff748ef..194917e9 100644 --- a/site/yarn.lock +++ b/site/yarn.lock @@ -23,14 +23,6 @@ chalk "^2.0.0" js-tokens "^4.0.0" -"@babel/runtime-corejs2@^7.10.2": - version "7.14.0" - resolved "https://registry.yarnpkg.com/@babel/runtime-corejs2/-/runtime-corejs2-7.14.0.tgz#5519b92ccc819bd3d0a1ecb25ab3cb5a775485f8" - integrity sha512-btR4E8JiGlmmDI5YgirlG9z3T91rBdxnVh2YuEStrHDcekffaaIeK+CE0S4IaYUyYhMa7rFDfF2GEO79XNbLEA== - dependencies: - core-js "^2.6.5" - regenerator-runtime "^0.13.4" - "@babel/runtime-corejs3@^7.10.2": version "7.14.0" resolved "https://registry.yarnpkg.com/@babel/runtime-corejs3/-/runtime-corejs3-7.14.0.tgz#6bf5fbc0b961f8e3202888cb2cd0fb7a0a9a3f66" @@ -1077,11 +1069,6 @@ core-js-pure@^3.0.0: resolved "https://registry.yarnpkg.com/core-js-pure/-/core-js-pure-3.12.1.tgz#934da8b9b7221e2a2443dc71dfa5bd77a7ea00b8" integrity sha512-1cch+qads4JnDSWsvc7d6nzlKAippwjUlf6vykkTLW53VSV+NkE6muGBToAjEA8pG90cSfcud3JgVmW2ds5TaQ== -core-js@^2.6.5: - version "2.6.12" - resolved "https://registry.yarnpkg.com/core-js/-/core-js-2.6.12.tgz#d9333dfa7b065e347cc5682219d6f690859cc2ec" - integrity sha512-Kb2wC0fvsWfQrgk8HU5lW6U/Lcs8+9aaYcy4ZFc6DDlo4nZ7n70dEgE5rtR0oG6ufKDUnrwfWL1mXR5ljDatrQ== - core-util-is@~1.0.0: version "1.0.2" resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7" @@ -3810,15 +3797,6 @@ punycode@^2.1.0: resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.1.1.tgz#b58b010ac40c22c5657616c8d2c2c02c7bf479ec" integrity sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A== -qiniu-js@^3.4.0: - version "3.4.0" - resolved "https://registry.yarnpkg.com/qiniu-js/-/qiniu-js-3.4.0.tgz#36980de07be4bab229ec0193a32c3318aa24b748" - integrity sha512-QJ5ShXQkyPuhMXEruyLza3V++8crvDNLTFOXzuMXrnXCvwtEbP+MZyYo4D96HRXnSFb6lAJ9s2QdzW1W3LBiIQ== - dependencies: - "@babel/runtime-corejs2" "^7.10.2" - querystring "^0.2.1" - spark-md5 "^3.0.0" - qs@6.7.0: version "6.7.0" resolved "https://registry.yarnpkg.com/qs/-/qs-6.7.0.tgz#41dc1a015e3d581f1621776be31afb2876a9b1bc" @@ -3829,11 +3807,6 @@ querystring@0.2.0: resolved "https://registry.yarnpkg.com/querystring/-/querystring-0.2.0.tgz#b209849203bb25df820da756e747005878521620" integrity sha1-sgmEkgO7Jd+CDadW50cAWHhSFiA= -querystring@^0.2.1: - version "0.2.1" - resolved "https://registry.yarnpkg.com/querystring/-/querystring-0.2.1.tgz#40d77615bb09d16902a85c3e38aa8b5ed761c2dd" - integrity sha512-wkvS7mL/JMugcup3/rMitHmd9ecIGd2lhFhK9N3UUQ450h66d1r3Y9nvXzQAW1Lq+wyx61k/1pfKS5KuKiyEbg== - querystringify@^2.1.1: version "2.2.0" resolved "https://registry.yarnpkg.com/querystringify/-/querystringify-2.2.0.tgz#3345941b4153cb9d082d8eee4cda2016a9aef7f6" @@ -4439,11 +4412,6 @@ source-map@~0.7.2: resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.7.3.tgz#5302f8169031735226544092e64981f751750383" integrity sha512-CkCj6giN3S+n9qrYiBTX5gystlENnRW5jZeNLHpe6aue+SrHcG5VYwujhW9s4dY31mEGsxBDrHR6oI69fTXsaQ== -spark-md5@^3.0.0: - version "3.0.1" - resolved "https://registry.yarnpkg.com/spark-md5/-/spark-md5-3.0.1.tgz#83a0e255734f2ab4e5c466e5a2cfc9ba2aa2124d" - integrity sha512-0tF3AGSD1ppQeuffsLDIOWlKUd3lS92tFxcsrh5Pe3ZphhnoK+oXIBTzOAThZCiuINZLvpiLH/1VS1/ANEJVig== - spdx-correct@^3.0.0: version "3.1.1" resolved "https://registry.yarnpkg.com/spdx-correct/-/spdx-correct-3.1.1.tgz#dece81ac9c1e6713e5f7d1b6f17d468fa53d89a9" diff --git a/src/upload/base.ts b/src/upload/base.ts index 527a8296..bff64dd7 100644 --- a/src/upload/base.ts +++ b/src/upload/base.ts @@ -265,19 +265,22 @@ export default abstract class Base { this.sendLog(result.reqId, 200) return } catch (err) { - this.logger.error(err) - this.clear() + if (this.aborted) { + this.logger.warn('upload is aborted.') + this.sendLog('', -2) + return + } + this.clear() + this.logger.error(err) if (err instanceof QiniuRequestError) { - const reqId = this.aborted ? '' : err.reqId - const code = this.aborted ? -2 : err.code - this.sendLog(reqId, code) + this.sendLog(err.reqId, err.code) // 检查并冻结当前的 host this.checkAndFreezeHost(err) const notReachRetryCount = ++this.retryCount <= this.config.retryCount - const needRetry = !this.aborted && RETRY_CODE_LIST.includes(err.code) + const needRetry = RETRY_CODE_LIST.includes(err.code) // 以下条件满足其中之一则会进行重新上传: // 1. 满足 needRetry 的条件且 retryCount 不为 0 @@ -294,17 +297,16 @@ export default abstract class Base { } private clear() { - this.logger.info('start cleaning all xhr.') this.xhrList.forEach(xhr => { xhr.onreadystatechange = null xhr.abort() }) - this.logger.info('cleanup completed.') this.xhrList = [] + this.logger.info('cleanup uploading xhr.') } public stop() { - this.logger.info('stop.') + this.logger.info('aborted.') this.clear() this.aborted = true } diff --git a/src/upload/resume.ts b/src/upload/resume.ts index ae8f271e..a0fa217e 100644 --- a/src/upload/resume.ts +++ b/src/upload/resume.ts @@ -92,7 +92,14 @@ export default class Resume extends Base { await this.initBeforeUploadChunks() const pool = new utils.Pool( - (chunkInfo: ChunkInfo) => this.uploadChunk(chunkInfo), + async (chunkInfo: ChunkInfo) => { + if (this.aborted) { + pool.abort() + throw new Error('pool is aborted') + } + + await this.uploadChunk(chunkInfo) + }, this.config.concurrentRequestLimit ) diff --git a/src/utils/crc32.test.ts b/src/utils/crc32.test.ts index 034a0360..921504c4 100644 --- a/src/utils/crc32.test.ts +++ b/src/utils/crc32.test.ts @@ -8,7 +8,7 @@ function mockFile(size = 4, name = 'mock.jpg', type = 'image/jpg'): File { return new File([blob], name) } -describe('test crc32', async () => { +describe('test crc32', () => { test('file', async () => { const crc32One = new CRC32() await expect(crc32One.file(mockFile(0))).resolves.toEqual(0) diff --git a/src/utils/pool.ts b/src/utils/pool.ts index d628ab0b..5172c7a1 100644 --- a/src/utils/pool.ts +++ b/src/utils/pool.ts @@ -7,6 +7,7 @@ export interface QueueContent { } export class Pool { + aborted = false queue: Array> = [] processing: Array> = [] @@ -23,7 +24,7 @@ export class Pool { }) } - run(item: QueueContent) { + private run(item: QueueContent) { this.queue = this.queue.filter(v => v !== item) this.processing.push(item) this.runTask(item.task).then( @@ -36,11 +37,17 @@ export class Pool { ) } - check() { + private check() { + if (this.aborted) return const processingNum = this.processing.length const availableNum = this.limit - processingNum this.queue.slice(0, availableNum).forEach(item => { this.run(item) }) } + + abort() { + this.queue = [] + this.aborted = true + } }