-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
51 lines (45 loc) · 1.21 KB
/
app.js
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
const {Readable, Writable, Transform} = require('stream');
class MyTransform extends Transform {
constructor(name){
super();
this.name = name;
this.data = Buffer.from([]);
return this;
}
_transform(chunk, encoding, callback){
console.log(this.name);
console.log(chunk.toString());
this.data = Buffer.concat([this.data, chunk]);
// callback();
callback(null, chunk);
}
// _flush(cb){
// this.push("??")
// console.log(this.data.toString());
// cb(null, this.data);
// }
}
let data = Buffer.from([]);
let writable = new Writable();
writable._write = (chunk, enc, next) => {
data = Buffer.concat([data, chunk])
next();
}
let readable_data = "Some data!";
let readable = new Readable({highWaterMark:1});
readable._read = () => {};
readable.push(readable_data);
readable.push(readable_data);
readable.push(null);
readable
.pipe(new MyTransform("a"))
.pipe(new MyTransform("b"))
.pipe(writable)
.on('finish', () => {
console.log('fin');
console.log(data.toString());
})
.on('error', (error) => {
console.log('err');
console.error(error);
});