@abtnode/util 1.16.52-beta-20251003-083412-fdfc4e36 → 1.16.52-beta-20251005-235515-42ad5caf
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/download-file.js +30 -16
- package/package.json +6 -6
package/lib/download-file.js
CHANGED
|
@@ -50,17 +50,23 @@ async function streamToString(stream) {
|
|
|
50
50
|
* @param {*} dest directory
|
|
51
51
|
* @param {{
|
|
52
52
|
* timeout?: number; // unit: ms
|
|
53
|
-
*
|
|
53
|
+
* checkCanceled?: () => Promise<boolean>;
|
|
54
54
|
* onProgress?: any
|
|
55
55
|
* minProgressInterval?: number
|
|
56
|
-
* }} [{ timeout = 600 * 1000,
|
|
56
|
+
* }} [{ timeout = 600 * 1000, checkCanceled, data }={}]
|
|
57
57
|
* @param {{}} context
|
|
58
58
|
* @return {*}
|
|
59
59
|
*/
|
|
60
60
|
const downloadFile = async (
|
|
61
61
|
url,
|
|
62
62
|
dest,
|
|
63
|
-
{
|
|
63
|
+
{
|
|
64
|
+
timeout = 600 * 1000,
|
|
65
|
+
minProgressInterval = 400,
|
|
66
|
+
// eslint-disable-next-line no-promise-executor-return
|
|
67
|
+
checkCanceled = () => new Promise((resolve) => resolve(false)),
|
|
68
|
+
onProgress,
|
|
69
|
+
} = {},
|
|
64
70
|
context = {}
|
|
65
71
|
) => {
|
|
66
72
|
const CONNECTION_TIMEOUT = 20 * 1000;
|
|
@@ -74,17 +80,14 @@ const downloadFile = async (
|
|
|
74
80
|
|
|
75
81
|
let fileStream;
|
|
76
82
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
}
|
|
86
|
-
};
|
|
87
|
-
}
|
|
83
|
+
const cancelDownload = () => {
|
|
84
|
+
clearTimeout(timer);
|
|
85
|
+
source.cancel('Manual cancel');
|
|
86
|
+
if (fileStream) {
|
|
87
|
+
fileStream.destroy(new Error('Manual cancel'));
|
|
88
|
+
fileStream = null;
|
|
89
|
+
}
|
|
90
|
+
};
|
|
88
91
|
|
|
89
92
|
const response = await axios({
|
|
90
93
|
url,
|
|
@@ -97,12 +100,23 @@ const downloadFile = async (
|
|
|
97
100
|
timeout,
|
|
98
101
|
});
|
|
99
102
|
|
|
103
|
+
let t = Date.now();
|
|
104
|
+
|
|
100
105
|
if (typeof onProgress === 'function') {
|
|
101
106
|
const throttled = throttle(onProgress, minProgressInterval, { leading: false, trailing: true });
|
|
102
107
|
const total = (response.headers || {})['content-length'] || 0;
|
|
103
108
|
let current = 0;
|
|
104
109
|
response.data.on('data', (chunk) => {
|
|
105
110
|
current += chunk.length;
|
|
111
|
+
// 每 2 秒检查一次, db-cache 里是否标记了取消, 如果有, 则取消下载
|
|
112
|
+
if (Date.now() - t > 2000) {
|
|
113
|
+
t = Date.now();
|
|
114
|
+
checkCanceled().then((isCancelled) => {
|
|
115
|
+
if (isCancelled) {
|
|
116
|
+
cancelDownload();
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
}
|
|
106
120
|
try {
|
|
107
121
|
throttled({ total, current });
|
|
108
122
|
} catch {
|
|
@@ -115,7 +129,7 @@ const downloadFile = async (
|
|
|
115
129
|
fileStream = response.data.pipe(fs.createWriteStream(dest));
|
|
116
130
|
await streamToPromise(fileStream);
|
|
117
131
|
|
|
118
|
-
if (
|
|
132
|
+
if (await checkCanceled()) {
|
|
119
133
|
if (fs.existsSync(dest)) {
|
|
120
134
|
fs.truncateSync(dest, 0);
|
|
121
135
|
fs.rmSync(dest, { force: true });
|
|
@@ -136,7 +150,7 @@ const downloadFile = async (
|
|
|
136
150
|
}
|
|
137
151
|
|
|
138
152
|
if (err.message === 'Manual cancel') {
|
|
139
|
-
if (
|
|
153
|
+
if (await checkCanceled()) {
|
|
140
154
|
return CANCEL;
|
|
141
155
|
}
|
|
142
156
|
throw new Error('Manual stop abnormal');
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"publishConfig": {
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
|
-
"version": "1.16.52-beta-
|
|
6
|
+
"version": "1.16.52-beta-20251005-235515-42ad5caf",
|
|
7
7
|
"description": "ArcBlock's JavaScript utility",
|
|
8
8
|
"main": "lib/index.js",
|
|
9
9
|
"files": [
|
|
@@ -18,14 +18,14 @@
|
|
|
18
18
|
"author": "polunzh <polunzh@gmail.com> (http://github.com/polunzh)",
|
|
19
19
|
"license": "Apache-2.0",
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@abtnode/constant": "1.16.52-beta-
|
|
22
|
-
"@abtnode/db-cache": "1.16.52-beta-
|
|
21
|
+
"@abtnode/constant": "1.16.52-beta-20251005-235515-42ad5caf",
|
|
22
|
+
"@abtnode/db-cache": "1.16.52-beta-20251005-235515-42ad5caf",
|
|
23
23
|
"@arcblock/did": "1.25.6",
|
|
24
24
|
"@arcblock/event-hub": "1.25.6",
|
|
25
25
|
"@arcblock/pm2": "^6.0.12",
|
|
26
|
-
"@blocklet/constant": "1.16.52-beta-
|
|
26
|
+
"@blocklet/constant": "1.16.52-beta-20251005-235515-42ad5caf",
|
|
27
27
|
"@blocklet/error": "^0.2.5",
|
|
28
|
-
"@blocklet/meta": "1.16.52-beta-
|
|
28
|
+
"@blocklet/meta": "1.16.52-beta-20251005-235515-42ad5caf",
|
|
29
29
|
"@blocklet/xss": "^0.2.9",
|
|
30
30
|
"@ocap/client": "1.25.6",
|
|
31
31
|
"@ocap/mcrypto": "1.25.6",
|
|
@@ -91,5 +91,5 @@
|
|
|
91
91
|
"fs-extra": "^11.2.0",
|
|
92
92
|
"jest": "^29.7.0"
|
|
93
93
|
},
|
|
94
|
-
"gitHead": "
|
|
94
|
+
"gitHead": "7b295929a123edac2cb292c43f2edda0d3e3e6b8"
|
|
95
95
|
}
|