-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathws.ts
More file actions
449 lines (385 loc) · 11 KB
/
ws.ts
File metadata and controls
449 lines (385 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
import { Buffer } from 'buffer'
import Ws, { type ClientOptions } from 'ws'
import _debug from 'debug'
import { Duplex, Transform } from 'readable-stream'
import { type IStream, type StreamBuilder } from '../shared'
import isBrowser from '../is-browser'
import { type IClientOptions } from '../client'
import type MqttClient from '../client'
import { BufferedDuplex, writev } from '../BufferedDuplex'
const debug = _debug('mqttjs:ws')
const WSS_OPTIONS = [
'rejectUnauthorized',
'ca',
'cert',
'key',
'pfx',
'passphrase',
]
function buildUrl(opts: IClientOptions, client: MqttClient) {
const url = `${opts.protocol}://${opts.hostname}:${opts.port}${opts.path}`
if (typeof opts.transformWsUrl === 'function') {
const result = opts.transformWsUrl(url, opts, client)
return result
}
return url
}
function setDefaultOpts(opts: IClientOptions) {
const options = opts
if (!opts.port) {
if (opts.protocol === 'wss') {
options.port = 443
} else {
options.port = 80
}
}
if (!opts.path) {
options.path = '/'
}
if (!opts.wsOptions) {
options.wsOptions = {}
}
if (!isBrowser && !opts.forceNativeWebSocket && opts.protocol === 'wss') {
// Add cert/key/ca etc options
WSS_OPTIONS.forEach((prop) => {
if (
Object.prototype.hasOwnProperty.call(opts, prop) &&
!Object.prototype.hasOwnProperty.call(opts.wsOptions, prop)
) {
options.wsOptions[prop] = opts[prop]
}
})
}
return options
}
function setDefaultBrowserOpts(opts: IClientOptions) {
const options = setDefaultOpts(opts)
if (!options.hostname) {
options.hostname = options.host
}
if (!options.hostname) {
// Throwing an error in a Web Worker if no `hostname` is given, because we
// can not determine the `hostname` automatically. If connecting to
// localhost, please supply the `hostname` as an argument.
if (typeof document === 'undefined') {
throw new Error('Could not determine host. Specify host manually.')
}
const parsed = new URL(document.URL)
options.hostname = parsed.hostname
if (!options.port) {
options.port = Number(parsed.port)
}
}
// objectMode should be defined for logic
if (options.objectMode === undefined) {
options.objectMode = !(
options.binary === true || options.binary === undefined
)
}
return options
}
function createWebSocket(
client: MqttClient,
url: string,
opts: IClientOptions,
) {
debug('createWebSocket')
debug(`protocol: ${opts.protocolId} ${opts.protocolVersion}`)
const websocketSubProtocol =
opts.protocolId === 'MQIsdp' && opts.protocolVersion === 3
? 'mqttv3.1'
: 'mqtt'
debug(
`creating new Websocket for url: ${url} and protocol: ${websocketSubProtocol}`,
)
let socket: Ws
if (opts.createWebsocket) {
socket = opts.createWebsocket(url, [websocketSubProtocol], opts)
} else {
socket = new Ws(
url,
[websocketSubProtocol],
opts.wsOptions as ClientOptions,
)
}
return socket
}
/* istanbul ignore next */
function createBrowserWebSocket(client: MqttClient, opts: IClientOptions) {
const websocketSubProtocol =
opts.protocolId === 'MQIsdp' && opts.protocolVersion === 3
? 'mqttv3.1'
: 'mqtt'
const urlOrPromise = buildUrl(opts, client)
if (urlOrPromise instanceof Promise) {
return urlOrPromise.then((url) => {
let socket: WebSocket
if (opts.createWebsocket) {
socket = opts.createWebsocket(url, [websocketSubProtocol], opts)
} else {
socket = new WebSocket(url, [websocketSubProtocol])
}
socket.binaryType = 'arraybuffer'
return socket
})
}
const url = urlOrPromise
let socket: WebSocket
if (opts.createWebsocket) {
socket = opts.createWebsocket(url, [websocketSubProtocol], opts)
} else {
socket = new WebSocket(url, [websocketSubProtocol])
}
socket.binaryType = 'arraybuffer'
return socket
}
const streamBuilder: StreamBuilder = (client, opts): IStream => {
debug('streamBuilder')
const options = setDefaultOpts(opts)
options.hostname = options.hostname || options.host || 'localhost'
const urlOrPromise = buildUrl(options, client)
if (typeof urlOrPromise === 'string') {
const url = urlOrPromise
const socket = createWebSocket(client, url, options)
const webSocketStream = Ws.createWebSocketStream(
socket,
options.wsOptions as any,
)
webSocketStream['url'] = url
socket.on('close', () => {
webSocketStream.destroy()
})
return webSocketStream
}
// async case: buffer data until the URL promise resolves then create the WebSocket
const writeQueue: Array<{
chunk: any
encoding: string
cb: (err?: Error) => void
}> = []
let wsStream: ReturnType<typeof Ws.createWebSocketStream> | null = null
let deferredDestroyed = false
const deferredStream = new Duplex({
read() {
// push model - data is pushed from wsStream events once
// the WebSocket connection is established
},
write(chunk, encoding, cb) {
if (wsStream) {
wsStream.write(chunk, encoding, cb)
} else {
writeQueue.push({ chunk, encoding, cb })
}
},
destroy(err, cb) {
deferredDestroyed = true
if (wsStream) wsStream.destroy(err)
cb(err)
},
})
urlOrPromise
.then((url) => {
if (deferredDestroyed) return
const socket = createWebSocket(client, url, options)
wsStream = Ws.createWebSocketStream(
socket,
options.wsOptions as any,
)
deferredStream['url'] = url
wsStream.on('data', (chunk) => {
deferredStream.push(chunk)
})
wsStream.on('end', () => {
deferredStream.push(null)
})
wsStream.on('error', (err) => {
deferredStream.destroy(err)
})
socket.on('close', () => {
wsStream!.destroy()
})
// flush buffered writes
const queue = writeQueue.splice(0)
for (const { chunk, encoding, cb } of queue) {
wsStream.write(chunk, encoding as BufferEncoding, cb)
}
})
.catch((err) => {
deferredStream.destroy(err)
})
return deferredStream
}
/* istanbul ignore next */
const browserStreamBuilder: StreamBuilder = (client, opts) => {
debug('browserStreamBuilder')
let stream: BufferedDuplex | (Transform & { socket?: WebSocket })
const options = setDefaultBrowserOpts(opts)
// sets the maximum socket buffer size before throttling
const bufferSize = options.browserBufferSize || 1024 * 512
const bufferTimeout = opts.browserBufferTimeout || 1000
const coerceToBuffer = !opts.objectMode
// mutable socket reference - set once the socket is available
let socketRef: WebSocket | null = null
// the proxy is a transform stream that forwards data to the socket
// it ensures data written to socket is a Buffer
const proxy = buildProxy(opts, socketWriteBrowser, socketEndBrowser)
if (!opts.objectMode) {
proxy._writev = writev.bind(proxy)
}
// the websocket connection (may be a Promise if transformWsUrl is async)
const socketOrPromise = createBrowserWebSocket(client, opts)
// methods for browserStreamBuilder
function buildProxy(
pOptions: IClientOptions,
socketWrite: typeof socketWriteBrowser,
socketEnd: typeof socketEndBrowser,
) {
const _proxy = new Transform({
objectMode: pOptions.objectMode,
})
_proxy._write = socketWrite
_proxy._flush = socketEnd
return _proxy
}
function attachSocketHandlers(socket: WebSocket) {
socketRef = socket
proxy.on('close', () => {
socket.close()
})
const eventListenerSupport =
typeof socket.addEventListener !== 'undefined'
// was already open when passed in
if (socket.readyState === socket.OPEN) {
stream = proxy
stream.socket = socket
} else {
// socket is not open. Use this to buffer writes until it is opened
stream = new BufferedDuplex(opts, proxy, socket)
if (eventListenerSupport) {
socket.addEventListener('open', onOpen)
} else {
socket.onopen = onOpen
}
}
if (eventListenerSupport) {
socket.addEventListener('close', onClose)
socket.addEventListener('error', onError)
socket.addEventListener('message', onMessage)
} else {
socket.onclose = onClose
socket.onerror = onError
socket.onmessage = onMessage
}
}
if (socketOrPromise instanceof Promise) {
// async case: create a BufferedDuplex immediately to buffer writes,
// then wire up the real socket once the URL promise resolves.
// Note: BufferedDuplex only stores the socket reference and does not
// call any methods on it, so an empty placeholder is safe here.
const placeholderSocket = {
close() {},
} as unknown as WebSocket
stream = new BufferedDuplex(opts, proxy, placeholderSocket)
socketOrPromise
.then((socket) => {
socketRef = socket
;(stream as BufferedDuplex).socket = socket
const eventListenerSupport =
typeof socket.addEventListener !== 'undefined'
if (eventListenerSupport) {
socket.addEventListener('open', onOpen)
socket.addEventListener('close', onClose)
socket.addEventListener('error', onError)
socket.addEventListener('message', onMessage)
} else {
socket.onopen = onOpen
socket.onclose = onClose
socket.onerror = onError
socket.onmessage = onMessage
}
if (socket.readyState === socket.OPEN) {
onOpen()
}
// wire up proxy close to close the real socket
proxy.on('close', () => {
socket.close()
})
})
.catch((err) => {
stream.destroy(err)
})
} else {
attachSocketHandlers(socketOrPromise)
}
function onOpen() {
debug('WebSocket onOpen')
if (stream instanceof BufferedDuplex) {
stream.socketReady()
}
}
/**
* https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/close_event
*/
function onClose(event: CloseEvent) {
debug('WebSocket onClose', event)
stream.end()
stream.destroy()
}
/**
* https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/error_event
*/
function onError(err: Event) {
debug('WebSocket onError', err)
const error = new Error('WebSocket error')
error['event'] = err
stream.destroy(error)
}
/**
* https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/message_event
*/
async function onMessage(event: MessageEvent) {
if (!proxy || !proxy.readable || !proxy.writable) {
return
}
let { data } = event
if (data instanceof ArrayBuffer) data = Buffer.from(data)
else if (data instanceof Blob)
data = Buffer.from(await new Response(data).arrayBuffer())
else data = Buffer.from(data as string, 'utf8')
proxy.push(data)
}
function socketWriteBrowser(
chunk: any,
enc: string,
next: (err?: Error) => void,
) {
if (!socketRef) {
next(new Error('WebSocket is not yet available'))
return
}
if (socketRef.bufferedAmount > bufferSize) {
// throttle data until buffered amount is reduced.
setTimeout(socketWriteBrowser, bufferTimeout, chunk, enc, next)
return
}
if (coerceToBuffer && typeof chunk === 'string') {
chunk = Buffer.from(chunk, 'utf8')
}
try {
// https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/send (note this doesn't have a cb as second arg)
socketRef.send(chunk)
} catch (err) {
return next(err)
}
next()
}
function socketEndBrowser(done: (error?: Error, data?: any) => void) {
if (socketRef) {
socketRef.close()
}
done()
}
// end methods for browserStreamBuilder
return stream
}
export { browserStreamBuilder, streamBuilder }