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.
Files changed (67) hide show
  1. package/CONTRIBUTING.md +27 -27
  2. package/LICENSE.md +15 -15
  3. package/README.md +689 -612
  4. package/bin/pub.js +146 -146
  5. package/bin/sub.js +123 -123
  6. package/dist/mqtt.js +11916 -14116
  7. package/dist/mqtt.min.js +1 -1
  8. package/doc/help.txt +8 -8
  9. package/doc/publish.txt +26 -26
  10. package/doc/subscribe.txt +26 -26
  11. package/examples/client/secure-client.js +24 -24
  12. package/examples/client/simple-both.js +13 -13
  13. package/examples/client/simple-publish.js +7 -7
  14. package/examples/client/simple-subscribe.js +9 -9
  15. package/examples/tls client/crt.ca.cg.pem +34 -34
  16. package/examples/tls client/mqttclient.js +48 -48
  17. package/examples/tls client/tls-cert.pem +13 -13
  18. package/examples/tls client/tls-key.pem +15 -15
  19. package/examples/wss/client.js +49 -49
  20. package/examples/wss/client_with_proxy.js +58 -58
  21. package/lib/client.js +1460 -1129
  22. package/lib/connect/ali.js +130 -0
  23. package/lib/connect/index.js +157 -149
  24. package/lib/connect/tcp.js +19 -19
  25. package/lib/connect/tls.js +41 -41
  26. package/lib/connect/ws.js +92 -92
  27. package/lib/connect/wx.js +134 -110
  28. package/lib/store.js +140 -140
  29. package/lib/validations.js +52 -52
  30. package/mqtt.js +41 -41
  31. package/package.json +112 -107
  32. package/test/abstract_client.js +3084 -2667
  33. package/test/abstract_store.js +135 -135
  34. package/test/browser/server.js +132 -132
  35. package/test/browser/test.js +92 -92
  36. package/test/client.js +1129 -544
  37. package/test/helpers/private-csr.pem +16 -16
  38. package/test/helpers/private-key.pem +27 -27
  39. package/test/helpers/public-cert.pem +19 -19
  40. package/test/helpers/public-key.pem +9 -9
  41. package/test/helpers/server.js +52 -52
  42. package/test/helpers/server_process.js +9 -9
  43. package/test/helpers/tls-cert.pem +14 -14
  44. package/test/helpers/tls-csr.pem +11 -11
  45. package/test/helpers/tls-key.pem +15 -15
  46. package/test/helpers/wrong-cert.pem +13 -13
  47. package/test/helpers/wrong-csr.pem +11 -11
  48. package/test/helpers/wrong-key.pem +15 -15
  49. package/test/mocha.opts +4 -4
  50. package/test/mqtt.js +230 -230
  51. package/test/mqtt_store.js +9 -9
  52. package/test/secure_client.js +157 -157
  53. package/test/server.js +93 -65
  54. package/test/store.js +10 -10
  55. package/test/typescript/broker-connect-subscribe-and-publish.js +24 -0
  56. package/test/typescript/broker-connect-subscribe-and-publish.js.map +1 -0
  57. package/test/typescript/broker-connect-subscribe-and-publish.ts +22 -22
  58. package/test/typescript/tsconfig.json +14 -14
  59. package/test/util.js +13 -13
  60. package/test/websocket_client.js +144 -144
  61. package/types/index.d.ts +27 -27
  62. package/types/lib/client-options.d.ts +178 -137
  63. package/types/lib/client.d.ts +231 -196
  64. package/types/lib/connect/index.d.ts +10 -10
  65. package/types/lib/store-options.d.ts +6 -6
  66. package/types/lib/store.d.ts +46 -46
  67. package/test/browser/wx.js +0 -93
package/bin/pub.js CHANGED
@@ -1,146 +1,146 @@
1
- #!/usr/bin/env node
2
-
3
- 'use strict'
4
-
5
- var mqtt = require('../')
6
- var pump = require('pump')
7
- var path = require('path')
8
- var fs = require('fs')
9
- var concat = require('concat-stream')
10
- var Writable = require('readable-stream').Writable
11
- var helpMe = require('help-me')({
12
- dir: path.join(__dirname, '..', 'doc')
13
- })
14
- var minimist = require('minimist')
15
- var split2 = require('split2')
16
-
17
- function send (args) {
18
- var client = mqtt.connect(args)
19
- client.on('connect', function () {
20
- client.publish(args.topic, args.message, args, function (err) {
21
- if (err) {
22
- console.warn(err)
23
- }
24
- client.end()
25
- })
26
- })
27
- client.on('error', function (err) {
28
- console.warn(err)
29
- client.end()
30
- })
31
- }
32
-
33
- function multisend (args) {
34
- var client = mqtt.connect(args)
35
- var sender = new Writable({
36
- objectMode: true
37
- })
38
- sender._write = function (line, enc, cb) {
39
- client.publish(args.topic, line.trim(), args, cb)
40
- }
41
-
42
- client.on('connect', function () {
43
- pump(process.stdin, split2(), sender, function (err) {
44
- client.end()
45
- if (err) {
46
- throw err
47
- }
48
- })
49
- })
50
- }
51
-
52
- function start (args) {
53
- args = minimist(args, {
54
- string: ['hostname', 'username', 'password', 'key', 'cert', 'ca', 'message', 'clientId', 'i', 'id'],
55
- boolean: ['stdin', 'retain', 'help', 'insecure', 'multiline'],
56
- alias: {
57
- port: 'p',
58
- hostname: ['h', 'host'],
59
- topic: 't',
60
- message: 'm',
61
- qos: 'q',
62
- clientId: ['i', 'id'],
63
- retain: 'r',
64
- username: 'u',
65
- password: 'P',
66
- stdin: 's',
67
- multiline: 'M',
68
- protocol: ['C', 'l'],
69
- help: 'H',
70
- ca: 'cafile'
71
- },
72
- default: {
73
- host: 'localhost',
74
- qos: 0,
75
- retain: false,
76
- topic: '',
77
- message: ''
78
- }
79
- })
80
-
81
- if (args.help) {
82
- return helpMe.toStdout('publish')
83
- }
84
-
85
- if (args.key) {
86
- args.key = fs.readFileSync(args.key)
87
- }
88
-
89
- if (args.cert) {
90
- args.cert = fs.readFileSync(args.cert)
91
- }
92
-
93
- if (args.ca) {
94
- args.ca = fs.readFileSync(args.ca)
95
- }
96
-
97
- if (args.key && args.cert && !args.protocol) {
98
- args.protocol = 'mqtts'
99
- }
100
-
101
- if (args.port) {
102
- if (typeof args.port !== 'number') {
103
- console.warn('# Port: number expected, \'%s\' was given.', typeof args.port)
104
- return
105
- }
106
- }
107
-
108
- if (args['will-topic']) {
109
- args.will = {}
110
- args.will.topic = args['will-topic']
111
- args.will.payload = args['will-message']
112
- args.will.qos = args['will-qos']
113
- args.will.retain = args['will-retain']
114
- }
115
-
116
- if (args.insecure) {
117
- args.rejectUnauthorized = false
118
- }
119
-
120
- args.topic = (args.topic || args._.shift()).toString()
121
- args.message = (args.message || args._.shift()).toString()
122
-
123
- if (!args.topic) {
124
- console.error('missing topic\n')
125
- return helpMe.toStdout('publish')
126
- }
127
-
128
- if (args.stdin) {
129
- if (args.multiline) {
130
- multisend(args)
131
- } else {
132
- process.stdin.pipe(concat(function (data) {
133
- args.message = data
134
- send(args)
135
- }))
136
- }
137
- } else {
138
- send(args)
139
- }
140
- }
141
-
142
- module.exports = start
143
-
144
- if (require.main === module) {
145
- start(process.argv.slice(2))
146
- }
1
+ #!/usr/bin/env node
2
+
3
+ 'use strict'
4
+
5
+ var mqtt = require('../')
6
+ var pump = require('pump')
7
+ var path = require('path')
8
+ var fs = require('fs')
9
+ var concat = require('concat-stream')
10
+ var Writable = require('readable-stream').Writable
11
+ var helpMe = require('help-me')({
12
+ dir: path.join(__dirname, '..', 'doc')
13
+ })
14
+ var minimist = require('minimist')
15
+ var split2 = require('split2')
16
+
17
+ function send (args) {
18
+ var client = mqtt.connect(args)
19
+ client.on('connect', function () {
20
+ client.publish(args.topic, args.message, args, function (err) {
21
+ if (err) {
22
+ console.warn(err)
23
+ }
24
+ client.end()
25
+ })
26
+ })
27
+ client.on('error', function (err) {
28
+ console.warn(err)
29
+ client.end()
30
+ })
31
+ }
32
+
33
+ function multisend (args) {
34
+ var client = mqtt.connect(args)
35
+ var sender = new Writable({
36
+ objectMode: true
37
+ })
38
+ sender._write = function (line, enc, cb) {
39
+ client.publish(args.topic, line.trim(), args, cb)
40
+ }
41
+
42
+ client.on('connect', function () {
43
+ pump(process.stdin, split2(), sender, function (err) {
44
+ client.end()
45
+ if (err) {
46
+ throw err
47
+ }
48
+ })
49
+ })
50
+ }
51
+
52
+ function start (args) {
53
+ args = minimist(args, {
54
+ string: ['hostname', 'username', 'password', 'key', 'cert', 'ca', 'message', 'clientId', 'i', 'id'],
55
+ boolean: ['stdin', 'retain', 'help', 'insecure', 'multiline'],
56
+ alias: {
57
+ port: 'p',
58
+ hostname: ['h', 'host'],
59
+ topic: 't',
60
+ message: 'm',
61
+ qos: 'q',
62
+ clientId: ['i', 'id'],
63
+ retain: 'r',
64
+ username: 'u',
65
+ password: 'P',
66
+ stdin: 's',
67
+ multiline: 'M',
68
+ protocol: ['C', 'l'],
69
+ help: 'H',
70
+ ca: 'cafile'
71
+ },
72
+ default: {
73
+ host: 'localhost',
74
+ qos: 0,
75
+ retain: false,
76
+ topic: '',
77
+ message: ''
78
+ }
79
+ })
80
+
81
+ if (args.help) {
82
+ return helpMe.toStdout('publish')
83
+ }
84
+
85
+ if (args.key) {
86
+ args.key = fs.readFileSync(args.key)
87
+ }
88
+
89
+ if (args.cert) {
90
+ args.cert = fs.readFileSync(args.cert)
91
+ }
92
+
93
+ if (args.ca) {
94
+ args.ca = fs.readFileSync(args.ca)
95
+ }
96
+
97
+ if (args.key && args.cert && !args.protocol) {
98
+ args.protocol = 'mqtts'
99
+ }
100
+
101
+ if (args.port) {
102
+ if (typeof args.port !== 'number') {
103
+ console.warn('# Port: number expected, \'%s\' was given.', typeof args.port)
104
+ return
105
+ }
106
+ }
107
+
108
+ if (args['will-topic']) {
109
+ args.will = {}
110
+ args.will.topic = args['will-topic']
111
+ args.will.payload = args['will-message']
112
+ args.will.qos = args['will-qos']
113
+ args.will.retain = args['will-retain']
114
+ }
115
+
116
+ if (args.insecure) {
117
+ args.rejectUnauthorized = false
118
+ }
119
+
120
+ args.topic = (args.topic || args._.shift()).toString()
121
+ args.message = (args.message || args._.shift()).toString()
122
+
123
+ if (!args.topic) {
124
+ console.error('missing topic\n')
125
+ return helpMe.toStdout('publish')
126
+ }
127
+
128
+ if (args.stdin) {
129
+ if (args.multiline) {
130
+ multisend(args)
131
+ } else {
132
+ process.stdin.pipe(concat(function (data) {
133
+ args.message = data
134
+ send(args)
135
+ }))
136
+ }
137
+ } else {
138
+ send(args)
139
+ }
140
+ }
141
+
142
+ module.exports = start
143
+
144
+ if (require.main === module) {
145
+ start(process.argv.slice(2))
146
+ }
package/bin/sub.js CHANGED
@@ -1,123 +1,123 @@
1
- #!/usr/bin/env node
2
-
3
- var mqtt = require('../')
4
- var path = require('path')
5
- var fs = require('fs')
6
- var helpMe = require('help-me')({
7
- dir: path.join(__dirname, '..', 'doc')
8
- })
9
- var minimist = require('minimist')
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
- var 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
- })
117
- }
118
-
119
- module.exports = start
120
-
121
- if (require.main === module) {
122
- start(process.argv.slice(2))
123
- }
1
+ #!/usr/bin/env node
2
+
3
+ var mqtt = require('../')
4
+ var path = require('path')
5
+ var fs = require('fs')
6
+ var helpMe = require('help-me')({
7
+ dir: path.join(__dirname, '..', 'doc')
8
+ })
9
+ var minimist = require('minimist')
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
+ var 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
+ })
117
+ }
118
+
119
+ module.exports = start
120
+
121
+ if (require.main === module) {
122
+ start(process.argv.slice(2))
123
+ }