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/README.md +8 -3
- package/build/lib/client.d.ts +4 -1
- package/build/lib/client.js.map +1 -1
- package/build/lib/connect/index.js +20 -12
- package/build/lib/connect/index.js.map +1 -1
- package/build/lib/handlers/ack.js +6 -3
- package/build/lib/handlers/ack.js.map +1 -1
- package/build/tsconfig.build.tsbuildinfo +1 -1
- package/dist/mqtt.esm.js +4 -4
- package/dist/mqtt.js +27 -17
- package/dist/mqtt.min.js +4 -4
- package/package.json +1 -1
- package/src/lib/client.ts +9 -2
- package/src/lib/connect/index.ts +33 -15
- package/src/lib/handlers/ack.ts +7 -3
package/package.json
CHANGED
package/src/lib/client.ts
CHANGED
|
@@ -64,7 +64,7 @@ const defaultConnectOptions: IClientOptions = {
|
|
|
64
64
|
timerVariant: 'auto',
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
-
export type
|
|
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
|
-
/**
|
|
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
|
package/src/lib/connect/index.ts
CHANGED
|
@@ -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
|
|
77
|
-
|
|
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
|
-
|
|
83
|
+
parsedOptions.port = Number(parsedUrl.port)
|
|
81
84
|
}
|
|
82
85
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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',
|
package/src/lib/handlers/ack.ts
CHANGED
|
@@ -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
|
-
|
|
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(
|
|
136
|
+
cb(err, packet)
|
|
133
137
|
break
|
|
134
138
|
}
|
|
135
139
|
case 'unsuback': {
|