-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
146 lines (116 loc) · 3.23 KB
/
main.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
var net = require('net'),
sockets = [],
commands = {},
defaults = {},
filters = {},
servers = {},
BB_DEFAULT_PORT = 51980,
BB_DEFAULT_HOST = '127.0.0.1';
function BootBoy(uid, port, host) {
this.uid = uid;
this.port = port;
this.host = host;
defaults[this.uid] = undefined;
filters[this.uid] = function (data, next) {
return data;
};
}
function uid() {
function part() {
return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
}
return part() + part() + '-' + part() + part();
}
function netRespond(socket, counter, response) {
if (sockets.indexOf(socket) === -1) {
socket.destroy();
} else {
socket.write(response + '\n');
if (--counter === 0) {
setImmediate(socket.end.bind(socket));
}
}
}
function register(name, handler) {
var command, config, myCommands;
commands[this.uid] = myCommands = commands[this.uid] || {};
if (typeof name === 'string' && name.length > 0) {
command = {
handlers: []
};
config = myCommands[name];
config = myCommands[name] = config || command;
if (typeof handler === 'function') {
config.handlers.push(handler.bind.bind(handler, this));
}
}
}
BootBoy.prototype.default = function (handler) {
if (typeof handler === 'function') {
defaults[this.uid] = handler.bind.bind(handler, this);
}
};
BootBoy.prototype.filter = function (fn) {
if (typeof fn === 'function') {
filters[this.uid] = fn.bind(this);
}
};
BootBoy.prototype.learn = function (command, handler) {
register.call(this, command, handler);
};
BootBoy.prototype.listen = function () {
var me = this;
if (servers[this.uid]) {
return;
}
servers[this.uid] = net.createServer(function (socket) {
sockets.push(socket);
socket.setEncoding('utf8');
socket.on('data', function (data) {
var command, config, handlers, handler, counter;
data = data.trim();
data = data.split('\n');
command = data.shift();
data = data.join('\n');
config = commands[me.uid][command] || {};
try {
data = filters[me.uid]({
command: command,
payload: data
});
} catch (err) {
setImmediate(netRespond.bind(null, socket, 1, err.message));
return;
}
handlers = config.handlers || [];
handlers = handlers.slice();
counter = 0;
if (handlers.length === 0) {
if (defaults[me.uid]) {
setImmediate(defaults[me.uid](data, netRespond.bind(null, socket, 1)));
} else {
setImmediate(socket.end.bind(socket));
}
}
while (handler = handlers.shift()) {
setImmediate(handler(data, netRespond.bind(null, socket, ++counter)));
}
});
socket.on('error', function (err) {
// no-op
});
socket.on('close', function (err) {
sockets = sockets.filter(function (socket) {
return socket !== this;
}, this);
if (err) {
this.destroy()
}
});
}).listen(this.port, this.host);
};
module.exports.hire = function (port, host) {
port = port || BB_DEFAULT_PORT;
host = host || BB_DEFAULT_HOST;
return new BootBoy(uid(), port, host);
};