vrpc 3.11.1 → 3.12.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/browser/vrpc.js +1 -1
- package/package.json +1 -1
- package/vrpc/VrpcAgent.js +16 -2
- package/vrpc/VrpcClient.js +16 -2
package/package.json
CHANGED
package/vrpc/VrpcAgent.js
CHANGED
|
@@ -102,6 +102,7 @@ class VrpcAgent extends EventEmitter {
|
|
|
102
102
|
* @param {String} [obj.bestEffort=true] If true, message will be sent with best effort, i.e. no caching if offline
|
|
103
103
|
* @param {String} [obj.version=''] The (user-defined) version of this agent
|
|
104
104
|
* @param {String} [obj.mqttClientId='<generated()>'] Explicitly set the mqtt client id.
|
|
105
|
+
* @param {Object} [obj.tls] TLS settings for a secure broker: `{ ca, rejectUnauthorized }`. Without it the broker's certificate is not verified (the historical behaviour); with `ca` the broker must present a chain the given certificates anchor.
|
|
105
106
|
*
|
|
106
107
|
* @example
|
|
107
108
|
* const agent = new Agent({
|
|
@@ -119,7 +120,8 @@ class VrpcAgent extends EventEmitter {
|
|
|
119
120
|
log = 'console',
|
|
120
121
|
bestEffort = true,
|
|
121
122
|
version = '',
|
|
122
|
-
mqttClientId = null
|
|
123
|
+
mqttClientId = null,
|
|
124
|
+
tls = null
|
|
123
125
|
} = {}) {
|
|
124
126
|
super()
|
|
125
127
|
this._validateDomain(domain)
|
|
@@ -132,6 +134,7 @@ class VrpcAgent extends EventEmitter {
|
|
|
132
134
|
this._broker = broker
|
|
133
135
|
this._qos = bestEffort ? 0 : 1
|
|
134
136
|
this._version = version
|
|
137
|
+
this._tls = tls
|
|
135
138
|
this._mqttClientId =
|
|
136
139
|
mqttClientId || `va3${VrpcAgent._createHash(this._domain + this._agent)}`
|
|
137
140
|
if (log === 'console') {
|
|
@@ -192,7 +195,7 @@ class VrpcAgent extends EventEmitter {
|
|
|
192
195
|
// agent must come back when it does (mqtt >= 5 stops otherwise)
|
|
193
196
|
reconnectOnConnackError: true,
|
|
194
197
|
clientId: this._mqttClientId,
|
|
195
|
-
|
|
198
|
+
...VrpcAgent._tlsOptions(this._tls),
|
|
196
199
|
will: {
|
|
197
200
|
topic: `${this._baseTopic}/__agentInfo__`,
|
|
198
201
|
payload: this._createAgentInfoPayload({ status: 'offline' }),
|
|
@@ -297,6 +300,17 @@ class VrpcAgent extends EventEmitter {
|
|
|
297
300
|
return token.slice(0, length)
|
|
298
301
|
}
|
|
299
302
|
|
|
303
|
+
/**
|
|
304
|
+
* The TLS part of the MQTT options: verification off when nothing was
|
|
305
|
+
* given (as it always was), the given anchors otherwise.
|
|
306
|
+
*/
|
|
307
|
+
static _tlsOptions (tls) {
|
|
308
|
+
if (!tls) return { rejectUnauthorized: false }
|
|
309
|
+
const options = { rejectUnauthorized: tls.rejectUnauthorized !== false }
|
|
310
|
+
if (tls.ca) options.ca = tls.ca
|
|
311
|
+
return options
|
|
312
|
+
}
|
|
313
|
+
|
|
300
314
|
static _generateAgentName () {
|
|
301
315
|
const { username } = os.userInfo()
|
|
302
316
|
const pathId = VrpcAgent._createHash(path.resolve, 4)
|
package/vrpc/VrpcClient.js
CHANGED
|
@@ -75,6 +75,7 @@ class VrpcClient extends EventEmitter {
|
|
|
75
75
|
* domain: 'vrpc',
|
|
76
76
|
* broker: 'mqtts://broker.hivemq.com:8883'
|
|
77
77
|
* })
|
|
78
|
+
* @param {Object} [obj.tls] TLS settings for a secure broker: `{ ca, rejectUnauthorized }`. Without it the broker's certificate is not verified (the historical behaviour); with `ca` the broker must present a chain the given certificates anchor.
|
|
78
79
|
*/
|
|
79
80
|
constructor ({
|
|
80
81
|
token,
|
|
@@ -89,7 +90,8 @@ class VrpcClient extends EventEmitter {
|
|
|
89
90
|
mqttClientId = null,
|
|
90
91
|
identity = null,
|
|
91
92
|
keepalive = 30,
|
|
92
|
-
requiresSchema = false
|
|
93
|
+
requiresSchema = false,
|
|
94
|
+
tls = null
|
|
93
95
|
} = {}) {
|
|
94
96
|
super()
|
|
95
97
|
// domain sanity check
|
|
@@ -137,6 +139,7 @@ class VrpcClient extends EventEmitter {
|
|
|
137
139
|
|
|
138
140
|
this._instance = nanoid(8)
|
|
139
141
|
this._mqttClientId = mqttClientId || this._createMqttClientId()
|
|
142
|
+
this._tls = tls
|
|
140
143
|
this._vrpcClientId = this._createVrpcClientId()
|
|
141
144
|
// the part of the connection id nobody else can learn: a broker that
|
|
142
145
|
// confines a client to topics under its own mqtt client id keeps
|
|
@@ -203,6 +206,17 @@ class VrpcClient extends EventEmitter {
|
|
|
203
206
|
return this._vrpcConnectionId
|
|
204
207
|
}
|
|
205
208
|
|
|
209
|
+
/**
|
|
210
|
+
* The TLS part of the MQTT options: verification off when nothing was
|
|
211
|
+
* given (as it always was), the given anchors otherwise.
|
|
212
|
+
*/
|
|
213
|
+
static _tlsOptions (tls) {
|
|
214
|
+
if (!tls) return { rejectUnauthorized: false }
|
|
215
|
+
const options = { rejectUnauthorized: tls.rejectUnauthorized !== false }
|
|
216
|
+
if (tls.ca) options.ca = tls.ca
|
|
217
|
+
return options
|
|
218
|
+
}
|
|
219
|
+
|
|
206
220
|
/**
|
|
207
221
|
* Actually connects to the MQTT broker.
|
|
208
222
|
*
|
|
@@ -229,7 +243,7 @@ class VrpcClient extends EventEmitter {
|
|
|
229
243
|
clean: true,
|
|
230
244
|
keepalive: this._keepalive,
|
|
231
245
|
clientId: this._mqttClientId,
|
|
232
|
-
|
|
246
|
+
...VrpcClient._tlsOptions(this._tls),
|
|
233
247
|
connectTimeout: this._timeout,
|
|
234
248
|
// a refused CONNACK is retried like any other failed attempt (see
|
|
235
249
|
// VrpcAgent): whoever wants a refusal to be final ends the client
|