mqtt 4.2.4 → 4.2.8
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/CHANGELOG.md +23 -0
- package/CONTRIBUTING.md +27 -27
- package/LICENSE.md +15 -15
- package/README.md +792 -784
- package/bin/mqtt.js +27 -27
- package/bin/pub.js +146 -146
- package/bin/sub.js +123 -123
- package/dist/mqtt.js +7466 -8450
- package/dist/mqtt.min.js +1 -1
- package/doc/help.txt +8 -8
- package/doc/publish.txt +26 -26
- package/doc/subscribe.txt +26 -26
- package/lib/client.js +1581 -1581
- package/lib/connect/ali.js +128 -128
- package/lib/connect/index.js +164 -177
- package/lib/connect/tcp.js +21 -21
- package/lib/connect/tls.js +45 -45
- package/lib/connect/ws.js +256 -255
- package/lib/connect/wx.js +134 -134
- package/lib/store.js +128 -128
- package/lib/validations.js +52 -52
- package/mqtt.js +17 -17
- package/package.json +109 -108
- package/types/index.d.ts +28 -27
- package/types/lib/client-options.d.ts +191 -191
- package/types/lib/client.d.ts +240 -232
- package/types/lib/connect/index.d.ts +10 -10
- package/types/lib/store-options.d.ts +6 -6
- package/types/lib/store.d.ts +46 -46
package/lib/connect/tls.js
CHANGED
|
@@ -1,45 +1,45 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
var tls = require('tls')
|
|
3
|
-
var debug = require('debug')('mqttjs:tls')
|
|
4
|
-
|
|
5
|
-
function buildBuilder (mqttClient, opts) {
|
|
6
|
-
var connection
|
|
7
|
-
opts.port = opts.port || 8883
|
|
8
|
-
opts.host = opts.hostname || opts.host || 'localhost'
|
|
9
|
-
opts.servername = opts.host
|
|
10
|
-
|
|
11
|
-
opts.rejectUnauthorized = opts.rejectUnauthorized !== false
|
|
12
|
-
|
|
13
|
-
delete opts.path
|
|
14
|
-
|
|
15
|
-
debug('port %d host %s rejectUnauthorized %b', opts.port, opts.host, opts.rejectUnauthorized)
|
|
16
|
-
|
|
17
|
-
connection = tls.connect(opts)
|
|
18
|
-
/* eslint no-use-before-define: [2, "nofunc"] */
|
|
19
|
-
connection.on('secureConnect', function () {
|
|
20
|
-
if (opts.rejectUnauthorized && !connection.authorized) {
|
|
21
|
-
connection.emit('error', new Error('TLS not authorized'))
|
|
22
|
-
} else {
|
|
23
|
-
connection.removeListener('error', handleTLSerrors)
|
|
24
|
-
}
|
|
25
|
-
})
|
|
26
|
-
|
|
27
|
-
function handleTLSerrors (err) {
|
|
28
|
-
// How can I get verify this error is a tls error?
|
|
29
|
-
if (opts.rejectUnauthorized) {
|
|
30
|
-
mqttClient.emit('error', err)
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// close this connection to match the behaviour of net
|
|
34
|
-
// otherwise all we get is an error from the connection
|
|
35
|
-
// and close event doesn't fire. This is a work around
|
|
36
|
-
// to enable the reconnect code to work the same as with
|
|
37
|
-
// net.createConnection
|
|
38
|
-
connection.end()
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
connection.on('error', handleTLSerrors)
|
|
42
|
-
return connection
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
module.exports = buildBuilder
|
|
1
|
+
'use strict'
|
|
2
|
+
var tls = require('tls')
|
|
3
|
+
var debug = require('debug')('mqttjs:tls')
|
|
4
|
+
|
|
5
|
+
function buildBuilder (mqttClient, opts) {
|
|
6
|
+
var connection
|
|
7
|
+
opts.port = opts.port || 8883
|
|
8
|
+
opts.host = opts.hostname || opts.host || 'localhost'
|
|
9
|
+
opts.servername = opts.host
|
|
10
|
+
|
|
11
|
+
opts.rejectUnauthorized = opts.rejectUnauthorized !== false
|
|
12
|
+
|
|
13
|
+
delete opts.path
|
|
14
|
+
|
|
15
|
+
debug('port %d host %s rejectUnauthorized %b', opts.port, opts.host, opts.rejectUnauthorized)
|
|
16
|
+
|
|
17
|
+
connection = tls.connect(opts)
|
|
18
|
+
/* eslint no-use-before-define: [2, "nofunc"] */
|
|
19
|
+
connection.on('secureConnect', function () {
|
|
20
|
+
if (opts.rejectUnauthorized && !connection.authorized) {
|
|
21
|
+
connection.emit('error', new Error('TLS not authorized'))
|
|
22
|
+
} else {
|
|
23
|
+
connection.removeListener('error', handleTLSerrors)
|
|
24
|
+
}
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
function handleTLSerrors (err) {
|
|
28
|
+
// How can I get verify this error is a tls error?
|
|
29
|
+
if (opts.rejectUnauthorized) {
|
|
30
|
+
mqttClient.emit('error', err)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// close this connection to match the behaviour of net
|
|
34
|
+
// otherwise all we get is an error from the connection
|
|
35
|
+
// and close event doesn't fire. This is a work around
|
|
36
|
+
// to enable the reconnect code to work the same as with
|
|
37
|
+
// net.createConnection
|
|
38
|
+
connection.end()
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
connection.on('error', handleTLSerrors)
|
|
42
|
+
return connection
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
module.exports = buildBuilder
|
package/lib/connect/ws.js
CHANGED
|
@@ -1,255 +1,256 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const WS = require('ws')
|
|
4
|
-
const debug = require('debug')('mqttjs:ws')
|
|
5
|
-
const duplexify = require('duplexify')
|
|
6
|
-
const Transform = require('readable-stream').Transform
|
|
7
|
-
|
|
8
|
-
let WSS_OPTIONS = [
|
|
9
|
-
'rejectUnauthorized',
|
|
10
|
-
'ca',
|
|
11
|
-
'cert',
|
|
12
|
-
'key',
|
|
13
|
-
'pfx',
|
|
14
|
-
'passphrase'
|
|
15
|
-
]
|
|
16
|
-
// eslint-disable-next-line camelcase
|
|
17
|
-
const IS_BROWSER = (typeof process !== 'undefined' && process.title === 'browser') || typeof __webpack_require__ === 'function'
|
|
18
|
-
function buildUrl (opts, client) {
|
|
19
|
-
let url = opts.protocol + '://' + opts.hostname + ':' + opts.port + opts.path
|
|
20
|
-
if (typeof (opts.transformWsUrl) === 'function') {
|
|
21
|
-
url = opts.transformWsUrl(url, opts, client)
|
|
22
|
-
}
|
|
23
|
-
return url
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
function setDefaultOpts (opts) {
|
|
27
|
-
let options = opts
|
|
28
|
-
if (!opts.hostname) {
|
|
29
|
-
options.hostname = 'localhost'
|
|
30
|
-
}
|
|
31
|
-
if (!opts.port) {
|
|
32
|
-
if (opts.protocol === 'wss') {
|
|
33
|
-
options.port = 443
|
|
34
|
-
} else {
|
|
35
|
-
options.port = 80
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
if (!opts.path) {
|
|
39
|
-
options.path = '/'
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
if (!opts.wsOptions) {
|
|
43
|
-
options.wsOptions = {}
|
|
44
|
-
}
|
|
45
|
-
if (!IS_BROWSER && opts.protocol === 'wss') {
|
|
46
|
-
// Add cert/key/ca etc options
|
|
47
|
-
WSS_OPTIONS.forEach(function (prop) {
|
|
48
|
-
if (opts.hasOwnProperty(prop) && !opts.wsOptions.hasOwnProperty(prop)) {
|
|
49
|
-
options.wsOptions[prop] = opts[prop]
|
|
50
|
-
}
|
|
51
|
-
})
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
return options
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
function setDefaultBrowserOpts (opts) {
|
|
58
|
-
let options = setDefaultOpts(opts)
|
|
59
|
-
|
|
60
|
-
if (!options.hostname) {
|
|
61
|
-
options.hostname = options.host
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
if (!options.hostname) {
|
|
65
|
-
// Throwing an error in a Web Worker if no `hostname` is given, because we
|
|
66
|
-
// can not determine the `hostname` automatically. If connecting to
|
|
67
|
-
// localhost, please supply the `hostname` as an argument.
|
|
68
|
-
if (typeof (document) === 'undefined') {
|
|
69
|
-
throw new Error('Could not determine host. Specify host manually.')
|
|
70
|
-
}
|
|
71
|
-
const parsed = new URL(document.URL)
|
|
72
|
-
options.hostname = parsed.hostname
|
|
73
|
-
|
|
74
|
-
if (!options.port) {
|
|
75
|
-
options.port = parsed.port
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
// objectMode should be defined for logic
|
|
80
|
-
if (options.objectMode === undefined) {
|
|
81
|
-
options.objectMode = !(options.binary === true || options.binary === undefined)
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
return options
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
function createWebSocket (client, url, opts) {
|
|
88
|
-
debug('createWebSocket')
|
|
89
|
-
debug('protocol: ' + opts.protocolId + ' ' + opts.protocolVersion)
|
|
90
|
-
const websocketSubProtocol =
|
|
91
|
-
(opts.protocolId === 'MQIsdp') && (opts.protocolVersion === 3)
|
|
92
|
-
? 'mqttv3.1'
|
|
93
|
-
: 'mqtt'
|
|
94
|
-
|
|
95
|
-
debug('creating new Websocket for url: ' + url + ' and protocol: ' + websocketSubProtocol)
|
|
96
|
-
let socket = new WS(url, [websocketSubProtocol], opts.wsOptions)
|
|
97
|
-
return socket
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
function createBrowserWebSocket (client, opts) {
|
|
101
|
-
const websocketSubProtocol =
|
|
102
|
-
(opts.protocolId === 'MQIsdp') && (opts.protocolVersion === 3)
|
|
103
|
-
? 'mqttv3.1'
|
|
104
|
-
: 'mqtt'
|
|
105
|
-
|
|
106
|
-
let url = buildUrl(opts, client)
|
|
107
|
-
/* global WebSocket */
|
|
108
|
-
let socket = new WebSocket(url, [websocketSubProtocol])
|
|
109
|
-
socket.binaryType = 'arraybuffer'
|
|
110
|
-
return socket
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
function streamBuilder (client, opts) {
|
|
114
|
-
debug('streamBuilder')
|
|
115
|
-
let options = setDefaultOpts(opts)
|
|
116
|
-
const url = buildUrl(options, client)
|
|
117
|
-
let socket = createWebSocket(client, url, options)
|
|
118
|
-
let webSocketStream = WS.createWebSocketStream(socket, options.wsOptions)
|
|
119
|
-
webSocketStream.url = url
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
let
|
|
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
|
-
socket.addEventListener('
|
|
166
|
-
socket.addEventListener('
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
socket.
|
|
170
|
-
socket.
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
proxy.
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
stream.
|
|
189
|
-
stream.
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
stream.
|
|
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
|
-
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const WS = require('ws')
|
|
4
|
+
const debug = require('debug')('mqttjs:ws')
|
|
5
|
+
const duplexify = require('duplexify')
|
|
6
|
+
const Transform = require('readable-stream').Transform
|
|
7
|
+
|
|
8
|
+
let WSS_OPTIONS = [
|
|
9
|
+
'rejectUnauthorized',
|
|
10
|
+
'ca',
|
|
11
|
+
'cert',
|
|
12
|
+
'key',
|
|
13
|
+
'pfx',
|
|
14
|
+
'passphrase'
|
|
15
|
+
]
|
|
16
|
+
// eslint-disable-next-line camelcase
|
|
17
|
+
const IS_BROWSER = (typeof process !== 'undefined' && process.title === 'browser') || typeof __webpack_require__ === 'function'
|
|
18
|
+
function buildUrl (opts, client) {
|
|
19
|
+
let url = opts.protocol + '://' + opts.hostname + ':' + opts.port + opts.path
|
|
20
|
+
if (typeof (opts.transformWsUrl) === 'function') {
|
|
21
|
+
url = opts.transformWsUrl(url, opts, client)
|
|
22
|
+
}
|
|
23
|
+
return url
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function setDefaultOpts (opts) {
|
|
27
|
+
let options = opts
|
|
28
|
+
if (!opts.hostname) {
|
|
29
|
+
options.hostname = 'localhost'
|
|
30
|
+
}
|
|
31
|
+
if (!opts.port) {
|
|
32
|
+
if (opts.protocol === 'wss') {
|
|
33
|
+
options.port = 443
|
|
34
|
+
} else {
|
|
35
|
+
options.port = 80
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (!opts.path) {
|
|
39
|
+
options.path = '/'
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (!opts.wsOptions) {
|
|
43
|
+
options.wsOptions = {}
|
|
44
|
+
}
|
|
45
|
+
if (!IS_BROWSER && opts.protocol === 'wss') {
|
|
46
|
+
// Add cert/key/ca etc options
|
|
47
|
+
WSS_OPTIONS.forEach(function (prop) {
|
|
48
|
+
if (opts.hasOwnProperty(prop) && !opts.wsOptions.hasOwnProperty(prop)) {
|
|
49
|
+
options.wsOptions[prop] = opts[prop]
|
|
50
|
+
}
|
|
51
|
+
})
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return options
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function setDefaultBrowserOpts (opts) {
|
|
58
|
+
let options = setDefaultOpts(opts)
|
|
59
|
+
|
|
60
|
+
if (!options.hostname) {
|
|
61
|
+
options.hostname = options.host
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (!options.hostname) {
|
|
65
|
+
// Throwing an error in a Web Worker if no `hostname` is given, because we
|
|
66
|
+
// can not determine the `hostname` automatically. If connecting to
|
|
67
|
+
// localhost, please supply the `hostname` as an argument.
|
|
68
|
+
if (typeof (document) === 'undefined') {
|
|
69
|
+
throw new Error('Could not determine host. Specify host manually.')
|
|
70
|
+
}
|
|
71
|
+
const parsed = new URL(document.URL)
|
|
72
|
+
options.hostname = parsed.hostname
|
|
73
|
+
|
|
74
|
+
if (!options.port) {
|
|
75
|
+
options.port = parsed.port
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// objectMode should be defined for logic
|
|
80
|
+
if (options.objectMode === undefined) {
|
|
81
|
+
options.objectMode = !(options.binary === true || options.binary === undefined)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return options
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function createWebSocket (client, url, opts) {
|
|
88
|
+
debug('createWebSocket')
|
|
89
|
+
debug('protocol: ' + opts.protocolId + ' ' + opts.protocolVersion)
|
|
90
|
+
const websocketSubProtocol =
|
|
91
|
+
(opts.protocolId === 'MQIsdp') && (opts.protocolVersion === 3)
|
|
92
|
+
? 'mqttv3.1'
|
|
93
|
+
: 'mqtt'
|
|
94
|
+
|
|
95
|
+
debug('creating new Websocket for url: ' + url + ' and protocol: ' + websocketSubProtocol)
|
|
96
|
+
let socket = new WS(url, [websocketSubProtocol], opts.wsOptions)
|
|
97
|
+
return socket
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function createBrowserWebSocket (client, opts) {
|
|
101
|
+
const websocketSubProtocol =
|
|
102
|
+
(opts.protocolId === 'MQIsdp') && (opts.protocolVersion === 3)
|
|
103
|
+
? 'mqttv3.1'
|
|
104
|
+
: 'mqtt'
|
|
105
|
+
|
|
106
|
+
let url = buildUrl(opts, client)
|
|
107
|
+
/* global WebSocket */
|
|
108
|
+
let socket = new WebSocket(url, [websocketSubProtocol])
|
|
109
|
+
socket.binaryType = 'arraybuffer'
|
|
110
|
+
return socket
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function streamBuilder (client, opts) {
|
|
114
|
+
debug('streamBuilder')
|
|
115
|
+
let options = setDefaultOpts(opts)
|
|
116
|
+
const url = buildUrl(options, client)
|
|
117
|
+
let socket = createWebSocket(client, url, options)
|
|
118
|
+
let webSocketStream = WS.createWebSocketStream(socket, options.wsOptions)
|
|
119
|
+
webSocketStream.url = url
|
|
120
|
+
socket.on('close', () => { webSocketStream.destroy() })
|
|
121
|
+
return webSocketStream
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function browserStreamBuilder (client, opts) {
|
|
125
|
+
debug('browserStreamBuilder')
|
|
126
|
+
let stream
|
|
127
|
+
let options = setDefaultBrowserOpts(opts)
|
|
128
|
+
// sets the maximum socket buffer size before throttling
|
|
129
|
+
const bufferSize = options.browserBufferSize || 1024 * 512
|
|
130
|
+
|
|
131
|
+
const bufferTimeout = opts.browserBufferTimeout || 1000
|
|
132
|
+
|
|
133
|
+
const coerceToBuffer = !opts.objectMode
|
|
134
|
+
|
|
135
|
+
let socket = createBrowserWebSocket(client, opts)
|
|
136
|
+
|
|
137
|
+
let proxy = buildProxy(opts, socketWriteBrowser, socketEndBrowser)
|
|
138
|
+
|
|
139
|
+
if (!opts.objectMode) {
|
|
140
|
+
proxy._writev = writev
|
|
141
|
+
}
|
|
142
|
+
proxy.on('close', () => { socket.close() })
|
|
143
|
+
|
|
144
|
+
const eventListenerSupport = (typeof socket.addEventListener !== 'undefined')
|
|
145
|
+
|
|
146
|
+
// was already open when passed in
|
|
147
|
+
if (socket.readyState === socket.OPEN) {
|
|
148
|
+
stream = proxy
|
|
149
|
+
} else {
|
|
150
|
+
stream = stream = duplexify(undefined, undefined, opts)
|
|
151
|
+
if (!opts.objectMode) {
|
|
152
|
+
stream._writev = writev
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (eventListenerSupport) {
|
|
156
|
+
socket.addEventListener('open', onopen)
|
|
157
|
+
} else {
|
|
158
|
+
socket.onopen = onopen
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
stream.socket = socket
|
|
163
|
+
|
|
164
|
+
if (eventListenerSupport) {
|
|
165
|
+
socket.addEventListener('close', onclose)
|
|
166
|
+
socket.addEventListener('error', onerror)
|
|
167
|
+
socket.addEventListener('message', onmessage)
|
|
168
|
+
} else {
|
|
169
|
+
socket.onclose = onclose
|
|
170
|
+
socket.onerror = onerror
|
|
171
|
+
socket.onmessage = onmessage
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// methods for browserStreamBuilder
|
|
175
|
+
|
|
176
|
+
function buildProxy (options, socketWrite, socketEnd) {
|
|
177
|
+
let proxy = new Transform({
|
|
178
|
+
objectModeMode: options.objectMode
|
|
179
|
+
})
|
|
180
|
+
|
|
181
|
+
proxy._write = socketWrite
|
|
182
|
+
proxy._flush = socketEnd
|
|
183
|
+
|
|
184
|
+
return proxy
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function onopen () {
|
|
188
|
+
stream.setReadable(proxy)
|
|
189
|
+
stream.setWritable(proxy)
|
|
190
|
+
stream.emit('connect')
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function onclose () {
|
|
194
|
+
stream.end()
|
|
195
|
+
stream.destroy()
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function onerror (err) {
|
|
199
|
+
stream.destroy(err)
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function onmessage (event) {
|
|
203
|
+
let data = event.data
|
|
204
|
+
if (data instanceof ArrayBuffer) data = Buffer.from(data)
|
|
205
|
+
else data = Buffer.from(data, 'utf8')
|
|
206
|
+
proxy.push(data)
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// this is to be enabled only if objectMode is false
|
|
210
|
+
function writev (chunks, cb) {
|
|
211
|
+
const buffers = new Array(chunks.length)
|
|
212
|
+
for (let i = 0; i < chunks.length; i++) {
|
|
213
|
+
if (typeof chunks[i].chunk === 'string') {
|
|
214
|
+
buffers[i] = Buffer.from(chunks[i], 'utf8')
|
|
215
|
+
} else {
|
|
216
|
+
buffers[i] = chunks[i].chunk
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
this._write(Buffer.concat(buffers), 'binary', cb)
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function socketWriteBrowser (chunk, enc, next) {
|
|
224
|
+
if (socket.bufferedAmount > bufferSize) {
|
|
225
|
+
// throttle data until buffered amount is reduced.
|
|
226
|
+
setTimeout(socketWriteBrowser, bufferTimeout, chunk, enc, next)
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
if (coerceToBuffer && typeof chunk === 'string') {
|
|
230
|
+
chunk = Buffer.from(chunk, 'utf8')
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
try {
|
|
234
|
+
socket.send(chunk)
|
|
235
|
+
} catch (err) {
|
|
236
|
+
return next(err)
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
next()
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function socketEndBrowser (done) {
|
|
243
|
+
socket.close()
|
|
244
|
+
done()
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// end methods for browserStreamBuilder
|
|
248
|
+
|
|
249
|
+
return stream
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
if (IS_BROWSER) {
|
|
253
|
+
module.exports = browserStreamBuilder
|
|
254
|
+
} else {
|
|
255
|
+
module.exports = streamBuilder
|
|
256
|
+
}
|