mqtt 4.2.8 → 4.3.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/CHANGELOG.md +62 -22
- package/CONTRIBUTING.md +27 -27
- package/LICENSE.md +15 -15
- package/README.md +843 -792
- package/bin/mqtt.js +27 -27
- package/bin/pub.js +146 -146
- package/bin/sub.js +123 -123
- package/dist/mqtt.js +12624 -4503
- package/dist/mqtt.min.js +1 -1
- package/doc/help.txt +8 -8
- package/doc/publish.txt +26 -26
- package/doc/subscribe.txt +26 -26
- package/lib/client.js +1890 -1581
- package/lib/connect/ali.js +128 -128
- package/lib/connect/index.js +165 -164
- package/lib/connect/tcp.js +20 -21
- package/lib/connect/tls.js +48 -45
- package/lib/connect/ws.js +256 -256
- package/lib/connect/wx.js +132 -134
- package/lib/default-message-id-provider.js +69 -0
- package/lib/store.js +128 -128
- package/lib/topic-alias-recv.js +47 -0
- package/lib/topic-alias-send.js +93 -0
- package/lib/unique-message-id-provider.js +65 -0
- package/lib/validations.js +52 -52
- package/mqtt.js +21 -17
- package/package.json +124 -109
- package/types/index.d.ts +30 -28
- package/types/lib/client-options.d.ts +204 -191
- package/types/lib/client.d.ts +261 -240
- package/types/lib/connect/index.d.ts +10 -10
- package/types/lib/default-message-id-provider.d.ts +49 -0
- package/types/lib/message-id-provider.d.ts +40 -0
- package/types/lib/store-options.d.ts +6 -6
- package/types/lib/store.d.ts +46 -46
- package/types/lib/unique-message-id-provider.d.ts +48 -0
package/lib/connect/wx.js
CHANGED
|
@@ -1,134 +1,132 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
/* global wx */
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
function
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
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
|
-
stream.
|
|
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
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
module.exports = buildStream
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const Transform = require('readable-stream').Transform
|
|
4
|
+
const duplexify = require('duplexify')
|
|
5
|
+
|
|
6
|
+
/* global wx */
|
|
7
|
+
let socketTask, proxy, stream
|
|
8
|
+
|
|
9
|
+
function buildProxy () {
|
|
10
|
+
const proxy = new Transform()
|
|
11
|
+
proxy._write = function (chunk, encoding, next) {
|
|
12
|
+
socketTask.send({
|
|
13
|
+
data: chunk.buffer,
|
|
14
|
+
success: function () {
|
|
15
|
+
next()
|
|
16
|
+
},
|
|
17
|
+
fail: function (errMsg) {
|
|
18
|
+
next(new Error(errMsg))
|
|
19
|
+
}
|
|
20
|
+
})
|
|
21
|
+
}
|
|
22
|
+
proxy._flush = function socketEnd (done) {
|
|
23
|
+
socketTask.close({
|
|
24
|
+
success: function () {
|
|
25
|
+
done()
|
|
26
|
+
}
|
|
27
|
+
})
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return proxy
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function setDefaultOpts (opts) {
|
|
34
|
+
if (!opts.hostname) {
|
|
35
|
+
opts.hostname = 'localhost'
|
|
36
|
+
}
|
|
37
|
+
if (!opts.path) {
|
|
38
|
+
opts.path = '/'
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (!opts.wsOptions) {
|
|
42
|
+
opts.wsOptions = {}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function buildUrl (opts, client) {
|
|
47
|
+
const protocol = opts.protocol === 'wxs' ? 'wss' : 'ws'
|
|
48
|
+
let url = protocol + '://' + opts.hostname + opts.path
|
|
49
|
+
if (opts.port && opts.port !== 80 && opts.port !== 443) {
|
|
50
|
+
url = protocol + '://' + opts.hostname + ':' + opts.port + opts.path
|
|
51
|
+
}
|
|
52
|
+
if (typeof (opts.transformWsUrl) === 'function') {
|
|
53
|
+
url = opts.transformWsUrl(url, opts, client)
|
|
54
|
+
}
|
|
55
|
+
return url
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function bindEventHandler () {
|
|
59
|
+
socketTask.onOpen(function () {
|
|
60
|
+
stream.setReadable(proxy)
|
|
61
|
+
stream.setWritable(proxy)
|
|
62
|
+
stream.emit('connect')
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
socketTask.onMessage(function (res) {
|
|
66
|
+
let data = res.data
|
|
67
|
+
|
|
68
|
+
if (data instanceof ArrayBuffer) data = Buffer.from(data)
|
|
69
|
+
else data = Buffer.from(data, 'utf8')
|
|
70
|
+
proxy.push(data)
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
socketTask.onClose(function () {
|
|
74
|
+
stream.end()
|
|
75
|
+
stream.destroy()
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
socketTask.onError(function (res) {
|
|
79
|
+
stream.destroy(new Error(res.errMsg))
|
|
80
|
+
})
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function buildStream (client, opts) {
|
|
84
|
+
opts.hostname = opts.hostname || opts.host
|
|
85
|
+
|
|
86
|
+
if (!opts.hostname) {
|
|
87
|
+
throw new Error('Could not determine host. Specify host manually.')
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const websocketSubProtocol =
|
|
91
|
+
(opts.protocolId === 'MQIsdp') && (opts.protocolVersion === 3)
|
|
92
|
+
? 'mqttv3.1'
|
|
93
|
+
: 'mqtt'
|
|
94
|
+
|
|
95
|
+
setDefaultOpts(opts)
|
|
96
|
+
|
|
97
|
+
const url = buildUrl(opts, client)
|
|
98
|
+
socketTask = wx.connectSocket({
|
|
99
|
+
url: url,
|
|
100
|
+
protocols: [websocketSubProtocol]
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
proxy = buildProxy()
|
|
104
|
+
stream = duplexify.obj()
|
|
105
|
+
stream._destroy = function (err, cb) {
|
|
106
|
+
socketTask.close({
|
|
107
|
+
success: function () {
|
|
108
|
+
cb && cb(err)
|
|
109
|
+
}
|
|
110
|
+
})
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const destroyRef = stream.destroy
|
|
114
|
+
stream.destroy = function () {
|
|
115
|
+
stream.destroy = destroyRef
|
|
116
|
+
|
|
117
|
+
const self = this
|
|
118
|
+
setTimeout(function () {
|
|
119
|
+
socketTask.close({
|
|
120
|
+
fail: function () {
|
|
121
|
+
self._destroy(new Error())
|
|
122
|
+
}
|
|
123
|
+
})
|
|
124
|
+
}, 0)
|
|
125
|
+
}.bind(stream)
|
|
126
|
+
|
|
127
|
+
bindEventHandler()
|
|
128
|
+
|
|
129
|
+
return stream
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
module.exports = buildStream
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* DefaultMessageAllocator constructor
|
|
5
|
+
* @constructor
|
|
6
|
+
*/
|
|
7
|
+
function DefaultMessageIdProvider () {
|
|
8
|
+
if (!(this instanceof DefaultMessageIdProvider)) {
|
|
9
|
+
return new DefaultMessageIdProvider()
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* MessageIDs starting with 1
|
|
14
|
+
* ensure that nextId is min. 1, see https://github.com/mqttjs/MQTT.js/issues/810
|
|
15
|
+
*/
|
|
16
|
+
this.nextId = Math.max(1, Math.floor(Math.random() * 65535))
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* allocate
|
|
21
|
+
*
|
|
22
|
+
* Get the next messageId.
|
|
23
|
+
* @return unsigned int
|
|
24
|
+
*/
|
|
25
|
+
DefaultMessageIdProvider.prototype.allocate = function () {
|
|
26
|
+
// id becomes current state of this.nextId and increments afterwards
|
|
27
|
+
const id = this.nextId++
|
|
28
|
+
// Ensure 16 bit unsigned int (max 65535, nextId got one higher)
|
|
29
|
+
if (this.nextId === 65536) {
|
|
30
|
+
this.nextId = 1
|
|
31
|
+
}
|
|
32
|
+
return id
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* getLastAllocated
|
|
37
|
+
* Get the last allocated messageId.
|
|
38
|
+
* @return unsigned int
|
|
39
|
+
*/
|
|
40
|
+
DefaultMessageIdProvider.prototype.getLastAllocated = function () {
|
|
41
|
+
return (this.nextId === 1) ? 65535 : (this.nextId - 1)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* register
|
|
46
|
+
* Register messageId. If success return true, otherwise return false.
|
|
47
|
+
* @param { unsigned int } - messageId to register,
|
|
48
|
+
* @return boolean
|
|
49
|
+
*/
|
|
50
|
+
DefaultMessageIdProvider.prototype.register = function (messageId) {
|
|
51
|
+
return true
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* deallocate
|
|
56
|
+
* Deallocate messageId.
|
|
57
|
+
* @param { unsigned int } - messageId to deallocate,
|
|
58
|
+
*/
|
|
59
|
+
DefaultMessageIdProvider.prototype.deallocate = function (messageId) {
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* clear
|
|
64
|
+
* Deallocate all messageIds.
|
|
65
|
+
*/
|
|
66
|
+
DefaultMessageIdProvider.prototype.clear = function () {
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
module.exports = DefaultMessageIdProvider
|
package/lib/store.js
CHANGED
|
@@ -1,128 +1,128 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Module dependencies
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
clean: true
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* In-memory implementation of the message store
|
|
16
|
-
* This can actually be saved into files.
|
|
17
|
-
*
|
|
18
|
-
* @param {Object} [options] - store options
|
|
19
|
-
*/
|
|
20
|
-
function Store (options) {
|
|
21
|
-
if (!(this instanceof Store)) {
|
|
22
|
-
return new Store(options)
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
this.options = options || {}
|
|
26
|
-
|
|
27
|
-
// Defaults
|
|
28
|
-
this.options = xtend(defaultStoreOptions, options)
|
|
29
|
-
|
|
30
|
-
this._inflights = new Map()
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Adds a packet to the store, a packet is
|
|
35
|
-
* anything that has a messageId property.
|
|
36
|
-
*
|
|
37
|
-
*/
|
|
38
|
-
Store.prototype.put = function (packet, cb) {
|
|
39
|
-
this._inflights.set(packet.messageId, packet)
|
|
40
|
-
|
|
41
|
-
if (cb) {
|
|
42
|
-
cb()
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
return this
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Creates a stream with all the packets in the store
|
|
50
|
-
*
|
|
51
|
-
*/
|
|
52
|
-
Store.prototype.createStream = function () {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
this._inflights.forEach(function (value, key) {
|
|
59
|
-
values.push(value)
|
|
60
|
-
})
|
|
61
|
-
|
|
62
|
-
stream._read = function () {
|
|
63
|
-
if (!destroyed && i < values.length) {
|
|
64
|
-
this.push(values[i++])
|
|
65
|
-
} else {
|
|
66
|
-
this.push(null)
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
stream.destroy = function () {
|
|
71
|
-
if (destroyed) {
|
|
72
|
-
return
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
destroyed = true
|
|
78
|
-
|
|
79
|
-
setTimeout(function () {
|
|
80
|
-
self.emit('close')
|
|
81
|
-
}, 0)
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
return stream
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* deletes a packet from the store.
|
|
89
|
-
*/
|
|
90
|
-
Store.prototype.del = function (packet, cb) {
|
|
91
|
-
packet = this._inflights.get(packet.messageId)
|
|
92
|
-
if (packet) {
|
|
93
|
-
this._inflights.delete(packet.messageId)
|
|
94
|
-
cb(null, packet)
|
|
95
|
-
} else if (cb) {
|
|
96
|
-
cb(new Error('missing packet'))
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
return this
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* get a packet from the store.
|
|
104
|
-
*/
|
|
105
|
-
Store.prototype.get = function (packet, cb) {
|
|
106
|
-
packet = this._inflights.get(packet.messageId)
|
|
107
|
-
if (packet) {
|
|
108
|
-
cb(null, packet)
|
|
109
|
-
} else if (cb) {
|
|
110
|
-
cb(new Error('missing packet'))
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
return this
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* Close the store
|
|
118
|
-
*/
|
|
119
|
-
Store.prototype.close = function (cb) {
|
|
120
|
-
if (this.options.clean) {
|
|
121
|
-
this._inflights = null
|
|
122
|
-
}
|
|
123
|
-
if (cb) {
|
|
124
|
-
cb()
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
module.exports = Store
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Module dependencies
|
|
5
|
+
*/
|
|
6
|
+
const xtend = require('xtend')
|
|
7
|
+
|
|
8
|
+
const Readable = require('readable-stream').Readable
|
|
9
|
+
const streamsOpts = { objectMode: true }
|
|
10
|
+
const defaultStoreOptions = {
|
|
11
|
+
clean: true
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* In-memory implementation of the message store
|
|
16
|
+
* This can actually be saved into files.
|
|
17
|
+
*
|
|
18
|
+
* @param {Object} [options] - store options
|
|
19
|
+
*/
|
|
20
|
+
function Store (options) {
|
|
21
|
+
if (!(this instanceof Store)) {
|
|
22
|
+
return new Store(options)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
this.options = options || {}
|
|
26
|
+
|
|
27
|
+
// Defaults
|
|
28
|
+
this.options = xtend(defaultStoreOptions, options)
|
|
29
|
+
|
|
30
|
+
this._inflights = new Map()
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Adds a packet to the store, a packet is
|
|
35
|
+
* anything that has a messageId property.
|
|
36
|
+
*
|
|
37
|
+
*/
|
|
38
|
+
Store.prototype.put = function (packet, cb) {
|
|
39
|
+
this._inflights.set(packet.messageId, packet)
|
|
40
|
+
|
|
41
|
+
if (cb) {
|
|
42
|
+
cb()
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return this
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Creates a stream with all the packets in the store
|
|
50
|
+
*
|
|
51
|
+
*/
|
|
52
|
+
Store.prototype.createStream = function () {
|
|
53
|
+
const stream = new Readable(streamsOpts)
|
|
54
|
+
const values = []
|
|
55
|
+
let destroyed = false
|
|
56
|
+
let i = 0
|
|
57
|
+
|
|
58
|
+
this._inflights.forEach(function (value, key) {
|
|
59
|
+
values.push(value)
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
stream._read = function () {
|
|
63
|
+
if (!destroyed && i < values.length) {
|
|
64
|
+
this.push(values[i++])
|
|
65
|
+
} else {
|
|
66
|
+
this.push(null)
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
stream.destroy = function () {
|
|
71
|
+
if (destroyed) {
|
|
72
|
+
return
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const self = this
|
|
76
|
+
|
|
77
|
+
destroyed = true
|
|
78
|
+
|
|
79
|
+
setTimeout(function () {
|
|
80
|
+
self.emit('close')
|
|
81
|
+
}, 0)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return stream
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* deletes a packet from the store.
|
|
89
|
+
*/
|
|
90
|
+
Store.prototype.del = function (packet, cb) {
|
|
91
|
+
packet = this._inflights.get(packet.messageId)
|
|
92
|
+
if (packet) {
|
|
93
|
+
this._inflights.delete(packet.messageId)
|
|
94
|
+
cb(null, packet)
|
|
95
|
+
} else if (cb) {
|
|
96
|
+
cb(new Error('missing packet'))
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return this
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* get a packet from the store.
|
|
104
|
+
*/
|
|
105
|
+
Store.prototype.get = function (packet, cb) {
|
|
106
|
+
packet = this._inflights.get(packet.messageId)
|
|
107
|
+
if (packet) {
|
|
108
|
+
cb(null, packet)
|
|
109
|
+
} else if (cb) {
|
|
110
|
+
cb(new Error('missing packet'))
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return this
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Close the store
|
|
118
|
+
*/
|
|
119
|
+
Store.prototype.close = function (cb) {
|
|
120
|
+
if (this.options.clean) {
|
|
121
|
+
this._inflights = null
|
|
122
|
+
}
|
|
123
|
+
if (cb) {
|
|
124
|
+
cb()
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
module.exports = Store
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Topic Alias receiving manager
|
|
5
|
+
* This holds alias to topic map
|
|
6
|
+
* @param {Number} [max] - topic alias maximum entries
|
|
7
|
+
*/
|
|
8
|
+
function TopicAliasRecv (max) {
|
|
9
|
+
if (!(this instanceof TopicAliasRecv)) {
|
|
10
|
+
return new TopicAliasRecv(max)
|
|
11
|
+
}
|
|
12
|
+
this.aliasToTopic = {}
|
|
13
|
+
this.max = max
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Insert or update topic - alias entry.
|
|
18
|
+
* @param {String} [topic] - topic
|
|
19
|
+
* @param {Number} [alias] - topic alias
|
|
20
|
+
* @returns {Boolean} - if success return true otherwise false
|
|
21
|
+
*/
|
|
22
|
+
TopicAliasRecv.prototype.put = function (topic, alias) {
|
|
23
|
+
if (alias === 0 || alias > this.max) {
|
|
24
|
+
return false
|
|
25
|
+
}
|
|
26
|
+
this.aliasToTopic[alias] = topic
|
|
27
|
+
this.length = Object.keys(this.aliasToTopic).length
|
|
28
|
+
return true
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Get topic by alias
|
|
33
|
+
* @param {String} [topic] - topic
|
|
34
|
+
* @returns {Number} - if mapped topic exists return topic alias, otherwise return undefined
|
|
35
|
+
*/
|
|
36
|
+
TopicAliasRecv.prototype.getTopicByAlias = function (alias) {
|
|
37
|
+
return this.aliasToTopic[alias]
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Clear all entries
|
|
42
|
+
*/
|
|
43
|
+
TopicAliasRecv.prototype.clear = function () {
|
|
44
|
+
this.aliasToTopic = {}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
module.exports = TopicAliasRecv
|