mqtt 2.18.9 → 3.0.0
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/CONTRIBUTING.md +27 -27
- package/LICENSE.md +15 -15
- package/README.md +689 -612
- package/bin/pub.js +146 -146
- package/bin/sub.js +123 -123
- package/dist/mqtt.js +11916 -14116
- 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/examples/client/secure-client.js +24 -24
- package/examples/client/simple-both.js +13 -13
- package/examples/client/simple-publish.js +7 -7
- package/examples/client/simple-subscribe.js +9 -9
- package/examples/tls client/crt.ca.cg.pem +34 -34
- package/examples/tls client/mqttclient.js +48 -48
- package/examples/tls client/tls-cert.pem +13 -13
- package/examples/tls client/tls-key.pem +15 -15
- package/examples/wss/client.js +49 -49
- package/examples/wss/client_with_proxy.js +58 -58
- package/lib/client.js +1460 -1129
- package/lib/connect/ali.js +130 -0
- package/lib/connect/index.js +157 -149
- package/lib/connect/tcp.js +19 -19
- package/lib/connect/tls.js +41 -41
- package/lib/connect/ws.js +92 -92
- package/lib/connect/wx.js +134 -110
- package/lib/store.js +140 -140
- package/lib/validations.js +52 -52
- package/mqtt.js +41 -41
- package/package.json +112 -107
- package/test/abstract_client.js +3084 -2667
- package/test/abstract_store.js +135 -135
- package/test/browser/server.js +132 -132
- package/test/browser/test.js +92 -92
- package/test/client.js +1129 -544
- package/test/helpers/private-csr.pem +16 -16
- package/test/helpers/private-key.pem +27 -27
- package/test/helpers/public-cert.pem +19 -19
- package/test/helpers/public-key.pem +9 -9
- package/test/helpers/server.js +52 -52
- package/test/helpers/server_process.js +9 -9
- package/test/helpers/tls-cert.pem +14 -14
- package/test/helpers/tls-csr.pem +11 -11
- package/test/helpers/tls-key.pem +15 -15
- package/test/helpers/wrong-cert.pem +13 -13
- package/test/helpers/wrong-csr.pem +11 -11
- package/test/helpers/wrong-key.pem +15 -15
- package/test/mocha.opts +4 -4
- package/test/mqtt.js +230 -230
- package/test/mqtt_store.js +9 -9
- package/test/secure_client.js +157 -157
- package/test/server.js +93 -65
- package/test/store.js +10 -10
- package/test/typescript/broker-connect-subscribe-and-publish.js +24 -0
- package/test/typescript/broker-connect-subscribe-and-publish.js.map +1 -0
- package/test/typescript/broker-connect-subscribe-and-publish.ts +22 -22
- package/test/typescript/tsconfig.json +14 -14
- package/test/util.js +13 -13
- package/test/websocket_client.js +144 -144
- package/types/index.d.ts +27 -27
- package/types/lib/client-options.d.ts +178 -137
- package/types/lib/client.d.ts +231 -196
- 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/test/browser/wx.js +0 -93
package/lib/connect/ws.js
CHANGED
|
@@ -1,92 +1,92 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
var websocket = require('websocket-stream')
|
|
4
|
-
var urlModule = require('url')
|
|
5
|
-
var WSS_OPTIONS = [
|
|
6
|
-
'rejectUnauthorized',
|
|
7
|
-
'ca',
|
|
8
|
-
'cert',
|
|
9
|
-
'key',
|
|
10
|
-
'pfx',
|
|
11
|
-
'passphrase'
|
|
12
|
-
]
|
|
13
|
-
var IS_BROWSER = process.title === 'browser'
|
|
14
|
-
|
|
15
|
-
function buildUrl (opts, client) {
|
|
16
|
-
var url = opts.protocol + '://' + opts.hostname + ':' + opts.port + opts.path
|
|
17
|
-
if (typeof (opts.transformWsUrl) === 'function') {
|
|
18
|
-
url = opts.transformWsUrl(url, opts, client)
|
|
19
|
-
}
|
|
20
|
-
return url
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
function setDefaultOpts (opts) {
|
|
24
|
-
if (!opts.hostname) {
|
|
25
|
-
opts.hostname = 'localhost'
|
|
26
|
-
}
|
|
27
|
-
if (!opts.port) {
|
|
28
|
-
if (opts.protocol === 'wss') {
|
|
29
|
-
opts.port = 443
|
|
30
|
-
} else {
|
|
31
|
-
opts.port = 80
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
if (!opts.path) {
|
|
35
|
-
opts.path = '/'
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
if (!opts.wsOptions) {
|
|
39
|
-
opts.wsOptions = {}
|
|
40
|
-
}
|
|
41
|
-
if (!IS_BROWSER && opts.protocol === 'wss') {
|
|
42
|
-
// Add cert/key/ca etc options
|
|
43
|
-
WSS_OPTIONS.forEach(function (prop) {
|
|
44
|
-
if (opts.hasOwnProperty(prop) && !opts.wsOptions.hasOwnProperty(prop)) {
|
|
45
|
-
opts.wsOptions[prop] = opts[prop]
|
|
46
|
-
}
|
|
47
|
-
})
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function createWebSocket (client, opts) {
|
|
52
|
-
var websocketSubProtocol =
|
|
53
|
-
(opts.protocolId === 'MQIsdp') && (opts.protocolVersion === 3)
|
|
54
|
-
? 'mqttv3.1'
|
|
55
|
-
: 'mqtt'
|
|
56
|
-
|
|
57
|
-
setDefaultOpts(opts)
|
|
58
|
-
var url = buildUrl(opts, client)
|
|
59
|
-
return websocket(url, [websocketSubProtocol], opts.wsOptions)
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
function buildBuilder (client, opts) {
|
|
63
|
-
return createWebSocket(client, opts)
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
function buildBuilderBrowser (client, opts) {
|
|
67
|
-
if (!opts.hostname) {
|
|
68
|
-
opts.hostname = opts.host
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
if (!opts.hostname) {
|
|
72
|
-
// Throwing an error in a Web Worker if no `hostname` is given, because we
|
|
73
|
-
// can not determine the `hostname` automatically. If connecting to
|
|
74
|
-
// localhost, please supply the `hostname` as an argument.
|
|
75
|
-
if (typeof (document) === 'undefined') {
|
|
76
|
-
throw new Error('Could not determine host. Specify host manually.')
|
|
77
|
-
}
|
|
78
|
-
var parsed = urlModule.parse(document.URL)
|
|
79
|
-
opts.hostname = parsed.hostname
|
|
80
|
-
|
|
81
|
-
if (!opts.port) {
|
|
82
|
-
opts.port = parsed.port
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
return createWebSocket(client, opts)
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
if (IS_BROWSER) {
|
|
89
|
-
module.exports = buildBuilderBrowser
|
|
90
|
-
} else {
|
|
91
|
-
module.exports = buildBuilder
|
|
92
|
-
}
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
var websocket = require('websocket-stream')
|
|
4
|
+
var urlModule = require('url')
|
|
5
|
+
var WSS_OPTIONS = [
|
|
6
|
+
'rejectUnauthorized',
|
|
7
|
+
'ca',
|
|
8
|
+
'cert',
|
|
9
|
+
'key',
|
|
10
|
+
'pfx',
|
|
11
|
+
'passphrase'
|
|
12
|
+
]
|
|
13
|
+
var IS_BROWSER = process.title === 'browser'
|
|
14
|
+
|
|
15
|
+
function buildUrl (opts, client) {
|
|
16
|
+
var url = opts.protocol + '://' + opts.hostname + ':' + opts.port + opts.path
|
|
17
|
+
if (typeof (opts.transformWsUrl) === 'function') {
|
|
18
|
+
url = opts.transformWsUrl(url, opts, client)
|
|
19
|
+
}
|
|
20
|
+
return url
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function setDefaultOpts (opts) {
|
|
24
|
+
if (!opts.hostname) {
|
|
25
|
+
opts.hostname = 'localhost'
|
|
26
|
+
}
|
|
27
|
+
if (!opts.port) {
|
|
28
|
+
if (opts.protocol === 'wss') {
|
|
29
|
+
opts.port = 443
|
|
30
|
+
} else {
|
|
31
|
+
opts.port = 80
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
if (!opts.path) {
|
|
35
|
+
opts.path = '/'
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (!opts.wsOptions) {
|
|
39
|
+
opts.wsOptions = {}
|
|
40
|
+
}
|
|
41
|
+
if (!IS_BROWSER && opts.protocol === 'wss') {
|
|
42
|
+
// Add cert/key/ca etc options
|
|
43
|
+
WSS_OPTIONS.forEach(function (prop) {
|
|
44
|
+
if (opts.hasOwnProperty(prop) && !opts.wsOptions.hasOwnProperty(prop)) {
|
|
45
|
+
opts.wsOptions[prop] = opts[prop]
|
|
46
|
+
}
|
|
47
|
+
})
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function createWebSocket (client, opts) {
|
|
52
|
+
var websocketSubProtocol =
|
|
53
|
+
(opts.protocolId === 'MQIsdp') && (opts.protocolVersion === 3)
|
|
54
|
+
? 'mqttv3.1'
|
|
55
|
+
: 'mqtt'
|
|
56
|
+
|
|
57
|
+
setDefaultOpts(opts)
|
|
58
|
+
var url = buildUrl(opts, client)
|
|
59
|
+
return websocket(url, [websocketSubProtocol], opts.wsOptions)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function buildBuilder (client, opts) {
|
|
63
|
+
return createWebSocket(client, opts)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function buildBuilderBrowser (client, opts) {
|
|
67
|
+
if (!opts.hostname) {
|
|
68
|
+
opts.hostname = opts.host
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (!opts.hostname) {
|
|
72
|
+
// Throwing an error in a Web Worker if no `hostname` is given, because we
|
|
73
|
+
// can not determine the `hostname` automatically. If connecting to
|
|
74
|
+
// localhost, please supply the `hostname` as an argument.
|
|
75
|
+
if (typeof (document) === 'undefined') {
|
|
76
|
+
throw new Error('Could not determine host. Specify host manually.')
|
|
77
|
+
}
|
|
78
|
+
var parsed = urlModule.parse(document.URL)
|
|
79
|
+
opts.hostname = parsed.hostname
|
|
80
|
+
|
|
81
|
+
if (!opts.port) {
|
|
82
|
+
opts.port = parsed.port
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return createWebSocket(client, opts)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (IS_BROWSER) {
|
|
89
|
+
module.exports = buildBuilderBrowser
|
|
90
|
+
} else {
|
|
91
|
+
module.exports = buildBuilder
|
|
92
|
+
}
|
package/lib/connect/wx.js
CHANGED
|
@@ -1,110 +1,134 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var
|
|
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
|
-
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
var Transform = require('readable-stream').Transform
|
|
4
|
+
var duplexify = require('duplexify')
|
|
5
|
+
|
|
6
|
+
/* global wx */
|
|
7
|
+
var socketTask
|
|
8
|
+
var proxy
|
|
9
|
+
var stream
|
|
10
|
+
|
|
11
|
+
function buildProxy () {
|
|
12
|
+
var proxy = new Transform()
|
|
13
|
+
proxy._write = function (chunk, encoding, next) {
|
|
14
|
+
socketTask.send({
|
|
15
|
+
data: chunk.buffer,
|
|
16
|
+
success: function () {
|
|
17
|
+
next()
|
|
18
|
+
},
|
|
19
|
+
fail: function (errMsg) {
|
|
20
|
+
next(new Error(errMsg))
|
|
21
|
+
}
|
|
22
|
+
})
|
|
23
|
+
}
|
|
24
|
+
proxy._flush = function socketEnd (done) {
|
|
25
|
+
socketTask.close({
|
|
26
|
+
success: function () {
|
|
27
|
+
done()
|
|
28
|
+
}
|
|
29
|
+
})
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return proxy
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function setDefaultOpts (opts) {
|
|
36
|
+
if (!opts.hostname) {
|
|
37
|
+
opts.hostname = 'localhost'
|
|
38
|
+
}
|
|
39
|
+
if (!opts.path) {
|
|
40
|
+
opts.path = '/'
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (!opts.wsOptions) {
|
|
44
|
+
opts.wsOptions = {}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function buildUrl (opts, client) {
|
|
49
|
+
var protocol = opts.protocol === 'wxs' ? 'wss' : 'ws'
|
|
50
|
+
var url = protocol + '://' + opts.hostname + opts.path
|
|
51
|
+
if (opts.port && opts.port !== 80 && opts.port !== 443) {
|
|
52
|
+
url = protocol + '://' + opts.hostname + ':' + opts.port + opts.path
|
|
53
|
+
}
|
|
54
|
+
if (typeof (opts.transformWsUrl) === 'function') {
|
|
55
|
+
url = opts.transformWsUrl(url, opts, client)
|
|
56
|
+
}
|
|
57
|
+
return url
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function bindEventHandler () {
|
|
61
|
+
socketTask.onOpen(function () {
|
|
62
|
+
stream.setReadable(proxy)
|
|
63
|
+
stream.setWritable(proxy)
|
|
64
|
+
stream.emit('connect')
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
socketTask.onMessage(function (res) {
|
|
68
|
+
var data = res.data
|
|
69
|
+
|
|
70
|
+
if (data instanceof ArrayBuffer) data = Buffer.from(data)
|
|
71
|
+
else data = Buffer.from(data, 'utf8')
|
|
72
|
+
proxy.push(data)
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
socketTask.onClose(function () {
|
|
76
|
+
stream.end()
|
|
77
|
+
stream.destroy()
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
socketTask.onError(function (res) {
|
|
81
|
+
stream.destroy(new Error(res.errMsg))
|
|
82
|
+
})
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function buildStream (client, opts) {
|
|
86
|
+
opts.hostname = opts.hostname || opts.host
|
|
87
|
+
|
|
88
|
+
if (!opts.hostname) {
|
|
89
|
+
throw new Error('Could not determine host. Specify host manually.')
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
var websocketSubProtocol =
|
|
93
|
+
(opts.protocolId === 'MQIsdp') && (opts.protocolVersion === 3)
|
|
94
|
+
? 'mqttv3.1'
|
|
95
|
+
: 'mqtt'
|
|
96
|
+
|
|
97
|
+
setDefaultOpts(opts)
|
|
98
|
+
|
|
99
|
+
var url = buildUrl(opts, client)
|
|
100
|
+
socketTask = wx.connectSocket({
|
|
101
|
+
url: url,
|
|
102
|
+
protocols: websocketSubProtocol
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
proxy = buildProxy()
|
|
106
|
+
stream = duplexify.obj()
|
|
107
|
+
stream._destroy = function (err, cb) {
|
|
108
|
+
socketTask.close({
|
|
109
|
+
success: function () {
|
|
110
|
+
cb && cb(err)
|
|
111
|
+
}
|
|
112
|
+
})
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
var destroyRef = stream.destroy
|
|
116
|
+
stream.destroy = function () {
|
|
117
|
+
stream.destroy = destroyRef
|
|
118
|
+
|
|
119
|
+
var self = this
|
|
120
|
+
process.nextTick(function () {
|
|
121
|
+
socketTask.close({
|
|
122
|
+
fail: function () {
|
|
123
|
+
self._destroy(new Error())
|
|
124
|
+
}
|
|
125
|
+
})
|
|
126
|
+
})
|
|
127
|
+
}.bind(stream)
|
|
128
|
+
|
|
129
|
+
bindEventHandler()
|
|
130
|
+
|
|
131
|
+
return stream
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
module.exports = buildStream
|