-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathduplex.js
More file actions
34 lines (26 loc) · 707 Bytes
/
duplex.js
File metadata and controls
34 lines (26 loc) · 707 Bytes
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
const {Duplex, PassThrough} = require('stream');
const {createReadStream, createWriteStream} = require('fs');
const readStream = createReadStream('./anime_dancing.mp4');
const writeStream = createWriteStream('./copy.mp4');
class Throttle extends Duplex {
constructor(ms) {
super();
this.delay = ms;
}
_read(){}
_write(chunk, encoding, callback) {
this.push(chunk);
setTimeout(callback, this.delay);
}
_final(){
this.push(null)
}
}
const report = new PassThrough();
const throttle = new Throttle(100);
let total = 0;
report.on('data', (chunk) => {
total += chunk.length;
console.log('bytes: ', total);
})
readStream.pipe(throttle).pipe(report).pipe(writeStream);