mqtt 5.3.0 → 5.3.2
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/build/lib/PingTimer.d.ts +11 -0
- package/build/lib/PingTimer.js +34 -0
- package/build/lib/PingTimer.js.map +1 -0
- package/build/lib/client.d.ts +3 -2
- package/build/lib/client.js +10 -21
- package/build/lib/client.js.map +1 -1
- package/build/lib/is-browser.js +8 -5
- package/build/lib/is-browser.js.map +1 -1
- package/build/lib/shared.d.ts +2 -1
- package/build/lib/shared.js +6 -1
- package/build/lib/shared.js.map +1 -1
- package/build/mqtt.js.map +1 -1
- package/build/tsconfig.build.tsbuildinfo +1 -1
- package/dist/mqtt.esm.js +5 -5
- package/dist/mqtt.js +349 -93
- package/dist/mqtt.min.js +5 -5
- package/package.json +2 -1
- package/src/lib/PingTimer.ts +39 -0
- package/src/lib/client.ts +13 -24
- package/src/lib/is-browser.ts +15 -8
- package/src/lib/shared.ts +7 -1
- package/src/mqtt.ts +0 -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.3.
|
|
4
|
+
"version": "5.3.2",
|
|
5
5
|
"contributors": [
|
|
6
6
|
"Adam Rudd <adamvrr@gmail.com>",
|
|
7
7
|
"Matteo Collina <matteo.collina@gmail.com> (https://github.com/mcollina)",
|
|
@@ -123,6 +123,7 @@
|
|
|
123
123
|
"reinterval": "^1.1.0",
|
|
124
124
|
"rfdc": "^1.3.0",
|
|
125
125
|
"split2": "^4.2.0",
|
|
126
|
+
"worker-timers": "^7.0.78",
|
|
126
127
|
"ws": "^8.14.2"
|
|
127
128
|
},
|
|
128
129
|
"devDependencies": {
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { clearTimeout as clearT, setTimeout as setT } from 'worker-timers'
|
|
2
|
+
import isBrowser from './is-browser'
|
|
3
|
+
|
|
4
|
+
export default class PingTimer {
|
|
5
|
+
private keepalive: number
|
|
6
|
+
|
|
7
|
+
private timer: any
|
|
8
|
+
|
|
9
|
+
private checkPing: () => void
|
|
10
|
+
|
|
11
|
+
private setTimeout = isBrowser ? setT : setTimeout
|
|
12
|
+
|
|
13
|
+
private clearTimeout = isBrowser ? clearT : clearTimeout
|
|
14
|
+
|
|
15
|
+
constructor(keepalive: number, checkPing: () => void) {
|
|
16
|
+
this.keepalive = keepalive * 1000
|
|
17
|
+
this.checkPing = checkPing
|
|
18
|
+
this.setup()
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
private setup() {
|
|
22
|
+
this.timer = this.setTimeout(() => {
|
|
23
|
+
this.checkPing()
|
|
24
|
+
this.reschedule()
|
|
25
|
+
}, this.keepalive)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
clear() {
|
|
29
|
+
if (this.timer) {
|
|
30
|
+
this.clearTimeout(this.timer)
|
|
31
|
+
this.timer = null
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
reschedule() {
|
|
36
|
+
this.clear()
|
|
37
|
+
this.setup()
|
|
38
|
+
}
|
|
39
|
+
}
|
package/src/lib/client.ts
CHANGED
|
@@ -19,7 +19,6 @@ import DefaultMessageIdProvider, {
|
|
|
19
19
|
IMessageIdProvider,
|
|
20
20
|
} from './default-message-id-provider'
|
|
21
21
|
import { DuplexOptions, Writable } from 'readable-stream'
|
|
22
|
-
import reInterval from 'reinterval'
|
|
23
22
|
import clone from 'rfdc/default'
|
|
24
23
|
import * as validations from './validations'
|
|
25
24
|
import _debug from 'debug'
|
|
@@ -34,24 +33,20 @@ import {
|
|
|
34
33
|
IStream,
|
|
35
34
|
StreamBuilder,
|
|
36
35
|
VoidCallback,
|
|
36
|
+
nextTick,
|
|
37
37
|
} from './shared'
|
|
38
38
|
import TopicAliasSend from './topic-alias-send'
|
|
39
39
|
import { TypedEventEmitter } from './TypedEmitter'
|
|
40
|
-
|
|
41
|
-
const nextTick = process
|
|
42
|
-
? process.nextTick
|
|
43
|
-
: (callback: () => void) => {
|
|
44
|
-
setTimeout(callback, 0)
|
|
45
|
-
}
|
|
40
|
+
import PingTimer from './PingTimer'
|
|
46
41
|
|
|
47
42
|
const setImmediate =
|
|
48
|
-
|
|
49
|
-
((...args: any[]) => {
|
|
43
|
+
globalThis.setImmediate ||
|
|
44
|
+
(((...args: any[]) => {
|
|
50
45
|
const callback = args.shift()
|
|
51
46
|
nextTick(() => {
|
|
52
47
|
callback(...args)
|
|
53
48
|
})
|
|
54
|
-
})
|
|
49
|
+
}) as typeof globalThis.setImmediate)
|
|
55
50
|
|
|
56
51
|
const defaultConnectOptions = {
|
|
57
52
|
keepalive: 60,
|
|
@@ -65,14 +60,6 @@ const defaultConnectOptions = {
|
|
|
65
60
|
writeCache: true,
|
|
66
61
|
}
|
|
67
62
|
|
|
68
|
-
const socketErrors = [
|
|
69
|
-
'ECONNREFUSED',
|
|
70
|
-
'EADDRINUSE',
|
|
71
|
-
'ECONNRESET',
|
|
72
|
-
'ENOTFOUND',
|
|
73
|
-
'ETIMEDOUT',
|
|
74
|
-
]
|
|
75
|
-
|
|
76
63
|
export type MqttProtocol =
|
|
77
64
|
| 'wss'
|
|
78
65
|
| 'ws'
|
|
@@ -367,7 +354,7 @@ export type OnConnectCallback = (packet: IConnackPacket) => void
|
|
|
367
354
|
export type OnDisconnectCallback = (packet: IDisconnectPacket) => void
|
|
368
355
|
export type ClientSubscribeCallback = (
|
|
369
356
|
err: Error | null,
|
|
370
|
-
granted
|
|
357
|
+
granted?: ISubscriptionGrant[],
|
|
371
358
|
) => void
|
|
372
359
|
export type OnMessageCallback = (
|
|
373
360
|
topic: string,
|
|
@@ -438,7 +425,7 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
|
|
|
438
425
|
|
|
439
426
|
public noop: (error?: any) => void
|
|
440
427
|
|
|
441
|
-
public pingTimer:
|
|
428
|
+
public pingTimer: PingTimer
|
|
442
429
|
|
|
443
430
|
/**
|
|
444
431
|
* The connection to the Broker. In browsers env this also have `socket` property
|
|
@@ -765,7 +752,9 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
|
|
|
765
752
|
|
|
766
753
|
const streamErrorHandler = (error) => {
|
|
767
754
|
this.log('streamErrorHandler :: error', error.message)
|
|
768
|
-
|
|
755
|
+
// error.code will only be set on NodeJS env, browser don't allow to detect errors on sockets
|
|
756
|
+
// also emitting errors on browsers seems to create issues
|
|
757
|
+
if (error.code) {
|
|
769
758
|
// handle error
|
|
770
759
|
this.log('streamErrorHandler :: emitting error')
|
|
771
760
|
this.emit('error', error)
|
|
@@ -2074,9 +2063,9 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
|
|
|
2074
2063
|
|
|
2075
2064
|
if (!this.pingTimer && this.options.keepalive) {
|
|
2076
2065
|
this.pingResp = true
|
|
2077
|
-
this.pingTimer =
|
|
2066
|
+
this.pingTimer = new PingTimer(this.options.keepalive, () => {
|
|
2078
2067
|
this._checkPing()
|
|
2079
|
-
}
|
|
2068
|
+
})
|
|
2080
2069
|
}
|
|
2081
2070
|
}
|
|
2082
2071
|
|
|
@@ -2091,7 +2080,7 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
|
|
|
2091
2080
|
this.options.keepalive &&
|
|
2092
2081
|
this.options.reschedulePings
|
|
2093
2082
|
) {
|
|
2094
|
-
this.pingTimer.reschedule(
|
|
2083
|
+
this.pingTimer.reschedule()
|
|
2095
2084
|
}
|
|
2096
2085
|
}
|
|
2097
2086
|
|
package/src/lib/is-browser.ts
CHANGED
|
@@ -1,11 +1,18 @@
|
|
|
1
|
-
const
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
self.constructor &&
|
|
1
|
+
const isStandardBrowserEnv = () =>
|
|
2
|
+
typeof window !== 'undefined' && typeof window.document !== 'undefined'
|
|
3
|
+
|
|
4
|
+
const isWebWorkerEnv = () =>
|
|
5
|
+
Boolean(
|
|
7
6
|
// eslint-disable-next-line no-restricted-globals
|
|
8
|
-
self
|
|
9
|
-
|
|
7
|
+
typeof self === 'object' &&
|
|
8
|
+
// eslint-disable-next-line no-restricted-globals
|
|
9
|
+
self?.constructor?.name?.includes('WorkerGlobalScope'),
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
const isReactNativeEnv = () =>
|
|
13
|
+
typeof navigator !== 'undefined' && navigator.product === 'ReactNative'
|
|
14
|
+
|
|
15
|
+
const isBrowser =
|
|
16
|
+
isStandardBrowserEnv() || isWebWorkerEnv() || isReactNativeEnv()
|
|
10
17
|
|
|
11
18
|
export default isBrowser
|
package/src/lib/shared.ts
CHANGED
|
@@ -10,7 +10,8 @@ export type GenericCallback<T> = (error?: Error, result?: T) => void
|
|
|
10
10
|
export type VoidCallback = () => void
|
|
11
11
|
|
|
12
12
|
export type IStream = Duplex & {
|
|
13
|
-
|
|
13
|
+
/** only set on browsers, it's a [WebSocket](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket) */
|
|
14
|
+
socket?: any
|
|
14
15
|
}
|
|
15
16
|
|
|
16
17
|
export type StreamBuilder = (
|
|
@@ -73,3 +74,8 @@ export function applyMixin(
|
|
|
73
74
|
}
|
|
74
75
|
}
|
|
75
76
|
}
|
|
77
|
+
export const nextTick = process
|
|
78
|
+
? process.nextTick
|
|
79
|
+
: (callback: () => void) => {
|
|
80
|
+
setTimeout(callback, 0)
|
|
81
|
+
}
|