xcraft-core-busclient 5.0.3 → 5.1.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/index.js CHANGED
@@ -402,6 +402,9 @@ class BusClient extends EventEmitter {
402
402
  let busConfig = this._busConfig;
403
403
  if (!busConfig) {
404
404
  busConfig = xEtc.load('xcraft-core-bus');
405
+ } else if (!busConfig.hasOwnProperty('clientKeepAlive')) {
406
+ const {clientKeepAlive} = xEtc.load('xcraft-core-bus');
407
+ busConfig.clientKeepAlive = clientKeepAlive;
405
408
  }
406
409
 
407
410
  /* The TLS certificate is ignored in case of unix socket use */
@@ -420,6 +423,10 @@ class BusClient extends EventEmitter {
420
423
 
421
424
  const options = {
422
425
  timeout: busConfig.timeout,
426
+ clientKeepAlive:
427
+ typeof busConfig.clientKeepAlive === 'string'
428
+ ? parseInt(busConfig.clientKeepAlive)
429
+ : busConfig.clientKeepAlive,
423
430
  noForwarding: busConfig.noForwarding,
424
431
  };
425
432
  if (busConfig.caPath) {
@@ -448,16 +455,24 @@ class BusClient extends EventEmitter {
448
455
  stop(callback) {
449
456
  xLog.verb(`Stopping for ${this._orcName || 'greathall'}...`);
450
457
 
451
- const unsubscribe = this._subscribeClose((err) => {
452
- unsubscribe();
453
- if (callback) {
454
- callback(err);
455
- }
456
- });
458
+ const isConnected = this._connected;
459
+
460
+ if (isConnected) {
461
+ const unsubscribe = this._subscribeClose((err) => {
462
+ unsubscribe();
463
+ if (callback) {
464
+ callback(err);
465
+ }
466
+ });
467
+ }
457
468
 
458
469
  this._connected = false;
459
470
  this._subSocket.stop();
460
471
  this._pushSocket.stop();
472
+
473
+ if (!isConnected && callback) {
474
+ callback();
475
+ }
461
476
  }
462
477
 
463
478
  /**
@@ -566,13 +581,13 @@ class BusClient extends EventEmitter {
566
581
  }
567
582
  }
568
583
 
569
- exports.newResponse = function (moduleName, orcName, routing) {
584
+ exports.newResponse = function (moduleName, orcName, routing, dataContext) {
570
585
  let self = null;
571
586
  if (this instanceof BusClient) {
572
587
  self = this;
573
588
  }
574
589
 
575
- return new Resp(self, moduleName, orcName, routing);
590
+ return new Resp(self, moduleName, orcName, routing, dataContext);
576
591
  };
577
592
 
578
593
  exports.initGlobal = function () {
package/lib/command.js CHANGED
@@ -38,19 +38,22 @@ Command.prototype.retry = function (msg) {
38
38
  * @param {string} which - Orc name or greathall.
39
39
  * @param {function(err, results)} [finishHandler] - Callback.
40
40
  * @param {Object} options - Options like forceNested.
41
+ * @param {Object} msgContext - Message context.
41
42
  */
42
43
  Command.prototype.send = function (
43
44
  cmd,
44
45
  data,
45
46
  which,
46
47
  finishHandler,
47
- options = {}
48
+ options = {},
49
+ msgContext
48
50
  ) {
49
51
  if (!which) {
50
52
  which = this._busClient.getOrcName() || 'greathall';
51
53
  }
52
54
 
53
55
  let busMessage = data;
56
+
54
57
  if (!data || !data._xcraftMessage) {
55
58
  busMessage = this.newMessage(cmd, which);
56
59
  busMessage.data = data;
@@ -59,6 +62,10 @@ Command.prototype.send = function (
59
62
  this._busClient.patchMessage(busMessage);
60
63
  }
61
64
 
65
+ if (msgContext) {
66
+ busMessage.context = msgContext;
67
+ }
68
+
62
69
  if (!busMessage.arp) {
63
70
  const {appId, appArgs} = require('xcraft-core-host');
64
71
  const {tribe} = appArgs();
package/lib/events.js CHANGED
@@ -153,8 +153,9 @@ Events.prototype.heartbeat = function () {
153
153
  * @param {Object} [data] - Payload.
154
154
  * @param {boolean} [serialize] - Stringify the object.
155
155
  * @param {string} [routing] - Router info (ee or axon).
156
+ * @param {Object} [msgContext] - Message context.
156
157
  */
157
- Events.prototype.send = function (topic, data, serialize, routing) {
158
+ Events.prototype.send = function (topic, data, serialize, routing, msgContext) {
158
159
  if (!this._busClient.isServerSide()) {
159
160
  const err = new Error('only the server can send events');
160
161
  xLog.err(err.stack);
@@ -199,6 +200,10 @@ Events.prototype.send = function (topic, data, serialize, routing) {
199
200
  isActivity = routing.activity;
200
201
  }
201
202
 
203
+ if (msgContext) {
204
+ busMessage.context = msgContext;
205
+ }
206
+
202
207
  /* Reduce noise... */
203
208
  if (topic !== this._prevTopic) {
204
209
  xLog.verb('client send notification(s) on topic:' + topic);
package/lib/resp.js CHANGED
@@ -2,15 +2,25 @@
2
2
 
3
3
  const watt = require('gigawatts');
4
4
 
5
+ const injectDataContext = (data, ctx) => {
6
+ for (const [k, v] of ctx) {
7
+ data[k] = v;
8
+ }
9
+ };
5
10
  class Events {
6
- constructor(busClient, log, orcName, routing) {
11
+ constructor(busClient, log, orcName, routing, msgContext) {
7
12
  this._busClient = busClient;
8
13
  this._orcName = orcName;
9
14
  this._routing = routing;
15
+ this._msgContext = msgContext;
10
16
 
11
17
  this.status = (this._busClient && this._busClient.events.status) || {};
12
18
  }
13
19
 
20
+ set msgContext(value) {
21
+ this._msgContext = value;
22
+ }
23
+
14
24
  get routing() {
15
25
  return this._routing;
16
26
  }
@@ -83,16 +93,22 @@ class Events {
83
93
  this._fixTopic(topic),
84
94
  data,
85
95
  serialize,
86
- routing
96
+ routing,
97
+ this._msgContext
87
98
  );
88
99
  }
89
100
  }
90
101
 
91
102
  class Command {
92
- constructor(busClient, log, orcName) {
103
+ constructor(busClient, log, orcName, msgContext) {
93
104
  this._log = log;
94
105
  this._busClient = busClient;
95
106
  this._orcName = orcName;
107
+ this._msgContext = msgContext;
108
+ }
109
+
110
+ set msgContext(value) {
111
+ this._msgContext = value;
96
112
  }
97
113
 
98
114
  retry(msg) {
@@ -111,7 +127,14 @@ class Command {
111
127
  }
112
128
 
113
129
  const orcName = (data && data.$orcName) || this._orcName;
114
- this._busClient.command.send(cmd, data, orcName, finishHandler, options);
130
+ this._busClient.command.send(
131
+ cmd,
132
+ data,
133
+ orcName,
134
+ finishHandler,
135
+ options,
136
+ this._msgContext
137
+ );
115
138
  }
116
139
 
117
140
  send(cmd, data, finishHandler) {
@@ -124,11 +147,16 @@ class Command {
124
147
  }
125
148
 
126
149
  class Resp {
127
- constructor(busClient, moduleName, orcName, routing = null) {
150
+ constructor(
151
+ busClient,
152
+ moduleName,
153
+ orcName,
154
+ routing = null,
155
+ msgContext = null
156
+ ) {
128
157
  this._busClient = busClient;
129
158
  this._cmdNames = {};
130
159
  this._orcName = orcName;
131
-
132
160
  /* Add a way for sending with the token server instead of an orcName */
133
161
  if (orcName === 'token') {
134
162
  const token = this._busClient.getToken();
@@ -141,12 +169,17 @@ class Resp {
141
169
  }
142
170
 
143
171
  this.log = require('xcraft-core-log')(moduleName, this);
144
- this.events = new Events(busClient, this.log, orcName, routing);
145
- this.command = new Command(busClient, this.log, orcName);
172
+ this.events = new Events(busClient, this.log, orcName, routing, msgContext);
173
+ this.command = new Command(busClient, this.log, orcName, msgContext);
146
174
 
147
175
  watt.wrapAll(this, 'connect', 'stop');
148
176
  }
149
177
 
178
+ set msgContext(value) {
179
+ this.events.msgContext = value;
180
+ this.command.msgContext = value;
181
+ }
182
+
150
183
  _makeCmdNames() {
151
184
  if (!this._busClient) {
152
185
  return {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xcraft-core-busclient",
3
- "version": "5.0.3",
3
+ "version": "5.1.1",
4
4
  "description": "Xcraft bus client",
5
5
  "main": "index.js",
6
6
  "engines": {