Skip to content

Commit 455ba5a

Browse files
committed
请求数和分片大小的上限改为可配置
1 parent efac90e commit 455ba5a

File tree

2 files changed

+54
-37
lines changed

2 files changed

+54
-37
lines changed

src/background.js

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,48 @@
11
import { configs } from './websites'
22
import downloadZip from './download'
33

4-
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
5-
const getHeaders = (xhr) => {
6-
const headers = {}
7-
if (Array.isArray(message.responseHeaders)) {
8-
message.responseHeaders.map(item => {
9-
headers[item] = xhr.getResponseHeader(item)
10-
})
11-
}
12-
return headers
13-
}
14-
const sendSuccess = (message, xhr) => {
15-
sendResponse([null, message, getHeaders(xhr), xhr])
4+
const getHeaders = (xhr, responseHeaders) => {
5+
const headers = {}
6+
if (Array.isArray(responseHeaders)) {
7+
responseHeaders.map(item => {
8+
headers[item] = xhr.getResponseHeader(item)
9+
})
1610
}
17-
const sendError = (error, xhr) => {
18-
sendResponse([error, null, getHeaders(xhr), xhr])
11+
return headers
12+
}
13+
14+
const sendCallback = (sendResponse, responseHeaders) => {
15+
return {
16+
sendSuccess (message, xhr) {
17+
sendResponse([null, message, getHeaders(xhr, responseHeaders), xhr])
18+
},
19+
sendError (error, xhr) {
20+
sendResponse([error, null, getHeaders(xhr, responseHeaders), xhr])
21+
}
1922
}
20-
if (/(get|post)/i.test(message.type)) {
23+
}
24+
25+
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
26+
const { type, responseHeaders } = message
27+
const { url, data, dataType, callback } = message
28+
const { fileName, files, options } = message
29+
const { sendSuccess, sendError } = sendCallback(sendResponse, responseHeaders)
30+
if (/(get|post)/i.test(type)) {
2131
ajax({
22-
url: message.url,
23-
method: message.type,
24-
data: message.data,
25-
dataType: message.dataType,
32+
url,
33+
method: type,
34+
data,
35+
dataType,
2636
success (data, xhr) {
27-
if (/text|blob/i.test(message.dataType)) {
37+
if (/text|blob/i.test(dataType)) {
2838
sendSuccess(data, xhr)
2939
} else {
30-
const obj = message.dataType === 'text' ? data : JSON.parse(data)
31-
const result = /^json$/i.test(message.dataType) ? {
32-
callback: message.callback,
40+
const obj = dataType === 'text' ? data : JSON.parse(data)
41+
const result = /^json$/i.test(dataType) ? {
42+
callback: noop(callback),
3343
data: obj
3444
} : data
35-
if (typeof message.callback === 'string' && message.callback.length) {
45+
if (typeof callback === 'string' && callback) {
3646
sendSuccess(result, xhr)
3747
} else {
3848
sendSuccess(obj, xhr)
@@ -43,8 +53,8 @@ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
4353
sendError(error, xhr)
4454
}
4555
})
46-
} else if (message.type === 'download') {
47-
downloadZip(message.fileName, message.files)
56+
} else if (type === 'download') {
57+
downloadZip(fileName, files, options)
4858
}
4959
return true
5060
})

src/download.js

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,17 @@ import md5 from 'md5'
22
import JSZip from 'jszip'
33
import FileSaver from 'jszip/vendor/FileSaver'
44

5-
// 每段数量上限
6-
export const partLimit = 1e3
7-
// 同时请求数上限
8-
export const requestLimit = 5
5+
const defaultOptions = {
6+
partLimit: 1e3,
7+
requestLimit: 5
8+
}
9+
10+
const options = Object.assign({}, defaultOptions)
11+
12+
export const mergeOptions = (newOptions) => {
13+
return Object.assign(options, defaultOptions, newOptions instanceof Object ? newOptions : {})
14+
}
15+
916

1017
export const noop = (func, defaultFunc) => {
1118
return typeof func === 'function' ? func : typeof defaultFunc === 'function' ? defaultFunc : () => {}
@@ -59,13 +66,13 @@ export const fetchBlobFile = (file) =>{
5966
})
6067
}
6168

62-
export const partTask = (items, handler, num = requestLimit) => {
69+
export const partTask = (items, handler, limit) => {
6370
let index = 0
6471
let list = items.slice(0)
6572
let queue = Promise.resolve()
6673
const result = []
6774
while (list.length) {
68-
const current = list.splice(0, num)
75+
const current = list.splice(0, limit)
6976
const currentIndex = index++
7077
queue = queue.then(() => {
7178
return new Promise((resolve) => {
@@ -79,7 +86,7 @@ export const partTask = (items, handler, num = requestLimit) => {
7986
return queue
8087
}
8188

82-
export const partRequest = (fileName, files) => {
89+
export const partRequest = (fileName, files, { requestLimit } = options) => {
8390
const zip = new JSZip()
8491
const handler = (files) => files.map(file => fetchBlobFile(file, zip).then(blob => {
8592
zip.file(file.name, blob)
@@ -99,18 +106,18 @@ export const partRequest = (fileName, files) => {
99106
})
100107
}
101108

102-
export const partDownload = (fileName, files) => {
109+
export const partDownload = (fileName, files, { partLimit } = options) => {
103110
const count = ~~(files.length / partLimit)
104111
return partTask(files, (files, index) => {
105112
const partMame = count >= 1 ? '-p' + (index + 1) + '-' + count : ''
106113
const name = fileName + partMame + '.zip'
107-
return [partRequest(name, files)]
114+
return [partRequest(name, files, options)]
108115
}, partLimit)
109116
}
110117

111-
export const downloadZip = (fileName, files) => {
118+
export const downloadZip = (fileName, files, options = {}) => {
112119
fileName = fileName || md5(files.map(item => item.downloadUrl).join('|'))
113-
return partDownload(fileName, files)
120+
return partDownload(fileName, files, mergeOptions(options))
114121
}
115122

116123
export default downloadZip

0 commit comments

Comments
 (0)