xcraft-core-busclient 5.5.1 → 5.7.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.
package/index.js CHANGED
@@ -9,6 +9,8 @@ const {
9
9
  } = require('xcraft-core-transport');
10
10
  const {v4: uuidV4} = require('uuid');
11
11
 
12
+ const fse = require('fs-extra');
13
+ const path = require('node:path');
12
14
  const xLog = require('xcraft-core-log')(moduleName, null);
13
15
  const xUtils = require('xcraft-core-utils');
14
16
 
@@ -18,6 +20,8 @@ const Resp = require('./lib/resp.js');
18
20
  let globalBusClient = null;
19
21
 
20
22
  class BusClient extends EventEmitter {
23
+ #lastErrorReason = null;
24
+
21
25
  constructor(busConfig, subscriptions) {
22
26
  super();
23
27
 
@@ -59,6 +63,10 @@ class BusClient extends EventEmitter {
59
63
  this._onConnectSubscribers = {};
60
64
 
61
65
  const onClosed = (err) => {
66
+ if (err && err?.code !== 'Z_BUF_ERROR') {
67
+ this.#lastErrorReason = err.code || err.message || err;
68
+ }
69
+
62
70
  if (!this._subClosed || !this._pushClosed) {
63
71
  return;
64
72
  }
@@ -283,6 +291,10 @@ class BusClient extends EventEmitter {
283
291
  });
284
292
  }
285
293
 
294
+ get lastErrorReason() {
295
+ return this.#lastErrorReason;
296
+ }
297
+
286
298
  _updateCommandsRegistry(registry, token) {
287
299
  this._commandsRegistry = registry;
288
300
  this._commandsRegistryTime = new Date().toISOString();
@@ -369,6 +381,21 @@ class BusClient extends EventEmitter {
369
381
  return this._onConnectSubscribers[key].unsubscribe;
370
382
  }
371
383
 
384
+ #tryToLoadClientKeys(xHost, hordeId) {
385
+ const keyPath = path.join(
386
+ xHost.realmsStorePath,
387
+ `${hordeId}@${xHost.variantId}-key.pem`
388
+ );
389
+ const certPath = path.join(
390
+ xHost.realmsStorePath,
391
+ `${hordeId}@${xHost.variantId}-cert.pem`
392
+ );
393
+
394
+ return fse.existsSync(keyPath) && fse.existsSync(certPath)
395
+ ? {keyPath, certPath}
396
+ : null;
397
+ }
398
+
372
399
  /**
373
400
  * Connect the client to the buses.
374
401
  *
@@ -446,14 +473,39 @@ class BusClient extends EventEmitter {
446
473
  : busConfig.clientKeepAlive,
447
474
  noForwarding: busConfig.noForwarding,
448
475
  };
449
- if (busConfig.caPath) {
450
- if (!path.isAbsolute(busConfig.caPath)) {
451
- options.caPath = path.join(resourcesPath, busConfig.caPath);
476
+
477
+ if (
478
+ busConfig.gatekeeper &&
479
+ busConfig.hordeId &&
480
+ !busConfig.keyPath &&
481
+ !busConfig.certPath
482
+ ) {
483
+ const keys = this.#tryToLoadClientKeys(xHost, busConfig.hordeId);
484
+ if (keys) {
485
+ const {keyPath, certPath} = keys;
486
+ busConfig.keyPath = keyPath;
487
+ busConfig.certPath = certPath;
488
+ xEtc.saveRun('xcraft-core-busclient', busConfig);
452
489
  } else {
453
- options.caPath = busConfig.caPath;
490
+ xLog.err(
491
+ `Missing client certificate for ${busConfig.hordeId}@${xHost.variantId}`
492
+ );
454
493
  }
455
494
  }
456
495
 
496
+ ['caPath', 'keyPath', 'certPath']
497
+ .filter((key) => busConfig[key])
498
+ .forEach((key) => {
499
+ if (
500
+ !busConfig[key].startsWith('base64:') &&
501
+ !path.isAbsolute(busConfig[key])
502
+ ) {
503
+ options[key] = path.join(resourcesPath, busConfig[key]);
504
+ } else {
505
+ options[key] = busConfig[key];
506
+ }
507
+ });
508
+
457
509
  this._subSocket.connect(backend, {
458
510
  port: parseInt(busConfig.notifierPort),
459
511
  host: busConfig.host,
package/lib/command.js CHANGED
@@ -18,12 +18,13 @@ class Command {
18
18
  this._routingKey = tribe ? `${appId}-${tribe}` : appId;
19
19
 
20
20
  this._push
21
- .on('close', (err) => this.#onNetworkError(err))
22
- .on('error', (err) => this.#onNetworkError(err))
23
- .on('reconnect attempt', (err) => this.#onNetworkError(err));
21
+ .on('close', (err) => this.#onNetworkChange(err))
22
+ .on('error', (err) => this.#onNetworkChange(err))
23
+ .on('connect', (err) => this.#onNetworkChange(err))
24
+ .on('reconnect attempt', (err) => this.#onNetworkChange(err));
24
25
  }
25
26
 
26
- #onNetworkError(err) {
27
+ #onNetworkChange(err) {
27
28
  for (const {cmd, unsub, callback} of Command.#unsubList.values()) {
28
29
  unsub();
29
30
  if (callback) {
package/lib/events.js CHANGED
@@ -156,6 +156,7 @@ class Events {
156
156
  * @param {boolean} [serialize] - Stringify the object.
157
157
  * @param {string} [routing] - Router info (ee or axon).
158
158
  * @param {object} [msgContext] - Message context.
159
+ * @returns {boolean} true if sent
159
160
  */
160
161
  send(topic, data, serialize, routing, msgContext) {
161
162
  if (!this._busClient.isServerSide()) {
@@ -211,13 +212,15 @@ class Events {
211
212
  this._prevTopic = topic;
212
213
  }
213
214
 
214
- notifier.send(topic, busMessage);
215
+ const sent = notifier.send(topic, busMessage);
215
216
 
216
217
  if (isActivity) {
217
218
  topic += '.activity';
218
219
  busMessage = this._busClient.newMessage(topic, which);
219
220
  notifier.send(topic, busMessage);
220
221
  }
222
+
223
+ return sent;
221
224
  }
222
225
 
223
226
  status = {
package/lib/resp.js CHANGED
@@ -86,7 +86,7 @@ class Events {
86
86
  }
87
87
  }
88
88
 
89
- this._busClient.events.send(
89
+ return this._busClient.events.send(
90
90
  this._fixTopic(topic),
91
91
  data,
92
92
  serialize,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xcraft-core-busclient",
3
- "version": "5.5.1",
3
+ "version": "5.7.0",
4
4
  "description": "Xcraft bus client",
5
5
  "main": "index.js",
6
6
  "engines": {
@@ -19,6 +19,7 @@
19
19
  "url": "git+https://github.com/Xcraft-Inc/xcraft-core-busclient.git"
20
20
  },
21
21
  "dependencies": {
22
+ "fs-extra": "^9.1.0",
22
23
  "gigawatts": "^4.0.1",
23
24
  "uuid": "^8.3.2",
24
25
  "xcraft-core-bus": "^5.0.0",