mqtt 4.2.0 → 4.2.1

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