vrpc 3.0.0-alpha.3 → 3.0.3

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
@@ -48,7 +48,7 @@ class VrpcAgent extends EventEmitter {
48
48
  })
49
49
  parser.add_argument('-d', '--domain', {
50
50
  help: 'Domain name',
51
- default: domain || 'public.vrpc'
51
+ default: domain || 'vrpc'
52
52
  })
53
53
  parser.add_argument('-t', '--token', {
54
54
  help: 'access token',
@@ -92,10 +92,10 @@ class VrpcAgent extends EventEmitter {
92
92
  *
93
93
  * @constructor
94
94
  * @param {Object} obj
95
- * @param {String} [obj.username] MQTT username (if no token is used)
95
+ * @param {String} [obj.username='<user>-<pathId>@<hostname>-<platform>-js'] MQTT username
96
96
  * @param {String} [obj.password] MQTT password (if no token is provided)
97
- * @param {String} [obj.token] Access token as generated by: https://app.vrpc.io (only optional when using default domain and broker)
98
- * @param {String} [obj.domain='public.vrpc'] The domain under which the agent-provided code is reachable
97
+ * @param {String} [obj.token] Access token
98
+ * @param {String} [obj.domain='vrpc'] The domain under which the agent-provided code is reachable
99
99
  * @param {String} [obj.agent='<user>-<pathId>@<hostname>-<platform>-js'] This agent's name
100
100
  * @param {String} [obj.broker='mqtts://vrpc.io:8883'] Broker url in form: `<scheme>://<host>:<port>`
101
101
  * @param {Object} [obj.log=console] Log object (must support debug, info, warn, and error level)
@@ -104,15 +104,15 @@ class VrpcAgent extends EventEmitter {
104
104
  *
105
105
  * @example
106
106
  * const agent = new Agent({
107
- * domain: 'public.vrpc'
107
+ * domain: 'vrpc'
108
108
  * agent: 'myAgent'
109
109
  * })
110
110
  */
111
111
  constructor ({
112
- username,
113
- password,
114
112
  token,
115
- domain = 'public.vrpc',
113
+ password,
114
+ username = VrpcAgent._generateAgentName(),
115
+ domain = 'vrpc',
116
116
  agent = VrpcAgent._generateAgentName(),
117
117
  broker = 'mqtts://vrpc.io:8883',
118
118
  log = 'console',
@@ -173,7 +173,7 @@ class VrpcAgent extends EventEmitter {
173
173
  let username = this._username
174
174
  let password = this._password
175
175
  if (this._token) {
176
- username = '__token__'
176
+ username = username || this._agent
177
177
  password = this._token
178
178
  }
179
179
  this._options = {
@@ -244,13 +244,17 @@ class VrpcAgent extends EventEmitter {
244
244
  }
245
245
  }
246
246
 
247
+ static _generateUserName () {
248
+ return `${os.userInfo().username}@${os.hostname()}-${os.platform()}-js`
249
+ }
250
+
247
251
  static _generateAgentName () {
248
252
  const { username } = os.userInfo()
249
253
  const pathId = crypto
250
254
  .createHash('md5')
251
255
  .update(path.resolve())
252
256
  .digest('hex')
253
- .substr(0, 4)
257
+ .substring(0, 4)
254
258
  return `${username}-${pathId}@${os.hostname()}-${os.platform()}-js`
255
259
  }
256
260
 
@@ -346,23 +350,23 @@ class VrpcAgent extends EventEmitter {
346
350
  }
347
351
 
348
352
  _getClasses () {
349
- return VrpcAdapter.getAvailableClasses()
353
+ return VrpcAdapter._getClassesArray()
350
354
  }
351
355
 
352
356
  _getInstances (className) {
353
- return VrpcAdapter.getAvailableInstances(className)
357
+ return VrpcAdapter._getInstancesArray(className)
354
358
  }
355
359
 
356
360
  _getMemberFunctions (className) {
357
- return VrpcAdapter.getAvailableMemberFunctions(className)
361
+ return VrpcAdapter._getMemberFunctionsArray(className)
358
362
  }
359
363
 
360
364
  _getStaticFunctions (className) {
361
- return VrpcAdapter.getAvailableStaticFunctions(className)
365
+ return VrpcAdapter._getStaticFunctionsArray(className)
362
366
  }
363
367
 
364
368
  _getMetaData (className) {
365
- return VrpcAdapter.getAvailableMetaData(className)
369
+ return VrpcAdapter._getMetaData(className)
366
370
  }
367
371
 
368
372
  async _ensureConnected () {
@@ -376,14 +380,18 @@ class VrpcAgent extends EventEmitter {
376
380
  }
377
381
 
378
382
  _handleVrpcCallback (json) {
379
- const { s } = json
383
+ const { s, i, r, e, a } = json
384
+ const topic = i.startsWith('__e__') ? i.slice(5) : s
380
385
  try {
381
- this._log.debug(`Forwarding callback to: ${s} with payload:`, json)
382
- this._mqttPublish(s, VrpcAgent._stringifySafely(json))
386
+ this._log.debug(`Forwarding callback to: ${topic} with payload:`, json)
387
+ this._mqttPublish(
388
+ topic,
389
+ VrpcAgent._stringifySafely({ a, r, e, i, v: VRPC_PROTOCOL_VERSION })
390
+ )
383
391
  } catch (err) {
384
392
  this._log.warn(
385
393
  err,
386
- `Problem publishing vrpc callback to ${sender} because of: ${err.message}`
394
+ `Problem publishing vrpc callback to ${topic} because of: ${err.message}`
387
395
  )
388
396
  }
389
397
  }
@@ -549,7 +557,9 @@ class VrpcAgent extends EventEmitter {
549
557
  break
550
558
  }
551
559
  }
552
- this._mqttPublish(json.s, VrpcAgent._stringifySafely(json))
560
+ const { a, r, e, i, v } = json
561
+ const res = e ? { a, r, e, i, v } : { a, r, i, v }
562
+ this._mqttPublish(json.s, VrpcAgent._stringifySafely(res))
553
563
  } catch (err) {
554
564
  this._log.error(
555
565
  err,
@@ -562,19 +572,17 @@ class VrpcAgent extends EventEmitter {
562
572
  // Client went offline
563
573
  const clientId = topic.slice(0, -15) // /__clientInfo__ = 15
564
574
  if (json.status === 'offline') {
575
+ VrpcAdapter._unregisterClient(clientId)
565
576
  const entry = this._isolatedInstances.get(clientId)
566
577
  if (entry) {
567
- // anonymous
568
578
  entry.forEach(instanceId => {
569
579
  const json = { f: '__delete__', a: [instanceId], r: null }
570
- // TODO Do not mutate!
571
580
  VrpcAdapter._call(json)
572
581
  if (json.r) {
573
582
  this._log.debug(`Auto-deleted isolated instance: ${instanceId}`)
574
583
  }
575
584
  })
576
585
  }
577
- VrpcAdapter._unregisterEventListeners(clientId)
578
586
  this._mqttUnsubscribe(`${clientId}/__clientInfo__`)
579
587
  this.emit('clientGone', clientId)
580
588
  }
@@ -620,7 +628,6 @@ class VrpcAgent extends EventEmitter {
620
628
  _unregisterInstance (instanceId, clientId) {
621
629
  const entryIsolated = this._isolatedInstances.get(clientId)
622
630
  if (entryIsolated && entryIsolated.has(instanceId)) {
623
- VrpcAdapter._unregisterEventListenersOfInstance(instanceId)
624
631
  entryIsolated.delete(instanceId)
625
632
  if (entryIsolated.length === 0) {
626
633
  this._isolatedInstances.delete(clientId)
@@ -633,7 +640,6 @@ class VrpcAgent extends EventEmitter {
633
640
  this._sharedInstances.forEach(async v => {
634
641
  if (v.has(instanceId)) {
635
642
  found = true
636
- VrpcAdapter._unregisterEventListenersOfInstance(instanceId)
637
643
  v.delete(instanceId)
638
644
  if (v.length === 0) {
639
645
  this._sharedInstances.delete(clientId)
@@ -748,8 +754,7 @@ class VrpcAgent extends EventEmitter {
748
754
  /**
749
755
  * Event 'clientGone'
750
756
  *
751
- * Emitted when a VRPC client exited that before produced state (e.g. created
752
- * an anonymous or named instance).
757
+ * Emitted when a tracked VRPC client exited.
753
758
  *
754
759
  * @event VrpcAgent#clientGone
755
760
  * @type {String} clientId