mqtt 5.0.0-beta.2 → 5.0.0-beta.4
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/README.md +48 -28
- package/build/package.json +162 -0
- package/build/src/bin/mqtt.d.ts +2 -0
- package/build/src/bin/mqtt.js +27 -0
- package/build/src/bin/mqtt.js.map +1 -0
- package/build/src/bin/pub.d.ts +2 -0
- package/build/src/bin/pub.js +146 -0
- package/build/src/bin/pub.js.map +1 -0
- package/build/src/bin/sub.d.ts +2 -0
- package/build/src/bin/sub.js +121 -0
- package/build/src/bin/sub.js.map +1 -0
- package/build/src/lib/TypedEmitter.d.ts +11 -0
- package/build/src/lib/TypedEmitter.js +13 -0
- package/build/src/lib/TypedEmitter.js.map +1 -0
- package/build/src/lib/client.d.ts +266 -0
- package/build/src/lib/client.js +1147 -0
- package/build/src/lib/client.js.map +1 -0
- package/build/src/lib/connect/ali.d.ts +3 -0
- package/build/src/lib/connect/ali.js +113 -0
- package/build/src/lib/connect/ali.js.map +1 -0
- package/build/src/lib/connect/index.d.ts +5 -0
- package/build/src/lib/connect/index.js +134 -0
- package/build/src/lib/connect/index.js.map +1 -0
- package/build/src/lib/connect/tcp.d.ts +3 -0
- package/build/src/lib/connect/tcp.js +18 -0
- package/build/src/lib/connect/tcp.js.map +1 -0
- package/build/src/lib/connect/tls.d.ts +3 -0
- package/build/src/lib/connect/tls.js +38 -0
- package/build/src/lib/connect/tls.js.map +1 -0
- package/build/src/lib/connect/ws.d.ts +3 -0
- package/build/src/lib/connect/ws.js +212 -0
- package/build/src/lib/connect/ws.js.map +1 -0
- package/build/src/lib/connect/wx.d.ts +3 -0
- package/build/src/lib/connect/wx.js +117 -0
- package/build/src/lib/connect/wx.js.map +1 -0
- package/build/src/lib/default-message-id-provider.d.ts +16 -0
- package/build/src/lib/default-message-id-provider.js +24 -0
- package/build/src/lib/default-message-id-provider.js.map +1 -0
- package/build/src/lib/handlers/ack.d.ts +48 -0
- package/build/src/lib/handlers/ack.js +129 -0
- package/build/src/lib/handlers/ack.js.map +1 -0
- package/build/src/lib/handlers/auth.d.ts +3 -0
- package/build/src/lib/handlers/auth.js +30 -0
- package/build/src/lib/handlers/auth.js.map +1 -0
- package/build/src/lib/handlers/connack.d.ts +3 -0
- package/build/src/lib/handlers/connack.js +48 -0
- package/build/src/lib/handlers/connack.js.map +1 -0
- package/build/src/lib/handlers/index.d.ts +3 -0
- package/build/src/lib/handlers/index.js +64 -0
- package/build/src/lib/handlers/index.js.map +1 -0
- package/build/src/lib/handlers/publish.d.ts +3 -0
- package/build/src/lib/handlers/publish.js +106 -0
- package/build/src/lib/handlers/publish.js.map +1 -0
- package/build/src/lib/handlers/pubrel.d.ts +3 -0
- package/build/src/lib/handlers/pubrel.js +25 -0
- package/build/src/lib/handlers/pubrel.js.map +1 -0
- package/build/src/lib/is-browser.d.ts +2 -0
- package/build/src/lib/is-browser.js +8 -0
- package/build/src/lib/is-browser.js.map +1 -0
- package/build/src/lib/shared.d.ts +17 -0
- package/build/src/lib/shared.js +35 -0
- package/build/src/lib/shared.js.map +1 -0
- package/build/src/lib/store.d.ts +24 -0
- package/build/src/lib/store.js +80 -0
- package/build/src/lib/store.js.map +1 -0
- package/build/src/lib/topic-alias-recv.d.ts +9 -0
- package/build/src/lib/topic-alias-recv.js +24 -0
- package/build/src/lib/topic-alias-recv.js.map +1 -0
- package/build/src/lib/topic-alias-send.d.ts +13 -0
- package/build/src/lib/topic-alias-send.js +56 -0
- package/build/src/lib/topic-alias-send.js.map +1 -0
- package/build/src/lib/unique-message-id-provider.d.ts +10 -0
- package/build/src/lib/unique-message-id-provider.js +26 -0
- package/build/src/lib/unique-message-id-provider.js.map +1 -0
- package/build/src/lib/validations.d.ts +2 -0
- package/build/src/lib/validations.js +32 -0
- package/build/src/lib/validations.js.map +1 -0
- package/build/src/mqtt.d.ts +8 -0
- package/build/src/mqtt.js +33 -0
- package/build/src/mqtt.js.map +1 -0
- package/build/tsconfig.build.tsbuildinfo +1 -0
- package/dist/mqtt.js +4987 -5249
- package/dist/mqtt.min.js +4 -4
- package/package.json +84 -66
- package/src/bin/mqtt.ts +32 -0
- package/src/bin/pub.ts +171 -0
- package/src/bin/sub.ts +146 -0
- package/src/lib/TypedEmitter.ts +45 -0
- package/src/lib/client.ts +2279 -0
- package/src/lib/connect/ali.ts +128 -0
- package/src/lib/connect/index.ts +180 -0
- package/src/lib/connect/tcp.ts +23 -0
- package/src/lib/connect/tls.ts +56 -0
- package/src/lib/connect/ws.ts +282 -0
- package/src/lib/connect/wx.ts +141 -0
- package/src/lib/default-message-id-provider.ts +99 -0
- package/src/lib/handlers/ack.ts +151 -0
- package/src/lib/handlers/auth.ts +44 -0
- package/src/lib/handlers/connack.ts +55 -0
- package/src/lib/handlers/index.ts +67 -0
- package/src/lib/handlers/publish.ts +183 -0
- package/src/lib/handlers/pubrel.ts +27 -0
- package/src/lib/is-browser.ts +12 -0
- package/src/lib/shared.ts +73 -0
- package/src/lib/store.ts +166 -0
- package/src/lib/topic-alias-recv.ts +48 -0
- package/src/lib/topic-alias-send.ts +96 -0
- package/src/lib/unique-message-id-provider.ts +63 -0
- package/src/lib/validations.ts +46 -0
- package/src/mqtt.ts +23 -0
- package/bin/mqtt.js +0 -27
- package/bin/pub.js +0 -146
- package/bin/sub.js +0 -123
- package/lib/client.js +0 -1918
- package/lib/connect/ali.js +0 -129
- package/lib/connect/index.js +0 -169
- package/lib/connect/tcp.js +0 -20
- package/lib/connect/tls.js +0 -48
- package/lib/connect/ws.js +0 -257
- package/lib/connect/wx.js +0 -133
- package/lib/default-message-id-provider.js +0 -69
- package/lib/is-browser.js +0 -11
- package/lib/store.js +0 -128
- package/lib/topic-alias-recv.js +0 -47
- package/lib/topic-alias-send.js +0 -91
- package/lib/unique-message-id-provider.js +0 -65
- package/lib/validations.js +0 -52
- package/mqtt.js +0 -21
- package/types/index.d.ts +0 -30
- package/types/lib/client-options.d.ts +0 -208
- package/types/lib/client.d.ts +0 -261
- package/types/lib/connect/index.d.ts +0 -19
- package/types/lib/default-message-id-provider.d.ts +0 -49
- package/types/lib/message-id-provider.d.ts +0 -40
- package/types/lib/store-options.d.ts +0 -6
- package/types/lib/store.d.ts +0 -46
- package/types/lib/unique-message-id-provider.d.ts +0 -48
package/lib/connect/ali.js
DELETED
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const { Buffer } = require('buffer')
|
|
4
|
-
const Transform = require('readable-stream').Transform
|
|
5
|
-
const duplexify = require('duplexify')
|
|
6
|
-
|
|
7
|
-
/* global FileReader */
|
|
8
|
-
let my
|
|
9
|
-
let proxy
|
|
10
|
-
let stream
|
|
11
|
-
let isInitialized = false
|
|
12
|
-
|
|
13
|
-
function buildProxy () {
|
|
14
|
-
const proxy = new Transform()
|
|
15
|
-
proxy._write = function (chunk, encoding, next) {
|
|
16
|
-
my.sendSocketMessage({
|
|
17
|
-
data: chunk.buffer,
|
|
18
|
-
success: function () {
|
|
19
|
-
next()
|
|
20
|
-
},
|
|
21
|
-
fail: function () {
|
|
22
|
-
next(new Error())
|
|
23
|
-
}
|
|
24
|
-
})
|
|
25
|
-
}
|
|
26
|
-
proxy._flush = function socketEnd (done) {
|
|
27
|
-
my.closeSocket({
|
|
28
|
-
success: function () {
|
|
29
|
-
done()
|
|
30
|
-
}
|
|
31
|
-
})
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
return proxy
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function setDefaultOpts (opts) {
|
|
38
|
-
if (!opts.hostname) {
|
|
39
|
-
opts.hostname = 'localhost'
|
|
40
|
-
}
|
|
41
|
-
if (!opts.path) {
|
|
42
|
-
opts.path = '/'
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
if (!opts.wsOptions) {
|
|
46
|
-
opts.wsOptions = {}
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
function buildUrl (opts, client) {
|
|
51
|
-
const protocol = opts.protocol === 'alis' ? 'wss' : 'ws'
|
|
52
|
-
let url = protocol + '://' + opts.hostname + opts.path
|
|
53
|
-
if (opts.port && opts.port !== 80 && opts.port !== 443) {
|
|
54
|
-
url = protocol + '://' + opts.hostname + ':' + opts.port + opts.path
|
|
55
|
-
}
|
|
56
|
-
if (typeof (opts.transformWsUrl) === 'function') {
|
|
57
|
-
url = opts.transformWsUrl(url, opts, client)
|
|
58
|
-
}
|
|
59
|
-
return url
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
function bindEventHandler () {
|
|
63
|
-
if (isInitialized) return
|
|
64
|
-
|
|
65
|
-
isInitialized = true
|
|
66
|
-
|
|
67
|
-
my.onSocketOpen(function () {
|
|
68
|
-
stream.setReadable(proxy)
|
|
69
|
-
stream.setWritable(proxy)
|
|
70
|
-
stream.emit('connect')
|
|
71
|
-
})
|
|
72
|
-
|
|
73
|
-
my.onSocketMessage(function (res) {
|
|
74
|
-
if (typeof res.data === 'string') {
|
|
75
|
-
const buffer = Buffer.from(res.data, 'base64')
|
|
76
|
-
proxy.push(buffer)
|
|
77
|
-
} else {
|
|
78
|
-
const reader = new FileReader()
|
|
79
|
-
reader.addEventListener('load', function () {
|
|
80
|
-
let data = reader.result
|
|
81
|
-
|
|
82
|
-
if (data instanceof ArrayBuffer) data = Buffer.from(data)
|
|
83
|
-
else data = Buffer.from(data, 'utf8')
|
|
84
|
-
proxy.push(data)
|
|
85
|
-
})
|
|
86
|
-
reader.readAsArrayBuffer(res.data)
|
|
87
|
-
}
|
|
88
|
-
})
|
|
89
|
-
|
|
90
|
-
my.onSocketClose(function () {
|
|
91
|
-
stream.end()
|
|
92
|
-
stream.destroy()
|
|
93
|
-
})
|
|
94
|
-
|
|
95
|
-
my.onSocketError(function (res) {
|
|
96
|
-
stream.destroy(res)
|
|
97
|
-
})
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
function buildStream (client, opts) {
|
|
101
|
-
opts.hostname = opts.hostname || opts.host
|
|
102
|
-
|
|
103
|
-
if (!opts.hostname) {
|
|
104
|
-
throw new Error('Could not determine host. Specify host manually.')
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
const websocketSubProtocol =
|
|
108
|
-
(opts.protocolId === 'MQIsdp') && (opts.protocolVersion === 3)
|
|
109
|
-
? 'mqttv3.1'
|
|
110
|
-
: 'mqtt'
|
|
111
|
-
|
|
112
|
-
setDefaultOpts(opts)
|
|
113
|
-
|
|
114
|
-
const url = buildUrl(opts, client)
|
|
115
|
-
my = opts.my
|
|
116
|
-
my.connectSocket({
|
|
117
|
-
url: url,
|
|
118
|
-
protocols: websocketSubProtocol
|
|
119
|
-
})
|
|
120
|
-
|
|
121
|
-
proxy = buildProxy()
|
|
122
|
-
stream = duplexify.obj()
|
|
123
|
-
|
|
124
|
-
bindEventHandler()
|
|
125
|
-
|
|
126
|
-
return stream
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
module.exports = buildStream
|
package/lib/connect/index.js
DELETED
|
@@ -1,169 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const MqttClient = require('../client')
|
|
4
|
-
const Store = require('../store')
|
|
5
|
-
const DefaultMessageIdProvider = require('../default-message-id-provider')
|
|
6
|
-
const UniqueMessageIdProvider = require('../unique-message-id-provider')
|
|
7
|
-
const IS_BROWSER = require('../is-browser').IS_BROWSER
|
|
8
|
-
const url = require('url')
|
|
9
|
-
const xtend = require('xtend')
|
|
10
|
-
const debug = require('debug')('mqttjs')
|
|
11
|
-
|
|
12
|
-
const protocols = {}
|
|
13
|
-
|
|
14
|
-
if (!IS_BROWSER) {
|
|
15
|
-
protocols.mqtt = require('./tcp')
|
|
16
|
-
protocols.tcp = require('./tcp')
|
|
17
|
-
protocols.ssl = require('./tls')
|
|
18
|
-
protocols.tls = require('./tls')
|
|
19
|
-
protocols.mqtts = require('./tls')
|
|
20
|
-
} else {
|
|
21
|
-
protocols.wx = require('./wx')
|
|
22
|
-
protocols.wxs = require('./wx')
|
|
23
|
-
|
|
24
|
-
protocols.ali = require('./ali')
|
|
25
|
-
protocols.alis = require('./ali')
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
protocols.ws = require('./ws')
|
|
29
|
-
protocols.wss = require('./ws')
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Parse the auth attribute and merge username and password in the options object.
|
|
33
|
-
*
|
|
34
|
-
* @param {Object} [opts] option object
|
|
35
|
-
*/
|
|
36
|
-
function parseAuthOptions (opts) {
|
|
37
|
-
let matches
|
|
38
|
-
if (opts.auth) {
|
|
39
|
-
matches = opts.auth.match(/^(.+):(.+)$/)
|
|
40
|
-
if (matches) {
|
|
41
|
-
opts.username = matches[1]
|
|
42
|
-
opts.password = matches[2]
|
|
43
|
-
} else {
|
|
44
|
-
opts.username = opts.auth
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* connect - connect to an MQTT broker.
|
|
51
|
-
*
|
|
52
|
-
* @param {String} [brokerUrl] - url of the broker, optional
|
|
53
|
-
* @param {Object} opts - see MqttClient#constructor
|
|
54
|
-
*/
|
|
55
|
-
function connect (brokerUrl, opts) {
|
|
56
|
-
debug('connecting to an MQTT broker...')
|
|
57
|
-
if ((typeof brokerUrl === 'object') && !opts) {
|
|
58
|
-
opts = brokerUrl
|
|
59
|
-
brokerUrl = null
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
opts = opts || {}
|
|
63
|
-
|
|
64
|
-
if (brokerUrl) {
|
|
65
|
-
// eslint-disable-next-line
|
|
66
|
-
const parsed = url.parse(brokerUrl, true)
|
|
67
|
-
if (parsed.port != null) {
|
|
68
|
-
parsed.port = Number(parsed.port)
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
opts = xtend(parsed, opts)
|
|
72
|
-
|
|
73
|
-
if (opts.protocol === null) {
|
|
74
|
-
throw new Error('Missing protocol')
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
opts.protocol = opts.protocol.replace(/:$/, '')
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// merge in the auth options if supplied
|
|
81
|
-
parseAuthOptions(opts)
|
|
82
|
-
|
|
83
|
-
// support clientId passed in the query string of the url
|
|
84
|
-
if (opts.query && typeof opts.query.clientId === 'string') {
|
|
85
|
-
opts.clientId = opts.query.clientId
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
if (opts.cert && opts.key) {
|
|
89
|
-
if (opts.protocol) {
|
|
90
|
-
if (['mqtts', 'wss', 'wxs', 'alis'].indexOf(opts.protocol) === -1) {
|
|
91
|
-
switch (opts.protocol) {
|
|
92
|
-
case 'mqtt':
|
|
93
|
-
opts.protocol = 'mqtts'
|
|
94
|
-
break
|
|
95
|
-
case 'ws':
|
|
96
|
-
opts.protocol = 'wss'
|
|
97
|
-
break
|
|
98
|
-
case 'wx':
|
|
99
|
-
opts.protocol = 'wxs'
|
|
100
|
-
break
|
|
101
|
-
case 'ali':
|
|
102
|
-
opts.protocol = 'alis'
|
|
103
|
-
break
|
|
104
|
-
default:
|
|
105
|
-
throw new Error('Unknown protocol for secure connection: "' + opts.protocol + '"!')
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
} else {
|
|
109
|
-
// A cert and key was provided, however no protocol was specified, so we will throw an error.
|
|
110
|
-
throw new Error('Missing secure protocol key')
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
if (!protocols[opts.protocol]) {
|
|
115
|
-
const isSecure = ['mqtts', 'wss'].indexOf(opts.protocol) !== -1
|
|
116
|
-
opts.protocol = [
|
|
117
|
-
'mqtt',
|
|
118
|
-
'mqtts',
|
|
119
|
-
'ws',
|
|
120
|
-
'wss',
|
|
121
|
-
'wx',
|
|
122
|
-
'wxs',
|
|
123
|
-
'ali',
|
|
124
|
-
'alis'
|
|
125
|
-
].filter(function (key, index) {
|
|
126
|
-
if (isSecure && index % 2 === 0) {
|
|
127
|
-
// Skip insecure protocols when requesting a secure one.
|
|
128
|
-
return false
|
|
129
|
-
}
|
|
130
|
-
return (typeof protocols[key] === 'function')
|
|
131
|
-
})[0]
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
if (opts.clean === false && !opts.clientId) {
|
|
135
|
-
throw new Error('Missing clientId for unclean clients')
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
if (opts.protocol) {
|
|
139
|
-
opts.defaultProtocol = opts.protocol
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
function wrapper (client) {
|
|
143
|
-
if (opts.servers) {
|
|
144
|
-
if (!client._reconnectCount || client._reconnectCount === opts.servers.length) {
|
|
145
|
-
client._reconnectCount = 0
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
opts.host = opts.servers[client._reconnectCount].host
|
|
149
|
-
opts.port = opts.servers[client._reconnectCount].port
|
|
150
|
-
opts.protocol = (!opts.servers[client._reconnectCount].protocol ? opts.defaultProtocol : opts.servers[client._reconnectCount].protocol)
|
|
151
|
-
opts.hostname = opts.host
|
|
152
|
-
|
|
153
|
-
client._reconnectCount++
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
debug('calling streambuilder for', opts.protocol)
|
|
157
|
-
return protocols[opts.protocol](client, opts)
|
|
158
|
-
}
|
|
159
|
-
const client = new MqttClient(wrapper, opts)
|
|
160
|
-
client.on('error', function () { /* Automatically set up client error handling */ })
|
|
161
|
-
return client
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
module.exports = connect
|
|
165
|
-
module.exports.connect = connect
|
|
166
|
-
module.exports.MqttClient = MqttClient
|
|
167
|
-
module.exports.Store = Store
|
|
168
|
-
module.exports.DefaultMessageIdProvider = DefaultMessageIdProvider
|
|
169
|
-
module.exports.UniqueMessageIdProvider = UniqueMessageIdProvider
|
package/lib/connect/tcp.js
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
const net = require('net')
|
|
3
|
-
const debug = require('debug')('mqttjs:tcp')
|
|
4
|
-
|
|
5
|
-
/*
|
|
6
|
-
variables port and host can be removed since
|
|
7
|
-
you have all required information in opts object
|
|
8
|
-
*/
|
|
9
|
-
function streamBuilder (client, opts) {
|
|
10
|
-
opts.port = opts.port || 1883
|
|
11
|
-
opts.hostname = opts.hostname || opts.host || 'localhost'
|
|
12
|
-
|
|
13
|
-
const port = opts.port
|
|
14
|
-
const host = opts.hostname
|
|
15
|
-
|
|
16
|
-
debug('port %d and host %s', port, host)
|
|
17
|
-
return net.createConnection(port, host)
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
module.exports = streamBuilder
|
package/lib/connect/tls.js
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,257 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const { Buffer } = require('buffer')
|
|
4
|
-
const WS = require('ws')
|
|
5
|
-
const debug = require('debug')('mqttjs:ws')
|
|
6
|
-
const duplexify = require('duplexify')
|
|
7
|
-
const Transform = require('readable-stream').Transform
|
|
8
|
-
const IS_BROWSER = require('../is-browser').IS_BROWSER
|
|
9
|
-
|
|
10
|
-
const WSS_OPTIONS = [
|
|
11
|
-
'rejectUnauthorized',
|
|
12
|
-
'ca',
|
|
13
|
-
'cert',
|
|
14
|
-
'key',
|
|
15
|
-
'pfx',
|
|
16
|
-
'passphrase'
|
|
17
|
-
]
|
|
18
|
-
|
|
19
|
-
function buildUrl (opts, client) {
|
|
20
|
-
let url = opts.protocol + '://' + opts.hostname + ':' + opts.port + opts.path
|
|
21
|
-
if (typeof (opts.transformWsUrl) === 'function') {
|
|
22
|
-
url = opts.transformWsUrl(url, opts, client)
|
|
23
|
-
}
|
|
24
|
-
return url
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function setDefaultOpts (opts) {
|
|
28
|
-
const options = opts
|
|
29
|
-
if (!opts.hostname) {
|
|
30
|
-
options.hostname = 'localhost'
|
|
31
|
-
}
|
|
32
|
-
if (!opts.port) {
|
|
33
|
-
if (opts.protocol === 'wss') {
|
|
34
|
-
options.port = 443
|
|
35
|
-
} else {
|
|
36
|
-
options.port = 80
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
if (!opts.path) {
|
|
40
|
-
options.path = '/'
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
if (!opts.wsOptions) {
|
|
44
|
-
options.wsOptions = {}
|
|
45
|
-
}
|
|
46
|
-
if (!IS_BROWSER && opts.protocol === 'wss') {
|
|
47
|
-
// Add cert/key/ca etc options
|
|
48
|
-
WSS_OPTIONS.forEach(function (prop) {
|
|
49
|
-
if (Object.prototype.hasOwnProperty.call(opts, prop) && !Object.prototype.hasOwnProperty.call(opts.wsOptions, prop)) {
|
|
50
|
-
options.wsOptions[prop] = opts[prop]
|
|
51
|
-
}
|
|
52
|
-
})
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
return options
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function setDefaultBrowserOpts (opts) {
|
|
59
|
-
const options = setDefaultOpts(opts)
|
|
60
|
-
|
|
61
|
-
if (!options.hostname) {
|
|
62
|
-
options.hostname = options.host
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
if (!options.hostname) {
|
|
66
|
-
// Throwing an error in a Web Worker if no `hostname` is given, because we
|
|
67
|
-
// can not determine the `hostname` automatically. If connecting to
|
|
68
|
-
// localhost, please supply the `hostname` as an argument.
|
|
69
|
-
if (typeof (document) === 'undefined') {
|
|
70
|
-
throw new Error('Could not determine host. Specify host manually.')
|
|
71
|
-
}
|
|
72
|
-
const parsed = new URL(document.URL)
|
|
73
|
-
options.hostname = parsed.hostname
|
|
74
|
-
|
|
75
|
-
if (!options.port) {
|
|
76
|
-
options.port = parsed.port
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// objectMode should be defined for logic
|
|
81
|
-
if (options.objectMode === undefined) {
|
|
82
|
-
options.objectMode = !(options.binary === true || options.binary === undefined)
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
return options
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
function createWebSocket (client, url, opts) {
|
|
89
|
-
debug('createWebSocket')
|
|
90
|
-
debug('protocol: ' + opts.protocolId + ' ' + opts.protocolVersion)
|
|
91
|
-
const websocketSubProtocol =
|
|
92
|
-
(opts.protocolId === 'MQIsdp') && (opts.protocolVersion === 3)
|
|
93
|
-
? 'mqttv3.1'
|
|
94
|
-
: 'mqtt'
|
|
95
|
-
|
|
96
|
-
debug('creating new Websocket for url: ' + url + ' and protocol: ' + websocketSubProtocol)
|
|
97
|
-
const socket = new WS(url, [websocketSubProtocol], opts.wsOptions)
|
|
98
|
-
return socket
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
function createBrowserWebSocket (client, opts) {
|
|
102
|
-
const websocketSubProtocol =
|
|
103
|
-
(opts.protocolId === 'MQIsdp') && (opts.protocolVersion === 3)
|
|
104
|
-
? 'mqttv3.1'
|
|
105
|
-
: 'mqtt'
|
|
106
|
-
|
|
107
|
-
const url = buildUrl(opts, client)
|
|
108
|
-
/* global WebSocket */
|
|
109
|
-
const socket = new WebSocket(url, [websocketSubProtocol])
|
|
110
|
-
socket.binaryType = 'arraybuffer'
|
|
111
|
-
return socket
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
function streamBuilder (client, opts) {
|
|
115
|
-
debug('streamBuilder')
|
|
116
|
-
const options = setDefaultOpts(opts)
|
|
117
|
-
const url = buildUrl(options, client)
|
|
118
|
-
const socket = createWebSocket(client, url, options)
|
|
119
|
-
const webSocketStream = WS.createWebSocketStream(socket, options.wsOptions)
|
|
120
|
-
webSocketStream.url = url
|
|
121
|
-
socket.on('close', () => { webSocketStream.destroy() })
|
|
122
|
-
return webSocketStream
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
function browserStreamBuilder (client, opts) {
|
|
126
|
-
debug('browserStreamBuilder')
|
|
127
|
-
let stream
|
|
128
|
-
const 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
|
-
const socket = createBrowserWebSocket(client, opts)
|
|
137
|
-
|
|
138
|
-
const proxy = buildProxy(opts, socketWriteBrowser, socketEndBrowser)
|
|
139
|
-
|
|
140
|
-
if (!opts.objectMode) {
|
|
141
|
-
proxy._writev = writev
|
|
142
|
-
}
|
|
143
|
-
proxy.on('close', () => { socket.close() })
|
|
144
|
-
|
|
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
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
if (eventListenerSupport) {
|
|
157
|
-
socket.addEventListener('open', onopen)
|
|
158
|
-
} else {
|
|
159
|
-
socket.onopen = onopen
|
|
160
|
-
}
|
|
161
|
-
}
|
|
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
|
-
const 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
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
if (IS_BROWSER) {
|
|
254
|
-
module.exports = browserStreamBuilder
|
|
255
|
-
} else {
|
|
256
|
-
module.exports = streamBuilder
|
|
257
|
-
}
|