Skip to content

Commit f0834f3

Browse files
committed
优化
1 parent 1970039 commit f0834f3

File tree

7 files changed

+27
-23
lines changed

7 files changed

+27
-23
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ install:
1313
script:
1414
- npm run lint
1515
- npm run test
16+
- npx codecov
1617
- npm run build
1718

1819
before_deploy:

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
"types": "esm/index.d.ts",
99
"module": "esm/index.js",
1010
"scripts": {
11+
"test": "jest --coverage",
1112
"clean": "del \"./(lib|dist|esm)\"",
12-
"test": "jest --coverage && npx codecov",
1313
"build": "npm run clean && tsc && babel esm --out-dir lib && webpack --optimize-minimize --config webpack.prod.js",
14-
"dev": "npm run lint && webpack-dev-server --open --config webpack.dev.js",
15-
"lint": "eslint --ext .ts src/",
14+
"dev": "webpack-dev-server --open --config webpack.dev.js",
15+
"lint": "tsc --noEmit && eslint --ext .ts src/",
1616
"server": "node test/server.js"
1717
},
1818
"repository": {

src/api/index.mock.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ import * as api from '.'
44
export const errorMap = {
55
networkError: new QiniuNetworkError('mock', 'message'), // 网络错误
66

7-
invalidParams: new QiniuRequestError(400, 'mock', 'message', {}), // 无效的参数
8-
expiredToken: new QiniuRequestError(401, 'mock', 'message', {}), // token 过期
7+
invalidParams: new QiniuRequestError(400, 'mock', 'message'), // 无效的参数
8+
expiredToken: new QiniuRequestError(401, 'mock', 'message'), // token 过期
99

10-
gatewayUnavailable: new QiniuRequestError(502, 'mock', 'message', {}), // 网关不可用
11-
serviceUnavailable: new QiniuRequestError(503, 'mock', 'message', {}), // 服务不可用
12-
serviceTimeout: new QiniuRequestError(504, 'mock', 'message', {}), // 服务超时
13-
serviceError: new QiniuRequestError(599, 'mock', 'message', {}), // 服务错误
10+
gatewayUnavailable: new QiniuRequestError(502, 'mock', 'message'), // 网关不可用
11+
serviceUnavailable: new QiniuRequestError(503, 'mock', 'message'), // 服务不可用
12+
serviceTimeout: new QiniuRequestError(504, 'mock', 'message'), // 服务超时
13+
serviceError: new QiniuRequestError(599, 'mock', 'message'), // 服务错误
1414

15-
invalidUploadId: new QiniuRequestError(612, 'mock', 'message', {}) // 无效的 upload id
15+
invalidUploadId: new QiniuRequestError(612, 'mock', 'message') // 无效的 upload id
1616
}
1717

1818
export type ApiName =

src/errors/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ export class QiniuRequestError extends QiniuError {
4343
public isRequestError = true
4444

4545
/**
46-
* @description 发生错误时服务端返回的错误信息,如果返回不是一个合法的 json、则该字段为 undefined
47-
*/
48-
public data: any = undefined
46+
* @description 发生错误时服务端返回的错误信息,如果返回不是一个合法的 json、则该字段为 undefined
47+
*/
48+
public data?: { [key: string]: any }
4949

50-
constructor(public code: number, public reqId: string, message: string, data: any) {
50+
constructor(public code: number, public reqId: string, message: string, data?: { [key: string]: any }) {
5151
super(QiniuErrorName.RequestError, message)
5252
this.data = data
5353
}
@@ -58,6 +58,6 @@ export class QiniuRequestError extends QiniuError {
5858
*/
5959
export class QiniuNetworkError extends QiniuRequestError {
6060
constructor(reqId = '', message: string) {
61-
super(0, reqId, message, undefined)
61+
super(0, reqId, message)
6262
}
6363
}

src/upload/hosts.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,9 @@ export class HostPool {
118118

119119
// 无可用的,去取离解冻最近的 host
120120
const priorityQueue = cachedHostList
121-
.slice().sort((hostA, hostB) => (
122-
hostA.getUnfreezeTime() || 0) - (hostB.getUnfreezeTime() || 0))
121+
.slice().sort(
122+
(hostA, hostB) => (hostA.getUnfreezeTime() || 0) - (hostB.getUnfreezeTime() || 0)
123+
)
123124

124125
return priorityQueue[0]
125126
}

src/upload/resume.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export interface UploadedChunkStorage extends UploadChunkData {
88
size: number
99

1010
// 标记该 chunk 是否来自缓存
11-
isLocalCached?: true
11+
isLocalCache?: boolean
1212
}
1313

1414
export interface ChunkLoaded {
@@ -107,6 +107,7 @@ export default class Resume extends Base {
107107
this.updateChunkProgress(chunk.size, index)
108108
}
109109

110+
// FIXME: 至少比对一下 size 吧
110111
if (info && !shouldCheckMD5) {
111112
reuseSaved()
112113
return
@@ -213,7 +214,7 @@ export default class Resume extends Base {
213214
this.uploadedList = localInfo.data
214215
if (Array.isArray(this.uploadedList)) {
215216
this.uploadedList.forEach(chunk => {
216-
chunk.isLocalCached = true
217+
chunk.isLocalCache = true
217218
})
218219
}
219220

src/utils/pool.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ describe('test Pool for control concurrency', () => {
2121
{ chunk, index: 5 }
2222
]
2323

24-
for (const item of data) {
25-
// eslint-disable-next-line no-await-in-loop
26-
await pool.enqueue(item)
24+
return Promise.all(data.map(async value => {
25+
await pool.enqueue(value)
2726
expect(pool.processing.length).toBeLessThanOrEqual(2)
28-
}
27+
})).then(() => {
28+
expect(m.mock.calls.length).toBe(6)
29+
})
2930
})
3031
})

0 commit comments

Comments
 (0)