mqtt 5.4.0 → 5.5.0

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.4.0",
4
+ "version": "5.5.0",
5
5
  "contributors": [
6
6
  "Adam Rudd <adamvrr@gmail.com>",
7
7
  "Matteo Collina <matteo.collina@gmail.com> (https://github.com/mcollina)",
@@ -63,7 +63,7 @@
63
63
  "build:browser": "node esbuild.js",
64
64
  "build": "npm run build:ts && npm run build:browser",
65
65
  "prepare": "npm run build",
66
- "unit-test:node": "node_modules/.bin/nyc --reporter=lcov --reporter=text node -r esbuild-register test/runTests.ts",
66
+ "unit-test:node": "node_modules/.bin/nyc node -r esbuild-register test/runTests.ts",
67
67
  "unit-test:browser": "wtr",
68
68
  "test:node": "npm run unit-test:node && codecov",
69
69
  "test:browser": "npm run build && npm run unit-test:browser",
@@ -77,6 +77,12 @@ export class BufferedDuplex extends Duplex {
77
77
  this.proxy.end(callback)
78
78
  }
79
79
 
80
+ _destroy(err: Error, callback: (error: Error) => void): void {
81
+ this.writeQueue = []
82
+ this.proxy.destroy(err)
83
+ callback(err)
84
+ }
85
+
80
86
  /** Method to call when socket is ready to stop buffering writes */
81
87
  socketReady() {
82
88
  this.emit('connect')
@@ -1,32 +1,40 @@
1
- import timers from './timers'
1
+ import getTimer, { type Timer } from './get-timer'
2
+ import type { TimerVariant } from './shared'
2
3
 
3
4
  export default class PingTimer {
4
5
  private keepalive: number
5
6
 
6
- private timer: any
7
+ private timerId: number
8
+
9
+ private timer: Timer
7
10
 
8
11
  private checkPing: () => void
9
12
 
10
- constructor(keepalive: number, checkPing: () => void) {
13
+ constructor(
14
+ keepalive: number,
15
+ checkPing: () => void,
16
+ variant: TimerVariant,
17
+ ) {
11
18
  this.keepalive = keepalive * 1000
12
19
  this.checkPing = checkPing
20
+ this.timer = getTimer(variant)
13
21
  this.reschedule()
14
22
  }
15
23
 
16
24
  clear() {
17
- if (this.timer) {
18
- timers.clear(this.timer)
19
- this.timer = null
25
+ if (this.timerId) {
26
+ this.timer.clear(this.timerId)
27
+ this.timerId = null
20
28
  }
21
29
  }
22
30
 
23
31
  reschedule() {
24
32
  this.clear()
25
- this.timer = timers.set(() => {
33
+ this.timerId = this.timer.set(() => {
26
34
  this.checkPing()
27
35
  // prevent possible race condition where the timer is destroyed on _cleauUp
28
36
  // and recreated here
29
- if (this.timer) {
37
+ if (this.timerId) {
30
38
  this.reschedule()
31
39
  }
32
40
  }, this.keepalive)
package/src/lib/client.ts CHANGED
@@ -32,6 +32,7 @@ import {
32
32
  GenericCallback,
33
33
  IStream,
34
34
  StreamBuilder,
35
+ TimerVariant,
35
36
  VoidCallback,
36
37
  nextTick,
37
38
  } from './shared'
@@ -49,7 +50,7 @@ const setImmediate =
49
50
  })
50
51
  }) as typeof globalThis.setImmediate)
51
52
 
52
- const defaultConnectOptions = {
53
+ const defaultConnectOptions: IClientOptions = {
53
54
  keepalive: 60,
54
55
  reschedulePings: true,
55
56
  protocolId: 'MQTT',
@@ -59,6 +60,7 @@ const defaultConnectOptions = {
59
60
  clean: true,
60
61
  resubscribe: true,
61
62
  writeCache: true,
63
+ timerVariant: 'auto',
62
64
  }
63
65
 
64
66
  export type MqttProtocol =
@@ -266,6 +268,10 @@ export interface IClientOptions extends ISecureClientOptions {
266
268
  will?: IConnectPacket['will']
267
269
  /** see `connect` packet: https://github.com/mqttjs/mqtt-packet/blob/master/types/index.d.ts#L65 */
268
270
  properties?: IConnectPacket['properties']
271
+ /**
272
+ * @description 'auto', set to 'native' or 'worker' if you're having issues with 'auto' detection
273
+ */
274
+ timerVariant?: TimerVariant
269
275
  }
270
276
 
271
277
  export interface IClientPublishOptions {
@@ -390,6 +396,8 @@ export interface MqttClientEventCallbacks {
390
396
  * (see Connection#connect)
391
397
  */
392
398
  export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbacks> {
399
+ public static VERSION = process.env.npm_package_version
400
+
393
401
  /** Public fields */
394
402
 
395
403
  /** It's true when client is connected to broker */
@@ -489,7 +497,7 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
489
497
  this.log = this.options.log || _debug('mqttjs:client')
490
498
  this.noop = this._noop.bind(this)
491
499
 
492
- this.log('MqttClient :: version:', process.env.npm_package_version)
500
+ this.log('MqttClient :: version:', MqttClient.VERSION)
493
501
 
494
502
  if (isWebWorker) {
495
503
  this.log('MqttClient :: environment', 'webworker')
@@ -2076,9 +2084,13 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
2076
2084
 
2077
2085
  if (!this.pingTimer && this.options.keepalive) {
2078
2086
  this.pingResp = true
2079
- this.pingTimer = new PingTimer(this.options.keepalive, () => {
2080
- this._checkPing()
2081
- })
2087
+ this.pingTimer = new PingTimer(
2088
+ this.options.keepalive,
2089
+ () => {
2090
+ this._checkPing()
2091
+ },
2092
+ this.options.timerVariant,
2093
+ )
2082
2094
  }
2083
2095
  }
2084
2096
 
@@ -120,6 +120,7 @@ function createWebSocket(
120
120
  return socket
121
121
  }
122
122
 
123
+ /* istanbul ignore next */
123
124
  function createBrowserWebSocket(client: MqttClient, opts: IClientOptions) {
124
125
  const websocketSubProtocol =
125
126
  opts.protocolId === 'MQIsdp' && opts.protocolVersion === 3
@@ -154,6 +155,7 @@ const streamBuilder: StreamBuilder = (client, opts) => {
154
155
  return webSocketStream
155
156
  }
156
157
 
158
+ /* istanbul ignore next */
157
159
  const browserStreamBuilder: StreamBuilder = (client, opts) => {
158
160
  debug('browserStreamBuilder')
159
161
  let stream: BufferedDuplex | (Transform & { socket?: WebSocket })
@@ -0,0 +1,38 @@
1
+ import isBrowser, { isWebWorker } from './is-browser'
2
+ import { clearTimeout as clearT, setTimeout as setT } from 'worker-timers'
3
+ import type { TimerVariant } from './shared'
4
+
5
+ // dont directly assign globals to class props otherwise this throws in web workers: Uncaught TypeError: Illegal invocation
6
+ // See: https://stackoverflow.com/questions/9677985/uncaught-typeerror-illegal-invocation-in-chrome
7
+
8
+ export interface Timer {
9
+ set: typeof setT
10
+ clear: typeof clearT
11
+ }
12
+
13
+ const workerTimer: Timer = {
14
+ set: setT,
15
+ clear: clearT,
16
+ }
17
+
18
+ const nativeTimer: Timer = {
19
+ set: (func, time) => setTimeout(func, time),
20
+ clear: (timerId) => clearTimeout(timerId),
21
+ }
22
+
23
+ const getTimer = (variant: TimerVariant): Timer => {
24
+ switch (variant) {
25
+ case 'native': {
26
+ return nativeTimer
27
+ }
28
+ case 'worker': {
29
+ return workerTimer
30
+ }
31
+ case 'auto':
32
+ default: {
33
+ return isBrowser && !isWebWorker ? workerTimer : nativeTimer
34
+ }
35
+ }
36
+ }
37
+
38
+ export default getTimer
package/src/lib/shared.ts CHANGED
@@ -27,6 +27,8 @@ export type PacketHandler = (
27
27
  done?: DoneCallback,
28
28
  ) => void
29
29
 
30
+ export type TimerVariant = 'auto' | 'worker' | 'native'
31
+
30
32
  export class ErrorWithReasonCode extends Error {
31
33
  public code: number
32
34
 
@@ -1,6 +0,0 @@
1
- import { clearTimeout as clearT, setTimeout as setT } from 'worker-timers';
2
- declare const timers: {
3
- set: typeof setT;
4
- clear: typeof clearT;
5
- };
6
- export default timers;
@@ -1 +0,0 @@
1
- {"version":3,"file":"timers.js","sourceRoot":"","sources":["../../src/lib/timers.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAAA,2DAAqD;AACrD,iDAA0E;AAK1E,MAAM,MAAM,GAA+C;IAC1D,GAAG,EACF,oBAAS,IAAI,CAAC,wBAAW;QACxB,CAAC,CAAC,0BAAI;QACN,CAAC,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,EAAE,CAAC,UAAU,CAAC,IAAI,EAAE,IAAI,CAAC;IAC1C,KAAK,EAAE,oBAAS,IAAI,CAAC,wBAAW,CAAC,CAAC,CAAC,4BAAM,CAAC,CAAC,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,YAAY,CAAC,KAAK,CAAC;CAC1E,CAAA;AAED,kBAAe,MAAM,CAAA"}
package/src/lib/timers.ts DELETED
@@ -1,15 +0,0 @@
1
- import isBrowser, { isWebWorker } from './is-browser'
2
- import { clearTimeout as clearT, setTimeout as setT } from 'worker-timers'
3
-
4
- // dont directly assign globals to class props otherwise this throws in web workers: Uncaught TypeError: Illegal invocation
5
- // See: https://stackoverflow.com/questions/9677985/uncaught-typeerror-illegal-invocation-in-chrome
6
-
7
- const timers: { set: typeof setT; clear: typeof clearT } = {
8
- set:
9
- isBrowser && !isWebWorker
10
- ? setT
11
- : (func, time) => setTimeout(func, time),
12
- clear: isBrowser && !isWebWorker ? clearT : (timer) => clearTimeout(timer),
13
- }
14
-
15
- export default timers