mqtt 4.2.6 → 4.3.1
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 +69 -0
- package/README.md +108 -49
- package/bin/mqtt.js +3 -3
- package/bin/pub.js +12 -12
- package/bin/sub.js +6 -6
- package/dist/mqtt.js +11466 -5784
- package/dist/mqtt.min.js +1 -1
- package/lib/client.js +569 -260
- package/lib/connect/ali.js +14 -14
- package/lib/connect/index.js +11 -10
- package/lib/connect/tcp.js +4 -5
- package/lib/connect/tls.js +8 -5
- package/lib/connect/ws.js +16 -15
- package/lib/connect/wx.js +11 -13
- package/lib/default-message-id-provider.js +69 -0
- package/lib/store.js +9 -9
- 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 +3 -3
- package/mqtt.js +7 -3
- package/package.json +122 -108
- package/types/index.d.ts +4 -1
- package/types/lib/client-options.d.ts +24 -11
- package/types/lib/client.d.ts +33 -4
- package/types/lib/default-message-id-provider.d.ts +49 -0
- package/types/lib/message-id-provider.d.ts +40 -0
- package/types/lib/unique-message-id-provider.d.ts +48 -0
package/lib/connect/ali.js
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
const Transform = require('readable-stream').Transform
|
|
4
|
+
const duplexify = require('duplexify')
|
|
5
5
|
|
|
6
6
|
/* global FileReader */
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
let my
|
|
8
|
+
let proxy
|
|
9
|
+
let stream
|
|
10
|
+
let isInitialized = false
|
|
11
11
|
|
|
12
12
|
function buildProxy () {
|
|
13
|
-
|
|
13
|
+
const proxy = new Transform()
|
|
14
14
|
proxy._write = function (chunk, encoding, next) {
|
|
15
15
|
my.sendSocketMessage({
|
|
16
16
|
data: chunk.buffer,
|
|
@@ -47,8 +47,8 @@ function setDefaultOpts (opts) {
|
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
function buildUrl (opts, client) {
|
|
50
|
-
|
|
51
|
-
|
|
50
|
+
const protocol = opts.protocol === 'alis' ? 'wss' : 'ws'
|
|
51
|
+
let url = protocol + '://' + opts.hostname + opts.path
|
|
52
52
|
if (opts.port && opts.port !== 80 && opts.port !== 443) {
|
|
53
53
|
url = protocol + '://' + opts.hostname + ':' + opts.port + opts.path
|
|
54
54
|
}
|
|
@@ -71,12 +71,12 @@ function bindEventHandler () {
|
|
|
71
71
|
|
|
72
72
|
my.onSocketMessage(function (res) {
|
|
73
73
|
if (typeof res.data === 'string') {
|
|
74
|
-
|
|
74
|
+
const buffer = Buffer.from(res.data, 'base64')
|
|
75
75
|
proxy.push(buffer)
|
|
76
76
|
} else {
|
|
77
|
-
|
|
77
|
+
const reader = new FileReader()
|
|
78
78
|
reader.addEventListener('load', function () {
|
|
79
|
-
|
|
79
|
+
let data = reader.result
|
|
80
80
|
|
|
81
81
|
if (data instanceof ArrayBuffer) data = Buffer.from(data)
|
|
82
82
|
else data = Buffer.from(data, 'utf8')
|
|
@@ -103,14 +103,14 @@ function buildStream (client, opts) {
|
|
|
103
103
|
throw new Error('Could not determine host. Specify host manually.')
|
|
104
104
|
}
|
|
105
105
|
|
|
106
|
-
|
|
106
|
+
const websocketSubProtocol =
|
|
107
107
|
(opts.protocolId === 'MQIsdp') && (opts.protocolVersion === 3)
|
|
108
108
|
? 'mqttv3.1'
|
|
109
109
|
: 'mqtt'
|
|
110
110
|
|
|
111
111
|
setDefaultOpts(opts)
|
|
112
112
|
|
|
113
|
-
|
|
113
|
+
const url = buildUrl(opts, client)
|
|
114
114
|
my = opts.my
|
|
115
115
|
my.connectSocket({
|
|
116
116
|
url: url,
|
package/lib/connect/index.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
const MqttClient = require('../client')
|
|
4
|
+
const Store = require('../store')
|
|
5
|
+
const url = require('url')
|
|
6
|
+
const xtend = require('xtend')
|
|
7
|
+
const debug = require('debug')('mqttjs')
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
const protocols = {}
|
|
10
10
|
|
|
11
11
|
// eslint-disable-next-line camelcase
|
|
12
12
|
if ((typeof process !== 'undefined' && process.title !== 'browser') || typeof __webpack_require__ !== 'function') {
|
|
@@ -32,7 +32,7 @@ protocols.wss = require('./ws')
|
|
|
32
32
|
* @param {Object} [opts] option object
|
|
33
33
|
*/
|
|
34
34
|
function parseAuthOptions (opts) {
|
|
35
|
-
|
|
35
|
+
let matches
|
|
36
36
|
if (opts.auth) {
|
|
37
37
|
matches = opts.auth.match(/^(.+):(.+)$/)
|
|
38
38
|
if (matches) {
|
|
@@ -60,7 +60,8 @@ function connect (brokerUrl, opts) {
|
|
|
60
60
|
opts = opts || {}
|
|
61
61
|
|
|
62
62
|
if (brokerUrl) {
|
|
63
|
-
|
|
63
|
+
// eslint-disable-next-line
|
|
64
|
+
const parsed = url.parse(brokerUrl, true)
|
|
64
65
|
if (parsed.port != null) {
|
|
65
66
|
parsed.port = Number(parsed.port)
|
|
66
67
|
}
|
|
@@ -109,7 +110,7 @@ function connect (brokerUrl, opts) {
|
|
|
109
110
|
}
|
|
110
111
|
|
|
111
112
|
if (!protocols[opts.protocol]) {
|
|
112
|
-
|
|
113
|
+
const isSecure = ['mqtts', 'wss'].indexOf(opts.protocol) !== -1
|
|
113
114
|
opts.protocol = [
|
|
114
115
|
'mqtt',
|
|
115
116
|
'mqtts',
|
|
@@ -153,7 +154,7 @@ function connect (brokerUrl, opts) {
|
|
|
153
154
|
debug('calling streambuilder for', opts.protocol)
|
|
154
155
|
return protocols[opts.protocol](client, opts)
|
|
155
156
|
}
|
|
156
|
-
|
|
157
|
+
const client = new MqttClient(wrapper, opts)
|
|
157
158
|
client.on('error', function () { /* Automatically set up client error handling */ })
|
|
158
159
|
return client
|
|
159
160
|
}
|
package/lib/connect/tcp.js
CHANGED
|
@@ -1,18 +1,17 @@
|
|
|
1
1
|
'use strict'
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
const net = require('net')
|
|
3
|
+
const debug = require('debug')('mqttjs:tcp')
|
|
4
4
|
|
|
5
5
|
/*
|
|
6
6
|
variables port and host can be removed since
|
|
7
7
|
you have all required information in opts object
|
|
8
8
|
*/
|
|
9
9
|
function streamBuilder (client, opts) {
|
|
10
|
-
var port, host
|
|
11
10
|
opts.port = opts.port || 1883
|
|
12
11
|
opts.hostname = opts.hostname || opts.host || 'localhost'
|
|
13
12
|
|
|
14
|
-
port = opts.port
|
|
15
|
-
host = opts.hostname
|
|
13
|
+
const port = opts.port
|
|
14
|
+
const host = opts.hostname
|
|
16
15
|
|
|
17
16
|
debug('port %d and host %s', port, host)
|
|
18
17
|
return net.createConnection(port, host)
|
package/lib/connect/tls.js
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
'use strict'
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
const tls = require('tls')
|
|
3
|
+
const net = require('net')
|
|
4
|
+
const debug = require('debug')('mqttjs:tls')
|
|
4
5
|
|
|
5
6
|
function buildBuilder (mqttClient, opts) {
|
|
6
|
-
var connection
|
|
7
7
|
opts.port = opts.port || 8883
|
|
8
8
|
opts.host = opts.hostname || opts.host || 'localhost'
|
|
9
|
-
|
|
9
|
+
|
|
10
|
+
if (net.isIP(opts.host) === 0) {
|
|
11
|
+
opts.servername = opts.host
|
|
12
|
+
}
|
|
10
13
|
|
|
11
14
|
opts.rejectUnauthorized = opts.rejectUnauthorized !== false
|
|
12
15
|
|
|
@@ -14,7 +17,7 @@ function buildBuilder (mqttClient, opts) {
|
|
|
14
17
|
|
|
15
18
|
debug('port %d host %s rejectUnauthorized %b', opts.port, opts.host, opts.rejectUnauthorized)
|
|
16
19
|
|
|
17
|
-
connection = tls.connect(opts)
|
|
20
|
+
const connection = tls.connect(opts)
|
|
18
21
|
/* eslint no-use-before-define: [2, "nofunc"] */
|
|
19
22
|
connection.on('secureConnect', function () {
|
|
20
23
|
if (opts.rejectUnauthorized && !connection.authorized) {
|
package/lib/connect/ws.js
CHANGED
|
@@ -5,7 +5,7 @@ const debug = require('debug')('mqttjs:ws')
|
|
|
5
5
|
const duplexify = require('duplexify')
|
|
6
6
|
const Transform = require('readable-stream').Transform
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
const WSS_OPTIONS = [
|
|
9
9
|
'rejectUnauthorized',
|
|
10
10
|
'ca',
|
|
11
11
|
'cert',
|
|
@@ -24,7 +24,7 @@ function buildUrl (opts, client) {
|
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
function setDefaultOpts (opts) {
|
|
27
|
-
|
|
27
|
+
const options = opts
|
|
28
28
|
if (!opts.hostname) {
|
|
29
29
|
options.hostname = 'localhost'
|
|
30
30
|
}
|
|
@@ -45,7 +45,7 @@ function setDefaultOpts (opts) {
|
|
|
45
45
|
if (!IS_BROWSER && opts.protocol === 'wss') {
|
|
46
46
|
// Add cert/key/ca etc options
|
|
47
47
|
WSS_OPTIONS.forEach(function (prop) {
|
|
48
|
-
if (
|
|
48
|
+
if (Object.prototype.hasOwnProperty.call(opts, prop) && !Object.prototype.hasOwnProperty.call(opts.wsOptions, prop)) {
|
|
49
49
|
options.wsOptions[prop] = opts[prop]
|
|
50
50
|
}
|
|
51
51
|
})
|
|
@@ -55,7 +55,7 @@ function setDefaultOpts (opts) {
|
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
function setDefaultBrowserOpts (opts) {
|
|
58
|
-
|
|
58
|
+
const options = setDefaultOpts(opts)
|
|
59
59
|
|
|
60
60
|
if (!options.hostname) {
|
|
61
61
|
options.hostname = options.host
|
|
@@ -93,7 +93,7 @@ function createWebSocket (client, url, opts) {
|
|
|
93
93
|
: 'mqtt'
|
|
94
94
|
|
|
95
95
|
debug('creating new Websocket for url: ' + url + ' and protocol: ' + websocketSubProtocol)
|
|
96
|
-
|
|
96
|
+
const socket = new WS(url, [websocketSubProtocol], opts.wsOptions)
|
|
97
97
|
return socket
|
|
98
98
|
}
|
|
99
99
|
|
|
@@ -103,27 +103,28 @@ function createBrowserWebSocket (client, opts) {
|
|
|
103
103
|
? 'mqttv3.1'
|
|
104
104
|
: 'mqtt'
|
|
105
105
|
|
|
106
|
-
|
|
106
|
+
const url = buildUrl(opts, client)
|
|
107
107
|
/* global WebSocket */
|
|
108
|
-
|
|
108
|
+
const socket = new WebSocket(url, [websocketSubProtocol])
|
|
109
109
|
socket.binaryType = 'arraybuffer'
|
|
110
110
|
return socket
|
|
111
111
|
}
|
|
112
112
|
|
|
113
113
|
function streamBuilder (client, opts) {
|
|
114
114
|
debug('streamBuilder')
|
|
115
|
-
|
|
115
|
+
const options = setDefaultOpts(opts)
|
|
116
116
|
const url = buildUrl(options, client)
|
|
117
|
-
|
|
118
|
-
|
|
117
|
+
const socket = createWebSocket(client, url, options)
|
|
118
|
+
const webSocketStream = WS.createWebSocketStream(socket, options.wsOptions)
|
|
119
119
|
webSocketStream.url = url
|
|
120
|
+
socket.on('close', () => { webSocketStream.destroy() })
|
|
120
121
|
return webSocketStream
|
|
121
122
|
}
|
|
122
123
|
|
|
123
124
|
function browserStreamBuilder (client, opts) {
|
|
124
125
|
debug('browserStreamBuilder')
|
|
125
126
|
let stream
|
|
126
|
-
|
|
127
|
+
const options = setDefaultBrowserOpts(opts)
|
|
127
128
|
// sets the maximum socket buffer size before throttling
|
|
128
129
|
const bufferSize = options.browserBufferSize || 1024 * 512
|
|
129
130
|
|
|
@@ -131,16 +132,16 @@ function browserStreamBuilder (client, opts) {
|
|
|
131
132
|
|
|
132
133
|
const coerceToBuffer = !opts.objectMode
|
|
133
134
|
|
|
134
|
-
|
|
135
|
+
const socket = createBrowserWebSocket(client, opts)
|
|
135
136
|
|
|
136
|
-
|
|
137
|
+
const proxy = buildProxy(opts, socketWriteBrowser, socketEndBrowser)
|
|
137
138
|
|
|
138
139
|
if (!opts.objectMode) {
|
|
139
140
|
proxy._writev = writev
|
|
140
141
|
}
|
|
141
142
|
proxy.on('close', () => { socket.close() })
|
|
142
143
|
|
|
143
|
-
const eventListenerSupport = (typeof socket.addEventListener
|
|
144
|
+
const eventListenerSupport = (typeof socket.addEventListener !== 'undefined')
|
|
144
145
|
|
|
145
146
|
// was already open when passed in
|
|
146
147
|
if (socket.readyState === socket.OPEN) {
|
|
@@ -173,7 +174,7 @@ function browserStreamBuilder (client, opts) {
|
|
|
173
174
|
// methods for browserStreamBuilder
|
|
174
175
|
|
|
175
176
|
function buildProxy (options, socketWrite, socketEnd) {
|
|
176
|
-
|
|
177
|
+
const proxy = new Transform({
|
|
177
178
|
objectModeMode: options.objectMode
|
|
178
179
|
})
|
|
179
180
|
|
package/lib/connect/wx.js
CHANGED
|
@@ -1,15 +1,13 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
const Transform = require('readable-stream').Transform
|
|
4
|
+
const duplexify = require('duplexify')
|
|
5
5
|
|
|
6
6
|
/* global wx */
|
|
7
|
-
|
|
8
|
-
var proxy
|
|
9
|
-
var stream
|
|
7
|
+
let socketTask, proxy, stream
|
|
10
8
|
|
|
11
9
|
function buildProxy () {
|
|
12
|
-
|
|
10
|
+
const proxy = new Transform()
|
|
13
11
|
proxy._write = function (chunk, encoding, next) {
|
|
14
12
|
socketTask.send({
|
|
15
13
|
data: chunk.buffer,
|
|
@@ -46,8 +44,8 @@ function setDefaultOpts (opts) {
|
|
|
46
44
|
}
|
|
47
45
|
|
|
48
46
|
function buildUrl (opts, client) {
|
|
49
|
-
|
|
50
|
-
|
|
47
|
+
const protocol = opts.protocol === 'wxs' ? 'wss' : 'ws'
|
|
48
|
+
let url = protocol + '://' + opts.hostname + opts.path
|
|
51
49
|
if (opts.port && opts.port !== 80 && opts.port !== 443) {
|
|
52
50
|
url = protocol + '://' + opts.hostname + ':' + opts.port + opts.path
|
|
53
51
|
}
|
|
@@ -65,7 +63,7 @@ function bindEventHandler () {
|
|
|
65
63
|
})
|
|
66
64
|
|
|
67
65
|
socketTask.onMessage(function (res) {
|
|
68
|
-
|
|
66
|
+
let data = res.data
|
|
69
67
|
|
|
70
68
|
if (data instanceof ArrayBuffer) data = Buffer.from(data)
|
|
71
69
|
else data = Buffer.from(data, 'utf8')
|
|
@@ -89,14 +87,14 @@ function buildStream (client, opts) {
|
|
|
89
87
|
throw new Error('Could not determine host. Specify host manually.')
|
|
90
88
|
}
|
|
91
89
|
|
|
92
|
-
|
|
90
|
+
const websocketSubProtocol =
|
|
93
91
|
(opts.protocolId === 'MQIsdp') && (opts.protocolVersion === 3)
|
|
94
92
|
? 'mqttv3.1'
|
|
95
93
|
: 'mqtt'
|
|
96
94
|
|
|
97
95
|
setDefaultOpts(opts)
|
|
98
96
|
|
|
99
|
-
|
|
97
|
+
const url = buildUrl(opts, client)
|
|
100
98
|
socketTask = wx.connectSocket({
|
|
101
99
|
url: url,
|
|
102
100
|
protocols: [websocketSubProtocol]
|
|
@@ -112,11 +110,11 @@ function buildStream (client, opts) {
|
|
|
112
110
|
})
|
|
113
111
|
}
|
|
114
112
|
|
|
115
|
-
|
|
113
|
+
const destroyRef = stream.destroy
|
|
116
114
|
stream.destroy = function () {
|
|
117
115
|
stream.destroy = destroyRef
|
|
118
116
|
|
|
119
|
-
|
|
117
|
+
const self = this
|
|
120
118
|
setTimeout(function () {
|
|
121
119
|
socketTask.close({
|
|
122
120
|
fail: function () {
|
|
@@ -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
|
@@ -3,11 +3,11 @@
|
|
|
3
3
|
/**
|
|
4
4
|
* Module dependencies
|
|
5
5
|
*/
|
|
6
|
-
|
|
6
|
+
const xtend = require('xtend')
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
const Readable = require('readable-stream').Readable
|
|
9
|
+
const streamsOpts = { objectMode: true }
|
|
10
|
+
const defaultStoreOptions = {
|
|
11
11
|
clean: true
|
|
12
12
|
}
|
|
13
13
|
|
|
@@ -50,10 +50,10 @@ Store.prototype.put = function (packet, cb) {
|
|
|
50
50
|
*
|
|
51
51
|
*/
|
|
52
52
|
Store.prototype.createStream = function () {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
53
|
+
const stream = new Readable(streamsOpts)
|
|
54
|
+
const values = []
|
|
55
|
+
let destroyed = false
|
|
56
|
+
let i = 0
|
|
57
57
|
|
|
58
58
|
this._inflights.forEach(function (value, key) {
|
|
59
59
|
values.push(value)
|
|
@@ -72,7 +72,7 @@ Store.prototype.createStream = function () {
|
|
|
72
72
|
return
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
-
|
|
75
|
+
const self = this
|
|
76
76
|
|
|
77
77
|
destroyed = true
|
|
78
78
|
|
|
@@ -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
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Module dependencies
|
|
5
|
+
*/
|
|
6
|
+
const LruMap = require('collections/lru-map')
|
|
7
|
+
const NumberAllocator = require('number-allocator').NumberAllocator
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Topic Alias sending manager
|
|
11
|
+
* This holds both topic to alias and alias to topic map
|
|
12
|
+
* @param {Number} [max] - topic alias maximum entries
|
|
13
|
+
*/
|
|
14
|
+
function TopicAliasSend (max) {
|
|
15
|
+
if (!(this instanceof TopicAliasSend)) {
|
|
16
|
+
return new TopicAliasSend(max)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (max > 0) {
|
|
20
|
+
this.aliasToTopic = new LruMap()
|
|
21
|
+
this.topicToAlias = {}
|
|
22
|
+
this.numberAllocator = new NumberAllocator(1, max)
|
|
23
|
+
this.max = max
|
|
24
|
+
this.length = 0
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Insert or update topic - alias entry.
|
|
30
|
+
* @param {String} [topic] - topic
|
|
31
|
+
* @param {Number} [alias] - topic alias
|
|
32
|
+
* @returns {Boolean} - if success return true otherwise false
|
|
33
|
+
*/
|
|
34
|
+
TopicAliasSend.prototype.put = function (topic, alias) {
|
|
35
|
+
if (alias === 0 || alias > this.max) {
|
|
36
|
+
return false
|
|
37
|
+
}
|
|
38
|
+
const entry = this.aliasToTopic.get(alias)
|
|
39
|
+
if (entry) {
|
|
40
|
+
delete this.topicToAlias[entry.topic]
|
|
41
|
+
}
|
|
42
|
+
this.aliasToTopic.set(alias, { topic: topic, alias: alias })
|
|
43
|
+
this.topicToAlias[topic] = alias
|
|
44
|
+
this.numberAllocator.use(alias)
|
|
45
|
+
this.length = this.aliasToTopic.length
|
|
46
|
+
return true
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Get topic by alias
|
|
51
|
+
* @param {Number} [alias] - topic alias
|
|
52
|
+
* @returns {String} - if mapped topic exists return topic, otherwise return undefined
|
|
53
|
+
*/
|
|
54
|
+
TopicAliasSend.prototype.getTopicByAlias = function (alias) {
|
|
55
|
+
const entry = this.aliasToTopic.get(alias)
|
|
56
|
+
if (typeof entry === 'undefined') return entry
|
|
57
|
+
return entry.topic
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Get topic by alias
|
|
62
|
+
* @param {String} [topic] - topic
|
|
63
|
+
* @returns {Number} - if mapped topic exists return topic alias, otherwise return undefined
|
|
64
|
+
*/
|
|
65
|
+
TopicAliasSend.prototype.getAliasByTopic = function (topic) {
|
|
66
|
+
const alias = this.topicToAlias[topic]
|
|
67
|
+
if (typeof alias !== 'undefined') {
|
|
68
|
+
this.aliasToTopic.get(alias) // LRU update
|
|
69
|
+
}
|
|
70
|
+
return alias
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Clear all entries
|
|
75
|
+
*/
|
|
76
|
+
TopicAliasSend.prototype.clear = function () {
|
|
77
|
+
this.aliasToTopic.clear()
|
|
78
|
+
this.topicToAlias = {}
|
|
79
|
+
this.numberAllocator.clear()
|
|
80
|
+
this.length = 0
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Get Least Recently Used (LRU) topic alias
|
|
85
|
+
* @returns {Number} - if vacant alias exists then return it, otherwise then return LRU alias
|
|
86
|
+
*/
|
|
87
|
+
TopicAliasSend.prototype.getLruAlias = function () {
|
|
88
|
+
const alias = this.numberAllocator.firstVacant()
|
|
89
|
+
if (alias) return alias
|
|
90
|
+
return this.aliasToTopic.min().alias
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
module.exports = TopicAliasSend
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const NumberAllocator = require('number-allocator').NumberAllocator
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* UniqueMessageAllocator constructor
|
|
7
|
+
* @constructor
|
|
8
|
+
*/
|
|
9
|
+
function UniqueMessageIdProvider () {
|
|
10
|
+
if (!(this instanceof UniqueMessageIdProvider)) {
|
|
11
|
+
return new UniqueMessageIdProvider()
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
this.numberAllocator = new NumberAllocator(1, 65535)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* allocate
|
|
19
|
+
*
|
|
20
|
+
* Get the next messageId.
|
|
21
|
+
* @return if messageId is fully allocated then return null,
|
|
22
|
+
* otherwise return the smallest usable unsigned int messageId.
|
|
23
|
+
*/
|
|
24
|
+
UniqueMessageIdProvider.prototype.allocate = function () {
|
|
25
|
+
this.lastId = this.numberAllocator.alloc()
|
|
26
|
+
return this.lastId
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* getLastAllocated
|
|
31
|
+
* Get the last allocated messageId.
|
|
32
|
+
* @return unsigned int
|
|
33
|
+
*/
|
|
34
|
+
UniqueMessageIdProvider.prototype.getLastAllocated = function () {
|
|
35
|
+
return this.lastId
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* register
|
|
40
|
+
* Register messageId. If success return true, otherwise return false.
|
|
41
|
+
* @param { unsigned int } - messageId to register,
|
|
42
|
+
* @return boolean
|
|
43
|
+
*/
|
|
44
|
+
UniqueMessageIdProvider.prototype.register = function (messageId) {
|
|
45
|
+
return this.numberAllocator.use(messageId)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* deallocate
|
|
50
|
+
* Deallocate messageId.
|
|
51
|
+
* @param { unsigned int } - messageId to deallocate,
|
|
52
|
+
*/
|
|
53
|
+
UniqueMessageIdProvider.prototype.deallocate = function (messageId) {
|
|
54
|
+
this.numberAllocator.free(messageId)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* clear
|
|
59
|
+
* Deallocate all messageIds.
|
|
60
|
+
*/
|
|
61
|
+
UniqueMessageIdProvider.prototype.clear = function () {
|
|
62
|
+
this.numberAllocator.clear()
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
module.exports = UniqueMessageIdProvider
|
package/lib/validations.js
CHANGED
|
@@ -10,9 +10,9 @@
|
|
|
10
10
|
* @returns {Boolean} If the topic is valid, returns true. Otherwise, returns false.
|
|
11
11
|
*/
|
|
12
12
|
function validateTopic (topic) {
|
|
13
|
-
|
|
13
|
+
const parts = topic.split('/')
|
|
14
14
|
|
|
15
|
-
for (
|
|
15
|
+
for (let i = 0; i < parts.length; i++) {
|
|
16
16
|
if (parts[i] === '+') {
|
|
17
17
|
continue
|
|
18
18
|
}
|
|
@@ -39,7 +39,7 @@ function validateTopics (topics) {
|
|
|
39
39
|
if (topics.length === 0) {
|
|
40
40
|
return 'empty_topic_list'
|
|
41
41
|
}
|
|
42
|
-
for (
|
|
42
|
+
for (let i = 0; i < topics.length; i++) {
|
|
43
43
|
if (!validateTopic(topics[i])) {
|
|
44
44
|
return topics[i]
|
|
45
45
|
}
|