@mtkruto/node 0.70.0 → 0.71.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/esm/_dnt.polyfills.d.ts +13 -13
- package/esm/_dnt.polyfills.d.ts.map +1 -1
- package/esm/_dnt.polyfills.js +11 -11
- package/esm/client/0_abortable_loop.d.ts +1 -1
- package/esm/client/0_abortable_loop.d.ts.map +1 -1
- package/esm/client/0_abortable_loop.js +2 -2
- package/esm/client/0_client_abstract.d.ts +1 -0
- package/esm/client/0_client_abstract.d.ts.map +1 -1
- package/esm/client/0_client_abstract.js +3 -0
- package/esm/client/2_client_encrypted.d.ts.map +1 -1
- package/esm/client/2_client_encrypted.js +26 -23
- package/esm/client/2_network_statistics_manager.d.ts +2 -2
- package/esm/client/2_network_statistics_manager.d.ts.map +1 -1
- package/esm/client/2_network_statistics_manager.js +30 -4
- package/esm/client/2_update_manager.js +1 -1
- package/esm/client/5_client.js +9 -9
- package/esm/session/1_session.d.ts +1 -0
- package/esm/session/1_session.d.ts.map +1 -1
- package/esm/session/1_session.js +3 -0
- package/esm/session/2_session_encrypted.d.ts +1 -1
- package/esm/session/2_session_encrypted.d.ts.map +1 -1
- package/esm/session/2_session_encrypted.js +153 -89
- package/esm/tl/2_telegram.d.ts +1 -1
- package/esm/tl/2_telegram.d.ts.map +1 -1
- package/package.json +1 -1
- package/script/_dnt.polyfills.d.ts +13 -13
- package/script/_dnt.polyfills.d.ts.map +1 -1
- package/script/_dnt.polyfills.js +11 -11
- package/script/client/0_abortable_loop.d.ts +1 -1
- package/script/client/0_abortable_loop.d.ts.map +1 -1
- package/script/client/0_abortable_loop.js +2 -2
- package/script/client/0_client_abstract.d.ts +1 -0
- package/script/client/0_client_abstract.d.ts.map +1 -1
- package/script/client/0_client_abstract.js +3 -0
- package/script/client/2_client_encrypted.d.ts.map +1 -1
- package/script/client/2_client_encrypted.js +26 -23
- package/script/client/2_network_statistics_manager.d.ts +2 -2
- package/script/client/2_network_statistics_manager.d.ts.map +1 -1
- package/script/client/2_network_statistics_manager.js +30 -4
- package/script/client/2_update_manager.js +1 -1
- package/script/client/5_client.js +9 -9
- package/script/session/1_session.d.ts +1 -0
- package/script/session/1_session.d.ts.map +1 -1
- package/script/session/1_session.js +3 -0
- package/script/session/2_session_encrypted.d.ts +1 -1
- package/script/session/2_session_encrypted.d.ts.map +1 -1
- package/script/session/2_session_encrypted.js +153 -89
- package/script/tl/2_telegram.d.ts +1 -1
- package/script/tl/2_telegram.d.ts.map +1 -1
|
@@ -34,21 +34,27 @@ const GZIP_PACKED = 0x3072CFA1;
|
|
|
34
34
|
const RPC_RESULT = 0xF35C6D01;
|
|
35
35
|
const RPC_ERROR = _2_tl_js_1.Mtproto.schema.definitions["rpc_error"][0];
|
|
36
36
|
class SessionEncrypted extends _1_session_js_1.Session {
|
|
37
|
+
static #TGCRYPTO_INITED = false;
|
|
37
38
|
#id = (0, _1_utilities_js_1.getRandomId)();
|
|
39
|
+
handlers = {};
|
|
40
|
+
#L;
|
|
41
|
+
#LsendLoop;
|
|
42
|
+
#LreceiveLoop;
|
|
43
|
+
#LpingLoop;
|
|
38
44
|
#authKey = new Uint8Array();
|
|
39
45
|
#authKeyId = 0n;
|
|
40
|
-
|
|
46
|
+
#sentMessages = new Set();
|
|
47
|
+
#pendingMessages = new Array();
|
|
48
|
+
#containers = new _0_deps_js_1.LruCache(20_000);
|
|
49
|
+
#pendingPings = new Map();
|
|
41
50
|
#toAcknowledge = new Array();
|
|
42
|
-
#pendingMessages = new Set();
|
|
43
|
-
#pendingContainers = new _0_deps_js_1.LruCache(20_000);
|
|
44
51
|
#pendingAcks = new _0_deps_js_1.LruCache(100);
|
|
45
|
-
#pendingPings = new Map();
|
|
46
|
-
#L;
|
|
47
|
-
static #TGCRYPTO_INITED = false;
|
|
48
52
|
constructor(dc, params) {
|
|
49
53
|
super(dc, params);
|
|
50
54
|
const L = this.#L = (0, _1_utilities_js_1.getLogger)("SessionEncrypted").client(id++);
|
|
51
|
-
this.#
|
|
55
|
+
this.#LsendLoop = L.branch("sendLoop");
|
|
56
|
+
this.#LreceiveLoop = L.branch("receiveLoop");
|
|
57
|
+
this.#LpingLoop = L.branch("pingLoop");
|
|
52
58
|
}
|
|
53
59
|
async setAuthKey(key) {
|
|
54
60
|
const hash = await (0, _1_utilities_js_1.sha1)(key);
|
|
@@ -68,13 +74,16 @@ class SessionEncrypted extends _1_session_js_1.Session {
|
|
|
68
74
|
SessionEncrypted.#TGCRYPTO_INITED = true;
|
|
69
75
|
}
|
|
70
76
|
this.#receiveLoop.start();
|
|
77
|
+
this.#sendLoop.start();
|
|
71
78
|
this.#pingLoop.start();
|
|
79
|
+
this.#awakeSendLoop?.();
|
|
72
80
|
}
|
|
73
81
|
disconnect() {
|
|
74
82
|
super.disconnect();
|
|
75
83
|
this.state.reset();
|
|
76
84
|
this.#id = (0, _1_utilities_js_1.getRandomId)();
|
|
77
|
-
this.#
|
|
85
|
+
this.#pingLoop.abort();
|
|
86
|
+
this.#awakeSendLoop?.();
|
|
78
87
|
this.#rejectAllPending(new _0_errors_js_1.ConnectionError("Not connected."));
|
|
79
88
|
}
|
|
80
89
|
#assertNotDisconnected() {
|
|
@@ -91,27 +100,24 @@ class SessionEncrypted extends _1_session_js_1.Session {
|
|
|
91
100
|
this.#rejectAllPending(new _0_session_error_js_1.SessionError("Session invalidated."));
|
|
92
101
|
}
|
|
93
102
|
#rejectAllPending(reason) {
|
|
94
|
-
for (const id of this.#
|
|
103
|
+
for (const id of this.#sentMessages) {
|
|
95
104
|
this.#onMessageFailed(id, reason);
|
|
96
105
|
}
|
|
97
|
-
this.#pendingMessages.clear();
|
|
98
106
|
for (const pendingPing of this.#pendingPings.values()) {
|
|
99
|
-
pendingPing.reject(reason);
|
|
107
|
+
pendingPing.promiseWithResolvers.reject(reason);
|
|
100
108
|
}
|
|
109
|
+
this.#sentMessages.clear();
|
|
101
110
|
this.#pendingPings.clear();
|
|
102
|
-
this.#
|
|
111
|
+
this.#containers.clear();
|
|
103
112
|
}
|
|
104
113
|
#onMessageFailed(id, reason) {
|
|
105
|
-
this.#
|
|
106
|
-
const
|
|
107
|
-
if (
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
(0, _1_utilities_js_1.drop)(this.#resendPendingPing(pendingPing));
|
|
111
|
-
}
|
|
112
|
-
else {
|
|
113
|
-
pendingPing.reject(reason);
|
|
114
|
+
this.#sentMessages.delete(id);
|
|
115
|
+
const pendingContainer = this.#containers.get(id);
|
|
116
|
+
if (pendingContainer) {
|
|
117
|
+
for (const id of pendingContainer) {
|
|
118
|
+
this.#onMessageFailed(id, reason);
|
|
114
119
|
}
|
|
120
|
+
this.#containers.delete(id);
|
|
115
121
|
return;
|
|
116
122
|
}
|
|
117
123
|
const pendingAck = this.#pendingAcks.get(id);
|
|
@@ -122,12 +128,15 @@ class SessionEncrypted extends _1_session_js_1.Session {
|
|
|
122
128
|
this.#pendingAcks.delete(id);
|
|
123
129
|
return;
|
|
124
130
|
}
|
|
125
|
-
const
|
|
126
|
-
if (
|
|
127
|
-
|
|
128
|
-
|
|
131
|
+
const pendingPing = this.#pendingPings.get(id);
|
|
132
|
+
if (pendingPing) {
|
|
133
|
+
this.#pendingPings.delete(id);
|
|
134
|
+
if (reason instanceof _0_session_error_js_1.SessionError) {
|
|
135
|
+
(0, _1_utilities_js_1.drop)(this.#resendPendingPing(pendingPing));
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
pendingPing.promiseWithResolvers.reject(reason);
|
|
129
139
|
}
|
|
130
|
-
this.#pendingContainers.delete(id);
|
|
131
140
|
return;
|
|
132
141
|
}
|
|
133
142
|
// message was not sent by us
|
|
@@ -142,41 +151,10 @@ class SessionEncrypted extends _1_session_js_1.Session {
|
|
|
142
151
|
await super.waitUntilConnected();
|
|
143
152
|
}
|
|
144
153
|
this.#assertNotDisconnected();
|
|
145
|
-
const
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
msg_id,
|
|
150
|
-
seqno,
|
|
151
|
-
body,
|
|
152
|
-
};
|
|
153
|
-
if (this.#toAcknowledge.length) {
|
|
154
|
-
const msg_ids = this.#toAcknowledge.splice(0, 8192);
|
|
155
|
-
const ack = {
|
|
156
|
-
_: "message",
|
|
157
|
-
msg_id: this.state.nextMessageId(),
|
|
158
|
-
seqno: this.state.nextSeqNo(false),
|
|
159
|
-
body: _2_tl_js_1.Mtproto.serializeObject({ _: "msgs_ack", msg_ids }),
|
|
160
|
-
};
|
|
161
|
-
this.#pendingAcks.set(ack.msg_id, msg_ids);
|
|
162
|
-
message = {
|
|
163
|
-
_: "message",
|
|
164
|
-
msg_id: this.state.nextMessageId(),
|
|
165
|
-
seqno: this.state.nextSeqNo(false),
|
|
166
|
-
body: {
|
|
167
|
-
_: "msg_container",
|
|
168
|
-
messages: [message, ack],
|
|
169
|
-
},
|
|
170
|
-
};
|
|
171
|
-
}
|
|
172
|
-
this.#L.out(message);
|
|
173
|
-
const payload = await this.#encryptMessage(message);
|
|
174
|
-
await this.transport.transport.send(payload);
|
|
175
|
-
this.#pendingMessages.add(msg_id);
|
|
176
|
-
if (!(message.body instanceof Uint8Array)) {
|
|
177
|
-
this.#pendingContainers.set(message.msg_id, message.body.messages.map((v) => v.msg_id));
|
|
178
|
-
}
|
|
179
|
-
return msg_id;
|
|
154
|
+
const pendingMessage = { body, promiseWithResolvers: Promise.withResolvers() };
|
|
155
|
+
this.#pendingMessages.push(pendingMessage);
|
|
156
|
+
this.#awakeSendLoop?.();
|
|
157
|
+
return await pendingMessage.promiseWithResolvers.promise;
|
|
180
158
|
}
|
|
181
159
|
async #receive() {
|
|
182
160
|
this.#assertNotDisconnected();
|
|
@@ -229,19 +207,98 @@ class SessionEncrypted extends _1_session_js_1.Session {
|
|
|
229
207
|
const _sessionId_ = plainReader.readInt64(false);
|
|
230
208
|
return (0, _2_tl_js_1.deserializeMessage)(plainReader);
|
|
231
209
|
}
|
|
210
|
+
//// SEND LOOP ////
|
|
211
|
+
#awakeSendLoop;
|
|
212
|
+
#sendLoop = new _0_abortable_loop_js_1.AbortableLoop(this.#sendLoopBody.bind(this), (err) => {
|
|
213
|
+
this.#LsendLoop.error("unhandled receive loop error:", err);
|
|
214
|
+
});
|
|
215
|
+
async #sendLoopBody(loop, signal) {
|
|
216
|
+
if (!this.connected) {
|
|
217
|
+
this.#LsendLoop.debug("aborting as not connected");
|
|
218
|
+
loop.abort();
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
const pendingMessage = this.#pendingMessages.shift();
|
|
222
|
+
if (pendingMessage === undefined) {
|
|
223
|
+
this.#LsendLoop.debug("no pending messages");
|
|
224
|
+
return await new Promise((resolve) => {
|
|
225
|
+
const onAbort = () => {
|
|
226
|
+
this.#LsendLoop.debug("got aborted while sleeping");
|
|
227
|
+
resolve();
|
|
228
|
+
};
|
|
229
|
+
signal.addEventListener("abort", onAbort);
|
|
230
|
+
this.#awakeSendLoop = () => {
|
|
231
|
+
this.#LsendLoop.debug("got awaken");
|
|
232
|
+
resolve();
|
|
233
|
+
signal.removeEventListener("abort", onAbort);
|
|
234
|
+
};
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
const msg_id = this.state.nextMessageId();
|
|
238
|
+
const seqno = this.state.nextSeqNo(true);
|
|
239
|
+
let message = {
|
|
240
|
+
_: "message",
|
|
241
|
+
msg_id,
|
|
242
|
+
seqno,
|
|
243
|
+
body: pendingMessage.body,
|
|
244
|
+
};
|
|
245
|
+
this.#LsendLoop.debug("msg_id =", msg_id, "seqno =", seqno);
|
|
246
|
+
if (this.#toAcknowledge.length) {
|
|
247
|
+
const msg_ids = this.#toAcknowledge.splice(0, 8192);
|
|
248
|
+
this.#LsendLoop.debug("acknowledging", msg_ids.length, "message(s) while sending this one");
|
|
249
|
+
const ack = {
|
|
250
|
+
_: "message",
|
|
251
|
+
msg_id: this.state.nextMessageId(),
|
|
252
|
+
seqno: this.state.nextSeqNo(false),
|
|
253
|
+
body: _2_tl_js_1.Mtproto.serializeObject({ _: "msgs_ack", msg_ids }),
|
|
254
|
+
};
|
|
255
|
+
this.#LsendLoop.debug("msgs_ack msg_id =", ack.msg_id, "seqno =", seqno);
|
|
256
|
+
this.#pendingAcks.set(ack.msg_id, msg_ids);
|
|
257
|
+
message = {
|
|
258
|
+
_: "message",
|
|
259
|
+
msg_id: this.state.nextMessageId(),
|
|
260
|
+
seqno: this.state.nextSeqNo(false),
|
|
261
|
+
body: {
|
|
262
|
+
_: "msg_container",
|
|
263
|
+
messages: [message, ack],
|
|
264
|
+
},
|
|
265
|
+
};
|
|
266
|
+
this.#LsendLoop.debug("container msg_id =", message.msg_id, "seqno =", message.seqno);
|
|
267
|
+
}
|
|
268
|
+
try {
|
|
269
|
+
const payload = await this.#encryptMessage(message);
|
|
270
|
+
await this.transport.transport.send(payload);
|
|
271
|
+
pendingMessage.promiseWithResolvers.resolve(msg_id);
|
|
272
|
+
}
|
|
273
|
+
catch (err) {
|
|
274
|
+
pendingMessage.promiseWithResolvers.reject(err);
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
this.#LsendLoop.out(message);
|
|
278
|
+
this.#sentMessages.add(msg_id);
|
|
279
|
+
if (!(message.body instanceof Uint8Array)) {
|
|
280
|
+
const msg_ids = message.body.messages.map((v) => v.msg_id);
|
|
281
|
+
this.#LsendLoop.debug("sent container", message.msg_id, "with messages", ...msg_ids);
|
|
282
|
+
this.#containers.set(message.msg_id, msg_ids);
|
|
283
|
+
}
|
|
284
|
+
else {
|
|
285
|
+
this.#LsendLoop.debug("sent message", message.msg_id);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
232
288
|
//// RECEIVE LOOP ////
|
|
233
289
|
#receiveLoop = new _0_abortable_loop_js_1.AbortableLoop(this.#receiveLoopBody.bind(this), (err) => {
|
|
234
|
-
this.#
|
|
290
|
+
this.#LreceiveLoop.error("unhandled receive loop error:", err);
|
|
235
291
|
});
|
|
236
|
-
async #receiveLoopBody() {
|
|
292
|
+
async #receiveLoopBody(loop) {
|
|
237
293
|
let message;
|
|
238
294
|
try {
|
|
239
295
|
message = await this.#receive();
|
|
240
296
|
}
|
|
241
297
|
catch (err) {
|
|
242
|
-
this.#
|
|
298
|
+
this.#LreceiveLoop.error("failed to receive message:", err);
|
|
243
299
|
if (!this.connected) {
|
|
244
|
-
this.#
|
|
300
|
+
this.#LreceiveLoop.debug("aborting as not connected");
|
|
301
|
+
loop.abort();
|
|
245
302
|
return;
|
|
246
303
|
}
|
|
247
304
|
else {
|
|
@@ -250,29 +307,33 @@ class SessionEncrypted extends _1_session_js_1.Session {
|
|
|
250
307
|
}
|
|
251
308
|
try {
|
|
252
309
|
if (message.body instanceof Uint8Array) {
|
|
253
|
-
this.#onMessage(message.msg_id, message.body);
|
|
310
|
+
this.#onMessage(message.msg_id, message.body, null);
|
|
254
311
|
}
|
|
255
312
|
else {
|
|
256
313
|
this.#onMessageContainer(message.msg_id, message.body);
|
|
257
314
|
}
|
|
258
315
|
}
|
|
259
316
|
catch (err) {
|
|
260
|
-
this.#
|
|
317
|
+
this.#LreceiveLoop.error("failed to handle message:", err);
|
|
261
318
|
}
|
|
262
319
|
}
|
|
263
320
|
//// RECEIVE LOOP HANDLERS ////
|
|
264
|
-
async #onMessage(msgId, body) {
|
|
321
|
+
async #onMessage(msgId, body, containerId) {
|
|
322
|
+
this.#LreceiveLoop.debug("received message with ID", msgId, "and size", body.length, "inside", ...(containerId === null ? ["no container"] : ["container", containerId]));
|
|
323
|
+
const logger = this.#LreceiveLoop.branch(msgId + "");
|
|
265
324
|
let reader = new _2_tl_js_1.TLReader(body);
|
|
266
325
|
let id = reader.readInt32(false);
|
|
267
326
|
if (id === GZIP_PACKED) {
|
|
327
|
+
logger.debug("unpacking compressed body");
|
|
268
328
|
reader = new _2_tl_js_1.TLReader(await (0, _1_utilities_js_1.gunzip)(reader.readBytes()));
|
|
269
329
|
id = reader.readInt32(false);
|
|
270
330
|
}
|
|
271
331
|
if (id === RPC_RESULT) {
|
|
272
|
-
this.#onRpcResult(msgId, reader.buffer);
|
|
332
|
+
this.#onRpcResult(msgId, reader.buffer, logger);
|
|
273
333
|
return;
|
|
274
334
|
}
|
|
275
335
|
if (!_2_tl_js_1.Mtproto.schema.identifierToName[id]) {
|
|
336
|
+
logger.debug("identified body as a non-MTProto constructor");
|
|
276
337
|
reader.unreadInt32();
|
|
277
338
|
this.handlers.onUpdate?.(reader.buffer);
|
|
278
339
|
return;
|
|
@@ -283,10 +344,10 @@ class SessionEncrypted extends _1_session_js_1.Session {
|
|
|
283
344
|
type = await _2_tl_js_1.Mtproto.deserializeType(_2_tl_js_1.X, reader);
|
|
284
345
|
}
|
|
285
346
|
catch (err) {
|
|
286
|
-
|
|
347
|
+
logger.error("failed to deserialize MTProto type:", err);
|
|
287
348
|
return;
|
|
288
349
|
}
|
|
289
|
-
|
|
350
|
+
logger.debug("received", (0, _2_tl_js_1.repr)(type));
|
|
290
351
|
if (_2_tl_js_1.Mtproto.is("new_session_created", type)) {
|
|
291
352
|
this.#onNewSessionCreated(msgId, type);
|
|
292
353
|
}
|
|
@@ -297,24 +358,26 @@ class SessionEncrypted extends _1_session_js_1.Session {
|
|
|
297
358
|
this.#onBadServerSalt(type);
|
|
298
359
|
}
|
|
299
360
|
else if (_2_tl_js_1.Mtproto.is("bad_msg_notification", type)) {
|
|
300
|
-
await this.#onBadMsgNotification(msgId, type);
|
|
361
|
+
await this.#onBadMsgNotification(msgId, type, logger);
|
|
301
362
|
}
|
|
302
363
|
else if (_2_tl_js_1.Mtproto.is("msg_detailed_info", type)) {
|
|
303
|
-
this.#onMsgDetailedInfo(type);
|
|
364
|
+
this.#onMsgDetailedInfo(type, logger);
|
|
304
365
|
}
|
|
305
366
|
else if (_2_tl_js_1.Mtproto.is("msg_new_detailed_info", type)) {
|
|
306
|
-
this.#onMsgNewDetailedInfo(type);
|
|
367
|
+
this.#onMsgNewDetailedInfo(type, logger);
|
|
307
368
|
}
|
|
308
369
|
else {
|
|
309
|
-
|
|
370
|
+
logger.warning(`unhandled MTProto type: ${(0, _2_tl_js_1.repr)(type)}`);
|
|
310
371
|
}
|
|
311
372
|
}
|
|
312
|
-
async #onRpcResult(msgId, body) {
|
|
373
|
+
async #onRpcResult(msgId, body, logger) {
|
|
374
|
+
logger.debug("received rpc_result");
|
|
313
375
|
this.#toAcknowledge.push(msgId);
|
|
314
376
|
let reader = new _2_tl_js_1.TLReader(body);
|
|
315
377
|
const reqMsgId = reader.readInt64();
|
|
316
378
|
let id = reader.readInt32(false);
|
|
317
379
|
if (id === GZIP_PACKED) {
|
|
380
|
+
logger.debug("unpacking compressed rpc_result");
|
|
318
381
|
reader = new _2_tl_js_1.TLReader(await (0, _1_utilities_js_1.gunzip)(reader.readBytes()));
|
|
319
382
|
id = reader.readInt32(false);
|
|
320
383
|
reader.unreadInt32();
|
|
@@ -323,6 +386,7 @@ class SessionEncrypted extends _1_session_js_1.Session {
|
|
|
323
386
|
reader.unreadInt32();
|
|
324
387
|
}
|
|
325
388
|
if (id === RPC_ERROR) {
|
|
389
|
+
logger.debug("received rpc_error from message", msgId);
|
|
326
390
|
const error = await _2_tl_js_1.Mtproto.deserializeType("rpc_error", reader);
|
|
327
391
|
this.handlers.onRpcError?.(reqMsgId, error);
|
|
328
392
|
}
|
|
@@ -330,13 +394,15 @@ class SessionEncrypted extends _1_session_js_1.Session {
|
|
|
330
394
|
this.handlers.onRpcResult?.(reqMsgId, reader.buffer);
|
|
331
395
|
}
|
|
332
396
|
}
|
|
333
|
-
#onMsgDetailedInfo(msgDetailedInfo) {
|
|
397
|
+
#onMsgDetailedInfo(msgDetailedInfo, logger) {
|
|
398
|
+
logger.debug("scheduling the acknowledgement of", msgDetailedInfo.answer_msg_id, "because of", msgDetailedInfo._);
|
|
334
399
|
this.#toAcknowledge.push(msgDetailedInfo.answer_msg_id);
|
|
335
400
|
}
|
|
336
|
-
#onMsgNewDetailedInfo(msgNewDetailedInfo) {
|
|
401
|
+
#onMsgNewDetailedInfo(msgNewDetailedInfo, logger) {
|
|
402
|
+
logger.debug("scheduling the acknowledgement of", msgNewDetailedInfo.answer_msg_id, "because of", msgNewDetailedInfo._);
|
|
337
403
|
this.#toAcknowledge.push(msgNewDetailedInfo.answer_msg_id);
|
|
338
404
|
}
|
|
339
|
-
async #onBadMsgNotification(msgId, badMsgNotification) {
|
|
405
|
+
async #onBadMsgNotification(msgId, badMsgNotification, logger) {
|
|
340
406
|
let low = false;
|
|
341
407
|
switch (badMsgNotification.error_code) {
|
|
342
408
|
case 16: // message ID too low
|
|
@@ -346,11 +412,12 @@ class SessionEncrypted extends _1_session_js_1.Session {
|
|
|
346
412
|
this.state.timeDifference = Math.abs((0, _1_utilities_js_1.toUnixTimestamp)(new Date()) - Number(msgId >> 32n));
|
|
347
413
|
if (!low) {
|
|
348
414
|
this.state.timeDifference = -this.state.timeDifference;
|
|
415
|
+
logger.debug("resetting time difference to", -this.state.timeDifference, "because the ID of the message", badMsgNotification.bad_msg_id, "was too high");
|
|
349
416
|
await this.#invalidateSession("message ID too high");
|
|
350
417
|
return;
|
|
351
418
|
}
|
|
352
419
|
else {
|
|
353
|
-
|
|
420
|
+
logger.debug("resending message", badMsgNotification.bad_msg_id, "because its ID was too low");
|
|
354
421
|
}
|
|
355
422
|
break;
|
|
356
423
|
case 48: // bad server salt
|
|
@@ -371,7 +438,7 @@ class SessionEncrypted extends _1_session_js_1.Session {
|
|
|
371
438
|
this.#toAcknowledge.push(msgId);
|
|
372
439
|
const pendingPing = this.#pendingPings.get(pong.msg_id);
|
|
373
440
|
if (pendingPing) {
|
|
374
|
-
pendingPing.resolve(pong);
|
|
441
|
+
pendingPing.promiseWithResolvers.resolve(pong);
|
|
375
442
|
this.#pendingPings.delete(pong.msg_id);
|
|
376
443
|
}
|
|
377
444
|
else {
|
|
@@ -384,9 +451,10 @@ class SessionEncrypted extends _1_session_js_1.Session {
|
|
|
384
451
|
this.#toAcknowledge.push(msgId);
|
|
385
452
|
}
|
|
386
453
|
#onMessageContainer(msgId, msgContainer) {
|
|
454
|
+
this.#LreceiveLoop.debug("received container with ID", msgId, "and", msgContainer.messages.length, "message(s)");
|
|
387
455
|
for (const message of msgContainer.messages) {
|
|
388
456
|
if (message.body instanceof Uint8Array) {
|
|
389
|
-
this.#onMessage(message.msg_id, message.body);
|
|
457
|
+
this.#onMessage(message.msg_id, message.body, msgId);
|
|
390
458
|
}
|
|
391
459
|
else {
|
|
392
460
|
this.#onMessageContainer(msgId, message.body);
|
|
@@ -395,12 +463,10 @@ class SessionEncrypted extends _1_session_js_1.Session {
|
|
|
395
463
|
}
|
|
396
464
|
//// PING LOOP ////
|
|
397
465
|
#pingInterval = 56 * _0_deps_js_1.SECOND;
|
|
398
|
-
#pingLoopAbortController;
|
|
399
|
-
#LpingLoop;
|
|
400
466
|
#pingLoop = new _0_abortable_loop_js_1.AbortableLoop(this.#pingLoopBody.bind(this), (err) => {
|
|
401
467
|
this.#LpingLoop.error(err);
|
|
402
468
|
});
|
|
403
|
-
async #pingLoopBody(signal) {
|
|
469
|
+
async #pingLoopBody(_loop, signal) {
|
|
404
470
|
let timeElapsed = 0;
|
|
405
471
|
await (0, _0_deps_js_1.delay)(Math.max(0, this.#pingInterval - timeElapsed), { signal });
|
|
406
472
|
if (!this.connected) {
|
|
@@ -420,9 +486,7 @@ class SessionEncrypted extends _1_session_js_1.Session {
|
|
|
420
486
|
const ping_id = (0, _1_utilities_js_1.getRandomId)();
|
|
421
487
|
const call = { _: "ping_delay_disconnect", ping_id, disconnect_delay };
|
|
422
488
|
const messageId = await this.send(_2_tl_js_1.Mtproto.serializeObject(call));
|
|
423
|
-
|
|
424
|
-
this.#pendingPings.set(messageId, { call, resolve, reject });
|
|
425
|
-
});
|
|
489
|
+
this.#pendingPings.set(messageId, { call, promiseWithResolvers: Promise.withResolvers() });
|
|
426
490
|
}
|
|
427
491
|
async #resendPendingPing(pendingPing) {
|
|
428
492
|
try {
|
|
@@ -430,7 +494,7 @@ class SessionEncrypted extends _1_session_js_1.Session {
|
|
|
430
494
|
this.#pendingPings.set(messageId, pendingPing);
|
|
431
495
|
}
|
|
432
496
|
catch (err) {
|
|
433
|
-
pendingPing.reject(err);
|
|
497
|
+
pendingPing.promiseWithResolvers.reject(err);
|
|
434
498
|
}
|
|
435
499
|
}
|
|
436
500
|
}
|
|
@@ -22,7 +22,7 @@ import * as Api from "./1_telegram_api.js";
|
|
|
22
22
|
export * from "./1_telegram_api.js";
|
|
23
23
|
export type DeserializedType = boolean | number | bigint | string | Uint8Array | Api.AnyType | Array<DeserializedType>;
|
|
24
24
|
export declare function deserializeType<T extends (keyof Api.Types) | "X" | string>(name: T, bufferOrReader: TLReader | Uint8Array): Promise<T extends keyof Api.Types ? Api.Types[T] : DeserializedType>;
|
|
25
|
-
export declare function serializeObject(object: Api.AnyObject): Uint8Array
|
|
25
|
+
export declare function serializeObject(object: Api.AnyObject): Uint8Array<ArrayBuffer>;
|
|
26
26
|
export declare function isValidObject(object: any): object is Api.AnyType;
|
|
27
27
|
export declare function assertIsValidObject(object: any): asserts object is Api.AnyType;
|
|
28
28
|
export declare function is<S extends keyof (Api.Types & Api.Functions)>(name: S, value: unknown): value is S extends keyof Api.Types ? Api.Types[S] : S extends keyof Api.Functions ? Api.Functions[S] : never;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"2_telegram.d.ts","sourceRoot":"","sources":["../../src/tl/2_telegram.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAGH,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAI5C,OAAO,KAAK,GAAG,MAAM,qBAAqB,CAAC;AAG3C,cAAc,qBAAqB,CAAC;AAEpC,MAAM,MAAM,gBAAgB,GAAG,OAAO,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,UAAU,GAAG,GAAG,CAAC,OAAO,GAAG,KAAK,CAAC,gBAAgB,CAAC,CAAC;AAEvH,wBAAsB,eAAe,CAAC,CAAC,SAAS,CAAC,MAAM,GA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