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
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { StreamBuilder } from '../shared'
|
|
2
|
+
|
|
3
|
+
import { Buffer } from 'buffer'
|
|
4
|
+
import { Duplex, Transform } from 'readable-stream'
|
|
5
|
+
import duplexify, { Duplexify } from 'duplexify'
|
|
6
|
+
import MqttClient, { IClientOptions } from '../client'
|
|
7
|
+
|
|
8
|
+
/* global wx */
|
|
9
|
+
let socketTask: any
|
|
10
|
+
let proxy: Transform
|
|
11
|
+
let stream: Duplexify
|
|
12
|
+
|
|
13
|
+
function buildProxy() {
|
|
14
|
+
const _proxy = new Transform()
|
|
15
|
+
_proxy._write = (chunk, encoding, next) => {
|
|
16
|
+
socketTask.send({
|
|
17
|
+
data: chunk.buffer,
|
|
18
|
+
success() {
|
|
19
|
+
next()
|
|
20
|
+
},
|
|
21
|
+
fail(errMsg) {
|
|
22
|
+
next(new Error(errMsg))
|
|
23
|
+
},
|
|
24
|
+
})
|
|
25
|
+
}
|
|
26
|
+
_proxy._flush = (done) => {
|
|
27
|
+
socketTask.close({
|
|
28
|
+
success() {
|
|
29
|
+
done()
|
|
30
|
+
},
|
|
31
|
+
})
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return _proxy
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function setDefaultOpts(opts) {
|
|
38
|
+
if (!opts.hostname) {
|
|
39
|
+
opts.hostname = 'localhost'
|
|
40
|
+
}
|
|
41
|
+
if (!opts.path) {
|
|
42
|
+
opts.path = '/'
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (!opts.wsOptions) {
|
|
46
|
+
opts.wsOptions = {}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function buildUrl(opts: IClientOptions, client: MqttClient) {
|
|
51
|
+
const protocol = opts.protocol === 'wxs' ? 'wss' : 'ws'
|
|
52
|
+
let url = `${protocol}://${opts.hostname}${opts.path}`
|
|
53
|
+
if (opts.port && opts.port !== 80 && opts.port !== 443) {
|
|
54
|
+
url = `${protocol}://${opts.hostname}:${opts.port}${opts.path}`
|
|
55
|
+
}
|
|
56
|
+
if (typeof opts.transformWsUrl === 'function') {
|
|
57
|
+
url = opts.transformWsUrl(url, opts, client)
|
|
58
|
+
}
|
|
59
|
+
return url
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function bindEventHandler() {
|
|
63
|
+
socketTask.onOpen(() => {
|
|
64
|
+
stream.setReadable(proxy)
|
|
65
|
+
stream.setWritable(proxy)
|
|
66
|
+
stream.emit('connect')
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
socketTask.onMessage((res) => {
|
|
70
|
+
let { data } = res
|
|
71
|
+
|
|
72
|
+
if (data instanceof ArrayBuffer) data = Buffer.from(data)
|
|
73
|
+
else data = Buffer.from(data, 'utf8')
|
|
74
|
+
proxy.push(data)
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
socketTask.onClose(() => {
|
|
78
|
+
stream.end()
|
|
79
|
+
stream.destroy()
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
socketTask.onError((res) => {
|
|
83
|
+
stream.destroy(new Error(res.errMsg))
|
|
84
|
+
})
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const buildStream: StreamBuilder = (client, opts) => {
|
|
88
|
+
opts.hostname = opts.hostname || opts.host
|
|
89
|
+
|
|
90
|
+
if (!opts.hostname) {
|
|
91
|
+
throw new Error('Could not determine host. Specify host manually.')
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const websocketSubProtocol =
|
|
95
|
+
opts.protocolId === 'MQIsdp' && opts.protocolVersion === 3
|
|
96
|
+
? 'mqttv3.1'
|
|
97
|
+
: 'mqtt'
|
|
98
|
+
|
|
99
|
+
setDefaultOpts(opts)
|
|
100
|
+
|
|
101
|
+
const url = buildUrl(opts, client)
|
|
102
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
103
|
+
// @ts-ignore
|
|
104
|
+
socketTask = wx.connectSocket({
|
|
105
|
+
url,
|
|
106
|
+
protocols: [websocketSubProtocol],
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
proxy = buildProxy()
|
|
110
|
+
stream = duplexify.obj()
|
|
111
|
+
stream._destroy = (err, cb) => {
|
|
112
|
+
socketTask.close({
|
|
113
|
+
success() {
|
|
114
|
+
if (cb) cb(err)
|
|
115
|
+
},
|
|
116
|
+
})
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const destroyRef = stream.destroy
|
|
120
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
121
|
+
// @ts-ignore
|
|
122
|
+
stream.destroy = () => {
|
|
123
|
+
stream.destroy = destroyRef
|
|
124
|
+
|
|
125
|
+
setTimeout(() => {
|
|
126
|
+
socketTask.close({
|
|
127
|
+
fail() {
|
|
128
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
129
|
+
// @ts-ignore
|
|
130
|
+
stream._destroy(new Error())
|
|
131
|
+
},
|
|
132
|
+
})
|
|
133
|
+
}, 0)
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
bindEventHandler()
|
|
137
|
+
|
|
138
|
+
return stream as unknown as Duplex
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export default buildStream
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
export interface IMessageIdProvider {
|
|
2
|
+
/**
|
|
3
|
+
* Allocate the first vacant messageId. The messageId become occupied status.
|
|
4
|
+
* @return {Number} - The first vacant messageId. If all messageIds are occupied, return null.
|
|
5
|
+
*/
|
|
6
|
+
allocate(): number | null
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Get the last allocated messageId.
|
|
10
|
+
* @return {Number} - messageId.
|
|
11
|
+
*/
|
|
12
|
+
getLastAllocated(): number | null
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Register the messageId. The messageId become occupied status.
|
|
16
|
+
* If the messageId has already been occupied, then return false.
|
|
17
|
+
* @param {Number} num - The messageId to request use.
|
|
18
|
+
* @return {Boolean} - If `num` was not occupied, then return true, otherwise return false.
|
|
19
|
+
*/
|
|
20
|
+
register(num: number): boolean
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Deallocate the messageId. The messageId become vacant status.
|
|
24
|
+
* @param {Number} num - The messageId to deallocate. The messageId must be occupied status.
|
|
25
|
+
* In other words, the messageId must be allocated by allocate() or
|
|
26
|
+
* occupied by register().
|
|
27
|
+
*/
|
|
28
|
+
deallocate(num: number): void
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Clear all occupied messageIds.
|
|
32
|
+
* The all messageIds are set to vacant status.
|
|
33
|
+
*/
|
|
34
|
+
clear(): void
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* DefaultMessageAllocator constructor
|
|
39
|
+
* @constructor
|
|
40
|
+
*/
|
|
41
|
+
export default class DefaultMessageIdProvider implements IMessageIdProvider {
|
|
42
|
+
private nextId: number
|
|
43
|
+
|
|
44
|
+
constructor() {
|
|
45
|
+
/**
|
|
46
|
+
* MessageIDs starting with 1
|
|
47
|
+
* ensure that nextId is min. 1, see https://github.com/mqttjs/MQTT.js/issues/810
|
|
48
|
+
*/
|
|
49
|
+
this.nextId = Math.max(1, Math.floor(Math.random() * 65535))
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* allocate
|
|
54
|
+
*
|
|
55
|
+
* Get the next messageId.
|
|
56
|
+
* @return unsigned int
|
|
57
|
+
*/
|
|
58
|
+
allocate() {
|
|
59
|
+
// id becomes current state of this.nextId and increments afterwards
|
|
60
|
+
const id = this.nextId++
|
|
61
|
+
// Ensure 16 bit unsigned int (max 65535, nextId got one higher)
|
|
62
|
+
if (this.nextId === 65536) {
|
|
63
|
+
this.nextId = 1
|
|
64
|
+
}
|
|
65
|
+
return id
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* getLastAllocated
|
|
70
|
+
* Get the last allocated messageId.
|
|
71
|
+
* @return unsigned int
|
|
72
|
+
*/
|
|
73
|
+
getLastAllocated() {
|
|
74
|
+
return this.nextId === 1 ? 65535 : this.nextId - 1
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* register
|
|
79
|
+
* Register messageId. If success return true, otherwise return false.
|
|
80
|
+
* @param { unsigned int } - messageId to register,
|
|
81
|
+
* @return boolean
|
|
82
|
+
*/
|
|
83
|
+
register(messageId: number) {
|
|
84
|
+
return true
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* deallocate
|
|
89
|
+
* Deallocate messageId.
|
|
90
|
+
* @param { unsigned int } - messageId to deallocate,
|
|
91
|
+
*/
|
|
92
|
+
deallocate(messageId: number) {}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* clear
|
|
96
|
+
* Deallocate all messageIds.
|
|
97
|
+
*/
|
|
98
|
+
clear() {}
|
|
99
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
// Other Socket Errors: EADDRINUSE, ECONNRESET, ENOTFOUND, ETIMEDOUT.
|
|
2
|
+
|
|
3
|
+
import { PacketHandler } from '../shared'
|
|
4
|
+
|
|
5
|
+
export const ReasonCodes = {
|
|
6
|
+
0: '',
|
|
7
|
+
1: 'Unacceptable protocol version',
|
|
8
|
+
2: 'Identifier rejected',
|
|
9
|
+
3: 'Server unavailable',
|
|
10
|
+
4: 'Bad username or password',
|
|
11
|
+
5: 'Not authorized',
|
|
12
|
+
16: 'No matching subscribers',
|
|
13
|
+
17: 'No subscription existed',
|
|
14
|
+
128: 'Unspecified error',
|
|
15
|
+
129: 'Malformed Packet',
|
|
16
|
+
130: 'Protocol Error',
|
|
17
|
+
131: 'Implementation specific error',
|
|
18
|
+
132: 'Unsupported Protocol Version',
|
|
19
|
+
133: 'Client Identifier not valid',
|
|
20
|
+
134: 'Bad User Name or Password',
|
|
21
|
+
135: 'Not authorized',
|
|
22
|
+
136: 'Server unavailable',
|
|
23
|
+
137: 'Server busy',
|
|
24
|
+
138: 'Banned',
|
|
25
|
+
139: 'Server shutting down',
|
|
26
|
+
140: 'Bad authentication method',
|
|
27
|
+
141: 'Keep Alive timeout',
|
|
28
|
+
142: 'Session taken over',
|
|
29
|
+
143: 'Topic Filter invalid',
|
|
30
|
+
144: 'Topic Name invalid',
|
|
31
|
+
145: 'Packet identifier in use',
|
|
32
|
+
146: 'Packet Identifier not found',
|
|
33
|
+
147: 'Receive Maximum exceeded',
|
|
34
|
+
148: 'Topic Alias invalid',
|
|
35
|
+
149: 'Packet too large',
|
|
36
|
+
150: 'Message rate too high',
|
|
37
|
+
151: 'Quota exceeded',
|
|
38
|
+
152: 'Administrative action',
|
|
39
|
+
153: 'Payload format invalid',
|
|
40
|
+
154: 'Retain not supported',
|
|
41
|
+
155: 'QoS not supported',
|
|
42
|
+
156: 'Use another server',
|
|
43
|
+
157: 'Server moved',
|
|
44
|
+
158: 'Shared Subscriptions not supported',
|
|
45
|
+
159: 'Connection rate exceeded',
|
|
46
|
+
160: 'Maximum connect time',
|
|
47
|
+
161: 'Subscription Identifiers not supported',
|
|
48
|
+
162: 'Wildcard Subscriptions not supported',
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const handleAck: PacketHandler = (client, packet) => {
|
|
52
|
+
/* eslint no-fallthrough: "off" */
|
|
53
|
+
const { messageId } = packet
|
|
54
|
+
const type = packet.cmd
|
|
55
|
+
let response = null
|
|
56
|
+
const cb = client.outgoing[messageId] ? client.outgoing[messageId].cb : null
|
|
57
|
+
let err
|
|
58
|
+
|
|
59
|
+
// Checking `!cb` happens to work, but it's not technically "correct".
|
|
60
|
+
//
|
|
61
|
+
// Why? client code assumes client "no callback" is the same as client "we're not
|
|
62
|
+
// waiting for responses" (puback, pubrec, pubcomp, suback, or unsuback).
|
|
63
|
+
//
|
|
64
|
+
// It would be better to check `if (!client.outgoing[messageId])` here, but
|
|
65
|
+
// there's no reason to change it and risk (another) regression.
|
|
66
|
+
//
|
|
67
|
+
// The only reason client code works is becaues code in MqttClient.publish,
|
|
68
|
+
// MqttClinet.subscribe, and MqttClient.unsubscribe ensures client we will
|
|
69
|
+
// have a callback even if the user doesn't pass one in.)
|
|
70
|
+
if (!cb) {
|
|
71
|
+
client.log('_handleAck :: Server sent an ack in error. Ignoring.')
|
|
72
|
+
// Server sent an ack in error, ignore it.
|
|
73
|
+
return
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Process
|
|
77
|
+
client.log('_handleAck :: packet type', type)
|
|
78
|
+
switch (type) {
|
|
79
|
+
case 'pubcomp':
|
|
80
|
+
// same thing as puback for QoS 2
|
|
81
|
+
case 'puback': {
|
|
82
|
+
const pubackRC = packet.reasonCode
|
|
83
|
+
// Callback - we're done
|
|
84
|
+
if (pubackRC && pubackRC > 0 && pubackRC !== 16) {
|
|
85
|
+
err = new Error(`Publish error: ${ReasonCodes[pubackRC]}`)
|
|
86
|
+
err.code = pubackRC
|
|
87
|
+
client['_removeOutgoingAndStoreMessage'](messageId, () => {
|
|
88
|
+
cb(err, packet)
|
|
89
|
+
})
|
|
90
|
+
} else {
|
|
91
|
+
client['_removeOutgoingAndStoreMessage'](messageId, cb)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
break
|
|
95
|
+
}
|
|
96
|
+
case 'pubrec': {
|
|
97
|
+
response = {
|
|
98
|
+
cmd: 'pubrel',
|
|
99
|
+
qos: 2,
|
|
100
|
+
messageId,
|
|
101
|
+
}
|
|
102
|
+
const pubrecRC = packet.reasonCode
|
|
103
|
+
|
|
104
|
+
if (pubrecRC && pubrecRC > 0 && pubrecRC !== 16) {
|
|
105
|
+
err = new Error(`Publish error: ${ReasonCodes[pubrecRC]}`)
|
|
106
|
+
err.code = pubrecRC
|
|
107
|
+
client['_removeOutgoingAndStoreMessage'](messageId, () => {
|
|
108
|
+
cb(err, packet)
|
|
109
|
+
})
|
|
110
|
+
} else {
|
|
111
|
+
client['_sendPacket'](response)
|
|
112
|
+
}
|
|
113
|
+
break
|
|
114
|
+
}
|
|
115
|
+
case 'suback': {
|
|
116
|
+
delete client.outgoing[messageId]
|
|
117
|
+
client.messageIdProvider.deallocate(messageId)
|
|
118
|
+
const granted = packet.granted as number[]
|
|
119
|
+
for (let grantedI = 0; grantedI < granted.length; grantedI++) {
|
|
120
|
+
if ((granted[grantedI] & 0x80) !== 0) {
|
|
121
|
+
// suback with Failure status
|
|
122
|
+
const topics = client.messageIdToTopic[messageId]
|
|
123
|
+
if (topics) {
|
|
124
|
+
topics.forEach((topic) => {
|
|
125
|
+
delete client['_resubscribeTopics'][topic]
|
|
126
|
+
})
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
delete client.messageIdToTopic[messageId]
|
|
131
|
+
client['_invokeStoreProcessingQueue']()
|
|
132
|
+
cb(null, packet)
|
|
133
|
+
break
|
|
134
|
+
}
|
|
135
|
+
case 'unsuback': {
|
|
136
|
+
delete client.outgoing[messageId]
|
|
137
|
+
client.messageIdProvider.deallocate(messageId)
|
|
138
|
+
client['_invokeStoreProcessingQueue']()
|
|
139
|
+
cb(null)
|
|
140
|
+
break
|
|
141
|
+
}
|
|
142
|
+
default:
|
|
143
|
+
client.emit('error', new Error('unrecognized packet type'))
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (client.disconnecting && Object.keys(client.outgoing).length === 0) {
|
|
147
|
+
client.emit('outgoingEmpty')
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export default handleAck
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { IAuthPacket } from 'mqtt-packet'
|
|
2
|
+
import { ErrorWithReasonCode, PacketHandler } from '../shared'
|
|
3
|
+
import { ReasonCodes } from './ack'
|
|
4
|
+
|
|
5
|
+
const handleAuth: PacketHandler = (
|
|
6
|
+
client,
|
|
7
|
+
packet: IAuthPacket & { returnCode: number },
|
|
8
|
+
) => {
|
|
9
|
+
const { options } = client
|
|
10
|
+
const version = options.protocolVersion
|
|
11
|
+
const rc = version === 5 ? packet.reasonCode : packet.returnCode
|
|
12
|
+
|
|
13
|
+
if (version !== 5) {
|
|
14
|
+
const err = new ErrorWithReasonCode(
|
|
15
|
+
`Protocol error: Auth packets are only supported in MQTT 5. Your version:${version}`,
|
|
16
|
+
rc,
|
|
17
|
+
)
|
|
18
|
+
client.emit('error', err)
|
|
19
|
+
return
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
client.handleAuth(
|
|
23
|
+
packet,
|
|
24
|
+
(err: ErrorWithReasonCode, packet2: IAuthPacket) => {
|
|
25
|
+
if (err) {
|
|
26
|
+
client.emit('error', err)
|
|
27
|
+
return
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (rc === 24) {
|
|
31
|
+
client.reconnecting = false
|
|
32
|
+
client['_sendPacket'](packet2)
|
|
33
|
+
} else {
|
|
34
|
+
const error = new ErrorWithReasonCode(
|
|
35
|
+
`Connection refused: ${ReasonCodes[rc]}`,
|
|
36
|
+
rc,
|
|
37
|
+
)
|
|
38
|
+
client.emit('error', error)
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export default handleAuth
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { ReasonCodes } from './ack'
|
|
2
|
+
import TopicAliasSend from '../topic-alias-send'
|
|
3
|
+
import { ErrorWithReasonCode, PacketHandler } from '../shared'
|
|
4
|
+
import { IConnackPacket } from 'mqtt-packet'
|
|
5
|
+
|
|
6
|
+
const handleConnack: PacketHandler = (client, packet: IConnackPacket) => {
|
|
7
|
+
client.log('_handleConnack')
|
|
8
|
+
const { options } = client
|
|
9
|
+
const version = options.protocolVersion
|
|
10
|
+
const rc = version === 5 ? packet.reasonCode : packet.returnCode
|
|
11
|
+
|
|
12
|
+
clearTimeout(client['connackTimer'])
|
|
13
|
+
delete client['topicAliasSend']
|
|
14
|
+
|
|
15
|
+
if (packet.properties) {
|
|
16
|
+
if (packet.properties.topicAliasMaximum) {
|
|
17
|
+
if (packet.properties.topicAliasMaximum > 0xffff) {
|
|
18
|
+
client.emit(
|
|
19
|
+
'error',
|
|
20
|
+
new Error('topicAliasMaximum from broker is out of range'),
|
|
21
|
+
)
|
|
22
|
+
return
|
|
23
|
+
}
|
|
24
|
+
if (packet.properties.topicAliasMaximum > 0) {
|
|
25
|
+
client['topicAliasSend'] = new TopicAliasSend(
|
|
26
|
+
packet.properties.topicAliasMaximum,
|
|
27
|
+
)
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
if (packet.properties.serverKeepAlive && options.keepalive) {
|
|
31
|
+
options.keepalive = packet.properties.serverKeepAlive
|
|
32
|
+
client['_shiftPingInterval']()
|
|
33
|
+
}
|
|
34
|
+
if (packet.properties.maximumPacketSize) {
|
|
35
|
+
if (!options.properties) {
|
|
36
|
+
options.properties = {}
|
|
37
|
+
}
|
|
38
|
+
options.properties.maximumPacketSize =
|
|
39
|
+
packet.properties.maximumPacketSize
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (rc === 0) {
|
|
44
|
+
client.reconnecting = false
|
|
45
|
+
client['_onConnect'](packet)
|
|
46
|
+
} else if (rc > 0) {
|
|
47
|
+
const err = new ErrorWithReasonCode(
|
|
48
|
+
`Connection refused: ${ReasonCodes[rc]}`,
|
|
49
|
+
rc,
|
|
50
|
+
)
|
|
51
|
+
client.emit('error', err)
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export default handleConnack
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import handlePublish from './publish'
|
|
2
|
+
import handleAuth from './auth'
|
|
3
|
+
import handleConnack from './connack'
|
|
4
|
+
import handleAck from './ack'
|
|
5
|
+
import handlePubrel from './pubrel'
|
|
6
|
+
import { PacketHandler } from '../shared'
|
|
7
|
+
|
|
8
|
+
const handle: PacketHandler = (client, packet, done) => {
|
|
9
|
+
const { options } = client
|
|
10
|
+
|
|
11
|
+
if (
|
|
12
|
+
options.protocolVersion === 5 &&
|
|
13
|
+
options.properties &&
|
|
14
|
+
options.properties.maximumPacketSize &&
|
|
15
|
+
options.properties.maximumPacketSize < packet.length
|
|
16
|
+
) {
|
|
17
|
+
client.emit('error', new Error(`exceeding packets size ${packet.cmd}`))
|
|
18
|
+
client.end({
|
|
19
|
+
reasonCode: 149,
|
|
20
|
+
properties: { reasonString: 'Maximum packet size was exceeded' },
|
|
21
|
+
})
|
|
22
|
+
return client
|
|
23
|
+
}
|
|
24
|
+
client.log('_handlePacket :: emitting packetreceive')
|
|
25
|
+
client.emit('packetreceive', packet)
|
|
26
|
+
|
|
27
|
+
switch (packet.cmd) {
|
|
28
|
+
case 'publish':
|
|
29
|
+
handlePublish(client, packet, done)
|
|
30
|
+
break
|
|
31
|
+
case 'puback':
|
|
32
|
+
case 'pubrec':
|
|
33
|
+
case 'pubcomp':
|
|
34
|
+
case 'suback':
|
|
35
|
+
case 'unsuback':
|
|
36
|
+
handleAck(client, packet)
|
|
37
|
+
done()
|
|
38
|
+
break
|
|
39
|
+
case 'pubrel':
|
|
40
|
+
handlePubrel(client, packet, done)
|
|
41
|
+
break
|
|
42
|
+
case 'connack':
|
|
43
|
+
handleConnack(client, packet)
|
|
44
|
+
done()
|
|
45
|
+
break
|
|
46
|
+
case 'auth':
|
|
47
|
+
handleAuth(client, packet)
|
|
48
|
+
done()
|
|
49
|
+
break
|
|
50
|
+
case 'pingresp':
|
|
51
|
+
// this will be checked in _checkPing client method every keepalive interval
|
|
52
|
+
client.pingResp = true
|
|
53
|
+
done()
|
|
54
|
+
break
|
|
55
|
+
case 'disconnect':
|
|
56
|
+
client.emit('disconnect', packet)
|
|
57
|
+
done()
|
|
58
|
+
break
|
|
59
|
+
default:
|
|
60
|
+
// TODO: unknown packet received. Should we emit an error?
|
|
61
|
+
client.log('_handlePacket :: unknown command')
|
|
62
|
+
done()
|
|
63
|
+
break
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export default handle
|