diff --git a/lib/topic-alias-send.js b/lib/topic-alias-send.js index c769d0572..b7a0250e0 100644 --- a/lib/topic-alias-send.js +++ b/lib/topic-alias-send.js @@ -3,7 +3,7 @@ /** * Module dependencies */ -const LruMap = require('collections/lru-map') +const LruMap = require('lru-cache') const NumberAllocator = require('number-allocator').NumberAllocator /** @@ -17,7 +17,7 @@ function TopicAliasSend (max) { } if (max > 0) { - this.aliasToTopic = new LruMap() + this.aliasToTopic = new LruMap({ max: max }) this.topicToAlias = {} this.numberAllocator = new NumberAllocator(1, max) this.max = max @@ -37,9 +37,9 @@ TopicAliasSend.prototype.put = function (topic, alias) { } const entry = this.aliasToTopic.get(alias) if (entry) { - delete this.topicToAlias[entry.topic] + delete this.topicToAlias[entry] } - this.aliasToTopic.set(alias, { topic: topic, alias: alias }) + this.aliasToTopic.set(alias, topic) this.topicToAlias[topic] = alias this.numberAllocator.use(alias) this.length = this.aliasToTopic.length @@ -52,9 +52,7 @@ TopicAliasSend.prototype.put = function (topic, alias) { * @returns {String} - if mapped topic exists return topic, otherwise return undefined */ TopicAliasSend.prototype.getTopicByAlias = function (alias) { - const entry = this.aliasToTopic.get(alias) - if (typeof entry === 'undefined') return entry - return entry.topic + return this.aliasToTopic.get(alias) } /** @@ -74,7 +72,7 @@ TopicAliasSend.prototype.getAliasByTopic = function (topic) { * Clear all entries */ TopicAliasSend.prototype.clear = function () { - this.aliasToTopic.clear() + this.aliasToTopic.reset() this.topicToAlias = {} this.numberAllocator.clear() this.length = 0 @@ -87,7 +85,7 @@ TopicAliasSend.prototype.clear = function () { TopicAliasSend.prototype.getLruAlias = function () { const alias = this.numberAllocator.firstVacant() if (alias) return alias - return this.aliasToTopic.min().alias + return this.aliasToTopic.keys()[this.aliasToTopic.length - 1] } module.exports = TopicAliasSend diff --git a/package.json b/package.json index 68fc30df4..3d4826f02 100644 --- a/package.json +++ b/package.json @@ -62,13 +62,13 @@ "net": false }, "dependencies": { - "collections": "^5.1.13", "commist": "^1.0.0", "concat-stream": "^2.0.0", "debug": "^4.1.1", "duplexify": "^4.1.1", "help-me": "^3.0.0", "inherits": "^2.0.3", + "lru-cache": "^6.0.0", "minimist": "^1.2.5", "mqtt-packet": "^6.8.0", "number-allocator": "^1.0.9",