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/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.8.1",
4
+ "version": "5.9.1",
5
5
  "contributors": [
6
6
  "Adam Rudd <adamvrr@gmail.com>",
7
7
  "Matteo Collina <matteo.collina@gmail.com> (https://github.com/mcollina)",
@@ -56,7 +56,9 @@ export class BufferedDuplex extends Duplex {
56
56
  this.isSocketOpen = false
57
57
 
58
58
  this.proxy.on('data', (chunk) => {
59
- this.push(chunk)
59
+ if (!this.destroyed) {
60
+ this.push(chunk)
61
+ }
60
62
  })
61
63
  }
62
64
 
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 = (error?: Error, packet?: Packet) => any
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 {
@@ -6,7 +6,7 @@ import MqttClient, {
6
6
  MqttClientEventCallbacks,
7
7
  MqttProtocol,
8
8
  } from '../client'
9
- import IS_BROWSER from '../is-browser'
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
- const protocols: Record<string, StreamBuilder> = {}
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,
@@ -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 IS_BROWSER from '../is-browser'
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 (!IS_BROWSER && opts.protocol === 'wss') {
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.push(data)
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 default IS_BROWSER ? browserStreamBuilder : streamBuilder
303
+ export { browserStreamBuilder, streamBuilder }
@@ -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 Error(`Publish error: ${ReasonCodes[pubackRC]}`)
86
- err.code = pubackRC
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 Error(`Publish error: ${ReasonCodes[pubrecRC]}`)
106
- err.code = pubrecRC
107
+ err = new ErrorWithReasonCode(
108
+ `Publish error: ${ReasonCodes[pubrecRC]}`,
109
+ pubrecRC,
110
+ )
107
111
  client['_removeOutgoingAndStoreMessage'](messageId, () => {
108
112
  cb(err, packet)
109
113
  })
@@ -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