-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.js
37 lines (30 loc) · 1.2 KB
/
worker.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
const { workerData, parentPort } = require('worker_threads');
const axios = require('axios');
const fs = require('fs');
const path = require('path');
const { Throttle } = require('stream-throttle');
const { url, filePath, threadId, speedLimit } = workerData;
const downloadFile = async (url, filePath, threadId, speedLimit) => {
const response = await axios({
method: 'get',
url: url,
responseType: 'stream'
});
const contentLength = response.headers.get('content-length');
const writer = fs.createWriteStream(filePath);
const throttle = new Throttle({ rate: speedLimit });
let downloadedLength = 0;
let fileName = filePath.split(path.sep).pop();
response.data.pipe(throttle).on('data', (chunk) => {
downloadedLength += chunk.length;
const progress = (downloadedLength / contentLength) * 100;
parentPort.postMessage({ type: 'progress', progress, downloadedLength, contentLength, threadId, fileName});
}).pipe(writer);
writer.on('finish', () => {
parentPort.postMessage({ type: 'complete', fileName });
});
writer.on('error', () => {
parentPort.postMessage({ type: 'error', message: 'File write error' });
});
};
downloadFile(url, filePath, threadId, speedLimit);