-
Notifications
You must be signed in to change notification settings - Fork 86
/
listener.js
executable file
·297 lines (210 loc) · 7.27 KB
/
listener.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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// Load modules
var Boom = require('boom');
var Call = require('call');
var Hoek = require('hoek');
var Joi = require('joi');
var Ws = require('ws');
var Socket = require('./socket');
// Declare internals
var internals = {};
exports = module.exports = internals.Listener = function (connection, settings) {
var self = this;
this._connection = connection;
this._settings = settings;
this._sockets = [];
this._router = new Call.Router();
// WebSocket listener
this._wss = new Ws.Server({ server: connection.listener });
this._wss.on('connection', function (ws) {
self._add(ws);
});
this._wss.on('error', function (err) {
});
// Register with connection
connection.plugins.nes = {
_listener: this
};
};
internals.Listener.prototype._add = function (ws) {
var self = this;
// Socket object
var socket = new Socket(ws, this);
// Subscriptions
self._sockets.push(socket);
ws.once('close', function (code, message) {
self._sockets.splice(self._sockets.indexOf(socket), 1);
});
// Authentication
socket.authenticate(function () {
if (self._settings.onConnect) {
self._settings.onConnect(socket);
}
});
};
internals.Listener.prototype.close = function () {
this._wss.close();
};
internals.Listener.broadcast = function (message) {
var update = {
type: 'broadcast',
message: message
};
var connections = this.connections;
for (var i = 0, il = connections.length; i < il; ++i) {
var connection = connections[i];
if (connection.plugins.nes) {
connection.plugins.nes._listener.broadcast(update);
}
}
};
internals.Listener.prototype.broadcast = function (update) {
var sockets = this._sockets;
for (var i = 0, il = sockets.length; i < il; ++i) {
var socket = sockets[i];
socket.send(update);
}
};
internals.subSchema = Joi.object({
filter: Joi.func(), // function (path, update, options, next), where options: { credentials, params }
auth: Joi.object({
mode: Joi.string().valid('required', 'optional'),
scope: Joi.array().items(Joi.string()).single().min(1),
entity: Joi.valid('user', 'app', 'any')
})
.allow(false)
});
internals.Listener.subscription = function (path, options) {
Hoek.assert(path, 'Subscription missing path');
Joi.assert(options, internals.subSchema, 'Invalid subscription options: ' + path);
var settings = Hoek.clone(options || {});
// Auth configuration
var auth = settings.auth;
if (auth) {
if (auth.scope) {
if (typeof auth.scope === 'string') {
auth.scope = [auth.scope];
}
for (var i = 0, il = auth.scope.length; i < il; ++i) {
if (/{([^}]+)}/.test(auth.scope[i])) {
auth.hasScopeParameters = true;
break;
}
}
}
auth.mode = auth.mode || 'required';
}
// Path configuration
var route = {
method: 'sub',
path: path
};
var config = {
subscribers: [], // { socket, path }
filter: settings.filter,
auth: auth
};
var connections = this.connections;
for (var c = 0, cl = connections.length; c < cl; ++c) {
var connection = connections[c];
if (connection.plugins.nes) {
connection.plugins.nes._listener._router.add(route, config);
}
}
};
internals.Listener.publish = function (path, update) {
Hoek.assert(path && path[0] === '/', 'Missing or invalid subscription path:', path || 'empty');
var message = {
type: 'pub',
path: path,
message: update
};
var connections = this.connections;
for (var i = 0, il = connections.length; i < il; ++i) {
var connection = connections[i];
if (connection.plugins.nes) {
connection.plugins.nes._listener.publish(path, message);
}
}
};
internals.Listener.prototype.publish = function (path, update) {
var sub = this._router.route('sub', path);
if (sub.isBoom) {
return;
}
var route = sub.route;
var actives = this._sockets;
var subscribers = route.subscribers;
var live = [];
for (var i = 0, il = subscribers.length; i < il; ++i) {
var subscribed = subscribers[i];
var socket = subscribed.socket;
if (actives.indexOf(socket) === -1) {
continue;
}
// Keep active socket in subscription
live.push(subscribed);
// Validate path matches
if (!sub.paramsArray.length || // Literal path
subscribed.path === path) { // Exact parameterized match
if (!route.filter) {
socket.send(update);
}
else {
route.filter(path, update.message, { credentials: socket._auth.credentials, params: sub.params }, internals.send(socket, update));
}
}
}
route.subscribers = live;
};
internals.send = function (socket, update) {
return function (isMatch) {
if (isMatch) {
return socket.send(update);
}
};
};
internals.Listener.prototype.subscribe = function (path, socket, next) {
if (path.indexOf('?') !== -1) {
return next(Boom.badRequest('Subscription path cannot contain query'));
}
var match = this._router.route('sub', path);
if (match.isBoom) {
return next(Boom.notFound());
}
var auth = this._connection.auth.lookup({ settings: { auth: match.route.auth } }); // Create a synthetic route
if (auth) {
var credentials = socket._auth.credentials;
if (credentials) {
// Check scope
if (auth.scope) {
var scopes = auth.scope;
if (auth.hasScopeParameters) {
scopes = [];
var context = { params: match.params };
for (var i = 0, il = auth.scope.length; i < il; ++i) {
scopes[i] = Hoek.reachTemplate(context, auth.scope[i]);
}
}
if (!credentials.scope ||
(typeof credentials.scope === 'string' ? scopes.indexOf(credentials.scope) === -1 : !Hoek.intersect(scopes, credentials.scope).length)) {
return next(Boom.forbidden('Insufficient scope, expected any of: ' + scopes));
}
}
// Check entity
var entity = auth.entity || 'any';
if (entity === 'user' &&
!credentials.user) {
return next(Boom.forbidden('Application credentials cannot be used on a user subscription'));
}
if (entity === 'app' &&
credentials.user) {
return next(Boom.forbidden('User credentials cannot be used on an application subscription'));
}
}
else if (auth.mode === 'required') {
return next(Boom.unauthorized());
}
}
match.route.subscribers.push({ socket: socket, path: path });
return next();
};