forked from sergeyksv/tinyhook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhook.js
More file actions
271 lines (231 loc) · 7.95 KB
/
hook.js
File metadata and controls
271 lines (231 loc) · 7.95 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
var EventEmitter = require('eventemitter2').EventEmitter2;
var util = require('util');
var nssocket = require('nssocket');
exports.Hook = Hook;
function Hook(options) {
if (!options) options = {};
// some options, name conversion close to hook.io
this.name = this.name || options.name || options['hook-name'] || 'no-name';
this.silent = this.silent || options.silent || true;
this.local = this.local || options.local || false;
this['hook-host'] = this['hook-host'] || options.host || options['hook-host'] || '127.0.0.1';
this['hook-port'] = this['hook-port'] || options.port || options['hook-port'] || 1976;
// some hookio flags that we support
this.listening = false;
this.ready = false;
// default eventemitter options
this.eventEmitter_Props = {
delimiter: "::",
wildcard: true
};
EventEmitter.call(this, this.eventEmitter_Props);
// semi-private props
this._clients = [];
this._client = null;
this._uid = 1;
this._eventTypes = {};
this._server = null;
}
util.inherits(Hook, EventEmitter);
Hook.prototype.spawn = require('./spawn').spawn;
Hook.prototype.listen = function(options, cb) {
// not sure which options can be passed, but lets
// keep this for compatibility with original hook.io
if (cb==null && options && options instanceof Function )
cb = options;
cb = cb || function () {};
var self = this;
var server = self._server = nssocket.createServer(function (socket) {
// assign unique client id
var cliId = self._uid += 1;
var client = {
id: cliId,
name: "hook_"+cliId,
socket: socket,
proxy: new EventEmitter(self.eventEmitter_Props)
};
self._clients.push(client);
// ignore errors, close will happens in anyway
socket.on('error', function (err) {
});
// properly shutdown connection
function destroy() {
// forget this client
for (var i=0; i<self._clients.length; i++) {
if (self._clients[i].id==cliId) {
self._clients.splice(i,1);
// shutdown proxy and nssocket
client.proxy.removeAllListeners();
client.socket.destroy();
break;
}
}
}
// clean context on client lost
socket.on('close', function () {
destroy();
});
// almost dummy hello greeting
socket.data('tinyhook::hello', function (d) {
client.name = d.name;
});
// handle on and off to filter delivery of messages
// everybody deliver to server, server filter and deliver to clients
// we'll use proxy/stub of native EventEmitter2 to repeat behavior
socket.data('tinyhook::on', function (d) {
if (client.proxy.listeners(d.type).length == 0) {
client.proxy.on(d.type, function (data) {
try {
client.socket.send('tinyhook::pushemit', data);
} catch (e) {
// both we and nssocket has a function that should prevent
// this happening, but probably died/corrupted sockets can't
// be detected by sockets error and close event and this can happens
destroy();
}
})
}
// synthesize newListener event
self.emit('hook::newListener', d.type, client.name);
});
socket.data('tinyhook::off', function (d) {
client.proxy.removeAllListeners(d.type);
});
// once we receive any event from child, deliver it to all clients
// with smart filtering which is provided by EventEmitter2
socket.data('tinyhook::emit', function (d) {
d.event = client.name+"::"+d.event;
self._clients.forEach(function (cli) {
cli.proxy.emit(d.event, d);
});
// don't forget about ourselves
EventEmitter.prototype.emit.apply(self, [d.event, d.data]);
});
});
server.on('error', function (e) {
server = self._server = null;
// here cb can be null, if we start listening and error happens after that
if (cb)
cb(e);
});
server.on('close', function (e) {
server = self._server = null;
self.listening = false;
self.ready = false;
});
server.on('listening', function () {
self.listening = true;
self.ready = true;
cb();
// set callback to null, so we wan't ping it one more time in error handler
cb = null;
EventEmitter.prototype.emit.apply(self, ['hook::ready']);
});
server.listen(self['hook-port'], self['hook-host']);
};
Hook.prototype.connect = function(options, cb) {
// not sure which options can be passed, but lets
// keep this for compatibility with original hook.io
if (cb==null && options && options instanceof Function )
cb = options;
cb = cb || function () {};
options = options|| {};
var self = this;
// since we using reconnect, will callback rightaway
cb();
var client = this._client = new nssocket.NsSocket({reconnect: options.reconnect});
client.connect(self['hook-port'], self['hook-host']);
// when connection started we sayng hello and push
// all known event types we have
client.on('start', function () {
client.send(['tinyhook', 'hello'], {protoVersion: 1, name: self.name});
// purge known event types
Object.keys(self._eventTypes).forEach(function(type) {
client.send(['tinyhook', 'on'], {type: type});
});
if (!self.ready) {
// simulate hook:ready
self.ready = true;
self.emit('hook::ready');
}
});
client.on('close', function() {
self.ready = false;
client = self._client = null;
})
// tranlate pushed emit to local one
client.data('tinyhook::pushemit',function (d) {
EventEmitter.prototype.emit.apply(self, [d.event, d.data]);
});
// every XX seconds do garbage collect and notify server about
// event we longer not listening. Realtime notification is not necessary
// Its ok if for some period we receive events that are not listened
self.gcId = setInterval(function () {
Object.keys(self._eventTypes).forEach(function(type) {
var listeners = self.listeners(type);
if (listeners == null || listeners.length == 0) {
// no more listener for this event
// push this to server
client.send(['tinyhook','off'],{type:type});
delete self._eventTypes[type];
}
});
}, 60000);
};
// Function will attempt to start server, if it fails we assume that server already available
// then it start in client mode. So first hook will became super hook, overs its clients
Hook.prototype.start = function(options, cb) {
// not sure which options can be passed, but lets
// keep this for compatibility with original hook.io
if (cb==null && options && options instanceof Function )
cb = options;
cb = cb || function () {};
options = options || {};
var self = this;
this.listen(function(e) {
if (e!=null && (e.code == 'EADDRINUSE' || e.code == 'EADDRNOTAVAIL')) {
// if server start fails we attempt to start in client mode
self.connect(options, cb);
} else {
cb(e);
}
});
};
Hook.prototype.stop = function(cb) {
cb = cb || function () {};
if (this._server) {
this._server.on('close',cb);
this._server.close();
} else if (this._client) {
clearInterval(this.gcId);
this._client.once('close',cb);
this._client.destroy();
} else {
cb();
}
};
// hook into core events to dispatch events as required
Hook.prototype.emit = function(event, data, callback) {
// on client send event to master
if (this._client) {
this._client.send(['tinyhook', 'emit'], {eid: this._uid++, event: event, data: data}, function () {});
}
// send to clients event emitted on server (master)
if (this._server) {
var d = {event: this.name+"::"+event, data: data};
this._clients.forEach(function (cli) {
cli.proxy.emit(d.event, d);
});
}
// still preserve local processing
EventEmitter.prototype.emit.apply(this, arguments);
};
Hook.prototype.on = function(type, listener) {
if (this._client) {
this._client.send(['tinyhook', 'on'], {type: type}, function () {});
}
if (this._eventTypes) {
this._eventTypes[type] = 1;
}
EventEmitter.prototype.on.apply(this, arguments);
};