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.
- package/README.md +43 -4
- package/build/package.json +162 -0
- package/build/src/bin/mqtt.d.ts +2 -0
- package/build/src/bin/mqtt.js +27 -0
- package/build/src/bin/mqtt.js.map +1 -0
- package/build/src/bin/pub.d.ts +2 -0
- package/build/src/bin/pub.js +146 -0
- package/build/src/bin/pub.js.map +1 -0
- package/build/src/bin/sub.d.ts +2 -0
- package/build/src/bin/sub.js +121 -0
- package/build/src/bin/sub.js.map +1 -0
- package/build/src/lib/TypedEmitter.d.ts +11 -0
- package/build/src/lib/TypedEmitter.js +13 -0
- package/build/src/lib/TypedEmitter.js.map +1 -0
- package/build/src/lib/client.d.ts +220 -0
- package/build/src/lib/client.js +1204 -0
- package/build/src/lib/client.js.map +1 -0
- package/build/src/lib/connect/ali.d.ts +3 -0
- package/build/src/lib/connect/ali.js +113 -0
- package/build/src/lib/connect/ali.js.map +1 -0
- package/build/src/lib/connect/index.d.ts +9 -0
- package/build/src/lib/connect/index.js +169 -0
- package/build/src/lib/connect/index.js.map +1 -0
- package/build/src/lib/connect/tcp.d.ts +3 -0
- package/build/src/lib/connect/tcp.js +18 -0
- package/build/src/lib/connect/tcp.js.map +1 -0
- package/build/src/lib/connect/tls.d.ts +3 -0
- package/build/src/lib/connect/tls.js +38 -0
- package/build/src/lib/connect/tls.js.map +1 -0
- package/build/src/lib/connect/ws.d.ts +3 -0
- package/build/src/lib/connect/ws.js +212 -0
- package/build/src/lib/connect/ws.js.map +1 -0
- package/build/src/lib/connect/wx.d.ts +3 -0
- package/build/src/lib/connect/wx.js +117 -0
- package/build/src/lib/connect/wx.js.map +1 -0
- package/build/src/lib/default-message-id-provider.d.ts +16 -0
- package/build/src/lib/default-message-id-provider.js +24 -0
- package/build/src/lib/default-message-id-provider.js.map +1 -0
- package/build/src/lib/handlers/ack.d.ts +48 -0
- package/build/src/lib/handlers/ack.js +129 -0
- package/build/src/lib/handlers/ack.js.map +1 -0
- package/build/src/lib/handlers/auth.d.ts +3 -0
- package/build/src/lib/handlers/auth.js +30 -0
- package/build/src/lib/handlers/auth.js.map +1 -0
- package/build/src/lib/handlers/connack.d.ts +3 -0
- package/build/src/lib/handlers/connack.js +48 -0
- package/build/src/lib/handlers/connack.js.map +1 -0
- package/build/src/lib/handlers/index.d.ts +3 -0
- package/build/src/lib/handlers/index.js +64 -0
- package/build/src/lib/handlers/index.js.map +1 -0
- package/build/src/lib/handlers/publish.d.ts +3 -0
- package/build/src/lib/handlers/publish.js +106 -0
- package/build/src/lib/handlers/publish.js.map +1 -0
- package/build/src/lib/handlers/pubrel.d.ts +3 -0
- package/build/src/lib/handlers/pubrel.js +25 -0
- package/build/src/lib/handlers/pubrel.js.map +1 -0
- package/build/src/lib/is-browser.d.ts +2 -0
- package/build/src/lib/is-browser.js +8 -0
- package/build/src/lib/is-browser.js.map +1 -0
- package/build/src/lib/shared.d.ts +18 -0
- package/build/src/lib/shared.js +35 -0
- package/build/src/lib/shared.js.map +1 -0
- package/build/src/lib/store.d.ts +24 -0
- package/build/src/lib/store.js +80 -0
- package/build/src/lib/store.js.map +1 -0
- package/build/src/lib/topic-alias-recv.d.ts +9 -0
- package/build/src/lib/topic-alias-recv.js +24 -0
- package/build/src/lib/topic-alias-recv.js.map +1 -0
- package/build/src/lib/topic-alias-send.d.ts +13 -0
- package/build/src/lib/topic-alias-send.js +56 -0
- package/build/src/lib/topic-alias-send.js.map +1 -0
- package/build/src/lib/unique-message-id-provider.d.ts +11 -0
- package/build/src/lib/unique-message-id-provider.js +26 -0
- package/build/src/lib/unique-message-id-provider.js.map +1 -0
- package/build/src/lib/validations.d.ts +2 -0
- package/build/src/lib/validations.js +32 -0
- package/build/src/lib/validations.js.map +1 -0
- package/build/src/mqtt.d.ts +10 -0
- package/build/src/mqtt.js +49 -0
- package/build/src/mqtt.js.map +1 -0
- package/build/tsconfig.build.tsbuildinfo +1 -0
- package/dist/mqtt.js +2726 -3493
- package/dist/mqtt.min.js +4 -4
- package/doc/subscribe.txt +1 -1
- package/package.json +49 -29
- package/{bin/mqtt.js → src/bin/mqtt.ts} +11 -6
- package/src/bin/pub.ts +171 -0
- package/src/bin/sub.ts +146 -0
- package/src/lib/TypedEmitter.ts +45 -0
- package/{lib/client.js → src/lib/client.ts} +895 -252
- package/{lib/connect/ali.js → src/lib/connect/ali.ts} +13 -11
- package/src/lib/connect/index.ts +241 -0
- package/{lib/connect/tcp.js → src/lib/connect/tcp.ts} +8 -4
- package/{lib/connect/tls.js → src/lib/connect/tls.ts} +10 -8
- package/{lib/connect/ws.js → src/lib/connect/ws.ts} +52 -36
- package/{lib/connect/wx.js → src/lib/connect/wx.ts} +18 -9
- package/src/lib/default-message-id-provider.ts +99 -0
- package/{lib/handlers/ack.js → src/lib/handlers/ack.ts} +17 -19
- package/src/lib/handlers/auth.ts +44 -0
- package/{lib/handlers/connack.js → src/lib/handlers/connack.ts} +15 -11
- package/{lib/handlers/index.js → src/lib/handlers/index.ts} +8 -7
- package/{lib/handlers/publish.js → src/lib/handlers/publish.ts} +73 -57
- package/src/lib/handlers/pubrel.ts +27 -0
- package/{lib/is-browser.js → src/lib/is-browser.ts} +5 -4
- package/src/lib/shared.ts +73 -0
- package/{lib/store.js → src/lib/store.ts} +61 -16
- package/{lib/topic-alias-recv.js → src/lib/topic-alias-recv.ts} +10 -6
- package/{lib/topic-alias-send.js → src/lib/topic-alias-send.ts} +19 -11
- package/{lib/unique-message-id-provider.js → src/lib/unique-message-id-provider.ts} +10 -7
- package/{lib/validations.js → src/lib/validations.ts} +2 -6
- package/src/mqtt.ts +26 -0
- package/bin/pub.js +0 -159
- package/bin/sub.js +0 -141
- package/lib/connect/index.js +0 -175
- package/lib/default-message-id-provider.js +0 -63
- package/lib/handlers/auth.js +0 -34
- package/lib/handlers/pubrel.js +0 -24
- package/mqtt.js +0 -21
- package/types/index.d.ts +0 -30
- package/types/lib/client-options.d.ts +0 -224
- package/types/lib/client.d.ts +0 -273
- package/types/lib/connect/index.d.ts +0 -19
- package/types/lib/default-message-id-provider.d.ts +0 -49
- package/types/lib/message-id-provider.d.ts +0 -40
- package/types/lib/store-options.d.ts +0 -6
- package/types/lib/store.d.ts +0 -46
- package/types/lib/unique-message-id-provider.d.ts +0 -48
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
export interface IMessageIdProvider {
|
|
2
|
+
/**
|
|
3
|
+
* Allocate the first vacant messageId. The messageId become occupied status.
|
|
4
|
+
* @return {Number} - The first vacant messageId. If all messageIds are occupied, return null.
|
|
5
|
+
*/
|
|
6
|
+
allocate(): number | null
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Get the last allocated messageId.
|
|
10
|
+
* @return {Number} - messageId.
|
|
11
|
+
*/
|
|
12
|
+
getLastAllocated(): number | null
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Register the messageId. The messageId become occupied status.
|
|
16
|
+
* If the messageId has already been occupied, then return false.
|
|
17
|
+
* @param {number} num - The messageId to request use.
|
|
18
|
+
* @return {boolean} - If `num` was not occupied, then return true, otherwise return false.
|
|
19
|
+
*/
|
|
20
|
+
register(num: number): boolean
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Deallocate the messageId. The messageId become vacant status.
|
|
24
|
+
* @param {Number} num - The messageId to deallocate. The messageId must be occupied status.
|
|
25
|
+
* In other words, the messageId must be allocated by allocate() or
|
|
26
|
+
* occupied by register().
|
|
27
|
+
*/
|
|
28
|
+
deallocate(num: number): void
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Clear all occupied messageIds.
|
|
32
|
+
* The all messageIds are set to vacant status.
|
|
33
|
+
*/
|
|
34
|
+
clear(): void
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* DefaultMessageAllocator constructor
|
|
39
|
+
* @constructor
|
|
40
|
+
*/
|
|
41
|
+
export default class DefaultMessageIdProvider implements IMessageIdProvider {
|
|
42
|
+
private nextId: number
|
|
43
|
+
|
|
44
|
+
constructor() {
|
|
45
|
+
/**
|
|
46
|
+
* MessageIDs starting with 1
|
|
47
|
+
* ensure that nextId is min. 1, see https://github.com/mqttjs/MQTT.js/issues/810
|
|
48
|
+
*/
|
|
49
|
+
this.nextId = Math.max(1, Math.floor(Math.random() * 65535))
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* allocate
|
|
54
|
+
*
|
|
55
|
+
* Get the next messageId.
|
|
56
|
+
* @return unsigned int
|
|
57
|
+
*/
|
|
58
|
+
allocate() {
|
|
59
|
+
// id becomes current state of this.nextId and increments afterwards
|
|
60
|
+
const id = this.nextId++
|
|
61
|
+
// Ensure 16 bit unsigned int (max 65535, nextId got one higher)
|
|
62
|
+
if (this.nextId === 65536) {
|
|
63
|
+
this.nextId = 1
|
|
64
|
+
}
|
|
65
|
+
return id
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* getLastAllocated
|
|
70
|
+
* Get the last allocated messageId.
|
|
71
|
+
* @return unsigned int
|
|
72
|
+
*/
|
|
73
|
+
getLastAllocated() {
|
|
74
|
+
return this.nextId === 1 ? 65535 : this.nextId - 1
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* register
|
|
79
|
+
* Register messageId. If success return true, otherwise return false.
|
|
80
|
+
* @param { unsigned int } - messageId to register,
|
|
81
|
+
* @return boolean
|
|
82
|
+
*/
|
|
83
|
+
register(messageId: number) {
|
|
84
|
+
return true
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* deallocate
|
|
89
|
+
* Deallocate messageId.
|
|
90
|
+
* @param { unsigned int } - messageId to deallocate,
|
|
91
|
+
*/
|
|
92
|
+
deallocate(messageId: number) {}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* clear
|
|
96
|
+
* Deallocate all messageIds.
|
|
97
|
+
*/
|
|
98
|
+
clear() {}
|
|
99
|
+
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
// Other Socket Errors: EADDRINUSE, ECONNRESET, ENOTFOUND, ETIMEDOUT.
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
import { PacketHandler } from '../shared'
|
|
4
|
+
|
|
5
|
+
export const ReasonCodes = {
|
|
4
6
|
0: '',
|
|
5
7
|
1: 'Unacceptable protocol version',
|
|
6
8
|
2: 'Identifier rejected',
|
|
@@ -46,7 +48,7 @@ const errors = {
|
|
|
46
48
|
162: 'Wildcard Subscriptions not supported',
|
|
47
49
|
}
|
|
48
50
|
|
|
49
|
-
|
|
51
|
+
const handleAck: PacketHandler = (client, packet) => {
|
|
50
52
|
/* eslint no-fallthrough: "off" */
|
|
51
53
|
const { messageId } = packet
|
|
52
54
|
const type = packet.cmd
|
|
@@ -80,13 +82,13 @@ function handleAck(client, packet) {
|
|
|
80
82
|
const pubackRC = packet.reasonCode
|
|
81
83
|
// Callback - we're done
|
|
82
84
|
if (pubackRC && pubackRC > 0 && pubackRC !== 16) {
|
|
83
|
-
err = new Error(`Publish error: ${
|
|
85
|
+
err = new Error(`Publish error: ${ReasonCodes[pubackRC]}`)
|
|
84
86
|
err.code = pubackRC
|
|
85
|
-
client
|
|
87
|
+
client['_removeOutgoingAndStoreMessage'](messageId, () => {
|
|
86
88
|
cb(err, packet)
|
|
87
89
|
})
|
|
88
90
|
} else {
|
|
89
|
-
client
|
|
91
|
+
client['_removeOutgoingAndStoreMessage'](messageId, cb)
|
|
90
92
|
}
|
|
91
93
|
|
|
92
94
|
break
|
|
@@ -100,43 +102,40 @@ function handleAck(client, packet) {
|
|
|
100
102
|
const pubrecRC = packet.reasonCode
|
|
101
103
|
|
|
102
104
|
if (pubrecRC && pubrecRC > 0 && pubrecRC !== 16) {
|
|
103
|
-
err = new Error(`Publish error: ${
|
|
105
|
+
err = new Error(`Publish error: ${ReasonCodes[pubrecRC]}`)
|
|
104
106
|
err.code = pubrecRC
|
|
105
|
-
client
|
|
107
|
+
client['_removeOutgoingAndStoreMessage'](messageId, () => {
|
|
106
108
|
cb(err, packet)
|
|
107
109
|
})
|
|
108
110
|
} else {
|
|
109
|
-
client
|
|
111
|
+
client['_sendPacket'](response)
|
|
110
112
|
}
|
|
111
113
|
break
|
|
112
114
|
}
|
|
113
115
|
case 'suback': {
|
|
114
116
|
delete client.outgoing[messageId]
|
|
115
117
|
client.messageIdProvider.deallocate(messageId)
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
grantedI
|
|
119
|
-
grantedI++
|
|
120
|
-
) {
|
|
121
|
-
if ((packet.granted[grantedI] & 0x80) !== 0) {
|
|
118
|
+
const granted = packet.granted as number[]
|
|
119
|
+
for (let grantedI = 0; grantedI < granted.length; grantedI++) {
|
|
120
|
+
if ((granted[grantedI] & 0x80) !== 0) {
|
|
122
121
|
// suback with Failure status
|
|
123
122
|
const topics = client.messageIdToTopic[messageId]
|
|
124
123
|
if (topics) {
|
|
125
124
|
topics.forEach((topic) => {
|
|
126
|
-
delete client
|
|
125
|
+
delete client['_resubscribeTopics'][topic]
|
|
127
126
|
})
|
|
128
127
|
}
|
|
129
128
|
}
|
|
130
129
|
}
|
|
131
130
|
delete client.messageIdToTopic[messageId]
|
|
132
|
-
client
|
|
131
|
+
client['_invokeStoreProcessingQueue']()
|
|
133
132
|
cb(null, packet)
|
|
134
133
|
break
|
|
135
134
|
}
|
|
136
135
|
case 'unsuback': {
|
|
137
136
|
delete client.outgoing[messageId]
|
|
138
137
|
client.messageIdProvider.deallocate(messageId)
|
|
139
|
-
client
|
|
138
|
+
client['_invokeStoreProcessingQueue']()
|
|
140
139
|
cb(null)
|
|
141
140
|
break
|
|
142
141
|
}
|
|
@@ -149,5 +148,4 @@ function handleAck(client, packet) {
|
|
|
149
148
|
}
|
|
150
149
|
}
|
|
151
150
|
|
|
152
|
-
|
|
153
|
-
module.exports.errors = errors
|
|
151
|
+
export default handleAck
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { IAuthPacket } from 'mqtt-packet'
|
|
2
|
+
import { ErrorWithReasonCode, PacketHandler } from '../shared'
|
|
3
|
+
import { ReasonCodes } from './ack'
|
|
4
|
+
|
|
5
|
+
const handleAuth: PacketHandler = (
|
|
6
|
+
client,
|
|
7
|
+
packet: IAuthPacket & { returnCode: number },
|
|
8
|
+
) => {
|
|
9
|
+
const { options } = client
|
|
10
|
+
const version = options.protocolVersion
|
|
11
|
+
const rc = version === 5 ? packet.reasonCode : packet.returnCode
|
|
12
|
+
|
|
13
|
+
if (version !== 5) {
|
|
14
|
+
const err = new ErrorWithReasonCode(
|
|
15
|
+
`Protocol error: Auth packets are only supported in MQTT 5. Your version:${version}`,
|
|
16
|
+
rc,
|
|
17
|
+
)
|
|
18
|
+
client.emit('error', err)
|
|
19
|
+
return
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
client.handleAuth(
|
|
23
|
+
packet,
|
|
24
|
+
(err: ErrorWithReasonCode, packet2: IAuthPacket) => {
|
|
25
|
+
if (err) {
|
|
26
|
+
client.emit('error', err)
|
|
27
|
+
return
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (rc === 24) {
|
|
31
|
+
client.reconnecting = false
|
|
32
|
+
client['_sendPacket'](packet2)
|
|
33
|
+
} else {
|
|
34
|
+
const error = new ErrorWithReasonCode(
|
|
35
|
+
`Connection refused: ${ReasonCodes[rc]}`,
|
|
36
|
+
rc,
|
|
37
|
+
)
|
|
38
|
+
client.emit('error', error)
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export default handleAuth
|
|
@@ -1,14 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { ReasonCodes } from './ack'
|
|
2
|
+
import TopicAliasSend from '../topic-alias-send'
|
|
3
|
+
import { ErrorWithReasonCode, PacketHandler } from '../shared'
|
|
4
|
+
import { IConnackPacket } from 'mqtt-packet'
|
|
3
5
|
|
|
4
|
-
|
|
6
|
+
const handleConnack: PacketHandler = (client, packet: IConnackPacket) => {
|
|
5
7
|
client.log('_handleConnack')
|
|
6
8
|
const { options } = client
|
|
7
9
|
const version = options.protocolVersion
|
|
8
10
|
const rc = version === 5 ? packet.reasonCode : packet.returnCode
|
|
9
11
|
|
|
10
|
-
clearTimeout(client
|
|
11
|
-
delete client
|
|
12
|
+
clearTimeout(client['connackTimer'])
|
|
13
|
+
delete client['topicAliasSend']
|
|
12
14
|
|
|
13
15
|
if (packet.properties) {
|
|
14
16
|
if (packet.properties.topicAliasMaximum) {
|
|
@@ -20,14 +22,14 @@ function handleConnack(client, packet) {
|
|
|
20
22
|
return
|
|
21
23
|
}
|
|
22
24
|
if (packet.properties.topicAliasMaximum > 0) {
|
|
23
|
-
client
|
|
25
|
+
client['topicAliasSend'] = new TopicAliasSend(
|
|
24
26
|
packet.properties.topicAliasMaximum,
|
|
25
27
|
)
|
|
26
28
|
}
|
|
27
29
|
}
|
|
28
30
|
if (packet.properties.serverKeepAlive && options.keepalive) {
|
|
29
31
|
options.keepalive = packet.properties.serverKeepAlive
|
|
30
|
-
client
|
|
32
|
+
client['_shiftPingInterval']()
|
|
31
33
|
}
|
|
32
34
|
if (packet.properties.maximumPacketSize) {
|
|
33
35
|
if (!options.properties) {
|
|
@@ -40,12 +42,14 @@ function handleConnack(client, packet) {
|
|
|
40
42
|
|
|
41
43
|
if (rc === 0) {
|
|
42
44
|
client.reconnecting = false
|
|
43
|
-
client
|
|
45
|
+
client['_onConnect'](packet)
|
|
44
46
|
} else if (rc > 0) {
|
|
45
|
-
const err = new
|
|
46
|
-
|
|
47
|
+
const err = new ErrorWithReasonCode(
|
|
48
|
+
`Connection refused: ${ReasonCodes[rc]}`,
|
|
49
|
+
rc,
|
|
50
|
+
)
|
|
47
51
|
client.emit('error', err)
|
|
48
52
|
}
|
|
49
53
|
}
|
|
50
54
|
|
|
51
|
-
|
|
55
|
+
export default handleConnack
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import handlePublish from './publish'
|
|
2
|
+
import handleAuth from './auth'
|
|
3
|
+
import handleConnack from './connack'
|
|
4
|
+
import handleAck from './ack'
|
|
5
|
+
import handlePubrel from './pubrel'
|
|
6
|
+
import { PacketHandler } from '../shared'
|
|
6
7
|
|
|
7
|
-
|
|
8
|
+
const handle: PacketHandler = (client, packet, done) => {
|
|
8
9
|
const { options } = client
|
|
9
10
|
|
|
10
11
|
if (
|
|
@@ -63,4 +64,4 @@ function handle(client, packet, done) {
|
|
|
63
64
|
}
|
|
64
65
|
}
|
|
65
66
|
|
|
66
|
-
|
|
67
|
+
export default handle
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { IPublishPacket } from 'mqtt-packet'
|
|
2
|
+
import { PacketHandler } from '../shared'
|
|
3
|
+
|
|
1
4
|
const validReasonCodes = [0, 16, 128, 131, 135, 144, 145, 151, 153]
|
|
2
5
|
|
|
3
6
|
/*
|
|
@@ -24,7 +27,7 @@ const validReasonCodes = [0, 16, 128, 131, 135, 144, 145, 151, 153]
|
|
|
24
27
|
|
|
25
28
|
for now i just suppressed the warnings
|
|
26
29
|
*/
|
|
27
|
-
|
|
30
|
+
const handlePublish: PacketHandler = (client, packet: IPublishPacket, done) => {
|
|
28
31
|
client.log('handlePublish: packet %o', packet)
|
|
29
32
|
done = typeof done !== 'undefined' ? done : client.noop
|
|
30
33
|
let topic = packet.topic.toString()
|
|
@@ -33,7 +36,7 @@ function handlePublish(client, packet, done) {
|
|
|
33
36
|
const { messageId } = packet
|
|
34
37
|
const { options } = client
|
|
35
38
|
if (client.options.protocolVersion === 5) {
|
|
36
|
-
let alias
|
|
39
|
+
let alias: number
|
|
37
40
|
if (packet.properties) {
|
|
38
41
|
alias = packet.properties.topicAlias
|
|
39
42
|
}
|
|
@@ -41,7 +44,7 @@ function handlePublish(client, packet, done) {
|
|
|
41
44
|
if (topic.length === 0) {
|
|
42
45
|
if (alias > 0 && alias <= 0xffff) {
|
|
43
46
|
const gotTopic =
|
|
44
|
-
client
|
|
47
|
+
client['topicAliasRecv'].getTopicByAlias(alias)
|
|
45
48
|
if (gotTopic) {
|
|
46
49
|
topic = gotTopic
|
|
47
50
|
client.log(
|
|
@@ -71,7 +74,7 @@ function handlePublish(client, packet, done) {
|
|
|
71
74
|
)
|
|
72
75
|
return
|
|
73
76
|
}
|
|
74
|
-
} else if (client
|
|
77
|
+
} else if (client['topicAliasRecv'].put(topic, alias)) {
|
|
75
78
|
client.log(
|
|
76
79
|
'handlePublish :: registered topic: %s - alias: %d',
|
|
77
80
|
topic,
|
|
@@ -93,67 +96,80 @@ function handlePublish(client, packet, done) {
|
|
|
93
96
|
client.log('handlePublish: qos %d', qos)
|
|
94
97
|
switch (qos) {
|
|
95
98
|
case 2: {
|
|
96
|
-
options.customHandleAcks(
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
'error',
|
|
107
|
-
|
|
108
|
-
)
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
)
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
99
|
+
options.customHandleAcks(
|
|
100
|
+
topic,
|
|
101
|
+
message as Buffer,
|
|
102
|
+
packet,
|
|
103
|
+
(error, code) => {
|
|
104
|
+
if (typeof error === 'number') {
|
|
105
|
+
code = error
|
|
106
|
+
error = null
|
|
107
|
+
}
|
|
108
|
+
if (error) {
|
|
109
|
+
return client.emit('error', error as Error)
|
|
110
|
+
}
|
|
111
|
+
if (validReasonCodes.indexOf(code) === -1) {
|
|
112
|
+
return client.emit(
|
|
113
|
+
'error',
|
|
114
|
+
new Error('Wrong reason code for pubrec'),
|
|
115
|
+
)
|
|
116
|
+
}
|
|
117
|
+
if (code) {
|
|
118
|
+
client['_sendPacket'](
|
|
119
|
+
{ cmd: 'pubrec', messageId, reasonCode: code },
|
|
120
|
+
done,
|
|
121
|
+
)
|
|
122
|
+
} else {
|
|
123
|
+
client.incomingStore.put(packet, () => {
|
|
124
|
+
client['_sendPacket'](
|
|
125
|
+
{ cmd: 'pubrec', messageId },
|
|
126
|
+
done,
|
|
127
|
+
)
|
|
128
|
+
})
|
|
129
|
+
}
|
|
130
|
+
},
|
|
131
|
+
)
|
|
121
132
|
break
|
|
122
133
|
}
|
|
123
134
|
case 1: {
|
|
124
135
|
// emit the message event
|
|
125
|
-
options.customHandleAcks(
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
if (validReasonCodes.indexOf(code) === -1) {
|
|
134
|
-
return client.emit(
|
|
135
|
-
'error',
|
|
136
|
-
new Error('Wrong reason code for puback'),
|
|
137
|
-
)
|
|
138
|
-
}
|
|
139
|
-
if (!code) {
|
|
140
|
-
client.emit('message', topic, message, packet)
|
|
141
|
-
}
|
|
142
|
-
client.handleMessage(packet, (err) => {
|
|
143
|
-
if (err) {
|
|
144
|
-
return done && done(err)
|
|
136
|
+
options.customHandleAcks(
|
|
137
|
+
topic,
|
|
138
|
+
message as Buffer,
|
|
139
|
+
packet,
|
|
140
|
+
(error, code) => {
|
|
141
|
+
if (typeof error === 'number') {
|
|
142
|
+
code = error
|
|
143
|
+
error = null
|
|
145
144
|
}
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
)
|
|
150
|
-
|
|
151
|
-
|
|
145
|
+
if (error) {
|
|
146
|
+
return client.emit('error', error as Error)
|
|
147
|
+
}
|
|
148
|
+
if (validReasonCodes.indexOf(code) === -1) {
|
|
149
|
+
return client.emit(
|
|
150
|
+
'error',
|
|
151
|
+
new Error('Wrong reason code for puback'),
|
|
152
|
+
)
|
|
153
|
+
}
|
|
154
|
+
if (!code) {
|
|
155
|
+
client.emit('message', topic, message as Buffer, packet)
|
|
156
|
+
}
|
|
157
|
+
client.handleMessage(packet, (err) => {
|
|
158
|
+
if (err) {
|
|
159
|
+
return done && done(err)
|
|
160
|
+
}
|
|
161
|
+
client['_sendPacket'](
|
|
162
|
+
{ cmd: 'puback', messageId, reasonCode: code },
|
|
163
|
+
done,
|
|
164
|
+
)
|
|
165
|
+
})
|
|
166
|
+
},
|
|
167
|
+
)
|
|
152
168
|
break
|
|
153
169
|
}
|
|
154
170
|
case 0:
|
|
155
171
|
// emit the message event
|
|
156
|
-
client.emit('message', topic, message, packet)
|
|
172
|
+
client.emit('message', topic, message as Buffer, packet)
|
|
157
173
|
client.handleMessage(packet, done)
|
|
158
174
|
break
|
|
159
175
|
default:
|
|
@@ -164,4 +180,4 @@ function handlePublish(client, packet, done) {
|
|
|
164
180
|
}
|
|
165
181
|
}
|
|
166
182
|
|
|
167
|
-
|
|
183
|
+
export default handlePublish
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { IPubcompPacket, IPublishPacket, IPubrelPacket } from 'mqtt-packet'
|
|
2
|
+
import { PacketHandler } from '../shared'
|
|
3
|
+
|
|
4
|
+
const handlePubrel: PacketHandler = (client, packet: IPubrelPacket, done) => {
|
|
5
|
+
client.log('handling pubrel packet')
|
|
6
|
+
const callback = typeof done !== 'undefined' ? done : client.noop
|
|
7
|
+
const { messageId } = packet
|
|
8
|
+
|
|
9
|
+
const comp: IPubcompPacket = { cmd: 'pubcomp', messageId }
|
|
10
|
+
|
|
11
|
+
client.incomingStore.get(packet, (err, pub: IPublishPacket) => {
|
|
12
|
+
if (!err) {
|
|
13
|
+
client.emit('message', pub.topic, pub.payload as Buffer, pub)
|
|
14
|
+
client.handleMessage(pub, (err2) => {
|
|
15
|
+
if (err2) {
|
|
16
|
+
return callback(err2)
|
|
17
|
+
}
|
|
18
|
+
client.incomingStore.del(pub, client.noop)
|
|
19
|
+
client['_sendPacket'](comp, callback)
|
|
20
|
+
})
|
|
21
|
+
} else {
|
|
22
|
+
client['_sendPacket'](comp, callback)
|
|
23
|
+
}
|
|
24
|
+
})
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export default handlePubrel
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
const legacyIsBrowser =
|
|
2
2
|
(typeof process !== 'undefined' && process.title === 'browser') ||
|
|
3
|
-
// eslint-disable-next-line camelcase
|
|
3
|
+
// eslint-disable-next-line camelcase, @typescript-eslint/ban-ts-comment
|
|
4
|
+
// @ts-ignore
|
|
4
5
|
typeof __webpack_require__ === 'function'
|
|
5
6
|
|
|
6
7
|
const isBrowser =
|
|
7
8
|
typeof window !== 'undefined' && typeof document !== 'undefined'
|
|
8
9
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
const IS_BROWSER = isBrowser || legacyIsBrowser
|
|
11
|
+
|
|
12
|
+
export default IS_BROWSER
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import type { Packet } from 'mqtt-packet'
|
|
2
|
+
import type internal from 'stream'
|
|
3
|
+
import type MqttClient from './client'
|
|
4
|
+
import type { IClientOptions } from './client'
|
|
5
|
+
|
|
6
|
+
export type DoneCallback = (error?: Error) => void
|
|
7
|
+
|
|
8
|
+
export type GenericCallback<T> = (error?: Error, result?: T) => void
|
|
9
|
+
|
|
10
|
+
export type VoidCallback = () => void
|
|
11
|
+
|
|
12
|
+
export type IStream = internal.Duplex
|
|
13
|
+
|
|
14
|
+
export type StreamBuilder = (
|
|
15
|
+
client: MqttClient,
|
|
16
|
+
opts?: IClientOptions,
|
|
17
|
+
) => IStream
|
|
18
|
+
|
|
19
|
+
export type Callback = () => void
|
|
20
|
+
|
|
21
|
+
export type PacketHandler = (
|
|
22
|
+
client: MqttClient,
|
|
23
|
+
packet: Packet,
|
|
24
|
+
done?: DoneCallback,
|
|
25
|
+
) => void
|
|
26
|
+
|
|
27
|
+
export class ErrorWithReasonCode extends Error {
|
|
28
|
+
public code: number
|
|
29
|
+
|
|
30
|
+
public constructor(message: string, code: number) {
|
|
31
|
+
super(message)
|
|
32
|
+
this.code = code
|
|
33
|
+
|
|
34
|
+
// We need to set the prototype explicitly
|
|
35
|
+
Object.setPrototypeOf(this, ErrorWithReasonCode.prototype)
|
|
36
|
+
Object.getPrototypeOf(this).name = 'ErrorWithReasonCode'
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
41
|
+
export type Constructor<T = {}> = new (...args: any[]) => T
|
|
42
|
+
|
|
43
|
+
export function applyMixin(
|
|
44
|
+
target: Constructor,
|
|
45
|
+
mixin: Constructor,
|
|
46
|
+
includeConstructor = false,
|
|
47
|
+
): void {
|
|
48
|
+
// Figure out the inheritance chain of the mixin
|
|
49
|
+
const inheritanceChain: Constructor[] = [mixin]
|
|
50
|
+
// eslint-disable-next-line no-constant-condition
|
|
51
|
+
while (true) {
|
|
52
|
+
const current = inheritanceChain[0]
|
|
53
|
+
const base = Object.getPrototypeOf(current)
|
|
54
|
+
if (base?.prototype) {
|
|
55
|
+
inheritanceChain.unshift(base)
|
|
56
|
+
} else {
|
|
57
|
+
break
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
for (const ctor of inheritanceChain) {
|
|
61
|
+
for (const prop of Object.getOwnPropertyNames(ctor.prototype)) {
|
|
62
|
+
// Do not override the constructor
|
|
63
|
+
if (includeConstructor || prop !== 'constructor') {
|
|
64
|
+
Object.defineProperty(
|
|
65
|
+
target.prototype,
|
|
66
|
+
prop,
|
|
67
|
+
Object.getOwnPropertyDescriptor(ctor.prototype, prop) ??
|
|
68
|
+
Object.create(null),
|
|
69
|
+
)
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|