-
Notifications
You must be signed in to change notification settings - Fork 188
/
monitor.js
84 lines (66 loc) · 3.53 KB
/
monitor.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
"use strict";
require("core-js/modules/es7.object.get-own-property-descriptors");
require("core-js/modules/es6.symbol");
require("core-js/modules/es6.object.to-string");
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
const portfinder = require('portfinder');
const charm = require('charm')();
const Configurable = require('./configurable');
const Component = require('./component');
const axon = require('@dashersw/axon');
const defaultOptions = {
monitor: true,
log: false
};
module.exports = class Monitor extends Configurable(Component) {
constructor(advertisement, discoveryOptions = {}, stream) {
advertisement.type = 'monitor';
discoveryOptions = _objectSpread({}, defaultOptions, {}, discoveryOptions);
super(advertisement, discoveryOptions);
this.stream = stream;
this.sock = new axon.SubEmitterSocket();
this.sock.sock.on('bind', () => this.startDiscovery());
this.sock.on('status', status => this.emit('status', status));
const onPort = (err, port) => {
advertisement.port = +port;
this.sock.bind(port);
this.sock.sock.server.on('error', err => {
if (err.code != 'EADDRINUSE') throw err;
portfinder.getPort({
host: this.discoveryOptions.address,
port: this.advertisement.port
}, onPort);
});
};
portfinder.getPort({
host: this.discoveryOptions.address,
port: this.advertisement.port
}, onPort);
}
startDiscovery() {
super.startDiscovery();
if (this.discoveryOptions.disableScreen) return;
const interval = this.discoveryOptions.interval || 5000;
charm.pipe(this.stream || process.stdout);
charm.reset().erase('screen').position(0, 0).write(' ');
const draw = () => {
charm.erase('screen');
let index = 3;
charm.position(0, 2);
charm.foreground('green').write('Name').move(16).write('id').move(37).write('Address').move(11).write('Port');
charm.erase('down');
this.discovery.eachNode(node => {
let port = node.advertisement.port || '----';
port += '';
charm.position(0, index).foreground('cyan').write(node.advertisement.name.slice(0, 20)).move(20 - node.advertisement.name.length, 0).foreground('magenta').write(node.id).move(3, 0).foreground('yellow').write(node.address).move(3, 0).foreground('red').write(port);
index++;
});
charm.position(0, 1);
setTimeout(draw, interval);
};
draw();
}
};
//# sourceMappingURL=monitor.js.map