This repository has been archived by the owner on Jun 11, 2023. It is now read-only.
forked from backblaze-b2-samples/multithreaded-downloader-js
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathMultiThread.js
129 lines (112 loc) · 3.85 KB
/
MultiThread.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
const fs = window.fs
class MultiThread {
constructor (options) {
Object.assign(this, options)
this.onStart = this.onStart ? this.onStart.bind(this) : () => {}
this.onFinish = this.onFinish ? this.onFinish.bind(this) : () => {}
this.onProgress = this.onProgress ? this.onProgress.bind(this) : () => {}
this.onError = this.onError ? this.onError.bind(this) : () => {}
this.loaded = 0
this.url = new URL(this.url)
this.headers = new Headers(this.headers)
this.controller = new AbortController()
this.queue = new PromiseQueue({
concurrency: this.threads,
signal: this.controller.signal,
onFinish: () => this.concatChunks().then(this.onFinish)
})
window.fetch(this.url, {
method: 'HEAD',
mode: 'cors',
headers: this.headers,
signal: this.controller.signal
})
.then(response => {
this.contentLength = parseInt(response.headers.get('Content-Length') || response.headers.get('content-length'))
this.chunkTotal = Math.ceil(this.contentLength / this.chunkSize)
this.onStart({contentLength: this.contentLength, chunks: this.chunkTotal})
fs.init({bytes: this.contentLength, type: window.TEMPORARY})
.then(() => fs.clear())
.then(() => {
for (let id = 0; id < this.chunkTotal; id++) {
this.buildChunk(id).then(chunk => this.addToQueue(chunk))
}
})
})
return this
}
buildChunk (id) {
const chunk = new Chunk({
id: id,
url: this.url,
headers: this.headers,
controller: this.controller,
startByte: id * this.chunkSize,
endByte: id * this.chunkSize + this.chunkSize - 1,
onStart: this.onChunkStart,
onFinish: this.onChunkFinish,
onError: this.onChunkError,
onProgress: ({id, contentLength, loaded, byteLength}) => {
this.loaded += byteLength
this.onProgress({contentLength: this.contentLength, loaded: this.loaded})
this.onChunkProgress({contentLength: contentLength, loaded: loaded, id: id})
}
})
return new Promise((resolve, reject) => resolve(chunk))
}
addToQueue (chunk) {
this.queue.add(() => new Promise((resolve, reject) => {
chunk.start()
.then(response => response.blob())
.then(blob => fs.writeFile(`${chunk.startByte}-${chunk.endByte}`, blob))
.then(resolve)
.catch(error => reject(error))
}), {attempts: this.retries})
}
concatChunks () {
return fs.readdir('/')
.then(chunks => chunks.sort((a, b) => {
const [aStart] = a.name.split('-').map(parseInt)
const [bStart] = b.name.split('-').map(parseInt)
if (aStart < bStart) return -1
if (aStart > bStart) return 1
return 0
}))
.then(chunks => {
const chunkQueue = new PromiseQueue({
signal: this.controller.signal,
onFinish: () => this.download(this.outputFile)
})
for (let id = 0; id < chunks.length; id++) {
const chunkPath = chunks[id].fullPath
chunkQueue.add(() => new Promise((resolve, reject) => {
fs.readFile(chunkPath, {type: 'ArrayBuffer'})
.then(buffer => fs.appendFile(this.fileName, buffer))
.then(outputFile => {
this.outputFile = outputFile
fs.unlink(chunkPath)
.then(resolve)
})
.catch(error => reject(error))
}), {attempts: this.retries})
}
})
}
download (file) {
const link = document.createElement('a')
link.download = this.fileName
return fs.getUrl(file)
.then(url => {
link.href = url
link.click()
})
}
cancel () {
this.controller.abort()
// if (this.writer) {
// this.writer.close()
// }
return Promise.resolve()
}
}
// module.exports = MultiThread