cvitool 1.0.6 → 1.0.7
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/build/src/hgo.js +2 -2
- package/build/src/streamhelper.d.ts +4 -2
- package/build/src/streamhelper.js +29 -14
- package/package.json +1 -1
- package/src/hgo.ts +2 -2
- package/src/streamhelper.ts +32 -16
package/build/src/hgo.js
CHANGED
|
@@ -192,7 +192,7 @@ function resHandld(res, resolve, reject, resType, method) {
|
|
|
192
192
|
break;
|
|
193
193
|
}
|
|
194
194
|
}
|
|
195
|
-
if (res.statusCode
|
|
195
|
+
if (res.statusCode >= 400) {
|
|
196
196
|
errHandle(reject, res, reqUrl, resHeaders);
|
|
197
197
|
return;
|
|
198
198
|
}
|
|
@@ -230,7 +230,7 @@ function resHandld(res, resolve, reject, resType, method) {
|
|
|
230
230
|
resBody = JSON.parse(responseStr);
|
|
231
231
|
}
|
|
232
232
|
catch (e) {
|
|
233
|
-
resBody =
|
|
233
|
+
resBody = responseStr;
|
|
234
234
|
}
|
|
235
235
|
}
|
|
236
236
|
}
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
2
3
|
import { ReadStream, WriteStream } from 'fs';
|
|
4
|
+
import * as http from 'http';
|
|
3
5
|
interface pipeOptions {
|
|
4
6
|
timeout?: number;
|
|
5
7
|
readBytesPreSec?: number;
|
|
6
8
|
}
|
|
7
|
-
declare function pipe(source: ReadStream, target: WriteStream, options?: pipeOptions): Promise<void>;
|
|
8
|
-
declare function limitStreamFlowingRate(stream: ReadStream, readBytesPreSec: number): void;
|
|
9
|
+
declare function pipe(source: ReadStream | http.IncomingMessage, target: WriteStream, options?: pipeOptions): Promise<void>;
|
|
10
|
+
declare function limitStreamFlowingRate(stream: ReadStream | http.IncomingMessage, readBytesPreSec: number): void;
|
|
9
11
|
export { pipeOptions, pipe, limitStreamFlowingRate };
|
|
@@ -26,9 +26,11 @@ function pipe(source, target, options) {
|
|
|
26
26
|
});
|
|
27
27
|
if (timeout) {
|
|
28
28
|
setTimeout(() => {
|
|
29
|
-
if (!target.
|
|
29
|
+
if (!target.destroyed) {
|
|
30
30
|
target.destroy();
|
|
31
|
-
source.
|
|
31
|
+
if (!source.destroyed) {
|
|
32
|
+
source.destroy();
|
|
33
|
+
}
|
|
32
34
|
const downloadErr = new Error(`文件下载超时|${(timeout / 1000).toFixed(3)}s`);
|
|
33
35
|
reject(downloadErr);
|
|
34
36
|
}
|
|
@@ -40,22 +42,35 @@ function pipe(source, target, options) {
|
|
|
40
42
|
exports.pipe = pipe;
|
|
41
43
|
function limitStreamFlowingRate(stream, readBytesPreSec) {
|
|
42
44
|
let start = 0;
|
|
45
|
+
let checkInterval;
|
|
43
46
|
let calReadBytesTotal = 0;
|
|
47
|
+
let isClosed = false;
|
|
44
48
|
stream.on('data', (chunk) => {
|
|
45
|
-
if (!start) {
|
|
46
|
-
start = Date.now();
|
|
47
|
-
}
|
|
48
49
|
calReadBytesTotal += chunk.length;
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
50
|
+
});
|
|
51
|
+
stream.once('data', () => {
|
|
52
|
+
start = Date.now();
|
|
53
|
+
checkInterval = setInterval(() => {
|
|
54
|
+
const requireReadBytesFromNow = Math.ceil(readBytesPreSec * ((Date.now() - start) / 1000));
|
|
55
|
+
if (calReadBytesTotal > requireReadBytesFromNow) {
|
|
56
|
+
const waitMs = Math.ceil((calReadBytesTotal - requireReadBytesFromNow) / readBytesPreSec * 1000);
|
|
57
|
+
if (waitMs > 50 && !isClosed && !stream.isPaused()) {
|
|
58
|
+
stream.pause();
|
|
59
|
+
setTimeout(() => {
|
|
60
|
+
if (!isClosed && stream.isPaused()) {
|
|
61
|
+
stream.resume();
|
|
62
|
+
}
|
|
63
|
+
}, waitMs);
|
|
64
|
+
}
|
|
57
65
|
}
|
|
58
|
-
}
|
|
66
|
+
}, 500);
|
|
67
|
+
});
|
|
68
|
+
stream.on('close', () => {
|
|
69
|
+
isClosed = true;
|
|
70
|
+
clearInterval(checkInterval);
|
|
71
|
+
});
|
|
72
|
+
stream.on('error', () => {
|
|
73
|
+
clearInterval(checkInterval);
|
|
59
74
|
});
|
|
60
75
|
}
|
|
61
76
|
exports.limitStreamFlowingRate = limitStreamFlowingRate;
|
package/package.json
CHANGED
package/src/hgo.ts
CHANGED
|
@@ -218,7 +218,7 @@ function resHandld(res: http.IncomingMessage, resolve: any, reject: any, resType
|
|
|
218
218
|
break;
|
|
219
219
|
}
|
|
220
220
|
}
|
|
221
|
-
if (res.statusCode
|
|
221
|
+
if (res.statusCode >= 400) {
|
|
222
222
|
errHandle(reject, res, reqUrl, resHeaders);
|
|
223
223
|
return;
|
|
224
224
|
}
|
|
@@ -253,7 +253,7 @@ function resHandld(res: http.IncomingMessage, resolve: any, reject: any, resType
|
|
|
253
253
|
try {
|
|
254
254
|
resBody = JSON.parse(responseStr);
|
|
255
255
|
} catch (e) {
|
|
256
|
-
resBody =
|
|
256
|
+
resBody = responseStr;
|
|
257
257
|
}
|
|
258
258
|
}
|
|
259
259
|
}
|
package/src/streamhelper.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { ReadStream, WriteStream } from 'fs';
|
|
2
|
+
import * as http from 'http';
|
|
2
3
|
|
|
3
4
|
interface pipeOptions {
|
|
4
5
|
timeout?: number,
|
|
5
6
|
readBytesPreSec?: number
|
|
6
7
|
}
|
|
7
8
|
|
|
8
|
-
async function pipe(source: ReadStream, target: WriteStream, options?: pipeOptions): Promise<void> {
|
|
9
|
+
async function pipe(source: ReadStream | http.IncomingMessage, target: WriteStream, options?: pipeOptions): Promise<void> {
|
|
9
10
|
const { timeout, readBytesPreSec } = options || {};
|
|
10
11
|
return new Promise((resolve, reject) => {
|
|
11
12
|
if (readBytesPreSec) {
|
|
@@ -20,9 +21,11 @@ async function pipe(source: ReadStream, target: WriteStream, options?: pipeOptio
|
|
|
20
21
|
});
|
|
21
22
|
if (timeout) {
|
|
22
23
|
setTimeout(() => {
|
|
23
|
-
if (!target.
|
|
24
|
+
if (!target.destroyed) {
|
|
24
25
|
target.destroy();
|
|
25
|
-
source.
|
|
26
|
+
if (!source.destroyed) {
|
|
27
|
+
source.destroy();
|
|
28
|
+
}
|
|
26
29
|
const downloadErr = new Error(`文件下载超时|${(timeout / 1000).toFixed(3)}s`);
|
|
27
30
|
reject(downloadErr);
|
|
28
31
|
}
|
|
@@ -31,24 +34,37 @@ async function pipe(source: ReadStream, target: WriteStream, options?: pipeOptio
|
|
|
31
34
|
});
|
|
32
35
|
}
|
|
33
36
|
|
|
34
|
-
function limitStreamFlowingRate(stream: ReadStream, readBytesPreSec: number) {
|
|
37
|
+
function limitStreamFlowingRate(stream: ReadStream | http.IncomingMessage, readBytesPreSec: number) {
|
|
35
38
|
let start = 0;
|
|
39
|
+
let checkInterval: any;
|
|
36
40
|
let calReadBytesTotal = 0;
|
|
41
|
+
let isClosed = false;
|
|
37
42
|
stream.on('data', (chunk) => {
|
|
38
|
-
if (!start) {
|
|
39
|
-
start = Date.now();
|
|
40
|
-
}
|
|
41
43
|
calReadBytesTotal += chunk.length;
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
44
|
+
});
|
|
45
|
+
stream.once('data', () => {
|
|
46
|
+
start = Date.now();
|
|
47
|
+
checkInterval = setInterval(() => {
|
|
48
|
+
const requireReadBytesFromNow = Math.ceil(readBytesPreSec * ((Date.now() - start) / 1000));
|
|
49
|
+
if (calReadBytesTotal > requireReadBytesFromNow) {
|
|
50
|
+
const waitMs = Math.ceil((calReadBytesTotal - requireReadBytesFromNow) / readBytesPreSec * 1000);
|
|
51
|
+
if (waitMs > 50 && !isClosed && !stream.isPaused()) {
|
|
52
|
+
stream.pause();
|
|
53
|
+
setTimeout(() => {
|
|
54
|
+
if (!isClosed && stream.isPaused()) {
|
|
55
|
+
stream.resume();
|
|
56
|
+
}
|
|
57
|
+
}, waitMs);
|
|
58
|
+
}
|
|
50
59
|
}
|
|
51
|
-
}
|
|
60
|
+
}, 500);
|
|
61
|
+
});
|
|
62
|
+
stream.on('close', () => {
|
|
63
|
+
isClosed = true;
|
|
64
|
+
clearInterval(checkInterval);
|
|
65
|
+
});
|
|
66
|
+
stream.on('error', () => {
|
|
67
|
+
clearInterval(checkInterval);
|
|
52
68
|
});
|
|
53
69
|
}
|
|
54
70
|
|