vrpc 3.0.0-alpha.1 → 3.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/vrpc/VrpcAgent.js CHANGED
@@ -169,7 +169,7 @@ class VrpcAgent extends EventEmitter {
169
169
  .createHash('md5')
170
170
  .update(this._domain + this._agent)
171
171
  .digest('hex')
172
- .substr(0, 18)
172
+ .substr(0, 20)
173
173
  let username = this._username
174
174
  let password = this._password
175
175
  if (this._token) {
@@ -181,7 +181,7 @@ class VrpcAgent extends EventEmitter {
181
181
  password,
182
182
  keepalive: 30,
183
183
  connectTimeout: 10 * 1000,
184
- clientId: `vrpca${md5}`,
184
+ clientId: `va3${md5}`,
185
185
  rejectUnauthorized: false,
186
186
  will: {
187
187
  topic: `${this._baseTopic}/__agentInfo__`,
@@ -346,23 +346,23 @@ class VrpcAgent extends EventEmitter {
346
346
  }
347
347
 
348
348
  _getClasses () {
349
- return VrpcAdapter.getAvailableClasses()
349
+ return VrpcAdapter._getClassesArray()
350
350
  }
351
351
 
352
352
  _getInstances (className) {
353
- return VrpcAdapter.getAvailableInstances(className)
353
+ return VrpcAdapter._getInstancesArray(className)
354
354
  }
355
355
 
356
356
  _getMemberFunctions (className) {
357
- return VrpcAdapter.getAvailableMemberFunctions(className)
357
+ return VrpcAdapter._getMemberFunctionsArray(className)
358
358
  }
359
359
 
360
360
  _getStaticFunctions (className) {
361
- return VrpcAdapter.getAvailableStaticFunctions(className)
361
+ return VrpcAdapter._getStaticFunctionsArray(className)
362
362
  }
363
363
 
364
364
  _getMetaData (className) {
365
- return VrpcAdapter.getAvailableMetaData(className)
365
+ return VrpcAdapter._getMetaData(className)
366
366
  }
367
367
 
368
368
  async _ensureConnected () {
@@ -376,14 +376,18 @@ class VrpcAgent extends EventEmitter {
376
376
  }
377
377
 
378
378
  _handleVrpcCallback (json) {
379
- const { s } = json
379
+ const { s, i, r, e, a } = json
380
+ const topic = i.startsWith('__e__') ? i.slice(5) : s
380
381
  try {
381
- this._log.debug(`Forwarding callback to: ${s} with payload:`, json)
382
- this._mqttPublish(s, VrpcAgent._stringifySafely(json))
382
+ this._log.debug(`Forwarding callback to: ${topic} with payload:`, json)
383
+ this._mqttPublish(
384
+ topic,
385
+ VrpcAgent._stringifySafely({ a, r, e, i, v: VRPC_PROTOCOL_VERSION })
386
+ )
383
387
  } catch (err) {
384
388
  this._log.warn(
385
389
  err,
386
- `Problem publishing vrpc callback to ${sender} because of: ${err.message}`
390
+ `Problem publishing vrpc callback to ${topic} because of: ${err.message}`
387
391
  )
388
392
  }
389
393
  }
@@ -516,7 +520,11 @@ class VrpcAgent extends EventEmitter {
516
520
  json.f = method
517
521
 
518
522
  // Mutates json and adds return value
519
- VrpcAdapter._call(json)
523
+ const mustTrack = VrpcAdapter._call(json)
524
+
525
+ if (mustTrack) {
526
+ this._mqttSubscribe(`${json.s}/__clientInfo__`)
527
+ }
520
528
 
521
529
  // Intersecting life-cycle functions
522
530
  switch (method) {
@@ -545,7 +553,9 @@ class VrpcAgent extends EventEmitter {
545
553
  break
546
554
  }
547
555
  }
548
- this._mqttPublish(json.s, VrpcAgent._stringifySafely(json))
556
+ const { a, r, e, i, v } = json
557
+ const res = e ? { a, r, e, i, v } : { a, r, i, v }
558
+ this._mqttPublish(json.s, VrpcAgent._stringifySafely(res))
549
559
  } catch (err) {
550
560
  this._log.error(
551
561
  err,
@@ -558,19 +568,17 @@ class VrpcAgent extends EventEmitter {
558
568
  // Client went offline
559
569
  const clientId = topic.slice(0, -15) // /__clientInfo__ = 15
560
570
  if (json.status === 'offline') {
571
+ VrpcAdapter._unregisterClient(clientId)
561
572
  const entry = this._isolatedInstances.get(clientId)
562
573
  if (entry) {
563
- // anonymous
564
574
  entry.forEach(instanceId => {
565
575
  const json = { f: '__delete__', a: [instanceId], r: null }
566
- // TODO Do not mutate!
567
576
  VrpcAdapter._call(json)
568
577
  if (json.r) {
569
578
  this._log.debug(`Auto-deleted isolated instance: ${instanceId}`)
570
579
  }
571
580
  })
572
581
  }
573
- VrpcAdapter._unregisterEventListeners(clientId)
574
582
  this._mqttUnsubscribe(`${clientId}/__clientInfo__`)
575
583
  this.emit('clientGone', clientId)
576
584
  }
@@ -742,8 +750,7 @@ class VrpcAgent extends EventEmitter {
742
750
  /**
743
751
  * Event 'clientGone'
744
752
  *
745
- * Emitted when a VRPC client exited that before produced state (e.g. created
746
- * an anonymous or named instance).
753
+ * Emitted when a tracked VRPC client exited.
747
754
  *
748
755
  * @event VrpcAgent#clientGone
749
756
  * @type {String} clientId