mqtt 5.0.0-beta.3 → 5.0.0
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 +43 -4
- 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 +220 -0
- package/build/src/lib/client.js +1204 -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 +9 -0
- package/build/src/lib/connect/index.js +169 -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 +18 -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 +11 -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 +10 -0
- package/build/src/mqtt.js +49 -0
- package/build/src/mqtt.js.map +1 -0
- package/build/tsconfig.build.tsbuildinfo +1 -0
- package/dist/mqtt.js +2726 -3493
- package/dist/mqtt.min.js +4 -4
- package/doc/subscribe.txt +1 -1
- package/package.json +49 -29
- package/{bin/mqtt.js → src/bin/mqtt.ts} +11 -6
- package/src/bin/pub.ts +171 -0
- package/src/bin/sub.ts +146 -0
- package/src/lib/TypedEmitter.ts +45 -0
- package/{lib/client.js → src/lib/client.ts} +895 -252
- package/{lib/connect/ali.js → src/lib/connect/ali.ts} +13 -11
- package/src/lib/connect/index.ts +241 -0
- package/{lib/connect/tcp.js → src/lib/connect/tcp.ts} +8 -4
- package/{lib/connect/tls.js → src/lib/connect/tls.ts} +10 -8
- package/{lib/connect/ws.js → src/lib/connect/ws.ts} +52 -36
- package/{lib/connect/wx.js → src/lib/connect/wx.ts} +18 -9
- package/src/lib/default-message-id-provider.ts +99 -0
- package/{lib/handlers/ack.js → src/lib/handlers/ack.ts} +17 -19
- package/src/lib/handlers/auth.ts +44 -0
- package/{lib/handlers/connack.js → src/lib/handlers/connack.ts} +15 -11
- package/{lib/handlers/index.js → src/lib/handlers/index.ts} +8 -7
- package/{lib/handlers/publish.js → src/lib/handlers/publish.ts} +73 -57
- package/src/lib/handlers/pubrel.ts +27 -0
- package/{lib/is-browser.js → src/lib/is-browser.ts} +5 -4
- package/src/lib/shared.ts +73 -0
- package/{lib/store.js → src/lib/store.ts} +61 -16
- package/{lib/topic-alias-recv.js → src/lib/topic-alias-recv.ts} +10 -6
- package/{lib/topic-alias-send.js → src/lib/topic-alias-send.ts} +19 -11
- package/{lib/unique-message-id-provider.js → src/lib/unique-message-id-provider.ts} +10 -7
- package/{lib/validations.js → src/lib/validations.ts} +2 -6
- package/src/mqtt.ts +26 -0
- package/bin/pub.js +0 -159
- package/bin/sub.js +0 -141
- package/lib/connect/index.js +0 -175
- package/lib/default-message-id-provider.js +0 -63
- package/lib/handlers/auth.js +0 -34
- package/lib/handlers/pubrel.js +0 -24
- package/mqtt.js +0 -21
- package/types/index.d.ts +0 -30
- package/types/lib/client-options.d.ts +0 -224
- package/types/lib/client.d.ts +0 -273
- 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,1204 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
const topic_alias_recv_1 = __importDefault(require("./topic-alias-recv"));
|
|
30
|
+
const mqtt_packet_1 = __importDefault(require("mqtt-packet"));
|
|
31
|
+
const default_message_id_provider_1 = __importDefault(require("./default-message-id-provider"));
|
|
32
|
+
const readable_stream_1 = require("readable-stream");
|
|
33
|
+
const reinterval_1 = __importDefault(require("reinterval"));
|
|
34
|
+
const default_1 = __importDefault(require("rfdc/default"));
|
|
35
|
+
const validations = __importStar(require("./validations"));
|
|
36
|
+
const debug_1 = __importDefault(require("debug"));
|
|
37
|
+
const store_1 = __importDefault(require("./store"));
|
|
38
|
+
const handlers_1 = __importDefault(require("./handlers"));
|
|
39
|
+
const TypedEmitter_1 = require("./TypedEmitter");
|
|
40
|
+
const nextTick = process
|
|
41
|
+
? process.nextTick
|
|
42
|
+
: (callback) => {
|
|
43
|
+
setTimeout(callback, 0);
|
|
44
|
+
};
|
|
45
|
+
const setImmediate = global.setImmediate ||
|
|
46
|
+
((...args) => {
|
|
47
|
+
const callback = args.shift();
|
|
48
|
+
nextTick(() => {
|
|
49
|
+
callback(...args);
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
const defaultConnectOptions = {
|
|
53
|
+
keepalive: 60,
|
|
54
|
+
reschedulePings: true,
|
|
55
|
+
protocolId: 'MQTT',
|
|
56
|
+
protocolVersion: 4,
|
|
57
|
+
reconnectPeriod: 1000,
|
|
58
|
+
connectTimeout: 30 * 1000,
|
|
59
|
+
clean: true,
|
|
60
|
+
resubscribe: true,
|
|
61
|
+
writeCache: true,
|
|
62
|
+
};
|
|
63
|
+
const socketErrors = [
|
|
64
|
+
'ECONNREFUSED',
|
|
65
|
+
'EADDRINUSE',
|
|
66
|
+
'ECONNRESET',
|
|
67
|
+
'ENOTFOUND',
|
|
68
|
+
'ETIMEDOUT',
|
|
69
|
+
];
|
|
70
|
+
class MqttClient extends TypedEmitter_1.TypedEventEmitter {
|
|
71
|
+
static defaultId() {
|
|
72
|
+
return `mqttjs_${Math.random().toString(16).substr(2, 8)}`;
|
|
73
|
+
}
|
|
74
|
+
constructor(streamBuilder, options) {
|
|
75
|
+
super();
|
|
76
|
+
this.options = options || {};
|
|
77
|
+
for (const k in defaultConnectOptions) {
|
|
78
|
+
if (typeof this.options[k] === 'undefined') {
|
|
79
|
+
this.options[k] = defaultConnectOptions[k];
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
this.options[k] = options[k];
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
this.log = this.options.log || (0, debug_1.default)('mqttjs:client');
|
|
86
|
+
this.noop = this._noop.bind(this);
|
|
87
|
+
this.log('MqttClient :: options.protocol', options.protocol);
|
|
88
|
+
this.log('MqttClient :: options.protocolVersion', options.protocolVersion);
|
|
89
|
+
this.log('MqttClient :: options.username', options.username);
|
|
90
|
+
this.log('MqttClient :: options.keepalive', options.keepalive);
|
|
91
|
+
this.log('MqttClient :: options.reconnectPeriod', options.reconnectPeriod);
|
|
92
|
+
this.log('MqttClient :: options.rejectUnauthorized', options.rejectUnauthorized);
|
|
93
|
+
this.log('MqttClient :: options.properties.topicAliasMaximum', options.properties
|
|
94
|
+
? options.properties.topicAliasMaximum
|
|
95
|
+
: undefined);
|
|
96
|
+
this.options.clientId =
|
|
97
|
+
typeof options.clientId === 'string'
|
|
98
|
+
? options.clientId
|
|
99
|
+
: MqttClient.defaultId();
|
|
100
|
+
this.log('MqttClient :: clientId', this.options.clientId);
|
|
101
|
+
this.options.customHandleAcks =
|
|
102
|
+
options.protocolVersion === 5 && options.customHandleAcks
|
|
103
|
+
? options.customHandleAcks
|
|
104
|
+
: (...args) => {
|
|
105
|
+
args[3](null, 0);
|
|
106
|
+
};
|
|
107
|
+
if (!this.options.writeCache) {
|
|
108
|
+
mqtt_packet_1.default.writeToStream.cacheNumbers = false;
|
|
109
|
+
}
|
|
110
|
+
this.streamBuilder = streamBuilder;
|
|
111
|
+
this.messageIdProvider =
|
|
112
|
+
typeof this.options.messageIdProvider === 'undefined'
|
|
113
|
+
? new default_message_id_provider_1.default()
|
|
114
|
+
: this.options.messageIdProvider;
|
|
115
|
+
this.outgoingStore = options.outgoingStore || new store_1.default();
|
|
116
|
+
this.incomingStore = options.incomingStore || new store_1.default();
|
|
117
|
+
this.queueQoSZero =
|
|
118
|
+
options.queueQoSZero === undefined ? true : options.queueQoSZero;
|
|
119
|
+
this._resubscribeTopics = {};
|
|
120
|
+
this.messageIdToTopic = {};
|
|
121
|
+
this.pingTimer = null;
|
|
122
|
+
this.connected = false;
|
|
123
|
+
this.disconnecting = false;
|
|
124
|
+
this.queue = [];
|
|
125
|
+
this.connackTimer = null;
|
|
126
|
+
this.reconnectTimer = null;
|
|
127
|
+
this._storeProcessing = false;
|
|
128
|
+
this._packetIdsDuringStoreProcessing = {};
|
|
129
|
+
this._storeProcessingQueue = [];
|
|
130
|
+
this.outgoing = {};
|
|
131
|
+
this._firstConnection = true;
|
|
132
|
+
if (options.properties && options.properties.topicAliasMaximum > 0) {
|
|
133
|
+
if (options.properties.topicAliasMaximum > 0xffff) {
|
|
134
|
+
this.log('MqttClient :: options.properties.topicAliasMaximum is out of range');
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
this.topicAliasRecv = new topic_alias_recv_1.default(options.properties.topicAliasMaximum);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
this.on('connect', () => {
|
|
141
|
+
const { queue } = this;
|
|
142
|
+
const deliver = () => {
|
|
143
|
+
const entry = queue.shift();
|
|
144
|
+
this.log('deliver :: entry %o', entry);
|
|
145
|
+
let packet = null;
|
|
146
|
+
if (!entry) {
|
|
147
|
+
this._resubscribe();
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
packet = entry.packet;
|
|
151
|
+
this.log('deliver :: call _sendPacket for %o', packet);
|
|
152
|
+
let send = true;
|
|
153
|
+
if (packet.messageId && packet.messageId !== 0) {
|
|
154
|
+
if (!this.messageIdProvider.register(packet.messageId)) {
|
|
155
|
+
send = false;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
if (send) {
|
|
159
|
+
this._sendPacket(packet, (err) => {
|
|
160
|
+
if (entry.cb) {
|
|
161
|
+
entry.cb(err);
|
|
162
|
+
}
|
|
163
|
+
deliver();
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
this.log('messageId: %d has already used. The message is skipped and removed.', packet.messageId);
|
|
168
|
+
deliver();
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
this.log('connect :: sending queued packets');
|
|
172
|
+
deliver();
|
|
173
|
+
});
|
|
174
|
+
this.on('close', () => {
|
|
175
|
+
this.log('close :: connected set to `false`');
|
|
176
|
+
this.connected = false;
|
|
177
|
+
this.log('close :: clearing connackTimer');
|
|
178
|
+
clearTimeout(this.connackTimer);
|
|
179
|
+
this.log('close :: clearing ping timer');
|
|
180
|
+
if (this.pingTimer !== null) {
|
|
181
|
+
this.pingTimer.clear();
|
|
182
|
+
this.pingTimer = null;
|
|
183
|
+
}
|
|
184
|
+
if (this.topicAliasRecv) {
|
|
185
|
+
this.topicAliasRecv.clear();
|
|
186
|
+
}
|
|
187
|
+
this.log('close :: calling _setupReconnect');
|
|
188
|
+
this._setupReconnect();
|
|
189
|
+
});
|
|
190
|
+
if (!this.options.manualConnect) {
|
|
191
|
+
this.log('MqttClient :: setting up stream');
|
|
192
|
+
this.connect();
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
handleAuth(packet, callback) {
|
|
196
|
+
callback();
|
|
197
|
+
}
|
|
198
|
+
handleMessage(packet, callback) {
|
|
199
|
+
callback();
|
|
200
|
+
}
|
|
201
|
+
_nextId() {
|
|
202
|
+
return this.messageIdProvider.allocate();
|
|
203
|
+
}
|
|
204
|
+
getLastMessageId() {
|
|
205
|
+
return this.messageIdProvider.getLastAllocated();
|
|
206
|
+
}
|
|
207
|
+
connect() {
|
|
208
|
+
var _a;
|
|
209
|
+
const writable = new readable_stream_1.Writable();
|
|
210
|
+
const parser = mqtt_packet_1.default.parser(this.options);
|
|
211
|
+
let completeParse = null;
|
|
212
|
+
const packets = [];
|
|
213
|
+
this.log('connect :: calling method to clear reconnect');
|
|
214
|
+
this._clearReconnect();
|
|
215
|
+
this.log('connect :: using streamBuilder provided to client to create stream');
|
|
216
|
+
this.stream = this.streamBuilder(this);
|
|
217
|
+
parser.on('packet', (packet) => {
|
|
218
|
+
this.log('parser :: on packet push to packets array.');
|
|
219
|
+
packets.push(packet);
|
|
220
|
+
});
|
|
221
|
+
const work = () => {
|
|
222
|
+
this.log('work :: getting next packet in queue');
|
|
223
|
+
const packet = packets.shift();
|
|
224
|
+
if (packet) {
|
|
225
|
+
this.log('work :: packet pulled from queue');
|
|
226
|
+
(0, handlers_1.default)(this, packet, nextTickWork);
|
|
227
|
+
}
|
|
228
|
+
else {
|
|
229
|
+
this.log('work :: no packets in queue');
|
|
230
|
+
const done = completeParse;
|
|
231
|
+
completeParse = null;
|
|
232
|
+
this.log('work :: done flag is %s', !!done);
|
|
233
|
+
if (done)
|
|
234
|
+
done();
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
const nextTickWork = () => {
|
|
238
|
+
if (packets.length) {
|
|
239
|
+
nextTick(work);
|
|
240
|
+
}
|
|
241
|
+
else {
|
|
242
|
+
const done = completeParse;
|
|
243
|
+
completeParse = null;
|
|
244
|
+
done();
|
|
245
|
+
}
|
|
246
|
+
};
|
|
247
|
+
writable._write = (buf, enc, done) => {
|
|
248
|
+
completeParse = done;
|
|
249
|
+
this.log('writable stream :: parsing buffer');
|
|
250
|
+
parser.parse(buf);
|
|
251
|
+
work();
|
|
252
|
+
};
|
|
253
|
+
const streamErrorHandler = (error) => {
|
|
254
|
+
this.log('streamErrorHandler :: error', error.message);
|
|
255
|
+
if (socketErrors.includes(error.code)) {
|
|
256
|
+
this.log('streamErrorHandler :: emitting error');
|
|
257
|
+
this.emit('error', error);
|
|
258
|
+
}
|
|
259
|
+
else {
|
|
260
|
+
this.noop(error);
|
|
261
|
+
}
|
|
262
|
+
};
|
|
263
|
+
this.log('connect :: pipe stream to writable stream');
|
|
264
|
+
this.stream.pipe(writable);
|
|
265
|
+
this.stream.on('error', streamErrorHandler);
|
|
266
|
+
this.stream.on('close', () => {
|
|
267
|
+
this.log('(%s)stream :: on close', this.options.clientId);
|
|
268
|
+
this._flushVolatile();
|
|
269
|
+
this.log('stream: emit close to MqttClient');
|
|
270
|
+
this.emit('close');
|
|
271
|
+
});
|
|
272
|
+
this.log('connect: sending packet `connect`');
|
|
273
|
+
const connectPacket = {
|
|
274
|
+
cmd: 'connect',
|
|
275
|
+
protocolId: this.options.protocolId,
|
|
276
|
+
protocolVersion: this.options.protocolVersion,
|
|
277
|
+
clean: this.options.clean,
|
|
278
|
+
clientId: this.options.clientId,
|
|
279
|
+
keepalive: this.options.keepalive,
|
|
280
|
+
username: this.options.username,
|
|
281
|
+
password: this.options.password,
|
|
282
|
+
properties: this.options.properties,
|
|
283
|
+
};
|
|
284
|
+
if (this.options.will) {
|
|
285
|
+
connectPacket.will = Object.assign(Object.assign({}, this.options.will), { payload: (_a = this.options.will) === null || _a === void 0 ? void 0 : _a.payload });
|
|
286
|
+
}
|
|
287
|
+
if (this.topicAliasRecv) {
|
|
288
|
+
if (!connectPacket.properties) {
|
|
289
|
+
connectPacket.properties = {};
|
|
290
|
+
}
|
|
291
|
+
if (this.topicAliasRecv) {
|
|
292
|
+
connectPacket.properties.topicAliasMaximum =
|
|
293
|
+
this.topicAliasRecv.max;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
this._writePacket(connectPacket);
|
|
297
|
+
parser.on('error', this.emit.bind(this, 'error'));
|
|
298
|
+
if (this.options.properties) {
|
|
299
|
+
if (!this.options.properties.authenticationMethod &&
|
|
300
|
+
this.options.properties.authenticationData) {
|
|
301
|
+
this.end(() => this.emit('error', new Error('Packet has no Authentication Method')));
|
|
302
|
+
return this;
|
|
303
|
+
}
|
|
304
|
+
if (this.options.properties.authenticationMethod &&
|
|
305
|
+
this.options.authPacket &&
|
|
306
|
+
typeof this.options.authPacket === 'object') {
|
|
307
|
+
const authPacket = Object.assign({ cmd: 'auth', reasonCode: 0 }, this.options.authPacket);
|
|
308
|
+
this._writePacket(authPacket);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
this.stream.setMaxListeners(1000);
|
|
312
|
+
clearTimeout(this.connackTimer);
|
|
313
|
+
this.connackTimer = setTimeout(() => {
|
|
314
|
+
this.log('!!connectTimeout hit!! Calling _cleanUp with force `true`');
|
|
315
|
+
this._cleanUp(true);
|
|
316
|
+
}, this.options.connectTimeout);
|
|
317
|
+
return this;
|
|
318
|
+
}
|
|
319
|
+
publish(topic, message, opts, callback) {
|
|
320
|
+
this.log('publish :: message `%s` to topic `%s`', message, topic);
|
|
321
|
+
const { options } = this;
|
|
322
|
+
if (typeof opts === 'function') {
|
|
323
|
+
callback = opts;
|
|
324
|
+
opts = null;
|
|
325
|
+
}
|
|
326
|
+
opts = opts || {};
|
|
327
|
+
const defaultOpts = {
|
|
328
|
+
qos: 0,
|
|
329
|
+
retain: false,
|
|
330
|
+
dup: false,
|
|
331
|
+
};
|
|
332
|
+
opts = Object.assign(Object.assign({}, defaultOpts), opts);
|
|
333
|
+
const { qos, retain, dup, properties, cbStorePut } = opts;
|
|
334
|
+
if (this._checkDisconnecting(callback)) {
|
|
335
|
+
return this;
|
|
336
|
+
}
|
|
337
|
+
const publishProc = () => {
|
|
338
|
+
let messageId = 0;
|
|
339
|
+
if (qos === 1 || qos === 2) {
|
|
340
|
+
messageId = this._nextId();
|
|
341
|
+
if (messageId === null) {
|
|
342
|
+
this.log('No messageId left');
|
|
343
|
+
return false;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
const packet = {
|
|
347
|
+
cmd: 'publish',
|
|
348
|
+
topic,
|
|
349
|
+
payload: message,
|
|
350
|
+
qos,
|
|
351
|
+
retain,
|
|
352
|
+
messageId,
|
|
353
|
+
dup,
|
|
354
|
+
};
|
|
355
|
+
if (options.protocolVersion === 5) {
|
|
356
|
+
packet.properties = properties;
|
|
357
|
+
}
|
|
358
|
+
this.log('publish :: qos', qos);
|
|
359
|
+
switch (qos) {
|
|
360
|
+
case 1:
|
|
361
|
+
case 2:
|
|
362
|
+
this.outgoing[packet.messageId] = {
|
|
363
|
+
volatile: false,
|
|
364
|
+
cb: callback || this.noop,
|
|
365
|
+
};
|
|
366
|
+
this.log('MqttClient:publish: packet cmd: %s', packet.cmd);
|
|
367
|
+
this._sendPacket(packet, undefined, cbStorePut);
|
|
368
|
+
break;
|
|
369
|
+
default:
|
|
370
|
+
this.log('MqttClient:publish: packet cmd: %s', packet.cmd);
|
|
371
|
+
this._sendPacket(packet, callback, cbStorePut);
|
|
372
|
+
break;
|
|
373
|
+
}
|
|
374
|
+
return true;
|
|
375
|
+
};
|
|
376
|
+
if (this._storeProcessing ||
|
|
377
|
+
this._storeProcessingQueue.length > 0 ||
|
|
378
|
+
!publishProc()) {
|
|
379
|
+
this._storeProcessingQueue.push({
|
|
380
|
+
invoke: publishProc,
|
|
381
|
+
cbStorePut: opts.cbStorePut,
|
|
382
|
+
callback,
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
return this;
|
|
386
|
+
}
|
|
387
|
+
publishAsync(topic, message, opts) {
|
|
388
|
+
return new Promise((resolve, reject) => {
|
|
389
|
+
this.publish(topic, message, opts, (err) => {
|
|
390
|
+
if (err) {
|
|
391
|
+
reject(err);
|
|
392
|
+
}
|
|
393
|
+
else {
|
|
394
|
+
resolve();
|
|
395
|
+
}
|
|
396
|
+
});
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
subscribe(topicObject, opts, callback) {
|
|
400
|
+
const version = this.options.protocolVersion;
|
|
401
|
+
if (typeof opts === 'function') {
|
|
402
|
+
callback = opts;
|
|
403
|
+
}
|
|
404
|
+
callback = callback || this.noop;
|
|
405
|
+
let resubscribe = false;
|
|
406
|
+
let topicsList = [];
|
|
407
|
+
if (typeof topicObject === 'string') {
|
|
408
|
+
topicObject = [topicObject];
|
|
409
|
+
topicsList = topicObject;
|
|
410
|
+
}
|
|
411
|
+
else if (Array.isArray(topicObject)) {
|
|
412
|
+
topicsList = topicObject;
|
|
413
|
+
}
|
|
414
|
+
else if (typeof topicObject === 'object') {
|
|
415
|
+
resubscribe = topicObject.resubscribe;
|
|
416
|
+
delete topicObject.resubscribe;
|
|
417
|
+
topicsList = Object.keys(topicObject);
|
|
418
|
+
}
|
|
419
|
+
const invalidTopic = validations.validateTopics(topicsList);
|
|
420
|
+
if (invalidTopic !== null) {
|
|
421
|
+
setImmediate(callback, new Error(`Invalid topic ${invalidTopic}`));
|
|
422
|
+
return this;
|
|
423
|
+
}
|
|
424
|
+
if (this._checkDisconnecting(callback)) {
|
|
425
|
+
this.log('subscribe: discconecting true');
|
|
426
|
+
return this;
|
|
427
|
+
}
|
|
428
|
+
const defaultOpts = {
|
|
429
|
+
qos: 0,
|
|
430
|
+
};
|
|
431
|
+
if (version === 5) {
|
|
432
|
+
defaultOpts.nl = false;
|
|
433
|
+
defaultOpts.rap = false;
|
|
434
|
+
defaultOpts.rh = 0;
|
|
435
|
+
}
|
|
436
|
+
opts = Object.assign(Object.assign({}, defaultOpts), opts);
|
|
437
|
+
const properties = opts.properties;
|
|
438
|
+
const subs = [];
|
|
439
|
+
const parseSub = (topic, subOptions) => {
|
|
440
|
+
subOptions = (subOptions || opts);
|
|
441
|
+
if (!Object.prototype.hasOwnProperty.call(this._resubscribeTopics, topic) ||
|
|
442
|
+
this._resubscribeTopics[topic].qos < subOptions.qos ||
|
|
443
|
+
resubscribe) {
|
|
444
|
+
const currentOpts = {
|
|
445
|
+
topic,
|
|
446
|
+
qos: subOptions.qos,
|
|
447
|
+
};
|
|
448
|
+
if (version === 5) {
|
|
449
|
+
currentOpts.nl = subOptions.nl;
|
|
450
|
+
currentOpts.rap = subOptions.rap;
|
|
451
|
+
currentOpts.rh = subOptions.rh;
|
|
452
|
+
currentOpts.properties = properties;
|
|
453
|
+
}
|
|
454
|
+
this.log('subscribe: pushing topic `%s` and qos `%s` to subs list', currentOpts.topic, currentOpts.qos);
|
|
455
|
+
subs.push(currentOpts);
|
|
456
|
+
}
|
|
457
|
+
};
|
|
458
|
+
if (Array.isArray(topicObject)) {
|
|
459
|
+
topicObject.forEach((topic) => {
|
|
460
|
+
this.log('subscribe: array topic %s', topic);
|
|
461
|
+
parseSub(topic);
|
|
462
|
+
});
|
|
463
|
+
}
|
|
464
|
+
else {
|
|
465
|
+
Object.keys(topicObject).forEach((topic) => {
|
|
466
|
+
this.log('subscribe: object topic %s, %o', topic, topicObject[topic]);
|
|
467
|
+
parseSub(topic, topicObject[topic]);
|
|
468
|
+
});
|
|
469
|
+
}
|
|
470
|
+
if (!subs.length) {
|
|
471
|
+
callback(null, []);
|
|
472
|
+
return this;
|
|
473
|
+
}
|
|
474
|
+
const subscribeProc = () => {
|
|
475
|
+
const messageId = this._nextId();
|
|
476
|
+
if (messageId === null) {
|
|
477
|
+
this.log('No messageId left');
|
|
478
|
+
return false;
|
|
479
|
+
}
|
|
480
|
+
const packet = {
|
|
481
|
+
cmd: 'subscribe',
|
|
482
|
+
subscriptions: subs,
|
|
483
|
+
messageId,
|
|
484
|
+
};
|
|
485
|
+
if (properties) {
|
|
486
|
+
packet.properties = properties;
|
|
487
|
+
}
|
|
488
|
+
if (this.options.resubscribe) {
|
|
489
|
+
this.log('subscribe :: resubscribe true');
|
|
490
|
+
const topics = [];
|
|
491
|
+
subs.forEach((sub) => {
|
|
492
|
+
if (this.options.reconnectPeriod > 0) {
|
|
493
|
+
const topic = { qos: sub.qos };
|
|
494
|
+
if (version === 5) {
|
|
495
|
+
topic.nl = sub.nl || false;
|
|
496
|
+
topic.rap = sub.rap || false;
|
|
497
|
+
topic.rh = sub.rh || 0;
|
|
498
|
+
topic.properties = sub.properties;
|
|
499
|
+
}
|
|
500
|
+
this._resubscribeTopics[sub.topic] = topic;
|
|
501
|
+
topics.push(sub.topic);
|
|
502
|
+
}
|
|
503
|
+
});
|
|
504
|
+
this.messageIdToTopic[packet.messageId] = topics;
|
|
505
|
+
}
|
|
506
|
+
this.outgoing[packet.messageId] = {
|
|
507
|
+
volatile: true,
|
|
508
|
+
cb(err, packet2) {
|
|
509
|
+
if (!err) {
|
|
510
|
+
const { granted } = packet2;
|
|
511
|
+
for (let i = 0; i < granted.length; i += 1) {
|
|
512
|
+
subs[i].qos = granted[i];
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
callback(err, subs);
|
|
516
|
+
},
|
|
517
|
+
};
|
|
518
|
+
this.log('subscribe :: call _sendPacket');
|
|
519
|
+
this._sendPacket(packet);
|
|
520
|
+
return true;
|
|
521
|
+
};
|
|
522
|
+
if (this._storeProcessing ||
|
|
523
|
+
this._storeProcessingQueue.length > 0 ||
|
|
524
|
+
!subscribeProc()) {
|
|
525
|
+
this._storeProcessingQueue.push({
|
|
526
|
+
invoke: subscribeProc,
|
|
527
|
+
callback,
|
|
528
|
+
});
|
|
529
|
+
}
|
|
530
|
+
return this;
|
|
531
|
+
}
|
|
532
|
+
subscribeAsync(topicObject, opts) {
|
|
533
|
+
return new Promise((resolve, reject) => {
|
|
534
|
+
this.subscribe(topicObject, opts, (err, granted) => {
|
|
535
|
+
if (err) {
|
|
536
|
+
reject(err);
|
|
537
|
+
}
|
|
538
|
+
else {
|
|
539
|
+
resolve(granted);
|
|
540
|
+
}
|
|
541
|
+
});
|
|
542
|
+
});
|
|
543
|
+
}
|
|
544
|
+
unsubscribe(topic, opts, callback) {
|
|
545
|
+
if (typeof topic === 'string') {
|
|
546
|
+
topic = [topic];
|
|
547
|
+
}
|
|
548
|
+
if (typeof opts === 'function') {
|
|
549
|
+
callback = opts;
|
|
550
|
+
}
|
|
551
|
+
callback = callback || this.noop;
|
|
552
|
+
const invalidTopic = validations.validateTopics(topic);
|
|
553
|
+
if (invalidTopic !== null) {
|
|
554
|
+
setImmediate(callback, new Error(`Invalid topic ${invalidTopic}`));
|
|
555
|
+
return this;
|
|
556
|
+
}
|
|
557
|
+
if (this._checkDisconnecting(callback)) {
|
|
558
|
+
return this;
|
|
559
|
+
}
|
|
560
|
+
const unsubscribeProc = () => {
|
|
561
|
+
const messageId = this._nextId();
|
|
562
|
+
if (messageId === null) {
|
|
563
|
+
this.log('No messageId left');
|
|
564
|
+
return false;
|
|
565
|
+
}
|
|
566
|
+
const packet = {
|
|
567
|
+
cmd: 'unsubscribe',
|
|
568
|
+
messageId,
|
|
569
|
+
unsubscriptions: [],
|
|
570
|
+
};
|
|
571
|
+
if (typeof topic === 'string') {
|
|
572
|
+
packet.unsubscriptions = [topic];
|
|
573
|
+
}
|
|
574
|
+
else if (Array.isArray(topic)) {
|
|
575
|
+
packet.unsubscriptions = topic;
|
|
576
|
+
}
|
|
577
|
+
if (this.options.resubscribe) {
|
|
578
|
+
packet.unsubscriptions.forEach((topic2) => {
|
|
579
|
+
delete this._resubscribeTopics[topic2];
|
|
580
|
+
});
|
|
581
|
+
}
|
|
582
|
+
if (typeof opts === 'object' && opts.properties) {
|
|
583
|
+
packet.properties = opts.properties;
|
|
584
|
+
}
|
|
585
|
+
this.outgoing[packet.messageId] = {
|
|
586
|
+
volatile: true,
|
|
587
|
+
cb: callback,
|
|
588
|
+
};
|
|
589
|
+
this.log('unsubscribe: call _sendPacket');
|
|
590
|
+
this._sendPacket(packet);
|
|
591
|
+
return true;
|
|
592
|
+
};
|
|
593
|
+
if (this._storeProcessing ||
|
|
594
|
+
this._storeProcessingQueue.length > 0 ||
|
|
595
|
+
!unsubscribeProc()) {
|
|
596
|
+
this._storeProcessingQueue.push({
|
|
597
|
+
invoke: unsubscribeProc,
|
|
598
|
+
callback,
|
|
599
|
+
});
|
|
600
|
+
}
|
|
601
|
+
return this;
|
|
602
|
+
}
|
|
603
|
+
unsubscribeAsync(topic, opts) {
|
|
604
|
+
return new Promise((resolve, reject) => {
|
|
605
|
+
this.unsubscribe(topic, opts, (err) => {
|
|
606
|
+
if (err) {
|
|
607
|
+
reject(err);
|
|
608
|
+
}
|
|
609
|
+
else {
|
|
610
|
+
resolve();
|
|
611
|
+
}
|
|
612
|
+
});
|
|
613
|
+
});
|
|
614
|
+
}
|
|
615
|
+
end(force, opts, cb) {
|
|
616
|
+
this.log('end :: (%s)', this.options.clientId);
|
|
617
|
+
if (force == null || typeof force !== 'boolean') {
|
|
618
|
+
cb = cb || opts;
|
|
619
|
+
opts = force;
|
|
620
|
+
force = false;
|
|
621
|
+
}
|
|
622
|
+
if (typeof opts !== 'object') {
|
|
623
|
+
cb = cb || opts;
|
|
624
|
+
opts = null;
|
|
625
|
+
}
|
|
626
|
+
this.log('end :: cb? %s', !!cb);
|
|
627
|
+
if (!cb || typeof cb !== 'function') {
|
|
628
|
+
cb = this.noop;
|
|
629
|
+
}
|
|
630
|
+
const closeStores = () => {
|
|
631
|
+
this.log('end :: closeStores: closing incoming and outgoing stores');
|
|
632
|
+
this.disconnected = true;
|
|
633
|
+
this.incomingStore.close((e1) => {
|
|
634
|
+
this.outgoingStore.close((e2) => {
|
|
635
|
+
this.log('end :: closeStores: emitting end');
|
|
636
|
+
this.emit('end');
|
|
637
|
+
if (cb) {
|
|
638
|
+
const err = e1 || e2;
|
|
639
|
+
this.log('end :: closeStores: invoking callback with args');
|
|
640
|
+
cb(err);
|
|
641
|
+
}
|
|
642
|
+
});
|
|
643
|
+
});
|
|
644
|
+
if (this._deferredReconnect) {
|
|
645
|
+
this._deferredReconnect();
|
|
646
|
+
}
|
|
647
|
+
};
|
|
648
|
+
const finish = () => {
|
|
649
|
+
this.log('end :: (%s) :: finish :: calling _cleanUp with force %s', this.options.clientId, force);
|
|
650
|
+
this._cleanUp(force, () => {
|
|
651
|
+
this.log('end :: finish :: calling process.nextTick on closeStores');
|
|
652
|
+
nextTick(closeStores);
|
|
653
|
+
}, opts);
|
|
654
|
+
};
|
|
655
|
+
if (this.disconnecting) {
|
|
656
|
+
cb();
|
|
657
|
+
return this;
|
|
658
|
+
}
|
|
659
|
+
this._clearReconnect();
|
|
660
|
+
this.disconnecting = true;
|
|
661
|
+
if (!force && Object.keys(this.outgoing).length > 0) {
|
|
662
|
+
this.log('end :: (%s) :: calling finish in 10ms once outgoing is empty', this.options.clientId);
|
|
663
|
+
this.once('outgoingEmpty', setTimeout.bind(null, finish, 10));
|
|
664
|
+
}
|
|
665
|
+
else {
|
|
666
|
+
this.log('end :: (%s) :: immediately calling finish', this.options.clientId);
|
|
667
|
+
finish();
|
|
668
|
+
}
|
|
669
|
+
return this;
|
|
670
|
+
}
|
|
671
|
+
endAsync(force, opts) {
|
|
672
|
+
return new Promise((resolve, reject) => {
|
|
673
|
+
this.end(force, opts, (err) => {
|
|
674
|
+
if (err) {
|
|
675
|
+
reject(err);
|
|
676
|
+
}
|
|
677
|
+
else {
|
|
678
|
+
resolve();
|
|
679
|
+
}
|
|
680
|
+
});
|
|
681
|
+
});
|
|
682
|
+
}
|
|
683
|
+
removeOutgoingMessage(messageId) {
|
|
684
|
+
if (this.outgoing[messageId]) {
|
|
685
|
+
const { cb } = this.outgoing[messageId];
|
|
686
|
+
this._removeOutgoingAndStoreMessage(messageId, () => {
|
|
687
|
+
cb(new Error('Message removed'));
|
|
688
|
+
});
|
|
689
|
+
}
|
|
690
|
+
return this;
|
|
691
|
+
}
|
|
692
|
+
reconnect(opts) {
|
|
693
|
+
this.log('client reconnect');
|
|
694
|
+
const f = () => {
|
|
695
|
+
if (opts) {
|
|
696
|
+
this.options.incomingStore = opts.incomingStore;
|
|
697
|
+
this.options.outgoingStore = opts.outgoingStore;
|
|
698
|
+
}
|
|
699
|
+
else {
|
|
700
|
+
this.options.incomingStore = null;
|
|
701
|
+
this.options.outgoingStore = null;
|
|
702
|
+
}
|
|
703
|
+
this.incomingStore = this.options.incomingStore || new store_1.default();
|
|
704
|
+
this.outgoingStore = this.options.outgoingStore || new store_1.default();
|
|
705
|
+
this.disconnecting = false;
|
|
706
|
+
this.disconnected = false;
|
|
707
|
+
this._deferredReconnect = null;
|
|
708
|
+
this._reconnect();
|
|
709
|
+
};
|
|
710
|
+
if (this.disconnecting && !this.disconnected) {
|
|
711
|
+
this._deferredReconnect = f;
|
|
712
|
+
}
|
|
713
|
+
else {
|
|
714
|
+
f();
|
|
715
|
+
}
|
|
716
|
+
return this;
|
|
717
|
+
}
|
|
718
|
+
_flushVolatile() {
|
|
719
|
+
if (this.outgoing) {
|
|
720
|
+
this.log('_flushVolatile :: deleting volatile messages from the queue and setting their callbacks as error function');
|
|
721
|
+
Object.keys(this.outgoing).forEach((messageId) => {
|
|
722
|
+
if (this.outgoing[messageId].volatile &&
|
|
723
|
+
typeof this.outgoing[messageId].cb === 'function') {
|
|
724
|
+
this.outgoing[messageId].cb(new Error('Connection closed'));
|
|
725
|
+
delete this.outgoing[messageId];
|
|
726
|
+
}
|
|
727
|
+
});
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
_flush() {
|
|
731
|
+
if (this.outgoing) {
|
|
732
|
+
this.log('_flush: queue exists? %b', !!this.outgoing);
|
|
733
|
+
Object.keys(this.outgoing).forEach((messageId) => {
|
|
734
|
+
if (typeof this.outgoing[messageId].cb === 'function') {
|
|
735
|
+
this.outgoing[messageId].cb(new Error('Connection closed'));
|
|
736
|
+
delete this.outgoing[messageId];
|
|
737
|
+
}
|
|
738
|
+
});
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
_removeTopicAliasAndRecoverTopicName(packet) {
|
|
742
|
+
let alias;
|
|
743
|
+
if (packet.properties) {
|
|
744
|
+
alias = packet.properties.topicAlias;
|
|
745
|
+
}
|
|
746
|
+
let topic = packet.topic.toString();
|
|
747
|
+
this.log('_removeTopicAliasAndRecoverTopicName :: alias %d, topic %o', alias, topic);
|
|
748
|
+
if (topic.length === 0) {
|
|
749
|
+
if (typeof alias === 'undefined') {
|
|
750
|
+
return new Error('Unregistered Topic Alias');
|
|
751
|
+
}
|
|
752
|
+
topic = this.topicAliasSend.getTopicByAlias(alias);
|
|
753
|
+
if (typeof topic === 'undefined') {
|
|
754
|
+
return new Error('Unregistered Topic Alias');
|
|
755
|
+
}
|
|
756
|
+
packet.topic = topic;
|
|
757
|
+
}
|
|
758
|
+
if (alias) {
|
|
759
|
+
delete packet.properties.topicAlias;
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
_checkDisconnecting(callback) {
|
|
763
|
+
if (this.disconnecting) {
|
|
764
|
+
if (callback && callback !== this.noop) {
|
|
765
|
+
callback(new Error('client disconnecting'));
|
|
766
|
+
}
|
|
767
|
+
else {
|
|
768
|
+
this.emit('error', new Error('client disconnecting'));
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
return this.disconnecting;
|
|
772
|
+
}
|
|
773
|
+
_reconnect() {
|
|
774
|
+
this.log('_reconnect: emitting reconnect to client');
|
|
775
|
+
this.emit('reconnect');
|
|
776
|
+
if (this.connected) {
|
|
777
|
+
this.end(() => {
|
|
778
|
+
this.connect();
|
|
779
|
+
});
|
|
780
|
+
this.log('client already connected. disconnecting first.');
|
|
781
|
+
}
|
|
782
|
+
else {
|
|
783
|
+
this.log('_reconnect: calling connect');
|
|
784
|
+
this.connect();
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
_setupReconnect() {
|
|
788
|
+
if (!this.disconnecting &&
|
|
789
|
+
!this.reconnectTimer &&
|
|
790
|
+
this.options.reconnectPeriod > 0) {
|
|
791
|
+
if (!this.reconnecting) {
|
|
792
|
+
this.log('_setupReconnect :: emit `offline` state');
|
|
793
|
+
this.emit('offline');
|
|
794
|
+
this.log('_setupReconnect :: set `reconnecting` to `true`');
|
|
795
|
+
this.reconnecting = true;
|
|
796
|
+
}
|
|
797
|
+
this.log('_setupReconnect :: setting reconnectTimer for %d ms', this.options.reconnectPeriod);
|
|
798
|
+
this.reconnectTimer = setInterval(() => {
|
|
799
|
+
this.log('reconnectTimer :: reconnect triggered!');
|
|
800
|
+
this._reconnect();
|
|
801
|
+
}, this.options.reconnectPeriod);
|
|
802
|
+
}
|
|
803
|
+
else {
|
|
804
|
+
this.log('_setupReconnect :: doing nothing...');
|
|
805
|
+
}
|
|
806
|
+
}
|
|
807
|
+
_clearReconnect() {
|
|
808
|
+
this.log('_clearReconnect : clearing reconnect timer');
|
|
809
|
+
if (this.reconnectTimer) {
|
|
810
|
+
clearInterval(this.reconnectTimer);
|
|
811
|
+
this.reconnectTimer = null;
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
_cleanUp(forced, done, opts = {}) {
|
|
815
|
+
if (done) {
|
|
816
|
+
this.log('_cleanUp :: done callback provided for on stream close');
|
|
817
|
+
this.stream.on('close', done);
|
|
818
|
+
}
|
|
819
|
+
this.log('_cleanUp :: forced? %s', forced);
|
|
820
|
+
if (forced) {
|
|
821
|
+
if (this.options.reconnectPeriod === 0 && this.options.clean) {
|
|
822
|
+
this._flush();
|
|
823
|
+
}
|
|
824
|
+
this.log('_cleanUp :: (%s) :: destroying stream', this.options.clientId);
|
|
825
|
+
this.stream.destroy();
|
|
826
|
+
}
|
|
827
|
+
else {
|
|
828
|
+
const packet = Object.assign({ cmd: 'disconnect' }, opts);
|
|
829
|
+
this.log('_cleanUp :: (%s) :: call _sendPacket with disconnect packet', this.options.clientId);
|
|
830
|
+
this._sendPacket(packet, () => {
|
|
831
|
+
this.log('_cleanUp :: (%s) :: destroying stream', this.options.clientId);
|
|
832
|
+
setImmediate(() => {
|
|
833
|
+
this.stream.end(() => {
|
|
834
|
+
this.log('_cleanUp :: (%s) :: stream destroyed', this.options.clientId);
|
|
835
|
+
});
|
|
836
|
+
});
|
|
837
|
+
});
|
|
838
|
+
}
|
|
839
|
+
if (!this.disconnecting) {
|
|
840
|
+
this.log('_cleanUp :: client not disconnecting. Clearing and resetting reconnect.');
|
|
841
|
+
this._clearReconnect();
|
|
842
|
+
this._setupReconnect();
|
|
843
|
+
}
|
|
844
|
+
if (this.pingTimer !== null) {
|
|
845
|
+
this.log('_cleanUp :: clearing pingTimer');
|
|
846
|
+
this.pingTimer.clear();
|
|
847
|
+
this.pingTimer = null;
|
|
848
|
+
}
|
|
849
|
+
if (done && !this.connected) {
|
|
850
|
+
this.log('_cleanUp :: (%s) :: removing stream `done` callback `close` listener', this.options.clientId);
|
|
851
|
+
this.stream.removeListener('close', done);
|
|
852
|
+
done();
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
_storeAndSend(packet, cb, cbStorePut) {
|
|
856
|
+
this.log('storeAndSend :: store packet with cmd %s to outgoingStore', packet.cmd);
|
|
857
|
+
let storePacket = packet;
|
|
858
|
+
let err;
|
|
859
|
+
if (storePacket.cmd === 'publish') {
|
|
860
|
+
storePacket = (0, default_1.default)(packet);
|
|
861
|
+
err = this._removeTopicAliasAndRecoverTopicName(storePacket);
|
|
862
|
+
if (err) {
|
|
863
|
+
return cb && cb(err);
|
|
864
|
+
}
|
|
865
|
+
}
|
|
866
|
+
this.outgoingStore.put(storePacket, (err2) => {
|
|
867
|
+
if (err2) {
|
|
868
|
+
return cb && cb(err2);
|
|
869
|
+
}
|
|
870
|
+
cbStorePut();
|
|
871
|
+
this._writePacket(packet, cb);
|
|
872
|
+
});
|
|
873
|
+
}
|
|
874
|
+
_applyTopicAlias(packet) {
|
|
875
|
+
if (this.options.protocolVersion === 5) {
|
|
876
|
+
if (packet.cmd === 'publish') {
|
|
877
|
+
let alias;
|
|
878
|
+
if (packet.properties) {
|
|
879
|
+
alias = packet.properties.topicAlias;
|
|
880
|
+
}
|
|
881
|
+
const topic = packet.topic.toString();
|
|
882
|
+
if (this.topicAliasSend) {
|
|
883
|
+
if (alias) {
|
|
884
|
+
if (topic.length !== 0) {
|
|
885
|
+
this.log('applyTopicAlias :: register topic: %s - alias: %d', topic, alias);
|
|
886
|
+
if (!this.topicAliasSend.put(topic, alias)) {
|
|
887
|
+
this.log('applyTopicAlias :: error out of range. topic: %s - alias: %d', topic, alias);
|
|
888
|
+
return new Error('Sending Topic Alias out of range');
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
}
|
|
892
|
+
else if (topic.length !== 0) {
|
|
893
|
+
if (this.options.autoAssignTopicAlias) {
|
|
894
|
+
alias = this.topicAliasSend.getAliasByTopic(topic);
|
|
895
|
+
if (alias) {
|
|
896
|
+
packet.topic = '';
|
|
897
|
+
packet.properties = Object.assign(Object.assign({}, packet.properties), { topicAlias: alias });
|
|
898
|
+
this.log('applyTopicAlias :: auto assign(use) topic: %s - alias: %d', topic, alias);
|
|
899
|
+
}
|
|
900
|
+
else {
|
|
901
|
+
alias = this.topicAliasSend.getLruAlias();
|
|
902
|
+
this.topicAliasSend.put(topic, alias);
|
|
903
|
+
packet.properties = Object.assign(Object.assign({}, packet.properties), { topicAlias: alias });
|
|
904
|
+
this.log('applyTopicAlias :: auto assign topic: %s - alias: %d', topic, alias);
|
|
905
|
+
}
|
|
906
|
+
}
|
|
907
|
+
else if (this.options.autoUseTopicAlias) {
|
|
908
|
+
alias = this.topicAliasSend.getAliasByTopic(topic);
|
|
909
|
+
if (alias) {
|
|
910
|
+
packet.topic = '';
|
|
911
|
+
packet.properties = Object.assign(Object.assign({}, packet.properties), { topicAlias: alias });
|
|
912
|
+
this.log('applyTopicAlias :: auto use topic: %s - alias: %d', topic, alias);
|
|
913
|
+
}
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
}
|
|
917
|
+
else if (alias) {
|
|
918
|
+
this.log('applyTopicAlias :: error out of range. topic: %s - alias: %d', topic, alias);
|
|
919
|
+
return new Error('Sending Topic Alias out of range');
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
_noop(err) {
|
|
925
|
+
this.log('noop ::', err);
|
|
926
|
+
}
|
|
927
|
+
_writePacket(packet, cb) {
|
|
928
|
+
this.log('_writePacket :: packet: %O', packet);
|
|
929
|
+
this.log('_writePacket :: emitting `packetsend`');
|
|
930
|
+
this.emit('packetsend', packet);
|
|
931
|
+
this._shiftPingInterval();
|
|
932
|
+
this.log('_writePacket :: writing to stream');
|
|
933
|
+
const result = mqtt_packet_1.default.writeToStream(packet, this.stream, this.options);
|
|
934
|
+
this.log('_writePacket :: writeToStream result %s', result);
|
|
935
|
+
if (!result && cb && cb !== this.noop) {
|
|
936
|
+
this.log('_writePacket :: handle events on `drain` once through callback.');
|
|
937
|
+
this.stream.once('drain', cb);
|
|
938
|
+
}
|
|
939
|
+
else if (cb) {
|
|
940
|
+
this.log('_writePacket :: invoking cb');
|
|
941
|
+
cb();
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
_sendPacket(packet, cb, cbStorePut, noStore) {
|
|
945
|
+
this.log('_sendPacket :: (%s) :: start', this.options.clientId);
|
|
946
|
+
cbStorePut = cbStorePut || this.noop;
|
|
947
|
+
cb = cb || this.noop;
|
|
948
|
+
const err = this._applyTopicAlias(packet);
|
|
949
|
+
if (err) {
|
|
950
|
+
cb(err);
|
|
951
|
+
return;
|
|
952
|
+
}
|
|
953
|
+
if (!this.connected) {
|
|
954
|
+
if (packet.cmd === 'auth') {
|
|
955
|
+
this._writePacket(packet, cb);
|
|
956
|
+
return;
|
|
957
|
+
}
|
|
958
|
+
this.log('_sendPacket :: client not connected. Storing packet offline.');
|
|
959
|
+
this._storePacket(packet, cb, cbStorePut);
|
|
960
|
+
return;
|
|
961
|
+
}
|
|
962
|
+
if (noStore) {
|
|
963
|
+
this._writePacket(packet, cb);
|
|
964
|
+
return;
|
|
965
|
+
}
|
|
966
|
+
switch (packet.cmd) {
|
|
967
|
+
case 'publish':
|
|
968
|
+
break;
|
|
969
|
+
case 'pubrel':
|
|
970
|
+
this._storeAndSend(packet, cb, cbStorePut);
|
|
971
|
+
return;
|
|
972
|
+
default:
|
|
973
|
+
this._writePacket(packet, cb);
|
|
974
|
+
return;
|
|
975
|
+
}
|
|
976
|
+
switch (packet.qos) {
|
|
977
|
+
case 2:
|
|
978
|
+
case 1:
|
|
979
|
+
this._storeAndSend(packet, cb, cbStorePut);
|
|
980
|
+
break;
|
|
981
|
+
case 0:
|
|
982
|
+
default:
|
|
983
|
+
this._writePacket(packet, cb);
|
|
984
|
+
break;
|
|
985
|
+
}
|
|
986
|
+
this.log('_sendPacket :: (%s) :: end', this.options.clientId);
|
|
987
|
+
}
|
|
988
|
+
_storePacket(packet, cb, cbStorePut) {
|
|
989
|
+
this.log('_storePacket :: packet: %o', packet);
|
|
990
|
+
this.log('_storePacket :: cb? %s', !!cb);
|
|
991
|
+
cbStorePut = cbStorePut || this.noop;
|
|
992
|
+
let storePacket = packet;
|
|
993
|
+
if (storePacket.cmd === 'publish') {
|
|
994
|
+
storePacket = (0, default_1.default)(packet);
|
|
995
|
+
const err = this._removeTopicAliasAndRecoverTopicName(storePacket);
|
|
996
|
+
if (err) {
|
|
997
|
+
return cb && cb(err);
|
|
998
|
+
}
|
|
999
|
+
}
|
|
1000
|
+
const qos = storePacket.qos || 0;
|
|
1001
|
+
if ((qos === 0 && this.queueQoSZero) || storePacket.cmd !== 'publish') {
|
|
1002
|
+
this.queue.push({ packet: storePacket, cb });
|
|
1003
|
+
}
|
|
1004
|
+
else if (qos > 0) {
|
|
1005
|
+
cb = this.outgoing[storePacket.messageId]
|
|
1006
|
+
? this.outgoing[storePacket.messageId].cb
|
|
1007
|
+
: null;
|
|
1008
|
+
this.outgoingStore.put(storePacket, (err) => {
|
|
1009
|
+
if (err) {
|
|
1010
|
+
return cb && cb(err);
|
|
1011
|
+
}
|
|
1012
|
+
cbStorePut();
|
|
1013
|
+
});
|
|
1014
|
+
}
|
|
1015
|
+
else if (cb) {
|
|
1016
|
+
cb(new Error('No connection to broker'));
|
|
1017
|
+
}
|
|
1018
|
+
}
|
|
1019
|
+
_setupPingTimer() {
|
|
1020
|
+
this.log('_setupPingTimer :: keepalive %d (seconds)', this.options.keepalive);
|
|
1021
|
+
if (!this.pingTimer && this.options.keepalive) {
|
|
1022
|
+
this.pingResp = true;
|
|
1023
|
+
this.pingTimer = (0, reinterval_1.default)(() => {
|
|
1024
|
+
this._checkPing();
|
|
1025
|
+
}, this.options.keepalive * 1000);
|
|
1026
|
+
}
|
|
1027
|
+
}
|
|
1028
|
+
_shiftPingInterval() {
|
|
1029
|
+
if (this.pingTimer &&
|
|
1030
|
+
this.options.keepalive &&
|
|
1031
|
+
this.options.reschedulePings) {
|
|
1032
|
+
this.pingTimer.reschedule(this.options.keepalive * 1000);
|
|
1033
|
+
}
|
|
1034
|
+
}
|
|
1035
|
+
_checkPing() {
|
|
1036
|
+
this.log('_checkPing :: checking ping...');
|
|
1037
|
+
if (this.pingResp) {
|
|
1038
|
+
this.log('_checkPing :: ping response received. Clearing flag and sending `pingreq`');
|
|
1039
|
+
this.pingResp = false;
|
|
1040
|
+
this._sendPacket({ cmd: 'pingreq' });
|
|
1041
|
+
}
|
|
1042
|
+
else {
|
|
1043
|
+
this.log('_checkPing :: calling _cleanUp with force true');
|
|
1044
|
+
this._cleanUp(true);
|
|
1045
|
+
}
|
|
1046
|
+
}
|
|
1047
|
+
_resubscribe() {
|
|
1048
|
+
this.log('_resubscribe');
|
|
1049
|
+
const _resubscribeTopicsKeys = Object.keys(this._resubscribeTopics);
|
|
1050
|
+
if (!this._firstConnection &&
|
|
1051
|
+
(this.options.clean ||
|
|
1052
|
+
(this.options.protocolVersion === 5 &&
|
|
1053
|
+
!this.connackPacket.sessionPresent)) &&
|
|
1054
|
+
_resubscribeTopicsKeys.length > 0) {
|
|
1055
|
+
if (this.options.resubscribe) {
|
|
1056
|
+
if (this.options.protocolVersion === 5) {
|
|
1057
|
+
this.log('_resubscribe: protocolVersion 5');
|
|
1058
|
+
for (let topicI = 0; topicI < _resubscribeTopicsKeys.length; topicI++) {
|
|
1059
|
+
const resubscribeTopic = {};
|
|
1060
|
+
resubscribeTopic[_resubscribeTopicsKeys[topicI]] =
|
|
1061
|
+
this._resubscribeTopics[_resubscribeTopicsKeys[topicI]];
|
|
1062
|
+
resubscribeTopic.resubscribe = true;
|
|
1063
|
+
this.subscribe(resubscribeTopic, {
|
|
1064
|
+
properties: resubscribeTopic[_resubscribeTopicsKeys[topicI]]
|
|
1065
|
+
.properties,
|
|
1066
|
+
});
|
|
1067
|
+
}
|
|
1068
|
+
}
|
|
1069
|
+
else {
|
|
1070
|
+
this._resubscribeTopics.resubscribe = true;
|
|
1071
|
+
this.subscribe(this._resubscribeTopics);
|
|
1072
|
+
}
|
|
1073
|
+
}
|
|
1074
|
+
else {
|
|
1075
|
+
this._resubscribeTopics = {};
|
|
1076
|
+
}
|
|
1077
|
+
}
|
|
1078
|
+
this._firstConnection = false;
|
|
1079
|
+
}
|
|
1080
|
+
_onConnect(packet) {
|
|
1081
|
+
if (this.disconnected) {
|
|
1082
|
+
this.emit('connect', packet);
|
|
1083
|
+
return;
|
|
1084
|
+
}
|
|
1085
|
+
this.connackPacket = packet;
|
|
1086
|
+
this.messageIdProvider.clear();
|
|
1087
|
+
this._setupPingTimer();
|
|
1088
|
+
this.connected = true;
|
|
1089
|
+
const startStreamProcess = () => {
|
|
1090
|
+
let outStore = this.outgoingStore.createStream();
|
|
1091
|
+
const remove = () => {
|
|
1092
|
+
outStore.destroy();
|
|
1093
|
+
outStore = null;
|
|
1094
|
+
this._flushStoreProcessingQueue();
|
|
1095
|
+
clearStoreProcessing();
|
|
1096
|
+
};
|
|
1097
|
+
const clearStoreProcessing = () => {
|
|
1098
|
+
this._storeProcessing = false;
|
|
1099
|
+
this._packetIdsDuringStoreProcessing = {};
|
|
1100
|
+
};
|
|
1101
|
+
this.once('close', remove);
|
|
1102
|
+
outStore.on('error', (err) => {
|
|
1103
|
+
clearStoreProcessing();
|
|
1104
|
+
this._flushStoreProcessingQueue();
|
|
1105
|
+
this.removeListener('close', remove);
|
|
1106
|
+
this.emit('error', err);
|
|
1107
|
+
});
|
|
1108
|
+
const storeDeliver = () => {
|
|
1109
|
+
if (!outStore) {
|
|
1110
|
+
return;
|
|
1111
|
+
}
|
|
1112
|
+
const packet2 = outStore.read(1);
|
|
1113
|
+
let cb;
|
|
1114
|
+
if (!packet2) {
|
|
1115
|
+
outStore.once('readable', storeDeliver);
|
|
1116
|
+
return;
|
|
1117
|
+
}
|
|
1118
|
+
this._storeProcessing = true;
|
|
1119
|
+
if (this._packetIdsDuringStoreProcessing[packet2.messageId]) {
|
|
1120
|
+
storeDeliver();
|
|
1121
|
+
return;
|
|
1122
|
+
}
|
|
1123
|
+
if (!this.disconnecting && !this.reconnectTimer) {
|
|
1124
|
+
cb = this.outgoing[packet2.messageId]
|
|
1125
|
+
? this.outgoing[packet2.messageId].cb
|
|
1126
|
+
: null;
|
|
1127
|
+
this.outgoing[packet2.messageId] = {
|
|
1128
|
+
volatile: false,
|
|
1129
|
+
cb(err, status) {
|
|
1130
|
+
if (cb) {
|
|
1131
|
+
cb(err, status);
|
|
1132
|
+
}
|
|
1133
|
+
storeDeliver();
|
|
1134
|
+
},
|
|
1135
|
+
};
|
|
1136
|
+
this._packetIdsDuringStoreProcessing[packet2.messageId] =
|
|
1137
|
+
true;
|
|
1138
|
+
if (this.messageIdProvider.register(packet2.messageId)) {
|
|
1139
|
+
this._sendPacket(packet2, undefined, undefined, true);
|
|
1140
|
+
}
|
|
1141
|
+
else {
|
|
1142
|
+
this.log('messageId: %d has already used.', packet2.messageId);
|
|
1143
|
+
}
|
|
1144
|
+
}
|
|
1145
|
+
else if (outStore.destroy) {
|
|
1146
|
+
outStore.destroy();
|
|
1147
|
+
}
|
|
1148
|
+
};
|
|
1149
|
+
outStore.on('end', () => {
|
|
1150
|
+
let allProcessed = true;
|
|
1151
|
+
for (const id in this._packetIdsDuringStoreProcessing) {
|
|
1152
|
+
if (!this._packetIdsDuringStoreProcessing[id]) {
|
|
1153
|
+
allProcessed = false;
|
|
1154
|
+
break;
|
|
1155
|
+
}
|
|
1156
|
+
}
|
|
1157
|
+
if (allProcessed) {
|
|
1158
|
+
clearStoreProcessing();
|
|
1159
|
+
this.removeListener('close', remove);
|
|
1160
|
+
this._invokeAllStoreProcessingQueue();
|
|
1161
|
+
this.emit('connect', packet);
|
|
1162
|
+
}
|
|
1163
|
+
else {
|
|
1164
|
+
startStreamProcess();
|
|
1165
|
+
}
|
|
1166
|
+
});
|
|
1167
|
+
storeDeliver();
|
|
1168
|
+
};
|
|
1169
|
+
startStreamProcess();
|
|
1170
|
+
}
|
|
1171
|
+
_invokeStoreProcessingQueue() {
|
|
1172
|
+
if (!this._storeProcessing && this._storeProcessingQueue.length > 0) {
|
|
1173
|
+
const f = this._storeProcessingQueue[0];
|
|
1174
|
+
if (f && f.invoke()) {
|
|
1175
|
+
this._storeProcessingQueue.shift();
|
|
1176
|
+
return true;
|
|
1177
|
+
}
|
|
1178
|
+
}
|
|
1179
|
+
return false;
|
|
1180
|
+
}
|
|
1181
|
+
_invokeAllStoreProcessingQueue() {
|
|
1182
|
+
while (this._invokeStoreProcessingQueue()) {
|
|
1183
|
+
}
|
|
1184
|
+
}
|
|
1185
|
+
_flushStoreProcessingQueue() {
|
|
1186
|
+
for (const f of this._storeProcessingQueue) {
|
|
1187
|
+
if (f.cbStorePut)
|
|
1188
|
+
f.cbStorePut(new Error('Connection closed'));
|
|
1189
|
+
if (f.callback)
|
|
1190
|
+
f.callback(new Error('Connection closed'));
|
|
1191
|
+
}
|
|
1192
|
+
this._storeProcessingQueue.splice(0);
|
|
1193
|
+
}
|
|
1194
|
+
_removeOutgoingAndStoreMessage(messageId, cb) {
|
|
1195
|
+
delete this.outgoing[messageId];
|
|
1196
|
+
this.outgoingStore.del({ messageId }, (err, packet) => {
|
|
1197
|
+
cb(err, packet);
|
|
1198
|
+
this.messageIdProvider.deallocate(messageId);
|
|
1199
|
+
this._invokeStoreProcessingQueue();
|
|
1200
|
+
});
|
|
1201
|
+
}
|
|
1202
|
+
}
|
|
1203
|
+
exports.default = MqttClient;
|
|
1204
|
+
//# sourceMappingURL=client.js.map
|