vrpc 3.0.2 → 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vrpc",
3
- "version": "3.0.2",
3
+ "version": "3.0.3",
4
4
  "description": "Variadic Remote Procedure Calls",
5
5
  "main": "index.js",
6
6
  "directories": {
package/vrpc/VrpcAgent.js CHANGED
@@ -92,9 +92,9 @@ 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)
97
+ * @param {String} [obj.token] Access token
98
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>`
@@ -109,9 +109,9 @@ class VrpcAgent extends EventEmitter {
109
109
  * })
110
110
  */
111
111
  constructor ({
112
- username,
113
- password,
114
112
  token,
113
+ password,
114
+ username = VrpcAgent._generateAgentName(),
115
115
  domain = 'vrpc',
116
116
  agent = VrpcAgent._generateAgentName(),
117
117
  broker = 'mqtts://vrpc.io:8883',
@@ -173,6 +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 = username || this._agent
176
177
  password = this._token
177
178
  }
178
179
  this._options = {
@@ -243,13 +244,17 @@ class VrpcAgent extends EventEmitter {
243
244
  }
244
245
  }
245
246
 
247
+ static _generateUserName () {
248
+ return `${os.userInfo().username}@${os.hostname()}-${os.platform()}-js`
249
+ }
250
+
246
251
  static _generateAgentName () {
247
252
  const { username } = os.userInfo()
248
253
  const pathId = crypto
249
254
  .createHash('md5')
250
255
  .update(path.resolve())
251
256
  .digest('hex')
252
- .substr(0, 4)
257
+ .substring(0, 4)
253
258
  return `${username}-${pathId}@${os.hostname()}-${os.platform()}-js`
254
259
  }
255
260
 
@@ -59,9 +59,9 @@ class VrpcClient extends EventEmitter {
59
59
  *
60
60
  * @constructor
61
61
  * @param {Object} options
62
- * @param {String} options.token Access token as generated by: https://app.vrpc.io
63
- * @param {String} options.username MQTT username (if no token is used)
64
- * @param {String} options.password MQTT password (if no token is provided)
62
+ * @param {String} [options.username='<user>@<hostname>-<platform>-js'] MQTT username
63
+ * @param {String} [options.password] MQTT password (if no token is provided)
64
+ * @param {String} [options.token] Access token
65
65
  * @param {String} options.domain Sets the domain
66
66
  * @param {String} [options.agent="*"] Sets default agent
67
67
  * @param {String} [options.broker="mqtts://vrpc.io:8883"] Broker url in form: `<scheme>://<host>:<port>`
@@ -76,8 +76,8 @@ class VrpcClient extends EventEmitter {
76
76
  */
77
77
  constructor ({
78
78
  token,
79
- username,
80
79
  password,
80
+ username = VrpcClient._generateUserName(),
81
81
  domain,
82
82
  agent = '*',
83
83
  broker = 'mqtts://vrpc.io:8883',
@@ -152,6 +152,7 @@ class VrpcClient extends EventEmitter {
152
152
  let username = this._username
153
153
  let password = this._password
154
154
  if (this._token) {
155
+ username = username || VrpcClient._generateUserName()
155
156
  password = this._token
156
157
  }
157
158
  const options = {
@@ -659,6 +660,10 @@ class VrpcClient extends EventEmitter {
659
660
  return new Promise(resolve => this._client.end(false, {}, resolve))
660
661
  }
661
662
 
663
+ static _generateUserName () {
664
+ return `${os.userInfo().username}@${os.hostname()}-${os.platform()}-js`
665
+ }
666
+
662
667
  _createMqttClientId () {
663
668
  const clientInfo =
664
669
  os.arch() +
@@ -932,7 +937,8 @@ class VrpcClient extends EventEmitter {
932
937
  if (typeof ret === 'string' && ret.substring(0, 5) === '__p__') {
933
938
  this._eventEmitter.once(ret, promiseData => {
934
939
  // TODO improve error message here, like above
935
- if (promiseData.e) reject(new Error(`[vrpc ${agent}-${c}-${f}]: ${promiseData.e}`))
940
+ if (promiseData.e)
941
+ reject(new Error(`[vrpc ${agent}-${c}-${f}]: ${promiseData.e}`))
936
942
  else resolve(promiseData.r)
937
943
  })
938
944
  } else {
@@ -1067,7 +1073,9 @@ class VrpcClient extends EventEmitter {
1067
1073
  : `${this._domain}/${agent}/${className}/${event}`
1068
1074
  const id = `__e__${topic}`
1069
1075
  if (this._cachedSubscriptions[topic]) {
1070
- const index = this._cachedSubscriptions[topic].findIndex(x => x.callback === callback)
1076
+ const index = this._cachedSubscriptions[topic].findIndex(
1077
+ x => x.callback === callback
1078
+ )
1071
1079
  if (index === -1) {
1072
1080
  this._log.warn(`Failed removing event listener ${id}`)
1073
1081
  return null