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/README.md +42 -26
- package/bin/mqtt.js +7 -7
- package/bin/pub.js +139 -126
- package/bin/sub.js +127 -109
- package/dist/mqtt.js +5512 -4899
- package/dist/mqtt.min.js +4 -4
- package/lib/client.js +1641 -1891
- 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 +39 -41
- package/types/lib/client-options.d.ts +17 -1
- package/types/lib/client.d.ts +13 -1
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
const validReasonCodes = [0, 16, 128, 131, 135, 144, 145, 151, 153]
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
those late 2 case should be rewrite to comply with coding style:
|
|
5
|
+
|
|
6
|
+
case 1:
|
|
7
|
+
case 0:
|
|
8
|
+
// do not wait sending a puback
|
|
9
|
+
// no callback passed
|
|
10
|
+
if (1 === qos) {
|
|
11
|
+
this._sendPacket({
|
|
12
|
+
cmd: 'puback',
|
|
13
|
+
messageId: messageId
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
// emit the message event for both qos 1 and 0
|
|
17
|
+
this.emit('message', topic, message, packet);
|
|
18
|
+
this.handleMessage(packet, done);
|
|
19
|
+
break;
|
|
20
|
+
default:
|
|
21
|
+
// do nothing but every switch mus have a default
|
|
22
|
+
// log or throw an error about unknown qos
|
|
23
|
+
break;
|
|
24
|
+
|
|
25
|
+
for now i just suppressed the warnings
|
|
26
|
+
*/
|
|
27
|
+
function handlePublish(client, packet, done) {
|
|
28
|
+
client.log('handlePublish: packet %o', packet)
|
|
29
|
+
done = typeof done !== 'undefined' ? done : client.noop
|
|
30
|
+
let topic = packet.topic.toString()
|
|
31
|
+
const message = packet.payload
|
|
32
|
+
const { qos } = packet
|
|
33
|
+
const { messageId } = packet
|
|
34
|
+
const { options } = client
|
|
35
|
+
if (client.options.protocolVersion === 5) {
|
|
36
|
+
let alias
|
|
37
|
+
if (packet.properties) {
|
|
38
|
+
alias = packet.properties.topicAlias
|
|
39
|
+
}
|
|
40
|
+
if (typeof alias !== 'undefined') {
|
|
41
|
+
if (topic.length === 0) {
|
|
42
|
+
if (alias > 0 && alias <= 0xffff) {
|
|
43
|
+
const gotTopic =
|
|
44
|
+
client.topicAliasRecv.getTopicByAlias(alias)
|
|
45
|
+
if (gotTopic) {
|
|
46
|
+
topic = gotTopic
|
|
47
|
+
client.log(
|
|
48
|
+
'handlePublish :: topic complemented by alias. topic: %s - alias: %d',
|
|
49
|
+
topic,
|
|
50
|
+
alias,
|
|
51
|
+
)
|
|
52
|
+
} else {
|
|
53
|
+
client.log(
|
|
54
|
+
'handlePublish :: unregistered topic alias. alias: %d',
|
|
55
|
+
alias,
|
|
56
|
+
)
|
|
57
|
+
client.emit(
|
|
58
|
+
'error',
|
|
59
|
+
new Error('Received unregistered Topic Alias'),
|
|
60
|
+
)
|
|
61
|
+
return
|
|
62
|
+
}
|
|
63
|
+
} else {
|
|
64
|
+
client.log(
|
|
65
|
+
'handlePublish :: topic alias out of range. alias: %d',
|
|
66
|
+
alias,
|
|
67
|
+
)
|
|
68
|
+
client.emit(
|
|
69
|
+
'error',
|
|
70
|
+
new Error('Received Topic Alias is out of range'),
|
|
71
|
+
)
|
|
72
|
+
return
|
|
73
|
+
}
|
|
74
|
+
} else if (client.topicAliasRecv.put(topic, alias)) {
|
|
75
|
+
client.log(
|
|
76
|
+
'handlePublish :: registered topic: %s - alias: %d',
|
|
77
|
+
topic,
|
|
78
|
+
alias,
|
|
79
|
+
)
|
|
80
|
+
} else {
|
|
81
|
+
client.log(
|
|
82
|
+
'handlePublish :: topic alias out of range. alias: %d',
|
|
83
|
+
alias,
|
|
84
|
+
)
|
|
85
|
+
client.emit(
|
|
86
|
+
'error',
|
|
87
|
+
new Error('Received Topic Alias is out of range'),
|
|
88
|
+
)
|
|
89
|
+
return
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
client.log('handlePublish: qos %d', qos)
|
|
94
|
+
switch (qos) {
|
|
95
|
+
case 2: {
|
|
96
|
+
options.customHandleAcks(topic, message, packet, (error, code) => {
|
|
97
|
+
if (!(error instanceof Error)) {
|
|
98
|
+
code = error
|
|
99
|
+
error = null
|
|
100
|
+
}
|
|
101
|
+
if (error) {
|
|
102
|
+
return client.emit('error', error)
|
|
103
|
+
}
|
|
104
|
+
if (validReasonCodes.indexOf(code) === -1) {
|
|
105
|
+
return client.emit(
|
|
106
|
+
'error',
|
|
107
|
+
new Error('Wrong reason code for pubrec'),
|
|
108
|
+
)
|
|
109
|
+
}
|
|
110
|
+
if (code) {
|
|
111
|
+
client._sendPacket(
|
|
112
|
+
{ cmd: 'pubrec', messageId, reasonCode: code },
|
|
113
|
+
done,
|
|
114
|
+
)
|
|
115
|
+
} else {
|
|
116
|
+
client.incomingStore.put(packet, () => {
|
|
117
|
+
client._sendPacket({ cmd: 'pubrec', messageId }, done)
|
|
118
|
+
})
|
|
119
|
+
}
|
|
120
|
+
})
|
|
121
|
+
break
|
|
122
|
+
}
|
|
123
|
+
case 1: {
|
|
124
|
+
// emit the message event
|
|
125
|
+
options.customHandleAcks(topic, message, packet, (error, code) => {
|
|
126
|
+
if (!(error instanceof Error)) {
|
|
127
|
+
code = error
|
|
128
|
+
error = null
|
|
129
|
+
}
|
|
130
|
+
if (error) {
|
|
131
|
+
return client.emit('error', error)
|
|
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)
|
|
145
|
+
}
|
|
146
|
+
client._sendPacket(
|
|
147
|
+
{ cmd: 'puback', messageId, reasonCode: code },
|
|
148
|
+
done,
|
|
149
|
+
)
|
|
150
|
+
})
|
|
151
|
+
})
|
|
152
|
+
break
|
|
153
|
+
}
|
|
154
|
+
case 0:
|
|
155
|
+
// emit the message event
|
|
156
|
+
client.emit('message', topic, message, packet)
|
|
157
|
+
client.handleMessage(packet, done)
|
|
158
|
+
break
|
|
159
|
+
default:
|
|
160
|
+
// do nothing
|
|
161
|
+
client.log('handlePublish: unknown QoS. Doing nothing.')
|
|
162
|
+
// log or throw an error about unknown qos
|
|
163
|
+
break
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
module.exports = handlePublish
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
function handlePubrel(client, packet, done) {
|
|
2
|
+
client.log('handling pubrel packet')
|
|
3
|
+
const callback = typeof done !== 'undefined' ? done : client.noop
|
|
4
|
+
const { messageId } = packet
|
|
5
|
+
|
|
6
|
+
const comp = { cmd: 'pubcomp', messageId }
|
|
7
|
+
|
|
8
|
+
client.incomingStore.get(packet, (err, pub) => {
|
|
9
|
+
if (!err) {
|
|
10
|
+
client.emit('message', pub.topic, pub.payload, pub)
|
|
11
|
+
client.handleMessage(pub, (err2) => {
|
|
12
|
+
if (err2) {
|
|
13
|
+
return callback(err2)
|
|
14
|
+
}
|
|
15
|
+
client.incomingStore.del(pub, client.noop)
|
|
16
|
+
client._sendPacket(comp, callback)
|
|
17
|
+
})
|
|
18
|
+
} else {
|
|
19
|
+
client._sendPacket(comp, callback)
|
|
20
|
+
}
|
|
21
|
+
})
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
module.exports = handlePubrel
|
package/lib/is-browser.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
const legacyIsBrowser =
|
|
2
|
-
(typeof process !== 'undefined' && process.title === 'browser') ||
|
|
3
|
-
// eslint-disable-next-line camelcase
|
|
4
|
-
|
|
2
|
+
(typeof process !== 'undefined' && process.title === 'browser') ||
|
|
3
|
+
// eslint-disable-next-line camelcase
|
|
4
|
+
typeof __webpack_require__ === 'function'
|
|
5
5
|
|
|
6
6
|
const isBrowser =
|
|
7
|
-
|
|
7
|
+
typeof window !== 'undefined' && typeof document !== 'undefined'
|
|
8
8
|
|
|
9
9
|
module.exports = {
|
|
10
|
-
|
|
10
|
+
IS_BROWSER: isBrowser || legacyIsBrowser,
|
|
11
11
|
}
|
package/lib/store.js
CHANGED
|
@@ -1,14 +1,11 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
1
|
/**
|
|
4
2
|
* Module dependencies
|
|
5
3
|
*/
|
|
6
|
-
const
|
|
4
|
+
const { Readable } = require('readable-stream')
|
|
7
5
|
|
|
8
|
-
const Readable = require('readable-stream').Readable
|
|
9
6
|
const streamsOpts = { objectMode: true }
|
|
10
7
|
const defaultStoreOptions = {
|
|
11
|
-
|
|
8
|
+
clean: true,
|
|
12
9
|
}
|
|
13
10
|
|
|
14
11
|
/**
|
|
@@ -17,112 +14,108 @@ const defaultStoreOptions = {
|
|
|
17
14
|
*
|
|
18
15
|
* @param {Object} [options] - store options
|
|
19
16
|
*/
|
|
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
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
}
|
|
123
|
-
if (cb) {
|
|
124
|
-
cb()
|
|
125
|
-
}
|
|
17
|
+
class Store {
|
|
18
|
+
constructor(options) {
|
|
19
|
+
this.options = options || {}
|
|
20
|
+
|
|
21
|
+
// Defaults
|
|
22
|
+
this.options = { ...defaultStoreOptions, ...options }
|
|
23
|
+
|
|
24
|
+
this._inflights = new Map()
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Adds a packet to the store, a packet is
|
|
29
|
+
* anything that has a messageId property.
|
|
30
|
+
*
|
|
31
|
+
*/
|
|
32
|
+
put(packet, cb) {
|
|
33
|
+
this._inflights.set(packet.messageId, packet)
|
|
34
|
+
|
|
35
|
+
if (cb) {
|
|
36
|
+
cb()
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return this
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Creates a stream with all the packets in the store
|
|
44
|
+
*
|
|
45
|
+
*/
|
|
46
|
+
createStream() {
|
|
47
|
+
const stream = new Readable(streamsOpts)
|
|
48
|
+
const values = []
|
|
49
|
+
let destroyed = false
|
|
50
|
+
let i = 0
|
|
51
|
+
|
|
52
|
+
this._inflights.forEach((value, key) => {
|
|
53
|
+
values.push(value)
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
stream._read = () => {
|
|
57
|
+
if (!destroyed && i < values.length) {
|
|
58
|
+
stream.push(values[i++])
|
|
59
|
+
} else {
|
|
60
|
+
stream.push(null)
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
stream.destroy = () => {
|
|
65
|
+
if (destroyed) {
|
|
66
|
+
return
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
destroyed = true
|
|
70
|
+
|
|
71
|
+
setTimeout(() => {
|
|
72
|
+
stream.emit('close')
|
|
73
|
+
}, 0)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return stream
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* deletes a packet from the store.
|
|
81
|
+
*/
|
|
82
|
+
del(packet, cb) {
|
|
83
|
+
packet = this._inflights.get(packet.messageId)
|
|
84
|
+
if (packet) {
|
|
85
|
+
this._inflights.delete(packet.messageId)
|
|
86
|
+
cb(null, packet)
|
|
87
|
+
} else if (cb) {
|
|
88
|
+
cb(new Error('missing packet'))
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return this
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* get a packet from the store.
|
|
96
|
+
*/
|
|
97
|
+
get(packet, cb) {
|
|
98
|
+
packet = this._inflights.get(packet.messageId)
|
|
99
|
+
if (packet) {
|
|
100
|
+
cb(null, packet)
|
|
101
|
+
} else if (cb) {
|
|
102
|
+
cb(new Error('missing packet'))
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return this
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Close the store
|
|
110
|
+
*/
|
|
111
|
+
close(cb) {
|
|
112
|
+
if (this.options.clean) {
|
|
113
|
+
this._inflights = null
|
|
114
|
+
}
|
|
115
|
+
if (cb) {
|
|
116
|
+
cb()
|
|
117
|
+
}
|
|
118
|
+
}
|
|
126
119
|
}
|
|
127
120
|
|
|
128
121
|
module.exports = Store
|
package/lib/topic-alias-recv.js
CHANGED
|
@@ -1,47 +1,44 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
1
|
/**
|
|
4
2
|
* Topic Alias receiving manager
|
|
5
3
|
* This holds alias to topic map
|
|
6
4
|
* @param {Number} [max] - topic alias maximum entries
|
|
7
5
|
*/
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
this.max = max
|
|
14
|
-
}
|
|
6
|
+
class TopicAliasRecv {
|
|
7
|
+
constructor(max) {
|
|
8
|
+
this.aliasToTopic = {}
|
|
9
|
+
this.max = max
|
|
10
|
+
}
|
|
15
11
|
|
|
16
|
-
/**
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
}
|
|
12
|
+
/**
|
|
13
|
+
* Insert or update topic - alias entry.
|
|
14
|
+
* @param {String} [topic] - topic
|
|
15
|
+
* @param {Number} [alias] - topic alias
|
|
16
|
+
* @returns {Boolean} - if success return true otherwise false
|
|
17
|
+
*/
|
|
18
|
+
put(topic, alias) {
|
|
19
|
+
if (alias === 0 || alias > this.max) {
|
|
20
|
+
return false
|
|
21
|
+
}
|
|
22
|
+
this.aliasToTopic[alias] = topic
|
|
23
|
+
this.length = Object.keys(this.aliasToTopic).length
|
|
24
|
+
return true
|
|
25
|
+
}
|
|
30
26
|
|
|
31
|
-
/**
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
}
|
|
27
|
+
/**
|
|
28
|
+
* Get topic by alias
|
|
29
|
+
* @param {String} [topic] - topic
|
|
30
|
+
* @returns {Number} - if mapped topic exists return topic alias, otherwise return undefined
|
|
31
|
+
*/
|
|
32
|
+
getTopicByAlias(alias) {
|
|
33
|
+
return this.aliasToTopic[alias]
|
|
34
|
+
}
|
|
39
35
|
|
|
40
|
-
/**
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
36
|
+
/**
|
|
37
|
+
* Clear all entries
|
|
38
|
+
*/
|
|
39
|
+
clear() {
|
|
40
|
+
this.aliasToTopic = {}
|
|
41
|
+
}
|
|
45
42
|
}
|
|
46
43
|
|
|
47
44
|
module.exports = TopicAliasRecv
|
package/lib/topic-alias-send.js
CHANGED
|
@@ -1,91 +1,88 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
1
|
/**
|
|
4
2
|
* Module dependencies
|
|
5
3
|
*/
|
|
6
|
-
const
|
|
7
|
-
const NumberAllocator = require('number-allocator')
|
|
4
|
+
const LRUCache = require('lru-cache')
|
|
5
|
+
const { NumberAllocator } = require('number-allocator')
|
|
8
6
|
|
|
9
7
|
/**
|
|
10
8
|
* Topic Alias sending manager
|
|
11
9
|
* This holds both topic to alias and alias to topic map
|
|
12
10
|
* @param {Number} [max] - topic alias maximum entries
|
|
13
11
|
*/
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
12
|
+
class TopicAliasSend {
|
|
13
|
+
constructor(max) {
|
|
14
|
+
if (max > 0) {
|
|
15
|
+
this.aliasToTopic = new LRUCache({ max })
|
|
16
|
+
this.topicToAlias = {}
|
|
17
|
+
this.numberAllocator = new NumberAllocator(1, max)
|
|
18
|
+
this.max = max
|
|
19
|
+
this.length = 0
|
|
20
|
+
}
|
|
21
|
+
}
|
|
18
22
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
23
|
+
/**
|
|
24
|
+
* Insert or update topic - alias entry.
|
|
25
|
+
* @param {String} [topic] - topic
|
|
26
|
+
* @param {Number} [alias] - topic alias
|
|
27
|
+
* @returns {Boolean} - if success return true otherwise false
|
|
28
|
+
*/
|
|
29
|
+
put(topic, alias) {
|
|
30
|
+
if (alias === 0 || alias > this.max) {
|
|
31
|
+
return false
|
|
32
|
+
}
|
|
33
|
+
const entry = this.aliasToTopic.get(alias)
|
|
34
|
+
if (entry) {
|
|
35
|
+
delete this.topicToAlias[entry]
|
|
36
|
+
}
|
|
37
|
+
this.aliasToTopic.set(alias, topic)
|
|
38
|
+
this.topicToAlias[topic] = alias
|
|
39
|
+
this.numberAllocator.use(alias)
|
|
40
|
+
this.length = this.aliasToTopic.size
|
|
41
|
+
return true
|
|
42
|
+
}
|
|
27
43
|
|
|
28
|
-
/**
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
return false
|
|
37
|
-
}
|
|
38
|
-
const entry = this.aliasToTopic.get(alias)
|
|
39
|
-
if (entry) {
|
|
40
|
-
delete this.topicToAlias[entry]
|
|
41
|
-
}
|
|
42
|
-
this.aliasToTopic.set(alias, topic)
|
|
43
|
-
this.topicToAlias[topic] = alias
|
|
44
|
-
this.numberAllocator.use(alias)
|
|
45
|
-
this.length = this.aliasToTopic.length
|
|
46
|
-
return true
|
|
47
|
-
}
|
|
44
|
+
/**
|
|
45
|
+
* Get topic by alias
|
|
46
|
+
* @param {Number} [alias] - topic alias
|
|
47
|
+
* @returns {String} - if mapped topic exists return topic, otherwise return undefined
|
|
48
|
+
*/
|
|
49
|
+
getTopicByAlias(alias) {
|
|
50
|
+
return this.aliasToTopic.get(alias)
|
|
51
|
+
}
|
|
48
52
|
|
|
49
|
-
/**
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
53
|
+
/**
|
|
54
|
+
* Get topic by alias
|
|
55
|
+
* @param {String} [topic] - topic
|
|
56
|
+
* @returns {Number} - if mapped topic exists return topic alias, otherwise return undefined
|
|
57
|
+
*/
|
|
58
|
+
getAliasByTopic(topic) {
|
|
59
|
+
const alias = this.topicToAlias[topic]
|
|
60
|
+
if (typeof alias !== 'undefined') {
|
|
61
|
+
this.aliasToTopic.get(alias) // LRU update
|
|
62
|
+
}
|
|
63
|
+
return alias
|
|
64
|
+
}
|
|
57
65
|
|
|
58
|
-
/**
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
}
|
|
68
|
-
return alias
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Clear all entries
|
|
73
|
-
*/
|
|
74
|
-
TopicAliasSend.prototype.clear = function () {
|
|
75
|
-
this.aliasToTopic.reset()
|
|
76
|
-
this.topicToAlias = {}
|
|
77
|
-
this.numberAllocator.clear()
|
|
78
|
-
this.length = 0
|
|
79
|
-
}
|
|
66
|
+
/**
|
|
67
|
+
* Clear all entries
|
|
68
|
+
*/
|
|
69
|
+
clear() {
|
|
70
|
+
this.aliasToTopic.clear()
|
|
71
|
+
this.topicToAlias = {}
|
|
72
|
+
this.numberAllocator.clear()
|
|
73
|
+
this.length = 0
|
|
74
|
+
}
|
|
80
75
|
|
|
81
|
-
/**
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
76
|
+
/**
|
|
77
|
+
* Get Least Recently Used (LRU) topic alias
|
|
78
|
+
* @returns {Number} - if vacant alias exists then return it, otherwise then return LRU alias
|
|
79
|
+
*/
|
|
80
|
+
getLruAlias() {
|
|
81
|
+
const alias = this.numberAllocator.firstVacant()
|
|
82
|
+
if (alias) return alias
|
|
83
|
+
// get last alias (key) from LRU cache
|
|
84
|
+
return [...this.aliasToTopic.keys()][this.aliasToTopic.size - 1]
|
|
85
|
+
}
|
|
89
86
|
}
|
|
90
87
|
|
|
91
88
|
module.exports = TopicAliasSend
|