@zerodensity/realityhub-api 1.23.0 → 1.23.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zerodensity/realityhub-api",
3
- "version": "1.23.0",
3
+ "version": "1.23.1",
4
4
  "description": "RealityHub API Javascript Implementation",
5
5
  "main": "src/index.js",
6
6
  "repository": {
@@ -31,6 +31,39 @@ if (runningInBrowser) {
31
31
  ws = require('ws');
32
32
  }
33
33
 
34
+ // Parsed JSON containers are private to each logical client. Strings (including
35
+ // large encoded payloads) can be reused without reparsing or copying their bytes.
36
+ function cloneMessageValue(value) {
37
+ if (value === null || typeof value !== 'object') return value;
38
+
39
+ const clone = Array.isArray(value) ? [] : {};
40
+ const pending = [{ source: value, target: clone }];
41
+
42
+ while (pending.length) {
43
+ const { source, target } = pending.pop();
44
+
45
+ for (const key of Object.keys(source)) {
46
+ const child = source[key];
47
+ let copy = child;
48
+
49
+ if (child !== null && typeof child === 'object') {
50
+ copy = Array.isArray(child) ? [] : {};
51
+ pending.push({ source: child, target: copy });
52
+ }
53
+
54
+ // Define own properties so a JSON "__proto__" key stays ordinary data.
55
+ Object.defineProperty(target, key, {
56
+ value: copy,
57
+ enumerable: true,
58
+ configurable: true,
59
+ writable: true,
60
+ });
61
+ }
62
+ }
63
+
64
+ return clone;
65
+ }
66
+
34
67
  module.exports = class BrokerClient extends BrokerBase {
35
68
  /**
36
69
  * BrokerClient constructor
@@ -221,7 +254,7 @@ module.exports = class BrokerClient extends BrokerBase {
221
254
 
222
255
  async onSocketMessage(event) {
223
256
  try {
224
- await this.handleMessage(event.data, this.socket);
257
+ await this.handleMessage(event.data);
225
258
  } catch (ex) {
226
259
  console.trace(ex);
227
260
  }
@@ -234,20 +267,34 @@ module.exports = class BrokerClient extends BrokerBase {
234
267
  * @private
235
268
  */
236
269
  async handleMessage(rawMessage) {
237
- let message;
270
+ try {
271
+ await this.handleParsedMessage(JSON.parse(rawMessage));
272
+ } catch (ex) {
273
+ console.trace(ex);
274
+ }
275
+ }
238
276
 
277
+ /**
278
+ * Dispatches an already parsed message without exposing it to event handlers.
279
+ * @param {object} message Parsed message
280
+ * @async
281
+ * @private
282
+ */
283
+ async handleParsedMessage(message) {
239
284
  try {
240
- message = JSON.parse(rawMessage);
241
285
  const socket = this.isDuplicate ? this.parent.getSocket() : this.socket;
242
286
 
243
287
  switch (message.type) {
244
288
  case 'response': {
245
- this.emit(`response::${message.requestId}`, message);
289
+ const eventName = `response::${message.requestId}`;
290
+ if (this.listenerCount(eventName)) {
291
+ this.emit(eventName, cloneMessageValue(message));
292
+ }
246
293
 
247
294
  // Send the response to other duplicates (if we are parent)
248
295
  if (!this.isDuplicate) {
249
296
  for (const duplicate of this.duplicates) {
250
- duplicate.handleMessage(rawMessage);
297
+ duplicate.handleParsedMessage(message);
251
298
  }
252
299
  }
253
300
 
@@ -257,10 +304,11 @@ module.exports = class BrokerClient extends BrokerBase {
257
304
  case 'event': {
258
305
  // Run previously registered event handlers
259
306
  for (const [subscribedEvent, entries] of this.events.entries()) {
260
- if (subscribedEvent === message.eventName) {
307
+ if (subscribedEvent === message.eventName && entries.length) {
308
+ const data = cloneMessageValue(message.data);
261
309
  for (const entry of entries) {
262
310
  try {
263
- entry.eventHandler(...message.data);
311
+ entry.eventHandler(...data);
264
312
  } catch (ex) {
265
313
  this.logger.warn(ex);
266
314
  } finally {
@@ -275,7 +323,7 @@ module.exports = class BrokerClient extends BrokerBase {
275
323
  // Send the event to other duplicates (if we are parent)
276
324
  if (!this.isDuplicate) {
277
325
  for (const duplicate of this.duplicates) {
278
- duplicate.handleMessage(rawMessage);
326
+ duplicate.handleParsedMessage(message);
279
327
  }
280
328
  }
281
329
 
@@ -295,7 +343,7 @@ module.exports = class BrokerClient extends BrokerBase {
295
343
  if (!this.isDuplicate) {
296
344
  for (const duplicate of this.duplicates) {
297
345
  if (duplicate.moduleName === targetModuleName) {
298
- duplicate.handleMessage(rawMessage);
346
+ duplicate.handleParsedMessage(message);
299
347
  return;
300
348
  }
301
349
  }
@@ -325,7 +373,7 @@ module.exports = class BrokerClient extends BrokerBase {
325
373
  if (!this.isDuplicate) {
326
374
  for (const duplicate of this.duplicates) {
327
375
  if (duplicate.moduleName === targetModuleName) {
328
- duplicate.handleMessage(rawMessage);
376
+ duplicate.handleParsedMessage(message);
329
377
  return;
330
378
  }
331
379
  }
@@ -348,7 +396,7 @@ module.exports = class BrokerClient extends BrokerBase {
348
396
  if (!this.isDuplicate && message.targetModuleName !== this.moduleName) {
349
397
  for (const duplicate of this.duplicates) {
350
398
  if (duplicate.moduleName === message.targetModuleName) {
351
- duplicate.handleMessage(rawMessage);
399
+ duplicate.handleParsedMessage(message);
352
400
  return;
353
401
  }
354
402
  }