mqtt 5.6.2 → 5.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/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.6.2",
4
+ "version": "5.7.1",
5
5
  "contributors": [
6
6
  "Adam Rudd <adamvrr@gmail.com>",
7
7
  "Matteo Collina <matteo.collina@gmail.com> (https://github.com/mcollina)",
package/src/lib/client.ts CHANGED
@@ -64,7 +64,7 @@ const defaultConnectOptions: IClientOptions = {
64
64
  timerVariant: 'auto',
65
65
  }
66
66
 
67
- export type MqttProtocol =
67
+ export type BaseMqttProtocol =
68
68
  | 'wss'
69
69
  | 'ws'
70
70
  | 'mqtt'
@@ -76,6 +76,11 @@ export type MqttProtocol =
76
76
  | 'ali'
77
77
  | 'alis'
78
78
 
79
+ // create a type that allows all MqttProtocol + `+unix` string
80
+ export type MqttProtocolWithUnix = `${BaseMqttProtocol}+unix`
81
+
82
+ export type MqttProtocol = BaseMqttProtocol | MqttProtocolWithUnix
83
+
79
84
  export type StorePutCallback = () => void
80
85
 
81
86
  export interface ISecureClientOptions {
@@ -142,7 +147,9 @@ export interface IClientOptions extends ISecureClientOptions {
142
147
  host?: string
143
148
  /** @deprecated use `host instead */
144
149
  hostname?: string
145
- /** Websocket `path` added as suffix */
150
+ /** Set to true if the connection is to a unix socket */
151
+ unixSocket?: boolean
152
+ /** Websocket `path` added as suffix or Unix socket path when `unixSocket` option is true */
146
153
  path?: string
147
154
  /** The `MqttProtocol` to use */
148
155
  protocol?: MqttProtocol
@@ -71,31 +71,46 @@ function connect(
71
71
 
72
72
  opts = opts || {}
73
73
 
74
+ // try to parse the broker url
74
75
  if (brokerUrl && typeof brokerUrl === 'string') {
75
76
  // eslint-disable-next-line
76
- const parsed = url.parse(brokerUrl, true)
77
- if (parsed.port != null) {
77
+ const parsedUrl = url.parse(brokerUrl, true)
78
+ const parsedOptions: Partial<IClientOptions> = {}
79
+
80
+ if (parsedUrl.port != null) {
78
81
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
79
82
  // @ts-ignore
80
- parsed.port = Number(parsed.port)
83
+ parsedOptions.port = Number(parsedUrl.port)
81
84
  }
82
85
 
83
- opts = {
84
- ...{
85
- port: parsed.port,
86
- host: parsed.hostname,
87
- protocol: parsed.protocol,
88
- query: parsed.query,
89
- auth: parsed.auth,
90
- },
91
- ...opts,
92
- } as IClientOptions
86
+ parsedOptions.host = parsedUrl.hostname
87
+ parsedOptions.query = parsedUrl.query as Record<string, string>
88
+ parsedOptions.auth = parsedUrl.auth
89
+ parsedOptions.protocol = parsedUrl.protocol as MqttProtocol
90
+ parsedOptions.path = parsedUrl.path
91
+
92
+ parsedOptions.protocol = parsedOptions.protocol?.replace(
93
+ /:$/,
94
+ '',
95
+ ) as MqttProtocol
93
96
 
94
- if (opts.protocol === null) {
97
+ opts = { ...parsedOptions, ...opts }
98
+
99
+ // when parsing an url expect the protocol to be set
100
+ if (!opts.protocol) {
95
101
  throw new Error('Missing protocol')
96
102
  }
103
+ }
104
+
105
+ opts.unixSocket = opts.unixSocket || opts.protocol?.includes('+unix')
97
106
 
98
- opts.protocol = opts.protocol.replace(/:$/, '') as MqttProtocol
107
+ if (opts.unixSocket) {
108
+ opts.protocol = opts.protocol.replace('+unix', '') as MqttProtocol
109
+ } else if (!opts.protocol?.startsWith('ws')) {
110
+ // consider path only with ws protocol or unix socket
111
+ // url.parse could return path (for example when url ends with a `/`)
112
+ // that could break the connection. See https://github.com/mqttjs/MQTT.js/pull/1874
113
+ delete opts.path
99
114
  }
100
115
 
101
116
  // merge in the auth options if supplied
@@ -136,6 +151,9 @@ function connect(
136
151
 
137
152
  if (!protocols[opts.protocol]) {
138
153
  const isSecure = ['mqtts', 'wss'].indexOf(opts.protocol) !== -1
154
+ // returns the first available protocol based on available protocols (that depends on environment)
155
+ // if no protocol is specified this will return mqtt on node and ws on browser
156
+ // if secure it will return mqtts on node and wss on browser
139
157
  opts.protocol = [
140
158
  'mqtt',
141
159
  'mqtts',
@@ -54,7 +54,7 @@ const handleAck: PacketHandler = (client, packet) => {
54
54
  const type = packet.cmd
55
55
  let response = null
56
56
  const cb = client.outgoing[messageId] ? client.outgoing[messageId].cb : null
57
- let err
57
+ let err = null
58
58
 
59
59
  // Checking `!cb` happens to work, but it's not technically "correct".
60
60
  //
@@ -117,7 +117,11 @@ const handleAck: PacketHandler = (client, packet) => {
117
117
  client.messageIdProvider.deallocate(messageId)
118
118
  const granted = packet.granted as number[]
119
119
  for (let grantedI = 0; grantedI < granted.length; grantedI++) {
120
- if ((granted[grantedI] & 0x80) !== 0) {
120
+ const subackRC = granted[grantedI]
121
+ if ((subackRC & 0x80) !== 0) {
122
+ err = new Error(`Subscribe error: ${ReasonCodes[subackRC]}`)
123
+ err.code = subackRC
124
+
121
125
  // suback with Failure status
122
126
  const topics = client.messageIdToTopic[messageId]
123
127
  if (topics) {
@@ -129,7 +133,7 @@ const handleAck: PacketHandler = (client, packet) => {
129
133
  }
130
134
  delete client.messageIdToTopic[messageId]
131
135
  client['_invokeStoreProcessingQueue']()
132
- cb(null, packet)
136
+ cb(err, packet)
133
137
  break
134
138
  }
135
139
  case 'unsuback': {