abmqtts 0.0.6 → 0.0.8
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/dist/fbns/fbns.client.js +162 -208
- package/dist/mqttot/mqttot.client.d.ts +2 -2
- package/dist/mqttot/mqttot.client.js +87 -99
- package/dist/mqttot/mqttot.connect.request.packet.d.ts +1 -1
- package/dist/mqttot/mqttot.connect.response.packet.d.ts +1 -1
- package/dist/mqttot/mqttot.connect.response.packet.js +15 -18
- package/dist/realtime/commands/commands.d.ts +1 -1
- package/dist/realtime/commands/direct.commands.d.ts +2 -2
- package/dist/realtime/mixins/message-sync.mixin.js +65 -69
- package/dist/realtime/mixins/realtime-sub.mixin.js +47 -53
- package/dist/realtime/realtime.client.d.ts +1 -1
- package/dist/realtime/realtime.client.events.d.ts +2 -2
- package/dist/realtime/realtime.client.js +179 -230
- package/dist/shared.d.ts +1 -1
- package/package.json +2 -2
package/dist/fbns/fbns.client.js
CHANGED
|
@@ -6,222 +6,176 @@ const fbns_device_auth_1 = require("./fbns.device-auth");
|
|
|
6
6
|
const shared_1 = require("../shared");
|
|
7
7
|
const mqttot_1 = require("../mqttot");
|
|
8
8
|
const chance_1 = require("chance");
|
|
9
|
-
const obmqtt_1 = require("
|
|
9
|
+
const obmqtt_1 = require("obmqtt");
|
|
10
10
|
const errors_1 = require("../errors");
|
|
11
11
|
const eventemitter3_1 = require("eventemitter3");
|
|
12
12
|
const fbns_utilities_1 = require("./fbns.utilities");
|
|
13
13
|
class FbnsClient extends eventemitter3_1.EventEmitter {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
14
|
+
get auth() {
|
|
15
|
+
return this._auth;
|
|
16
|
+
}
|
|
17
|
+
set auth(value) {
|
|
18
|
+
this._auth = value;
|
|
19
|
+
}
|
|
20
|
+
constructor(ig) {
|
|
21
|
+
super();
|
|
22
|
+
this.ig = ig;
|
|
23
|
+
this.fbnsDebug = (0, shared_1.debugChannel)('fbns');
|
|
24
|
+
this.safeDisconnect = false;
|
|
25
|
+
this._auth = new fbns_device_auth_1.FbnsDeviceAuth(this.ig);
|
|
26
|
+
}
|
|
27
|
+
buildConnection() {
|
|
28
|
+
this.fbnsDebug('Constructing connection');
|
|
29
|
+
this.conn = new mqttot_1.MQTToTConnection({
|
|
30
|
+
clientIdentifier: this._auth.clientId,
|
|
31
|
+
clientInfo: {
|
|
32
|
+
userId: BigInt(this._auth.userId),
|
|
33
|
+
userAgent: (0, shared_1.createFbnsUserAgent)(this.ig),
|
|
34
|
+
clientCapabilities: 183,
|
|
35
|
+
endpointCapabilities: 128,
|
|
36
|
+
publishFormat: 1,
|
|
37
|
+
noAutomaticForeground: true,
|
|
38
|
+
makeUserAvailableInForeground: false,
|
|
39
|
+
deviceId: this._auth.deviceId,
|
|
40
|
+
isInitiallyForeground: false,
|
|
41
|
+
networkType: 1,
|
|
42
|
+
networkSubtype: 0,
|
|
43
|
+
clientMqttSessionId: BigInt(Date.now()) & BigInt(0xffffffff),
|
|
44
|
+
subscribeTopics: [76, 80, 231],
|
|
45
|
+
clientType: 'device_auth',
|
|
46
|
+
appId: BigInt(567310203415052),
|
|
47
|
+
deviceSecret: this._auth.deviceSecret,
|
|
48
|
+
anotherUnknown: BigInt(-1),
|
|
49
|
+
clientStack: 3,
|
|
50
|
+
},
|
|
51
|
+
password: this._auth.password,
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
async connect({ enableTrace, autoReconnect, socksOptions, additionalTlsOptions, } = {}) {
|
|
55
|
+
this.fbnsDebug('Connecting to FBNS...');
|
|
56
|
+
this.auth.update();
|
|
57
|
+
this.client = new mqttot_1.MQTToTClient({
|
|
58
|
+
url: constants_1.FBNS.HOST_NAME_V6,
|
|
59
|
+
payloadProvider: () => {
|
|
60
|
+
this.buildConnection();
|
|
61
|
+
if (!this.conn) {
|
|
62
|
+
throw new errors_1.InvalidStateError("No connection created - can't build provider");
|
|
63
|
+
}
|
|
64
|
+
return (0, shared_1.compressDeflate)(this.conn.toThrift());
|
|
65
|
+
},
|
|
66
|
+
enableTrace,
|
|
67
|
+
autoReconnect: autoReconnect ?? true,
|
|
68
|
+
requirePayload: true,
|
|
69
|
+
socksOptions,
|
|
70
|
+
additionalOptions: additionalTlsOptions,
|
|
71
|
+
});
|
|
72
|
+
this.client.on('warning', w => this.emit('warning', w));
|
|
73
|
+
this.client.on('error', e => this.emit('error', e));
|
|
74
|
+
this.client.on('disconnect', reason => this.safeDisconnect
|
|
75
|
+
? this.emit('disconnect', reason && JSON.stringify(reason))
|
|
76
|
+
: this.emit('error', new errors_1.ClientDisconnectedError(`MQTToTClient got disconnected. Reason: ${reason && JSON.stringify(reason)}`)));
|
|
77
|
+
this.client.listen(constants_1.FbnsTopics.FBNS_MESSAGE.id, msg => this.handleMessage(msg));
|
|
78
|
+
this.client.listen({
|
|
79
|
+
topic: constants_1.FbnsTopics.FBNS_EXP_LOGGING.id,
|
|
80
|
+
transformer: async (msg) => JSON.parse((await (0, shared_1.tryUnzipAsync)(msg.payload)).toString()),
|
|
81
|
+
}, msg => this.emit('logging', msg));
|
|
82
|
+
this.client.listen(constants_1.FbnsTopics.PP.id, msg => this.emit('pp', msg.payload.toString()));
|
|
83
|
+
this.client.on('connect', async (res) => {
|
|
84
|
+
if (!this.client) {
|
|
85
|
+
throw new obmqtt_1.IllegalStateError('No client registered but an event was received');
|
|
86
|
+
}
|
|
87
|
+
this.fbnsDebug('Connected to MQTT');
|
|
88
|
+
if (!res?.payload?.length) {
|
|
89
|
+
this.fbnsDebug(`Received empty connect packet. Reason: ${res.errorName}; Try resetting your fbns state!`);
|
|
90
|
+
this.emit('error', new errors_1.EmptyPacketError('Received empty connect packet. Try resetting your fbns state!'));
|
|
91
|
+
await this.client.disconnect();
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
const payload = res.payload.toString('utf8');
|
|
95
|
+
this.fbnsDebug(`Received auth: ${payload}`);
|
|
96
|
+
this._auth.read(payload);
|
|
97
|
+
this.emit('auth', this.auth);
|
|
98
|
+
await this.client.mqttotPublish({
|
|
99
|
+
topic: constants_1.FbnsTopics.FBNS_REG_REQ.id,
|
|
100
|
+
payload: Buffer.from(JSON.stringify({
|
|
101
|
+
pkg_name: constants_1.INSTAGRAM_PACKAGE_NAME,
|
|
102
|
+
appid: this.ig.state.fbAnalyticsApplicationId,
|
|
103
|
+
}), 'utf8'),
|
|
104
|
+
qosLevel: 1,
|
|
105
|
+
});
|
|
106
|
+
// this.buildConnection(); ?
|
|
107
|
+
});
|
|
108
|
+
await this.client
|
|
109
|
+
.connect({
|
|
110
|
+
keepAlive: 60,
|
|
111
|
+
protocolLevel: 3,
|
|
112
|
+
clean: true,
|
|
113
|
+
connectDelay: 60 * 1000,
|
|
114
|
+
})
|
|
115
|
+
.catch(e => {
|
|
116
|
+
this.fbnsDebug(`Connection failed: ${e}`);
|
|
117
|
+
throw e;
|
|
118
|
+
});
|
|
119
|
+
if (!this.client.ready)
|
|
120
|
+
throw new Error('OB Connection failed');
|
|
121
|
+
await this.client.subscribe({ topic: constants_1.FbnsTopics.FBNS_MESSAGE.id });
|
|
122
|
+
const msg = await (0, shared_1.listenOnce)(this.client, constants_1.FbnsTopics.FBNS_REG_RESP.id);
|
|
123
|
+
const data = await (0, shared_1.tryUnzipAsync)(msg.payload);
|
|
124
|
+
const payload = data.toString('utf8');
|
|
125
|
+
this.fbnsDebug(`Received register response: ${payload}`);
|
|
126
|
+
const { token, error } = JSON.parse(payload);
|
|
127
|
+
if (error) {
|
|
128
|
+
this.emit('error', error);
|
|
129
|
+
throw error;
|
|
130
|
+
}
|
|
131
|
+
try {
|
|
132
|
+
await this.sendPushRegister(token);
|
|
133
|
+
}
|
|
134
|
+
catch (e) {
|
|
135
|
+
if (e instanceof Error) {
|
|
136
|
+
this.emit('error', e);
|
|
137
|
+
}
|
|
138
|
+
throw e;
|
|
70
139
|
}
|
|
71
|
-
return (0, shared_1.compressDeflate)(this.conn.toThrift());
|
|
72
|
-
},
|
|
73
|
-
enableTrace,
|
|
74
|
-
autoReconnect: autoReconnect ?? true,
|
|
75
|
-
requirePayload: true,
|
|
76
|
-
socksOptions,
|
|
77
|
-
additionalOptions: additionalTlsOptions,
|
|
78
|
-
});
|
|
79
|
-
this.client.on("warning", (w) => this.emit("warning", w));
|
|
80
|
-
this.client.on("error", (e) => this.emit("error", e));
|
|
81
|
-
this.client.on("disconnect", (reason) =>
|
|
82
|
-
this.safeDisconnect
|
|
83
|
-
? this.emit("disconnect", reason && JSON.stringify(reason))
|
|
84
|
-
: this.emit(
|
|
85
|
-
"error",
|
|
86
|
-
new errors_1.ClientDisconnectedError(
|
|
87
|
-
`MQTToTClient got disconnected. Reason: ${
|
|
88
|
-
reason && JSON.stringify(reason)
|
|
89
|
-
}`
|
|
90
|
-
)
|
|
91
|
-
)
|
|
92
|
-
);
|
|
93
|
-
this.client.listen(constants_1.FbnsTopics.FBNS_MESSAGE.id, (msg) =>
|
|
94
|
-
this.handleMessage(msg)
|
|
95
|
-
);
|
|
96
|
-
this.client.listen(
|
|
97
|
-
{
|
|
98
|
-
topic: constants_1.FbnsTopics.FBNS_EXP_LOGGING.id,
|
|
99
|
-
transformer: async (msg) =>
|
|
100
|
-
JSON.parse(
|
|
101
|
-
(await (0, shared_1.tryUnzipAsync)(msg.payload)).toString()
|
|
102
|
-
),
|
|
103
|
-
},
|
|
104
|
-
(msg) => this.emit("logging", msg)
|
|
105
|
-
);
|
|
106
|
-
this.client.listen(constants_1.FbnsTopics.PP.id, (msg) =>
|
|
107
|
-
this.emit("pp", msg.payload.toString())
|
|
108
|
-
);
|
|
109
|
-
this.client.on("connect", async (res) => {
|
|
110
|
-
if (!this.client) {
|
|
111
|
-
throw new obmqtt_1.IllegalStateError(
|
|
112
|
-
"No client registered but an event was received"
|
|
113
|
-
);
|
|
114
|
-
}
|
|
115
|
-
this.fbnsDebug("Connected to MQTT");
|
|
116
|
-
if (!res?.payload?.length) {
|
|
117
|
-
this.fbnsDebug(
|
|
118
|
-
`Received empty connect packet. Reason: ${res.errorName}; Try resetting your fbns state!`
|
|
119
|
-
);
|
|
120
|
-
this.emit(
|
|
121
|
-
"error",
|
|
122
|
-
new errors_1.EmptyPacketError(
|
|
123
|
-
"Received empty connect packet. Try resetting your fbns state!"
|
|
124
|
-
)
|
|
125
|
-
);
|
|
126
|
-
await this.client.disconnect();
|
|
127
|
-
return;
|
|
128
|
-
}
|
|
129
|
-
const payload = res.payload.toString("utf8");
|
|
130
|
-
this.fbnsDebug(`Received auth: ${payload}`);
|
|
131
|
-
this._auth.read(payload);
|
|
132
|
-
this.emit("auth", this.auth);
|
|
133
|
-
await this.client.mqttotPublish({
|
|
134
|
-
topic: constants_1.FbnsTopics.FBNS_REG_REQ.id,
|
|
135
|
-
payload: Buffer.from(
|
|
136
|
-
JSON.stringify({
|
|
137
|
-
pkg_name: constants_1.INSTAGRAM_PACKAGE_NAME,
|
|
138
|
-
appid: this.ig.state.fbAnalyticsApplicationId,
|
|
139
|
-
}),
|
|
140
|
-
"utf8"
|
|
141
|
-
),
|
|
142
|
-
qosLevel: 1,
|
|
143
|
-
});
|
|
144
|
-
// this.buildConnection(); ?
|
|
145
|
-
});
|
|
146
|
-
await this.client
|
|
147
|
-
.connect({
|
|
148
|
-
keepAlive: 60,
|
|
149
|
-
protocolLevel: 3,
|
|
150
|
-
clean: true,
|
|
151
|
-
connectDelay: 60 * 1000,
|
|
152
|
-
})
|
|
153
|
-
.catch((e) => {
|
|
154
|
-
this.fbnsDebug(`Connection failed: ${e}`);
|
|
155
|
-
throw e;
|
|
156
|
-
});
|
|
157
|
-
if (!this.client.ready) throw new Error("OB Connection failed");
|
|
158
|
-
await this.client.subscribe({
|
|
159
|
-
topic: constants_1.FbnsTopics.FBNS_MESSAGE.id,
|
|
160
|
-
});
|
|
161
|
-
const msg = await (0, shared_1.listenOnce)(
|
|
162
|
-
this.client,
|
|
163
|
-
constants_1.FbnsTopics.FBNS_REG_RESP.id
|
|
164
|
-
);
|
|
165
|
-
const data = await (0, shared_1.tryUnzipAsync)(msg.payload);
|
|
166
|
-
const payload = data.toString("utf8");
|
|
167
|
-
this.fbnsDebug(`Received register response: ${payload}`);
|
|
168
|
-
const { token, error } = JSON.parse(payload);
|
|
169
|
-
if (error) {
|
|
170
|
-
this.emit("error", error);
|
|
171
|
-
throw error;
|
|
172
140
|
}
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
throw e;
|
|
141
|
+
disconnect() {
|
|
142
|
+
this.safeDisconnect = true;
|
|
143
|
+
if (!this.client) {
|
|
144
|
+
return Promise.resolve();
|
|
145
|
+
}
|
|
146
|
+
return this.client.disconnect();
|
|
180
147
|
}
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
148
|
+
async handleMessage(msg) {
|
|
149
|
+
const payload = JSON.parse((await (0, shared_1.tryUnzipAsync)(msg.payload)).toString('utf8'));
|
|
150
|
+
if ((0, shared_1.notUndefined)(payload.fbpushnotif)) {
|
|
151
|
+
const notification = (0, fbns_utilities_1.createNotificationFromJson)(payload.fbpushnotif);
|
|
152
|
+
this.emit('push', notification);
|
|
153
|
+
if (notification.collapseKey)
|
|
154
|
+
this.emit(notification.collapseKey, notification);
|
|
155
|
+
}
|
|
156
|
+
else {
|
|
157
|
+
this.fbnsDebug(`Received a message without 'fbpushnotif': ${JSON.stringify(payload)}`);
|
|
158
|
+
this.emit('message', payload);
|
|
159
|
+
}
|
|
186
160
|
}
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
this.emit("message", payload);
|
|
161
|
+
async sendPushRegister(token) {
|
|
162
|
+
const { body } = await this.ig.request.send({
|
|
163
|
+
url: `/api/v1/push/register/`,
|
|
164
|
+
method: 'POST',
|
|
165
|
+
form: {
|
|
166
|
+
device_type: 'android_mqtt',
|
|
167
|
+
is_main_push_channel: true,
|
|
168
|
+
device_sub_type: 2,
|
|
169
|
+
device_token: token,
|
|
170
|
+
_csrftoken: this.ig.state.cookieCsrfToken,
|
|
171
|
+
guid: this.ig.state.uuid,
|
|
172
|
+
uuid: this.ig.state.uuid,
|
|
173
|
+
users: this.ig.state.cookieUserId,
|
|
174
|
+
family_device_id: new chance_1.Chance().guid({ version: 4 }),
|
|
175
|
+
},
|
|
176
|
+
});
|
|
177
|
+
return body;
|
|
205
178
|
}
|
|
206
|
-
}
|
|
207
|
-
async sendPushRegister(token) {
|
|
208
|
-
const { body } = await this.ig.request.send({
|
|
209
|
-
url: `/api/v1/push/register/`,
|
|
210
|
-
method: "POST",
|
|
211
|
-
form: {
|
|
212
|
-
device_type: "android_mqtt",
|
|
213
|
-
is_main_push_channel: true,
|
|
214
|
-
device_sub_type: 2,
|
|
215
|
-
device_token: token,
|
|
216
|
-
_csrftoken: this.ig.state.cookieCsrfToken,
|
|
217
|
-
guid: this.ig.state.uuid,
|
|
218
|
-
uuid: this.ig.state.uuid,
|
|
219
|
-
users: this.ig.state.cookieUserId,
|
|
220
|
-
family_device_id: new chance_1.Chance().guid({ version: 4 }),
|
|
221
|
-
},
|
|
222
|
-
});
|
|
223
|
-
return body;
|
|
224
|
-
}
|
|
225
179
|
}
|
|
226
180
|
exports.FbnsClient = FbnsClient;
|
|
227
|
-
//# sourceMappingURL=fbns.client.js.map
|
|
181
|
+
//# sourceMappingURL=fbns.client.js.map
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { MQTToTConnectPacketOptions } from './mqttot.connect.request.packet';
|
|
2
|
-
import { ConnectRequestOptions, DefaultPacketReadResultMap, DefaultPacketWriteOptions, MqttClient, MqttMessage, MqttMessageOutgoing, PacketFlowFunc, PacketType } from '
|
|
2
|
+
import { ConnectRequestOptions, DefaultPacketReadResultMap, DefaultPacketWriteOptions, MqttClient, MqttMessage, MqttMessageOutgoing, PacketFlowFunc, PacketType } from 'obmqtt';
|
|
3
3
|
import { MQTToTConnectResponsePacket } from './mqttot.connect.response.packet';
|
|
4
4
|
import { SocksProxy } from 'socks';
|
|
5
5
|
import { ConnectionOptions } from 'tls';
|
|
@@ -34,4 +34,4 @@ export declare class MQTToTClient extends MqttClient<MQTToTReadMap, MQTToTWriteM
|
|
|
34
34
|
mqttotPublish(message: MqttMessage): Promise<MqttMessageOutgoing>;
|
|
35
35
|
}
|
|
36
36
|
export declare function mqttotConnectFlow(payload: Buffer, requirePayload: boolean): PacketFlowFunc<MQTToTReadMap, MQTToTWriteMap, MQTToTConnectResponsePacket>;
|
|
37
|
-
export {
|
|
37
|
+
export {};
|
|
@@ -4,110 +4,98 @@ exports.MQTToTClient = void 0;
|
|
|
4
4
|
exports.mqttotConnectFlow = mqttotConnectFlow;
|
|
5
5
|
const shared_1 = require("../shared");
|
|
6
6
|
const mqttot_connect_request_packet_1 = require("./mqttot.connect.request.packet");
|
|
7
|
-
const obmqtt_1 = require("
|
|
7
|
+
const obmqtt_1 = require("obmqtt");
|
|
8
8
|
const errors_1 = require("../errors");
|
|
9
9
|
const mqttot_connect_response_packet_1 = require("./mqttot.connect.response.packet");
|
|
10
10
|
class MQTToTClient extends obmqtt_1.MqttClient {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
11
|
+
constructor(options) {
|
|
12
|
+
super({
|
|
13
|
+
autoReconnect: options.autoReconnect,
|
|
14
|
+
readMap: {
|
|
15
|
+
...obmqtt_1.DefaultPacketReadMap,
|
|
16
|
+
[obmqtt_1.PacketType.ConnAck]: mqttot_connect_response_packet_1.readConnectResponsePacket,
|
|
17
|
+
},
|
|
18
|
+
writeMap: {
|
|
19
|
+
...obmqtt_1.DefaultPacketWriteMap,
|
|
20
|
+
[obmqtt_1.PacketType.Connect]: mqttot_connect_request_packet_1.writeConnectRequestPacket,
|
|
21
|
+
},
|
|
22
|
+
transport: options.socksOptions
|
|
23
|
+
? new obmqtt_1.SocksTlsTransport({
|
|
24
|
+
host: options.url,
|
|
25
|
+
port: 443,
|
|
26
|
+
proxyOptions: options.socksOptions,
|
|
27
|
+
additionalOptions: options.additionalOptions,
|
|
28
|
+
})
|
|
29
|
+
: new obmqtt_1.TlsTransport({
|
|
30
|
+
host: options.url,
|
|
31
|
+
port: 443,
|
|
32
|
+
additionalOptions: options.additionalOptions,
|
|
33
|
+
}),
|
|
34
|
+
});
|
|
35
|
+
this.mqttotDebug = (msg, ...args) => (0, shared_1.debugChannel)('mqttot')(`${options.url}: ${msg}`, ...args);
|
|
36
|
+
this.connectPayloadProvider = options.payloadProvider;
|
|
37
|
+
this.mqttotDebug(`Creating client`);
|
|
38
|
+
this.registerListeners();
|
|
39
|
+
this.requirePayload = options.requirePayload;
|
|
40
|
+
}
|
|
41
|
+
registerListeners() {
|
|
42
|
+
const printErrorOrWarning = (type) => (e) => {
|
|
43
|
+
if (typeof e === 'string') {
|
|
44
|
+
this.mqttotDebug(`${type}: ${e}`);
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
this.mqttotDebug(`${type}: ${e.message}\n\tStack: ${e.stack}`);
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
this.on('error', printErrorOrWarning('Error'));
|
|
51
|
+
this.on('warning', printErrorOrWarning('Warning'));
|
|
52
|
+
this.on('disconnect', e => this.mqttotDebug(`Disconnected. ${e}`));
|
|
53
|
+
}
|
|
54
|
+
async connect(options) {
|
|
55
|
+
this.connectPayload = await this.connectPayloadProvider();
|
|
56
|
+
return super.connect(options);
|
|
57
|
+
}
|
|
58
|
+
getConnectFlow() {
|
|
59
|
+
if (!this.connectPayload) {
|
|
60
|
+
throw new obmqtt_1.IllegalStateError('Called getConnectFlow() before calling connect()');
|
|
61
|
+
}
|
|
62
|
+
return mqttotConnectFlow(this.connectPayload, this.requirePayload);
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Compresses the payload
|
|
66
|
+
* @param {MqttMessage} message
|
|
67
|
+
* @returns {Promise<MqttMessageOutgoing>}
|
|
68
|
+
*/
|
|
69
|
+
async mqttotPublish(message) {
|
|
70
|
+
this.mqttotDebug(`Publishing ${message.payload.byteLength}bytes to topic ${message.topic}`);
|
|
71
|
+
return await this.publish({
|
|
72
|
+
topic: message.topic,
|
|
73
|
+
payload: await (0, shared_1.compressDeflate)(message.payload),
|
|
74
|
+
qosLevel: message.qosLevel,
|
|
75
|
+
});
|
|
65
76
|
}
|
|
66
|
-
return mqttotConnectFlow(this.connectPayload, this.requirePayload);
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* Compresses the payload
|
|
70
|
-
* @param {MqttMessage} message
|
|
71
|
-
* @returns {Promise<MqttMessageOutgoing>}
|
|
72
|
-
*/
|
|
73
|
-
async mqttotPublish(message) {
|
|
74
|
-
this.mqttotDebug(
|
|
75
|
-
`Publishing ${message.payload.byteLength}bytes to topic ${message.topic}`
|
|
76
|
-
);
|
|
77
|
-
return await this.publish({
|
|
78
|
-
topic: message.topic,
|
|
79
|
-
payload: await (0, shared_1.compressDeflate)(message.payload),
|
|
80
|
-
qosLevel: message.qosLevel,
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
77
|
}
|
|
84
78
|
exports.MQTToTClient = MQTToTClient;
|
|
85
79
|
function mqttotConnectFlow(payload, requirePayload) {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
new errors_1.ConnectionFailedError(
|
|
107
|
-
`CONNACK returnCode: ${packet.returnCode} errorName: ${packet.errorName}`
|
|
108
|
-
)
|
|
109
|
-
);
|
|
110
|
-
},
|
|
111
|
-
});
|
|
80
|
+
return (success, error) => ({
|
|
81
|
+
start: () => ({
|
|
82
|
+
type: obmqtt_1.PacketType.Connect,
|
|
83
|
+
options: {
|
|
84
|
+
payload,
|
|
85
|
+
keepAlive: 60,
|
|
86
|
+
},
|
|
87
|
+
}),
|
|
88
|
+
accept: obmqtt_1.isConnAck,
|
|
89
|
+
next: (packet) => {
|
|
90
|
+
if (packet.isSuccess) {
|
|
91
|
+
if (packet.payload?.length || !requirePayload)
|
|
92
|
+
success(packet);
|
|
93
|
+
else
|
|
94
|
+
error(new errors_1.EmptyPacketError(`CONNACK: no payload (payloadExpected): ${packet.payload}`));
|
|
95
|
+
}
|
|
96
|
+
else
|
|
97
|
+
error(new errors_1.ConnectionFailedError(`CONNACK returnCode: ${packet.returnCode} errorName: ${packet.errorName}`));
|
|
98
|
+
},
|
|
99
|
+
});
|
|
112
100
|
}
|
|
113
|
-
//# sourceMappingURL=mqttot.client.js.map
|
|
101
|
+
//# sourceMappingURL=mqttot.client.js.map
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ConnectResponsePacket, PacketStream, ConnectReturnCode } from '
|
|
1
|
+
import { ConnectResponsePacket, PacketStream, ConnectReturnCode } from 'obmqtt';
|
|
2
2
|
export declare class MQTToTConnectResponsePacket extends ConnectResponsePacket {
|
|
3
3
|
readonly payload: Buffer;
|
|
4
4
|
constructor(ackFlags: number, returnCode: ConnectReturnCode, payload: Buffer);
|
|
@@ -2,26 +2,23 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.MQTToTConnectResponsePacket = void 0;
|
|
4
4
|
exports.readConnectResponsePacket = readConnectResponsePacket;
|
|
5
|
-
const obmqtt_1 = require("
|
|
5
|
+
const obmqtt_1 = require("obmqtt");
|
|
6
6
|
class MQTToTConnectResponsePacket extends obmqtt_1.ConnectResponsePacket {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
constructor(ackFlags, returnCode, payload) {
|
|
8
|
+
super(ackFlags, returnCode);
|
|
9
|
+
this.payload = payload;
|
|
10
|
+
}
|
|
11
11
|
}
|
|
12
12
|
exports.MQTToTConnectResponsePacket = MQTToTConnectResponsePacket;
|
|
13
13
|
function readConnectResponsePacket(stream, remaining) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
ack,
|
|
23
|
-
returnCode,
|
|
24
|
-
remaining > 2 ? stream.readStringAsBuffer() : Buffer.alloc(0)
|
|
25
|
-
);
|
|
14
|
+
const ack = stream.readByte();
|
|
15
|
+
const returnCode = stream.readByte();
|
|
16
|
+
if (ack > 1) {
|
|
17
|
+
throw new Error('Invalid ack');
|
|
18
|
+
}
|
|
19
|
+
else if (returnCode > 5) {
|
|
20
|
+
throw new Error('Invalid return code');
|
|
21
|
+
}
|
|
22
|
+
return new MQTToTConnectResponsePacket(ack, returnCode, remaining > 2 ? stream.readStringAsBuffer() : Buffer.alloc(0));
|
|
26
23
|
}
|
|
27
|
-
//# sourceMappingURL=mqttot.connect.response.packet.js.map
|
|
24
|
+
//# sourceMappingURL=mqttot.connect.response.packet.js.map
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Topic } from '../../topic';
|
|
2
2
|
import { MQTToTClient } from '../../mqttot';
|
|
3
|
-
import { MqttMessageOutgoing } from '
|
|
3
|
+
import { MqttMessageOutgoing } from 'obmqtt';
|
|
4
4
|
export declare class Commands {
|
|
5
5
|
private client;
|
|
6
6
|
constructor(client: MQTToTClient);
|