mqtt 5.0.0-beta.2 → 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/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
- dir: path.join(__dirname, '..', 'doc')
7
+ dir: path.join(__dirname, '..', 'doc'),
8
8
  })
9
9
  const minimist = require('minimist')
10
10
 
11
- function start (args) {
12
- args = minimist(args, {
13
- string: ['hostname', 'username', 'password', 'key', 'cert', 'ca', 'clientId', 'i', 'id'],
14
- boolean: ['stdin', 'help', 'clean', 'insecure'],
15
- alias: {
16
- port: 'p',
17
- hostname: ['h', 'host'],
18
- topic: 't',
19
- qos: 'q',
20
- clean: 'c',
21
- keepalive: 'k',
22
- clientId: ['i', 'id'],
23
- username: 'u',
24
- password: 'P',
25
- protocol: ['C', 'l'],
26
- verbose: 'v',
27
- help: '-H',
28
- ca: 'cafile'
29
- },
30
- default: {
31
- host: 'localhost',
32
- qos: 0,
33
- retain: false,
34
- clean: true,
35
- keepAlive: 30 // 30 sec
36
- }
37
- })
38
-
39
- if (args.help) {
40
- return helpMe.toStdout('subscribe')
41
- }
42
-
43
- args.topic = args.topic || args._.shift()
44
-
45
- if (!args.topic) {
46
- console.error('missing topic\n')
47
- return helpMe.toStdout('subscribe')
48
- }
49
-
50
- if (args.key) {
51
- args.key = fs.readFileSync(args.key)
52
- }
53
-
54
- if (args.cert) {
55
- args.cert = fs.readFileSync(args.cert)
56
- }
57
-
58
- if (args.ca) {
59
- args.ca = fs.readFileSync(args.ca)
60
- }
61
-
62
- if (args.key && args.cert && !args.protocol) {
63
- args.protocol = 'mqtts'
64
- }
65
-
66
- if (args.insecure) {
67
- args.rejectUnauthorized = false
68
- }
69
-
70
- if (args.port) {
71
- if (typeof args.port !== 'number') {
72
- console.warn('# Port: number expected, \'%s\' was given.', typeof args.port)
73
- return
74
- }
75
- }
76
-
77
- if (args['will-topic']) {
78
- args.will = {}
79
- args.will.topic = args['will-topic']
80
- args.will.payload = args['will-message']
81
- args.will.qos = args['will-qos']
82
- args.will.retain = args['will-retain']
83
- }
84
-
85
- args.keepAlive = args['keep-alive']
86
-
87
- const client = mqtt.connect(args)
88
-
89
- client.on('connect', function () {
90
- client.subscribe(args.topic, { qos: args.qos }, function (err, result) {
91
- if (err) {
92
- console.error(err)
93
- process.exit(1)
94
- }
95
-
96
- result.forEach(function (sub) {
97
- if (sub.qos > 2) {
98
- console.error('subscription negated to', sub.topic, 'with code', sub.qos)
99
- process.exit(1)
100
- }
101
- })
102
- })
103
- })
104
-
105
- client.on('message', function (topic, payload) {
106
- if (args.verbose) {
107
- console.log(topic, payload.toString())
108
- } else {
109
- console.log(payload.toString())
110
- }
111
- })
112
-
113
- client.on('error', function (err) {
114
- console.warn(err)
115
- client.end()
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
- start(process.argv.slice(2))
140
+ start(process.argv.slice(2))
123
141
  }