@mtkruto/node 0.67.2 → 0.68.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/0_errors.js +2 -6
- package/esm/3_errors.js +2 -12
- package/esm/3_types.d.ts +1 -0
- package/esm/3_types.d.ts.map +1 -1
- package/esm/3_types.js +1 -0
- package/esm/4_errors.js +2 -12
- package/esm/_dnt.polyfills.d.ts +99 -0
- package/esm/_dnt.polyfills.d.ts.map +1 -1
- package/esm/_dnt.polyfills.js +127 -1
- package/esm/_dnt.test_polyfills.d.ts.map +1 -0
- package/esm/client/0_filters_test.d.ts.map +1 -1
- package/esm/client/0_markdown_test.d.ts.map +1 -1
- package/esm/client/0_params.d.ts +2 -0
- package/esm/client/0_params.d.ts.map +1 -1
- package/esm/client/0_password.d.ts +5 -5
- package/esm/client/0_password.d.ts.map +1 -1
- package/esm/client/0_password_test.d.ts.map +1 -1
- package/esm/client/0_storage_operations.js +143 -155
- package/esm/client/0_storage_operations_test.d.ts.map +1 -1
- package/esm/client/0_utilities.d.ts +1 -1
- package/esm/client/0_utilities_test.d.ts.map +1 -1
- package/esm/client/1_client_plain.js +4 -22
- package/esm/client/1_composer.js +8 -21
- package/esm/client/2_account_manager.js +85 -97
- package/esm/client/2_bot_info_manager.js +26 -38
- package/esm/client/2_business_connection_manager.js +10 -23
- package/esm/client/2_client_encrypted.js +194 -211
- package/esm/client/2_file_manager.d.ts +4 -3
- package/esm/client/2_file_manager.d.ts.map +1 -1
- package/esm/client/2_file_manager.js +268 -224
- package/esm/client/2_file_manager_test.d.ts.map +1 -1
- package/esm/client/2_network_statistics_manager.js +8 -21
- package/esm/client/2_payment_manager.js +7 -20
- package/esm/client/2_reaction_manager.js +7 -20
- package/esm/client/2_translations_manager.js +97 -107
- package/esm/client/2_update_manager.js +764 -759
- package/esm/client/3_client_encrypted_pool.js +10 -26
- package/esm/client/3_client_encrypted_pool_test.d.ts.map +1 -1
- package/esm/client/3_message_manager.d.ts +1 -1
- package/esm/client/3_message_manager.js +441 -448
- package/esm/client/3_message_manager_test.d.ts.map +1 -1
- package/esm/client/3_video_chat_manager.d.ts +1 -1
- package/esm/client/3_video_chat_manager.js +57 -68
- package/esm/client/4_callback_query_manager.js +18 -30
- package/esm/client/4_chat_list_manager.js +322 -318
- package/esm/client/4_chat_manager.js +118 -131
- package/esm/client/4_forum_manager.js +67 -73
- package/esm/client/4_gift_manager.js +16 -29
- package/esm/client/4_inline_query_manager.js +16 -28
- package/esm/client/4_link_preview_manager.js +6 -19
- package/esm/client/4_poll_manager.js +44 -57
- package/esm/client/4_story_manager.js +41 -53
- package/esm/client/5_client.d.ts +7 -0
- package/esm/client/5_client.d.ts.map +1 -1
- package/esm/client/5_client.js +1269 -1288
- package/esm/connection/1_connection_tcp.node.js +48 -65
- package/esm/connection/1_connection_web_socket.js +75 -91
- package/esm/deps/jsr.io/@roj/tgcrypto/1.0.1/dist/tgcrypto.js +3 -11
- package/esm/deps/jsr.io/@std/async/1.0.13/mux_async_iterator.js +31 -47
- package/esm/deps/jsr.io/@std/async/1.0.13/tee.js +11 -34
- package/esm/deps/jsr.io/@std/datetime/0.225.5/_date_time_formatter.js +4 -17
- package/esm/mod.js +2 -12
- package/esm/session/0_session_state.js +12 -38
- package/esm/session/1_session.js +49 -72
- package/esm/session/2_session_encrypted.js +324 -320
- package/esm/storage/1_utilities_test.d.ts.map +1 -1
- package/esm/storage/2_storage_indexed_db.js +26 -44
- package/esm/storage/2_storage_local_storage.node.js +12 -25
- package/esm/storage/2_storage_memory.js +24 -41
- package/esm/storage/2_storage_session_storage.js +3 -16
- package/esm/tl/0_utilities_test.d.ts.map +1 -1
- package/esm/tl/1_tl_reader.js +80 -88
- package/esm/tl/1_tl_reader_test.d.ts.map +1 -1
- package/esm/tl/1_tl_writer.js +155 -164
- package/esm/tl/1_tl_writer_test.d.ts.map +1 -1
- package/esm/transport/0_transport.js +1 -8
- package/esm/transport/1_transport_abridged.js +11 -24
- package/esm/transport/1_transport_intermediate.js +10 -23
- package/esm/transport/2_transport_provider_web_socket.d.ts.map +1 -1
- package/esm/types/0_upload_progress.d.ts +29 -0
- package/esm/types/0_upload_progress.d.ts.map +1 -0
- package/esm/types/0_upload_progress.js +20 -0
- package/esm/types/1_sticker.d.ts +1 -1
- package/esm/types/1_sticker.d.ts.map +1 -1
- package/esm/types/2_poll_answer.d.ts +19 -0
- package/esm/types/2_poll_answer.d.ts.map +1 -1
- package/esm/types/2_poll_answer.js +19 -0
- package/esm/types/7_update.d.ts +21 -2
- package/esm/types/7_update.d.ts.map +1 -1
- package/esm/utilities/0_bigint_test.d.ts.map +1 -1
- package/esm/utilities/0_buffer.d.ts +1 -1
- package/esm/utilities/0_buffer.d.ts.map +1 -1
- package/esm/utilities/0_buffer_test.d.ts.map +1 -1
- package/esm/utilities/0_hash.d.ts +2 -2
- package/esm/utilities/0_hash.d.ts.map +1 -1
- package/esm/utilities/0_mutex.js +4 -19
- package/esm/utilities/0_part_stream.js +11 -25
- package/esm/utilities/0_part_stream_test.d.ts.map +1 -1
- package/esm/utilities/0_rle.d.ts +2 -2
- package/esm/utilities/0_rle.d.ts.map +1 -1
- package/esm/utilities/0_rle_test.d.ts.map +1 -1
- package/esm/utilities/0_text.d.ts +20 -1
- package/esm/utilities/0_text.d.ts.map +1 -1
- package/esm/utilities/0_text.js +19 -0
- package/esm/utilities/1_auth.d.ts +1 -1
- package/esm/utilities/1_auth.d.ts.map +1 -1
- package/esm/utilities/1_base64.d.ts +1 -1
- package/esm/utilities/1_base64.d.ts.map +1 -1
- package/esm/utilities/1_base64_test.d.ts.map +1 -1
- package/esm/utilities/1_crypto.js +42 -53
- package/esm/utilities/1_crypto_test.d.ts.map +1 -1
- package/esm/utilities/1_logger.d.ts +2 -2
- package/esm/utilities/1_math_test.d.ts.map +1 -1
- package/esm/utilities/2_queue.js +29 -47
- package/package.json +1 -1
- package/script/0_errors.js +2 -6
- package/script/2_tl.js +17 -7
- package/script/3_errors.js +2 -12
- package/script/3_types.d.ts +1 -0
- package/script/3_types.d.ts.map +1 -1
- package/script/3_types.js +1 -0
- package/script/4_constants.js +17 -7
- package/script/4_errors.js +2 -12
- package/script/_dnt.polyfills.d.ts +99 -0
- package/script/_dnt.polyfills.d.ts.map +1 -1
- package/script/_dnt.polyfills.js +128 -0
- package/script/_dnt.test_polyfills.d.ts.map +1 -0
- package/script/client/0_filters_test.d.ts.map +1 -1
- package/script/client/0_markdown_test.d.ts.map +1 -1
- package/script/client/0_params.d.ts +2 -0
- package/script/client/0_params.d.ts.map +1 -1
- package/script/client/0_password.d.ts +5 -5
- package/script/client/0_password.d.ts.map +1 -1
- package/script/client/0_password.js +17 -7
- package/script/client/0_password_test.d.ts.map +1 -1
- package/script/client/0_storage_operations.js +143 -155
- package/script/client/0_storage_operations_test.d.ts.map +1 -1
- package/script/client/0_utilities.d.ts +1 -1
- package/script/client/0_utilities_test.d.ts.map +1 -1
- package/script/client/1_client_plain.js +4 -22
- package/script/client/1_composer.js +8 -21
- package/script/client/2_account_manager.js +85 -97
- package/script/client/2_bot_info_manager.js +26 -38
- package/script/client/2_business_connection_manager.js +10 -23
- package/script/client/2_client_encrypted.js +195 -212
- package/script/client/2_file_manager.d.ts +4 -3
- package/script/client/2_file_manager.d.ts.map +1 -1
- package/script/client/2_file_manager.js +285 -231
- package/script/client/2_file_manager_test.d.ts.map +1 -1
- package/script/client/2_network_statistics_manager.js +8 -21
- package/script/client/2_payment_manager.js +7 -20
- package/script/client/2_reaction_manager.js +7 -20
- package/script/client/2_translations_manager.js +98 -108
- package/script/client/2_update_manager.js +764 -759
- package/script/client/3_client_encrypted_pool.js +10 -26
- package/script/client/3_client_encrypted_pool_test.d.ts.map +1 -1
- package/script/client/3_message_manager.d.ts +1 -1
- package/script/client/3_message_manager.js +441 -448
- package/script/client/3_message_manager_test.d.ts.map +1 -1
- package/script/client/3_video_chat_manager.d.ts +1 -1
- package/script/client/3_video_chat_manager.js +57 -68
- package/script/client/4_callback_query_manager.js +18 -30
- package/script/client/4_chat_list_manager.js +322 -318
- package/script/client/4_chat_manager.js +118 -131
- package/script/client/4_forum_manager.js +67 -73
- package/script/client/4_gift_manager.js +16 -29
- package/script/client/4_inline_query_manager.js +16 -28
- package/script/client/4_link_preview_manager.js +6 -19
- package/script/client/4_poll_manager.js +44 -57
- package/script/client/4_story_manager.js +41 -53
- package/script/client/5_client.d.ts +7 -0
- package/script/client/5_client.d.ts.map +1 -1
- package/script/client/5_client.js +1269 -1288
- package/script/connection/1_connection_tcp.node.js +48 -65
- package/script/connection/1_connection_web_socket.js +92 -98
- package/script/deps/jsr.io/@roj/tgcrypto/1.0.1/dist/tgcrypto.js +3 -11
- package/script/deps/jsr.io/@std/assert/1.0.13/equal.js +17 -7
- package/script/deps/jsr.io/@std/async/1.0.13/delay.js +17 -7
- package/script/deps/jsr.io/@std/async/1.0.13/mux_async_iterator.js +31 -47
- package/script/deps/jsr.io/@std/async/1.0.13/tee.js +11 -34
- package/script/deps/jsr.io/@std/datetime/0.225.5/_date_time_formatter.js +4 -17
- package/script/deps/jsr.io/@std/internal/1.0.10/format.js +17 -7
- package/script/deps/jsr.io/@std/internal/1.0.10/styles.js +17 -7
- package/script/deps/jsr.io/@std/path/1.1.0/_os.js +17 -7
- package/script/mod.js +19 -19
- package/script/session/0_session_state.js +12 -38
- package/script/session/1_session.js +66 -79
- package/script/session/2_session_encrypted.js +325 -321
- package/script/storage/1_utilities_test.d.ts.map +1 -1
- package/script/storage/2_storage_indexed_db.js +26 -44
- package/script/storage/2_storage_local_storage.node.js +12 -25
- package/script/storage/2_storage_memory.js +24 -41
- package/script/storage/2_storage_session_storage.js +3 -16
- package/script/tl/0_utilities_test.d.ts.map +1 -1
- package/script/tl/1_tl_reader.js +81 -89
- package/script/tl/1_tl_reader_test.d.ts.map +1 -1
- package/script/tl/1_tl_writer.js +156 -165
- package/script/tl/1_tl_writer_test.d.ts.map +1 -1
- package/script/tl/2_telegram.js +17 -7
- package/script/transport/0_obfuscation.js +17 -7
- package/script/transport/0_transport.js +1 -8
- package/script/transport/1_transport_abridged.js +11 -24
- package/script/transport/1_transport_intermediate.js +10 -23
- package/script/transport/2_transport_provider_web_socket.d.ts.map +1 -1
- package/script/types/0_upload_progress.d.ts +29 -0
- package/script/types/0_upload_progress.d.ts.map +1 -0
- package/script/types/0_upload_progress.js +21 -0
- package/script/types/1_sticker.d.ts +1 -1
- package/script/types/1_sticker.d.ts.map +1 -1
- package/script/types/2_poll_answer.d.ts +19 -0
- package/script/types/2_poll_answer.d.ts.map +1 -1
- package/script/types/2_poll_answer.js +19 -0
- package/script/types/7_update.d.ts +21 -2
- package/script/types/7_update.d.ts.map +1 -1
- package/script/utilities/0_bigint.js +17 -7
- package/script/utilities/0_bigint_test.d.ts.map +1 -1
- package/script/utilities/0_buffer.d.ts +1 -1
- package/script/utilities/0_buffer.d.ts.map +1 -1
- package/script/utilities/0_buffer_test.d.ts.map +1 -1
- package/script/utilities/0_env.js +17 -7
- package/script/utilities/0_hash.d.ts +2 -2
- package/script/utilities/0_hash.d.ts.map +1 -1
- package/script/utilities/0_hash.js +17 -7
- package/script/utilities/0_misc.js +17 -7
- package/script/utilities/0_mutex.js +4 -19
- package/script/utilities/0_part_stream.js +11 -25
- package/script/utilities/0_part_stream_test.d.ts.map +1 -1
- package/script/utilities/0_rle.d.ts +2 -2
- package/script/utilities/0_rle.d.ts.map +1 -1
- package/script/utilities/0_rle_test.d.ts.map +1 -1
- package/script/utilities/0_text.d.ts +20 -1
- package/script/utilities/0_text.d.ts.map +1 -1
- package/script/utilities/0_text.js +19 -0
- package/script/utilities/1_auth.d.ts +1 -1
- package/script/utilities/1_auth.d.ts.map +1 -1
- package/script/utilities/1_auth.js +17 -7
- package/script/utilities/1_base64.d.ts +1 -1
- package/script/utilities/1_base64.d.ts.map +1 -1
- package/script/utilities/1_base64_test.d.ts.map +1 -1
- package/script/utilities/1_crypto.js +60 -61
- package/script/utilities/1_crypto_test.d.ts.map +1 -1
- package/script/utilities/1_logger.d.ts +2 -2
- package/script/utilities/1_math_test.d.ts.map +1 -1
- package/script/utilities/2_queue.js +30 -48
|
@@ -18,18 +18,6 @@
|
|
|
18
18
|
* You should have received a copy of the GNU Lesser General Public License
|
|
19
19
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
20
20
|
*/
|
|
21
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
22
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
23
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
24
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
25
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
26
|
-
};
|
|
27
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
28
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
29
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
30
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
31
|
-
};
|
|
32
|
-
var _SessionEncrypted_instances, _a, _SessionEncrypted_id, _SessionEncrypted_authKey, _SessionEncrypted_authKeyId, _SessionEncrypted_toAcknowledge, _SessionEncrypted_pendingMessages, _SessionEncrypted_pendingPings, _SessionEncrypted_L, _SessionEncrypted_TGCRYPTO_INITED, _SessionEncrypted_assertNotDisconnected, _SessionEncrypted_invalidateSession, _SessionEncrypted_rejectAllPending, _SessionEncrypted_onMessageFailed, _SessionEncrypted_setServerSalt, _SessionEncrypted_receive, _SessionEncrypted_encryptMessage, _SessionEncrypted_decryptMessage, _SessionEncrypted_startReceiveLoop, _SessionEncrypted_receiveLoopActive, _SessionEncrypted_receiveLoop, _SessionEncrypted_onMessage, _SessionEncrypted_onRpcResult, _SessionEncrypted_onMsgDetailedInfo, _SessionEncrypted_onMsgNewDetailedInfo, _SessionEncrypted_onBadMsgNotification, _SessionEncrypted_onBadServerSalt, _SessionEncrypted_onPong, _SessionEncrypted_onNewSessionCreated, _SessionEncrypted_onMessageContainer, _SessionEncrypted_pingInterval, _SessionEncrypted_startPingLoop, _SessionEncrypted_pingLoopAbortController, _SessionEncrypted_LpingLoop, _SessionEncrypted_pingLoop, _SessionEncrypted_sendPingDelayDisconnect, _SessionEncrypted_resendPendingPing;
|
|
33
21
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
34
22
|
exports.SessionEncrypted = void 0;
|
|
35
23
|
const _0_deps_js_1 = require("../0_deps.js");
|
|
@@ -45,62 +33,96 @@ const GZIP_PACKED = 0x3072CFA1;
|
|
|
45
33
|
const RPC_RESULT = 0xF35C6D01;
|
|
46
34
|
const RPC_ERROR = _2_tl_js_1.Mtproto.schema.definitions["rpc_error"][0];
|
|
47
35
|
class SessionEncrypted extends _1_session_js_1.Session {
|
|
36
|
+
#id = (0, _1_utilities_js_1.getRandomId)();
|
|
37
|
+
#authKey = new Uint8Array();
|
|
38
|
+
#authKeyId = 0n;
|
|
39
|
+
handlers = {};
|
|
40
|
+
#toAcknowledge = new Array();
|
|
41
|
+
#pendingMessages = new Set();
|
|
42
|
+
#pendingPings = new Map();
|
|
43
|
+
#L;
|
|
44
|
+
static #TGCRYPTO_INITED = false;
|
|
48
45
|
constructor(dc, params) {
|
|
49
46
|
super(dc, params);
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
_SessionEncrypted_authKey.set(this, new Uint8Array());
|
|
53
|
-
_SessionEncrypted_authKeyId.set(this, 0n);
|
|
54
|
-
Object.defineProperty(this, "handlers", {
|
|
55
|
-
enumerable: true,
|
|
56
|
-
configurable: true,
|
|
57
|
-
writable: true,
|
|
58
|
-
value: {}
|
|
59
|
-
});
|
|
60
|
-
_SessionEncrypted_toAcknowledge.set(this, new Array());
|
|
61
|
-
_SessionEncrypted_pendingMessages.set(this, new Set());
|
|
62
|
-
_SessionEncrypted_pendingPings.set(this, new Map());
|
|
63
|
-
_SessionEncrypted_L.set(this, void 0);
|
|
64
|
-
_SessionEncrypted_receiveLoopActive.set(this, false);
|
|
65
|
-
//// PING LOOP ////
|
|
66
|
-
_SessionEncrypted_pingInterval.set(this, 56 * _0_deps_js_1.SECOND);
|
|
67
|
-
_SessionEncrypted_pingLoopAbortController.set(this, void 0);
|
|
68
|
-
_SessionEncrypted_LpingLoop.set(this, void 0);
|
|
69
|
-
const L = __classPrivateFieldSet(this, _SessionEncrypted_L, (0, _1_utilities_js_1.getLogger)("SessionEncrypted").client(id++), "f");
|
|
70
|
-
__classPrivateFieldSet(this, _SessionEncrypted_LpingLoop, L.branch("#pingLoop"), "f");
|
|
47
|
+
const L = this.#L = (0, _1_utilities_js_1.getLogger)("SessionEncrypted").client(id++);
|
|
48
|
+
this.#LpingLoop = L.branch("#pingLoop");
|
|
71
49
|
}
|
|
72
50
|
async setAuthKey(key) {
|
|
73
51
|
const hash = await (0, _1_utilities_js_1.sha1)(key);
|
|
74
|
-
|
|
75
|
-
|
|
52
|
+
this.#authKeyId = (0, _1_utilities_js_1.bigIntFromBuffer)(hash.slice(-8), true, false);
|
|
53
|
+
this.#authKey = key;
|
|
76
54
|
}
|
|
77
55
|
get authKey() {
|
|
78
|
-
return
|
|
56
|
+
return this.#authKey;
|
|
79
57
|
}
|
|
80
58
|
async connect() {
|
|
81
59
|
if (!this.connected) {
|
|
82
|
-
|
|
60
|
+
this.#rejectAllPending(new _0_errors_js_1.ConnectionError("Not connected."));
|
|
83
61
|
}
|
|
84
62
|
await super.connect();
|
|
85
|
-
if (!
|
|
63
|
+
if (!SessionEncrypted.#TGCRYPTO_INITED) {
|
|
86
64
|
await (0, _0_deps_js_1.initTgCrypto)();
|
|
87
|
-
|
|
65
|
+
SessionEncrypted.#TGCRYPTO_INITED = true;
|
|
88
66
|
}
|
|
89
|
-
|
|
90
|
-
|
|
67
|
+
this.#startReceiveLoop();
|
|
68
|
+
this.#startPingLoop();
|
|
91
69
|
}
|
|
92
70
|
disconnect() {
|
|
93
71
|
super.disconnect();
|
|
94
72
|
this.state.reset();
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
73
|
+
this.#id = (0, _1_utilities_js_1.getRandomId)();
|
|
74
|
+
this.#pingLoopAbortController?.abort();
|
|
75
|
+
this.#rejectAllPending(new _0_errors_js_1.ConnectionError("Not connected."));
|
|
76
|
+
}
|
|
77
|
+
#assertNotDisconnected() {
|
|
78
|
+
if (this.disconnected) {
|
|
79
|
+
throw new _0_errors_js_1.ConnectionError("Not connected.");
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
async #invalidateSession(reason) {
|
|
83
|
+
this.#L.debug("invalidating session because of", reason);
|
|
84
|
+
this.#id = (0, _1_utilities_js_1.getRandomId)();
|
|
85
|
+
this.state.reset();
|
|
86
|
+
this.disconnect();
|
|
87
|
+
await this.connect();
|
|
88
|
+
this.#rejectAllPending(new _0_session_error_js_1.SessionError("Session invalidated."));
|
|
89
|
+
}
|
|
90
|
+
#rejectAllPending(reason) {
|
|
91
|
+
for (const id of this.#pendingMessages) {
|
|
92
|
+
this.#onMessageFailed(id, reason);
|
|
93
|
+
}
|
|
94
|
+
this.#pendingMessages.clear();
|
|
95
|
+
for (const pendingPing of this.#pendingPings.values()) {
|
|
96
|
+
pendingPing.reject(reason);
|
|
97
|
+
}
|
|
98
|
+
this.#pendingPings.clear();
|
|
99
|
+
}
|
|
100
|
+
#onMessageFailed(id, reason) {
|
|
101
|
+
this.#pendingMessages.delete(id);
|
|
102
|
+
const pendingPing = this.#pendingPings.get(id);
|
|
103
|
+
if (pendingPing) {
|
|
104
|
+
this.#pendingPings.delete(id);
|
|
105
|
+
if (reason instanceof _0_session_error_js_1.SessionError) {
|
|
106
|
+
(0, _1_utilities_js_1.drop)(this.#resendPendingPing(pendingPing));
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
pendingPing.reject(reason);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
// message was not sent by us
|
|
114
|
+
this.handlers.onMessageFailed?.(id, reason);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
#setServerSalt(newServerSalt) {
|
|
118
|
+
this.state.serverSalt = newServerSalt;
|
|
119
|
+
this.handlers.onNewServerSalt?.(newServerSalt);
|
|
98
120
|
}
|
|
99
121
|
async send(body) {
|
|
100
122
|
if (!this.disconnected && !this.connected) {
|
|
101
123
|
await super.waitUntilConnected();
|
|
102
124
|
}
|
|
103
|
-
|
|
125
|
+
this.#assertNotDisconnected();
|
|
104
126
|
const msg_id = this.state.nextMessageId();
|
|
105
127
|
const seqno = this.state.nextSeqNo(true);
|
|
106
128
|
let message = {
|
|
@@ -109,12 +131,12 @@ class SessionEncrypted extends _1_session_js_1.Session {
|
|
|
109
131
|
seqno,
|
|
110
132
|
body,
|
|
111
133
|
};
|
|
112
|
-
if (
|
|
134
|
+
if (this.#toAcknowledge.length) {
|
|
113
135
|
const ack = {
|
|
114
136
|
_: "message",
|
|
115
137
|
msg_id: this.state.nextMessageId(),
|
|
116
138
|
seqno: this.state.nextSeqNo(false),
|
|
117
|
-
body: _2_tl_js_1.Mtproto.serializeObject({ _: "msgs_ack", msg_ids:
|
|
139
|
+
body: _2_tl_js_1.Mtproto.serializeObject({ _: "msgs_ack", msg_ids: this.#toAcknowledge.splice(0, 8192) }),
|
|
118
140
|
};
|
|
119
141
|
message = {
|
|
120
142
|
_: "message",
|
|
@@ -126,309 +148,291 @@ class SessionEncrypted extends _1_session_js_1.Session {
|
|
|
126
148
|
},
|
|
127
149
|
};
|
|
128
150
|
}
|
|
129
|
-
|
|
130
|
-
const payload = await
|
|
151
|
+
this.#L.out(message);
|
|
152
|
+
const payload = await this.#encryptMessage(message);
|
|
131
153
|
await this.transport.transport.send(payload);
|
|
132
|
-
|
|
154
|
+
this.#pendingMessages.add(msg_id);
|
|
133
155
|
return msg_id;
|
|
134
156
|
}
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
157
|
+
async #receive() {
|
|
158
|
+
this.#assertNotDisconnected();
|
|
159
|
+
const buffer = await this.transport.transport.receive();
|
|
160
|
+
if (buffer.length == 4) {
|
|
161
|
+
const int = (0, _1_utilities_js_1.bigIntFromBuffer)(buffer, true, true);
|
|
162
|
+
throw new _0_errors_js_1.TransportError(Number(int));
|
|
163
|
+
}
|
|
164
|
+
try {
|
|
165
|
+
const decrypted = await this.#decryptMessage(buffer);
|
|
166
|
+
this.#L.in(decrypted);
|
|
167
|
+
return decrypted;
|
|
168
|
+
}
|
|
169
|
+
catch (err) {
|
|
170
|
+
await this.#invalidateSession("decryption error");
|
|
171
|
+
throw err;
|
|
172
|
+
}
|
|
140
173
|
}
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
174
|
+
async #encryptMessage(message) {
|
|
175
|
+
const payloadWriter = new _1_tl_writer_js_1.TLWriter();
|
|
176
|
+
payloadWriter.writeInt64(this.state.serverSalt);
|
|
177
|
+
payloadWriter.writeInt64(this.#id);
|
|
178
|
+
payloadWriter.write(await (0, _2_tl_js_1.serializeMessage)(message));
|
|
179
|
+
payloadWriter.write(new Uint8Array((0, _1_utilities_js_1.mod)(-(payloadWriter.buffer.length + 12), 16) + 12));
|
|
180
|
+
const payload = payloadWriter.buffer;
|
|
181
|
+
const messageKey = (await (0, _1_utilities_js_1.sha256)((0, _0_deps_js_1.concat)([this.#authKey.subarray(88, 120), payload]))).subarray(8, 24);
|
|
182
|
+
const a = await (0, _1_utilities_js_1.sha256)((0, _0_deps_js_1.concat)([messageKey, this.#authKey.subarray(0, 36)]));
|
|
183
|
+
const b = await (0, _1_utilities_js_1.sha256)((0, _0_deps_js_1.concat)([this.#authKey.subarray(40, 76), messageKey]));
|
|
184
|
+
const aesKey = (0, _0_deps_js_1.concat)([a.subarray(0, 8), b.subarray(8, 24), a.subarray(24, 32)]);
|
|
185
|
+
const aesIV = (0, _0_deps_js_1.concat)([b.subarray(0, 8), a.subarray(8, 24), b.subarray(24, 32)]);
|
|
186
|
+
const messageWriter = new _1_tl_writer_js_1.TLWriter();
|
|
187
|
+
messageWriter.writeInt64(this.#authKeyId);
|
|
188
|
+
messageWriter.write(messageKey);
|
|
189
|
+
messageWriter.write((0, _0_deps_js_1.ige256Encrypt)(payload, aesKey, aesIV));
|
|
190
|
+
return messageWriter.buffer;
|
|
191
|
+
}
|
|
192
|
+
async #decryptMessage(buffer) {
|
|
193
|
+
const reader = new _2_tl_js_1.TLReader(buffer);
|
|
194
|
+
(0, _0_deps_js_1.assertEquals)(reader.readInt64(false), this.#authKeyId);
|
|
195
|
+
const messageKey_ = reader.readInt128();
|
|
196
|
+
const messageKey = (0, _1_utilities_js_1.bufferFromBigInt)(messageKey_, 16, true, true);
|
|
197
|
+
const a = await (0, _1_utilities_js_1.sha256)((0, _0_deps_js_1.concat)([messageKey, this.#authKey.subarray(8, 44)]));
|
|
198
|
+
const b = await (0, _1_utilities_js_1.sha256)((0, _0_deps_js_1.concat)([this.#authKey.subarray(48, 84), messageKey]));
|
|
199
|
+
const aesKey = (0, _0_deps_js_1.concat)([a.subarray(0, 8), b.subarray(8, 24), a.subarray(24, 32)]);
|
|
200
|
+
const aesIv = (0, _0_deps_js_1.concat)([b.subarray(0, 8), a.subarray(8, 24), b.subarray(24, 32)]);
|
|
201
|
+
const plaintext = (0, _0_deps_js_1.ige256Decrypt)(reader.buffer, aesKey, aesIv);
|
|
202
|
+
(0, _0_deps_js_1.assertEquals)(plaintext.buffer.byteLength % 4, 0);
|
|
203
|
+
const plainReader = new _2_tl_js_1.TLReader(plaintext);
|
|
204
|
+
const _salt = plainReader.readInt64();
|
|
205
|
+
const _sessionId_ = plainReader.readInt64(false);
|
|
206
|
+
return (0, _2_tl_js_1.deserializeMessage)(plainReader);
|
|
207
|
+
}
|
|
208
|
+
//// RECEIVE LOOP ////
|
|
209
|
+
#startReceiveLoop() {
|
|
210
|
+
if (!this.#receiveLoopActive) {
|
|
211
|
+
(0, _1_utilities_js_1.drop)(this.#receiveLoop());
|
|
212
|
+
}
|
|
151
213
|
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
214
|
+
#receiveLoopActive = false;
|
|
215
|
+
async #receiveLoop() {
|
|
216
|
+
this.#receiveLoopActive = true;
|
|
217
|
+
try {
|
|
218
|
+
while (this.connected) {
|
|
219
|
+
let message;
|
|
220
|
+
try {
|
|
221
|
+
message = await this.#receive();
|
|
222
|
+
}
|
|
223
|
+
catch (err) {
|
|
224
|
+
this.#L.error("failed to receive message:", err);
|
|
225
|
+
if (!this.connected) {
|
|
226
|
+
break;
|
|
227
|
+
}
|
|
228
|
+
else {
|
|
229
|
+
continue;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
try {
|
|
233
|
+
if (message.body instanceof Uint8Array) {
|
|
234
|
+
this.#onMessage(message.msg_id, message.body);
|
|
235
|
+
}
|
|
236
|
+
else {
|
|
237
|
+
this.#onMessageContainer(message.msg_id, message.body);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
catch (err) {
|
|
241
|
+
this.#L.error("failed to handle message:", err);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
finally {
|
|
246
|
+
this.#receiveLoopActive = false;
|
|
247
|
+
}
|
|
155
248
|
}
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
249
|
+
//// RECEIVE LOOP HANDLERS ////
|
|
250
|
+
async #onMessage(msgId, body) {
|
|
251
|
+
let reader = new _2_tl_js_1.TLReader(body);
|
|
252
|
+
let id = reader.readInt32(false);
|
|
253
|
+
if (id == GZIP_PACKED) {
|
|
254
|
+
reader = new _2_tl_js_1.TLReader(await (0, _1_utilities_js_1.gunzip)(reader.readBytes()));
|
|
255
|
+
id = reader.readInt32(false);
|
|
256
|
+
}
|
|
257
|
+
if (id == RPC_RESULT) {
|
|
258
|
+
this.#onRpcResult(msgId, reader.buffer);
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
if (!_2_tl_js_1.Mtproto.schema.identifierToName[id]) {
|
|
262
|
+
reader.unreadInt32();
|
|
263
|
+
this.handlers.onUpdate?.(reader.buffer);
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
let type;
|
|
267
|
+
try {
|
|
268
|
+
reader.unreadInt32();
|
|
269
|
+
type = await _2_tl_js_1.Mtproto.deserializeType(_2_tl_js_1.X, reader);
|
|
270
|
+
}
|
|
271
|
+
catch (err) {
|
|
272
|
+
this.#L.error("failed to deserialize MTProto type:", err);
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
this.#L.debug("received", (0, _2_tl_js_1.repr)(type));
|
|
276
|
+
if (_2_tl_js_1.Mtproto.is("new_session_created", type)) {
|
|
277
|
+
this.#onNewSessionCreated(msgId, type);
|
|
278
|
+
}
|
|
279
|
+
else if (_2_tl_js_1.Mtproto.is("pong", type)) {
|
|
280
|
+
this.#onPong(msgId, type);
|
|
281
|
+
}
|
|
282
|
+
else if (_2_tl_js_1.Mtproto.is("bad_server_salt", type)) {
|
|
283
|
+
this.#onBadServerSalt(type);
|
|
284
|
+
}
|
|
285
|
+
else if (_2_tl_js_1.Mtproto.is("bad_msg_notification", type)) {
|
|
286
|
+
await this.#onBadMsgNotification(msgId, type);
|
|
287
|
+
}
|
|
288
|
+
else if (_2_tl_js_1.Mtproto.is("msg_detailed_info", type)) {
|
|
289
|
+
this.#onMsgDetailedInfo(type);
|
|
290
|
+
}
|
|
291
|
+
else if (_2_tl_js_1.Mtproto.is("msg_new_detailed_info", type)) {
|
|
292
|
+
this.#onMsgNewDetailedInfo(type);
|
|
164
293
|
}
|
|
165
294
|
else {
|
|
166
|
-
|
|
295
|
+
this.#L.debug(`unhandled MTProto type: ${(0, _2_tl_js_1.repr)(type)}`);
|
|
167
296
|
}
|
|
168
297
|
}
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
298
|
+
async #onRpcResult(msgId, body) {
|
|
299
|
+
this.#toAcknowledge.push(msgId);
|
|
300
|
+
let reader = new _2_tl_js_1.TLReader(body);
|
|
301
|
+
const reqMsgId = reader.readInt64();
|
|
302
|
+
let id = reader.readInt32(false);
|
|
303
|
+
if (id == GZIP_PACKED) {
|
|
304
|
+
reader = new _2_tl_js_1.TLReader(await (0, _1_utilities_js_1.gunzip)(reader.readBytes()));
|
|
305
|
+
id = reader.readInt32(false);
|
|
306
|
+
reader.unreadInt32();
|
|
307
|
+
}
|
|
308
|
+
else {
|
|
309
|
+
reader.unreadInt32();
|
|
310
|
+
}
|
|
311
|
+
if (id == RPC_ERROR) {
|
|
312
|
+
const error = await _2_tl_js_1.Mtproto.deserializeType("rpc_error", reader);
|
|
313
|
+
this.handlers.onRpcError?.(reqMsgId, error);
|
|
314
|
+
}
|
|
315
|
+
else {
|
|
316
|
+
this.handlers.onRpcResult?.(reqMsgId, reader.buffer);
|
|
317
|
+
}
|
|
182
318
|
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
319
|
+
#onMsgDetailedInfo(msgDetailedInfo) {
|
|
320
|
+
this.#toAcknowledge.push(msgDetailedInfo.answer_msg_id);
|
|
321
|
+
}
|
|
322
|
+
#onMsgNewDetailedInfo(msgNewDetailedInfo) {
|
|
323
|
+
this.#toAcknowledge.push(msgNewDetailedInfo.answer_msg_id);
|
|
324
|
+
}
|
|
325
|
+
async #onBadMsgNotification(msgId, badMsgNotification) {
|
|
326
|
+
let low = false;
|
|
327
|
+
switch (badMsgNotification.error_code) {
|
|
328
|
+
case 16: // message ID too low
|
|
329
|
+
low = true;
|
|
330
|
+
/* falls through */
|
|
331
|
+
case 17: // message ID too high
|
|
332
|
+
this.state.timeDifference = Math.abs((0, _1_utilities_js_1.toUnixTimestamp)(new Date()) - Number(msgId >> 32n));
|
|
333
|
+
if (!low) {
|
|
334
|
+
this.state.timeDifference = -this.state.timeDifference;
|
|
335
|
+
await this.#invalidateSession("message ID too high");
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
else {
|
|
339
|
+
this.#L.debug("message ID too low, resending message");
|
|
340
|
+
}
|
|
341
|
+
break;
|
|
342
|
+
case 48: // bad server salt
|
|
343
|
+
// resend
|
|
344
|
+
this.#L.debug("resending message that caused bad_server_salt");
|
|
345
|
+
break;
|
|
346
|
+
default:
|
|
347
|
+
await this.#invalidateSession("unexpected bad_msg_notification");
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
this.#onMessageFailed(badMsgNotification.bad_msg_id, new _0_session_error_js_1.SessionError(badMsgNotification._));
|
|
351
|
+
}
|
|
352
|
+
#onBadServerSalt(badServerSalt) {
|
|
353
|
+
this.#setServerSalt(badServerSalt.new_server_salt);
|
|
354
|
+
this.#onMessageFailed(badServerSalt.bad_msg_id, new _0_session_error_js_1.SessionError(badServerSalt._));
|
|
355
|
+
}
|
|
356
|
+
#onPong(msgId, pong) {
|
|
357
|
+
this.#toAcknowledge.push(msgId);
|
|
358
|
+
const pendingPing = this.#pendingPings.get(pong.msg_id);
|
|
359
|
+
if (pendingPing) {
|
|
360
|
+
pendingPing.resolve(pong);
|
|
361
|
+
this.#pendingPings.delete(pong.msg_id);
|
|
362
|
+
}
|
|
363
|
+
else {
|
|
364
|
+
// pong is not ours
|
|
365
|
+
this.handlers.onPong?.(pong);
|
|
366
|
+
}
|
|
187
367
|
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
368
|
+
#onNewSessionCreated(msgId, newSessionCreated) {
|
|
369
|
+
this.#setServerSalt(newSessionCreated.server_salt);
|
|
370
|
+
this.#toAcknowledge.push(msgId);
|
|
191
371
|
}
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
const b = await (0, _1_utilities_js_1.sha256)((0, _0_deps_js_1.concat)([__classPrivateFieldGet(this, _SessionEncrypted_authKey, "f").subarray(40, 76), messageKey]));
|
|
202
|
-
const aesKey = (0, _0_deps_js_1.concat)([a.subarray(0, 8), b.subarray(8, 24), a.subarray(24, 32)]);
|
|
203
|
-
const aesIV = (0, _0_deps_js_1.concat)([b.subarray(0, 8), a.subarray(8, 24), b.subarray(24, 32)]);
|
|
204
|
-
const messageWriter = new _1_tl_writer_js_1.TLWriter();
|
|
205
|
-
messageWriter.writeInt64(__classPrivateFieldGet(this, _SessionEncrypted_authKeyId, "f"));
|
|
206
|
-
messageWriter.write(messageKey);
|
|
207
|
-
messageWriter.write((0, _0_deps_js_1.ige256Encrypt)(payload, aesKey, aesIV));
|
|
208
|
-
return messageWriter.buffer;
|
|
209
|
-
}, _SessionEncrypted_decryptMessage = async function _SessionEncrypted_decryptMessage(buffer) {
|
|
210
|
-
const reader = new _2_tl_js_1.TLReader(buffer);
|
|
211
|
-
(0, _0_deps_js_1.assertEquals)(reader.readInt64(false), __classPrivateFieldGet(this, _SessionEncrypted_authKeyId, "f"));
|
|
212
|
-
const messageKey_ = reader.readInt128();
|
|
213
|
-
const messageKey = (0, _1_utilities_js_1.bufferFromBigInt)(messageKey_, 16, true, true);
|
|
214
|
-
const a = await (0, _1_utilities_js_1.sha256)((0, _0_deps_js_1.concat)([messageKey, __classPrivateFieldGet(this, _SessionEncrypted_authKey, "f").subarray(8, 44)]));
|
|
215
|
-
const b = await (0, _1_utilities_js_1.sha256)((0, _0_deps_js_1.concat)([__classPrivateFieldGet(this, _SessionEncrypted_authKey, "f").subarray(48, 84), messageKey]));
|
|
216
|
-
const aesKey = (0, _0_deps_js_1.concat)([a.subarray(0, 8), b.subarray(8, 24), a.subarray(24, 32)]);
|
|
217
|
-
const aesIv = (0, _0_deps_js_1.concat)([b.subarray(0, 8), a.subarray(8, 24), b.subarray(24, 32)]);
|
|
218
|
-
const plaintext = (0, _0_deps_js_1.ige256Decrypt)(reader.buffer, aesKey, aesIv);
|
|
219
|
-
(0, _0_deps_js_1.assertEquals)(plaintext.buffer.byteLength % 4, 0);
|
|
220
|
-
const plainReader = new _2_tl_js_1.TLReader(plaintext);
|
|
221
|
-
const _salt = plainReader.readInt64();
|
|
222
|
-
const _sessionId_ = plainReader.readInt64(false);
|
|
223
|
-
return (0, _2_tl_js_1.deserializeMessage)(plainReader);
|
|
224
|
-
}, _SessionEncrypted_startReceiveLoop = function _SessionEncrypted_startReceiveLoop() {
|
|
225
|
-
if (!__classPrivateFieldGet(this, _SessionEncrypted_receiveLoopActive, "f")) {
|
|
226
|
-
(0, _1_utilities_js_1.drop)(__classPrivateFieldGet(this, _SessionEncrypted_instances, "m", _SessionEncrypted_receiveLoop).call(this));
|
|
372
|
+
#onMessageContainer(msgId, msgContainer) {
|
|
373
|
+
for (const message of msgContainer.messages) {
|
|
374
|
+
if (message.body instanceof Uint8Array) {
|
|
375
|
+
this.#onMessage(message.msg_id, message.body);
|
|
376
|
+
}
|
|
377
|
+
else {
|
|
378
|
+
this.#onMessageContainer(msgId, message.body);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
227
381
|
}
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
382
|
+
//// PING LOOP ////
|
|
383
|
+
#pingInterval = 56 * _0_deps_js_1.SECOND;
|
|
384
|
+
#startPingLoop() {
|
|
385
|
+
(0, _1_utilities_js_1.drop)(this.#pingLoop());
|
|
386
|
+
}
|
|
387
|
+
#pingLoopAbortController;
|
|
388
|
+
#LpingLoop;
|
|
389
|
+
async #pingLoop() {
|
|
390
|
+
this.#pingLoopAbortController?.abort();
|
|
391
|
+
const controller = this.#pingLoopAbortController = new AbortController();
|
|
392
|
+
let timeElapsed = 0;
|
|
231
393
|
while (this.connected) {
|
|
232
|
-
let message;
|
|
233
394
|
try {
|
|
234
|
-
|
|
235
|
-
}
|
|
236
|
-
catch (err) {
|
|
237
|
-
__classPrivateFieldGet(this, _SessionEncrypted_L, "f").error("failed to receive message:", err);
|
|
395
|
+
await (0, _0_deps_js_1.delay)(Math.max(0, this.#pingInterval - timeElapsed), { signal: controller.signal });
|
|
238
396
|
if (!this.connected) {
|
|
239
|
-
break;
|
|
240
|
-
}
|
|
241
|
-
else {
|
|
242
397
|
continue;
|
|
243
398
|
}
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
399
|
+
controller.signal.throwIfAborted();
|
|
400
|
+
const then = Date.now();
|
|
401
|
+
try {
|
|
402
|
+
await this.#sendPingDelayDisconnect(this.#pingInterval / _0_deps_js_1.SECOND + 15);
|
|
248
403
|
}
|
|
249
|
-
|
|
250
|
-
|
|
404
|
+
finally {
|
|
405
|
+
timeElapsed = Date.now() - then;
|
|
251
406
|
}
|
|
407
|
+
controller.signal.throwIfAborted();
|
|
252
408
|
}
|
|
253
409
|
catch (err) {
|
|
254
|
-
|
|
410
|
+
if (err instanceof DOMException && err.name == "AbortError") {
|
|
411
|
+
break;
|
|
412
|
+
}
|
|
413
|
+
else if (!this.connected) {
|
|
414
|
+
break;
|
|
415
|
+
}
|
|
416
|
+
this.#LpingLoop.error(err);
|
|
255
417
|
}
|
|
256
418
|
}
|
|
257
419
|
}
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
let id = reader.readInt32(false);
|
|
266
|
-
if (id == GZIP_PACKED) {
|
|
267
|
-
reader = new _2_tl_js_1.TLReader(await (0, _1_utilities_js_1.gunzip)(reader.readBytes()));
|
|
268
|
-
id = reader.readInt32(false);
|
|
269
|
-
}
|
|
270
|
-
if (id == RPC_RESULT) {
|
|
271
|
-
__classPrivateFieldGet(this, _SessionEncrypted_instances, "m", _SessionEncrypted_onRpcResult).call(this, msgId, reader.buffer);
|
|
272
|
-
return;
|
|
273
|
-
}
|
|
274
|
-
if (!_2_tl_js_1.Mtproto.schema.identifierToName[id]) {
|
|
275
|
-
reader.unreadInt32();
|
|
276
|
-
this.handlers.onUpdate?.(reader.buffer);
|
|
277
|
-
return;
|
|
278
|
-
}
|
|
279
|
-
let type;
|
|
280
|
-
try {
|
|
281
|
-
reader.unreadInt32();
|
|
282
|
-
type = await _2_tl_js_1.Mtproto.deserializeType(_2_tl_js_1.X, reader);
|
|
283
|
-
}
|
|
284
|
-
catch (err) {
|
|
285
|
-
__classPrivateFieldGet(this, _SessionEncrypted_L, "f").error("failed to deserialize MTProto type:", err);
|
|
286
|
-
return;
|
|
287
|
-
}
|
|
288
|
-
__classPrivateFieldGet(this, _SessionEncrypted_L, "f").debug("received", (0, _2_tl_js_1.repr)(type));
|
|
289
|
-
if (_2_tl_js_1.Mtproto.is("new_session_created", type)) {
|
|
290
|
-
__classPrivateFieldGet(this, _SessionEncrypted_instances, "m", _SessionEncrypted_onNewSessionCreated).call(this, msgId, type);
|
|
291
|
-
}
|
|
292
|
-
else if (_2_tl_js_1.Mtproto.is("pong", type)) {
|
|
293
|
-
__classPrivateFieldGet(this, _SessionEncrypted_instances, "m", _SessionEncrypted_onPong).call(this, msgId, type);
|
|
294
|
-
}
|
|
295
|
-
else if (_2_tl_js_1.Mtproto.is("bad_server_salt", type)) {
|
|
296
|
-
__classPrivateFieldGet(this, _SessionEncrypted_instances, "m", _SessionEncrypted_onBadServerSalt).call(this, type);
|
|
297
|
-
}
|
|
298
|
-
else if (_2_tl_js_1.Mtproto.is("bad_msg_notification", type)) {
|
|
299
|
-
await __classPrivateFieldGet(this, _SessionEncrypted_instances, "m", _SessionEncrypted_onBadMsgNotification).call(this, msgId, type);
|
|
300
|
-
}
|
|
301
|
-
else if (_2_tl_js_1.Mtproto.is("msg_detailed_info", type)) {
|
|
302
|
-
__classPrivateFieldGet(this, _SessionEncrypted_instances, "m", _SessionEncrypted_onMsgDetailedInfo).call(this, type);
|
|
303
|
-
}
|
|
304
|
-
else if (_2_tl_js_1.Mtproto.is("msg_new_detailed_info", type)) {
|
|
305
|
-
__classPrivateFieldGet(this, _SessionEncrypted_instances, "m", _SessionEncrypted_onMsgNewDetailedInfo).call(this, type);
|
|
306
|
-
}
|
|
307
|
-
else {
|
|
308
|
-
__classPrivateFieldGet(this, _SessionEncrypted_L, "f").debug(`unhandled MTProto type: ${(0, _2_tl_js_1.repr)(type)}`);
|
|
309
|
-
}
|
|
310
|
-
}, _SessionEncrypted_onRpcResult = async function _SessionEncrypted_onRpcResult(msgId, body) {
|
|
311
|
-
__classPrivateFieldGet(this, _SessionEncrypted_toAcknowledge, "f").push(msgId);
|
|
312
|
-
let reader = new _2_tl_js_1.TLReader(body);
|
|
313
|
-
const reqMsgId = reader.readInt64();
|
|
314
|
-
let id = reader.readInt32(false);
|
|
315
|
-
if (id == GZIP_PACKED) {
|
|
316
|
-
reader = new _2_tl_js_1.TLReader(await (0, _1_utilities_js_1.gunzip)(reader.readBytes()));
|
|
317
|
-
id = reader.readInt32(false);
|
|
318
|
-
reader.unreadInt32();
|
|
319
|
-
}
|
|
320
|
-
else {
|
|
321
|
-
reader.unreadInt32();
|
|
322
|
-
}
|
|
323
|
-
if (id == RPC_ERROR) {
|
|
324
|
-
const error = await _2_tl_js_1.Mtproto.deserializeType("rpc_error", reader);
|
|
325
|
-
this.handlers.onRpcError?.(reqMsgId, error);
|
|
326
|
-
}
|
|
327
|
-
else {
|
|
328
|
-
this.handlers.onRpcResult?.(reqMsgId, reader.buffer);
|
|
329
|
-
}
|
|
330
|
-
}, _SessionEncrypted_onMsgDetailedInfo = function _SessionEncrypted_onMsgDetailedInfo(msgDetailedInfo) {
|
|
331
|
-
__classPrivateFieldGet(this, _SessionEncrypted_toAcknowledge, "f").push(msgDetailedInfo.answer_msg_id);
|
|
332
|
-
}, _SessionEncrypted_onMsgNewDetailedInfo = function _SessionEncrypted_onMsgNewDetailedInfo(msgNewDetailedInfo) {
|
|
333
|
-
__classPrivateFieldGet(this, _SessionEncrypted_toAcknowledge, "f").push(msgNewDetailedInfo.answer_msg_id);
|
|
334
|
-
}, _SessionEncrypted_onBadMsgNotification = async function _SessionEncrypted_onBadMsgNotification(msgId, badMsgNotification) {
|
|
335
|
-
let low = false;
|
|
336
|
-
switch (badMsgNotification.error_code) {
|
|
337
|
-
case 16: // message ID too low
|
|
338
|
-
low = true;
|
|
339
|
-
/* falls through */
|
|
340
|
-
case 17: // message ID too high
|
|
341
|
-
this.state.timeDifference = Math.abs((0, _1_utilities_js_1.toUnixTimestamp)(new Date()) - Number(msgId >> 32n));
|
|
342
|
-
if (!low) {
|
|
343
|
-
this.state.timeDifference = -this.state.timeDifference;
|
|
344
|
-
await __classPrivateFieldGet(this, _SessionEncrypted_instances, "m", _SessionEncrypted_invalidateSession).call(this, "message ID too high");
|
|
345
|
-
return;
|
|
346
|
-
}
|
|
347
|
-
else {
|
|
348
|
-
__classPrivateFieldGet(this, _SessionEncrypted_L, "f").debug("message ID too low, resending message");
|
|
349
|
-
}
|
|
350
|
-
break;
|
|
351
|
-
case 48: // bad server salt
|
|
352
|
-
// resend
|
|
353
|
-
__classPrivateFieldGet(this, _SessionEncrypted_L, "f").debug("resending message that caused bad_server_salt");
|
|
354
|
-
break;
|
|
355
|
-
default:
|
|
356
|
-
await __classPrivateFieldGet(this, _SessionEncrypted_instances, "m", _SessionEncrypted_invalidateSession).call(this, "unexpected bad_msg_notification");
|
|
357
|
-
return;
|
|
358
|
-
}
|
|
359
|
-
__classPrivateFieldGet(this, _SessionEncrypted_instances, "m", _SessionEncrypted_onMessageFailed).call(this, badMsgNotification.bad_msg_id, new _0_session_error_js_1.SessionError(badMsgNotification._));
|
|
360
|
-
}, _SessionEncrypted_onBadServerSalt = function _SessionEncrypted_onBadServerSalt(badServerSalt) {
|
|
361
|
-
__classPrivateFieldGet(this, _SessionEncrypted_instances, "m", _SessionEncrypted_setServerSalt).call(this, badServerSalt.new_server_salt);
|
|
362
|
-
__classPrivateFieldGet(this, _SessionEncrypted_instances, "m", _SessionEncrypted_onMessageFailed).call(this, badServerSalt.bad_msg_id, new _0_session_error_js_1.SessionError(badServerSalt._));
|
|
363
|
-
}, _SessionEncrypted_onPong = function _SessionEncrypted_onPong(msgId, pong) {
|
|
364
|
-
__classPrivateFieldGet(this, _SessionEncrypted_toAcknowledge, "f").push(msgId);
|
|
365
|
-
const pendingPing = __classPrivateFieldGet(this, _SessionEncrypted_pendingPings, "f").get(pong.msg_id);
|
|
366
|
-
if (pendingPing) {
|
|
367
|
-
pendingPing.resolve(pong);
|
|
368
|
-
__classPrivateFieldGet(this, _SessionEncrypted_pendingPings, "f").delete(pong.msg_id);
|
|
369
|
-
}
|
|
370
|
-
else {
|
|
371
|
-
// pong is not ours
|
|
372
|
-
this.handlers.onPong?.(pong);
|
|
373
|
-
}
|
|
374
|
-
}, _SessionEncrypted_onNewSessionCreated = function _SessionEncrypted_onNewSessionCreated(msgId, newSessionCreated) {
|
|
375
|
-
__classPrivateFieldGet(this, _SessionEncrypted_instances, "m", _SessionEncrypted_setServerSalt).call(this, newSessionCreated.server_salt);
|
|
376
|
-
__classPrivateFieldGet(this, _SessionEncrypted_toAcknowledge, "f").push(msgId);
|
|
377
|
-
}, _SessionEncrypted_onMessageContainer = function _SessionEncrypted_onMessageContainer(msgId, msgContainer) {
|
|
378
|
-
for (const message of msgContainer.messages) {
|
|
379
|
-
if (message.body instanceof Uint8Array) {
|
|
380
|
-
__classPrivateFieldGet(this, _SessionEncrypted_instances, "m", _SessionEncrypted_onMessage).call(this, message.msg_id, message.body);
|
|
381
|
-
}
|
|
382
|
-
else {
|
|
383
|
-
__classPrivateFieldGet(this, _SessionEncrypted_instances, "m", _SessionEncrypted_onMessageContainer).call(this, msgId, message.body);
|
|
384
|
-
}
|
|
420
|
+
async #sendPingDelayDisconnect(disconnect_delay) {
|
|
421
|
+
const ping_id = (0, _1_utilities_js_1.getRandomId)();
|
|
422
|
+
const call = { _: "ping_delay_disconnect", ping_id, disconnect_delay };
|
|
423
|
+
const messageId = await this.send(_2_tl_js_1.Mtproto.serializeObject(call));
|
|
424
|
+
await new Promise((resolve, reject) => {
|
|
425
|
+
this.#pendingPings.set(messageId, { call, resolve, reject });
|
|
426
|
+
});
|
|
385
427
|
}
|
|
386
|
-
|
|
387
|
-
(0, _1_utilities_js_1.drop)(__classPrivateFieldGet(this, _SessionEncrypted_instances, "m", _SessionEncrypted_pingLoop).call(this));
|
|
388
|
-
}, _SessionEncrypted_pingLoop = async function _SessionEncrypted_pingLoop() {
|
|
389
|
-
__classPrivateFieldGet(this, _SessionEncrypted_pingLoopAbortController, "f")?.abort();
|
|
390
|
-
const controller = __classPrivateFieldSet(this, _SessionEncrypted_pingLoopAbortController, new AbortController(), "f");
|
|
391
|
-
let timeElapsed = 0;
|
|
392
|
-
while (this.connected) {
|
|
428
|
+
async #resendPendingPing(pendingPing) {
|
|
393
429
|
try {
|
|
394
|
-
await (
|
|
395
|
-
|
|
396
|
-
continue;
|
|
397
|
-
}
|
|
398
|
-
controller.signal.throwIfAborted();
|
|
399
|
-
const then = Date.now();
|
|
400
|
-
try {
|
|
401
|
-
await __classPrivateFieldGet(this, _SessionEncrypted_instances, "m", _SessionEncrypted_sendPingDelayDisconnect).call(this, __classPrivateFieldGet(this, _SessionEncrypted_pingInterval, "f") / _0_deps_js_1.SECOND + 15);
|
|
402
|
-
}
|
|
403
|
-
finally {
|
|
404
|
-
timeElapsed = Date.now() - then;
|
|
405
|
-
}
|
|
406
|
-
controller.signal.throwIfAborted();
|
|
430
|
+
const messageId = await this.send(_2_tl_js_1.Mtproto.serializeObject(pendingPing.call));
|
|
431
|
+
this.#pendingPings.set(messageId, pendingPing);
|
|
407
432
|
}
|
|
408
433
|
catch (err) {
|
|
409
|
-
|
|
410
|
-
break;
|
|
411
|
-
}
|
|
412
|
-
else if (!this.connected) {
|
|
413
|
-
break;
|
|
414
|
-
}
|
|
415
|
-
__classPrivateFieldGet(this, _SessionEncrypted_LpingLoop, "f").error(err);
|
|
434
|
+
pendingPing.reject(err);
|
|
416
435
|
}
|
|
417
436
|
}
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
const call = { _: "ping_delay_disconnect", ping_id, disconnect_delay };
|
|
421
|
-
const messageId = await this.send(_2_tl_js_1.Mtproto.serializeObject(call));
|
|
422
|
-
await new Promise((resolve, reject) => {
|
|
423
|
-
__classPrivateFieldGet(this, _SessionEncrypted_pendingPings, "f").set(messageId, { call, resolve, reject });
|
|
424
|
-
});
|
|
425
|
-
}, _SessionEncrypted_resendPendingPing = async function _SessionEncrypted_resendPendingPing(pendingPing) {
|
|
426
|
-
try {
|
|
427
|
-
const messageId = await this.send(_2_tl_js_1.Mtproto.serializeObject(pendingPing.call));
|
|
428
|
-
__classPrivateFieldGet(this, _SessionEncrypted_pendingPings, "f").set(messageId, pendingPing);
|
|
429
|
-
}
|
|
430
|
-
catch (err) {
|
|
431
|
-
pendingPing.reject(err);
|
|
432
|
-
}
|
|
433
|
-
};
|
|
434
|
-
_SessionEncrypted_TGCRYPTO_INITED = { value: false };
|
|
437
|
+
}
|
|
438
|
+
exports.SessionEncrypted = SessionEncrypted;
|