1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.ProgressCallbackTransform = void 0;
|
7 |
|
8 | function _stream() {
|
9 | const data = require("stream");
|
10 |
|
11 | _stream = function () {
|
12 | return data;
|
13 | };
|
14 |
|
15 | return data;
|
16 | }
|
17 |
|
18 | class ProgressCallbackTransform extends _stream().Transform {
|
19 | constructor(total, cancellationToken, onProgress) {
|
20 | super();
|
21 | this.total = total;
|
22 | this.cancellationToken = cancellationToken;
|
23 | this.onProgress = onProgress;
|
24 | this.start = Date.now();
|
25 | this.transferred = 0;
|
26 | this.delta = 0;
|
27 | this.nextUpdate = this.start + 1000;
|
28 | }
|
29 |
|
30 | _transform(chunk, encoding, callback) {
|
31 | if (this.cancellationToken.cancelled) {
|
32 | callback(new Error("Cancelled"), null);
|
33 | return;
|
34 | }
|
35 |
|
36 | this.transferred += chunk.length;
|
37 | this.delta += chunk.length;
|
38 | const now = Date.now();
|
39 |
|
40 | if (now >= this.nextUpdate && this.transferred !== this.total
|
41 |
|
42 | ) {
|
43 | this.nextUpdate = now + 1000;
|
44 | this.onProgress({
|
45 | total: this.total,
|
46 | delta: this.delta,
|
47 | transferred: this.transferred,
|
48 | percent: this.transferred / this.total * 100,
|
49 | bytesPerSecond: Math.round(this.transferred / ((now - this.start) / 1000))
|
50 | });
|
51 | this.delta = 0;
|
52 | }
|
53 |
|
54 | callback(null, chunk);
|
55 | }
|
56 |
|
57 | _flush(callback) {
|
58 | if (this.cancellationToken.cancelled) {
|
59 | callback(new Error("Cancelled"));
|
60 | return;
|
61 | }
|
62 |
|
63 | this.onProgress({
|
64 | total: this.total,
|
65 | delta: this.delta,
|
66 | transferred: this.total,
|
67 | percent: 100,
|
68 | bytesPerSecond: Math.round(this.transferred / ((Date.now() - this.start) / 1000))
|
69 | });
|
70 | this.delta = 0;
|
71 | callback(null);
|
72 | }
|
73 |
|
74 | } exports.ProgressCallbackTransform = ProgressCallbackTransform;
|
75 |
|
76 |
|
\ | No newline at end of file |