mqtt 5.8.1 → 5.9.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 -7
- package/build/lib/BufferedDuplex.js +3 -1
- package/build/lib/BufferedDuplex.js.map +1 -1
- package/build/lib/client.d.ts +2 -1
- package/build/lib/client.js.map +1 -1
- package/build/lib/connect/index.d.ts +1 -0
- package/build/lib/connect/index.js +21 -16
- package/build/lib/connect/index.js.map +1 -1
- package/build/lib/connect/ws.d.ts +3 -2
- package/build/lib/connect/ws.js +7 -3
- package/build/lib/connect/ws.js.map +1 -1
- package/build/lib/handlers/ack.js +3 -4
- package/build/lib/handlers/ack.js.map +1 -1
- package/build/lib/is-browser.js +1 -5
- package/build/lib/is-browser.js.map +1 -1
- package/build/tsconfig.build.tsbuildinfo +1 -1
- package/dist/mqtt.esm.js +5 -5
- package/dist/mqtt.js +553 -553
- package/dist/mqtt.min.js +5 -5
- package/package.json +1 -1
- package/src/lib/BufferedDuplex.ts +3 -1
- package/src/lib/client.ts +8 -1
- package/src/lib/connect/index.ts +31 -19
- package/src/lib/connect/ws.ts +6 -4
- package/src/lib/handlers/ack.ts +9 -5
- package/src/lib/is-browser.ts +1 -7
package/package.json
CHANGED
package/src/lib/client.ts
CHANGED
|
@@ -282,6 +282,10 @@ export interface IClientOptions extends ISecureClientOptions {
|
|
|
282
282
|
* or pass a custom timer object
|
|
283
283
|
*/
|
|
284
284
|
timerVariant?: TimerVariant | Timer
|
|
285
|
+
/**
|
|
286
|
+
* false, set to true to force the use of native WebSocket if you're having issues with the detection
|
|
287
|
+
*/
|
|
288
|
+
forceNativeWebSocket?: boolean
|
|
285
289
|
}
|
|
286
290
|
|
|
287
291
|
export interface IClientPublishOptions {
|
|
@@ -381,7 +385,10 @@ export type OnMessageCallback = (
|
|
|
381
385
|
export type OnPacketCallback = (packet: Packet) => void
|
|
382
386
|
export type OnCloseCallback = () => void
|
|
383
387
|
export type OnErrorCallback = (error: Error | ErrorWithReasonCode) => void
|
|
384
|
-
export type PacketCallback = (
|
|
388
|
+
export type PacketCallback = (
|
|
389
|
+
error?: Error | ErrorWithReasonCode,
|
|
390
|
+
packet?: Packet,
|
|
391
|
+
) => any
|
|
385
392
|
export type CloseCallback = (error?: Error) => void
|
|
386
393
|
|
|
387
394
|
export interface MqttClientEventCallbacks {
|
package/src/lib/connect/index.ts
CHANGED
|
@@ -6,7 +6,7 @@ import MqttClient, {
|
|
|
6
6
|
MqttClientEventCallbacks,
|
|
7
7
|
MqttProtocol,
|
|
8
8
|
} from '../client'
|
|
9
|
-
import
|
|
9
|
+
import isBrowser from '../is-browser'
|
|
10
10
|
import { StreamBuilder } from '../shared'
|
|
11
11
|
|
|
12
12
|
// Handling the process.nextTick is not a function error in react-native applications.
|
|
@@ -16,24 +16,7 @@ if (typeof process?.nextTick !== 'function') {
|
|
|
16
16
|
|
|
17
17
|
const debug = _debug('mqttjs')
|
|
18
18
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
if (!IS_BROWSER) {
|
|
22
|
-
protocols.mqtt = require('./tcp').default
|
|
23
|
-
protocols.tcp = require('./tcp').default
|
|
24
|
-
protocols.ssl = require('./tls').default
|
|
25
|
-
protocols.tls = protocols.ssl
|
|
26
|
-
protocols.mqtts = require('./tls').default
|
|
27
|
-
} else {
|
|
28
|
-
protocols.wx = require('./wx').default
|
|
29
|
-
protocols.wxs = require('./wx').default
|
|
30
|
-
|
|
31
|
-
protocols.ali = require('./ali').default
|
|
32
|
-
protocols.alis = require('./ali').default
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
protocols.ws = require('./ws').default
|
|
36
|
-
protocols.wss = require('./ws').default
|
|
19
|
+
let protocols: Record<string, StreamBuilder> = null
|
|
37
20
|
|
|
38
21
|
/**
|
|
39
22
|
* Parse the auth attribute and merge username and password in the options object.
|
|
@@ -152,6 +135,30 @@ function connect(
|
|
|
152
135
|
}
|
|
153
136
|
}
|
|
154
137
|
|
|
138
|
+
// only loads the protocols once
|
|
139
|
+
if (!protocols) {
|
|
140
|
+
protocols = {}
|
|
141
|
+
if (!isBrowser && !opts.forceNativeWebSocket) {
|
|
142
|
+
protocols.ws = require('./ws').streamBuilder
|
|
143
|
+
protocols.wss = require('./ws').streamBuilder
|
|
144
|
+
|
|
145
|
+
protocols.mqtt = require('./tcp').default
|
|
146
|
+
protocols.tcp = require('./tcp').default
|
|
147
|
+
protocols.ssl = require('./tls').default
|
|
148
|
+
protocols.tls = protocols.ssl
|
|
149
|
+
protocols.mqtts = require('./tls').default
|
|
150
|
+
} else {
|
|
151
|
+
protocols.ws = require('./ws').browserStreamBuilder
|
|
152
|
+
protocols.wss = require('./ws').browserStreamBuilder
|
|
153
|
+
|
|
154
|
+
protocols.wx = require('./wx').default
|
|
155
|
+
protocols.wxs = require('./wx').default
|
|
156
|
+
|
|
157
|
+
protocols.ali = require('./ali').default
|
|
158
|
+
protocols.alis = require('./ali').default
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
155
162
|
if (!protocols[opts.protocol]) {
|
|
156
163
|
const isSecure = ['mqtts', 'wss'].indexOf(opts.protocol) !== -1
|
|
157
164
|
// returns the first available protocol based on available protocols (that depends on environment)
|
|
@@ -219,6 +226,11 @@ function connectAsync(
|
|
|
219
226
|
brokerUrl: string,
|
|
220
227
|
opts?: IClientOptions,
|
|
221
228
|
): Promise<MqttClient>
|
|
229
|
+
function connectAsync(
|
|
230
|
+
brokerUrl: string,
|
|
231
|
+
opts: IClientOptions,
|
|
232
|
+
allowRetries: boolean,
|
|
233
|
+
): Promise<MqttClient>
|
|
222
234
|
function connectAsync(
|
|
223
235
|
brokerUrl: string | IClientOptions,
|
|
224
236
|
opts?: IClientOptions,
|
package/src/lib/connect/ws.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { Buffer } from 'buffer'
|
|
|
3
3
|
import Ws, { ClientOptions } from 'ws'
|
|
4
4
|
import _debug from 'debug'
|
|
5
5
|
import { DuplexOptions, Transform } from 'readable-stream'
|
|
6
|
-
import
|
|
6
|
+
import isBrowser from '../is-browser'
|
|
7
7
|
import MqttClient, { IClientOptions } from '../client'
|
|
8
8
|
import { BufferedDuplex, writev } from '../BufferedDuplex'
|
|
9
9
|
|
|
@@ -44,7 +44,7 @@ function setDefaultOpts(opts: IClientOptions) {
|
|
|
44
44
|
if (!opts.wsOptions) {
|
|
45
45
|
options.wsOptions = {}
|
|
46
46
|
}
|
|
47
|
-
if (!
|
|
47
|
+
if (!isBrowser && !opts.forceNativeWebSocket && opts.protocol === 'wss') {
|
|
48
48
|
// Add cert/key/ca etc options
|
|
49
49
|
WSS_OPTIONS.forEach((prop) => {
|
|
50
50
|
if (
|
|
@@ -260,7 +260,9 @@ const browserStreamBuilder: StreamBuilder = (client, opts) => {
|
|
|
260
260
|
let { data } = event
|
|
261
261
|
if (data instanceof ArrayBuffer) data = Buffer.from(data)
|
|
262
262
|
else data = Buffer.from(data as string, 'utf8')
|
|
263
|
-
proxy.
|
|
263
|
+
if (proxy && !proxy.destroyed) {
|
|
264
|
+
proxy.push(data)
|
|
265
|
+
}
|
|
264
266
|
}
|
|
265
267
|
|
|
266
268
|
function socketWriteBrowser(
|
|
@@ -298,4 +300,4 @@ const browserStreamBuilder: StreamBuilder = (client, opts) => {
|
|
|
298
300
|
return stream
|
|
299
301
|
}
|
|
300
302
|
|
|
301
|
-
export
|
|
303
|
+
export { browserStreamBuilder, streamBuilder }
|
package/src/lib/handlers/ack.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// Other Socket Errors: EADDRINUSE, ECONNRESET, ENOTFOUND, ETIMEDOUT.
|
|
2
2
|
|
|
3
|
-
import { PacketHandler } from '../shared'
|
|
3
|
+
import { PacketHandler, ErrorWithReasonCode } from '../shared'
|
|
4
4
|
|
|
5
5
|
export const ReasonCodes = {
|
|
6
6
|
0: '',
|
|
@@ -82,8 +82,10 @@ const handleAck: PacketHandler = (client, packet) => {
|
|
|
82
82
|
const pubackRC = packet.reasonCode
|
|
83
83
|
// Callback - we're done
|
|
84
84
|
if (pubackRC && pubackRC > 0 && pubackRC !== 16) {
|
|
85
|
-
err = new
|
|
86
|
-
|
|
85
|
+
err = new ErrorWithReasonCode(
|
|
86
|
+
`Publish error: ${ReasonCodes[pubackRC]}`,
|
|
87
|
+
pubackRC,
|
|
88
|
+
)
|
|
87
89
|
client['_removeOutgoingAndStoreMessage'](messageId, () => {
|
|
88
90
|
cb(err, packet)
|
|
89
91
|
})
|
|
@@ -102,8 +104,10 @@ const handleAck: PacketHandler = (client, packet) => {
|
|
|
102
104
|
const pubrecRC = packet.reasonCode
|
|
103
105
|
|
|
104
106
|
if (pubrecRC && pubrecRC > 0 && pubrecRC !== 16) {
|
|
105
|
-
err = new
|
|
106
|
-
|
|
107
|
+
err = new ErrorWithReasonCode(
|
|
108
|
+
`Publish error: ${ReasonCodes[pubrecRC]}`,
|
|
109
|
+
pubrecRC,
|
|
110
|
+
)
|
|
107
111
|
client['_removeOutgoingAndStoreMessage'](messageId, () => {
|
|
108
112
|
cb(err, packet)
|
|
109
113
|
})
|
package/src/lib/is-browser.ts
CHANGED
|
@@ -25,9 +25,6 @@ const isStandardBrowserEnv = () => {
|
|
|
25
25
|
return false
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
const isTxikijsEnv = () =>
|
|
29
|
-
typeof navigator !== 'undefined' && navigator.userAgent === 'txiki.js'
|
|
30
|
-
|
|
31
28
|
const isWebWorkerEnv = () =>
|
|
32
29
|
Boolean(
|
|
33
30
|
// eslint-disable-next-line no-restricted-globals
|
|
@@ -40,10 +37,7 @@ const isReactNativeEnv = () =>
|
|
|
40
37
|
typeof navigator !== 'undefined' && navigator.product === 'ReactNative'
|
|
41
38
|
|
|
42
39
|
const isBrowser =
|
|
43
|
-
isStandardBrowserEnv() ||
|
|
44
|
-
isWebWorkerEnv() ||
|
|
45
|
-
isReactNativeEnv() ||
|
|
46
|
-
isTxikijsEnv()
|
|
40
|
+
isStandardBrowserEnv() || isWebWorkerEnv() || isReactNativeEnv()
|
|
47
41
|
|
|
48
42
|
export const isWebWorker = isWebWorkerEnv()
|
|
49
43
|
|