mqtt 5.0.0-beta.4 → 5.0.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/README.md +113 -120
- package/build/package.json +1 -1
- package/build/src/lib/client.d.ts +37 -83
- package/build/src/lib/client.js +71 -14
- package/build/src/lib/client.js.map +1 -1
- package/build/src/lib/connect/ali.js.map +1 -1
- package/build/src/lib/connect/index.d.ts +4 -0
- package/build/src/lib/connect/index.js +36 -1
- package/build/src/lib/connect/index.js.map +1 -1
- package/build/src/lib/connect/tcp.js.map +1 -1
- package/build/src/lib/connect/tls.js.map +1 -1
- package/build/src/lib/connect/ws.js +1 -1
- package/build/src/lib/connect/ws.js.map +1 -1
- package/build/src/lib/connect/wx.js.map +1 -1
- package/build/src/lib/handlers/publish.js.map +1 -1
- package/build/src/lib/shared.d.ts +5 -4
- package/build/src/lib/unique-message-id-provider.d.ts +3 -2
- package/build/src/lib/unique-message-id-provider.js.map +1 -1
- package/build/src/mqtt.d.ts +4 -2
- package/build/src/mqtt.js +18 -2
- package/build/src/mqtt.js.map +1 -1
- package/build/tsconfig.build.tsbuildinfo +1 -1
- package/dist/mqtt.js +127 -19
- package/dist/mqtt.min.js +4 -4
- package/doc/subscribe.txt +1 -1
- package/package.json +1 -1
- package/src/lib/client.ts +211 -176
- package/src/lib/connect/ali.ts +2 -2
- package/src/lib/connect/index.ts +66 -5
- package/src/lib/connect/tcp.ts +1 -1
- package/src/lib/connect/tls.ts +1 -3
- package/src/lib/connect/ws.ts +4 -5
- package/src/lib/connect/wx.ts +2 -2
- package/src/lib/default-message-id-provider.ts +2 -2
- package/src/lib/handlers/publish.ts +2 -2
- package/src/lib/shared.ts +4 -4
- package/src/lib/unique-message-id-provider.ts +3 -2
- package/src/mqtt.ts +4 -1
package/src/lib/connect/index.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-var-requires */
|
|
2
|
+
import _debug from 'debug'
|
|
2
3
|
import url from 'url'
|
|
3
|
-
import MqttClient, {
|
|
4
|
+
import MqttClient, {
|
|
5
|
+
IClientOptions,
|
|
6
|
+
MqttClientEventCallbacks,
|
|
7
|
+
MqttProtocol,
|
|
8
|
+
} from '../client'
|
|
4
9
|
import IS_BROWSER from '../is-browser'
|
|
5
|
-
import Store from '../store'
|
|
6
|
-
import DefaultMessageIdProvider from '../default-message-id-provider'
|
|
7
|
-
import UniqueMessageIdProvider from '../unique-message-id-provider'
|
|
8
|
-
import _debug from 'debug'
|
|
9
10
|
import { StreamBuilder } from '../shared'
|
|
10
11
|
|
|
11
12
|
const debug = _debug('mqttjs')
|
|
@@ -177,4 +178,64 @@ function connect(
|
|
|
177
178
|
return client
|
|
178
179
|
}
|
|
179
180
|
|
|
181
|
+
function connectAsync(brokerUrl: string): Promise<MqttClient>
|
|
182
|
+
function connectAsync(opts: IClientOptions): Promise<MqttClient>
|
|
183
|
+
function connectAsync(
|
|
184
|
+
brokerUrl: string,
|
|
185
|
+
opts?: IClientOptions,
|
|
186
|
+
): Promise<MqttClient>
|
|
187
|
+
function connectAsync(
|
|
188
|
+
brokerUrl: string | IClientOptions,
|
|
189
|
+
opts?: IClientOptions,
|
|
190
|
+
allowRetries = true,
|
|
191
|
+
): Promise<MqttClient> {
|
|
192
|
+
return new Promise((resolve, reject) => {
|
|
193
|
+
const client = connect(brokerUrl as string, opts)
|
|
194
|
+
|
|
195
|
+
const promiseResolutionListeners: Partial<MqttClientEventCallbacks> = {
|
|
196
|
+
connect: (connack) => {
|
|
197
|
+
removePromiseResolutionListeners()
|
|
198
|
+
resolve(client) // Resolve on connect
|
|
199
|
+
},
|
|
200
|
+
end: () => {
|
|
201
|
+
removePromiseResolutionListeners()
|
|
202
|
+
resolve(client) // Resolve on end
|
|
203
|
+
},
|
|
204
|
+
error: (err) => {
|
|
205
|
+
removePromiseResolutionListeners()
|
|
206
|
+
client.end()
|
|
207
|
+
reject(err) // Reject on error
|
|
208
|
+
},
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// If retries are not allowed, reject on close
|
|
212
|
+
if (allowRetries === false) {
|
|
213
|
+
promiseResolutionListeners.close = () => {
|
|
214
|
+
promiseResolutionListeners.error(
|
|
215
|
+
new Error("Couldn't connect to server"),
|
|
216
|
+
)
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// Remove listeners added to client by this promise
|
|
221
|
+
function removePromiseResolutionListeners() {
|
|
222
|
+
Object.keys(promiseResolutionListeners).forEach((eventName) => {
|
|
223
|
+
client.off(
|
|
224
|
+
eventName as keyof MqttClientEventCallbacks,
|
|
225
|
+
promiseResolutionListeners[eventName],
|
|
226
|
+
)
|
|
227
|
+
})
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// Add listeners to client
|
|
231
|
+
Object.keys(promiseResolutionListeners).forEach((eventName) => {
|
|
232
|
+
client.on(
|
|
233
|
+
eventName as keyof MqttClientEventCallbacks,
|
|
234
|
+
promiseResolutionListeners[eventName],
|
|
235
|
+
)
|
|
236
|
+
})
|
|
237
|
+
})
|
|
238
|
+
}
|
|
239
|
+
|
|
180
240
|
export default connect
|
|
241
|
+
export { connectAsync }
|
package/src/lib/connect/tcp.ts
CHANGED
|
@@ -17,7 +17,7 @@ const buildStream: StreamBuilder = (client, opts) => {
|
|
|
17
17
|
const host = opts.hostname
|
|
18
18
|
|
|
19
19
|
debug('port %d and host %s', port, host)
|
|
20
|
-
return net.createConnection(port, host)
|
|
20
|
+
return net.createConnection(port, host)
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
export default buildStream
|
package/src/lib/connect/tls.ts
CHANGED
|
@@ -2,7 +2,6 @@ import tls from 'tls'
|
|
|
2
2
|
import net from 'net'
|
|
3
3
|
import _debug from 'debug'
|
|
4
4
|
import { StreamBuilder } from '../shared'
|
|
5
|
-
import { Duplex } from 'readable-stream'
|
|
6
5
|
|
|
7
6
|
const debug = _debug('mqttjs:tls')
|
|
8
7
|
|
|
@@ -26,7 +25,6 @@ const buildStream: StreamBuilder = (client, opts) => {
|
|
|
26
25
|
)
|
|
27
26
|
|
|
28
27
|
const connection = tls.connect(opts)
|
|
29
|
-
/* eslint no-use-before-define: [2, "nofunc"] */
|
|
30
28
|
connection.on('secureConnect', () => {
|
|
31
29
|
if (opts.rejectUnauthorized && !connection.authorized) {
|
|
32
30
|
connection.emit('error', new Error('TLS not authorized'))
|
|
@@ -50,7 +48,7 @@ const buildStream: StreamBuilder = (client, opts) => {
|
|
|
50
48
|
}
|
|
51
49
|
|
|
52
50
|
connection.on('error', handleTLSerrors)
|
|
53
|
-
return connection
|
|
51
|
+
return connection
|
|
54
52
|
}
|
|
55
53
|
|
|
56
54
|
export default buildStream
|
package/src/lib/connect/ws.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { Buffer } from 'buffer'
|
|
|
4
4
|
import WS, { ClientOptions } from 'ws'
|
|
5
5
|
import _debug from 'debug'
|
|
6
6
|
import duplexify from 'duplexify'
|
|
7
|
-
import {
|
|
7
|
+
import { DuplexOptions, Transform } from 'readable-stream'
|
|
8
8
|
import IS_BROWSER from '../is-browser'
|
|
9
9
|
import MqttClient, { IClientOptions } from '../client'
|
|
10
10
|
|
|
@@ -137,13 +137,12 @@ const streamBuilder: StreamBuilder = (client, opts) => {
|
|
|
137
137
|
socket,
|
|
138
138
|
options.wsOptions as DuplexOptions,
|
|
139
139
|
)
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
webSocketStream.url = url
|
|
140
|
+
|
|
141
|
+
webSocketStream['url'] = url
|
|
143
142
|
socket.on('close', () => {
|
|
144
143
|
webSocketStream.destroy()
|
|
145
144
|
})
|
|
146
|
-
return webSocketStream
|
|
145
|
+
return webSocketStream
|
|
147
146
|
}
|
|
148
147
|
|
|
149
148
|
const browserStreamBuilder: StreamBuilder = (client, opts) => {
|
package/src/lib/connect/wx.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { StreamBuilder } from '../shared'
|
|
2
2
|
|
|
3
3
|
import { Buffer } from 'buffer'
|
|
4
|
-
import {
|
|
4
|
+
import { Transform } from 'readable-stream'
|
|
5
5
|
import duplexify, { Duplexify } from 'duplexify'
|
|
6
6
|
import MqttClient, { IClientOptions } from '../client'
|
|
7
7
|
|
|
@@ -135,7 +135,7 @@ const buildStream: StreamBuilder = (client, opts) => {
|
|
|
135
135
|
|
|
136
136
|
bindEventHandler()
|
|
137
137
|
|
|
138
|
-
return stream
|
|
138
|
+
return stream
|
|
139
139
|
}
|
|
140
140
|
|
|
141
141
|
export default buildStream
|
|
@@ -14,8 +14,8 @@ export interface IMessageIdProvider {
|
|
|
14
14
|
/**
|
|
15
15
|
* Register the messageId. The messageId become occupied status.
|
|
16
16
|
* If the messageId has already been occupied, then return false.
|
|
17
|
-
* @param {
|
|
18
|
-
* @return {
|
|
17
|
+
* @param {number} num - The messageId to request use.
|
|
18
|
+
* @return {boolean} - If `num` was not occupied, then return true, otherwise return false.
|
|
19
19
|
*/
|
|
20
20
|
register(num: number): boolean
|
|
21
21
|
|
|
@@ -106,7 +106,7 @@ const handlePublish: PacketHandler = (client, packet: IPublishPacket, done) => {
|
|
|
106
106
|
error = null
|
|
107
107
|
}
|
|
108
108
|
if (error) {
|
|
109
|
-
return client.emit('error', error)
|
|
109
|
+
return client.emit('error', error as Error)
|
|
110
110
|
}
|
|
111
111
|
if (validReasonCodes.indexOf(code) === -1) {
|
|
112
112
|
return client.emit(
|
|
@@ -143,7 +143,7 @@ const handlePublish: PacketHandler = (client, packet: IPublishPacket, done) => {
|
|
|
143
143
|
error = null
|
|
144
144
|
}
|
|
145
145
|
if (error) {
|
|
146
|
-
return client.emit('error', error)
|
|
146
|
+
return client.emit('error', error as Error)
|
|
147
147
|
}
|
|
148
148
|
if (validReasonCodes.indexOf(code) === -1) {
|
|
149
149
|
return client.emit(
|
package/src/lib/shared.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Packet } from 'mqtt-packet'
|
|
2
|
+
import type internal from 'stream'
|
|
2
3
|
import type MqttClient from './client'
|
|
3
|
-
import {
|
|
4
|
-
import { Duplex, Writable } from 'readable-stream'
|
|
4
|
+
import type { IClientOptions } from './client'
|
|
5
5
|
|
|
6
6
|
export type DoneCallback = (error?: Error) => void
|
|
7
7
|
|
|
@@ -9,7 +9,7 @@ export type GenericCallback<T> = (error?: Error, result?: T) => void
|
|
|
9
9
|
|
|
10
10
|
export type VoidCallback = () => void
|
|
11
11
|
|
|
12
|
-
export type IStream = Duplex
|
|
12
|
+
export type IStream = internal.Duplex
|
|
13
13
|
|
|
14
14
|
export type StreamBuilder = (
|
|
15
15
|
client: MqttClient,
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { NumberAllocator } from 'number-allocator'
|
|
2
|
+
import { IMessageIdProvider } from './default-message-id-provider'
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* UniqueMessageAllocator constructor
|
|
5
6
|
* @constructor
|
|
6
7
|
*/
|
|
7
|
-
export default class UniqueMessageIdProvider {
|
|
8
|
+
export default class UniqueMessageIdProvider implements IMessageIdProvider {
|
|
8
9
|
private numberAllocator: NumberAllocator
|
|
9
10
|
|
|
10
11
|
private lastId: number
|
|
@@ -41,7 +42,7 @@ export default class UniqueMessageIdProvider {
|
|
|
41
42
|
* @return boolean
|
|
42
43
|
*/
|
|
43
44
|
register(messageId: number) {
|
|
44
|
-
return this.numberAllocator.use(messageId)
|
|
45
|
+
return this.numberAllocator.use(messageId) as boolean
|
|
45
46
|
}
|
|
46
47
|
|
|
47
48
|
/**
|
package/src/mqtt.ts
CHANGED
|
@@ -9,11 +9,12 @@ import MqttClient from './lib/client'
|
|
|
9
9
|
import DefaultMessageIdProvider from './lib/default-message-id-provider'
|
|
10
10
|
import UniqueMessageIdProvider from './lib/unique-message-id-provider'
|
|
11
11
|
import Store, { IStore } from './lib/store'
|
|
12
|
-
import connect from './lib/connect'
|
|
12
|
+
import connect, { connectAsync } from './lib/connect'
|
|
13
13
|
|
|
14
14
|
export const Client = MqttClient
|
|
15
15
|
export {
|
|
16
16
|
connect,
|
|
17
|
+
connectAsync,
|
|
17
18
|
MqttClient,
|
|
18
19
|
Store,
|
|
19
20
|
DefaultMessageIdProvider,
|
|
@@ -21,3 +22,5 @@ export {
|
|
|
21
22
|
IStore,
|
|
22
23
|
}
|
|
23
24
|
export * from './lib/client'
|
|
25
|
+
export * from './lib/shared'
|
|
26
|
+
export { ReasonCodes } from './lib/handlers/ack'
|