mqtt 4.2.8 → 4.3.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.
@@ -1,45 +1,48 @@
1
- 'use strict'
2
- var tls = require('tls')
3
- var debug = require('debug')('mqttjs:tls')
4
-
5
- function buildBuilder (mqttClient, opts) {
6
- var connection
7
- opts.port = opts.port || 8883
8
- opts.host = opts.hostname || opts.host || 'localhost'
9
- opts.servername = opts.host
10
-
11
- opts.rejectUnauthorized = opts.rejectUnauthorized !== false
12
-
13
- delete opts.path
14
-
15
- debug('port %d host %s rejectUnauthorized %b', opts.port, opts.host, opts.rejectUnauthorized)
16
-
17
- connection = tls.connect(opts)
18
- /* eslint no-use-before-define: [2, "nofunc"] */
19
- connection.on('secureConnect', function () {
20
- if (opts.rejectUnauthorized && !connection.authorized) {
21
- connection.emit('error', new Error('TLS not authorized'))
22
- } else {
23
- connection.removeListener('error', handleTLSerrors)
24
- }
25
- })
26
-
27
- function handleTLSerrors (err) {
28
- // How can I get verify this error is a tls error?
29
- if (opts.rejectUnauthorized) {
30
- mqttClient.emit('error', err)
31
- }
32
-
33
- // close this connection to match the behaviour of net
34
- // otherwise all we get is an error from the connection
35
- // and close event doesn't fire. This is a work around
36
- // to enable the reconnect code to work the same as with
37
- // net.createConnection
38
- connection.end()
39
- }
40
-
41
- connection.on('error', handleTLSerrors)
42
- return connection
43
- }
44
-
45
- module.exports = buildBuilder
1
+ 'use strict'
2
+ const tls = require('tls')
3
+ const net = require('net')
4
+ const debug = require('debug')('mqttjs:tls')
5
+
6
+ function buildBuilder (mqttClient, opts) {
7
+ opts.port = opts.port || 8883
8
+ opts.host = opts.hostname || opts.host || 'localhost'
9
+
10
+ if (net.isIP(opts.host) === 0) {
11
+ opts.servername = opts.host
12
+ }
13
+
14
+ opts.rejectUnauthorized = opts.rejectUnauthorized !== false
15
+
16
+ delete opts.path
17
+
18
+ debug('port %d host %s rejectUnauthorized %b', opts.port, opts.host, opts.rejectUnauthorized)
19
+
20
+ const connection = tls.connect(opts)
21
+ /* eslint no-use-before-define: [2, "nofunc"] */
22
+ connection.on('secureConnect', function () {
23
+ if (opts.rejectUnauthorized && !connection.authorized) {
24
+ connection.emit('error', new Error('TLS not authorized'))
25
+ } else {
26
+ connection.removeListener('error', handleTLSerrors)
27
+ }
28
+ })
29
+
30
+ function handleTLSerrors (err) {
31
+ // How can I get verify this error is a tls error?
32
+ if (opts.rejectUnauthorized) {
33
+ mqttClient.emit('error', err)
34
+ }
35
+
36
+ // close this connection to match the behaviour of net
37
+ // otherwise all we get is an error from the connection
38
+ // and close event doesn't fire. This is a work around
39
+ // to enable the reconnect code to work the same as with
40
+ // net.createConnection
41
+ connection.end()
42
+ }
43
+
44
+ connection.on('error', handleTLSerrors)
45
+ return connection
46
+ }
47
+
48
+ module.exports = buildBuilder
package/lib/connect/ws.js CHANGED
@@ -1,256 +1,256 @@
1
- 'use strict'
2
-
3
- const WS = require('ws')
4
- const debug = require('debug')('mqttjs:ws')
5
- const duplexify = require('duplexify')
6
- const Transform = require('readable-stream').Transform
7
-
8
- let WSS_OPTIONS = [
9
- 'rejectUnauthorized',
10
- 'ca',
11
- 'cert',
12
- 'key',
13
- 'pfx',
14
- 'passphrase'
15
- ]
16
- // eslint-disable-next-line camelcase
17
- const IS_BROWSER = (typeof process !== 'undefined' && process.title === 'browser') || typeof __webpack_require__ === 'function'
18
- function buildUrl (opts, client) {
19
- let url = opts.protocol + '://' + opts.hostname + ':' + opts.port + opts.path
20
- if (typeof (opts.transformWsUrl) === 'function') {
21
- url = opts.transformWsUrl(url, opts, client)
22
- }
23
- return url
24
- }
25
-
26
- function setDefaultOpts (opts) {
27
- let options = opts
28
- if (!opts.hostname) {
29
- options.hostname = 'localhost'
30
- }
31
- if (!opts.port) {
32
- if (opts.protocol === 'wss') {
33
- options.port = 443
34
- } else {
35
- options.port = 80
36
- }
37
- }
38
- if (!opts.path) {
39
- options.path = '/'
40
- }
41
-
42
- if (!opts.wsOptions) {
43
- options.wsOptions = {}
44
- }
45
- if (!IS_BROWSER && opts.protocol === 'wss') {
46
- // Add cert/key/ca etc options
47
- WSS_OPTIONS.forEach(function (prop) {
48
- if (opts.hasOwnProperty(prop) && !opts.wsOptions.hasOwnProperty(prop)) {
49
- options.wsOptions[prop] = opts[prop]
50
- }
51
- })
52
- }
53
-
54
- return options
55
- }
56
-
57
- function setDefaultBrowserOpts (opts) {
58
- let options = setDefaultOpts(opts)
59
-
60
- if (!options.hostname) {
61
- options.hostname = options.host
62
- }
63
-
64
- if (!options.hostname) {
65
- // Throwing an error in a Web Worker if no `hostname` is given, because we
66
- // can not determine the `hostname` automatically. If connecting to
67
- // localhost, please supply the `hostname` as an argument.
68
- if (typeof (document) === 'undefined') {
69
- throw new Error('Could not determine host. Specify host manually.')
70
- }
71
- const parsed = new URL(document.URL)
72
- options.hostname = parsed.hostname
73
-
74
- if (!options.port) {
75
- options.port = parsed.port
76
- }
77
- }
78
-
79
- // objectMode should be defined for logic
80
- if (options.objectMode === undefined) {
81
- options.objectMode = !(options.binary === true || options.binary === undefined)
82
- }
83
-
84
- return options
85
- }
86
-
87
- function createWebSocket (client, url, opts) {
88
- debug('createWebSocket')
89
- debug('protocol: ' + opts.protocolId + ' ' + opts.protocolVersion)
90
- const websocketSubProtocol =
91
- (opts.protocolId === 'MQIsdp') && (opts.protocolVersion === 3)
92
- ? 'mqttv3.1'
93
- : 'mqtt'
94
-
95
- debug('creating new Websocket for url: ' + url + ' and protocol: ' + websocketSubProtocol)
96
- let socket = new WS(url, [websocketSubProtocol], opts.wsOptions)
97
- return socket
98
- }
99
-
100
- function createBrowserWebSocket (client, opts) {
101
- const websocketSubProtocol =
102
- (opts.protocolId === 'MQIsdp') && (opts.protocolVersion === 3)
103
- ? 'mqttv3.1'
104
- : 'mqtt'
105
-
106
- let url = buildUrl(opts, client)
107
- /* global WebSocket */
108
- let socket = new WebSocket(url, [websocketSubProtocol])
109
- socket.binaryType = 'arraybuffer'
110
- return socket
111
- }
112
-
113
- function streamBuilder (client, opts) {
114
- debug('streamBuilder')
115
- let options = setDefaultOpts(opts)
116
- const url = buildUrl(options, client)
117
- let socket = createWebSocket(client, url, options)
118
- let webSocketStream = WS.createWebSocketStream(socket, options.wsOptions)
119
- webSocketStream.url = url
120
- socket.on('close', () => { webSocketStream.destroy() })
121
- return webSocketStream
122
- }
123
-
124
- function browserStreamBuilder (client, opts) {
125
- debug('browserStreamBuilder')
126
- let stream
127
- let options = setDefaultBrowserOpts(opts)
128
- // sets the maximum socket buffer size before throttling
129
- const bufferSize = options.browserBufferSize || 1024 * 512
130
-
131
- const bufferTimeout = opts.browserBufferTimeout || 1000
132
-
133
- const coerceToBuffer = !opts.objectMode
134
-
135
- let socket = createBrowserWebSocket(client, opts)
136
-
137
- let proxy = buildProxy(opts, socketWriteBrowser, socketEndBrowser)
138
-
139
- if (!opts.objectMode) {
140
- proxy._writev = writev
141
- }
142
- proxy.on('close', () => { socket.close() })
143
-
144
- const eventListenerSupport = (typeof socket.addEventListener !== 'undefined')
145
-
146
- // was already open when passed in
147
- if (socket.readyState === socket.OPEN) {
148
- stream = proxy
149
- } else {
150
- stream = stream = duplexify(undefined, undefined, opts)
151
- if (!opts.objectMode) {
152
- stream._writev = writev
153
- }
154
-
155
- if (eventListenerSupport) {
156
- socket.addEventListener('open', onopen)
157
- } else {
158
- socket.onopen = onopen
159
- }
160
- }
161
-
162
- stream.socket = socket
163
-
164
- if (eventListenerSupport) {
165
- socket.addEventListener('close', onclose)
166
- socket.addEventListener('error', onerror)
167
- socket.addEventListener('message', onmessage)
168
- } else {
169
- socket.onclose = onclose
170
- socket.onerror = onerror
171
- socket.onmessage = onmessage
172
- }
173
-
174
- // methods for browserStreamBuilder
175
-
176
- function buildProxy (options, socketWrite, socketEnd) {
177
- let proxy = new Transform({
178
- objectModeMode: options.objectMode
179
- })
180
-
181
- proxy._write = socketWrite
182
- proxy._flush = socketEnd
183
-
184
- return proxy
185
- }
186
-
187
- function onopen () {
188
- stream.setReadable(proxy)
189
- stream.setWritable(proxy)
190
- stream.emit('connect')
191
- }
192
-
193
- function onclose () {
194
- stream.end()
195
- stream.destroy()
196
- }
197
-
198
- function onerror (err) {
199
- stream.destroy(err)
200
- }
201
-
202
- function onmessage (event) {
203
- let data = event.data
204
- if (data instanceof ArrayBuffer) data = Buffer.from(data)
205
- else data = Buffer.from(data, 'utf8')
206
- proxy.push(data)
207
- }
208
-
209
- // this is to be enabled only if objectMode is false
210
- function writev (chunks, cb) {
211
- const buffers = new Array(chunks.length)
212
- for (let i = 0; i < chunks.length; i++) {
213
- if (typeof chunks[i].chunk === 'string') {
214
- buffers[i] = Buffer.from(chunks[i], 'utf8')
215
- } else {
216
- buffers[i] = chunks[i].chunk
217
- }
218
- }
219
-
220
- this._write(Buffer.concat(buffers), 'binary', cb)
221
- }
222
-
223
- function socketWriteBrowser (chunk, enc, next) {
224
- if (socket.bufferedAmount > bufferSize) {
225
- // throttle data until buffered amount is reduced.
226
- setTimeout(socketWriteBrowser, bufferTimeout, chunk, enc, next)
227
- }
228
-
229
- if (coerceToBuffer && typeof chunk === 'string') {
230
- chunk = Buffer.from(chunk, 'utf8')
231
- }
232
-
233
- try {
234
- socket.send(chunk)
235
- } catch (err) {
236
- return next(err)
237
- }
238
-
239
- next()
240
- }
241
-
242
- function socketEndBrowser (done) {
243
- socket.close()
244
- done()
245
- }
246
-
247
- // end methods for browserStreamBuilder
248
-
249
- return stream
250
- }
251
-
252
- if (IS_BROWSER) {
253
- module.exports = browserStreamBuilder
254
- } else {
255
- module.exports = streamBuilder
256
- }
1
+ 'use strict'
2
+
3
+ const WS = require('ws')
4
+ const debug = require('debug')('mqttjs:ws')
5
+ const duplexify = require('duplexify')
6
+ const Transform = require('readable-stream').Transform
7
+
8
+ const WSS_OPTIONS = [
9
+ 'rejectUnauthorized',
10
+ 'ca',
11
+ 'cert',
12
+ 'key',
13
+ 'pfx',
14
+ 'passphrase'
15
+ ]
16
+ // eslint-disable-next-line camelcase
17
+ const IS_BROWSER = (typeof process !== 'undefined' && process.title === 'browser') || typeof __webpack_require__ === 'function'
18
+ function buildUrl (opts, client) {
19
+ let url = opts.protocol + '://' + opts.hostname + ':' + opts.port + opts.path
20
+ if (typeof (opts.transformWsUrl) === 'function') {
21
+ url = opts.transformWsUrl(url, opts, client)
22
+ }
23
+ return url
24
+ }
25
+
26
+ function setDefaultOpts (opts) {
27
+ const options = opts
28
+ if (!opts.hostname) {
29
+ options.hostname = 'localhost'
30
+ }
31
+ if (!opts.port) {
32
+ if (opts.protocol === 'wss') {
33
+ options.port = 443
34
+ } else {
35
+ options.port = 80
36
+ }
37
+ }
38
+ if (!opts.path) {
39
+ options.path = '/'
40
+ }
41
+
42
+ if (!opts.wsOptions) {
43
+ options.wsOptions = {}
44
+ }
45
+ if (!IS_BROWSER && opts.protocol === 'wss') {
46
+ // Add cert/key/ca etc options
47
+ WSS_OPTIONS.forEach(function (prop) {
48
+ if (Object.prototype.hasOwnProperty.call(opts, prop) && !Object.prototype.hasOwnProperty.call(opts.wsOptions, prop)) {
49
+ options.wsOptions[prop] = opts[prop]
50
+ }
51
+ })
52
+ }
53
+
54
+ return options
55
+ }
56
+
57
+ function setDefaultBrowserOpts (opts) {
58
+ const options = setDefaultOpts(opts)
59
+
60
+ if (!options.hostname) {
61
+ options.hostname = options.host
62
+ }
63
+
64
+ if (!options.hostname) {
65
+ // Throwing an error in a Web Worker if no `hostname` is given, because we
66
+ // can not determine the `hostname` automatically. If connecting to
67
+ // localhost, please supply the `hostname` as an argument.
68
+ if (typeof (document) === 'undefined') {
69
+ throw new Error('Could not determine host. Specify host manually.')
70
+ }
71
+ const parsed = new URL(document.URL)
72
+ options.hostname = parsed.hostname
73
+
74
+ if (!options.port) {
75
+ options.port = parsed.port
76
+ }
77
+ }
78
+
79
+ // objectMode should be defined for logic
80
+ if (options.objectMode === undefined) {
81
+ options.objectMode = !(options.binary === true || options.binary === undefined)
82
+ }
83
+
84
+ return options
85
+ }
86
+
87
+ function createWebSocket (client, url, opts) {
88
+ debug('createWebSocket')
89
+ debug('protocol: ' + opts.protocolId + ' ' + opts.protocolVersion)
90
+ const websocketSubProtocol =
91
+ (opts.protocolId === 'MQIsdp') && (opts.protocolVersion === 3)
92
+ ? 'mqttv3.1'
93
+ : 'mqtt'
94
+
95
+ debug('creating new Websocket for url: ' + url + ' and protocol: ' + websocketSubProtocol)
96
+ const socket = new WS(url, [websocketSubProtocol], opts.wsOptions)
97
+ return socket
98
+ }
99
+
100
+ function createBrowserWebSocket (client, opts) {
101
+ const websocketSubProtocol =
102
+ (opts.protocolId === 'MQIsdp') && (opts.protocolVersion === 3)
103
+ ? 'mqttv3.1'
104
+ : 'mqtt'
105
+
106
+ const url = buildUrl(opts, client)
107
+ /* global WebSocket */
108
+ const socket = new WebSocket(url, [websocketSubProtocol])
109
+ socket.binaryType = 'arraybuffer'
110
+ return socket
111
+ }
112
+
113
+ function streamBuilder (client, opts) {
114
+ debug('streamBuilder')
115
+ const options = setDefaultOpts(opts)
116
+ const url = buildUrl(options, client)
117
+ const socket = createWebSocket(client, url, options)
118
+ const webSocketStream = WS.createWebSocketStream(socket, options.wsOptions)
119
+ webSocketStream.url = url
120
+ socket.on('close', () => { webSocketStream.destroy() })
121
+ return webSocketStream
122
+ }
123
+
124
+ function browserStreamBuilder (client, opts) {
125
+ debug('browserStreamBuilder')
126
+ let stream
127
+ const options = setDefaultBrowserOpts(opts)
128
+ // sets the maximum socket buffer size before throttling
129
+ const bufferSize = options.browserBufferSize || 1024 * 512
130
+
131
+ const bufferTimeout = opts.browserBufferTimeout || 1000
132
+
133
+ const coerceToBuffer = !opts.objectMode
134
+
135
+ const socket = createBrowserWebSocket(client, opts)
136
+
137
+ const proxy = buildProxy(opts, socketWriteBrowser, socketEndBrowser)
138
+
139
+ if (!opts.objectMode) {
140
+ proxy._writev = writev
141
+ }
142
+ proxy.on('close', () => { socket.close() })
143
+
144
+ const eventListenerSupport = (typeof socket.addEventListener !== 'undefined')
145
+
146
+ // was already open when passed in
147
+ if (socket.readyState === socket.OPEN) {
148
+ stream = proxy
149
+ } else {
150
+ stream = stream = duplexify(undefined, undefined, opts)
151
+ if (!opts.objectMode) {
152
+ stream._writev = writev
153
+ }
154
+
155
+ if (eventListenerSupport) {
156
+ socket.addEventListener('open', onopen)
157
+ } else {
158
+ socket.onopen = onopen
159
+ }
160
+ }
161
+
162
+ stream.socket = socket
163
+
164
+ if (eventListenerSupport) {
165
+ socket.addEventListener('close', onclose)
166
+ socket.addEventListener('error', onerror)
167
+ socket.addEventListener('message', onmessage)
168
+ } else {
169
+ socket.onclose = onclose
170
+ socket.onerror = onerror
171
+ socket.onmessage = onmessage
172
+ }
173
+
174
+ // methods for browserStreamBuilder
175
+
176
+ function buildProxy (options, socketWrite, socketEnd) {
177
+ const proxy = new Transform({
178
+ objectModeMode: options.objectMode
179
+ })
180
+
181
+ proxy._write = socketWrite
182
+ proxy._flush = socketEnd
183
+
184
+ return proxy
185
+ }
186
+
187
+ function onopen () {
188
+ stream.setReadable(proxy)
189
+ stream.setWritable(proxy)
190
+ stream.emit('connect')
191
+ }
192
+
193
+ function onclose () {
194
+ stream.end()
195
+ stream.destroy()
196
+ }
197
+
198
+ function onerror (err) {
199
+ stream.destroy(err)
200
+ }
201
+
202
+ function onmessage (event) {
203
+ let data = event.data
204
+ if (data instanceof ArrayBuffer) data = Buffer.from(data)
205
+ else data = Buffer.from(data, 'utf8')
206
+ proxy.push(data)
207
+ }
208
+
209
+ // this is to be enabled only if objectMode is false
210
+ function writev (chunks, cb) {
211
+ const buffers = new Array(chunks.length)
212
+ for (let i = 0; i < chunks.length; i++) {
213
+ if (typeof chunks[i].chunk === 'string') {
214
+ buffers[i] = Buffer.from(chunks[i], 'utf8')
215
+ } else {
216
+ buffers[i] = chunks[i].chunk
217
+ }
218
+ }
219
+
220
+ this._write(Buffer.concat(buffers), 'binary', cb)
221
+ }
222
+
223
+ function socketWriteBrowser (chunk, enc, next) {
224
+ if (socket.bufferedAmount > bufferSize) {
225
+ // throttle data until buffered amount is reduced.
226
+ setTimeout(socketWriteBrowser, bufferTimeout, chunk, enc, next)
227
+ }
228
+
229
+ if (coerceToBuffer && typeof chunk === 'string') {
230
+ chunk = Buffer.from(chunk, 'utf8')
231
+ }
232
+
233
+ try {
234
+ socket.send(chunk)
235
+ } catch (err) {
236
+ return next(err)
237
+ }
238
+
239
+ next()
240
+ }
241
+
242
+ function socketEndBrowser (done) {
243
+ socket.close()
244
+ done()
245
+ }
246
+
247
+ // end methods for browserStreamBuilder
248
+
249
+ return stream
250
+ }
251
+
252
+ if (IS_BROWSER) {
253
+ module.exports = browserStreamBuilder
254
+ } else {
255
+ module.exports = streamBuilder
256
+ }