mqtt 4.2.4 → 4.2.8

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,45 @@
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
+ 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
package/lib/connect/ws.js CHANGED
@@ -1,255 +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
- return webSocketStream
121
- }
122
-
123
- function browserStreamBuilder (client, opts) {
124
- debug('browserStreamBuilder')
125
- let stream
126
- let options = setDefaultBrowserOpts(opts)
127
- // sets the maximum socket buffer size before throttling
128
- const bufferSize = options.browserBufferSize || 1024 * 512
129
-
130
- const bufferTimeout = opts.browserBufferTimeout || 1000
131
-
132
- const coerceToBuffer = !opts.objectMode
133
-
134
- let socket = createBrowserWebSocket(client, opts)
135
-
136
- let proxy = buildProxy(opts, socketWriteBrowser, socketEndBrowser)
137
-
138
- if (!opts.objectMode) {
139
- proxy._writev = writev
140
- }
141
- proxy.on('close', () => { socket.close() })
142
-
143
- const eventListenerSupport = (typeof socket.addEventListener === 'undefined')
144
-
145
- // was already open when passed in
146
- if (socket.readyState === socket.OPEN) {
147
- stream = proxy
148
- } else {
149
- stream = stream = duplexify(undefined, undefined, opts)
150
- if (!opts.objectMode) {
151
- stream._writev = writev
152
- }
153
-
154
- if (eventListenerSupport) {
155
- socket.addEventListener('open', onopen)
156
- } else {
157
- socket.onopen = onopen
158
- }
159
- }
160
-
161
- stream.socket = socket
162
-
163
- if (eventListenerSupport) {
164
- socket.addEventListener('close', onclose)
165
- socket.addEventListener('error', onerror)
166
- socket.addEventListener('message', onmessage)
167
- } else {
168
- socket.onclose = onclose
169
- socket.onerror = onerror
170
- socket.onmessage = onmessage
171
- }
172
-
173
- // methods for browserStreamBuilder
174
-
175
- function buildProxy (options, socketWrite, socketEnd) {
176
- let proxy = new Transform({
177
- objectModeMode: options.objectMode
178
- })
179
-
180
- proxy._write = socketWrite
181
- proxy._flush = socketEnd
182
-
183
- return proxy
184
- }
185
-
186
- function onopen () {
187
- stream.setReadable(proxy)
188
- stream.setWritable(proxy)
189
- stream.emit('connect')
190
- }
191
-
192
- function onclose () {
193
- stream.end()
194
- stream.destroy()
195
- }
196
-
197
- function onerror (err) {
198
- stream.destroy(err)
199
- }
200
-
201
- function onmessage (event) {
202
- let data = event.data
203
- if (data instanceof ArrayBuffer) data = Buffer.from(data)
204
- else data = Buffer.from(data, 'utf8')
205
- proxy.push(data)
206
- }
207
-
208
- // this is to be enabled only if objectMode is false
209
- function writev (chunks, cb) {
210
- const buffers = new Array(chunks.length)
211
- for (let i = 0; i < chunks.length; i++) {
212
- if (typeof chunks[i].chunk === 'string') {
213
- buffers[i] = Buffer.from(chunks[i], 'utf8')
214
- } else {
215
- buffers[i] = chunks[i].chunk
216
- }
217
- }
218
-
219
- this._write(Buffer.concat(buffers), 'binary', cb)
220
- }
221
-
222
- function socketWriteBrowser (chunk, enc, next) {
223
- if (socket.bufferedAmount > bufferSize) {
224
- // throttle data until buffered amount is reduced.
225
- setTimeout(socketWriteBrowser, bufferTimeout, chunk, enc, next)
226
- }
227
-
228
- if (coerceToBuffer && typeof chunk === 'string') {
229
- chunk = Buffer.from(chunk, 'utf8')
230
- }
231
-
232
- try {
233
- socket.send(chunk)
234
- } catch (err) {
235
- return next(err)
236
- }
237
-
238
- next()
239
- }
240
-
241
- function socketEndBrowser (done) {
242
- socket.close()
243
- done()
244
- }
245
-
246
- // end methods for browserStreamBuilder
247
-
248
- return stream
249
- }
250
-
251
- if (IS_BROWSER) {
252
- module.exports = browserStreamBuilder
253
- } else {
254
- module.exports = streamBuilder
255
- }
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
+ }