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/dist/mqtt.js +192 -35
- package/dist/mqtt.min.js +1 -1
- package/lib/connect/ws.js +191 -34
- package/package.json +1 -1
package/dist/mqtt.js
CHANGED
|
@@ -1785,10 +1785,14 @@ module.exports = buildBuilder
|
|
|
1785
1785
|
(function (process){
|
|
1786
1786
|
'use strict'
|
|
1787
1787
|
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
1791
|
-
|
|
1788
|
+
const WS = require('ws')
|
|
1789
|
+
const debug = require('debug')('mqttjs:ws')
|
|
1790
|
+
const duplexify = require('duplexify')
|
|
1791
|
+
const Buffer = require('safe-buffer').Buffer
|
|
1792
|
+
const urlModule = require('url')
|
|
1793
|
+
const Transform = require('readable-stream').Transform
|
|
1794
|
+
|
|
1795
|
+
let WSS_OPTIONS = [
|
|
1792
1796
|
'rejectUnauthorized',
|
|
1793
1797
|
'ca',
|
|
1794
1798
|
'cert',
|
|
@@ -1797,9 +1801,9 @@ var WSS_OPTIONS = [
|
|
|
1797
1801
|
'passphrase'
|
|
1798
1802
|
]
|
|
1799
1803
|
// eslint-disable-next-line camelcase
|
|
1800
|
-
|
|
1804
|
+
const IS_BROWSER = (typeof process !== 'undefined' && process.title === 'browser') || typeof __webpack_require__ === 'function'
|
|
1801
1805
|
function buildUrl (opts, client) {
|
|
1802
|
-
|
|
1806
|
+
let url = opts.protocol + '://' + opts.hostname + ':' + opts.port + opts.path
|
|
1803
1807
|
if (typeof (opts.transformWsUrl) === 'function') {
|
|
1804
1808
|
url = opts.transformWsUrl(url, opts, client)
|
|
1805
1809
|
}
|
|
@@ -1807,75 +1811,228 @@ function buildUrl (opts, client) {
|
|
|
1807
1811
|
}
|
|
1808
1812
|
|
|
1809
1813
|
function setDefaultOpts (opts) {
|
|
1814
|
+
let options = opts
|
|
1810
1815
|
if (!opts.hostname) {
|
|
1811
|
-
|
|
1816
|
+
options.hostname = 'localhost'
|
|
1812
1817
|
}
|
|
1813
1818
|
if (!opts.port) {
|
|
1814
1819
|
if (opts.protocol === 'wss') {
|
|
1815
|
-
|
|
1820
|
+
options.port = 443
|
|
1816
1821
|
} else {
|
|
1817
|
-
|
|
1822
|
+
options.port = 80
|
|
1818
1823
|
}
|
|
1819
1824
|
}
|
|
1820
1825
|
if (!opts.path) {
|
|
1821
|
-
|
|
1826
|
+
options.path = '/'
|
|
1822
1827
|
}
|
|
1823
1828
|
|
|
1824
1829
|
if (!opts.wsOptions) {
|
|
1825
|
-
|
|
1830
|
+
options.wsOptions = {}
|
|
1826
1831
|
}
|
|
1827
1832
|
if (!IS_BROWSER && opts.protocol === 'wss') {
|
|
1828
1833
|
// Add cert/key/ca etc options
|
|
1829
1834
|
WSS_OPTIONS.forEach(function (prop) {
|
|
1830
1835
|
if (opts.hasOwnProperty(prop) && !opts.wsOptions.hasOwnProperty(prop)) {
|
|
1831
|
-
|
|
1836
|
+
options.wsOptions[prop] = opts[prop]
|
|
1832
1837
|
}
|
|
1833
1838
|
})
|
|
1834
1839
|
}
|
|
1840
|
+
|
|
1841
|
+
return options
|
|
1835
1842
|
}
|
|
1836
1843
|
|
|
1837
|
-
function
|
|
1844
|
+
function setDefaultBrowserOpts (opts) {
|
|
1845
|
+
let options = setDefaultOpts(opts)
|
|
1846
|
+
|
|
1847
|
+
if (!options.hostname) {
|
|
1848
|
+
options.hostname = options.host
|
|
1849
|
+
}
|
|
1850
|
+
|
|
1851
|
+
if (!options.hostname) {
|
|
1852
|
+
// Throwing an error in a Web Worker if no `hostname` is given, because we
|
|
1853
|
+
// can not determine the `hostname` automatically. If connecting to
|
|
1854
|
+
// localhost, please supply the `hostname` as an argument.
|
|
1855
|
+
if (typeof (document) === 'undefined') {
|
|
1856
|
+
throw new Error('Could not determine host. Specify host manually.')
|
|
1857
|
+
}
|
|
1858
|
+
const parsed = urlModule.parse(document.URL)
|
|
1859
|
+
options.hostname = parsed.hostname
|
|
1860
|
+
|
|
1861
|
+
if (!options.port) {
|
|
1862
|
+
options.port = parsed.port
|
|
1863
|
+
}
|
|
1864
|
+
}
|
|
1865
|
+
|
|
1866
|
+
// objectMode should be defined for logic
|
|
1867
|
+
if (options.objectMode === undefined) {
|
|
1868
|
+
options.objectMode = !(options.binary === true || options.binary === undefined)
|
|
1869
|
+
}
|
|
1870
|
+
|
|
1871
|
+
return options
|
|
1872
|
+
}
|
|
1873
|
+
|
|
1874
|
+
function createWebSocket (client, url, opts) {
|
|
1838
1875
|
debug('createWebSocket')
|
|
1839
1876
|
debug('protocol: ' + opts.protocolId + ' ' + opts.protocolVersion)
|
|
1840
|
-
|
|
1877
|
+
const websocketSubProtocol =
|
|
1841
1878
|
(opts.protocolId === 'MQIsdp') && (opts.protocolVersion === 3)
|
|
1842
1879
|
? 'mqttv3.1'
|
|
1843
1880
|
: 'mqtt'
|
|
1844
1881
|
|
|
1845
|
-
setDefaultOpts(opts)
|
|
1846
|
-
var url = buildUrl(opts, client)
|
|
1847
1882
|
debug('creating new Websocket for url: ' + url + ' and protocol: ' + websocketSubProtocol)
|
|
1848
|
-
|
|
1849
|
-
|
|
1850
|
-
|
|
1851
|
-
|
|
1883
|
+
let socket = new WS(url, [websocketSubProtocol], opts.wsOptions)
|
|
1884
|
+
return socket
|
|
1885
|
+
}
|
|
1886
|
+
|
|
1887
|
+
function createBrowserWebSocket (client, opts) {
|
|
1888
|
+
const websocketSubProtocol =
|
|
1889
|
+
(opts.protocolId === 'MQIsdp') && (opts.protocolVersion === 3)
|
|
1890
|
+
? 'mqttv3.1'
|
|
1891
|
+
: 'mqtt'
|
|
1892
|
+
|
|
1893
|
+
let url = buildUrl(opts, client)
|
|
1894
|
+
/* global WebSocket */
|
|
1895
|
+
let socket = new WebSocket(url, [websocketSubProtocol])
|
|
1896
|
+
socket.binaryType = 'arraybuffer'
|
|
1897
|
+
return socket
|
|
1852
1898
|
}
|
|
1853
1899
|
|
|
1854
1900
|
function streamBuilder (client, opts) {
|
|
1855
|
-
|
|
1901
|
+
debug('streamBuilder')
|
|
1902
|
+
let options = setDefaultOpts(opts)
|
|
1903
|
+
const url = buildUrl(options, client)
|
|
1904
|
+
let socket = createWebSocket(client, url, options)
|
|
1905
|
+
let webSocketStream = WS.createWebSocketStream(socket, options.wsOptions)
|
|
1906
|
+
webSocketStream.url = url
|
|
1907
|
+
return webSocketStream
|
|
1856
1908
|
}
|
|
1857
1909
|
|
|
1858
1910
|
function browserStreamBuilder (client, opts) {
|
|
1859
1911
|
debug('browserStreamBuilder')
|
|
1860
|
-
|
|
1861
|
-
|
|
1912
|
+
let stream
|
|
1913
|
+
let options = setDefaultBrowserOpts(opts)
|
|
1914
|
+
// sets the maximum socket buffer size before throttling
|
|
1915
|
+
const bufferSize = options.browserBufferSize || 1024 * 512
|
|
1916
|
+
|
|
1917
|
+
const bufferTimeout = opts.browserBufferTimeout || 1000
|
|
1918
|
+
|
|
1919
|
+
const coerceToBuffer = !opts.objectMode
|
|
1920
|
+
|
|
1921
|
+
let socket = createBrowserWebSocket(client, opts)
|
|
1922
|
+
|
|
1923
|
+
let proxy = buildProxy(opts, socketWriteBrowser, socketEndBrowser)
|
|
1924
|
+
|
|
1925
|
+
if (!opts.objectMode) {
|
|
1926
|
+
proxy._writev = writev
|
|
1862
1927
|
}
|
|
1928
|
+
proxy.on('close', () => { socket.close() })
|
|
1863
1929
|
|
|
1864
|
-
|
|
1865
|
-
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
|
|
1930
|
+
const eventListenerSupport = (typeof socket.addEventListener === 'undefined')
|
|
1931
|
+
|
|
1932
|
+
// was already open when passed in
|
|
1933
|
+
if (socket.readyState === socket.OPEN) {
|
|
1934
|
+
stream = proxy
|
|
1935
|
+
} else {
|
|
1936
|
+
stream = stream = duplexify(undefined, undefined, opts)
|
|
1937
|
+
if (!opts.objectMode) {
|
|
1938
|
+
stream._writev = writev
|
|
1870
1939
|
}
|
|
1871
|
-
var parsed = urlModule.parse(document.URL)
|
|
1872
|
-
opts.hostname = parsed.hostname
|
|
1873
1940
|
|
|
1874
|
-
if (
|
|
1875
|
-
|
|
1941
|
+
if (eventListenerSupport) {
|
|
1942
|
+
socket.addEventListener('open', onopen)
|
|
1943
|
+
} else {
|
|
1944
|
+
socket.onopen = onopen
|
|
1876
1945
|
}
|
|
1877
1946
|
}
|
|
1878
|
-
|
|
1947
|
+
|
|
1948
|
+
stream.socket = socket
|
|
1949
|
+
|
|
1950
|
+
if (eventListenerSupport) {
|
|
1951
|
+
socket.addEventListener('close', onclose)
|
|
1952
|
+
socket.addEventListener('error', onerror)
|
|
1953
|
+
socket.addEventListener('message', onmessage)
|
|
1954
|
+
} else {
|
|
1955
|
+
socket.onclose = onclose
|
|
1956
|
+
socket.onerror = onerror
|
|
1957
|
+
socket.onmessage = onmessage
|
|
1958
|
+
}
|
|
1959
|
+
|
|
1960
|
+
// methods for browserStreamBuilder
|
|
1961
|
+
|
|
1962
|
+
function buildProxy (options, socketWrite, socketEnd) {
|
|
1963
|
+
let proxy = new Transform({
|
|
1964
|
+
objectModeMode: options.objectMode
|
|
1965
|
+
})
|
|
1966
|
+
|
|
1967
|
+
proxy._write = socketWrite
|
|
1968
|
+
proxy._flush = socketEnd
|
|
1969
|
+
|
|
1970
|
+
return proxy
|
|
1971
|
+
}
|
|
1972
|
+
|
|
1973
|
+
function onopen () {
|
|
1974
|
+
stream.setReadable(proxy)
|
|
1975
|
+
stream.setWritable(proxy)
|
|
1976
|
+
stream.emit('connect')
|
|
1977
|
+
}
|
|
1978
|
+
|
|
1979
|
+
function onclose () {
|
|
1980
|
+
stream.end()
|
|
1981
|
+
stream.destroy()
|
|
1982
|
+
}
|
|
1983
|
+
|
|
1984
|
+
function onerror (err) {
|
|
1985
|
+
stream.destroy(err)
|
|
1986
|
+
}
|
|
1987
|
+
|
|
1988
|
+
function onmessage (event) {
|
|
1989
|
+
let data = event.data
|
|
1990
|
+
if (data instanceof ArrayBuffer) data = Buffer.from(data)
|
|
1991
|
+
else data = Buffer.from(data, 'utf8')
|
|
1992
|
+
proxy.push(data)
|
|
1993
|
+
}
|
|
1994
|
+
|
|
1995
|
+
// this is to be enabled only if objectMode is false
|
|
1996
|
+
function writev (chunks, cb) {
|
|
1997
|
+
const buffers = new Array(chunks.length)
|
|
1998
|
+
for (let i = 0; i < chunks.length; i++) {
|
|
1999
|
+
if (typeof chunks[i].chunk === 'string') {
|
|
2000
|
+
buffers[i] = Buffer.from(chunks[i], 'utf8')
|
|
2001
|
+
} else {
|
|
2002
|
+
buffers[i] = chunks[i].chunk
|
|
2003
|
+
}
|
|
2004
|
+
}
|
|
2005
|
+
|
|
2006
|
+
this._write(Buffer.concat(buffers), 'binary', cb)
|
|
2007
|
+
}
|
|
2008
|
+
|
|
2009
|
+
function socketWriteBrowser (chunk, enc, next) {
|
|
2010
|
+
if (socket.bufferedAmount > bufferSize) {
|
|
2011
|
+
// throttle data until buffered amount is reduced.
|
|
2012
|
+
setTimeout(socketWriteBrowser, bufferTimeout, chunk, enc, next)
|
|
2013
|
+
}
|
|
2014
|
+
|
|
2015
|
+
if (coerceToBuffer && typeof chunk === 'string') {
|
|
2016
|
+
chunk = Buffer.from(chunk, 'utf8')
|
|
2017
|
+
}
|
|
2018
|
+
|
|
2019
|
+
try {
|
|
2020
|
+
socket.send(chunk)
|
|
2021
|
+
} catch (err) {
|
|
2022
|
+
return next(err)
|
|
2023
|
+
}
|
|
2024
|
+
|
|
2025
|
+
next()
|
|
2026
|
+
}
|
|
2027
|
+
|
|
2028
|
+
function socketEndBrowser (done) {
|
|
2029
|
+
socket.close()
|
|
2030
|
+
done()
|
|
2031
|
+
}
|
|
2032
|
+
|
|
2033
|
+
// end methods for browserStreamBuilder
|
|
2034
|
+
|
|
2035
|
+
return stream
|
|
1879
2036
|
}
|
|
1880
2037
|
|
|
1881
2038
|
if (IS_BROWSER) {
|
|
@@ -1885,7 +2042,7 @@ if (IS_BROWSER) {
|
|
|
1885
2042
|
}
|
|
1886
2043
|
|
|
1887
2044
|
}).call(this,require('_process'))
|
|
1888
|
-
},{"_process":101,"debug":17,"url":132,"ws":139}],6:[function(require,module,exports){
|
|
2045
|
+
},{"_process":101,"debug":17,"duplexify":19,"readable-stream":115,"safe-buffer":117,"url":132,"ws":139}],6:[function(require,module,exports){
|
|
1889
2046
|
(function (Buffer){
|
|
1890
2047
|
'use strict'
|
|
1891
2048
|
|