-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMuxer.js
127 lines (113 loc) · 3.63 KB
/
Muxer.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
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
import { Transform, Readable, Writable, PassThrough, Duplex } from "stream";
import { AsyncQueue } from "./libs/AsyncQueue.js";
import { EventEmitter } from "events";
//import { ScanData } from "./libs/typedef.js";
class MuxerFeedback extends Duplex {
/** @param {Readable} channel */
constructor(channel, feedback_counter) {
super({ objectMode: true });
channel.on("data", async (object)=> {
try {
await this.queue.push(object);
} catch(err) {
console.log("MuxerFeedbackInner3: ", err);
}
});
this.feedback_counter = feedback_counter;
this.once("finish", ()=> {
console.log("MuxerFeedback: finish, channel:", channel.readableEnded, this.feedback_counter.cnt);
feedback_counter.on("change", ()=> {
if(feedback_counter.cnt <= 0) {
console.log("MuxerInput: cnt finish");
clearInterval(intv);
channel.end();
}
});
feedback_counter.emit("change");
});
channel.once("end", ()=> {
console.log("MuxerFeedback: channel end");
this.queue.destroy();
this.end();
this.push(null);
});
}
is_ended = false;
/** @type {AsyncQueue<ScanData>} */
queue = new AsyncQueue(2);
/** @param {ScanData} scan_data */
async _write(scan_data, encoding, callback) {
try {
this.feedback_counter.inc(1, );
await this.queue.push(scan_data);
} catch(err) {
console.log("MuxerFeedbackInner1: ", err);
}
callback();
}
async _read() {
try {
const result = await this.queue.pop();
if(result == undefined)
return this.push(null);
this.push(result);
} catch(err) {
console.log("MuxerFeedbackInner2: ", err);
}
}
};
class MuxerInput extends Transform {
/** @param {Writable} channel */
constructor(channel, feedback_counter) {
super({ objectMode: true });
this.channel = channel;
this.feedback_counter = feedback_counter;
}
/** @param {ScanData} object */
_transform(object, encoding, callback) {
if(!object) {
this.feedback_counter.dec();
return callback();
}
if(object.done) {
this.feedback_counter.dec();
this.push(object); // the Prober and Tagger is done with this host, send it to the output
} else
this.channel.write(object); // Tagger added more tags, send it back to the Prober
callback();
}
};
class FeedbackCounter extends EventEmitter {
constructor() {
super();
}
cnt = 0;
logs = new Map();
inc(num=1, logkey, logval) {
this.cnt += num;
if(logkey)
this.logs.set(logkey, logval);
//console.log("FeedbackCounter: ", this.cnt);
this.emit("change");
}
dec(num=1, logkey) {
this.cnt -= num;
if(logkey)
this.logs.delete(logkey);
//console.log("FeedbackCounter: ", this.cnt);
this.emit("change");
}
};
function makeMuxer() {
const pass_through = new PassThrough({objectMode: true});
const feedback_counter = new FeedbackCounter();
pass_through.on("error", err=> {
console.log("PASSTHROUGH", err);
});
const input = new MuxerInput(pass_through, feedback_counter);
const feedback = new MuxerFeedback(pass_through, feedback_counter);
return [feedback, input, feedback_counter];
}
export {
makeMuxer,
};