mqtt 5.13.1 → 5.13.3

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.
Files changed (77) hide show
  1. package/build/bin/mqtt.js +1 -1
  2. package/build/bin/mqtt.js.map +1 -1
  3. package/build/bin/pub.js +3 -4
  4. package/build/bin/pub.js.map +1 -1
  5. package/build/bin/sub.js.map +1 -1
  6. package/build/lib/BufferedDuplex.js +4 -0
  7. package/build/lib/BufferedDuplex.js.map +1 -1
  8. package/build/lib/KeepaliveManager.js +8 -1
  9. package/build/lib/KeepaliveManager.js.map +1 -1
  10. package/build/lib/client.d.ts +2 -2
  11. package/build/lib/client.js +59 -16
  12. package/build/lib/client.js.map +1 -1
  13. package/build/lib/connect/ali.js +5 -6
  14. package/build/lib/connect/ali.js.map +1 -1
  15. package/build/lib/connect/index.js +8 -8
  16. package/build/lib/connect/index.js.map +1 -1
  17. package/build/lib/connect/socks.js +25 -27
  18. package/build/lib/connect/socks.js.map +1 -1
  19. package/build/lib/connect/tcp.js.map +1 -1
  20. package/build/lib/connect/tls.js +12 -17
  21. package/build/lib/connect/tls.js.map +1 -1
  22. package/build/lib/connect/ws.js.map +1 -1
  23. package/build/lib/connect/wx.js.map +1 -1
  24. package/build/lib/default-message-id-provider.js +1 -0
  25. package/build/lib/default-message-id-provider.js.map +1 -1
  26. package/build/lib/get-timer.js +1 -1
  27. package/build/lib/get-timer.js.map +1 -1
  28. package/build/lib/handlers/connack.js.map +1 -1
  29. package/build/lib/is-browser.js +4 -8
  30. package/build/lib/is-browser.js.map +1 -1
  31. package/build/lib/shared.d.ts +3 -2
  32. package/build/lib/shared.js +6 -4
  33. package/build/lib/shared.js.map +1 -1
  34. package/build/lib/store.js +3 -1
  35. package/build/lib/store.js.map +1 -1
  36. package/build/lib/topic-alias-recv.js +3 -0
  37. package/build/lib/topic-alias-recv.js.map +1 -1
  38. package/build/lib/topic-alias-send.js +5 -0
  39. package/build/lib/topic-alias-send.js.map +1 -1
  40. package/build/lib/unique-message-id-provider.js +2 -0
  41. package/build/lib/unique-message-id-provider.js.map +1 -1
  42. package/build/tsconfig.build.tsbuildinfo +1 -1
  43. package/dist/mqtt.esm.js +5 -5
  44. package/dist/mqtt.js +211 -152
  45. package/dist/mqtt.min.js +5 -5
  46. package/package.json +5 -5
  47. package/src/bin/mqtt.ts +0 -39
  48. package/src/bin/pub.ts +0 -174
  49. package/src/bin/sub.ts +0 -146
  50. package/src/index.ts +0 -4
  51. package/src/lib/BufferedDuplex.ts +0 -115
  52. package/src/lib/KeepaliveManager.ts +0 -112
  53. package/src/lib/TypedEmitter.ts +0 -71
  54. package/src/lib/client.ts +0 -2428
  55. package/src/lib/connect/ali.ts +0 -128
  56. package/src/lib/connect/index.ts +0 -294
  57. package/src/lib/connect/socks.ts +0 -236
  58. package/src/lib/connect/tcp.ts +0 -29
  59. package/src/lib/connect/tls.ts +0 -71
  60. package/src/lib/connect/ws.ts +0 -307
  61. package/src/lib/connect/wx.ts +0 -143
  62. package/src/lib/default-message-id-provider.ts +0 -99
  63. package/src/lib/get-timer.ts +0 -40
  64. package/src/lib/handlers/ack.ts +0 -159
  65. package/src/lib/handlers/auth.ts +0 -44
  66. package/src/lib/handlers/connack.ts +0 -58
  67. package/src/lib/handlers/index.ts +0 -74
  68. package/src/lib/handlers/publish.ts +0 -183
  69. package/src/lib/handlers/pubrel.ts +0 -31
  70. package/src/lib/is-browser.ts +0 -46
  71. package/src/lib/shared.ts +0 -100
  72. package/src/lib/store.ts +0 -166
  73. package/src/lib/topic-alias-recv.ts +0 -48
  74. package/src/lib/topic-alias-send.ts +0 -96
  75. package/src/lib/unique-message-id-provider.ts +0 -64
  76. package/src/lib/validations.ts +0 -46
  77. package/src/mqtt.ts +0 -28
@@ -1,71 +0,0 @@
1
- import tls, { type TLSSocket } from 'tls'
2
- import net from 'net'
3
- import _debug from 'debug'
4
- import { type StreamBuilder } from '../shared'
5
- import { type IClientOptions } from '../client'
6
- import openSocks from './socks'
7
-
8
- const debug = _debug('mqttjs:tls')
9
-
10
- function connect(opts: IClientOptions): TLSSocket {
11
- const { host, port, socksProxy, ...rest } = opts
12
-
13
- return tls.connect(
14
- socksProxy
15
- ? {
16
- ...rest,
17
- socket: openSocks(host, port, socksProxy, {
18
- timeout: opts.socksTimeout,
19
- }),
20
- }
21
- : opts,
22
- )
23
- }
24
-
25
- const buildStream: StreamBuilder = (client, opts) => {
26
- opts.port = opts.port || 8883
27
- opts.host = opts.hostname || opts.host || 'localhost'
28
-
29
- if (net.isIP(opts.host) === 0) {
30
- opts.servername = opts.host
31
- }
32
-
33
- opts.rejectUnauthorized = opts.rejectUnauthorized !== false
34
-
35
- delete opts.path
36
-
37
- debug(
38
- 'port %d host %s rejectUnauthorized %b',
39
- opts.port,
40
- opts.host,
41
- opts.rejectUnauthorized,
42
- )
43
-
44
- const connection = connect(opts)
45
- connection.on('secureConnect', () => {
46
- if (opts.rejectUnauthorized && !connection.authorized) {
47
- connection.emit('error', new Error('TLS not authorized'))
48
- } else {
49
- connection.removeListener('error', handleTLSerrors)
50
- }
51
- })
52
-
53
- function handleTLSerrors(err: Error) {
54
- // How can I get verify this error is a tls error?
55
- if (opts.rejectUnauthorized) {
56
- client.emit('error', err)
57
- }
58
-
59
- // close this connection to match the behaviour of net
60
- // otherwise all we get is an error from the connection
61
- // and close event doesn't fire. This is a work around
62
- // to enable the reconnect code to work the same as with
63
- // net.createConnection
64
- connection.end()
65
- }
66
-
67
- connection.on('error', handleTLSerrors)
68
- return connection
69
- }
70
-
71
- export default buildStream
@@ -1,307 +0,0 @@
1
- import { type StreamBuilder } from '../shared'
2
- import { Buffer } from 'buffer'
3
- import Ws, { type ClientOptions } from 'ws'
4
- import _debug from 'debug'
5
- import { type DuplexOptions, Transform } from 'readable-stream'
6
- import isBrowser from '../is-browser'
7
- import { type IClientOptions } from '../client'
8
- import type MqttClient from '../client'
9
- import { BufferedDuplex, writev } from '../BufferedDuplex'
10
-
11
- const debug = _debug('mqttjs:ws')
12
-
13
- const WSS_OPTIONS = [
14
- 'rejectUnauthorized',
15
- 'ca',
16
- 'cert',
17
- 'key',
18
- 'pfx',
19
- 'passphrase',
20
- ]
21
-
22
- function buildUrl(opts: IClientOptions, client: MqttClient) {
23
- let url = `${opts.protocol}://${opts.hostname}:${opts.port}${opts.path}`
24
- if (typeof opts.transformWsUrl === 'function') {
25
- url = opts.transformWsUrl(url, opts, client)
26
- }
27
- return url
28
- }
29
-
30
- function setDefaultOpts(opts: IClientOptions) {
31
- const options = opts
32
-
33
- if (!opts.port) {
34
- if (opts.protocol === 'wss') {
35
- options.port = 443
36
- } else {
37
- options.port = 80
38
- }
39
- }
40
-
41
- if (!opts.path) {
42
- options.path = '/'
43
- }
44
-
45
- if (!opts.wsOptions) {
46
- options.wsOptions = {}
47
- }
48
- if (!isBrowser && !opts.forceNativeWebSocket && opts.protocol === 'wss') {
49
- // Add cert/key/ca etc options
50
- WSS_OPTIONS.forEach((prop) => {
51
- if (
52
- Object.prototype.hasOwnProperty.call(opts, prop) &&
53
- !Object.prototype.hasOwnProperty.call(opts.wsOptions, prop)
54
- ) {
55
- options.wsOptions[prop] = opts[prop]
56
- }
57
- })
58
- }
59
-
60
- return options
61
- }
62
-
63
- function setDefaultBrowserOpts(opts: IClientOptions) {
64
- const options = setDefaultOpts(opts)
65
-
66
- if (!options.hostname) {
67
- options.hostname = options.host
68
- }
69
-
70
- if (!options.hostname) {
71
- // Throwing an error in a Web Worker if no `hostname` is given, because we
72
- // can not determine the `hostname` automatically. If connecting to
73
- // localhost, please supply the `hostname` as an argument.
74
- if (typeof document === 'undefined') {
75
- throw new Error('Could not determine host. Specify host manually.')
76
- }
77
- const parsed = new URL(document.URL)
78
- options.hostname = parsed.hostname
79
-
80
- if (!options.port) {
81
- options.port = Number(parsed.port)
82
- }
83
- }
84
-
85
- // objectMode should be defined for logic
86
- if (options.objectMode === undefined) {
87
- options.objectMode = !(
88
- options.binary === true || options.binary === undefined
89
- )
90
- }
91
-
92
- return options
93
- }
94
-
95
- function createWebSocket(
96
- client: MqttClient,
97
- url: string,
98
- opts: IClientOptions,
99
- ) {
100
- debug('createWebSocket')
101
- debug(`protocol: ${opts.protocolId} ${opts.protocolVersion}`)
102
- const websocketSubProtocol =
103
- opts.protocolId === 'MQIsdp' && opts.protocolVersion === 3
104
- ? 'mqttv3.1'
105
- : 'mqtt'
106
-
107
- debug(
108
- `creating new Websocket for url: ${url} and protocol: ${websocketSubProtocol}`,
109
- )
110
- let socket: Ws
111
- if (opts.createWebsocket) {
112
- socket = opts.createWebsocket(url, [websocketSubProtocol], opts)
113
- } else {
114
- socket = new Ws(
115
- url,
116
- [websocketSubProtocol],
117
- opts.wsOptions as ClientOptions,
118
- )
119
- }
120
- return socket
121
- }
122
-
123
- /* istanbul ignore next */
124
- function createBrowserWebSocket(client: MqttClient, opts: IClientOptions) {
125
- const websocketSubProtocol =
126
- opts.protocolId === 'MQIsdp' && opts.protocolVersion === 3
127
- ? 'mqttv3.1'
128
- : 'mqtt'
129
-
130
- const url = buildUrl(opts, client)
131
- let socket: WebSocket
132
- if (opts.createWebsocket) {
133
- socket = opts.createWebsocket(url, [websocketSubProtocol], opts)
134
- } else {
135
- socket = new WebSocket(url, [websocketSubProtocol])
136
- }
137
- socket.binaryType = 'arraybuffer'
138
- return socket
139
- }
140
-
141
- const streamBuilder: StreamBuilder = (client, opts) => {
142
- debug('streamBuilder')
143
- const options = setDefaultOpts(opts)
144
-
145
- options.hostname = options.hostname || options.host || 'localhost'
146
-
147
- const url = buildUrl(options, client)
148
- const socket = createWebSocket(client, url, options)
149
- const webSocketStream = Ws.createWebSocketStream(
150
- socket,
151
- options.wsOptions as DuplexOptions,
152
- )
153
-
154
- webSocketStream['url'] = url
155
- socket.on('close', () => {
156
- webSocketStream.destroy()
157
- })
158
- return webSocketStream
159
- }
160
-
161
- /* istanbul ignore next */
162
- const browserStreamBuilder: StreamBuilder = (client, opts) => {
163
- debug('browserStreamBuilder')
164
- let stream: BufferedDuplex | (Transform & { socket?: WebSocket })
165
- const options = setDefaultBrowserOpts(opts)
166
- // sets the maximum socket buffer size before throttling
167
- const bufferSize = options.browserBufferSize || 1024 * 512
168
-
169
- const bufferTimeout = opts.browserBufferTimeout || 1000
170
-
171
- const coerceToBuffer = !opts.objectMode
172
-
173
- // the websocket connection
174
- const socket = createBrowserWebSocket(client, opts)
175
-
176
- // the proxy is a transform stream that forwards data to the socket
177
- // it ensures data written to socket is a Buffer
178
- const proxy = buildProxy(opts, socketWriteBrowser, socketEndBrowser)
179
-
180
- if (!opts.objectMode) {
181
- proxy._writev = writev.bind(proxy)
182
- }
183
- proxy.on('close', () => {
184
- socket.close()
185
- })
186
-
187
- const eventListenerSupport = typeof socket.addEventListener !== 'undefined'
188
-
189
- // was already open when passed in
190
- if (socket.readyState === socket.OPEN) {
191
- stream = proxy
192
- stream.socket = socket
193
- } else {
194
- // socket is not open. Use this to buffer writes until it is opened
195
- stream = new BufferedDuplex(opts, proxy, socket)
196
-
197
- if (eventListenerSupport) {
198
- socket.addEventListener('open', onOpen)
199
- } else {
200
- socket.onopen = onOpen
201
- }
202
- }
203
-
204
- if (eventListenerSupport) {
205
- socket.addEventListener('close', onClose)
206
- socket.addEventListener('error', onError)
207
- socket.addEventListener('message', onMessage)
208
- } else {
209
- socket.onclose = onClose
210
- socket.onerror = onError
211
- socket.onmessage = onMessage
212
- }
213
-
214
- // methods for browserStreamBuilder
215
-
216
- function buildProxy(
217
- pOptions: IClientOptions,
218
- socketWrite: typeof socketWriteBrowser,
219
- socketEnd: typeof socketEndBrowser,
220
- ) {
221
- const _proxy = new Transform({
222
- objectMode: pOptions.objectMode,
223
- })
224
-
225
- _proxy._write = socketWrite
226
- _proxy._flush = socketEnd
227
-
228
- return _proxy
229
- }
230
-
231
- function onOpen() {
232
- debug('WebSocket onOpen')
233
- if (stream instanceof BufferedDuplex) {
234
- stream.socketReady()
235
- }
236
- }
237
-
238
- /**
239
- * https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/close_event
240
- */
241
- function onClose(event: CloseEvent) {
242
- debug('WebSocket onClose', event)
243
- stream.end()
244
- stream.destroy()
245
- }
246
-
247
- /**
248
- * https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/error_event
249
- */
250
- function onError(err: Event) {
251
- debug('WebSocket onError', err)
252
- const error = new Error('WebSocket error')
253
- error['event'] = err
254
- stream.destroy(error)
255
- }
256
-
257
- /**
258
- * https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/message_event
259
- */
260
- async function onMessage(event: MessageEvent) {
261
- if (!proxy || proxy.destroyed || !proxy.readable) {
262
- return
263
- }
264
- let { data } = event
265
- if (data instanceof ArrayBuffer) data = Buffer.from(data)
266
- else if (data instanceof Blob)
267
- data = Buffer.from(await new Response(data).arrayBuffer())
268
- else data = Buffer.from(data as string, 'utf8')
269
- proxy.push(data)
270
- }
271
-
272
- function socketWriteBrowser(
273
- chunk: any,
274
- enc: string,
275
- next: (err?: Error) => void,
276
- ) {
277
- if (socket.bufferedAmount > bufferSize) {
278
- // throttle data until buffered amount is reduced.
279
- setTimeout(socketWriteBrowser, bufferTimeout, chunk, enc, next)
280
- return
281
- }
282
-
283
- if (coerceToBuffer && typeof chunk === 'string') {
284
- chunk = Buffer.from(chunk, 'utf8')
285
- }
286
-
287
- try {
288
- // https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/send (note this doesn't have a cb as second arg)
289
- socket.send(chunk)
290
- } catch (err) {
291
- return next(err)
292
- }
293
-
294
- next()
295
- }
296
-
297
- function socketEndBrowser(done: (error?: Error, data?: any) => void) {
298
- socket.close()
299
- done()
300
- }
301
-
302
- // end methods for browserStreamBuilder
303
-
304
- return stream
305
- }
306
-
307
- export { browserStreamBuilder, streamBuilder }
@@ -1,143 +0,0 @@
1
- import { type StreamBuilder } from '../shared'
2
-
3
- import { Buffer } from 'buffer'
4
- import { Transform } from 'readable-stream'
5
- import { type IClientOptions } from '../client'
6
- import type MqttClient from '../client'
7
- import { BufferedDuplex } from '../BufferedDuplex'
8
-
9
- /* global wx */
10
- let socketTask: any
11
- let proxy: Transform
12
- let stream: BufferedDuplex
13
-
14
- declare global {
15
- const wx: any
16
- }
17
-
18
- function buildProxy() {
19
- const _proxy = new Transform()
20
- _proxy._write = (chunk, encoding, next) => {
21
- socketTask.send({
22
- data: chunk.buffer,
23
- success() {
24
- next()
25
- },
26
- fail(errMsg) {
27
- next(new Error(errMsg))
28
- },
29
- })
30
- }
31
- _proxy._flush = (done) => {
32
- socketTask.close({
33
- success() {
34
- done()
35
- },
36
- })
37
- }
38
-
39
- return _proxy
40
- }
41
-
42
- function setDefaultOpts(opts) {
43
- if (!opts.hostname) {
44
- opts.hostname = 'localhost'
45
- }
46
- if (!opts.path) {
47
- opts.path = '/'
48
- }
49
-
50
- if (!opts.wsOptions) {
51
- opts.wsOptions = {}
52
- }
53
- }
54
-
55
- function buildUrl(opts: IClientOptions, client: MqttClient) {
56
- const protocol = opts.protocol === 'wxs' ? 'wss' : 'ws'
57
- let url = `${protocol}://${opts.hostname}${opts.path}`
58
- if (opts.port && opts.port !== 80 && opts.port !== 443) {
59
- url = `${protocol}://${opts.hostname}:${opts.port}${opts.path}`
60
- }
61
- if (typeof opts.transformWsUrl === 'function') {
62
- url = opts.transformWsUrl(url, opts, client)
63
- }
64
- return url
65
- }
66
-
67
- function bindEventHandler() {
68
- socketTask.onOpen(() => {
69
- stream.socketReady()
70
- })
71
-
72
- socketTask.onMessage((res) => {
73
- let { data } = res
74
-
75
- if (data instanceof ArrayBuffer) data = Buffer.from(data)
76
- else data = Buffer.from(data, 'utf8')
77
- proxy.push(data)
78
- })
79
-
80
- socketTask.onClose(() => {
81
- stream.emit('close')
82
- stream.end()
83
- stream.destroy()
84
- })
85
-
86
- socketTask.onError((error) => {
87
- const err = new Error(error.errMsg)
88
- stream.destroy(err)
89
- })
90
- }
91
-
92
- const buildStream: StreamBuilder = (client, opts) => {
93
- opts.hostname = opts.hostname || opts.host
94
-
95
- if (!opts.hostname) {
96
- throw new Error('Could not determine host. Specify host manually.')
97
- }
98
-
99
- const websocketSubProtocol =
100
- opts.protocolId === 'MQIsdp' && opts.protocolVersion === 3
101
- ? 'mqttv3.1'
102
- : 'mqtt'
103
-
104
- setDefaultOpts(opts)
105
-
106
- const url = buildUrl(opts, client)
107
- // https://github.com/wechat-miniprogram/api-typings/blob/master/types/wx/lib.wx.api.d.ts#L20984
108
- socketTask = wx.connectSocket({
109
- url,
110
- protocols: [websocketSubProtocol],
111
- })
112
-
113
- proxy = buildProxy()
114
- stream = new BufferedDuplex(opts, proxy, socketTask)
115
- stream._destroy = (err, cb) => {
116
- socketTask.close({
117
- success() {
118
- if (cb) cb(err)
119
- },
120
- })
121
- }
122
-
123
- const destroyRef = stream.destroy
124
- stream.destroy = (err, cb) => {
125
- stream.destroy = destroyRef
126
-
127
- setTimeout(() => {
128
- socketTask.close({
129
- fail() {
130
- stream._destroy(err, cb)
131
- },
132
- })
133
- }, 0)
134
-
135
- return stream
136
- }
137
-
138
- bindEventHandler()
139
-
140
- return stream
141
- }
142
-
143
- export default buildStream
@@ -1,99 +0,0 @@
1
- export interface IMessageIdProvider {
2
- /**
3
- * Allocate the first vacant messageId. The messageId become occupied status.
4
- * @return {Number} - The first vacant messageId. If all messageIds are occupied, return null.
5
- */
6
- allocate(): number | null
7
-
8
- /**
9
- * Get the last allocated messageId.
10
- * @return {Number} - messageId.
11
- */
12
- getLastAllocated(): number | null
13
-
14
- /**
15
- * Register the messageId. The messageId become occupied status.
16
- * If the messageId has already been occupied, then return false.
17
- * @param {number} num - The messageId to request use.
18
- * @return {boolean} - If `num` was not occupied, then return true, otherwise return false.
19
- */
20
- register(num: number): boolean
21
-
22
- /**
23
- * Deallocate the messageId. The messageId become vacant status.
24
- * @param {Number} num - The messageId to deallocate. The messageId must be occupied status.
25
- * In other words, the messageId must be allocated by allocate() or
26
- * occupied by register().
27
- */
28
- deallocate(num: number): void
29
-
30
- /**
31
- * Clear all occupied messageIds.
32
- * The all messageIds are set to vacant status.
33
- */
34
- clear(): void
35
- }
36
-
37
- /**
38
- * DefaultMessageAllocator constructor
39
- * @constructor
40
- */
41
- export default class DefaultMessageIdProvider implements IMessageIdProvider {
42
- private nextId: number
43
-
44
- constructor() {
45
- /**
46
- * MessageIDs starting with 1
47
- * ensure that nextId is min. 1, see https://github.com/mqttjs/MQTT.js/issues/810
48
- */
49
- this.nextId = Math.max(1, Math.floor(Math.random() * 65535))
50
- }
51
-
52
- /**
53
- * allocate
54
- *
55
- * Get the next messageId.
56
- * @return unsigned int
57
- */
58
- allocate() {
59
- // id becomes current state of this.nextId and increments afterwards
60
- const id = this.nextId++
61
- // Ensure 16 bit unsigned int (max 65535, nextId got one higher)
62
- if (this.nextId === 65536) {
63
- this.nextId = 1
64
- }
65
- return id
66
- }
67
-
68
- /**
69
- * getLastAllocated
70
- * Get the last allocated messageId.
71
- * @return unsigned int
72
- */
73
- getLastAllocated() {
74
- return this.nextId === 1 ? 65535 : this.nextId - 1
75
- }
76
-
77
- /**
78
- * register
79
- * Register messageId. If success return true, otherwise return false.
80
- * @param { unsigned int } - messageId to register,
81
- * @return boolean
82
- */
83
- register(messageId: number) {
84
- return true
85
- }
86
-
87
- /**
88
- * deallocate
89
- * Deallocate messageId.
90
- * @param { unsigned int } - messageId to deallocate,
91
- */
92
- deallocate(messageId: number) {}
93
-
94
- /**
95
- * clear
96
- * Deallocate all messageIds.
97
- */
98
- clear() {}
99
- }
@@ -1,40 +0,0 @@
1
- import isBrowser, { isWebWorker, isReactNativeBrowser } from './is-browser'
2
- import { clearInterval as clearI, setInterval as setI } 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 setI
10
- clear: typeof clearI
11
- }
12
-
13
- const workerTimer: Timer = {
14
- set: setI,
15
- clear: clearI,
16
- }
17
-
18
- const nativeTimer: Timer = {
19
- set: (func, time) => setInterval(func, time),
20
- clear: (timerId) => clearInterval(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 && !isReactNativeBrowser
34
- ? workerTimer
35
- : nativeTimer
36
- }
37
- }
38
- }
39
-
40
- export default getTimer