mqtt 5.0.0-beta.3 → 5.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 (127) hide show
  1. package/README.md +43 -4
  2. package/build/package.json +162 -0
  3. package/build/src/bin/mqtt.d.ts +2 -0
  4. package/build/src/bin/mqtt.js +27 -0
  5. package/build/src/bin/mqtt.js.map +1 -0
  6. package/build/src/bin/pub.d.ts +2 -0
  7. package/build/src/bin/pub.js +146 -0
  8. package/build/src/bin/pub.js.map +1 -0
  9. package/build/src/bin/sub.d.ts +2 -0
  10. package/build/src/bin/sub.js +121 -0
  11. package/build/src/bin/sub.js.map +1 -0
  12. package/build/src/lib/TypedEmitter.d.ts +11 -0
  13. package/build/src/lib/TypedEmitter.js +13 -0
  14. package/build/src/lib/TypedEmitter.js.map +1 -0
  15. package/build/src/lib/client.d.ts +220 -0
  16. package/build/src/lib/client.js +1204 -0
  17. package/build/src/lib/client.js.map +1 -0
  18. package/build/src/lib/connect/ali.d.ts +3 -0
  19. package/build/src/lib/connect/ali.js +113 -0
  20. package/build/src/lib/connect/ali.js.map +1 -0
  21. package/build/src/lib/connect/index.d.ts +9 -0
  22. package/build/src/lib/connect/index.js +169 -0
  23. package/build/src/lib/connect/index.js.map +1 -0
  24. package/build/src/lib/connect/tcp.d.ts +3 -0
  25. package/build/src/lib/connect/tcp.js +18 -0
  26. package/build/src/lib/connect/tcp.js.map +1 -0
  27. package/build/src/lib/connect/tls.d.ts +3 -0
  28. package/build/src/lib/connect/tls.js +38 -0
  29. package/build/src/lib/connect/tls.js.map +1 -0
  30. package/build/src/lib/connect/ws.d.ts +3 -0
  31. package/build/src/lib/connect/ws.js +212 -0
  32. package/build/src/lib/connect/ws.js.map +1 -0
  33. package/build/src/lib/connect/wx.d.ts +3 -0
  34. package/build/src/lib/connect/wx.js +117 -0
  35. package/build/src/lib/connect/wx.js.map +1 -0
  36. package/build/src/lib/default-message-id-provider.d.ts +16 -0
  37. package/build/src/lib/default-message-id-provider.js +24 -0
  38. package/build/src/lib/default-message-id-provider.js.map +1 -0
  39. package/build/src/lib/handlers/ack.d.ts +48 -0
  40. package/build/src/lib/handlers/ack.js +129 -0
  41. package/build/src/lib/handlers/ack.js.map +1 -0
  42. package/build/src/lib/handlers/auth.d.ts +3 -0
  43. package/build/src/lib/handlers/auth.js +30 -0
  44. package/build/src/lib/handlers/auth.js.map +1 -0
  45. package/build/src/lib/handlers/connack.d.ts +3 -0
  46. package/build/src/lib/handlers/connack.js +48 -0
  47. package/build/src/lib/handlers/connack.js.map +1 -0
  48. package/build/src/lib/handlers/index.d.ts +3 -0
  49. package/build/src/lib/handlers/index.js +64 -0
  50. package/build/src/lib/handlers/index.js.map +1 -0
  51. package/build/src/lib/handlers/publish.d.ts +3 -0
  52. package/build/src/lib/handlers/publish.js +106 -0
  53. package/build/src/lib/handlers/publish.js.map +1 -0
  54. package/build/src/lib/handlers/pubrel.d.ts +3 -0
  55. package/build/src/lib/handlers/pubrel.js +25 -0
  56. package/build/src/lib/handlers/pubrel.js.map +1 -0
  57. package/build/src/lib/is-browser.d.ts +2 -0
  58. package/build/src/lib/is-browser.js +8 -0
  59. package/build/src/lib/is-browser.js.map +1 -0
  60. package/build/src/lib/shared.d.ts +18 -0
  61. package/build/src/lib/shared.js +35 -0
  62. package/build/src/lib/shared.js.map +1 -0
  63. package/build/src/lib/store.d.ts +24 -0
  64. package/build/src/lib/store.js +80 -0
  65. package/build/src/lib/store.js.map +1 -0
  66. package/build/src/lib/topic-alias-recv.d.ts +9 -0
  67. package/build/src/lib/topic-alias-recv.js +24 -0
  68. package/build/src/lib/topic-alias-recv.js.map +1 -0
  69. package/build/src/lib/topic-alias-send.d.ts +13 -0
  70. package/build/src/lib/topic-alias-send.js +56 -0
  71. package/build/src/lib/topic-alias-send.js.map +1 -0
  72. package/build/src/lib/unique-message-id-provider.d.ts +11 -0
  73. package/build/src/lib/unique-message-id-provider.js +26 -0
  74. package/build/src/lib/unique-message-id-provider.js.map +1 -0
  75. package/build/src/lib/validations.d.ts +2 -0
  76. package/build/src/lib/validations.js +32 -0
  77. package/build/src/lib/validations.js.map +1 -0
  78. package/build/src/mqtt.d.ts +10 -0
  79. package/build/src/mqtt.js +49 -0
  80. package/build/src/mqtt.js.map +1 -0
  81. package/build/tsconfig.build.tsbuildinfo +1 -0
  82. package/dist/mqtt.js +2726 -3493
  83. package/dist/mqtt.min.js +4 -4
  84. package/doc/subscribe.txt +1 -1
  85. package/package.json +49 -29
  86. package/{bin/mqtt.js → src/bin/mqtt.ts} +11 -6
  87. package/src/bin/pub.ts +171 -0
  88. package/src/bin/sub.ts +146 -0
  89. package/src/lib/TypedEmitter.ts +45 -0
  90. package/{lib/client.js → src/lib/client.ts} +895 -252
  91. package/{lib/connect/ali.js → src/lib/connect/ali.ts} +13 -11
  92. package/src/lib/connect/index.ts +241 -0
  93. package/{lib/connect/tcp.js → src/lib/connect/tcp.ts} +8 -4
  94. package/{lib/connect/tls.js → src/lib/connect/tls.ts} +10 -8
  95. package/{lib/connect/ws.js → src/lib/connect/ws.ts} +52 -36
  96. package/{lib/connect/wx.js → src/lib/connect/wx.ts} +18 -9
  97. package/src/lib/default-message-id-provider.ts +99 -0
  98. package/{lib/handlers/ack.js → src/lib/handlers/ack.ts} +17 -19
  99. package/src/lib/handlers/auth.ts +44 -0
  100. package/{lib/handlers/connack.js → src/lib/handlers/connack.ts} +15 -11
  101. package/{lib/handlers/index.js → src/lib/handlers/index.ts} +8 -7
  102. package/{lib/handlers/publish.js → src/lib/handlers/publish.ts} +73 -57
  103. package/src/lib/handlers/pubrel.ts +27 -0
  104. package/{lib/is-browser.js → src/lib/is-browser.ts} +5 -4
  105. package/src/lib/shared.ts +73 -0
  106. package/{lib/store.js → src/lib/store.ts} +61 -16
  107. package/{lib/topic-alias-recv.js → src/lib/topic-alias-recv.ts} +10 -6
  108. package/{lib/topic-alias-send.js → src/lib/topic-alias-send.ts} +19 -11
  109. package/{lib/unique-message-id-provider.js → src/lib/unique-message-id-provider.ts} +10 -7
  110. package/{lib/validations.js → src/lib/validations.ts} +2 -6
  111. package/src/mqtt.ts +26 -0
  112. package/bin/pub.js +0 -159
  113. package/bin/sub.js +0 -141
  114. package/lib/connect/index.js +0 -175
  115. package/lib/default-message-id-provider.js +0 -63
  116. package/lib/handlers/auth.js +0 -34
  117. package/lib/handlers/pubrel.js +0 -24
  118. package/mqtt.js +0 -21
  119. package/types/index.d.ts +0 -30
  120. package/types/lib/client-options.d.ts +0 -224
  121. package/types/lib/client.d.ts +0 -273
  122. package/types/lib/connect/index.d.ts +0 -19
  123. package/types/lib/default-message-id-provider.d.ts +0 -49
  124. package/types/lib/message-id-provider.d.ts +0 -40
  125. package/types/lib/store-options.d.ts +0 -6
  126. package/types/lib/store.d.ts +0 -46
  127. package/types/lib/unique-message-id-provider.d.ts +0 -48
@@ -1,21 +1,66 @@
1
1
  /**
2
2
  * Module dependencies
3
3
  */
4
- const { Readable } = require('readable-stream')
4
+ import { Readable } from 'readable-stream'
5
+ import { Packet } from 'mqtt-packet'
6
+ import { DoneCallback } from './shared'
5
7
 
6
8
  const streamsOpts = { objectMode: true }
7
9
  const defaultStoreOptions = {
8
10
  clean: true,
9
11
  }
10
12
 
13
+ export interface IStoreOptions {
14
+ /**
15
+ * true, clear _inflights at close
16
+ */
17
+ clean?: boolean
18
+ }
19
+
20
+ export type PacketCallback = (error?: Error, packet?: Packet) => void
21
+
22
+ export interface IStore {
23
+ /**
24
+ * Adds a packet to the store, a packet is
25
+ * anything that has a messageId property.
26
+ *
27
+ */
28
+ put(packet: Packet, cb: DoneCallback): IStore
29
+
30
+ /**
31
+ * Creates a stream with all the packets in the store
32
+ *
33
+ */
34
+ createStream(): Readable
35
+
36
+ /**
37
+ * deletes a packet from the store.
38
+ */
39
+ del(packet: Pick<Packet, 'messageId'>, cb: PacketCallback): IStore
40
+
41
+ /**
42
+ * get a packet from the store.
43
+ */
44
+ get(packet: Pick<Packet, 'messageId'>, cb: PacketCallback): IStore
45
+
46
+ /**
47
+ * Close the store
48
+ */
49
+ close(cb: DoneCallback): void
50
+ }
51
+
11
52
  /**
12
53
  * In-memory implementation of the message store
13
54
  * This can actually be saved into files.
14
55
  *
15
56
  * @param {Object} [options] - store options
16
57
  */
17
- class Store {
18
- constructor(options) {
58
+ export default class Store implements IStore {
59
+ private options: IStoreOptions
60
+
61
+ private _inflights: Map<number, Packet>
62
+
63
+ constructor(options?: IStoreOptions) {
19
64
  this.options = options || {}
20
65
 
21
66
  // Defaults
@@ -29,7 +74,7 @@ class Store {
29
74
  * anything that has a messageId property.
30
75
  *
31
76
  */
32
- put(packet, cb) {
77
+ put(packet: Packet, cb: DoneCallback) {
33
78
  this._inflights.set(packet.messageId, packet)
34
79
 
35
80
  if (cb) {
@@ -61,7 +106,7 @@ class Store {
61
106
  }
62
107
  }
63
108
 
64
- stream.destroy = () => {
109
+ stream.destroy = (err) => {
65
110
  if (destroyed) {
66
111
  return
67
112
  }
@@ -71,6 +116,8 @@ class Store {
71
116
  setTimeout(() => {
72
117
  stream.emit('close')
73
118
  }, 0)
119
+
120
+ return stream
74
121
  }
75
122
 
76
123
  return stream
@@ -79,11 +126,11 @@ class Store {
79
126
  /**
80
127
  * deletes a packet from the store.
81
128
  */
82
- del(packet, cb) {
83
- packet = this._inflights.get(packet.messageId)
84
- if (packet) {
129
+ del(packet: Pick<Packet, 'messageId'>, cb: PacketCallback) {
130
+ const toDelete = this._inflights.get(packet.messageId)
131
+ if (toDelete) {
85
132
  this._inflights.delete(packet.messageId)
86
- cb(null, packet)
133
+ cb(null, toDelete)
87
134
  } else if (cb) {
88
135
  cb(new Error('missing packet'))
89
136
  }
@@ -94,10 +141,10 @@ class Store {
94
141
  /**
95
142
  * get a packet from the store.
96
143
  */
97
- get(packet, cb) {
98
- packet = this._inflights.get(packet.messageId)
99
- if (packet) {
100
- cb(null, packet)
144
+ get(packet: Pick<Packet, 'messageId'>, cb: PacketCallback) {
145
+ const storedPacket = this._inflights.get(packet.messageId)
146
+ if (storedPacket) {
147
+ cb(null, storedPacket)
101
148
  } else if (cb) {
102
149
  cb(new Error('missing packet'))
103
150
  }
@@ -108,7 +155,7 @@ class Store {
108
155
  /**
109
156
  * Close the store
110
157
  */
111
- close(cb) {
158
+ close(cb: DoneCallback) {
112
159
  if (this.options.clean) {
113
160
  this._inflights = null
114
161
  }
@@ -117,5 +164,3 @@ class Store {
117
164
  }
118
165
  }
119
166
  }
120
-
121
- module.exports = Store
@@ -3,8 +3,14 @@
3
3
  * This holds alias to topic map
4
4
  * @param {Number} [max] - topic alias maximum entries
5
5
  */
6
- class TopicAliasRecv {
7
- constructor(max) {
6
+ export default class TopicAliasRecv {
7
+ private aliasToTopic: Record<number, string>
8
+
9
+ public max: number
10
+
11
+ public length: number
12
+
13
+ constructor(max: number) {
8
14
  this.aliasToTopic = {}
9
15
  this.max = max
10
16
  }
@@ -15,7 +21,7 @@ class TopicAliasRecv {
15
21
  * @param {Number} [alias] - topic alias
16
22
  * @returns {Boolean} - if success return true otherwise false
17
23
  */
18
- put(topic, alias) {
24
+ put(topic: string, alias: number): boolean {
19
25
  if (alias === 0 || alias > this.max) {
20
26
  return false
21
27
  }
@@ -29,7 +35,7 @@ class TopicAliasRecv {
29
35
  * @param {String} [topic] - topic
30
36
  * @returns {Number} - if mapped topic exists return topic alias, otherwise return undefined
31
37
  */
32
- getTopicByAlias(alias) {
38
+ getTopicByAlias(alias: number): string {
33
39
  return this.aliasToTopic[alias]
34
40
  }
35
41
 
@@ -40,5 +46,3 @@ class TopicAliasRecv {
40
46
  this.aliasToTopic = {}
41
47
  }
42
48
  }
43
-
44
- module.exports = TopicAliasRecv
@@ -1,18 +1,28 @@
1
1
  /**
2
2
  * Module dependencies
3
3
  */
4
- const LRUCache = require('lru-cache')
5
- const { NumberAllocator } = require('number-allocator')
4
+ import LRUCache from 'lru-cache'
5
+ import { NumberAllocator } from 'number-allocator'
6
6
 
7
7
  /**
8
8
  * Topic Alias sending manager
9
9
  * This holds both topic to alias and alias to topic map
10
10
  * @param {Number} [max] - topic alias maximum entries
11
11
  */
12
- class TopicAliasSend {
13
- constructor(max) {
12
+ export default class TopicAliasSend {
13
+ private aliasToTopic: LRUCache<number, string>
14
+
15
+ private topicToAlias: Record<string, number>
16
+
17
+ private max: number
18
+
19
+ private numberAllocator: NumberAllocator
20
+
21
+ public length: number
22
+
23
+ constructor(max: number) {
14
24
  if (max > 0) {
15
- this.aliasToTopic = new LRUCache({ max })
25
+ this.aliasToTopic = new LRUCache<number, string>({ max })
16
26
  this.topicToAlias = {}
17
27
  this.numberAllocator = new NumberAllocator(1, max)
18
28
  this.max = max
@@ -26,7 +36,7 @@ class TopicAliasSend {
26
36
  * @param {Number} [alias] - topic alias
27
37
  * @returns {Boolean} - if success return true otherwise false
28
38
  */
29
- put(topic, alias) {
39
+ put(topic: string, alias: number): boolean {
30
40
  if (alias === 0 || alias > this.max) {
31
41
  return false
32
42
  }
@@ -46,7 +56,7 @@ class TopicAliasSend {
46
56
  * @param {Number} [alias] - topic alias
47
57
  * @returns {String} - if mapped topic exists return topic, otherwise return undefined
48
58
  */
49
- getTopicByAlias(alias) {
59
+ getTopicByAlias(alias: number): string {
50
60
  return this.aliasToTopic.get(alias)
51
61
  }
52
62
 
@@ -55,7 +65,7 @@ class TopicAliasSend {
55
65
  * @param {String} [topic] - topic
56
66
  * @returns {Number} - if mapped topic exists return topic alias, otherwise return undefined
57
67
  */
58
- getAliasByTopic(topic) {
68
+ getAliasByTopic(topic: string): number | undefined {
59
69
  const alias = this.topicToAlias[topic]
60
70
  if (typeof alias !== 'undefined') {
61
71
  this.aliasToTopic.get(alias) // LRU update
@@ -77,12 +87,10 @@ class TopicAliasSend {
77
87
  * Get Least Recently Used (LRU) topic alias
78
88
  * @returns {Number} - if vacant alias exists then return it, otherwise then return LRU alias
79
89
  */
80
- getLruAlias() {
90
+ getLruAlias(): number {
81
91
  const alias = this.numberAllocator.firstVacant()
82
92
  if (alias) return alias
83
93
  // get last alias (key) from LRU cache
84
94
  return [...this.aliasToTopic.keys()][this.aliasToTopic.size - 1]
85
95
  }
86
96
  }
87
-
88
- module.exports = TopicAliasSend
@@ -1,10 +1,15 @@
1
- const { NumberAllocator } = require('number-allocator')
1
+ import { NumberAllocator } from 'number-allocator'
2
+ import { IMessageIdProvider } from './default-message-id-provider'
2
3
 
3
4
  /**
4
5
  * UniqueMessageAllocator constructor
5
6
  * @constructor
6
7
  */
7
- class UniqueMessageIdProvider {
8
+ export default class UniqueMessageIdProvider implements IMessageIdProvider {
9
+ private numberAllocator: NumberAllocator
10
+
11
+ private lastId: number
12
+
8
13
  constructor() {
9
14
  this.numberAllocator = new NumberAllocator(1, 65535)
10
15
  }
@@ -36,8 +41,8 @@ class UniqueMessageIdProvider {
36
41
  * @param { unsigned int } - messageId to register,
37
42
  * @return boolean
38
43
  */
39
- register(messageId) {
40
- return this.numberAllocator.use(messageId)
44
+ register(messageId: number) {
45
+ return this.numberAllocator.use(messageId) as boolean
41
46
  }
42
47
 
43
48
  /**
@@ -45,7 +50,7 @@ class UniqueMessageIdProvider {
45
50
  * Deallocate messageId.
46
51
  * @param { unsigned int } - messageId to deallocate,
47
52
  */
48
- deallocate(messageId) {
53
+ deallocate(messageId: number) {
49
54
  this.numberAllocator.free(messageId)
50
55
  }
51
56
 
@@ -57,5 +62,3 @@ class UniqueMessageIdProvider {
57
62
  this.numberAllocator.clear()
58
63
  }
59
64
  }
60
-
61
- module.exports = UniqueMessageIdProvider
@@ -7,7 +7,7 @@
7
7
  * @param {String} topic - A topic
8
8
  * @returns {Boolean} If the topic is valid, returns true. Otherwise, returns false.
9
9
  */
10
- function validateTopic(topic) {
10
+ export function validateTopic(topic: string): boolean {
11
11
  const parts = topic.split('/')
12
12
 
13
13
  for (let i = 0; i < parts.length; i++) {
@@ -33,7 +33,7 @@ function validateTopic(topic) {
33
33
  * @param {Array} topics - Array of topics
34
34
  * @returns {String} If the topics is valid, returns null. Otherwise, returns the invalid one
35
35
  */
36
- function validateTopics(topics) {
36
+ export function validateTopics(topics: string[]): string {
37
37
  if (topics.length === 0) {
38
38
  return 'empty_topic_list'
39
39
  }
@@ -44,7 +44,3 @@ function validateTopics(topics) {
44
44
  }
45
45
  return null
46
46
  }
47
-
48
- module.exports = {
49
- validateTopics,
50
- }
package/src/mqtt.ts ADDED
@@ -0,0 +1,26 @@
1
+ /*
2
+ * Copyright (c) 2015-2015 MQTT.js contributors.
3
+ * Copyright (c) 2011-2014 Adam Rudd.
4
+ *
5
+ * See LICENSE for more information
6
+ */
7
+
8
+ import MqttClient from './lib/client'
9
+ import DefaultMessageIdProvider from './lib/default-message-id-provider'
10
+ import UniqueMessageIdProvider from './lib/unique-message-id-provider'
11
+ import Store, { IStore } from './lib/store'
12
+ import connect, { connectAsync } from './lib/connect'
13
+
14
+ export const Client = MqttClient
15
+ export {
16
+ connect,
17
+ connectAsync,
18
+ MqttClient,
19
+ Store,
20
+ DefaultMessageIdProvider,
21
+ UniqueMessageIdProvider,
22
+ IStore,
23
+ }
24
+ export * from './lib/client'
25
+ export * from './lib/shared'
26
+ export { ReasonCodes } from './lib/handlers/ack'
package/bin/pub.js DELETED
@@ -1,159 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- const mqtt = require('..')
4
- const { pipeline, Writable } = require('readable-stream')
5
- const path = require('path')
6
- const fs = require('fs')
7
- const concat = require('concat-stream')
8
- const helpMe = require('help-me')({
9
- dir: path.join(__dirname, '..', 'doc'),
10
- })
11
- const minimist = require('minimist')
12
- const split2 = require('split2')
13
-
14
- function send(args) {
15
- const client = mqtt.connect(args)
16
- client.on('connect', () => {
17
- client.publish(args.topic, args.message, args, (err) => {
18
- if (err) {
19
- console.warn(err)
20
- }
21
- client.end()
22
- })
23
- })
24
- client.on('error', (err) => {
25
- console.warn(err)
26
- client.end()
27
- })
28
- }
29
-
30
- function multisend(args) {
31
- const client = mqtt.connect(args)
32
- const sender = new Writable({
33
- objectMode: true,
34
- })
35
- sender._write = (line, enc, cb) => {
36
- client.publish(args.topic, line.trim(), args, cb)
37
- }
38
-
39
- client.on('connect', () => {
40
- pipeline(process.stdin, split2(), sender, (err) => {
41
- client.end()
42
- if (err) {
43
- throw err
44
- }
45
- })
46
- })
47
- }
48
-
49
- function start(args) {
50
- args = minimist(args, {
51
- string: [
52
- 'hostname',
53
- 'username',
54
- 'password',
55
- 'key',
56
- 'cert',
57
- 'ca',
58
- 'message',
59
- 'clientId',
60
- 'i',
61
- 'id',
62
- ],
63
- boolean: ['stdin', 'retain', 'help', 'insecure', 'multiline'],
64
- alias: {
65
- port: 'p',
66
- hostname: ['h', 'host'],
67
- topic: 't',
68
- message: 'm',
69
- qos: 'q',
70
- clientId: ['i', 'id'],
71
- retain: 'r',
72
- username: 'u',
73
- password: 'P',
74
- stdin: 's',
75
- multiline: 'M',
76
- protocol: ['C', 'l'],
77
- help: 'H',
78
- ca: 'cafile',
79
- },
80
- default: {
81
- host: 'localhost',
82
- qos: 0,
83
- retain: false,
84
- topic: '',
85
- message: '',
86
- },
87
- })
88
-
89
- if (args.help) {
90
- return helpMe.toStdout('publish')
91
- }
92
-
93
- if (args.key) {
94
- args.key = fs.readFileSync(args.key)
95
- }
96
-
97
- if (args.cert) {
98
- args.cert = fs.readFileSync(args.cert)
99
- }
100
-
101
- if (args.ca) {
102
- args.ca = fs.readFileSync(args.ca)
103
- }
104
-
105
- if (args.key && args.cert && !args.protocol) {
106
- args.protocol = 'mqtts'
107
- }
108
-
109
- if (args.port) {
110
- if (typeof args.port !== 'number') {
111
- console.warn(
112
- "# Port: number expected, '%s' was given.",
113
- typeof args.port,
114
- )
115
- return
116
- }
117
- }
118
-
119
- if (args['will-topic']) {
120
- args.will = {}
121
- args.will.topic = args['will-topic']
122
- args.will.payload = args['will-message']
123
- args.will.qos = args['will-qos']
124
- args.will.retain = args['will-retain']
125
- }
126
-
127
- if (args.insecure) {
128
- args.rejectUnauthorized = false
129
- }
130
-
131
- args.topic = (args.topic || args._.shift()).toString()
132
- args.message = (args.message || args._.shift()).toString()
133
-
134
- if (!args.topic) {
135
- console.error('missing topic\n')
136
- return helpMe.toStdout('publish')
137
- }
138
-
139
- if (args.stdin) {
140
- if (args.multiline) {
141
- multisend(args)
142
- } else {
143
- process.stdin.pipe(
144
- concat((data) => {
145
- args.message = data
146
- send(args)
147
- }),
148
- )
149
- }
150
- } else {
151
- send(args)
152
- }
153
- }
154
-
155
- module.exports = start
156
-
157
- if (require.main === module) {
158
- start(process.argv.slice(2))
159
- }
package/bin/sub.js DELETED
@@ -1,141 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- const mqtt = require('..')
4
- const path = require('path')
5
- const fs = require('fs')
6
- const helpMe = require('help-me')({
7
- dir: path.join(__dirname, '..', 'doc'),
8
- })
9
- const minimist = require('minimist')
10
-
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
- })
135
- }
136
-
137
- module.exports = start
138
-
139
- if (require.main === module) {
140
- start(process.argv.slice(2))
141
- }