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/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.0",
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
- global.setImmediate ||
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: ISubscriptionGrant[],
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: any
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
- if (socketErrors.includes(error.code)) {
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 = reInterval(() => {
2066
+ this.pingTimer = new PingTimer(this.options.keepalive, () => {
2078
2067
  this._checkPing()
2079
- }, this.options.keepalive * 1000)
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(this.options.keepalive * 1000)
2083
+ this.pingTimer.reschedule()
2095
2084
  }
2096
2085
  }
2097
2086
 
@@ -1,11 +1,18 @@
1
- const isBrowser =
2
- (typeof window !== 'undefined' && typeof window.document !== 'undefined') ||
3
- // eslint-disable-next-line no-restricted-globals
4
- (typeof self === 'object' &&
5
- // eslint-disable-next-line no-restricted-globals
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.constructor.name === 'DedicatedWorkerGlobalScope') || // is web worker
9
- (typeof navigator !== 'undefined' && navigator.product === 'ReactNative') // while navigator.product is deprecated
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
- socket?: WebSocket
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
+ }
package/src/mqtt.ts CHANGED
@@ -4,7 +4,6 @@
4
4
  *
5
5
  * See LICENSE for more information
6
6
  */
7
-
8
7
  import MqttClient from './lib/client'
9
8
  import DefaultMessageIdProvider from './lib/default-message-id-provider'
10
9
  import UniqueMessageIdProvider from './lib/unique-message-id-provider'