mqtt 5.10.3 → 5.11.0
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 +6 -1
- package/build/bin/mqtt.js.map +1 -1
- package/build/bin/pub.js +1 -1
- package/build/bin/pub.js.map +1 -1
- package/build/bin/sub.js +1 -1
- package/build/bin/sub.js.map +1 -1
- package/build/index.js +17 -7
- package/build/index.js.map +1 -1
- package/build/lib/BufferedDuplex.js +3 -3
- package/build/lib/BufferedDuplex.js.map +1 -1
- package/build/lib/KeepaliveManager.js.map +1 -1
- package/build/lib/client.d.ts +2 -2
- package/build/lib/client.js +17 -7
- package/build/lib/client.js.map +1 -1
- package/build/lib/connect/ali.js.map +1 -1
- package/build/lib/connect/index.js +13 -7
- package/build/lib/connect/index.js.map +1 -1
- package/build/lib/connect/socks.d.ts +8 -0
- package/build/lib/connect/socks.js +183 -0
- package/build/lib/connect/socks.js.map +1 -0
- package/build/lib/connect/tcp.js +6 -0
- package/build/lib/connect/tcp.js.map +1 -1
- package/build/lib/connect/tls.js +20 -1
- package/build/lib/connect/tls.js.map +1 -1
- package/build/lib/connect/ws.js +4 -3
- package/build/lib/connect/ws.js.map +1 -1
- package/build/lib/connect/wx.js.map +1 -1
- package/build/lib/default-message-id-provider.js.map +1 -1
- package/build/lib/get-timer.js +17 -7
- package/build/lib/get-timer.js.map +1 -1
- package/build/lib/handlers/ack.js.map +1 -1
- package/build/lib/handlers/auth.js.map +1 -1
- package/build/lib/handlers/connack.js.map +1 -1
- package/build/lib/handlers/index.js.map +1 -1
- package/build/lib/handlers/publish.js.map +1 -1
- package/build/lib/handlers/pubrel.js.map +1 -1
- package/build/lib/is-browser.js.map +1 -1
- package/build/lib/shared.d.ts +0 -1
- package/build/lib/shared.js +2 -2
- package/build/lib/shared.js.map +1 -1
- package/build/lib/store.js.map +1 -1
- package/build/lib/topic-alias-recv.js.map +1 -1
- package/build/lib/topic-alias-send.js.map +1 -1
- package/build/lib/validations.d.ts +1 -1
- package/build/lib/validations.js +2 -3
- package/build/lib/validations.js.map +1 -1
- package/build/mqtt.js +17 -7
- package/build/mqtt.js.map +1 -1
- package/build/tsconfig.build.tsbuildinfo +1 -1
- package/dist/mqtt.esm.js +8 -5
- package/dist/mqtt.js +4410 -5279
- package/dist/mqtt.min.js +8 -5
- package/package.json +34 -33
- package/src/lib/BufferedDuplex.ts +1 -1
- package/src/lib/client.ts +5 -1
- package/src/lib/connect/index.ts +11 -5
- package/src/lib/connect/socks.ts +236 -0
- package/src/lib/connect/tcp.ts +7 -0
- package/src/lib/connect/tls.ts +19 -2
- package/src/lib/connect/ws.ts +4 -3
- package/src/lib/shared.ts +1 -1
- package/src/lib/validations.ts +1 -1
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": "5.
|
|
4
|
+
"version": "5.11.0",
|
|
5
5
|
"contributors": [
|
|
6
6
|
"Adam Rudd <adamvrr@gmail.com>",
|
|
7
7
|
"Matteo Collina <matteo.collina@gmail.com> (https://github.com/mcollina)",
|
|
@@ -111,48 +111,49 @@
|
|
|
111
111
|
"net": false
|
|
112
112
|
},
|
|
113
113
|
"dependencies": {
|
|
114
|
-
"@types/readable-stream": "^4.0.
|
|
115
|
-
"@types/ws": "^8.5.
|
|
114
|
+
"@types/readable-stream": "^4.0.18",
|
|
115
|
+
"@types/ws": "^8.5.14",
|
|
116
116
|
"commist": "^3.2.0",
|
|
117
117
|
"concat-stream": "^2.0.0",
|
|
118
|
-
"debug": "^4.
|
|
118
|
+
"debug": "^4.4.0",
|
|
119
119
|
"help-me": "^5.0.0",
|
|
120
|
-
"lru-cache": "^10.
|
|
120
|
+
"lru-cache": "^10.4.3",
|
|
121
121
|
"minimist": "^1.2.8",
|
|
122
|
-
"mqtt-packet": "^9.0.
|
|
122
|
+
"mqtt-packet": "^9.0.2",
|
|
123
123
|
"number-allocator": "^1.0.14",
|
|
124
|
-
"readable-stream": "^4.
|
|
124
|
+
"readable-stream": "^4.7.0",
|
|
125
125
|
"reinterval": "^1.1.0",
|
|
126
|
-
"rfdc": "^1.
|
|
126
|
+
"rfdc": "^1.4.1",
|
|
127
|
+
"socks": "^2.8.3",
|
|
127
128
|
"split2": "^4.2.0",
|
|
128
|
-
"worker-timers": "^7.1.
|
|
129
|
-
"ws": "^8.
|
|
129
|
+
"worker-timers": "^7.1.8",
|
|
130
|
+
"ws": "^8.18.0"
|
|
130
131
|
},
|
|
131
132
|
"devDependencies": {
|
|
132
|
-
"@esm-bundle/chai": "^4.3.4
|
|
133
|
+
"@esm-bundle/chai": "^4.3.4",
|
|
133
134
|
"@release-it/conventional-changelog": "^7.0.2",
|
|
134
|
-
"@types/chai": "^4.3.
|
|
135
|
-
"@types/node": "^20.
|
|
136
|
-
"@types/sinon": "^17.0.
|
|
137
|
-
"@types/tape": "^5.
|
|
138
|
-
"@typescript-eslint/eslint-plugin": "^6.
|
|
139
|
-
"@typescript-eslint/parser": "^6.
|
|
140
|
-
"@web/test-runner": "^0.
|
|
135
|
+
"@types/chai": "^4.3.20",
|
|
136
|
+
"@types/node": "^20.17.16",
|
|
137
|
+
"@types/sinon": "^17.0.3",
|
|
138
|
+
"@types/tape": "^5.8.1",
|
|
139
|
+
"@typescript-eslint/eslint-plugin": "^6.21.0",
|
|
140
|
+
"@typescript-eslint/parser": "^6.21.0",
|
|
141
|
+
"@web/test-runner": "^0.19.0",
|
|
141
142
|
"@web/test-runner-playwright": "^0.11.0",
|
|
142
143
|
"aedes-cli": "^0.8.0",
|
|
143
|
-
"chai": "^4.
|
|
144
|
-
"chokidar": "^3.
|
|
144
|
+
"chai": "^4.5.0",
|
|
145
|
+
"chokidar": "^3.6.0",
|
|
145
146
|
"conventional-changelog-cli": "^4.1.0",
|
|
146
147
|
"end-of-stream": "^1.4.4",
|
|
147
|
-
"esbuild": "^0.
|
|
148
|
+
"esbuild": "^0.25.0",
|
|
148
149
|
"esbuild-plugin-polyfill-node": "^0.3.0",
|
|
149
|
-
"esbuild-register": "^3.
|
|
150
|
-
"eslint": "^8.
|
|
150
|
+
"esbuild-register": "^3.6.0",
|
|
151
|
+
"eslint": "^8.57.1",
|
|
151
152
|
"eslint-config-airbnb-base": "^15.0.0",
|
|
152
153
|
"eslint-config-airbnb-typescript": "^17.1.0",
|
|
153
|
-
"eslint-config-prettier": "^9.
|
|
154
|
-
"eslint-plugin-import": "^2.
|
|
155
|
-
"eslint-plugin-prettier": "^5.
|
|
154
|
+
"eslint-config-prettier": "^9.1.0",
|
|
155
|
+
"eslint-plugin-import": "^2.31.0",
|
|
156
|
+
"eslint-plugin-prettier": "^5.2.3",
|
|
156
157
|
"global": "^4.4.0",
|
|
157
158
|
"leaked-handles": "^5.2.0",
|
|
158
159
|
"mkdirp": "^3.0.1",
|
|
@@ -160,14 +161,14 @@
|
|
|
160
161
|
"mqtt-level-store": "^3.1.0",
|
|
161
162
|
"nyc": "^15.1.0",
|
|
162
163
|
"pre-commit": "^1.2.2",
|
|
163
|
-
"prettier": "^3.
|
|
164
|
-
"release-it": "^16.
|
|
165
|
-
"rimraf": "^5.0.
|
|
164
|
+
"prettier": "^3.4.2",
|
|
165
|
+
"release-it": "^16.3.0",
|
|
166
|
+
"rimraf": "^5.0.10",
|
|
166
167
|
"should": "^13.2.3",
|
|
167
|
-
"sinon": "^17.0.
|
|
168
|
+
"sinon": "^17.0.2",
|
|
168
169
|
"snazzy": "^9.0.0",
|
|
169
|
-
"tape": "^5.
|
|
170
|
-
"ts-node": "^10.9.
|
|
171
|
-
"typescript": "^5.
|
|
170
|
+
"tape": "^5.9.0",
|
|
171
|
+
"ts-node": "^10.9.2",
|
|
172
|
+
"typescript": "^5.7.3"
|
|
172
173
|
}
|
|
173
174
|
}
|
package/src/lib/client.ts
CHANGED
|
@@ -140,6 +140,10 @@ export interface IClientOptions extends ISecureClientOptions {
|
|
|
140
140
|
query?: Record<string, string>
|
|
141
141
|
/** Auth string in the format <username>:<password> */
|
|
142
142
|
auth?: string
|
|
143
|
+
/** Optional SOCKS proxy to use for TCP / TLS connections , i.e. socks5://localhost:1333, socks4://localhost:1333, socks5h://localhost:1333 . Default is socks5h. */
|
|
144
|
+
socksProxy?: string
|
|
145
|
+
/** Timeout for establishing a socks connection */
|
|
146
|
+
socksTimeout?: number
|
|
143
147
|
/** Custom ack handler */
|
|
144
148
|
customHandleAcks?: AckHandler
|
|
145
149
|
/** Broker port */
|
|
@@ -571,7 +575,7 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
|
|
|
571
575
|
? options.customHandleAcks
|
|
572
576
|
: (...args) => {
|
|
573
577
|
args[3](null, 0)
|
|
574
|
-
|
|
578
|
+
}
|
|
575
579
|
|
|
576
580
|
// Disable pre-generated write cache if requested. Will allocate buffers on-the-fly instead. WARNING: This can affect write performance
|
|
577
581
|
if (!this.options.writeCache) {
|
package/src/lib/connect/index.ts
CHANGED
|
@@ -72,17 +72,14 @@ function connect(
|
|
|
72
72
|
parsedOptions.protocol = parsedUrl.protocol as MqttProtocol
|
|
73
73
|
parsedOptions.path = parsedUrl.path
|
|
74
74
|
|
|
75
|
-
parsedOptions.protocol = parsedOptions.protocol?.replace(
|
|
76
|
-
/:$/,
|
|
77
|
-
'',
|
|
78
|
-
) as MqttProtocol
|
|
79
|
-
|
|
80
75
|
opts = { ...parsedOptions, ...opts }
|
|
81
76
|
|
|
82
77
|
// when parsing an url expect the protocol to be set
|
|
83
78
|
if (!opts.protocol) {
|
|
84
79
|
throw new Error('Missing protocol')
|
|
85
80
|
}
|
|
81
|
+
|
|
82
|
+
opts.protocol = opts.protocol.replace(/:$/, '') as MqttProtocol
|
|
86
83
|
}
|
|
87
84
|
|
|
88
85
|
opts.unixSocket = opts.unixSocket || opts.protocol?.includes('+unix')
|
|
@@ -107,6 +104,15 @@ function connect(
|
|
|
107
104
|
opts.clientId = opts.query.clientId
|
|
108
105
|
}
|
|
109
106
|
|
|
107
|
+
if (isBrowser || opts.unixSocket) {
|
|
108
|
+
opts.socksProxy = undefined
|
|
109
|
+
} else if (
|
|
110
|
+
opts.socksProxy === undefined &&
|
|
111
|
+
typeof process !== 'undefined'
|
|
112
|
+
) {
|
|
113
|
+
opts.socksProxy = process.env['MQTTJS_SOCKS_PROXY']
|
|
114
|
+
}
|
|
115
|
+
|
|
110
116
|
if (opts.cert && opts.key) {
|
|
111
117
|
if (opts.protocol) {
|
|
112
118
|
if (['mqtts', 'wss', 'wxs', 'alis'].indexOf(opts.protocol) === -1) {
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import _debug from 'debug'
|
|
2
|
+
import { Duplex } from 'stream'
|
|
3
|
+
import { SocksClient, SocksProxy } from 'socks'
|
|
4
|
+
import * as dns from 'dns'
|
|
5
|
+
import { SocksProxyType } from 'socks/typings/common/constants'
|
|
6
|
+
import { IStream } from '../shared'
|
|
7
|
+
import { promisify } from 'util'
|
|
8
|
+
import { Socket } from 'net'
|
|
9
|
+
import assert from 'assert'
|
|
10
|
+
|
|
11
|
+
const debug = _debug('mqttjs:socks')
|
|
12
|
+
|
|
13
|
+
export interface SocksConnectionOptions {
|
|
14
|
+
timeout?: number
|
|
15
|
+
lookup?: (hostname: string) => Promise<{ address: string }>
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
class ProxyStream extends Duplex {
|
|
19
|
+
private _flowing = false
|
|
20
|
+
|
|
21
|
+
private _socket?: Socket
|
|
22
|
+
|
|
23
|
+
constructor() {
|
|
24
|
+
super({ autoDestroy: false })
|
|
25
|
+
|
|
26
|
+
this.cork()
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
_start(socket: Socket): void {
|
|
30
|
+
debug('proxy stream started')
|
|
31
|
+
|
|
32
|
+
assert(!this._socket)
|
|
33
|
+
|
|
34
|
+
if (this.destroyed) {
|
|
35
|
+
socket.destroy(this.errored)
|
|
36
|
+
return
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
this._socket = socket
|
|
40
|
+
|
|
41
|
+
if (!this._flowing) socket.pause()
|
|
42
|
+
|
|
43
|
+
socket.on('data', this._onData)
|
|
44
|
+
socket.on('end', this._onEnd)
|
|
45
|
+
socket.on('error', this._onError)
|
|
46
|
+
socket.on('close', this._onClose)
|
|
47
|
+
|
|
48
|
+
socket.emit('connect')
|
|
49
|
+
|
|
50
|
+
this.uncork()
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
_write(
|
|
54
|
+
chunk: any,
|
|
55
|
+
encoding: BufferEncoding,
|
|
56
|
+
callback: (error?: Error | null) => void,
|
|
57
|
+
): void {
|
|
58
|
+
assert(this._socket)
|
|
59
|
+
|
|
60
|
+
this._socket.write(chunk, callback)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
_read(size: number): void {
|
|
64
|
+
this._flowing = true
|
|
65
|
+
|
|
66
|
+
this._socket?.resume?.()
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
_destroy(
|
|
70
|
+
error: Error | null,
|
|
71
|
+
callback: (error?: Error | null) => void,
|
|
72
|
+
): void {
|
|
73
|
+
this._socket?.destroy?.(error)
|
|
74
|
+
|
|
75
|
+
callback(error)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
private _onData = (chunk: any): void => {
|
|
79
|
+
assert(this._socket)
|
|
80
|
+
|
|
81
|
+
this._flowing = this.push(chunk)
|
|
82
|
+
if (!this._flowing) this._socket.pause()
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
private _onEnd = (): void => {
|
|
86
|
+
debug('proxy stream received EOF')
|
|
87
|
+
|
|
88
|
+
this.push(null)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
private _onClose = (): void => {
|
|
92
|
+
debug('proxy stream closed')
|
|
93
|
+
|
|
94
|
+
this.destroy()
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
private _onError = (err: any): void => {
|
|
98
|
+
debug('proxy stream died with error %s', err)
|
|
99
|
+
|
|
100
|
+
this.destroy(err)
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function fatal<T>(e: T): T {
|
|
105
|
+
try {
|
|
106
|
+
if ((e as any).code === undefined) (e as any).code = 'SOCKS'
|
|
107
|
+
return e
|
|
108
|
+
} catch {
|
|
109
|
+
return e
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function typeFromProtocol(
|
|
114
|
+
proto: string,
|
|
115
|
+
): [SocksProxyType | undefined, boolean] {
|
|
116
|
+
switch (proto) {
|
|
117
|
+
case 'socks5h:':
|
|
118
|
+
return [5, true]
|
|
119
|
+
|
|
120
|
+
case 'socks4a:':
|
|
121
|
+
return [4, true]
|
|
122
|
+
|
|
123
|
+
case 'socks5:':
|
|
124
|
+
return [5, false]
|
|
125
|
+
|
|
126
|
+
case 'socks4:':
|
|
127
|
+
return [4, false]
|
|
128
|
+
|
|
129
|
+
default:
|
|
130
|
+
return [undefined, false]
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function parseSocksUrl(url: string): [SocksProxy, boolean] {
|
|
135
|
+
const parsedUrl = new URL(url)
|
|
136
|
+
|
|
137
|
+
if (parsedUrl.pathname || parsedUrl.hash || parsedUrl.search) {
|
|
138
|
+
throw fatal(new Error('bad SOCKS URL'))
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const [type, resolveThroughProxy] = typeFromProtocol(parsedUrl.protocol)
|
|
142
|
+
if (!type) {
|
|
143
|
+
throw fatal(new Error('bad SOCKS URL: invalid protocol'))
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const port = parseInt(parsedUrl.port, 10)
|
|
147
|
+
if (Number.isNaN(port)) {
|
|
148
|
+
throw fatal(new Error('bad SOCKS URL: invalid port'))
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const proxy: SocksProxy = {
|
|
152
|
+
host: parsedUrl.hostname,
|
|
153
|
+
port,
|
|
154
|
+
type,
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
return [proxy, resolveThroughProxy]
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
async function connectSocks(
|
|
161
|
+
destinationHost: string,
|
|
162
|
+
destinationPort: number,
|
|
163
|
+
socksUrl: string,
|
|
164
|
+
stream: ProxyStream,
|
|
165
|
+
options: SocksConnectionOptions = {},
|
|
166
|
+
): Promise<void> {
|
|
167
|
+
const lookup = options.lookup ?? promisify(dns.lookup)
|
|
168
|
+
|
|
169
|
+
const [proxy, resolveThroughProxy] = parseSocksUrl(socksUrl)
|
|
170
|
+
|
|
171
|
+
if (!resolveThroughProxy) {
|
|
172
|
+
debug('resolving %s locally', destinationHost)
|
|
173
|
+
|
|
174
|
+
destinationHost = (
|
|
175
|
+
await lookup(destinationHost, {
|
|
176
|
+
family: proxy.type === 4 ? 4 : 0,
|
|
177
|
+
})
|
|
178
|
+
).address
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
debug(
|
|
182
|
+
'establishing SOCKS%d connection to %s:%d via %s:%d',
|
|
183
|
+
proxy.type,
|
|
184
|
+
destinationHost,
|
|
185
|
+
destinationPort,
|
|
186
|
+
proxy.host,
|
|
187
|
+
proxy.port,
|
|
188
|
+
)
|
|
189
|
+
|
|
190
|
+
const socksClient = new SocksClient({
|
|
191
|
+
command: 'connect',
|
|
192
|
+
destination: {
|
|
193
|
+
host: destinationHost,
|
|
194
|
+
port: destinationPort,
|
|
195
|
+
},
|
|
196
|
+
proxy: { ...proxy },
|
|
197
|
+
timeout: options.timeout,
|
|
198
|
+
})
|
|
199
|
+
socksClient.connect()
|
|
200
|
+
|
|
201
|
+
socksClient.on('established', ({ socket }) => stream._start(socket))
|
|
202
|
+
|
|
203
|
+
socksClient.on('error', (e) => {
|
|
204
|
+
debug('SOCKS failed: %s', e)
|
|
205
|
+
stream.destroy(fatal(e))
|
|
206
|
+
})
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export default function openSocks(
|
|
210
|
+
destinationHost: string,
|
|
211
|
+
destinationPort: number,
|
|
212
|
+
socksUrl: string,
|
|
213
|
+
options?: SocksConnectionOptions,
|
|
214
|
+
): IStream {
|
|
215
|
+
debug(
|
|
216
|
+
'SOCKS connection to %s:%d via %s',
|
|
217
|
+
destinationHost,
|
|
218
|
+
destinationPort,
|
|
219
|
+
socksUrl,
|
|
220
|
+
)
|
|
221
|
+
|
|
222
|
+
const stream = new ProxyStream()
|
|
223
|
+
|
|
224
|
+
connectSocks(
|
|
225
|
+
destinationHost,
|
|
226
|
+
destinationPort,
|
|
227
|
+
socksUrl,
|
|
228
|
+
stream,
|
|
229
|
+
options,
|
|
230
|
+
).catch((e) => {
|
|
231
|
+
debug('SOCKS failed: %s', e)
|
|
232
|
+
stream.destroy(e)
|
|
233
|
+
})
|
|
234
|
+
|
|
235
|
+
return stream
|
|
236
|
+
}
|
package/src/lib/connect/tcp.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { StreamBuilder } from '../shared'
|
|
|
2
2
|
|
|
3
3
|
import net from 'net'
|
|
4
4
|
import _debug from 'debug'
|
|
5
|
+
import openSocks from './socks'
|
|
5
6
|
|
|
6
7
|
const debug = _debug('mqttjs:tcp')
|
|
7
8
|
/*
|
|
@@ -12,6 +13,12 @@ const buildStream: StreamBuilder = (client, opts) => {
|
|
|
12
13
|
opts.port = opts.port || 1883
|
|
13
14
|
opts.hostname = opts.hostname || opts.host || 'localhost'
|
|
14
15
|
|
|
16
|
+
if (opts.socksProxy) {
|
|
17
|
+
return openSocks(opts.hostname, opts.port, opts.socksProxy, {
|
|
18
|
+
timeout: opts.socksTimeout,
|
|
19
|
+
})
|
|
20
|
+
}
|
|
21
|
+
|
|
15
22
|
const { port, path } = opts
|
|
16
23
|
const host = opts.hostname
|
|
17
24
|
|
package/src/lib/connect/tls.ts
CHANGED
|
@@ -1,10 +1,27 @@
|
|
|
1
|
-
import tls from 'tls'
|
|
1
|
+
import tls, { TLSSocket } from 'tls'
|
|
2
2
|
import net from 'net'
|
|
3
3
|
import _debug from 'debug'
|
|
4
4
|
import { StreamBuilder } from '../shared'
|
|
5
|
+
import { IClientOptions } from '../client'
|
|
6
|
+
import openSocks from './socks'
|
|
5
7
|
|
|
6
8
|
const debug = _debug('mqttjs:tls')
|
|
7
9
|
|
|
10
|
+
function connect(opts: IClientOptions): TLSSocket {
|
|
11
|
+
const { host, port, socksProxy, ...rest } = opts
|
|
12
|
+
|
|
13
|
+
return tls.connect(
|
|
14
|
+
socksProxy
|
|
15
|
+
? {
|
|
16
|
+
...rest,
|
|
17
|
+
socket: openSocks(host, port, socksProxy, {
|
|
18
|
+
timeout: opts.socksTimeout,
|
|
19
|
+
}),
|
|
20
|
+
}
|
|
21
|
+
: opts,
|
|
22
|
+
)
|
|
23
|
+
}
|
|
24
|
+
|
|
8
25
|
const buildStream: StreamBuilder = (client, opts) => {
|
|
9
26
|
opts.port = opts.port || 8883
|
|
10
27
|
opts.host = opts.hostname || opts.host || 'localhost'
|
|
@@ -24,7 +41,7 @@ const buildStream: StreamBuilder = (client, opts) => {
|
|
|
24
41
|
opts.rejectUnauthorized,
|
|
25
42
|
)
|
|
26
43
|
|
|
27
|
-
const connection =
|
|
44
|
+
const connection = connect(opts)
|
|
28
45
|
connection.on('secureConnect', () => {
|
|
29
46
|
if (opts.rejectUnauthorized && !connection.authorized) {
|
|
30
47
|
connection.emit('error', new Error('TLS not authorized'))
|
package/src/lib/connect/ws.ts
CHANGED
|
@@ -257,14 +257,15 @@ const browserStreamBuilder: StreamBuilder = (client, opts) => {
|
|
|
257
257
|
* https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/message_event
|
|
258
258
|
*/
|
|
259
259
|
async function onMessage(event: MessageEvent) {
|
|
260
|
+
if (!proxy || proxy.destroyed || !proxy.readable) {
|
|
261
|
+
return
|
|
262
|
+
}
|
|
260
263
|
let { data } = event
|
|
261
264
|
if (data instanceof ArrayBuffer) data = Buffer.from(data)
|
|
262
265
|
else if (data instanceof Blob)
|
|
263
266
|
data = Buffer.from(await new Response(data).arrayBuffer())
|
|
264
267
|
else data = Buffer.from(data as string, 'utf8')
|
|
265
|
-
|
|
266
|
-
proxy.push(data)
|
|
267
|
-
}
|
|
268
|
+
proxy.push(data)
|
|
268
269
|
}
|
|
269
270
|
|
|
270
271
|
function socketWriteBrowser(
|
package/src/lib/shared.ts
CHANGED
package/src/lib/validations.ts
CHANGED
|
@@ -33,7 +33,7 @@ export function validateTopic(topic: string): boolean {
|
|
|
33
33
|
* @param {Array} topics - Array of topics
|
|
34
34
|
* @returns {String} If the topics is valid, returns null. Otherwise, returns the invalid one
|
|
35
35
|
*/
|
|
36
|
-
export function validateTopics(topics: string[]): string {
|
|
36
|
+
export function validateTopics(topics: string[]): string | null {
|
|
37
37
|
if (topics.length === 0) {
|
|
38
38
|
return 'empty_topic_list'
|
|
39
39
|
}
|