-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
BufferedDuplex.ts
113 lines (96 loc) · 2.68 KB
/
BufferedDuplex.ts
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
import { Duplex, Transform } from 'readable-stream'
import { Buffer } from 'buffer'
import { IClientOptions } from './client'
/**
* Utils writev function for browser, ensure to write Buffers to socket (convert strings).
*/
export function writev(
chunks: { chunk: any; encoding: string }[],
cb: (err?: Error) => void,
) {
const buffers = new Array(chunks.length)
for (let i = 0; i < chunks.length; i++) {
if (typeof chunks[i].chunk === 'string') {
buffers[i] = Buffer.from(chunks[i].chunk, 'utf8')
} else {
buffers[i] = chunks[i].chunk
}
}
this._write(Buffer.concat(buffers), 'binary', cb)
}
/**
* How this works:
* - `socket` is the `WebSocket` instance, the connection to our broker.
* - `proxy` is a `Transform`, it ensure data written to the `socket` is a `Buffer`.
* This class buffers the data written to the `proxy` (so then to `socket`) until the `socket` is ready.
* The stream returned from this class, will be passed to the `MqttClient`.
*/
export class BufferedDuplex extends Duplex {
public socket: WebSocket
private proxy: Transform
private isSocketOpen: boolean
private writeQueue: Array<{
chunk: any
encoding: string
cb: (err?: Error) => void
}>
constructor(opts: IClientOptions, proxy: Transform, socket: WebSocket) {
super({
objectMode: true,
})
this.proxy = proxy
this.socket = socket
this.writeQueue = []
if (!opts.objectMode) {
this._writev = writev.bind(this)
}
this.isSocketOpen = false
this.proxy.on('data', (chunk) => {
this.push(chunk)
})
}
_read(size?: number): void {
this.proxy.read(size)
}
_write(chunk: any, encoding: string, cb: (err?: Error) => void) {
if (!this.isSocketOpen) {
// Buffer the data in a queue
this.writeQueue.push({ chunk, encoding, cb })
} else {
this.writeToProxy(chunk, encoding, cb)
}
}
_final(callback: (error?: Error) => void): void {
this.writeQueue = []
this.proxy.end(callback)
}
_destroy(err: Error, callback: (error: Error) => void): void {
this.writeQueue = []
// do not pass error here otherwise we should listen for `error` event on proxy to prevent uncaught exception
this.proxy.destroy()
callback(err)
}
/** Method to call when socket is ready to stop buffering writes */
socketReady() {
this.emit('connect')
this.isSocketOpen = true
this.processWriteQueue()
}
private writeToProxy(
chunk: any,
encoding: string,
cb: (err?: Error) => void,
) {
if (this.proxy.write(chunk, encoding) === false) {
this.proxy.once('drain', cb)
} else {
cb()
}
}
private processWriteQueue() {
while (this.writeQueue.length > 0) {
const { chunk, encoding, cb } = this.writeQueue.shift()!
this.writeToProxy(chunk, encoding, cb)
}
}
}