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
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { Buffer } from 'buffer'
|
|
2
|
+
import { Duplex, Transform } from 'readable-stream'
|
|
3
|
+
import duplexify, { Duplexify } from 'duplexify'
|
|
4
|
+
import { StreamBuilder } from '../shared'
|
|
5
|
+
import MqttClient, { IClientOptions } from '../client'
|
|
6
|
+
|
|
7
|
+
let my: any
|
|
8
|
+
let proxy: Transform
|
|
9
|
+
let stream: Duplexify
|
|
10
|
+
let isInitialized = false
|
|
11
|
+
|
|
12
|
+
function buildProxy() {
|
|
13
|
+
const _proxy = new Transform()
|
|
14
|
+
_proxy._write = (chunk, encoding, next) => {
|
|
15
|
+
my.sendSocketMessage({
|
|
16
|
+
data: chunk.buffer,
|
|
17
|
+
success() {
|
|
18
|
+
next()
|
|
19
|
+
},
|
|
20
|
+
fail() {
|
|
21
|
+
next(new Error())
|
|
22
|
+
},
|
|
23
|
+
})
|
|
24
|
+
}
|
|
25
|
+
_proxy._flush = (done) => {
|
|
26
|
+
my.closeSocket({
|
|
27
|
+
success() {
|
|
28
|
+
done()
|
|
29
|
+
},
|
|
30
|
+
})
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return _proxy
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function setDefaultOpts(opts: IClientOptions) {
|
|
37
|
+
if (!opts.hostname) {
|
|
38
|
+
opts.hostname = 'localhost'
|
|
39
|
+
}
|
|
40
|
+
if (!opts.path) {
|
|
41
|
+
opts.path = '/'
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (!opts.wsOptions) {
|
|
45
|
+
opts.wsOptions = {}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function buildUrl(opts: IClientOptions, client: MqttClient) {
|
|
50
|
+
const protocol = opts.protocol === 'alis' ? 'wss' : 'ws'
|
|
51
|
+
let url = `${protocol}://${opts.hostname}${opts.path}`
|
|
52
|
+
if (opts.port && opts.port !== 80 && opts.port !== 443) {
|
|
53
|
+
url = `${protocol}://${opts.hostname}:${opts.port}${opts.path}`
|
|
54
|
+
}
|
|
55
|
+
if (typeof opts.transformWsUrl === 'function') {
|
|
56
|
+
url = opts.transformWsUrl(url, opts, client)
|
|
57
|
+
}
|
|
58
|
+
return url
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function bindEventHandler() {
|
|
62
|
+
if (isInitialized) return
|
|
63
|
+
|
|
64
|
+
isInitialized = true
|
|
65
|
+
|
|
66
|
+
my.onSocketOpen(() => {
|
|
67
|
+
stream.setReadable(proxy)
|
|
68
|
+
stream.setWritable(proxy)
|
|
69
|
+
stream.emit('connect')
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
my.onSocketMessage((res) => {
|
|
73
|
+
if (typeof res.data === 'string') {
|
|
74
|
+
const buffer = Buffer.from(res.data, 'base64')
|
|
75
|
+
proxy.push(buffer)
|
|
76
|
+
} else {
|
|
77
|
+
const reader = new FileReader()
|
|
78
|
+
reader.addEventListener('load', () => {
|
|
79
|
+
let data = reader.result
|
|
80
|
+
|
|
81
|
+
if (data instanceof ArrayBuffer) data = Buffer.from(data)
|
|
82
|
+
else data = Buffer.from(data, 'utf8')
|
|
83
|
+
proxy.push(data)
|
|
84
|
+
})
|
|
85
|
+
reader.readAsArrayBuffer(res.data)
|
|
86
|
+
}
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
my.onSocketClose(() => {
|
|
90
|
+
stream.end()
|
|
91
|
+
stream.destroy()
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
my.onSocketError((res) => {
|
|
95
|
+
stream.destroy(res)
|
|
96
|
+
})
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const buildStream: StreamBuilder = (client, opts) => {
|
|
100
|
+
opts.hostname = opts.hostname || opts.host
|
|
101
|
+
|
|
102
|
+
if (!opts.hostname) {
|
|
103
|
+
throw new Error('Could not determine host. Specify host manually.')
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const websocketSubProtocol =
|
|
107
|
+
opts.protocolId === 'MQIsdp' && opts.protocolVersion === 3
|
|
108
|
+
? 'mqttv3.1'
|
|
109
|
+
: 'mqtt'
|
|
110
|
+
|
|
111
|
+
setDefaultOpts(opts)
|
|
112
|
+
|
|
113
|
+
const url = buildUrl(opts, client)
|
|
114
|
+
my = opts.my
|
|
115
|
+
my.connectSocket({
|
|
116
|
+
url,
|
|
117
|
+
protocols: websocketSubProtocol,
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
proxy = buildProxy()
|
|
121
|
+
stream = duplexify.obj()
|
|
122
|
+
|
|
123
|
+
bindEventHandler()
|
|
124
|
+
|
|
125
|
+
return stream as unknown as Duplex
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export default buildStream
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-var-requires */
|
|
2
|
+
import url from 'url'
|
|
3
|
+
import MqttClient, { IClientOptions, MqttProtocol } from '../client'
|
|
4
|
+
import IS_BROWSER from '../is-browser'
|
|
5
|
+
import Store from '../store'
|
|
6
|
+
import DefaultMessageIdProvider from '../default-message-id-provider'
|
|
7
|
+
import UniqueMessageIdProvider from '../unique-message-id-provider'
|
|
8
|
+
import _debug from 'debug'
|
|
9
|
+
import { StreamBuilder } from '../shared'
|
|
10
|
+
|
|
11
|
+
const debug = _debug('mqttjs')
|
|
12
|
+
|
|
13
|
+
const protocols: Record<string, StreamBuilder> = {}
|
|
14
|
+
|
|
15
|
+
if (!IS_BROWSER) {
|
|
16
|
+
protocols.mqtt = require('./tcp').default
|
|
17
|
+
protocols.tcp = require('./tcp').default
|
|
18
|
+
protocols.ssl = require('./tls').default
|
|
19
|
+
protocols.tls = protocols.ssl
|
|
20
|
+
protocols.mqtts = require('./tls').default
|
|
21
|
+
} else {
|
|
22
|
+
protocols.wx = require('./wx').default
|
|
23
|
+
protocols.wxs = require('./wx').default
|
|
24
|
+
|
|
25
|
+
protocols.ali = require('./ali').default
|
|
26
|
+
protocols.alis = require('./ali').default
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
protocols.ws = require('./ws').default
|
|
30
|
+
protocols.wss = require('./ws').default
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Parse the auth attribute and merge username and password in the options object.
|
|
34
|
+
*
|
|
35
|
+
* @param {Object} [opts] option object
|
|
36
|
+
*/
|
|
37
|
+
function parseAuthOptions(opts: IClientOptions) {
|
|
38
|
+
let matches: RegExpMatchArray | null
|
|
39
|
+
if (opts.auth) {
|
|
40
|
+
matches = opts.auth.match(/^(.+):(.+)$/)
|
|
41
|
+
if (matches) {
|
|
42
|
+
opts.username = matches[1]
|
|
43
|
+
opts.password = matches[2]
|
|
44
|
+
} else {
|
|
45
|
+
opts.username = opts.auth
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* connect - connect to an MQTT broker.
|
|
52
|
+
*/
|
|
53
|
+
function connect(brokerUrl: string): MqttClient
|
|
54
|
+
function connect(opts: IClientOptions): MqttClient
|
|
55
|
+
function connect(brokerUrl: string, opts?: IClientOptions): MqttClient
|
|
56
|
+
function connect(
|
|
57
|
+
brokerUrl: string | IClientOptions,
|
|
58
|
+
opts?: IClientOptions,
|
|
59
|
+
): MqttClient {
|
|
60
|
+
debug('connecting to an MQTT broker...')
|
|
61
|
+
if (typeof brokerUrl === 'object' && !opts) {
|
|
62
|
+
opts = brokerUrl
|
|
63
|
+
brokerUrl = ''
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
opts = opts || {}
|
|
67
|
+
|
|
68
|
+
if (brokerUrl && typeof brokerUrl === 'string') {
|
|
69
|
+
// eslint-disable-next-line
|
|
70
|
+
const parsed = url.parse(brokerUrl, true)
|
|
71
|
+
if (parsed.port != null) {
|
|
72
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
73
|
+
// @ts-ignore
|
|
74
|
+
parsed.port = Number(parsed.port)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
opts = { ...parsed, ...opts } as IClientOptions
|
|
78
|
+
|
|
79
|
+
if (opts.protocol === null) {
|
|
80
|
+
throw new Error('Missing protocol')
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
opts.protocol = opts.protocol.replace(/:$/, '') as MqttProtocol
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// merge in the auth options if supplied
|
|
87
|
+
parseAuthOptions(opts)
|
|
88
|
+
|
|
89
|
+
// support clientId passed in the query string of the url
|
|
90
|
+
if (opts.query && typeof opts.query.clientId === 'string') {
|
|
91
|
+
opts.clientId = opts.query.clientId
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (opts.cert && opts.key) {
|
|
95
|
+
if (opts.protocol) {
|
|
96
|
+
if (['mqtts', 'wss', 'wxs', 'alis'].indexOf(opts.protocol) === -1) {
|
|
97
|
+
switch (opts.protocol) {
|
|
98
|
+
case 'mqtt':
|
|
99
|
+
opts.protocol = 'mqtts'
|
|
100
|
+
break
|
|
101
|
+
case 'ws':
|
|
102
|
+
opts.protocol = 'wss'
|
|
103
|
+
break
|
|
104
|
+
case 'wx':
|
|
105
|
+
opts.protocol = 'wxs'
|
|
106
|
+
break
|
|
107
|
+
case 'ali':
|
|
108
|
+
opts.protocol = 'alis'
|
|
109
|
+
break
|
|
110
|
+
default:
|
|
111
|
+
throw new Error(
|
|
112
|
+
`Unknown protocol for secure connection: "${opts.protocol}"!`,
|
|
113
|
+
)
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
} else {
|
|
117
|
+
// A cert and key was provided, however no protocol was specified, so we will throw an error.
|
|
118
|
+
throw new Error('Missing secure protocol key')
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (!protocols[opts.protocol]) {
|
|
123
|
+
const isSecure = ['mqtts', 'wss'].indexOf(opts.protocol) !== -1
|
|
124
|
+
opts.protocol = [
|
|
125
|
+
'mqtt',
|
|
126
|
+
'mqtts',
|
|
127
|
+
'ws',
|
|
128
|
+
'wss',
|
|
129
|
+
'wx',
|
|
130
|
+
'wxs',
|
|
131
|
+
'ali',
|
|
132
|
+
'alis',
|
|
133
|
+
].filter((key, index) => {
|
|
134
|
+
if (isSecure && index % 2 === 0) {
|
|
135
|
+
// Skip insecure protocols when requesting a secure one.
|
|
136
|
+
return false
|
|
137
|
+
}
|
|
138
|
+
return typeof protocols[key] === 'function'
|
|
139
|
+
})[0] as MqttProtocol
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (opts.clean === false && !opts.clientId) {
|
|
143
|
+
throw new Error('Missing clientId for unclean clients')
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (opts.protocol) {
|
|
147
|
+
opts.defaultProtocol = opts.protocol
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function wrapper(client: MqttClient) {
|
|
151
|
+
if (opts.servers) {
|
|
152
|
+
if (
|
|
153
|
+
!client._reconnectCount ||
|
|
154
|
+
client._reconnectCount === opts.servers.length
|
|
155
|
+
) {
|
|
156
|
+
client._reconnectCount = 0
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
opts.host = opts.servers[client._reconnectCount].host
|
|
160
|
+
opts.port = opts.servers[client._reconnectCount].port
|
|
161
|
+
opts.protocol = !opts.servers[client._reconnectCount].protocol
|
|
162
|
+
? opts.defaultProtocol
|
|
163
|
+
: opts.servers[client._reconnectCount].protocol
|
|
164
|
+
opts.hostname = opts.host
|
|
165
|
+
|
|
166
|
+
client._reconnectCount++
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
debug('calling streambuilder for', opts.protocol)
|
|
170
|
+
return protocols[opts.protocol](client, opts)
|
|
171
|
+
}
|
|
172
|
+
const client = new MqttClient(wrapper, opts)
|
|
173
|
+
client.on('error', () => {
|
|
174
|
+
/* Automatically set up client error handling */
|
|
175
|
+
})
|
|
176
|
+
|
|
177
|
+
return client
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export default connect
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { StreamBuilder } from '../shared'
|
|
2
|
+
|
|
3
|
+
import net from 'net'
|
|
4
|
+
import _debug from 'debug'
|
|
5
|
+
import { Duplex } from 'readable-stream'
|
|
6
|
+
|
|
7
|
+
const debug = _debug('mqttjs:tcp')
|
|
8
|
+
/*
|
|
9
|
+
variables port and host can be removed since
|
|
10
|
+
you have all required information in opts object
|
|
11
|
+
*/
|
|
12
|
+
const buildStream: StreamBuilder = (client, opts) => {
|
|
13
|
+
opts.port = opts.port || 1883
|
|
14
|
+
opts.hostname = opts.hostname || opts.host || 'localhost'
|
|
15
|
+
|
|
16
|
+
const { port } = opts
|
|
17
|
+
const host = opts.hostname
|
|
18
|
+
|
|
19
|
+
debug('port %d and host %s', port, host)
|
|
20
|
+
return net.createConnection(port, host) as unknown as Duplex
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export default buildStream
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import tls from 'tls'
|
|
2
|
+
import net from 'net'
|
|
3
|
+
import _debug from 'debug'
|
|
4
|
+
import { StreamBuilder } from '../shared'
|
|
5
|
+
import { Duplex } from 'readable-stream'
|
|
6
|
+
|
|
7
|
+
const debug = _debug('mqttjs:tls')
|
|
8
|
+
|
|
9
|
+
const buildStream: StreamBuilder = (client, opts) => {
|
|
10
|
+
opts.port = opts.port || 8883
|
|
11
|
+
opts.host = opts.hostname || opts.host || 'localhost'
|
|
12
|
+
|
|
13
|
+
if (net.isIP(opts.host) === 0) {
|
|
14
|
+
opts.servername = opts.host
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
opts.rejectUnauthorized = opts.rejectUnauthorized !== false
|
|
18
|
+
|
|
19
|
+
delete opts.path
|
|
20
|
+
|
|
21
|
+
debug(
|
|
22
|
+
'port %d host %s rejectUnauthorized %b',
|
|
23
|
+
opts.port,
|
|
24
|
+
opts.host,
|
|
25
|
+
opts.rejectUnauthorized,
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
const connection = tls.connect(opts)
|
|
29
|
+
/* eslint no-use-before-define: [2, "nofunc"] */
|
|
30
|
+
connection.on('secureConnect', () => {
|
|
31
|
+
if (opts.rejectUnauthorized && !connection.authorized) {
|
|
32
|
+
connection.emit('error', new Error('TLS not authorized'))
|
|
33
|
+
} else {
|
|
34
|
+
connection.removeListener('error', handleTLSerrors)
|
|
35
|
+
}
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
function handleTLSerrors(err: Error) {
|
|
39
|
+
// How can I get verify this error is a tls error?
|
|
40
|
+
if (opts.rejectUnauthorized) {
|
|
41
|
+
client.emit('error', err)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// close this connection to match the behaviour of net
|
|
45
|
+
// otherwise all we get is an error from the connection
|
|
46
|
+
// and close event doesn't fire. This is a work around
|
|
47
|
+
// to enable the reconnect code to work the same as with
|
|
48
|
+
// net.createConnection
|
|
49
|
+
connection.end()
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
connection.on('error', handleTLSerrors)
|
|
53
|
+
return connection as unknown as Duplex
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export default buildStream
|
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
import { StreamBuilder } from '../shared'
|
|
2
|
+
|
|
3
|
+
import { Buffer } from 'buffer'
|
|
4
|
+
import WS, { ClientOptions } from 'ws'
|
|
5
|
+
import _debug from 'debug'
|
|
6
|
+
import duplexify from 'duplexify'
|
|
7
|
+
import { Duplex, DuplexOptions, Transform } from 'readable-stream'
|
|
8
|
+
import IS_BROWSER from '../is-browser'
|
|
9
|
+
import MqttClient, { IClientOptions } from '../client'
|
|
10
|
+
|
|
11
|
+
const debug = _debug('mqttjs:ws')
|
|
12
|
+
|
|
13
|
+
const WSS_OPTIONS = [
|
|
14
|
+
'rejectUnauthorized',
|
|
15
|
+
'ca',
|
|
16
|
+
'cert',
|
|
17
|
+
'key',
|
|
18
|
+
'pfx',
|
|
19
|
+
'passphrase',
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
function buildUrl(opts: IClientOptions, client: MqttClient) {
|
|
23
|
+
let url = `${opts.protocol}://${opts.hostname}:${opts.port}${opts.path}`
|
|
24
|
+
if (typeof opts.transformWsUrl === 'function') {
|
|
25
|
+
url = opts.transformWsUrl(url, opts, client)
|
|
26
|
+
}
|
|
27
|
+
return url
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function setDefaultOpts(opts: IClientOptions) {
|
|
31
|
+
const options = opts
|
|
32
|
+
if (!opts.hostname) {
|
|
33
|
+
options.hostname = 'localhost'
|
|
34
|
+
}
|
|
35
|
+
if (!opts.port) {
|
|
36
|
+
if (opts.protocol === 'wss') {
|
|
37
|
+
options.port = 443
|
|
38
|
+
} else {
|
|
39
|
+
options.port = 80
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if (!opts.path) {
|
|
43
|
+
options.path = '/'
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (!opts.wsOptions) {
|
|
47
|
+
options.wsOptions = {}
|
|
48
|
+
}
|
|
49
|
+
if (!IS_BROWSER && opts.protocol === 'wss') {
|
|
50
|
+
// Add cert/key/ca etc options
|
|
51
|
+
WSS_OPTIONS.forEach((prop) => {
|
|
52
|
+
if (
|
|
53
|
+
Object.prototype.hasOwnProperty.call(opts, prop) &&
|
|
54
|
+
!Object.prototype.hasOwnProperty.call(opts.wsOptions, prop)
|
|
55
|
+
) {
|
|
56
|
+
options.wsOptions[prop] = opts[prop]
|
|
57
|
+
}
|
|
58
|
+
})
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return options
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function setDefaultBrowserOpts(opts: IClientOptions) {
|
|
65
|
+
const options = setDefaultOpts(opts)
|
|
66
|
+
|
|
67
|
+
if (!options.hostname) {
|
|
68
|
+
options.hostname = options.host
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (!options.hostname) {
|
|
72
|
+
// Throwing an error in a Web Worker if no `hostname` is given, because we
|
|
73
|
+
// can not determine the `hostname` automatically. If connecting to
|
|
74
|
+
// localhost, please supply the `hostname` as an argument.
|
|
75
|
+
if (typeof document === 'undefined') {
|
|
76
|
+
throw new Error('Could not determine host. Specify host manually.')
|
|
77
|
+
}
|
|
78
|
+
const parsed = new URL(document.URL)
|
|
79
|
+
options.hostname = parsed.hostname
|
|
80
|
+
|
|
81
|
+
if (!options.port) {
|
|
82
|
+
options.port = Number(parsed.port)
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// objectMode should be defined for logic
|
|
87
|
+
if (options.objectMode === undefined) {
|
|
88
|
+
options.objectMode = !(
|
|
89
|
+
options.binary === true || options.binary === undefined
|
|
90
|
+
)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return options
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function createWebSocket(
|
|
97
|
+
client: MqttClient,
|
|
98
|
+
url: string,
|
|
99
|
+
opts: IClientOptions,
|
|
100
|
+
) {
|
|
101
|
+
debug('createWebSocket')
|
|
102
|
+
debug(`protocol: ${opts.protocolId} ${opts.protocolVersion}`)
|
|
103
|
+
const websocketSubProtocol =
|
|
104
|
+
opts.protocolId === 'MQIsdp' && opts.protocolVersion === 3
|
|
105
|
+
? 'mqttv3.1'
|
|
106
|
+
: 'mqtt'
|
|
107
|
+
|
|
108
|
+
debug(
|
|
109
|
+
`creating new Websocket for url: ${url} and protocol: ${websocketSubProtocol}`,
|
|
110
|
+
)
|
|
111
|
+
const socket = new WS(
|
|
112
|
+
url,
|
|
113
|
+
[websocketSubProtocol],
|
|
114
|
+
opts.wsOptions as ClientOptions,
|
|
115
|
+
)
|
|
116
|
+
return socket
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function createBrowserWebSocket(client: MqttClient, opts: IClientOptions) {
|
|
120
|
+
const websocketSubProtocol =
|
|
121
|
+
opts.protocolId === 'MQIsdp' && opts.protocolVersion === 3
|
|
122
|
+
? 'mqttv3.1'
|
|
123
|
+
: 'mqtt'
|
|
124
|
+
|
|
125
|
+
const url = buildUrl(opts, client)
|
|
126
|
+
const socket = new WebSocket(url, [websocketSubProtocol])
|
|
127
|
+
socket.binaryType = 'arraybuffer'
|
|
128
|
+
return socket
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const streamBuilder: StreamBuilder = (client, opts) => {
|
|
132
|
+
debug('streamBuilder')
|
|
133
|
+
const options = setDefaultOpts(opts)
|
|
134
|
+
const url = buildUrl(options, client)
|
|
135
|
+
const socket = createWebSocket(client, url, options)
|
|
136
|
+
const webSocketStream = WS.createWebSocketStream(
|
|
137
|
+
socket,
|
|
138
|
+
options.wsOptions as DuplexOptions,
|
|
139
|
+
)
|
|
140
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
141
|
+
// @ts-ignore
|
|
142
|
+
webSocketStream.url = url
|
|
143
|
+
socket.on('close', () => {
|
|
144
|
+
webSocketStream.destroy()
|
|
145
|
+
})
|
|
146
|
+
return webSocketStream as Duplex
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const browserStreamBuilder: StreamBuilder = (client, opts) => {
|
|
150
|
+
debug('browserStreamBuilder')
|
|
151
|
+
let stream
|
|
152
|
+
const options = setDefaultBrowserOpts(opts)
|
|
153
|
+
// sets the maximum socket buffer size before throttling
|
|
154
|
+
const bufferSize = options.browserBufferSize || 1024 * 512
|
|
155
|
+
|
|
156
|
+
const bufferTimeout = opts.browserBufferTimeout || 1000
|
|
157
|
+
|
|
158
|
+
const coerceToBuffer = !opts.objectMode
|
|
159
|
+
|
|
160
|
+
const socket = createBrowserWebSocket(client, opts)
|
|
161
|
+
const proxy = buildProxy(opts, socketWriteBrowser, socketEndBrowser)
|
|
162
|
+
|
|
163
|
+
if (!opts.objectMode) {
|
|
164
|
+
proxy._writev = writev
|
|
165
|
+
}
|
|
166
|
+
proxy.on('close', () => {
|
|
167
|
+
socket.close()
|
|
168
|
+
})
|
|
169
|
+
|
|
170
|
+
const eventListenerSupport = typeof socket.addEventListener !== 'undefined'
|
|
171
|
+
|
|
172
|
+
// was already open when passed in
|
|
173
|
+
if (socket.readyState === socket.OPEN) {
|
|
174
|
+
stream = proxy
|
|
175
|
+
} else {
|
|
176
|
+
stream = duplexify(undefined, undefined, opts)
|
|
177
|
+
if (!opts.objectMode) {
|
|
178
|
+
stream._writev = writev
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if (eventListenerSupport) {
|
|
182
|
+
socket.addEventListener('open', onOpen)
|
|
183
|
+
} else {
|
|
184
|
+
socket.onopen = onOpen
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
stream.socket = socket
|
|
189
|
+
|
|
190
|
+
if (eventListenerSupport) {
|
|
191
|
+
socket.addEventListener('close', onClose)
|
|
192
|
+
socket.addEventListener('error', onError)
|
|
193
|
+
socket.addEventListener('message', onMessage)
|
|
194
|
+
} else {
|
|
195
|
+
socket.onclose = onClose
|
|
196
|
+
socket.onerror = onError
|
|
197
|
+
socket.onmessage = onMessage
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// methods for browserStreamBuilder
|
|
201
|
+
|
|
202
|
+
function buildProxy(pOptions: IClientOptions, socketWrite, socketEnd) {
|
|
203
|
+
const _proxy = new Transform({
|
|
204
|
+
objectMode: pOptions.objectMode,
|
|
205
|
+
})
|
|
206
|
+
|
|
207
|
+
_proxy._write = socketWrite
|
|
208
|
+
_proxy._flush = socketEnd
|
|
209
|
+
|
|
210
|
+
return _proxy
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function onOpen() {
|
|
214
|
+
stream.setReadable(proxy)
|
|
215
|
+
stream.setWritable(proxy)
|
|
216
|
+
stream.emit('connect')
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function onClose() {
|
|
220
|
+
stream.end()
|
|
221
|
+
stream.destroy()
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function onError(err: Event) {
|
|
225
|
+
stream.destroy(err)
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function onMessage(event: MessageEvent) {
|
|
229
|
+
let { data } = event
|
|
230
|
+
if (data instanceof ArrayBuffer) data = Buffer.from(data)
|
|
231
|
+
else data = Buffer.from(data, 'utf8')
|
|
232
|
+
proxy.push(data)
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// this is to be enabled only if objectMode is false
|
|
236
|
+
function writev(chunks: any, cb: (err?: Error) => void) {
|
|
237
|
+
const buffers = new Array(chunks.length)
|
|
238
|
+
for (let i = 0; i < chunks.length; i++) {
|
|
239
|
+
if (typeof chunks[i].chunk === 'string') {
|
|
240
|
+
buffers[i] = Buffer.from(chunks[i], 'utf8')
|
|
241
|
+
} else {
|
|
242
|
+
buffers[i] = chunks[i].chunk
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
this._write(Buffer.concat(buffers), 'binary', cb)
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function socketWriteBrowser(
|
|
250
|
+
chunk: any,
|
|
251
|
+
enc: string,
|
|
252
|
+
next: (err?: Error) => void,
|
|
253
|
+
) {
|
|
254
|
+
if (socket.bufferedAmount > bufferSize) {
|
|
255
|
+
// throttle data until buffered amount is reduced.
|
|
256
|
+
setTimeout(socketWriteBrowser, bufferTimeout, chunk, enc, next)
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
if (coerceToBuffer && typeof chunk === 'string') {
|
|
260
|
+
chunk = Buffer.from(chunk, 'utf8')
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
try {
|
|
264
|
+
socket.send(chunk)
|
|
265
|
+
} catch (err) {
|
|
266
|
+
return next(err)
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
next()
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
function socketEndBrowser(done) {
|
|
273
|
+
socket.close()
|
|
274
|
+
done()
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// end methods for browserStreamBuilder
|
|
278
|
+
|
|
279
|
+
return stream
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
export default IS_BROWSER ? browserStreamBuilder : streamBuilder
|