mqtt 5.2.2 → 5.3.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/README.md +2 -2
- package/build/lib/BufferedDuplex.d.ts +19 -0
- package/build/lib/BufferedDuplex.js +70 -0
- package/build/lib/BufferedDuplex.js.map +1 -0
- package/build/lib/client.js.map +1 -1
- package/build/lib/connect/ali.js +5 -10
- package/build/lib/connect/ali.js.map +1 -1
- package/build/lib/connect/ws.js +15 -24
- package/build/lib/connect/ws.js.map +1 -1
- package/build/lib/connect/wx.d.ts +3 -0
- package/build/lib/connect/wx.js +9 -12
- package/build/lib/connect/wx.js.map +1 -1
- package/build/lib/is-browser.js +8 -5
- package/build/lib/is-browser.js.map +1 -1
- package/build/lib/shared.d.ts +3 -1
- package/build/lib/shared.js.map +1 -1
- package/build/tsconfig.build.tsbuildinfo +1 -1
- package/dist/mqtt.esm.js +5 -17
- package/dist/mqtt.js +3570 -7370
- package/dist/mqtt.min.js +5 -17
- package/package.json +1 -3
- package/src/lib/BufferedDuplex.ts +105 -0
- package/src/lib/client.ts +4 -0
- package/src/lib/connect/ali.ts +7 -8
- package/src/lib/connect/ws.ts +42 -36
- package/src/lib/connect/wx.ts +16 -16
- package/src/lib/is-browser.ts +15 -8
- package/src/lib/shared.ts +4 -1
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
|
+
"version": "5.3.1",
|
|
5
5
|
"contributors": [
|
|
6
6
|
"Adam Rudd <adamvrr@gmail.com>",
|
|
7
7
|
"Matteo Collina <matteo.collina@gmail.com> (https://github.com/mcollina)",
|
|
@@ -113,7 +113,6 @@
|
|
|
113
113
|
"commist": "^3.2.0",
|
|
114
114
|
"concat-stream": "^2.0.0",
|
|
115
115
|
"debug": "^4.3.4",
|
|
116
|
-
"duplexify": "^4.1.2",
|
|
117
116
|
"help-me": "^4.2.0",
|
|
118
117
|
"lru-cache": "^10.0.1",
|
|
119
118
|
"minimist": "^1.2.8",
|
|
@@ -130,7 +129,6 @@
|
|
|
130
129
|
"@esm-bundle/chai": "^4.3.4-fix.0",
|
|
131
130
|
"@release-it/conventional-changelog": "^7.0.2",
|
|
132
131
|
"@types/chai": "^4.3.10",
|
|
133
|
-
"@types/duplexify": "^3.6.4",
|
|
134
132
|
"@types/node": "^20.9.0",
|
|
135
133
|
"@types/sinon": "^17.0.1",
|
|
136
134
|
"@types/tape": "^5.6.4",
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { Duplex, Transform } from 'readable-stream'
|
|
2
|
+
import { IClientOptions } from './client'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Utils writev function for browser, ensure to write Buffers to socket (convert strings).
|
|
6
|
+
*/
|
|
7
|
+
export function writev(
|
|
8
|
+
chunks: { chunk: any; encoding: string }[],
|
|
9
|
+
cb: (err?: Error) => void,
|
|
10
|
+
) {
|
|
11
|
+
const buffers = new Array(chunks.length)
|
|
12
|
+
for (let i = 0; i < chunks.length; i++) {
|
|
13
|
+
if (typeof chunks[i].chunk === 'string') {
|
|
14
|
+
buffers[i] = Buffer.from(chunks[i].chunk, 'utf8')
|
|
15
|
+
} else {
|
|
16
|
+
buffers[i] = chunks[i].chunk
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
this._write(Buffer.concat(buffers), 'binary', cb)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* How this works:
|
|
25
|
+
* - `socket` is the `WebSocket` instance, the connection to our broker.
|
|
26
|
+
* - `proxy` is a `Transform`, it ensure data written to the `socket` is a `Buffer`.
|
|
27
|
+
* This class buffers the data written to the `proxy` (so then to `socket`) until the `socket` is ready.
|
|
28
|
+
* The stream returned from this class, will be passed to the `MqttClient`.
|
|
29
|
+
*/
|
|
30
|
+
export class BufferedDuplex extends Duplex {
|
|
31
|
+
public socket: WebSocket
|
|
32
|
+
|
|
33
|
+
private proxy: Transform
|
|
34
|
+
|
|
35
|
+
private isSocketOpen: boolean
|
|
36
|
+
|
|
37
|
+
private writeQueue: Array<{
|
|
38
|
+
chunk: any
|
|
39
|
+
encoding: string
|
|
40
|
+
cb: (err?: Error) => void
|
|
41
|
+
}>
|
|
42
|
+
|
|
43
|
+
constructor(opts: IClientOptions, proxy: Transform, socket: WebSocket) {
|
|
44
|
+
super({
|
|
45
|
+
objectMode: true,
|
|
46
|
+
})
|
|
47
|
+
this.proxy = proxy
|
|
48
|
+
this.socket = socket
|
|
49
|
+
this.writeQueue = []
|
|
50
|
+
|
|
51
|
+
if (!opts.objectMode) {
|
|
52
|
+
this._writev = writev.bind(this)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
this.isSocketOpen = false
|
|
56
|
+
|
|
57
|
+
this.proxy.on('data', (chunk) => {
|
|
58
|
+
this.push(chunk)
|
|
59
|
+
})
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
_read(size?: number): void {
|
|
63
|
+
this.proxy.read(size)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
_write(chunk: any, encoding: string, cb: (err?: Error) => void) {
|
|
67
|
+
if (!this.isSocketOpen) {
|
|
68
|
+
// Buffer the data in a queue
|
|
69
|
+
this.writeQueue.push({ chunk, encoding, cb })
|
|
70
|
+
} else {
|
|
71
|
+
this.writeToProxy(chunk, encoding, cb)
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
_final(callback: (error?: Error) => void): void {
|
|
76
|
+
this.writeQueue = []
|
|
77
|
+
this.proxy.end(callback)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Method to call when socket is ready to stop buffering writes */
|
|
81
|
+
socketReady() {
|
|
82
|
+
this.emit('connect')
|
|
83
|
+
this.isSocketOpen = true
|
|
84
|
+
this.processWriteQueue()
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
private writeToProxy(
|
|
88
|
+
chunk: any,
|
|
89
|
+
encoding: string,
|
|
90
|
+
cb: (err?: Error) => void,
|
|
91
|
+
) {
|
|
92
|
+
if (this.proxy.write(chunk, encoding) === false) {
|
|
93
|
+
this.proxy.once('drain', cb)
|
|
94
|
+
} else {
|
|
95
|
+
cb()
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
private processWriteQueue() {
|
|
100
|
+
while (this.writeQueue.length > 0) {
|
|
101
|
+
const { chunk, encoding, cb } = this.writeQueue.shift()!
|
|
102
|
+
this.writeToProxy(chunk, encoding, cb)
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
package/src/lib/client.ts
CHANGED
|
@@ -440,6 +440,10 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
|
|
|
440
440
|
|
|
441
441
|
public pingTimer: any
|
|
442
442
|
|
|
443
|
+
/**
|
|
444
|
+
* The connection to the Broker. In browsers env this also have `socket` property
|
|
445
|
+
* set to the `WebSocket` instance.
|
|
446
|
+
*/
|
|
443
447
|
public stream: IStream
|
|
444
448
|
|
|
445
449
|
public queue: { packet: Packet; cb: PacketCallback }[]
|
package/src/lib/connect/ali.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { Buffer } from 'buffer'
|
|
2
2
|
import { Transform } from 'readable-stream'
|
|
3
|
-
import duplexify, { Duplexify } from 'duplexify'
|
|
4
3
|
import { StreamBuilder } from '../shared'
|
|
5
4
|
import MqttClient, { IClientOptions } from '../client'
|
|
5
|
+
import { BufferedDuplex } from '../BufferedDuplex'
|
|
6
6
|
|
|
7
7
|
let my: any
|
|
8
8
|
let proxy: Transform
|
|
9
|
-
let stream:
|
|
9
|
+
let stream: BufferedDuplex
|
|
10
10
|
let isInitialized = false
|
|
11
11
|
|
|
12
12
|
function buildProxy() {
|
|
@@ -64,9 +64,7 @@ function bindEventHandler() {
|
|
|
64
64
|
isInitialized = true
|
|
65
65
|
|
|
66
66
|
my.onSocketOpen(() => {
|
|
67
|
-
stream.
|
|
68
|
-
stream.setWritable(proxy)
|
|
69
|
-
stream.emit('connect')
|
|
67
|
+
stream.socketReady()
|
|
70
68
|
})
|
|
71
69
|
|
|
72
70
|
my.onSocketMessage((res) => {
|
|
@@ -91,8 +89,8 @@ function bindEventHandler() {
|
|
|
91
89
|
stream.destroy()
|
|
92
90
|
})
|
|
93
91
|
|
|
94
|
-
my.onSocketError((
|
|
95
|
-
stream.destroy(
|
|
92
|
+
my.onSocketError((err) => {
|
|
93
|
+
stream.destroy(err)
|
|
96
94
|
})
|
|
97
95
|
}
|
|
98
96
|
|
|
@@ -112,13 +110,14 @@ const buildStream: StreamBuilder = (client, opts) => {
|
|
|
112
110
|
|
|
113
111
|
const url = buildUrl(opts, client)
|
|
114
112
|
my = opts.my
|
|
113
|
+
// https://miniprogram.alipay.com/docs/miniprogram/mpdev/api_network_connectsocket
|
|
115
114
|
my.connectSocket({
|
|
116
115
|
url,
|
|
117
116
|
protocols: websocketSubProtocol,
|
|
118
117
|
})
|
|
119
118
|
|
|
120
119
|
proxy = buildProxy()
|
|
121
|
-
stream =
|
|
120
|
+
stream = new BufferedDuplex(opts, proxy, my)
|
|
122
121
|
|
|
123
122
|
bindEventHandler()
|
|
124
123
|
|
package/src/lib/connect/ws.ts
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import { StreamBuilder } from '../shared'
|
|
2
|
-
|
|
3
2
|
import { Buffer } from 'buffer'
|
|
4
|
-
import
|
|
3
|
+
import Ws, { ClientOptions } from 'ws'
|
|
5
4
|
import _debug from 'debug'
|
|
6
|
-
import duplexify from 'duplexify'
|
|
7
5
|
import { DuplexOptions, Transform } from 'readable-stream'
|
|
8
6
|
import IS_BROWSER from '../is-browser'
|
|
9
7
|
import MqttClient, { IClientOptions } from '../client'
|
|
8
|
+
import { BufferedDuplex, writev } from '../BufferedDuplex'
|
|
10
9
|
|
|
11
10
|
const debug = _debug('mqttjs:ws')
|
|
12
11
|
|
|
@@ -108,11 +107,11 @@ function createWebSocket(
|
|
|
108
107
|
debug(
|
|
109
108
|
`creating new Websocket for url: ${url} and protocol: ${websocketSubProtocol}`,
|
|
110
109
|
)
|
|
111
|
-
let socket:
|
|
110
|
+
let socket: Ws
|
|
112
111
|
if (opts.createWebsocket) {
|
|
113
112
|
socket = opts.createWebsocket(url, [websocketSubProtocol], opts)
|
|
114
113
|
} else {
|
|
115
|
-
socket = new
|
|
114
|
+
socket = new Ws(
|
|
116
115
|
url,
|
|
117
116
|
[websocketSubProtocol],
|
|
118
117
|
opts.wsOptions as ClientOptions,
|
|
@@ -143,7 +142,7 @@ const streamBuilder: StreamBuilder = (client, opts) => {
|
|
|
143
142
|
const options = setDefaultOpts(opts)
|
|
144
143
|
const url = buildUrl(options, client)
|
|
145
144
|
const socket = createWebSocket(client, url, options)
|
|
146
|
-
const webSocketStream =
|
|
145
|
+
const webSocketStream = Ws.createWebSocketStream(
|
|
147
146
|
socket,
|
|
148
147
|
options.wsOptions as DuplexOptions,
|
|
149
148
|
)
|
|
@@ -157,7 +156,7 @@ const streamBuilder: StreamBuilder = (client, opts) => {
|
|
|
157
156
|
|
|
158
157
|
const browserStreamBuilder: StreamBuilder = (client, opts) => {
|
|
159
158
|
debug('browserStreamBuilder')
|
|
160
|
-
let stream
|
|
159
|
+
let stream: BufferedDuplex | (Transform & { socket?: WebSocket })
|
|
161
160
|
const options = setDefaultBrowserOpts(opts)
|
|
162
161
|
// sets the maximum socket buffer size before throttling
|
|
163
162
|
const bufferSize = options.browserBufferSize || 1024 * 512
|
|
@@ -166,11 +165,15 @@ const browserStreamBuilder: StreamBuilder = (client, opts) => {
|
|
|
166
165
|
|
|
167
166
|
const coerceToBuffer = !opts.objectMode
|
|
168
167
|
|
|
168
|
+
// the websocket connection
|
|
169
169
|
const socket = createBrowserWebSocket(client, opts)
|
|
170
|
+
|
|
171
|
+
// the proxy is a transform stream that forwards data to the socket
|
|
172
|
+
// it ensures data written to socket is a Buffer
|
|
170
173
|
const proxy = buildProxy(opts, socketWriteBrowser, socketEndBrowser)
|
|
171
174
|
|
|
172
175
|
if (!opts.objectMode) {
|
|
173
|
-
proxy._writev = writev
|
|
176
|
+
proxy._writev = writev.bind(proxy)
|
|
174
177
|
}
|
|
175
178
|
proxy.on('close', () => {
|
|
176
179
|
socket.close()
|
|
@@ -181,11 +184,10 @@ const browserStreamBuilder: StreamBuilder = (client, opts) => {
|
|
|
181
184
|
// was already open when passed in
|
|
182
185
|
if (socket.readyState === socket.OPEN) {
|
|
183
186
|
stream = proxy
|
|
187
|
+
stream.socket = socket
|
|
184
188
|
} else {
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
stream._writev = writev
|
|
188
|
-
}
|
|
189
|
+
// socket is not open. Use this to buffer writes until it is opened
|
|
190
|
+
stream = new BufferedDuplex(opts, proxy, socket)
|
|
189
191
|
|
|
190
192
|
if (eventListenerSupport) {
|
|
191
193
|
socket.addEventListener('open', onOpen)
|
|
@@ -194,8 +196,6 @@ const browserStreamBuilder: StreamBuilder = (client, opts) => {
|
|
|
194
196
|
}
|
|
195
197
|
}
|
|
196
198
|
|
|
197
|
-
stream.socket = socket
|
|
198
|
-
|
|
199
199
|
if (eventListenerSupport) {
|
|
200
200
|
socket.addEventListener('close', onClose)
|
|
201
201
|
socket.addEventListener('error', onError)
|
|
@@ -208,7 +208,11 @@ const browserStreamBuilder: StreamBuilder = (client, opts) => {
|
|
|
208
208
|
|
|
209
209
|
// methods for browserStreamBuilder
|
|
210
210
|
|
|
211
|
-
function buildProxy(
|
|
211
|
+
function buildProxy(
|
|
212
|
+
pOptions: IClientOptions,
|
|
213
|
+
socketWrite: typeof socketWriteBrowser,
|
|
214
|
+
socketEnd: typeof socketEndBrowser,
|
|
215
|
+
) {
|
|
212
216
|
const _proxy = new Transform({
|
|
213
217
|
objectMode: pOptions.objectMode,
|
|
214
218
|
})
|
|
@@ -220,41 +224,41 @@ const browserStreamBuilder: StreamBuilder = (client, opts) => {
|
|
|
220
224
|
}
|
|
221
225
|
|
|
222
226
|
function onOpen() {
|
|
223
|
-
|
|
224
|
-
stream
|
|
225
|
-
|
|
227
|
+
debug('WebSocket onOpen')
|
|
228
|
+
if (stream instanceof BufferedDuplex) {
|
|
229
|
+
stream.socketReady()
|
|
230
|
+
}
|
|
226
231
|
}
|
|
227
232
|
|
|
228
|
-
|
|
233
|
+
/**
|
|
234
|
+
* https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/close_event
|
|
235
|
+
*/
|
|
236
|
+
function onClose(event: CloseEvent) {
|
|
237
|
+
debug('WebSocket onClose', event)
|
|
229
238
|
stream.end()
|
|
230
239
|
stream.destroy()
|
|
231
240
|
}
|
|
232
241
|
|
|
242
|
+
/**
|
|
243
|
+
* https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/error_event
|
|
244
|
+
*/
|
|
233
245
|
function onError(err: Event) {
|
|
234
|
-
|
|
246
|
+
debug('WebSocket onError', err)
|
|
247
|
+
const error = new Error('WebSocket error')
|
|
248
|
+
error['event'] = err
|
|
249
|
+
stream.destroy(error)
|
|
235
250
|
}
|
|
236
251
|
|
|
252
|
+
/**
|
|
253
|
+
* https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/message_event
|
|
254
|
+
*/
|
|
237
255
|
function onMessage(event: MessageEvent) {
|
|
238
256
|
let { data } = event
|
|
239
257
|
if (data instanceof ArrayBuffer) data = Buffer.from(data)
|
|
240
|
-
else data = Buffer.from(data, 'utf8')
|
|
258
|
+
else data = Buffer.from(data as string, 'utf8')
|
|
241
259
|
proxy.push(data)
|
|
242
260
|
}
|
|
243
261
|
|
|
244
|
-
// this is to be enabled only if objectMode is false
|
|
245
|
-
function writev(chunks: any, cb: (err?: Error) => void) {
|
|
246
|
-
const buffers = new Array(chunks.length)
|
|
247
|
-
for (let i = 0; i < chunks.length; i++) {
|
|
248
|
-
if (typeof chunks[i].chunk === 'string') {
|
|
249
|
-
buffers[i] = Buffer.from(chunks[i], 'utf8')
|
|
250
|
-
} else {
|
|
251
|
-
buffers[i] = chunks[i].chunk
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
this._write(Buffer.concat(buffers), 'binary', cb)
|
|
256
|
-
}
|
|
257
|
-
|
|
258
262
|
function socketWriteBrowser(
|
|
259
263
|
chunk: any,
|
|
260
264
|
enc: string,
|
|
@@ -263,6 +267,7 @@ const browserStreamBuilder: StreamBuilder = (client, opts) => {
|
|
|
263
267
|
if (socket.bufferedAmount > bufferSize) {
|
|
264
268
|
// throttle data until buffered amount is reduced.
|
|
265
269
|
setTimeout(socketWriteBrowser, bufferTimeout, chunk, enc, next)
|
|
270
|
+
return
|
|
266
271
|
}
|
|
267
272
|
|
|
268
273
|
if (coerceToBuffer && typeof chunk === 'string') {
|
|
@@ -270,6 +275,7 @@ const browserStreamBuilder: StreamBuilder = (client, opts) => {
|
|
|
270
275
|
}
|
|
271
276
|
|
|
272
277
|
try {
|
|
278
|
+
// https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/send (note this doesn't have a cb as second arg)
|
|
273
279
|
socket.send(chunk)
|
|
274
280
|
} catch (err) {
|
|
275
281
|
return next(err)
|
|
@@ -278,7 +284,7 @@ const browserStreamBuilder: StreamBuilder = (client, opts) => {
|
|
|
278
284
|
next()
|
|
279
285
|
}
|
|
280
286
|
|
|
281
|
-
function socketEndBrowser(done) {
|
|
287
|
+
function socketEndBrowser(done: (error?: Error, data?: any) => void) {
|
|
282
288
|
socket.close()
|
|
283
289
|
done()
|
|
284
290
|
}
|
package/src/lib/connect/wx.ts
CHANGED
|
@@ -2,13 +2,17 @@ import { StreamBuilder } from '../shared'
|
|
|
2
2
|
|
|
3
3
|
import { Buffer } from 'buffer'
|
|
4
4
|
import { Transform } from 'readable-stream'
|
|
5
|
-
import duplexify, { Duplexify } from 'duplexify'
|
|
6
5
|
import MqttClient, { IClientOptions } from '../client'
|
|
6
|
+
import { BufferedDuplex } from '../BufferedDuplex'
|
|
7
7
|
|
|
8
8
|
/* global wx */
|
|
9
9
|
let socketTask: any
|
|
10
10
|
let proxy: Transform
|
|
11
|
-
let stream:
|
|
11
|
+
let stream: BufferedDuplex
|
|
12
|
+
|
|
13
|
+
declare global {
|
|
14
|
+
const wx: any
|
|
15
|
+
}
|
|
12
16
|
|
|
13
17
|
function buildProxy() {
|
|
14
18
|
const _proxy = new Transform()
|
|
@@ -61,9 +65,7 @@ function buildUrl(opts: IClientOptions, client: MqttClient) {
|
|
|
61
65
|
|
|
62
66
|
function bindEventHandler() {
|
|
63
67
|
socketTask.onOpen(() => {
|
|
64
|
-
stream.
|
|
65
|
-
stream.setWritable(proxy)
|
|
66
|
-
stream.emit('connect')
|
|
68
|
+
stream.socketReady()
|
|
67
69
|
})
|
|
68
70
|
|
|
69
71
|
socketTask.onMessage((res) => {
|
|
@@ -80,8 +82,9 @@ function bindEventHandler() {
|
|
|
80
82
|
stream.destroy()
|
|
81
83
|
})
|
|
82
84
|
|
|
83
|
-
socketTask.onError((
|
|
84
|
-
|
|
85
|
+
socketTask.onError((error) => {
|
|
86
|
+
const err = new Error(error.errMsg)
|
|
87
|
+
stream.destroy(err)
|
|
85
88
|
})
|
|
86
89
|
}
|
|
87
90
|
|
|
@@ -100,15 +103,14 @@ const buildStream: StreamBuilder = (client, opts) => {
|
|
|
100
103
|
setDefaultOpts(opts)
|
|
101
104
|
|
|
102
105
|
const url = buildUrl(opts, client)
|
|
103
|
-
//
|
|
104
|
-
// @ts-ignore
|
|
106
|
+
// https://github.com/wechat-miniprogram/api-typings/blob/master/types/wx/lib.wx.api.d.ts#L20984
|
|
105
107
|
socketTask = wx.connectSocket({
|
|
106
108
|
url,
|
|
107
109
|
protocols: [websocketSubProtocol],
|
|
108
110
|
})
|
|
109
111
|
|
|
110
112
|
proxy = buildProxy()
|
|
111
|
-
stream =
|
|
113
|
+
stream = new BufferedDuplex(opts, proxy, socketTask)
|
|
112
114
|
stream._destroy = (err, cb) => {
|
|
113
115
|
socketTask.close({
|
|
114
116
|
success() {
|
|
@@ -118,20 +120,18 @@ const buildStream: StreamBuilder = (client, opts) => {
|
|
|
118
120
|
}
|
|
119
121
|
|
|
120
122
|
const destroyRef = stream.destroy
|
|
121
|
-
|
|
122
|
-
// @ts-ignore
|
|
123
|
-
stream.destroy = () => {
|
|
123
|
+
stream.destroy = (err, cb) => {
|
|
124
124
|
stream.destroy = destroyRef
|
|
125
125
|
|
|
126
126
|
setTimeout(() => {
|
|
127
127
|
socketTask.close({
|
|
128
128
|
fail() {
|
|
129
|
-
|
|
130
|
-
// @ts-ignore
|
|
131
|
-
stream._destroy(new Error())
|
|
129
|
+
stream._destroy(err, cb)
|
|
132
130
|
},
|
|
133
131
|
})
|
|
134
132
|
}, 0)
|
|
133
|
+
|
|
134
|
+
return stream
|
|
135
135
|
}
|
|
136
136
|
|
|
137
137
|
bindEventHandler()
|
package/src/lib/is-browser.ts
CHANGED
|
@@ -1,11 +1,18 @@
|
|
|
1
|
-
const
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
self.constructor &&
|
|
1
|
+
const isStandardBrowserEnv = () =>
|
|
2
|
+
typeof window !== 'undefined' && typeof window.document !== 'undefined'
|
|
3
|
+
|
|
4
|
+
const isWebWorkerEnv = () =>
|
|
5
|
+
Boolean(
|
|
7
6
|
// eslint-disable-next-line no-restricted-globals
|
|
8
|
-
self
|
|
9
|
-
|
|
7
|
+
typeof self === 'object' &&
|
|
8
|
+
// eslint-disable-next-line no-restricted-globals
|
|
9
|
+
self?.constructor?.name?.includes('WorkerGlobalScope'),
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
const isReactNativeEnv = () =>
|
|
13
|
+
typeof navigator !== 'undefined' && navigator.product === 'ReactNative'
|
|
14
|
+
|
|
15
|
+
const isBrowser =
|
|
16
|
+
isStandardBrowserEnv() || isWebWorkerEnv() || isReactNativeEnv()
|
|
10
17
|
|
|
11
18
|
export default isBrowser
|
package/src/lib/shared.ts
CHANGED
|
@@ -9,7 +9,10 @@ export type GenericCallback<T> = (error?: Error, result?: T) => void
|
|
|
9
9
|
|
|
10
10
|
export type VoidCallback = () => void
|
|
11
11
|
|
|
12
|
-
export type IStream = Duplex
|
|
12
|
+
export type IStream = Duplex & {
|
|
13
|
+
/** only set on browsers, it's a [WebSocket](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket) */
|
|
14
|
+
socket?: any
|
|
15
|
+
}
|
|
13
16
|
|
|
14
17
|
export type StreamBuilder = (
|
|
15
18
|
client: MqttClient,
|