mqtt 5.0.0-beta.1 → 5.0.0-beta.3
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 +44 -28
- package/bin/mqtt.js +7 -7
- package/bin/pub.js +139 -126
- package/bin/sub.js +127 -109
- package/dist/mqtt.js +5513 -4892
- package/dist/mqtt.min.js +4 -4
- package/lib/client.js +1642 -1884
- package/lib/connect/ali.js +100 -103
- package/lib/connect/index.js +135 -129
- package/lib/connect/tcp.js +7 -8
- package/lib/connect/tls.js +45 -41
- package/lib/connect/ws.js +240 -232
- package/lib/connect/wx.js +117 -118
- package/lib/default-message-id-provider.js +56 -62
- package/lib/handlers/ack.js +153 -0
- package/lib/handlers/auth.js +34 -0
- package/lib/handlers/connack.js +51 -0
- package/lib/handlers/index.js +66 -0
- package/lib/handlers/publish.js +167 -0
- package/lib/handlers/pubrel.js +24 -0
- package/lib/is-browser.js +5 -5
- package/lib/store.js +104 -111
- package/lib/topic-alias-recv.js +33 -36
- package/lib/topic-alias-send.js +71 -74
- package/lib/unique-message-id-provider.js +53 -57
- package/lib/validations.js +27 -29
- package/package.json +40 -45
- package/types/lib/client-options.d.ts +17 -1
- package/types/lib/client.d.ts +13 -1
package/bin/sub.js
CHANGED
|
@@ -1,123 +1,141 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
const mqtt = require('
|
|
3
|
+
const mqtt = require('..')
|
|
4
4
|
const path = require('path')
|
|
5
5
|
const fs = require('fs')
|
|
6
6
|
const helpMe = require('help-me')({
|
|
7
|
-
|
|
7
|
+
dir: path.join(__dirname, '..', 'doc'),
|
|
8
8
|
})
|
|
9
9
|
const minimist = require('minimist')
|
|
10
10
|
|
|
11
|
-
function start
|
|
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
|
-
|
|
11
|
+
function start(args) {
|
|
12
|
+
args = minimist(args, {
|
|
13
|
+
string: [
|
|
14
|
+
'hostname',
|
|
15
|
+
'username',
|
|
16
|
+
'password',
|
|
17
|
+
'key',
|
|
18
|
+
'cert',
|
|
19
|
+
'ca',
|
|
20
|
+
'clientId',
|
|
21
|
+
'i',
|
|
22
|
+
'id',
|
|
23
|
+
],
|
|
24
|
+
boolean: ['stdin', 'help', 'clean', 'insecure'],
|
|
25
|
+
alias: {
|
|
26
|
+
port: 'p',
|
|
27
|
+
hostname: ['h', 'host'],
|
|
28
|
+
topic: 't',
|
|
29
|
+
qos: 'q',
|
|
30
|
+
clean: 'c',
|
|
31
|
+
keepalive: 'k',
|
|
32
|
+
clientId: ['i', 'id'],
|
|
33
|
+
username: 'u',
|
|
34
|
+
password: 'P',
|
|
35
|
+
protocol: ['C', 'l'],
|
|
36
|
+
verbose: 'v',
|
|
37
|
+
help: '-H',
|
|
38
|
+
ca: 'cafile',
|
|
39
|
+
},
|
|
40
|
+
default: {
|
|
41
|
+
host: 'localhost',
|
|
42
|
+
qos: 0,
|
|
43
|
+
retain: false,
|
|
44
|
+
clean: true,
|
|
45
|
+
keepAlive: 30, // 30 sec
|
|
46
|
+
},
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
if (args.help) {
|
|
50
|
+
return helpMe.toStdout('subscribe')
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
args.topic = args.topic || args._.shift()
|
|
54
|
+
|
|
55
|
+
if (!args.topic) {
|
|
56
|
+
console.error('missing topic\n')
|
|
57
|
+
return helpMe.toStdout('subscribe')
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (args.key) {
|
|
61
|
+
args.key = fs.readFileSync(args.key)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (args.cert) {
|
|
65
|
+
args.cert = fs.readFileSync(args.cert)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (args.ca) {
|
|
69
|
+
args.ca = fs.readFileSync(args.ca)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (args.key && args.cert && !args.protocol) {
|
|
73
|
+
args.protocol = 'mqtts'
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (args.insecure) {
|
|
77
|
+
args.rejectUnauthorized = false
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (args.port) {
|
|
81
|
+
if (typeof args.port !== 'number') {
|
|
82
|
+
console.warn(
|
|
83
|
+
"# Port: number expected, '%s' was given.",
|
|
84
|
+
typeof args.port,
|
|
85
|
+
)
|
|
86
|
+
return
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (args['will-topic']) {
|
|
91
|
+
args.will = {}
|
|
92
|
+
args.will.topic = args['will-topic']
|
|
93
|
+
args.will.payload = args['will-message']
|
|
94
|
+
args.will.qos = args['will-qos']
|
|
95
|
+
args.will.retain = args['will-retain']
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
args.keepAlive = args['keep-alive']
|
|
99
|
+
|
|
100
|
+
const client = mqtt.connect(args)
|
|
101
|
+
|
|
102
|
+
client.on('connect', () => {
|
|
103
|
+
client.subscribe(args.topic, { qos: args.qos }, (err, result) => {
|
|
104
|
+
if (err) {
|
|
105
|
+
console.error(err)
|
|
106
|
+
process.exit(1)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
result.forEach((sub) => {
|
|
110
|
+
if (sub.qos > 2) {
|
|
111
|
+
console.error(
|
|
112
|
+
'subscription negated to',
|
|
113
|
+
sub.topic,
|
|
114
|
+
'with code',
|
|
115
|
+
sub.qos,
|
|
116
|
+
)
|
|
117
|
+
process.exit(1)
|
|
118
|
+
}
|
|
119
|
+
})
|
|
120
|
+
})
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
client.on('message', (topic, payload) => {
|
|
124
|
+
if (args.verbose) {
|
|
125
|
+
console.log(topic, payload.toString())
|
|
126
|
+
} else {
|
|
127
|
+
console.log(payload.toString())
|
|
128
|
+
}
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
client.on('error', (err) => {
|
|
132
|
+
console.warn(err)
|
|
133
|
+
client.end()
|
|
134
|
+
})
|
|
117
135
|
}
|
|
118
136
|
|
|
119
137
|
module.exports = start
|
|
120
138
|
|
|
121
139
|
if (require.main === module) {
|
|
122
|
-
|
|
140
|
+
start(process.argv.slice(2))
|
|
123
141
|
}
|