mqtt 5.13.1 → 5.13.2

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/src/lib/store.ts DELETED
@@ -1,166 +0,0 @@
1
- /**
2
- * Module dependencies
3
- */
4
- import { Readable } from 'readable-stream'
5
- import { type Packet } from 'mqtt-packet'
6
- import { type DoneCallback } from './shared'
7
-
8
- const streamsOpts = { objectMode: true }
9
- const defaultStoreOptions = {
10
- clean: true,
11
- }
12
-
13
- export interface IStoreOptions {
14
- /**
15
- * true, clear _inflights at close
16
- */
17
- clean?: boolean
18
- }
19
-
20
- export type PacketCallback = (error?: Error, packet?: Packet) => void
21
-
22
- export interface IStore {
23
- /**
24
- * Adds a packet to the store, a packet is
25
- * anything that has a messageId property.
26
- *
27
- */
28
- put(packet: Packet, cb: DoneCallback): IStore
29
-
30
- /**
31
- * Creates a stream with all the packets in the store
32
- *
33
- */
34
- createStream(): Readable
35
-
36
- /**
37
- * deletes a packet from the store.
38
- */
39
- del(packet: Pick<Packet, 'messageId'>, cb: PacketCallback): IStore
40
-
41
- /**
42
- * get a packet from the store.
43
- */
44
- get(packet: Pick<Packet, 'messageId'>, cb: PacketCallback): IStore
45
-
46
- /**
47
- * Close the store
48
- */
49
- close(cb: DoneCallback): void
50
- }
51
-
52
- /**
53
- * In-memory implementation of the message store
54
- * This can actually be saved into files.
55
- *
56
- * @param {Object} [options] - store options
57
- */
58
- export default class Store implements IStore {
59
- private options: IStoreOptions
60
-
61
- private _inflights: Map<number, Packet>
62
-
63
- constructor(options?: IStoreOptions) {
64
- this.options = options || {}
65
-
66
- // Defaults
67
- this.options = { ...defaultStoreOptions, ...options }
68
-
69
- this._inflights = new Map()
70
- }
71
-
72
- /**
73
- * Adds a packet to the store, a packet is
74
- * anything that has a messageId property.
75
- *
76
- */
77
- put(packet: Packet, cb: DoneCallback) {
78
- this._inflights.set(packet.messageId, packet)
79
-
80
- if (cb) {
81
- cb()
82
- }
83
-
84
- return this
85
- }
86
-
87
- /**
88
- * Creates a stream with all the packets in the store
89
- *
90
- */
91
- createStream() {
92
- const stream = new Readable(streamsOpts)
93
- const values = []
94
- let destroyed = false
95
- let i = 0
96
-
97
- this._inflights.forEach((value, key) => {
98
- values.push(value)
99
- })
100
-
101
- stream._read = () => {
102
- if (!destroyed && i < values.length) {
103
- stream.push(values[i++])
104
- } else {
105
- stream.push(null)
106
- }
107
- }
108
-
109
- stream.destroy = (err) => {
110
- if (destroyed) {
111
- return
112
- }
113
-
114
- destroyed = true
115
-
116
- setTimeout(() => {
117
- stream.emit('close')
118
- }, 0)
119
-
120
- return stream
121
- }
122
-
123
- return stream
124
- }
125
-
126
- /**
127
- * deletes a packet from the store.
128
- */
129
- del(packet: Pick<Packet, 'messageId'>, cb: PacketCallback) {
130
- const toDelete = this._inflights.get(packet.messageId)
131
- if (toDelete) {
132
- this._inflights.delete(packet.messageId)
133
- cb(null, toDelete)
134
- } else if (cb) {
135
- cb(new Error('missing packet'))
136
- }
137
-
138
- return this
139
- }
140
-
141
- /**
142
- * get a packet from the store.
143
- */
144
- get(packet: Pick<Packet, 'messageId'>, cb: PacketCallback) {
145
- const storedPacket = this._inflights.get(packet.messageId)
146
- if (storedPacket) {
147
- cb(null, storedPacket)
148
- } else if (cb) {
149
- cb(new Error('missing packet'))
150
- }
151
-
152
- return this
153
- }
154
-
155
- /**
156
- * Close the store
157
- */
158
- close(cb: DoneCallback) {
159
- if (this.options.clean) {
160
- this._inflights = null
161
- }
162
- if (cb) {
163
- cb()
164
- }
165
- }
166
- }
@@ -1,48 +0,0 @@
1
- /**
2
- * Topic Alias receiving manager
3
- * This holds alias to topic map
4
- * @param {Number} [max] - topic alias maximum entries
5
- */
6
- export default class TopicAliasRecv {
7
- private aliasToTopic: Record<number, string>
8
-
9
- public max: number
10
-
11
- public length: number
12
-
13
- constructor(max: number) {
14
- this.aliasToTopic = {}
15
- this.max = max
16
- }
17
-
18
- /**
19
- * Insert or update topic - alias entry.
20
- * @param {String} [topic] - topic
21
- * @param {Number} [alias] - topic alias
22
- * @returns {Boolean} - if success return true otherwise false
23
- */
24
- put(topic: string, alias: number): boolean {
25
- if (alias === 0 || alias > this.max) {
26
- return false
27
- }
28
- this.aliasToTopic[alias] = topic
29
- this.length = Object.keys(this.aliasToTopic).length
30
- return true
31
- }
32
-
33
- /**
34
- * Get topic by alias
35
- * @param {String} [topic] - topic
36
- * @returns {Number} - if mapped topic exists return topic alias, otherwise return undefined
37
- */
38
- getTopicByAlias(alias: number): string {
39
- return this.aliasToTopic[alias]
40
- }
41
-
42
- /**
43
- * Clear all entries
44
- */
45
- clear() {
46
- this.aliasToTopic = {}
47
- }
48
- }
@@ -1,96 +0,0 @@
1
- /**
2
- * Module dependencies
3
- */
4
- import { LRUCache } from 'lru-cache'
5
- import { NumberAllocator } from 'number-allocator'
6
-
7
- /**
8
- * Topic Alias sending manager
9
- * This holds both topic to alias and alias to topic map
10
- * @param {Number} [max] - topic alias maximum entries
11
- */
12
- export default class TopicAliasSend {
13
- private aliasToTopic: LRUCache<number, string>
14
-
15
- private topicToAlias: Record<string, number>
16
-
17
- private max: number
18
-
19
- private numberAllocator: NumberAllocator
20
-
21
- public length: number
22
-
23
- constructor(max: number) {
24
- if (max > 0) {
25
- this.aliasToTopic = new LRUCache<number, string>({ max })
26
- this.topicToAlias = {}
27
- this.numberAllocator = new NumberAllocator(1, max)
28
- this.max = max
29
- this.length = 0
30
- }
31
- }
32
-
33
- /**
34
- * Insert or update topic - alias entry.
35
- * @param {String} [topic] - topic
36
- * @param {Number} [alias] - topic alias
37
- * @returns {Boolean} - if success return true otherwise false
38
- */
39
- put(topic: string, alias: number): boolean {
40
- if (alias === 0 || alias > this.max) {
41
- return false
42
- }
43
- const entry = this.aliasToTopic.get(alias)
44
- if (entry) {
45
- delete this.topicToAlias[entry]
46
- }
47
- this.aliasToTopic.set(alias, topic)
48
- this.topicToAlias[topic] = alias
49
- this.numberAllocator.use(alias)
50
- this.length = this.aliasToTopic.size
51
- return true
52
- }
53
-
54
- /**
55
- * Get topic by alias
56
- * @param {Number} [alias] - topic alias
57
- * @returns {String} - if mapped topic exists return topic, otherwise return undefined
58
- */
59
- getTopicByAlias(alias: number): string {
60
- return this.aliasToTopic.get(alias)
61
- }
62
-
63
- /**
64
- * Get topic by alias
65
- * @param {String} [topic] - topic
66
- * @returns {Number} - if mapped topic exists return topic alias, otherwise return undefined
67
- */
68
- getAliasByTopic(topic: string): number | undefined {
69
- const alias = this.topicToAlias[topic]
70
- if (typeof alias !== 'undefined') {
71
- this.aliasToTopic.get(alias) // LRU update
72
- }
73
- return alias
74
- }
75
-
76
- /**
77
- * Clear all entries
78
- */
79
- clear() {
80
- this.aliasToTopic.clear()
81
- this.topicToAlias = {}
82
- this.numberAllocator.clear()
83
- this.length = 0
84
- }
85
-
86
- /**
87
- * Get Least Recently Used (LRU) topic alias
88
- * @returns {Number} - if vacant alias exists then return it, otherwise then return LRU alias
89
- */
90
- getLruAlias(): number {
91
- const alias = this.numberAllocator.firstVacant()
92
- if (alias) return alias
93
- // get last alias (key) from LRU cache
94
- return [...this.aliasToTopic.keys()][this.aliasToTopic.size - 1]
95
- }
96
- }
@@ -1,64 +0,0 @@
1
- import { NumberAllocator } from 'number-allocator'
2
- import { type IMessageIdProvider } from './default-message-id-provider'
3
-
4
- /**
5
- * UniqueMessageAllocator constructor
6
- * @constructor
7
- */
8
- export default class UniqueMessageIdProvider implements IMessageIdProvider {
9
- private numberAllocator: NumberAllocator
10
-
11
- private lastId: number
12
-
13
- constructor() {
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
- allocate() {
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
- getLastAllocated() {
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
- register(messageId: number) {
45
- return this.numberAllocator.use(messageId) as boolean
46
- }
47
-
48
- /**
49
- * deallocate
50
- * Deallocate messageId.
51
- * @param { unsigned int } - messageId to deallocate,
52
- */
53
- deallocate(messageId: number) {
54
- this.numberAllocator.free(messageId)
55
- }
56
-
57
- /**
58
- * clear
59
- * Deallocate all messageIds.
60
- */
61
- clear() {
62
- this.numberAllocator.clear()
63
- }
64
- }
@@ -1,46 +0,0 @@
1
- /**
2
- * Validate a topic to see if it's valid or not.
3
- * A topic is valid if it follow below rules:
4
- * - Rule #1: If any part of the topic is not `+` or `#`, then it must not contain `+` and '#'
5
- * - Rule #2: Part `#` must be located at the end of the mailbox
6
- *
7
- * @param {String} topic - A topic
8
- * @returns {Boolean} If the topic is valid, returns true. Otherwise, returns false.
9
- */
10
- export function validateTopic(topic: string): boolean {
11
- const parts = topic.split('/')
12
-
13
- for (let i = 0; i < parts.length; i++) {
14
- if (parts[i] === '+') {
15
- continue
16
- }
17
-
18
- if (parts[i] === '#') {
19
- // for Rule #2
20
- return i === parts.length - 1
21
- }
22
-
23
- if (parts[i].indexOf('+') !== -1 || parts[i].indexOf('#') !== -1) {
24
- return false
25
- }
26
- }
27
-
28
- return true
29
- }
30
-
31
- /**
32
- * Validate an array of topics to see if any of them is valid or not
33
- * @param {Array} topics - Array of topics
34
- * @returns {String} If the topics is valid, returns null. Otherwise, returns the invalid one
35
- */
36
- export function validateTopics(topics: string[]): string | null {
37
- if (topics.length === 0) {
38
- return 'empty_topic_list'
39
- }
40
- for (let i = 0; i < topics.length; i++) {
41
- if (!validateTopic(topics[i])) {
42
- return topics[i]
43
- }
44
- }
45
- return null
46
- }
package/src/mqtt.ts DELETED
@@ -1,28 +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
- import MqttClient from './lib/client'
8
- import DefaultMessageIdProvider from './lib/default-message-id-provider'
9
- import UniqueMessageIdProvider from './lib/unique-message-id-provider'
10
- import Store, { IStore } from './lib/store'
11
- import connect, { connectAsync } from './lib/connect'
12
- import KeepaliveManager from './lib/KeepaliveManager'
13
-
14
- export const Client = MqttClient
15
- export {
16
- connect,
17
- connectAsync,
18
- MqttClient,
19
- Store,
20
- DefaultMessageIdProvider,
21
- UniqueMessageIdProvider,
22
- IStore,
23
- KeepaliveManager,
24
- }
25
- export * from './lib/client'
26
- export * from './lib/shared'
27
- export { ReasonCodes } from './lib/handlers/ack'
28
- export type { Timer } from './lib/get-timer'