@zerodensity/realityhub-api 1.6.0 → 1.7.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.
@@ -0,0 +1,19 @@
1
+ name: Publish Package to NPM (tag next)
2
+
3
+ on:
4
+ workflow_dispatch:
5
+
6
+ jobs:
7
+ publish-npm:
8
+ name: Publish Job
9
+ runs-on: ubuntu-latest
10
+ steps:
11
+ - uses: actions/checkout@v3
12
+ - uses: actions/setup-node@v3
13
+ with:
14
+ node-version: '18'
15
+ registry-url: https://registry.npmjs.org/
16
+ - run: npm ci
17
+ - run: npm publish --tag next
18
+ env:
19
+ NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
@@ -10,12 +10,13 @@ jobs:
10
10
  name: Publish Job
11
11
  runs-on: ubuntu-latest
12
12
  steps:
13
- - uses: actions/checkout@v2
14
- - uses: actions/setup-node@v1
13
+ - uses: actions/checkout@v3
14
+ - uses: actions/setup-node@v3
15
15
  with:
16
- node-version: 14
16
+ node-version: '18'
17
17
  registry-url: https://registry.npmjs.org/
18
18
  - run: npm ci
19
19
  - run: npm publish
20
+ - run: npm publish --tag latest
20
21
  env:
21
22
  NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
package/README.md CHANGED
@@ -27,7 +27,17 @@ BrokerClient.initModule({
27
27
  hub: {
28
28
  host: '127.0.0.1',
29
29
  port: 3000,
30
+ // or alternatively, provide the websocket url
31
+ // url: http://127.0.0.1:3000/core
30
32
  },
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
+ }
31
41
  }).then((brokerClient) => {
32
42
  // brokerClient is connected to RealityHub and ready to use
33
43
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zerodensity/realityhub-api",
3
- "version": "1.6.0",
3
+ "version": "1.7.1",
4
4
  "description": "RealityHub API Javascript Implementation",
5
5
  "main": "src/index.js",
6
6
  "repository": {
@@ -34,6 +34,7 @@ module.exports = class BrokerBase extends EventEmitter {
34
34
  this.messageTimeout = 2000;
35
35
 
36
36
  this.overridenTimeout = NaN; // NaN = use the implementation
37
+ this.destroyed = false;
37
38
 
38
39
  try {
39
40
  if (typeof window != 'undefined' && typeof localStorage != 'undefined') {
@@ -73,6 +74,10 @@ module.exports = class BrokerBase extends EventEmitter {
73
74
  this.initProxy();
74
75
  }
75
76
 
77
+ get errorEmittingEnabled() {
78
+ return this.listeners('error').length > 0;
79
+ }
80
+
76
81
  getMethodProxy(vendorName, moduleName, options) {
77
82
  options = {
78
83
  timeout: 2000,
@@ -430,6 +435,11 @@ module.exports = class BrokerBase extends EventEmitter {
430
435
  try {
431
436
  responseMessage = await onceMultiple(this, [`response::${message.id}`], this.overridenTimeout || message.timeout || this.messageTimeout);
432
437
  } catch (ex) {
438
+ if (this.errorEmittingEnabled) {
439
+ this.emit('error', ex);
440
+ return;
441
+ }
442
+
433
443
  const logger = this.logger || console;
434
444
  logger.debug(`${this.moduleName} failed to send message ${message.type} to ${message.targetModuleName || ''}`);
435
445
  return;
@@ -483,6 +493,11 @@ module.exports = class BrokerBase extends EventEmitter {
483
493
  excludedClients: options.excludedClients || [],
484
494
  })
485
495
  .catch((err) => {
496
+ if (this.errorEmittingEnabled) {
497
+ this.emit('error', err);
498
+ return;
499
+ }
500
+
486
501
  console.error(`Couldn't emit ${fullyQualifiedName}`);
487
502
 
488
503
  if (err.code !== 'TIMEOUT') {
@@ -493,5 +508,6 @@ module.exports = class BrokerBase extends EventEmitter {
493
508
 
494
509
  destroy() {
495
510
  this.removeAllListeners();
511
+ this.destroyed = true;
496
512
  }
497
513
  }
@@ -89,11 +89,14 @@ module.exports = class BrokerClient extends BrokerBase {
89
89
  * @returns {Promise}
90
90
  */
91
91
  getConnectPromise() {
92
- return new Promise((resolve) => {
92
+ return new Promise((resolve, reject) => {
93
93
  const looper = setInterval(() => {
94
94
  if (this.connected) {
95
95
  clearInterval(looper);
96
96
  resolve();
97
+ } else if (this.destroyed) {
98
+ clearInterval(looper);
99
+ reject(new Error('BrokerClient is destroyed'));
97
100
  }
98
101
  }, 0);
99
102
  });
@@ -110,6 +113,7 @@ module.exports = class BrokerClient extends BrokerBase {
110
113
  * @param {object} options Options
111
114
  * @param {string} options.host Hostname or IP of the target server.
112
115
  * @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)
113
117
  */
114
118
  connect(options) {
115
119
  if (this.isDuplicate) return;
@@ -119,6 +123,8 @@ module.exports = class BrokerClient extends BrokerBase {
119
123
 
120
124
  if (!options) {
121
125
  url = new URL(location.href);
126
+ } else if (options.url) {
127
+ url = new URL(options.url);
122
128
  } else {
123
129
  url = { hostname: options.host, port: options.port };
124
130
  }
@@ -210,6 +216,11 @@ module.exports = class BrokerClient extends BrokerBase {
210
216
  try {
211
217
  await this.handleMessage(event.data, this.socket);
212
218
  } catch (ex) {
219
+ if (this.errorEmittingEnabled) {
220
+ this.emit('error', ex);
221
+ return;
222
+ }
223
+
213
224
  console.trace(ex);
214
225
  }
215
226
  }
@@ -249,7 +260,11 @@ module.exports = class BrokerClient extends BrokerBase {
249
260
  try {
250
261
  entry.eventHandler(...message.data);
251
262
  } catch (ex) {
252
- this.logger.warn(ex);
263
+ if (this.errorEmittingEnabled) {
264
+ this.emit('error', ex);
265
+ } else {
266
+ this.logger.warn(ex);
267
+ }
253
268
  } finally {
254
269
  if (entry.once) {
255
270
  this.unsubscribeFromAPIEvent(subscribedEvent, entry.eventHandler);
@@ -399,6 +414,11 @@ module.exports = class BrokerClient extends BrokerBase {
399
414
  }
400
415
  }
401
416
  } catch (ex) {
417
+ if (this.errorEmittingEnabled) {
418
+ this.emit('error', ex);
419
+ return;
420
+ }
421
+
402
422
  if (ex.code === 'TIMEOUT') {
403
423
  console.warn('Message timed out.');
404
424
  console.log(message);
@@ -513,6 +533,11 @@ module.exports = class BrokerClient extends BrokerBase {
513
533
  }
514
534
  }
515
535
  } catch (ex) {
536
+ if (this.errorEmittingEnabled) {
537
+ this.emit('error', ex);
538
+ return;
539
+ }
540
+
516
541
  console.trace(ex.message);
517
542
  }
518
543
  }
@@ -527,7 +552,11 @@ module.exports = class BrokerClient extends BrokerBase {
527
552
  }
528
553
 
529
554
  if (!this.isDuplicate) {
530
- this.logger.trace(err);
555
+ if (this.errorEmittingEnabled) {
556
+ this.emit('error', err);
557
+ } else {
558
+ this.logger.trace(err);
559
+ }
531
560
  }
532
561
 
533
562
  if (!this.isDuplicate) {
@@ -596,6 +625,11 @@ module.exports = class BrokerClient extends BrokerBase {
596
625
 
597
626
  return Promise.all(promises);
598
627
  } catch (ex) {
628
+ if (this.errorEmittingEnabled) {
629
+ this.emit('error', ex);
630
+ return;
631
+ }
632
+
599
633
  console.trace(ex.message);
600
634
  }
601
635
  }
@@ -637,6 +671,11 @@ module.exports = class BrokerClient extends BrokerBase {
637
671
  super.destroy();
638
672
  }
639
673
  } catch (ex) {
674
+ if (this.errorEmittingEnabled) {
675
+ this.emit('error', ex);
676
+ return;
677
+ }
678
+
640
679
  console.trace(ex.message);
641
680
  }
642
681
  }
@@ -673,6 +712,12 @@ module.exports = class BrokerClient extends BrokerBase {
673
712
  return this.registerHandlersToRemote(remote);
674
713
  }
675
714
 
715
+ /**
716
+ * @typedef {function} ErrorCallback
717
+ * @param {Error} error
718
+ * @param {BrokerClient} brokerClient
719
+ /
720
+
676
721
  /**
677
722
  * Third-party modules can use this method to initialize a BrokerClient
678
723
  * and register themselves to RealityHub.
@@ -691,12 +736,20 @@ module.exports = class BrokerClient extends BrokerBase {
691
736
  * @param {{ host: string, port: number }} params.hub RealityHub connection parameters
692
737
  * @param {string} params.hub.host RealityHub hostname or IP address
693
738
  * @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
694
741
  * @returns {Promise<BrokerClient, Error>} A BrokerClient instance.
695
742
  */
696
743
  static async initModule(params) {
697
- const { moduleName, serverURL, hub, webSocketURL = '/core', clientModuleName, menuTitle } = params;
744
+ const { moduleName, serverURL, hub, webSocketURL = '/core', clientModuleName, menuTitle, onError} = params;
698
745
  const hubClient = new BrokerClient({ moduleName, webSocketURL });
699
746
 
747
+ if (typeof onError === 'function') {
748
+ hubClient.on('error', (error) => {
749
+ onError(error, hubClient);
750
+ });
751
+ }
752
+
700
753
  hubClient.connect(hub);
701
754
  await hubClient.getConnectPromise();
702
755
  if (!serverURL) return hubClient;
@@ -1,19 +0,0 @@
1
- name: Publish Package to Github Packages
2
-
3
- on:
4
- workflow_dispatch:
5
-
6
- jobs:
7
- publish-gpr:
8
- name: Publish Job
9
- runs-on: ubuntu-latest
10
- steps:
11
- - uses: actions/checkout@v2
12
- - uses: actions/setup-node@v1
13
- with:
14
- node-version: 14
15
- registry-url: https://npm.pkg.github.com/
16
- - run: npm ci
17
- - run: npm publish
18
- env:
19
- NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}