@zerodensity/realityhub-api 1.22.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.
@@ -12,7 +12,6 @@ const { EventEmitter } = require('node:events');
12
12
  * @property {*} data
13
13
  * @property {string|null} [error]
14
14
  * @property {boolean} [stale]
15
- * @property {string} [status] `disabled` when the host intentionally stops source traffic.
16
15
  *
17
16
  * @typedef {Object} Subscription
18
17
  * @property {function(): void} stop
@@ -35,7 +34,6 @@ const { EventEmitter } = require('node:events');
35
34
  * @property {function(Object|null): Promise<Snapshot>} setState
36
35
  * @property {function(Function): void} schedule Queue background work after the current handler/startup.
37
36
  * @property {Object} data Named query bindings configured on the field.
38
- * @property {boolean} data.enabled Whether bound query traffic is enabled for this field.
39
37
  * @property {function(string, Object=): Promise<DataSnapshot>} data.request
40
38
  * @property {function(string, Object, Function, Function=): Subscription} data.subscribe Legacy direct callbacks.
41
39
  * @property {function(string, Object, Function, Function=): Watch} data.watch Await callbacks in the handler queue.
@@ -175,7 +173,6 @@ function createBackendRuntime({ snapshot: initial, context = {}, request, emit }
175
173
  enqueue(operation).catch(report);
176
174
  },
177
175
  data: {
178
- enabled: context.dataSourceEnabled !== false,
179
176
  request: (query, parameters = {}) => request('data.request', { query, parameters }),
180
177
  subscribe,
181
178
  watch,
@@ -11,7 +11,6 @@ const copy = (value) => JSON.parse(JSON.stringify(value));
11
11
  * @property {Object} context
12
12
  * @property {boolean} backend
13
13
  * @property {boolean} paused
14
- * @property {boolean} dataSourceEnabled
15
14
  * @property {'connecting'|'connected'|'disconnected'|'closed'|'error'} connection
16
15
  * @property {string|null} error
17
16
  *
@@ -59,7 +58,6 @@ function createClient({ window: target = window, timeoutMs = 15000 } = {}) {
59
58
  context: {},
60
59
  backend: Boolean(backend),
61
60
  paused: false,
62
- dataSourceEnabled: true,
63
61
  connection: 'connecting',
64
62
  error: null,
65
63
  };
@@ -178,7 +176,6 @@ function createClient({ window: target = window, timeoutMs = 15000 } = {}) {
178
176
  readOnly: Boolean(message.readOnly),
179
177
  context: message.context || {},
180
178
  paused: Boolean(message.paused),
181
- dataSourceEnabled: message.dataSourceEnabled !== false,
182
179
  connection: backend ? backendConnection : 'connected',
183
180
  ...(!backend ? { state: message.state || null, value: message.value === undefined ? null : message.value } : {}),
184
181
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zerodensity/realityhub-api",
3
- "version": "1.22.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
  }