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.
Files changed (62) hide show
  1. package/README.md +6 -1
  2. package/build/bin/mqtt.js.map +1 -1
  3. package/build/bin/pub.js +1 -1
  4. package/build/bin/pub.js.map +1 -1
  5. package/build/bin/sub.js +1 -1
  6. package/build/bin/sub.js.map +1 -1
  7. package/build/index.js +17 -7
  8. package/build/index.js.map +1 -1
  9. package/build/lib/BufferedDuplex.js +3 -3
  10. package/build/lib/BufferedDuplex.js.map +1 -1
  11. package/build/lib/KeepaliveManager.js.map +1 -1
  12. package/build/lib/client.d.ts +2 -2
  13. package/build/lib/client.js +17 -7
  14. package/build/lib/client.js.map +1 -1
  15. package/build/lib/connect/ali.js.map +1 -1
  16. package/build/lib/connect/index.js +13 -7
  17. package/build/lib/connect/index.js.map +1 -1
  18. package/build/lib/connect/socks.d.ts +8 -0
  19. package/build/lib/connect/socks.js +183 -0
  20. package/build/lib/connect/socks.js.map +1 -0
  21. package/build/lib/connect/tcp.js +6 -0
  22. package/build/lib/connect/tcp.js.map +1 -1
  23. package/build/lib/connect/tls.js +20 -1
  24. package/build/lib/connect/tls.js.map +1 -1
  25. package/build/lib/connect/ws.js +4 -3
  26. package/build/lib/connect/ws.js.map +1 -1
  27. package/build/lib/connect/wx.js.map +1 -1
  28. package/build/lib/default-message-id-provider.js.map +1 -1
  29. package/build/lib/get-timer.js +17 -7
  30. package/build/lib/get-timer.js.map +1 -1
  31. package/build/lib/handlers/ack.js.map +1 -1
  32. package/build/lib/handlers/auth.js.map +1 -1
  33. package/build/lib/handlers/connack.js.map +1 -1
  34. package/build/lib/handlers/index.js.map +1 -1
  35. package/build/lib/handlers/publish.js.map +1 -1
  36. package/build/lib/handlers/pubrel.js.map +1 -1
  37. package/build/lib/is-browser.js.map +1 -1
  38. package/build/lib/shared.d.ts +0 -1
  39. package/build/lib/shared.js +2 -2
  40. package/build/lib/shared.js.map +1 -1
  41. package/build/lib/store.js.map +1 -1
  42. package/build/lib/topic-alias-recv.js.map +1 -1
  43. package/build/lib/topic-alias-send.js.map +1 -1
  44. package/build/lib/validations.d.ts +1 -1
  45. package/build/lib/validations.js +2 -3
  46. package/build/lib/validations.js.map +1 -1
  47. package/build/mqtt.js +17 -7
  48. package/build/mqtt.js.map +1 -1
  49. package/build/tsconfig.build.tsbuildinfo +1 -1
  50. package/dist/mqtt.esm.js +8 -5
  51. package/dist/mqtt.js +4410 -5279
  52. package/dist/mqtt.min.js +8 -5
  53. package/package.json +34 -33
  54. package/src/lib/BufferedDuplex.ts +1 -1
  55. package/src/lib/client.ts +5 -1
  56. package/src/lib/connect/index.ts +11 -5
  57. package/src/lib/connect/socks.ts +236 -0
  58. package/src/lib/connect/tcp.ts +7 -0
  59. package/src/lib/connect/tls.ts +19 -2
  60. package/src/lib/connect/ws.ts +4 -3
  61. package/src/lib/shared.ts +1 -1
  62. 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.10.3",
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.5",
115
- "@types/ws": "^8.5.9",
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.3.4",
118
+ "debug": "^4.4.0",
119
119
  "help-me": "^5.0.0",
120
- "lru-cache": "^10.0.1",
120
+ "lru-cache": "^10.4.3",
121
121
  "minimist": "^1.2.8",
122
- "mqtt-packet": "^9.0.1",
122
+ "mqtt-packet": "^9.0.2",
123
123
  "number-allocator": "^1.0.14",
124
- "readable-stream": "^4.4.2",
124
+ "readable-stream": "^4.7.0",
125
125
  "reinterval": "^1.1.0",
126
- "rfdc": "^1.3.0",
126
+ "rfdc": "^1.4.1",
127
+ "socks": "^2.8.3",
127
128
  "split2": "^4.2.0",
128
- "worker-timers": "^7.1.4",
129
- "ws": "^8.17.1"
129
+ "worker-timers": "^7.1.8",
130
+ "ws": "^8.18.0"
130
131
  },
131
132
  "devDependencies": {
132
- "@esm-bundle/chai": "^4.3.4-fix.0",
133
+ "@esm-bundle/chai": "^4.3.4",
133
134
  "@release-it/conventional-changelog": "^7.0.2",
134
- "@types/chai": "^4.3.10",
135
- "@types/node": "^20.9.0",
136
- "@types/sinon": "^17.0.1",
137
- "@types/tape": "^5.6.4",
138
- "@typescript-eslint/eslint-plugin": "^6.10.0",
139
- "@typescript-eslint/parser": "^6.10.0",
140
- "@web/test-runner": "^0.18.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.3.10",
144
- "chokidar": "^3.5.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.19.5",
148
+ "esbuild": "^0.25.0",
148
149
  "esbuild-plugin-polyfill-node": "^0.3.0",
149
- "esbuild-register": "^3.5.0",
150
- "eslint": "^8.53.0",
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.0.0",
154
- "eslint-plugin-import": "^2.29.0",
155
- "eslint-plugin-prettier": "^5.0.1",
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.0.3",
164
- "release-it": "^16.2.1",
165
- "rimraf": "^5.0.5",
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.1",
168
+ "sinon": "^17.0.2",
168
169
  "snazzy": "^9.0.0",
169
- "tape": "^5.7.2",
170
- "ts-node": "^10.9.1",
171
- "typescript": "^5.2.2"
170
+ "tape": "^5.9.0",
171
+ "ts-node": "^10.9.2",
172
+ "typescript": "^5.7.3"
172
173
  }
173
174
  }
@@ -56,7 +56,7 @@ export class BufferedDuplex extends Duplex {
56
56
  this.isSocketOpen = false
57
57
 
58
58
  this.proxy.on('data', (chunk) => {
59
- if (!this.destroyed) {
59
+ if (!this.destroyed && this.readable) {
60
60
  this.push(chunk)
61
61
  }
62
62
  })
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) {
@@ -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
+ }
@@ -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
 
@@ -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 = tls.connect(opts)
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'))
@@ -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
- if (proxy && !proxy.destroyed) {
266
- proxy.push(data)
267
- }
268
+ proxy.push(data)
268
269
  }
269
270
 
270
271
  function socketWriteBrowser(
package/src/lib/shared.ts CHANGED
@@ -81,7 +81,7 @@ export const nextTick =
81
81
  ? process.nextTick
82
82
  : (callback: () => void) => {
83
83
  setTimeout(callback, 0)
84
- }
84
+ }
85
85
 
86
86
  // eslint-disable-next-line @typescript-eslint/no-var-requires
87
87
  export const MQTTJS_VERSION = require('../../package.json').version
@@ -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
  }