cvitool 1.0.761 → 1.0.762

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.
@@ -53,29 +53,26 @@ function pipe(source, target, options) {
53
53
  * @param readBytesPreSec 每秒读取的字节数
54
54
  */
55
55
  function limitStreamFlowingRate(stream, readBytesPreSec) {
56
- let start = 0;
57
- let checkInterval;
58
- let calReadBytesTotal = 0;
59
56
  let isClosed = false;
57
+ const interval = 100;
58
+ const bytesPerInterval = Math.floor(readBytesPreSec / 10);
59
+ let remainBytesThisInterval = bytesPerInterval;
60
+ const checkInterval = setInterval(() => {
61
+ if (remainBytesThisInterval < 0) {
62
+ remainBytesThisInterval += bytesPerInterval;
63
+ }
64
+ if (remainBytesThisInterval >= 0) {
65
+ remainBytesThisInterval = bytesPerInterval;
66
+ }
67
+ if (!isClosed && stream.isPaused()) {
68
+ stream.resume();
69
+ }
70
+ }, interval);
60
71
  stream.on('data', (chunk) => {
61
- calReadBytesTotal += chunk.length;
62
- });
63
- stream.once('data', () => {
64
- start = Date.now();
65
- checkInterval = setInterval(() => {
66
- const requireReadBytesFromNow = Math.ceil(readBytesPreSec * ((Date.now() - start) / 1000));
67
- if (calReadBytesTotal > requireReadBytesFromNow) {
68
- const waitMs = Math.ceil((calReadBytesTotal - requireReadBytesFromNow) / readBytesPreSec * 1000);
69
- if (waitMs > 50 && !isClosed && !stream.isPaused()) {
70
- stream.pause();
71
- setTimeout(() => {
72
- if (!isClosed && stream.isPaused()) {
73
- stream.resume();
74
- }
75
- }, waitMs);
76
- }
77
- }
78
- }, 500);
72
+ remainBytesThisInterval -= chunk.length;
73
+ if (remainBytesThisInterval <= 0) {
74
+ stream.pause();
75
+ }
79
76
  });
80
77
  stream.on('close', () => {
81
78
  isClosed = true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cvitool",
3
- "version": "1.0.761",
3
+ "version": "1.0.762",
4
4
  "description": "cvitool",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -46,29 +46,28 @@ async function pipe(source: Readable, target: Writable, options?: pipeOptions):
46
46
  * @param readBytesPreSec 每秒读取的字节数
47
47
  */
48
48
  function limitStreamFlowingRate(stream: Readable, readBytesPreSec: number) {
49
- let start = 0;
50
- let checkInterval: any;
51
- let calReadBytesTotal = 0;
52
49
  let isClosed = false;
50
+ const interval = 100;
51
+ const bytesPerInterval = Math.floor(readBytesPreSec / 10);
52
+ let remainBytesThisInterval = bytesPerInterval;
53
+
54
+ const checkInterval = setInterval(() => {
55
+ if (remainBytesThisInterval < 0) {
56
+ remainBytesThisInterval += bytesPerInterval;
57
+ }
58
+ if (remainBytesThisInterval >= 0) {
59
+ remainBytesThisInterval = bytesPerInterval;
60
+ }
61
+ if (!isClosed && stream.isPaused()) {
62
+ stream.resume();
63
+ }
64
+ }, interval);
65
+
53
66
  stream.on('data', (chunk) => {
54
- calReadBytesTotal += chunk.length;
55
- });
56
- stream.once('data', () => {
57
- start = Date.now();
58
- checkInterval = setInterval(() => {
59
- const requireReadBytesFromNow = Math.ceil(readBytesPreSec * ((Date.now() - start) / 1000));
60
- if (calReadBytesTotal > requireReadBytesFromNow) {
61
- const waitMs = Math.ceil((calReadBytesTotal - requireReadBytesFromNow) / readBytesPreSec * 1000);
62
- if (waitMs > 50 && !isClosed && !stream.isPaused()) {
63
- stream.pause();
64
- setTimeout(() => {
65
- if (!isClosed && stream.isPaused()) {
66
- stream.resume();
67
- }
68
- }, waitMs);
69
- }
70
- }
71
- }, 500);
67
+ remainBytesThisInterval -= chunk.length;
68
+ if (remainBytesThisInterval <= 0) {
69
+ stream.pause();
70
+ }
72
71
  });
73
72
  stream.on('close', () => {
74
73
  isClosed = true;