@zerodensity/realityhub-api 2.0.0 → 2.0.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/.github/workflows/npm-publish-next.yml +10 -2
- package/.github/workflows/npm-publish.yml +10 -2
- package/.prettierrc.json +18 -0
- package/README.md +72 -61
- package/dist/index.cjs +2 -0
- package/dist/index.cjs.map +1 -0
- package/package.json +22 -5
- package/rollup.config.js +15 -0
- package/src/index.d.ts +115 -0
- package/src/index.js +7 -5
- package/src/lib/BrokerBase.js +180 -176
- package/src/lib/BrokerClient.js +70 -80
- package/src/lib/BrokerError.js +2 -2
- package/src/lib/RawRequest.js +2 -2
- package/src/lib/consoleLogger.js +5 -5
- package/src/lib/onceMultiple.js +2 -2
- package/.claude/settings.local.json +0 -10
- package/src/index.cjs +0 -17
package/src/lib/BrokerClient.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
// This file is part of realityhub-api.
|
|
4
4
|
//
|
|
5
5
|
// realityhub-api is free software: you can redistribute it and/or modify
|
|
6
|
-
// it under the terms of the GNU General Public License version 2, as published by
|
|
6
|
+
// it under the terms of the GNU General Public License version 2, as published by
|
|
7
7
|
// the Free Software Foundation.
|
|
8
8
|
//
|
|
9
9
|
// realityhub-api is distributed in the hope that it will be useful,
|
|
@@ -15,13 +15,16 @@
|
|
|
15
15
|
// along with realityhub-api. If not, see <https://www.gnu.org/licenses/>.
|
|
16
16
|
|
|
17
17
|
import { v4 as uuid } from 'uuid';
|
|
18
|
-
import
|
|
19
|
-
import
|
|
20
|
-
import
|
|
21
|
-
import
|
|
22
|
-
import
|
|
18
|
+
import BrokerBase from './BrokerBase.js';
|
|
19
|
+
import BrokerError from './BrokerError.js';
|
|
20
|
+
import RawRequest from './RawRequest.js';
|
|
21
|
+
import consoleLogger from './consoleLogger.js';
|
|
22
|
+
import onceMultiple from './onceMultiple.js';
|
|
23
|
+
import WebSocket from 'ws';
|
|
23
24
|
|
|
24
|
-
|
|
25
|
+
const WS = typeof window !== 'undefined' ? window.WebSocket : WebSocket;
|
|
26
|
+
|
|
27
|
+
export default class BrokerClient extends BrokerBase {
|
|
25
28
|
/**
|
|
26
29
|
* BrokerClient constructor
|
|
27
30
|
* @param {object} params Parameters
|
|
@@ -37,13 +40,7 @@ export class BrokerClient extends BrokerBase {
|
|
|
37
40
|
|
|
38
41
|
this.setMaxListeners(20);
|
|
39
42
|
|
|
40
|
-
for (const func of [
|
|
41
|
-
this.onOpen,
|
|
42
|
-
this.onClose,
|
|
43
|
-
this.onError,
|
|
44
|
-
this.connect,
|
|
45
|
-
this.onSocketMessage,
|
|
46
|
-
]) {
|
|
43
|
+
for (const func of [this.onOpen, this.onClose, this.onError, this.connect, this.onSocketMessage]) {
|
|
47
44
|
const name = func.name;
|
|
48
45
|
this[name] = func.bind(this);
|
|
49
46
|
}
|
|
@@ -116,7 +113,7 @@ export class BrokerClient extends BrokerBase {
|
|
|
116
113
|
url = { hostname: options.host, port: options.port };
|
|
117
114
|
}
|
|
118
115
|
|
|
119
|
-
const scheme =
|
|
116
|
+
const scheme = url.protocol === 'https:' || this.ssl ? 'wss' : 'ws';
|
|
120
117
|
|
|
121
118
|
const webSocketURL = url.port
|
|
122
119
|
? `${scheme}://${url.hostname}:${url.port}${this.webSocketURL}`
|
|
@@ -124,22 +121,22 @@ export class BrokerClient extends BrokerBase {
|
|
|
124
121
|
|
|
125
122
|
this.logger.info(`BrokerClient is connecting to ${webSocketURL}`);
|
|
126
123
|
|
|
127
|
-
if (typeof process !==
|
|
124
|
+
if (typeof process !== 'undefined' && process.versions != null && process.versions.node != null) {
|
|
128
125
|
const NODE_TLS_REJECT_UNAUTHORIZED = process.env.NODE_TLS_REJECT_UNAUTHORIZED;
|
|
129
126
|
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0;
|
|
130
|
-
this.socket = new
|
|
127
|
+
this.socket = new WS(`${webSocketURL}?module=true`);
|
|
131
128
|
process.env.NODE_TLS_REJECT_UNAUTHORIZED = NODE_TLS_REJECT_UNAUTHORIZED;
|
|
132
129
|
} else {
|
|
133
|
-
this.socket = new
|
|
130
|
+
this.socket = new WS(webSocketURL);
|
|
134
131
|
}
|
|
135
|
-
|
|
132
|
+
|
|
136
133
|
this.addSocketListeners();
|
|
137
134
|
}
|
|
138
135
|
|
|
139
136
|
/**
|
|
140
137
|
* Duplicates a BrokerClient in order to share the same WebSocket.
|
|
141
138
|
* @param {object} params Parameters
|
|
142
|
-
* @param {string} params.moduleName Module name of the duplicate BrokerClient (a duplicate
|
|
139
|
+
* @param {string} params.moduleName Module name of the duplicate BrokerClient (a duplicate
|
|
143
140
|
* can have a different name than its parent)
|
|
144
141
|
* @returns {BrokerClient}
|
|
145
142
|
*/
|
|
@@ -171,12 +168,10 @@ export class BrokerClient extends BrokerBase {
|
|
|
171
168
|
duplicate.on('destroy', () => duplicates.delete(duplicate));
|
|
172
169
|
duplicates.add(duplicate);
|
|
173
170
|
|
|
174
|
-
duplicate
|
|
175
|
-
.ping
|
|
176
|
-
.
|
|
177
|
-
|
|
178
|
-
console.trace(ex.message);
|
|
179
|
-
});
|
|
171
|
+
duplicate.ping().catch((ex) => {
|
|
172
|
+
console.error(`Failed to send ping`);
|
|
173
|
+
console.trace(ex.message);
|
|
174
|
+
});
|
|
180
175
|
|
|
181
176
|
return duplicate;
|
|
182
177
|
}
|
|
@@ -291,21 +286,20 @@ export class BrokerClient extends BrokerBase {
|
|
|
291
286
|
}
|
|
292
287
|
}
|
|
293
288
|
|
|
294
|
-
await this.sendResponse(
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
);
|
|
289
|
+
await this.sendResponse(socket, message, false, [
|
|
290
|
+
{
|
|
291
|
+
error: `${message.eventName} sent to ${this.moduleName}. This is probably a mistake.`,
|
|
292
|
+
},
|
|
293
|
+
]);
|
|
300
294
|
}
|
|
301
295
|
|
|
302
296
|
break;
|
|
303
297
|
}
|
|
304
298
|
|
|
305
299
|
case 'unsubscribe': {
|
|
306
|
-
const [vendor, moduleName, ...rest] = message.eventName.split(
|
|
307
|
-
const eventName = rest.join(
|
|
308
|
-
const targetModuleName = [vendor, moduleName].join(
|
|
300
|
+
const [vendor, moduleName, ...rest] = message.eventName.split('.');
|
|
301
|
+
const eventName = rest.join('.');
|
|
302
|
+
const targetModuleName = [vendor, moduleName].join('.');
|
|
309
303
|
|
|
310
304
|
if (this.moduleName === targetModuleName) {
|
|
311
305
|
this.emit('unsubscribe', { eventName });
|
|
@@ -321,12 +315,11 @@ export class BrokerClient extends BrokerBase {
|
|
|
321
315
|
}
|
|
322
316
|
}
|
|
323
317
|
|
|
324
|
-
await this.sendResponse(
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
);
|
|
318
|
+
await this.sendResponse(socket, message, false, [
|
|
319
|
+
{
|
|
320
|
+
error: `${message.eventName} sent to ${this.moduleName}. This is probably a mistake.`,
|
|
321
|
+
},
|
|
322
|
+
]);
|
|
330
323
|
}
|
|
331
324
|
|
|
332
325
|
break;
|
|
@@ -361,12 +354,11 @@ export class BrokerClient extends BrokerBase {
|
|
|
361
354
|
|
|
362
355
|
default: {
|
|
363
356
|
if (!this.apiHandlers.has(message.type)) {
|
|
364
|
-
await this.sendResponse(
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
);
|
|
357
|
+
await this.sendResponse(socket, message, false, [
|
|
358
|
+
{
|
|
359
|
+
error: `There is no handler registered for this type of message: ${message.type}`,
|
|
360
|
+
},
|
|
361
|
+
]);
|
|
370
362
|
return;
|
|
371
363
|
}
|
|
372
364
|
|
|
@@ -415,23 +407,19 @@ export class BrokerClient extends BrokerBase {
|
|
|
415
407
|
/**
|
|
416
408
|
* Sends a message.
|
|
417
409
|
* @async
|
|
418
|
-
* @param {object} message
|
|
410
|
+
* @param {object} message
|
|
419
411
|
* @returns {Promise.<Array, Error>}
|
|
420
412
|
*/
|
|
421
413
|
async sendMessage(message) {
|
|
422
414
|
const id = uuid();
|
|
423
415
|
const socket = this.isDuplicate ? this.parent.getSocket() : this.socket;
|
|
424
|
-
const webSocketMessage = Object.assign(
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
},
|
|
432
|
-
);
|
|
433
|
-
|
|
434
|
-
if (socket.readyState !== WebSocket.OPEN) {
|
|
416
|
+
const webSocketMessage = Object.assign({}, message, {
|
|
417
|
+
id,
|
|
418
|
+
time: new Date().valueOf(),
|
|
419
|
+
moduleName: this.moduleName,
|
|
420
|
+
});
|
|
421
|
+
|
|
422
|
+
if (socket.readyState !== WS.OPEN) {
|
|
435
423
|
try {
|
|
436
424
|
await onceMultiple(this, ['connect'], webSocketMessage.timeout || this.messageTimeout);
|
|
437
425
|
} catch (ex) {
|
|
@@ -478,18 +466,13 @@ export class BrokerClient extends BrokerBase {
|
|
|
478
466
|
*/
|
|
479
467
|
resubscribeModuleEvents() {
|
|
480
468
|
for (const eventName of this.events.keys()) {
|
|
481
|
-
const moduleName = eventName
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
type: 'subscribe',
|
|
489
|
-
eventName,
|
|
490
|
-
targetModuleName: moduleName,
|
|
491
|
-
})
|
|
492
|
-
.catch(new Function());
|
|
469
|
+
const moduleName = eventName.split('.').slice(0, 2).join('.');
|
|
470
|
+
|
|
471
|
+
this.sendMessage({
|
|
472
|
+
type: 'subscribe',
|
|
473
|
+
eventName,
|
|
474
|
+
targetModuleName: moduleName,
|
|
475
|
+
}).catch(new Function());
|
|
493
476
|
}
|
|
494
477
|
}
|
|
495
478
|
|
|
@@ -522,7 +505,7 @@ export class BrokerClient extends BrokerBase {
|
|
|
522
505
|
|
|
523
506
|
/**
|
|
524
507
|
* @private
|
|
525
|
-
* @param {ErrorEvent} err
|
|
508
|
+
* @param {ErrorEvent} err
|
|
526
509
|
*/
|
|
527
510
|
onError(err) {
|
|
528
511
|
if (err.error instanceof Error) {
|
|
@@ -556,6 +539,8 @@ export class BrokerClient extends BrokerBase {
|
|
|
556
539
|
duplicate.onClose();
|
|
557
540
|
}
|
|
558
541
|
}
|
|
542
|
+
} else {
|
|
543
|
+
this.emit('reconnectfailure');
|
|
559
544
|
}
|
|
560
545
|
|
|
561
546
|
setTimeout(() => this.connect(this.connectOptions), 1000);
|
|
@@ -644,13 +629,13 @@ export class BrokerClient extends BrokerBase {
|
|
|
644
629
|
}
|
|
645
630
|
}
|
|
646
631
|
|
|
647
|
-
/**
|
|
632
|
+
/**
|
|
648
633
|
* Register a module's API handlers to RealityHub
|
|
649
634
|
* @async
|
|
650
|
-
* @param {Object.<string, function>} handlers Key will be registered to the API tree.
|
|
635
|
+
* @param {Object.<string, function>} handlers Key will be registered to the API tree.
|
|
651
636
|
* @param {*} [context=null] Handlers' `this` will be set to this context.
|
|
652
|
-
* The value (function) will handle the API calls.
|
|
653
|
-
* @param {string} [remote='hub.core'] Remote
|
|
637
|
+
* The value (function) will handle the API calls.
|
|
638
|
+
* @param {string} [remote='hub.core'] Remote
|
|
654
639
|
* @example
|
|
655
640
|
* // server.js
|
|
656
641
|
* brokerClient.registerAPIHandlers(this, {
|
|
@@ -658,7 +643,7 @@ export class BrokerClient extends BrokerBase {
|
|
|
658
643
|
* return number1 + number2;
|
|
659
644
|
* },
|
|
660
645
|
* }).catch((ex) => console.trace(ex));
|
|
661
|
-
*
|
|
646
|
+
*
|
|
662
647
|
* // client.js
|
|
663
648
|
* brokerClient.api.moduleVendor.moduleName.addNumber(3, 5)
|
|
664
649
|
* .then((result) => {
|
|
@@ -689,8 +674,8 @@ export class BrokerClient extends BrokerBase {
|
|
|
689
674
|
* RealityHub will look for an `index.js` file in this path. This script file will be imported
|
|
690
675
|
* by RealityHub's `index.html` via a `<script type="module">` tag. Relative paths in your scripts
|
|
691
676
|
* will be proxied by RealityHub.
|
|
692
|
-
* @param {string} [params.webSocketURL="/core"] WebSocket URL to connect. RealityHub's API Server
|
|
693
|
-
* is serving at `/core` by default. *(Default: /core)*
|
|
677
|
+
* @param {string} [params.webSocketURL="/core"] WebSocket URL to connect. RealityHub's API Server
|
|
678
|
+
* is serving at `/core` by default. *(Default: /core)*
|
|
694
679
|
* @param {{ host: string, port: number }} params.hub RealityHub connection parameters
|
|
695
680
|
* @param {string} params.hub.host RealityHub hostname or IP address
|
|
696
681
|
* @param {string} params.hub.port RealityHub port
|
|
@@ -704,7 +689,12 @@ export class BrokerClient extends BrokerBase {
|
|
|
704
689
|
await hubClient.getConnectPromise();
|
|
705
690
|
if (!serverURL) return hubClient;
|
|
706
691
|
|
|
707
|
-
await hubClient.api.hub.core.registerProxyURL({
|
|
692
|
+
await hubClient.api.hub.core.registerProxyURL({
|
|
693
|
+
moduleName,
|
|
694
|
+
serverURL,
|
|
695
|
+
clientModuleName,
|
|
696
|
+
menuTitle,
|
|
697
|
+
});
|
|
708
698
|
return hubClient;
|
|
709
699
|
}
|
|
710
700
|
}
|
package/src/lib/BrokerError.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
// This file is part of realityhub-api.
|
|
4
4
|
//
|
|
5
5
|
// realityhub-api is free software: you can redistribute it and/or modify
|
|
6
|
-
// it under the terms of the GNU General Public License version 2, as published by
|
|
6
|
+
// it under the terms of the GNU General Public License version 2, as published by
|
|
7
7
|
// the Free Software Foundation.
|
|
8
8
|
//
|
|
9
9
|
// realityhub-api is distributed in the hope that it will be useful,
|
|
@@ -14,4 +14,4 @@
|
|
|
14
14
|
// You should have received a copy of the GNU General Public License
|
|
15
15
|
// along with realityhub-api. If not, see <https://www.gnu.org/licenses/>.
|
|
16
16
|
|
|
17
|
-
export class BrokerError extends Error {
|
|
17
|
+
export default class BrokerError extends Error {}
|
package/src/lib/RawRequest.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
// This file is part of realityhub-api.
|
|
4
4
|
//
|
|
5
5
|
// realityhub-api is free software: you can redistribute it and/or modify
|
|
6
|
-
// it under the terms of the GNU General Public License version 2, as published by
|
|
6
|
+
// it under the terms of the GNU General Public License version 2, as published by
|
|
7
7
|
// the Free Software Foundation.
|
|
8
8
|
//
|
|
9
9
|
// realityhub-api is distributed in the hope that it will be useful,
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
// You should have received a copy of the GNU General Public License
|
|
15
15
|
// along with realityhub-api. If not, see <https://www.gnu.org/licenses/>.
|
|
16
16
|
|
|
17
|
-
export class RawRequest {
|
|
17
|
+
export default class RawRequest {
|
|
18
18
|
setCallback(callback) {
|
|
19
19
|
if (typeof callback !== 'function') {
|
|
20
20
|
throw new Error('callback must be a function.');
|
package/src/lib/consoleLogger.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
// This file is part of realityhub-api.
|
|
4
4
|
//
|
|
5
5
|
// realityhub-api is free software: you can redistribute it and/or modify
|
|
6
|
-
// it under the terms of the GNU General Public License version 2, as published by
|
|
6
|
+
// it under the terms of the GNU General Public License version 2, as published by
|
|
7
7
|
// the Free Software Foundation.
|
|
8
8
|
//
|
|
9
9
|
// realityhub-api is distributed in the hope that it will be useful,
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
// You should have received a copy of the GNU General Public License
|
|
15
15
|
// along with realityhub-api. If not, see <https://www.gnu.org/licenses/>.
|
|
16
16
|
|
|
17
|
-
export function consoleLogger(moduleName, options = { silent: false }) {
|
|
17
|
+
export default function consoleLogger(moduleName, options = { silent: false }) {
|
|
18
18
|
const { silent } = options;
|
|
19
19
|
|
|
20
20
|
return ['log', 'info', 'warn', 'error', 'trace', 'debug'].reduce((o, item) => {
|
|
@@ -27,13 +27,13 @@ export function consoleLogger(moduleName, options = { silent: false }) {
|
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
if (['trace', 'debug'].includes(item)) {
|
|
30
|
-
for (const arg of
|
|
30
|
+
for (const arg of args || []) {
|
|
31
31
|
console.log(arg);
|
|
32
32
|
}
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
return console[item](...args);
|
|
36
|
-
}
|
|
36
|
+
};
|
|
37
37
|
return o;
|
|
38
38
|
}, {});
|
|
39
|
-
}
|
|
39
|
+
}
|
package/src/lib/onceMultiple.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
// This file is part of realityhub-api.
|
|
4
4
|
//
|
|
5
5
|
// realityhub-api is free software: you can redistribute it and/or modify
|
|
6
|
-
// it under the terms of the GNU General Public License version 2, as published by
|
|
6
|
+
// it under the terms of the GNU General Public License version 2, as published by
|
|
7
7
|
// the Free Software Foundation.
|
|
8
8
|
//
|
|
9
9
|
// realityhub-api is distributed in the hope that it will be useful,
|
|
@@ -22,7 +22,7 @@ class TimeoutError extends Error {
|
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
-
export function onceMultiple(target, eventNames, timeout = null) {
|
|
25
|
+
export default function onceMultiple(target, eventNames, timeout = null) {
|
|
26
26
|
return new Promise((resolve, reject) => {
|
|
27
27
|
let timer;
|
|
28
28
|
let handler;
|
package/src/index.cjs
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
// Copyright (c) 2019-2025 Zero Density Inc.
|
|
2
|
-
//
|
|
3
|
-
// This file is part of realityhub-api.
|
|
4
|
-
//
|
|
5
|
-
// realityhub-api is free software: you can redistribute it and/or modify
|
|
6
|
-
// it under the terms of the GNU General Public License version 2, as published by
|
|
7
|
-
// the Free Software Foundation.
|
|
8
|
-
//
|
|
9
|
-
// realityhub-api is distributed in the hope that it will be useful,
|
|
10
|
-
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11
|
-
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12
|
-
// GNU General Public License for more details.
|
|
13
|
-
//
|
|
14
|
-
// You should have received a copy of the GNU General Public License
|
|
15
|
-
// along with realityhub-api. If not, see <https://www.gnu.org/licenses/>.
|
|
16
|
-
|
|
17
|
-
module.exports = require('./index.js');
|