mqtt 5.3.6 → 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.3.6",
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)",
@@ -39,7 +39,7 @@
39
39
  ".": {
40
40
  "browser": {
41
41
  "import": "./dist/mqtt.esm.js",
42
- "default": "./dist/index.js"
42
+ "default": "./dist/mqtt.min.js"
43
43
  },
44
44
  "default": "./build/index.js"
45
45
  },
@@ -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",
@@ -123,7 +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
+ "worker-timers": "^7.1.4",
127
127
  "ws": "^8.14.2"
128
128
  },
129
129
  "devDependencies": {
@@ -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,43 +1,40 @@
1
- import { clearTimeout as clearT, setTimeout as setT } from 'worker-timers'
2
- import isBrowser, { isWebWorker } from './is-browser'
1
+ import getTimer, { type Timer } from './get-timer'
2
+ import type { TimerVariant } from './shared'
3
3
 
4
4
  export default class PingTimer {
5
5
  private keepalive: number
6
6
 
7
- private timer: any
7
+ private timerId: number
8
8
 
9
- private checkPing: () => void
10
-
11
- // dont directly assign globals to class props otherwise this throws in web workers: Uncaught TypeError: Illegal invocation
12
- // See: https://stackoverflow.com/questions/9677985/uncaught-typeerror-illegal-invocation-in-chrome
13
- private _setTimeout: typeof setT =
14
- isBrowser && !isWebWorker
15
- ? setT
16
- : (func, time) => setTimeout(func, time)
9
+ private timer: Timer
17
10
 
18
- private _clearTimeout: typeof clearT =
19
- isBrowser && !isWebWorker ? clearT : (timer) => clearTimeout(timer)
11
+ private checkPing: () => void
20
12
 
21
- constructor(keepalive: number, checkPing: () => void) {
13
+ constructor(
14
+ keepalive: number,
15
+ checkPing: () => void,
16
+ variant: TimerVariant,
17
+ ) {
22
18
  this.keepalive = keepalive * 1000
23
19
  this.checkPing = checkPing
20
+ this.timer = getTimer(variant)
24
21
  this.reschedule()
25
22
  }
26
23
 
27
24
  clear() {
28
- if (this.timer) {
29
- this._clearTimeout(this.timer)
30
- this.timer = null
25
+ if (this.timerId) {
26
+ this.timer.clear(this.timerId)
27
+ this.timerId = null
31
28
  }
32
29
  }
33
30
 
34
31
  reschedule() {
35
32
  this.clear()
36
- this.timer = this._setTimeout(() => {
33
+ this.timerId = this.timer.set(() => {
37
34
  this.checkPing()
38
35
  // prevent possible race condition where the timer is destroyed on _cleauUp
39
36
  // and recreated here
40
- if (this.timer) {
37
+ if (this.timerId) {
41
38
  this.reschedule()
42
39
  }
43
40
  }, this.keepalive)
package/src/lib/client.ts CHANGED
@@ -32,12 +32,14 @@ import {
32
32
  GenericCallback,
33
33
  IStream,
34
34
  StreamBuilder,
35
+ TimerVariant,
35
36
  VoidCallback,
36
37
  nextTick,
37
38
  } from './shared'
38
39
  import TopicAliasSend from './topic-alias-send'
39
40
  import { TypedEventEmitter } from './TypedEmitter'
40
41
  import PingTimer from './PingTimer'
42
+ import isBrowser, { isWebWorker } from './is-browser'
41
43
 
42
44
  const setImmediate =
43
45
  globalThis.setImmediate ||
@@ -48,7 +50,7 @@ const setImmediate =
48
50
  })
49
51
  }) as typeof globalThis.setImmediate)
50
52
 
51
- const defaultConnectOptions = {
53
+ const defaultConnectOptions: IClientOptions = {
52
54
  keepalive: 60,
53
55
  reschedulePings: true,
54
56
  protocolId: 'MQTT',
@@ -58,6 +60,7 @@ const defaultConnectOptions = {
58
60
  clean: true,
59
61
  resubscribe: true,
60
62
  writeCache: true,
63
+ timerVariant: 'auto',
61
64
  }
62
65
 
63
66
  export type MqttProtocol =
@@ -265,6 +268,10 @@ export interface IClientOptions extends ISecureClientOptions {
265
268
  will?: IConnectPacket['will']
266
269
  /** see `connect` packet: https://github.com/mqttjs/mqtt-packet/blob/master/types/index.d.ts#L65 */
267
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
268
275
  }
269
276
 
270
277
  export interface IClientPublishOptions {
@@ -389,6 +396,8 @@ export interface MqttClientEventCallbacks {
389
396
  * (see Connection#connect)
390
397
  */
391
398
  export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbacks> {
399
+ public static VERSION = process.env.npm_package_version
400
+
392
401
  /** Public fields */
393
402
 
394
403
  /** It's true when client is connected to broker */
@@ -488,6 +497,17 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
488
497
  this.log = this.options.log || _debug('mqttjs:client')
489
498
  this.noop = this._noop.bind(this)
490
499
 
500
+ this.log('MqttClient :: version:', MqttClient.VERSION)
501
+
502
+ if (isWebWorker) {
503
+ this.log('MqttClient :: environment', 'webworker')
504
+ } else {
505
+ this.log(
506
+ 'MqttClient :: environment',
507
+ isBrowser ? 'browser' : 'node',
508
+ )
509
+ }
510
+
491
511
  this.log('MqttClient :: options.protocol', options.protocol)
492
512
  this.log(
493
513
  'MqttClient :: options.protocolVersion',
@@ -2064,9 +2084,13 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
2064
2084
 
2065
2085
  if (!this.pingTimer && this.options.keepalive) {
2066
2086
  this.pingResp = true
2067
- this.pingTimer = new PingTimer(this.options.keepalive, () => {
2068
- this._checkPing()
2069
- })
2087
+ this.pingTimer = new PingTimer(
2088
+ this.options.keepalive,
2089
+ () => {
2090
+ this._checkPing()
2091
+ },
2092
+ this.options.timerVariant,
2093
+ )
2070
2094
  }
2071
2095
  }
2072
2096
 
@@ -2100,6 +2124,7 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
2100
2124
  this._sendPacket({ cmd: 'pingreq' })
2101
2125
  } else {
2102
2126
  // do a forced cleanup since socket will be in bad shape
2127
+ this.emit('error', new Error('Keepalive timeout'))
2103
2128
  this.log('_checkPing :: calling _cleanUp with force true')
2104
2129
  this._cleanUp(true)
2105
2130
  }
@@ -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
 
package/src/mqtt.ts CHANGED
@@ -9,6 +9,7 @@ import DefaultMessageIdProvider from './lib/default-message-id-provider'
9
9
  import UniqueMessageIdProvider from './lib/unique-message-id-provider'
10
10
  import Store, { IStore } from './lib/store'
11
11
  import connect, { connectAsync } from './lib/connect'
12
+ import PingTimer from './lib/PingTimer'
12
13
 
13
14
  export const Client = MqttClient
14
15
  export {
@@ -19,6 +20,7 @@ export {
19
20
  DefaultMessageIdProvider,
20
21
  UniqueMessageIdProvider,
21
22
  IStore,
23
+ PingTimer,
22
24
  }
23
25
  export * from './lib/client'
24
26
  export * from './lib/shared'