-
Notifications
You must be signed in to change notification settings - Fork 22
/
process.js
62 lines (49 loc) · 1.67 KB
/
process.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
'use strict';
const spawn = require('child_process').spawn;
const events = require('events');
const readline = require('readline');
const validator = require('validator');
class Process extends events.EventEmitter {
constructor(command, args) {
super();
this.command = command;
this.args = args;
}
trace(domainName) {
if (!this.isValidDomainName(domainName)) {
throw "Invalid domain name or IP address";
}
this.args.push(domainName);
const process = spawn(this.command, this.args);
process.on('close', (code) => {
this.emit('close', code);
});
this.emit('pid', process.pid);
let isDestinationCaptured = false;
if (process.pid) {
readline.createInterface({
input: process.stdout,
terminal: false
})
.on('line', (line) => {
if (!isDestinationCaptured) {
const destination = this.parseDestination(line);
if (destination !== null) {
this.emit('destination', destination);
isDestinationCaptured = true;
}
}
const hop = this.parseHop(line);
if (hop !== null) {
this.emit('hop', hop);
}
});
}
}
isValidDomainName(domainName) {
return validator.isFQDN(domainName + '') || validator.isIP(domainName + '');
}
parseDestination(data) {}
parseHop(hopData) {}
}
module.exports = Process;