/
websocket.js
451 lines (366 loc) · 12.1 KB
/
websocket.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/*
* Kuzzle, a backend software, self-hostable and ready to use
* to power modern apps
*
* Copyright 2015-2018 Kuzzle
* mailto: support AT kuzzle.io
* website: http://kuzzle.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
const
errorsManager = require('../../../../../config/error-codes/throw').wrap('network', 'websocket'),
assert = require('assert'),
debug = require('../../../../../kuzzleDebug')('kuzzle:entry-point:protocols:websocket'),
Protocol = require('./protocol'),
{ Server: WebSocketServer, Sender } = require('ws'),
ClientConnection = require('../clientConnection'),
{
Request,
errors: {
InternalError: KuzzleInternalError
}
} = require('kuzzle-common-objects'),
removeErrorStack = require('../removeErrorStack');
// Used by the broadcast method to build JSON payloads while limiting the
// number of JSON serializations
const
jsonRoomProp = ',"room":"',
jsonEnder = '"}';
// Passed to ws.Sender to build a RFC-6455 frame
const wsFrameOptions = {
fin: true,
rsv1: false,
opcode: 1,
mask: false,
readOnly: true
};
/**
* @class WebSocketConnection
*/
class WebSocketConnection {
constructor(socket, lastActivity) {
this.alive = true;
this.socket = socket;
this.channels = new Set();
this.lastActivity = lastActivity;
}
}
/**
* @class WebsocketProtocol
*/
class WebSocketProtocol extends Protocol {
constructor () {
super();
this.server = null;
this.kuzzle = null;
this.heartbeatInterval = null;
this.idleSweepInterval = null;
// Prevents thousands of "Date.now()" per second: it's far more efficient
// to have a timestamp property refreshed every second or so, since we
// don't need to be more precise than that
this.activityTimestamp = Date.now();
this.activityInterval = setInterval(() => {
this.activityTimestamp = Date.now();
}, 1000);
// Map<channel, Set<connection IDs>>
this.channels = new Map();
// Map<connection ID, WebSocketConnection>
this.connectionPool = new Map();
}
/**
*
* @param {EmbeddedEntryPoint} entryPoint
*/
init (entryPoint) {
return super.init('websocket', entryPoint)
.then(() => {
if (this.config.enabled === false) {
return false;
}
debug('initializing WebSocket Server with config: %a', this.config);
this._startHeartbeat();
this._startIdleSweeps();
this.kuzzle = this.entryPoint.kuzzle;
this.server = new WebSocketServer({
server: entryPoint.httpServer,
maxPayload: this.maxRequestSize,
perMessageDeflate: false
});
this.server.on('connection', this.onConnection.bind(this));
this.server.on('error', this.onServerError.bind(this));
return true;
});
}
onServerError(error) {
this.kuzzle.log.error(
`[websocket] An error has occured "${error.message}":\n${error.stack}`);
}
onConnection(socket, request) {
if (request.url && request.url.startsWith('/socket.io/')) {
// Discarding request management here: let socket.io protocol manage
// this connection
return;
}
const
ips = this._getIps(request),
connection = new ClientConnection(this.name, ips, request.headers),
wsConnection = new WebSocketConnection(socket, this.activityTimestamp);
debug('[%s] creating Websocket connection', connection.id);
this.entryPoint.newConnection(connection);
this.connectionPool.set(connection.id, wsConnection);
socket.on('close', () => {
debug('[%s] received a `close` event', connection.id);
this.onClientDisconnection(connection.id);
});
socket.on('error', () => {
debug('[%s] received an `error` event', connection.id);
this.onClientDisconnection(connection.id);
});
socket.on('message', data => {
debug('[%s] received a `message` event', connection.id);
this.onClientMessage(connection, data);
});
socket.on('ping', () => {
debug('[%s] received a `ping` event', connection.id);
wsConnection.alive = true;
wsConnection.lastActivity = this.activityTimestamp;
});
socket.on('pong', () => {
debug('[%s] received a `pong` event', connection.id);
wsConnection.alive = true;
wsConnection.lastActivity = this.activityTimestamp;
});
}
onClientDisconnection(clientId) {
debug('[%s] Client disconnected', clientId);
this.entryPoint.removeConnection(clientId);
const connection = this.connectionPool.get(clientId);
if (!connection) {
return;
}
connection.alive = false;
for (const channel of connection.channels) {
const ids = this.channels.get(channel);
if (ids) {
ids.delete(clientId);
if (ids.size === 0) {
this.channels.delete(channel);
}
}
}
this.connectionPool.delete(clientId);
}
onClientMessage(connection, data) {
const wsConnection = this.connectionPool.get(connection.id);
if (!data || !wsConnection) {
return;
}
wsConnection.lastActivity = this.activityTimestamp;
let parsed;
debug('[%s] onClientMessage: %a', connection.id, data);
try {
parsed = JSON.parse(data);
}
catch (e) {
/*
we cannot add a "room" information since we need to extract
a request ID from the incoming data, which is apparently
not a valid JSON
So... the error is forwarded to the client, hoping they know
what to do with it.
*/
const errRequest = new Request(
{},
{
connection,
error: errorsManager.getError('websocket_request_error', e.message)
});
return this._send(
connection.id,
JSON.stringify(removeErrorStack(errRequest.response.toJSON()).content));
}
try {
this.entryPoint.execute(new Request(parsed, {connection}), result => {
if (result.content && typeof result.content === 'object') {
result.content.room = result.requestId;
}
return this._send(connection.id, JSON.stringify(result.content));
});
}
catch (e) {
const errobj = {
room: parsed.requestId,
status: 400,
error: {
message: e.message
}
};
this.entryPoint.kuzzle.log.error(new KuzzleInternalError(e.message));
return this._send(connection.id, JSON.stringify(errobj));
}
}
/**
* /!\ WARNING: CRITICAL CODE SECTION AHEAD /!\
* (this means performance over maintenability or readability... sorry)
*
* Do not change without reason: this method is used by the real-time engine
* to send very similar notifications to A LOT of different sockets
*
* This function precomputes RFC-6455 WebSocket frames and send them raw to
* destination sockets. This prevents the recomputing of the same frame
* over and over again for each broadcast.
*
* @param {Object} data
*/
broadcast(data) {
const
stringified = JSON.stringify(data.payload),
// 255 bytes should be enough to hold the following:
// ,"room":"<channel identifier>"
// (with current channel encoding, this is less than 100 bytes)
payload = Buffer.allocUnsafe(stringified.length + 255);
let offset = stringified.length - 1;
payload.write(stringified, 0);
payload.write(jsonRoomProp, offset);
offset += jsonRoomProp.length;
for (const channel of data.channels) {
const connectionIds = this.channels.get(channel);
if (connectionIds) {
// Adds the room property to the message
payload.write(channel, offset);
payload.write(jsonEnder, offset + channel.length);
// prevent buffer overwrites due to socket.send being an
// async method (race condition)
const
payloadLength = offset + channel.length + jsonEnder.length,
payloadSafeCopy = Buffer.allocUnsafe(payloadLength);
payload.copy(payloadSafeCopy, 0, 0, payloadLength);
const frame = Sender.frame(payloadSafeCopy, wsFrameOptions);
for (const connectionId of connectionIds) {
const connection = this.connectionPool.get(connectionId);
if (connection &&
connection.alive &&
connection.socket.readyState === connection.socket.OPEN
) {
connection.socket._sender.sendFrame(frame);
}
}
}
}
}
notify(data) {
const payload = data.payload;
debug('notify: %a', data);
data.channels.forEach(channel => {
payload.room = channel;
this._send(data.connectionId, JSON.stringify(payload));
});
}
joinChannel(channel, connectionId) {
debug('joinChannel: %s %s', channel, connectionId);
const connection = this.connectionPool.get(connectionId);
if (!connection || !connection.alive) {
return;
}
let ids = this.channels.get(channel);
if (!ids) {
ids = new Set([connectionId]);
this.channels.set(channel, ids);
} else {
ids.add(connectionId);
}
connection.channels.add(channel);
}
leaveChannel(channel, connectionId) {
debug('leaveChannel: %s %s', channel, connectionId);
const
connection = this.connectionPool.get(connectionId),
ids = this.channels.get(channel);
if (!connection || !ids || !ids.has(connectionId)) {
return;
}
ids.delete(connectionId);
if (ids.size === 0) {
this.channels.delete(channel);
}
connection.channels.delete(channel);
}
disconnect(clientId, message = 'Connection closed by remote host') {
debug('[%s] disconnect', clientId);
const connection = this.connectionPool.get(clientId);
if (connection) {
connection.alive = false;
connection.socket.close(1011, message);
}
}
_send (id, data) {
debug('[%s] send: %a', id, data);
const connection = this.connectionPool.get(id);
if ( connection
&& connection.alive
&& connection.socket.readyState === connection.socket.OPEN
) {
connection.socket.send(data);
}
}
_startHeartbeat() {
assert(
Number.isInteger(this.config.heartbeat) && this.config.heartbeat >= 0,
`WebSocket: invalid heartbeat value ${this.config.heartbeat}`);
if (this.config.heartbeat > 0) {
this.heartbeatInterval = setInterval(
this._doHeartbeat.bind(this),
this.config.heartbeat);
}
}
_startIdleSweeps() {
assert(
Number.isInteger(this.config.idleTimeout) && this.config.idleTimeout >= 0,
`WebSocket: invalid idleTimeout value ${this.config.idleTimeout}`);
if (this.config.idleTimeout > 0) {
this.idleSweepInterval = setInterval(
this._sweepIdleSockets.bind(this),
this.config.idleTimeout);
}
}
_doHeartbeat() {
debug('Heartbeat');
const
lastActivityThreshold = this.activityTimestamp - this.config.heartbeat;
for (const connection of this.connectionPool.values()) {
if ( connection.alive === false
|| connection.socket.readyState !== connection.socket.OPEN
) {
connection.socket.terminate();
} else if (connection.lastActivity < lastActivityThreshold) {
// emit a PING request only if the socket has been inactive for longer
// than the heartbeat value
connection.alive = false;
connection.socket.ping();
}
}
}
_sweepIdleSockets() {
debug('Cleaning dead sockets');
const now = Date.now();
for (const connection of this.connectionPool.values()) {
if ((now - connection.lastActivity) > this.config.idleTimeout) {
// correctly triggers the 'close' event handler on that socket
connection.socket.terminate();
}
}
}
}
module.exports = WebSocketProtocol;