mqtt 5.0.0-beta.2 → 5.0.0-beta.4
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 +48 -28
- 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 +266 -0
- package/build/src/lib/client.js +1147 -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 +5 -0
- package/build/src/lib/connect/index.js +134 -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 +17 -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 +10 -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 +8 -0
- package/build/src/mqtt.js +33 -0
- package/build/src/mqtt.js.map +1 -0
- package/build/tsconfig.build.tsbuildinfo +1 -0
- package/dist/mqtt.js +4987 -5249
- package/dist/mqtt.min.js +4 -4
- package/package.json +84 -66
- package/src/bin/mqtt.ts +32 -0
- package/src/bin/pub.ts +171 -0
- package/src/bin/sub.ts +146 -0
- package/src/lib/TypedEmitter.ts +45 -0
- package/src/lib/client.ts +2279 -0
- package/src/lib/connect/ali.ts +128 -0
- package/src/lib/connect/index.ts +180 -0
- package/src/lib/connect/tcp.ts +23 -0
- package/src/lib/connect/tls.ts +56 -0
- package/src/lib/connect/ws.ts +282 -0
- package/src/lib/connect/wx.ts +141 -0
- package/src/lib/default-message-id-provider.ts +99 -0
- package/src/lib/handlers/ack.ts +151 -0
- package/src/lib/handlers/auth.ts +44 -0
- package/src/lib/handlers/connack.ts +55 -0
- package/src/lib/handlers/index.ts +67 -0
- package/src/lib/handlers/publish.ts +183 -0
- package/src/lib/handlers/pubrel.ts +27 -0
- package/src/lib/is-browser.ts +12 -0
- package/src/lib/shared.ts +73 -0
- package/src/lib/store.ts +166 -0
- package/src/lib/topic-alias-recv.ts +48 -0
- package/src/lib/topic-alias-send.ts +96 -0
- package/src/lib/unique-message-id-provider.ts +63 -0
- package/src/lib/validations.ts +46 -0
- package/src/mqtt.ts +23 -0
- package/bin/mqtt.js +0 -27
- package/bin/pub.js +0 -146
- package/bin/sub.js +0 -123
- package/lib/client.js +0 -1918
- package/lib/connect/ali.js +0 -129
- package/lib/connect/index.js +0 -169
- package/lib/connect/tcp.js +0 -20
- package/lib/connect/tls.js +0 -48
- package/lib/connect/ws.js +0 -257
- package/lib/connect/wx.js +0 -133
- package/lib/default-message-id-provider.js +0 -69
- package/lib/is-browser.js +0 -11
- package/lib/store.js +0 -128
- package/lib/topic-alias-recv.js +0 -47
- package/lib/topic-alias-send.js +0 -91
- package/lib/unique-message-id-provider.js +0 -65
- package/lib/validations.js +0 -52
- package/mqtt.js +0 -21
- package/types/index.d.ts +0 -30
- package/types/lib/client-options.d.ts +0 -208
- package/types/lib/client.d.ts +0 -261
- 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
package/lib/connect/wx.js
DELETED
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const { Buffer } = require('buffer')
|
|
4
|
-
const Transform = require('readable-stream').Transform
|
|
5
|
-
const duplexify = require('duplexify')
|
|
6
|
-
|
|
7
|
-
/* global wx */
|
|
8
|
-
let socketTask, proxy, stream
|
|
9
|
-
|
|
10
|
-
function buildProxy () {
|
|
11
|
-
const proxy = new Transform()
|
|
12
|
-
proxy._write = function (chunk, encoding, next) {
|
|
13
|
-
socketTask.send({
|
|
14
|
-
data: chunk.buffer,
|
|
15
|
-
success: function () {
|
|
16
|
-
next()
|
|
17
|
-
},
|
|
18
|
-
fail: function (errMsg) {
|
|
19
|
-
next(new Error(errMsg))
|
|
20
|
-
}
|
|
21
|
-
})
|
|
22
|
-
}
|
|
23
|
-
proxy._flush = function socketEnd (done) {
|
|
24
|
-
socketTask.close({
|
|
25
|
-
success: function () {
|
|
26
|
-
done()
|
|
27
|
-
}
|
|
28
|
-
})
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
return proxy
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
function setDefaultOpts (opts) {
|
|
35
|
-
if (!opts.hostname) {
|
|
36
|
-
opts.hostname = 'localhost'
|
|
37
|
-
}
|
|
38
|
-
if (!opts.path) {
|
|
39
|
-
opts.path = '/'
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
if (!opts.wsOptions) {
|
|
43
|
-
opts.wsOptions = {}
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function buildUrl (opts, client) {
|
|
48
|
-
const protocol = opts.protocol === 'wxs' ? 'wss' : 'ws'
|
|
49
|
-
let url = protocol + '://' + opts.hostname + opts.path
|
|
50
|
-
if (opts.port && opts.port !== 80 && opts.port !== 443) {
|
|
51
|
-
url = protocol + '://' + opts.hostname + ':' + opts.port + opts.path
|
|
52
|
-
}
|
|
53
|
-
if (typeof (opts.transformWsUrl) === 'function') {
|
|
54
|
-
url = opts.transformWsUrl(url, opts, client)
|
|
55
|
-
}
|
|
56
|
-
return url
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
function bindEventHandler () {
|
|
60
|
-
socketTask.onOpen(function () {
|
|
61
|
-
stream.setReadable(proxy)
|
|
62
|
-
stream.setWritable(proxy)
|
|
63
|
-
stream.emit('connect')
|
|
64
|
-
})
|
|
65
|
-
|
|
66
|
-
socketTask.onMessage(function (res) {
|
|
67
|
-
let data = res.data
|
|
68
|
-
|
|
69
|
-
if (data instanceof ArrayBuffer) data = Buffer.from(data)
|
|
70
|
-
else data = Buffer.from(data, 'utf8')
|
|
71
|
-
proxy.push(data)
|
|
72
|
-
})
|
|
73
|
-
|
|
74
|
-
socketTask.onClose(function () {
|
|
75
|
-
stream.end()
|
|
76
|
-
stream.destroy()
|
|
77
|
-
})
|
|
78
|
-
|
|
79
|
-
socketTask.onError(function (res) {
|
|
80
|
-
stream.destroy(new Error(res.errMsg))
|
|
81
|
-
})
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
function buildStream (client, opts) {
|
|
85
|
-
opts.hostname = opts.hostname || opts.host
|
|
86
|
-
|
|
87
|
-
if (!opts.hostname) {
|
|
88
|
-
throw new Error('Could not determine host. Specify host manually.')
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
const websocketSubProtocol =
|
|
92
|
-
(opts.protocolId === 'MQIsdp') && (opts.protocolVersion === 3)
|
|
93
|
-
? 'mqttv3.1'
|
|
94
|
-
: 'mqtt'
|
|
95
|
-
|
|
96
|
-
setDefaultOpts(opts)
|
|
97
|
-
|
|
98
|
-
const url = buildUrl(opts, client)
|
|
99
|
-
socketTask = wx.connectSocket({
|
|
100
|
-
url: url,
|
|
101
|
-
protocols: [websocketSubProtocol]
|
|
102
|
-
})
|
|
103
|
-
|
|
104
|
-
proxy = buildProxy()
|
|
105
|
-
stream = duplexify.obj()
|
|
106
|
-
stream._destroy = function (err, cb) {
|
|
107
|
-
socketTask.close({
|
|
108
|
-
success: function () {
|
|
109
|
-
cb && cb(err)
|
|
110
|
-
}
|
|
111
|
-
})
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
const destroyRef = stream.destroy
|
|
115
|
-
stream.destroy = function () {
|
|
116
|
-
stream.destroy = destroyRef
|
|
117
|
-
|
|
118
|
-
const self = this
|
|
119
|
-
setTimeout(function () {
|
|
120
|
-
socketTask.close({
|
|
121
|
-
fail: function () {
|
|
122
|
-
self._destroy(new Error())
|
|
123
|
-
}
|
|
124
|
-
})
|
|
125
|
-
}, 0)
|
|
126
|
-
}.bind(stream)
|
|
127
|
-
|
|
128
|
-
bindEventHandler()
|
|
129
|
-
|
|
130
|
-
return stream
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
module.exports = buildStream
|
|
@@ -1,69 +0,0 @@
|
|
|
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/is-browser.js
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
const legacyIsBrowser =
|
|
2
|
-
(typeof process !== 'undefined' && process.title === 'browser') ||
|
|
3
|
-
// eslint-disable-next-line camelcase
|
|
4
|
-
typeof __webpack_require__ === 'function'
|
|
5
|
-
|
|
6
|
-
const isBrowser =
|
|
7
|
-
typeof window !== 'undefined' && typeof document !== 'undefined'
|
|
8
|
-
|
|
9
|
-
module.exports = {
|
|
10
|
-
IS_BROWSER: isBrowser || legacyIsBrowser
|
|
11
|
-
}
|
package/lib/store.js
DELETED
|
@@ -1,128 +0,0 @@
|
|
|
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
|
package/lib/topic-alias-recv.js
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
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
|
package/lib/topic-alias-send.js
DELETED
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Module dependencies
|
|
5
|
-
*/
|
|
6
|
-
const LruMap = require('lru-cache')
|
|
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({ max: max })
|
|
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]
|
|
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
|
-
}
|
|
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
|
-
return this.aliasToTopic.get(alias)
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Get topic by alias
|
|
60
|
-
* @param {String} [topic] - topic
|
|
61
|
-
* @returns {Number} - if mapped topic exists return topic alias, otherwise return undefined
|
|
62
|
-
*/
|
|
63
|
-
TopicAliasSend.prototype.getAliasByTopic = function (topic) {
|
|
64
|
-
const alias = this.topicToAlias[topic]
|
|
65
|
-
if (typeof alias !== 'undefined') {
|
|
66
|
-
this.aliasToTopic.get(alias) // LRU update
|
|
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
|
-
}
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* Get Least Recently Used (LRU) topic alias
|
|
83
|
-
* @returns {Number} - if vacant alias exists then return it, otherwise then return LRU alias
|
|
84
|
-
*/
|
|
85
|
-
TopicAliasSend.prototype.getLruAlias = function () {
|
|
86
|
-
const alias = this.numberAllocator.firstVacant()
|
|
87
|
-
if (alias) return alias
|
|
88
|
-
return this.aliasToTopic.keys()[this.aliasToTopic.length - 1]
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
module.exports = TopicAliasSend
|
|
@@ -1,65 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Validate a topic to see if it's valid or not.
|
|
5
|
-
* A topic is valid if it follow below rules:
|
|
6
|
-
* - Rule #1: If any part of the topic is not `+` or `#`, then it must not contain `+` and '#'
|
|
7
|
-
* - Rule #2: Part `#` must be located at the end of the mailbox
|
|
8
|
-
*
|
|
9
|
-
* @param {String} topic - A topic
|
|
10
|
-
* @returns {Boolean} If the topic is valid, returns true. Otherwise, returns false.
|
|
11
|
-
*/
|
|
12
|
-
function validateTopic (topic) {
|
|
13
|
-
const parts = topic.split('/')
|
|
14
|
-
|
|
15
|
-
for (let i = 0; i < parts.length; i++) {
|
|
16
|
-
if (parts[i] === '+') {
|
|
17
|
-
continue
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
if (parts[i] === '#') {
|
|
21
|
-
// for Rule #2
|
|
22
|
-
return i === parts.length - 1
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
if (parts[i].indexOf('+') !== -1 || parts[i].indexOf('#') !== -1) {
|
|
26
|
-
return false
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
return true
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Validate an array of topics to see if any of them is valid or not
|
|
35
|
-
* @param {Array} topics - Array of topics
|
|
36
|
-
* @returns {String} If the topics is valid, returns null. Otherwise, returns the invalid one
|
|
37
|
-
*/
|
|
38
|
-
function validateTopics (topics) {
|
|
39
|
-
if (topics.length === 0) {
|
|
40
|
-
return 'empty_topic_list'
|
|
41
|
-
}
|
|
42
|
-
for (let i = 0; i < topics.length; i++) {
|
|
43
|
-
if (!validateTopic(topics[i])) {
|
|
44
|
-
return topics[i]
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
return null
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
module.exports = {
|
|
51
|
-
validateTopics: validateTopics
|
|
52
|
-
}
|
package/mqtt.js
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
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
|
-
const MqttClient = require('./lib/client')
|
|
9
|
-
const connect = require('./lib/connect')
|
|
10
|
-
const Store = require('./lib/store')
|
|
11
|
-
const DefaultMessageIdProvider = require('./lib/default-message-id-provider')
|
|
12
|
-
const UniqueMessageIdProvider = require('./lib/unique-message-id-provider')
|
|
13
|
-
|
|
14
|
-
module.exports.connect = connect
|
|
15
|
-
|
|
16
|
-
// Expose MqttClient
|
|
17
|
-
module.exports.MqttClient = MqttClient
|
|
18
|
-
module.exports.Client = MqttClient
|
|
19
|
-
module.exports.Store = Store
|
|
20
|
-
module.exports.DefaultMessageIdProvider = DefaultMessageIdProvider
|
|
21
|
-
module.exports.UniqueMessageIdProvider = UniqueMessageIdProvider
|
package/types/index.d.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
export * from './lib/client'
|
|
2
|
-
export * from './lib/connect'
|
|
3
|
-
export * from './lib/store'
|
|
4
|
-
export * from './lib/client-options'
|
|
5
|
-
import { MqttClient } from './lib/client'
|
|
6
|
-
export { MqttClient as Client }
|
|
7
|
-
export {
|
|
8
|
-
QoS,
|
|
9
|
-
PacketCmd,
|
|
10
|
-
IPacket,
|
|
11
|
-
IConnectPacket,
|
|
12
|
-
IPublishPacket,
|
|
13
|
-
IConnackPacket,
|
|
14
|
-
ISubscription,
|
|
15
|
-
ISubscribePacket,
|
|
16
|
-
ISubackPacket,
|
|
17
|
-
IUnsubscribePacket,
|
|
18
|
-
IUnsubackPacket,
|
|
19
|
-
IPubackPacket,
|
|
20
|
-
IPubcompPacket,
|
|
21
|
-
IPubrelPacket,
|
|
22
|
-
IPubrecPacket,
|
|
23
|
-
IPingreqPacket,
|
|
24
|
-
IPingrespPacket,
|
|
25
|
-
IDisconnectPacket,
|
|
26
|
-
Packet,
|
|
27
|
-
UserProperties
|
|
28
|
-
} from 'mqtt-packet'
|
|
29
|
-
export { IMessageIdProvider } from './lib/message-id-provider'
|
|
30
|
-
export { UniqueMessageIdProvider } from './lib/unique-message-id-provider'
|