-
Notifications
You must be signed in to change notification settings - Fork 1
/
progress-stream.ts
253 lines (210 loc) · 6.35 KB
/
progress-stream.ts
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import { Transform, TransformCallback, TransformOptions } from 'stream';
import { EventEmitter } from 'events';
export interface Progress {
percentage: number;
transferred: number;
total: number;
remaining: number;
eta: number;
runtime: number;
delta: number;
speed: number;
}
interface SpeedMonitorOptionsRequired {
total: number;
interval: number;
}
interface SpeedMonitorOptionsOptional {
transferred: number;
windowSize: number;
autoStart: boolean;
}
export type SpeedMonitorOptions = SpeedMonitorOptionsRequired & Partial<SpeedMonitorOptionsOptional>;
interface SpeedMonitorEvents {
'progress': (progress: Progress) => void;
}
export declare interface SpeedMonitor {
addListener<U extends keyof SpeedMonitorEvents>(
event: U,
listener: SpeedMonitorEvents[U],
): this;
emit<U extends keyof SpeedMonitorEvents>(
event: U,
...args: Parameters<SpeedMonitorEvents[U]>,
): boolean;
on<U extends keyof SpeedMonitorEvents>(
event: U,
listener: SpeedMonitorEvents[U],
): this;
once<U extends keyof SpeedMonitorEvents>(
event: U,
listener: SpeedMonitorEvents[U],
): this;
prependListener<U extends keyof SpeedMonitorEvents>(
event: U,
listener: SpeedMonitorEvents[U],
): this;
prependOnceListener<U extends keyof SpeedMonitorEvents>(
event: U,
listener: SpeedMonitorEvents[U],
): this;
removeListener<U extends keyof SpeedMonitorEvents>(
event: U,
listener: SpeedMonitorEvents[U],
): this;
}
const DEFAULT_SPEED_MONITOR_OPTIONS: SpeedMonitorOptionsOptional = {
transferred: 0,
windowSize: 4,
autoStart: true,
};
export class SpeedMonitor extends EventEmitter {
readonly total: number;
readonly interval: number;
readonly startTime: number;
readonly windowSize: number;
private _speed: number;
private _transferred: number;
private readonly speedBuffer: number[];
private speedBufferPointer: number;
private lastLoaded: number;
private lastTime: number;
private timer?: number;
constructor(options: SpeedMonitorOptions) {
const _options: SpeedMonitorOptionsRequired & SpeedMonitorOptionsOptional = {
...DEFAULT_SPEED_MONITOR_OPTIONS,
...options,
};
const {
transferred,
windowSize,
total,
interval,
autoStart,
...eventEmitterOptions
} = _options;
super(eventEmitterOptions);
this.total = total;
this.startTime = Date.now();
this._speed = 0;
this._transferred = transferred;
this.windowSize = windowSize;
this.speedBuffer = [];
this.speedBufferPointer = 0;
this.lastLoaded = this.transferred;
this.lastTime = this.startTime;
this.interval = interval;
this.handleProgress = this.handleProgress.bind(this);
if (autoStart) {
this.start();
}
this.destroy = this.destroy.bind(this);
}
get paused(): boolean {
return !this.timer;
}
get running(): boolean {
return !this.paused;
}
/**
* Bytes/ms
*/
get speed(): number {
return this._speed;
}
get transferred(): number {
return this._transferred;
}
updateProgress(delta: number) {
this._transferred += delta;
if (delta < 0) {
this.lastLoaded += delta;
}
}
start() {
if (this.running) {
return;
}
this.timer = setInterval(this.handleProgress, this.interval) as unknown as number;
}
pause() {
if (this.paused) {
return;
}
if (this.lastLoaded !== this._transferred) {
this.handleProgress();
}
this.clearTimer();
}
private clearTimer() {
if (this.timer) {
clearInterval(this.timer);
this.timer = undefined;
}
}
private calcSpeed(currentSpeed: number): number {
if (this.speedBuffer.length < this.windowSize) {
this.speedBuffer.push(currentSpeed);
} else {
this.speedBuffer[this.speedBufferPointer] = currentSpeed;
}
this.speedBufferPointer = (this.speedBufferPointer + 1) % this.windowSize;
return this.speedBuffer.reduce(
(avg, curr, idx) => avg + ((curr - avg) / (idx + 1))
);
}
private handleProgress() {
const now = Date.now();
const delta = this.transferred - this.lastLoaded;
const duration = now - this.lastTime;
this.lastLoaded = this.transferred;
this.lastTime = now;
this._speed = this.calcSpeed(delta / duration);
const remaining = this.total - this.transferred;
const eta = remaining / this._speed;
const progress: Progress = {
delta: delta,
eta: eta,
percentage: Math.min(100, this.transferred * 100 / this.total),
remaining: remaining,
runtime: now - this.startTime,
speed: this._speed,
total: this.total,
transferred: this.transferred,
};
this.emit('progress', progress);
}
destroy() {
this.pause();
this.removeAllListeners();
}
}
export interface ProgressOptions extends TransformOptions {
monitor: SpeedMonitor;
}
export class ProgressStream extends Transform {
private readonly monitor: SpeedMonitor;
private readonly customTransform?: TransformOptions['transform'];
constructor(options: ProgressOptions) {
const {
monitor,
transform,
...transformOptions
} = options;
super(transformOptions);
this.monitor = monitor;
this.customTransform = transform;
this.on('pipe', () => this.monitor.start());
this.on('unpipe', () => this.monitor.pause());
this.on('close', () => this.monitor.destroy());
this.on('error', () => this.monitor.destroy());
}
_transform(chunk: any, encoding: BufferEncoding, callback: TransformCallback) {
this.monitor.updateProgress(chunk.length);
if (this.customTransform) {
this.customTransform(chunk, encoding, callback);
} else {
callback(null, chunk);
}
}
}