mqtt 5.8.1 → 5.9.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.8.1",
4
+ "version": "5.9.0",
5
5
  "contributors": [
6
6
  "Adam Rudd <adamvrr@gmail.com>",
7
7
  "Matteo Collina <matteo.collina@gmail.com> (https://github.com/mcollina)",
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 {
@@ -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 (
@@ -298,4 +298,4 @@ const browserStreamBuilder: StreamBuilder = (client, opts) => {
298
298
  return stream
299
299
  }
300
300
 
301
- export default IS_BROWSER ? browserStreamBuilder : streamBuilder
301
+ export { browserStreamBuilder, streamBuilder }
@@ -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