@zerodensity/realityhub-api 1.7.1 → 1.8.0

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.
@@ -8,10 +8,10 @@ jobs:
8
8
  name: Publish Job
9
9
  runs-on: ubuntu-latest
10
10
  steps:
11
- - uses: actions/checkout@v3
12
- - uses: actions/setup-node@v3
11
+ - uses: actions/checkout@v4
12
+ - uses: actions/setup-node@v4
13
13
  with:
14
- node-version: '18'
14
+ node-version: '20'
15
15
  registry-url: https://registry.npmjs.org/
16
16
  - run: npm ci
17
17
  - run: npm publish --tag next
@@ -10,10 +10,10 @@ jobs:
10
10
  name: Publish Job
11
11
  runs-on: ubuntu-latest
12
12
  steps:
13
- - uses: actions/checkout@v3
14
- - uses: actions/setup-node@v3
13
+ - uses: actions/checkout@v4
14
+ - uses: actions/setup-node@v4
15
15
  with:
16
- node-version: '18'
16
+ node-version: '20'
17
17
  registry-url: https://registry.npmjs.org/
18
18
  - run: npm ci
19
19
  - run: npm publish
package/README.md CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  A helper module to connect third-party modules to RealityHub.
4
4
 
5
+ ## Support Requests
6
+
7
+ This repository is for distribution only. For support please visit: [Zero Density Support](https://support.zerodensity.io).
8
+
5
9
  ## Installation
6
10
 
7
11
  ```bash
@@ -27,17 +31,7 @@ BrokerClient.initModule({
27
31
  hub: {
28
32
  host: '127.0.0.1',
29
33
  port: 3000,
30
- // or alternatively, provide the websocket url
31
- // url: http://127.0.0.1:3000/core
32
34
  },
33
- // optional error handler
34
- // brokerClient will log the error if not provided
35
- onError: (error, brokerClient) => {
36
- // you may destroy the instance (and retry calling initModule() later)
37
- // brokerClient.destroy();
38
- // it will try to recover otherwise
39
- console.error(error.message);
40
- }
41
35
  }).then((brokerClient) => {
42
36
  // brokerClient is connected to RealityHub and ready to use
43
37
  });
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "@zerodensity/realityhub-api",
3
- "version": "1.7.1",
3
+ "version": "1.8.0",
4
4
  "description": "RealityHub API Javascript Implementation",
5
5
  "main": "src/index.js",
6
+ "scripts": {},
6
7
  "repository": {
7
8
  "type": "git",
8
9
  "url": "git+ssh://git@github.com/zerodensity/realityhub-api.git"
@@ -19,12 +19,20 @@ const EventEmitter = require('events');
19
19
  const BrokerError = require('./BrokerError.js');
20
20
  const onceMultiple = require('./onceMultiple.js');
21
21
 
22
- const DEFAULT_MAX_WS_PACKET_SIZE = 4 /*MB*/ * 1024 * 1024;
22
+ const DEFAULT_MAX_WS_PACKET_SIZE = 20 /*MB*/ * 1024 * 1024;
23
23
 
24
+ /**
25
+ * BrokerBase constructor
26
+ * @param {object} params Parameters
27
+ * @param {string} [params.moduleName] Module name
28
+ * @param {number} [params.maxPacketSize] Maximum websocket packet size
29
+ * @param {Logger} [params.logger] Logger instance
30
+ */
24
31
  module.exports = class BrokerBase extends EventEmitter {
25
32
  constructor(params) {
26
33
  super();
27
34
  this.moduleName = params.moduleName;
35
+ this.maxPacketSize = params.maxPacketSize || DEFAULT_MAX_WS_PACKET_SIZE;
28
36
 
29
37
  this.logger = params.logger;
30
38
 
@@ -34,7 +42,6 @@ module.exports = class BrokerBase extends EventEmitter {
34
42
  this.messageTimeout = 2000;
35
43
 
36
44
  this.overridenTimeout = NaN; // NaN = use the implementation
37
- this.destroyed = false;
38
45
 
39
46
  try {
40
47
  if (typeof window != 'undefined' && typeof localStorage != 'undefined') {
@@ -53,18 +60,16 @@ module.exports = class BrokerBase extends EventEmitter {
53
60
 
54
61
  try {
55
62
  if (typeof window != 'undefined' && typeof localStorage != 'undefined') {
56
- maxPacketSizeRead = Number(localStorage.getItem('MAX_WS_PACKET_SIZE')) || NaN;
63
+ maxPacketSizeRead ||= Number(localStorage.getItem('MAX_WS_PACKET_SIZE'));
57
64
  } else if (process && process.env) {
58
- maxPacketSizeRead = Number(process.env.MAX_WS_PACKET_SIZE) || NaN;
65
+ maxPacketSizeRead ||= Number(process.env.MAX_WS_PACKET_SIZE);
59
66
  }
60
-
61
- maxPacketSizeRead = Math.max(maxPacketSizeRead, 1024000);
62
67
  } catch (ex) {
63
68
  const logger = this.logger || console;
64
69
  logger.error(`Cannot read maxPacketSize from either localStorage or env, defaulting to ${maxPacketSizeRead}`)
65
70
  }
66
71
 
67
- this.maxPacketSize = maxPacketSizeRead || DEFAULT_MAX_WS_PACKET_SIZE;
72
+ this.maxPacketSize = Math.max(this.maxPacketSize, maxPacketSizeRead);
68
73
 
69
74
  if(this.maxPacketSize !== DEFAULT_MAX_WS_PACKET_SIZE) {
70
75
  const logger = this.logger || console;
@@ -74,10 +79,6 @@ module.exports = class BrokerBase extends EventEmitter {
74
79
  this.initProxy();
75
80
  }
76
81
 
77
- get errorEmittingEnabled() {
78
- return this.listeners('error').length > 0;
79
- }
80
-
81
82
  getMethodProxy(vendorName, moduleName, options) {
82
83
  options = {
83
84
  timeout: 2000,
@@ -435,11 +436,6 @@ module.exports = class BrokerBase extends EventEmitter {
435
436
  try {
436
437
  responseMessage = await onceMultiple(this, [`response::${message.id}`], this.overridenTimeout || message.timeout || this.messageTimeout);
437
438
  } catch (ex) {
438
- if (this.errorEmittingEnabled) {
439
- this.emit('error', ex);
440
- return;
441
- }
442
-
443
439
  const logger = this.logger || console;
444
440
  logger.debug(`${this.moduleName} failed to send message ${message.type} to ${message.targetModuleName || ''}`);
445
441
  return;
@@ -493,11 +489,6 @@ module.exports = class BrokerBase extends EventEmitter {
493
489
  excludedClients: options.excludedClients || [],
494
490
  })
495
491
  .catch((err) => {
496
- if (this.errorEmittingEnabled) {
497
- this.emit('error', err);
498
- return;
499
- }
500
-
501
492
  console.error(`Couldn't emit ${fullyQualifiedName}`);
502
493
 
503
494
  if (err.code !== 'TIMEOUT') {
@@ -508,6 +499,5 @@ module.exports = class BrokerBase extends EventEmitter {
508
499
 
509
500
  destroy() {
510
501
  this.removeAllListeners();
511
- this.destroyed = true;
512
502
  }
513
503
  }
@@ -36,6 +36,8 @@ module.exports = class BrokerClient extends BrokerBase {
36
36
  * BrokerClient constructor
37
37
  * @param {object} params Parameters
38
38
  * @param {string} params.webSocketURL WebSocket URL
39
+ * @param {string} [params.moduleName] Module name
40
+ * @param {number} [params.maxPacketSize] Maximum websocket packet size
39
41
  * @param {Logger} [params.logger] Logger instance
40
42
  * @param {boolean} [params.isDuplicate] [Private property, used internally]
41
43
  * @param {boolean} [params.parent] [Private property, used internally]
@@ -89,14 +91,11 @@ module.exports = class BrokerClient extends BrokerBase {
89
91
  * @returns {Promise}
90
92
  */
91
93
  getConnectPromise() {
92
- return new Promise((resolve, reject) => {
94
+ return new Promise((resolve) => {
93
95
  const looper = setInterval(() => {
94
96
  if (this.connected) {
95
97
  clearInterval(looper);
96
98
  resolve();
97
- } else if (this.destroyed) {
98
- clearInterval(looper);
99
- reject(new Error('BrokerClient is destroyed'));
100
99
  }
101
100
  }, 0);
102
101
  });
@@ -113,7 +112,6 @@ module.exports = class BrokerClient extends BrokerBase {
113
112
  * @param {object} options Options
114
113
  * @param {string} options.host Hostname or IP of the target server.
115
114
  * @param {number} options.port WebSocket port of the target server.
116
- * @param {string} options.url A full URL of the websocket router (alternative to host and port options)
117
115
  */
118
116
  connect(options) {
119
117
  if (this.isDuplicate) return;
@@ -123,8 +121,6 @@ module.exports = class BrokerClient extends BrokerBase {
123
121
 
124
122
  if (!options) {
125
123
  url = new URL(location.href);
126
- } else if (options.url) {
127
- url = new URL(options.url);
128
124
  } else {
129
125
  url = { hostname: options.host, port: options.port };
130
126
  }
@@ -216,11 +212,6 @@ module.exports = class BrokerClient extends BrokerBase {
216
212
  try {
217
213
  await this.handleMessage(event.data, this.socket);
218
214
  } catch (ex) {
219
- if (this.errorEmittingEnabled) {
220
- this.emit('error', ex);
221
- return;
222
- }
223
-
224
215
  console.trace(ex);
225
216
  }
226
217
  }
@@ -260,11 +251,7 @@ module.exports = class BrokerClient extends BrokerBase {
260
251
  try {
261
252
  entry.eventHandler(...message.data);
262
253
  } catch (ex) {
263
- if (this.errorEmittingEnabled) {
264
- this.emit('error', ex);
265
- } else {
266
- this.logger.warn(ex);
267
- }
254
+ this.logger.warn(ex);
268
255
  } finally {
269
256
  if (entry.once) {
270
257
  this.unsubscribeFromAPIEvent(subscribedEvent, entry.eventHandler);
@@ -414,11 +401,6 @@ module.exports = class BrokerClient extends BrokerBase {
414
401
  }
415
402
  }
416
403
  } catch (ex) {
417
- if (this.errorEmittingEnabled) {
418
- this.emit('error', ex);
419
- return;
420
- }
421
-
422
404
  if (ex.code === 'TIMEOUT') {
423
405
  console.warn('Message timed out.');
424
406
  console.log(message);
@@ -533,11 +515,6 @@ module.exports = class BrokerClient extends BrokerBase {
533
515
  }
534
516
  }
535
517
  } catch (ex) {
536
- if (this.errorEmittingEnabled) {
537
- this.emit('error', ex);
538
- return;
539
- }
540
-
541
518
  console.trace(ex.message);
542
519
  }
543
520
  }
@@ -552,11 +529,7 @@ module.exports = class BrokerClient extends BrokerBase {
552
529
  }
553
530
 
554
531
  if (!this.isDuplicate) {
555
- if (this.errorEmittingEnabled) {
556
- this.emit('error', err);
557
- } else {
558
- this.logger.trace(err);
559
- }
532
+ this.logger.trace(err);
560
533
  }
561
534
 
562
535
  if (!this.isDuplicate) {
@@ -625,11 +598,6 @@ module.exports = class BrokerClient extends BrokerBase {
625
598
 
626
599
  return Promise.all(promises);
627
600
  } catch (ex) {
628
- if (this.errorEmittingEnabled) {
629
- this.emit('error', ex);
630
- return;
631
- }
632
-
633
601
  console.trace(ex.message);
634
602
  }
635
603
  }
@@ -671,11 +639,6 @@ module.exports = class BrokerClient extends BrokerBase {
671
639
  super.destroy();
672
640
  }
673
641
  } catch (ex) {
674
- if (this.errorEmittingEnabled) {
675
- this.emit('error', ex);
676
- return;
677
- }
678
-
679
642
  console.trace(ex.message);
680
643
  }
681
644
  }
@@ -712,12 +675,6 @@ module.exports = class BrokerClient extends BrokerBase {
712
675
  return this.registerHandlersToRemote(remote);
713
676
  }
714
677
 
715
- /**
716
- * @typedef {function} ErrorCallback
717
- * @param {Error} error
718
- * @param {BrokerClient} brokerClient
719
- /
720
-
721
678
  /**
722
679
  * Third-party modules can use this method to initialize a BrokerClient
723
680
  * and register themselves to RealityHub.
@@ -736,20 +693,12 @@ module.exports = class BrokerClient extends BrokerBase {
736
693
  * @param {{ host: string, port: number }} params.hub RealityHub connection parameters
737
694
  * @param {string} params.hub.host RealityHub hostname or IP address
738
695
  * @param {string} params.hub.port RealityHub port
739
- * @param {string} params.hub.url RealityHub websocket URL (alternative to host and port)
740
- * @param {ErrorCallback} [params.onError] Error handler callback, BrokerClient will catch the errors if not specified
741
696
  * @returns {Promise<BrokerClient, Error>} A BrokerClient instance.
742
697
  */
743
698
  static async initModule(params) {
744
- const { moduleName, serverURL, hub, webSocketURL = '/core', clientModuleName, menuTitle, onError} = params;
699
+ const { moduleName, serverURL, hub, webSocketURL = '/core', clientModuleName, menuTitle } = params;
745
700
  const hubClient = new BrokerClient({ moduleName, webSocketURL });
746
701
 
747
- if (typeof onError === 'function') {
748
- hubClient.on('error', (error) => {
749
- onError(error, hubClient);
750
- });
751
- }
752
-
753
702
  hubClient.connect(hub);
754
703
  await hubClient.getConnectPromise();
755
704
  if (!serverURL) return hubClient;