@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.
Files changed (244) hide show
  1. package/esm/0_errors.js +2 -6
  2. package/esm/3_errors.js +2 -12
  3. package/esm/3_types.d.ts +1 -0
  4. package/esm/3_types.d.ts.map +1 -1
  5. package/esm/3_types.js +1 -0
  6. package/esm/4_errors.js +2 -12
  7. package/esm/_dnt.polyfills.d.ts +99 -0
  8. package/esm/_dnt.polyfills.d.ts.map +1 -1
  9. package/esm/_dnt.polyfills.js +127 -1
  10. package/esm/_dnt.test_polyfills.d.ts.map +1 -0
  11. package/esm/client/0_filters_test.d.ts.map +1 -1
  12. package/esm/client/0_markdown_test.d.ts.map +1 -1
  13. package/esm/client/0_params.d.ts +2 -0
  14. package/esm/client/0_params.d.ts.map +1 -1
  15. package/esm/client/0_password.d.ts +5 -5
  16. package/esm/client/0_password.d.ts.map +1 -1
  17. package/esm/client/0_password_test.d.ts.map +1 -1
  18. package/esm/client/0_storage_operations.js +143 -155
  19. package/esm/client/0_storage_operations_test.d.ts.map +1 -1
  20. package/esm/client/0_utilities.d.ts +1 -1
  21. package/esm/client/0_utilities_test.d.ts.map +1 -1
  22. package/esm/client/1_client_plain.js +4 -22
  23. package/esm/client/1_composer.js +8 -21
  24. package/esm/client/2_account_manager.js +85 -97
  25. package/esm/client/2_bot_info_manager.js +26 -38
  26. package/esm/client/2_business_connection_manager.js +10 -23
  27. package/esm/client/2_client_encrypted.js +194 -211
  28. package/esm/client/2_file_manager.d.ts +4 -3
  29. package/esm/client/2_file_manager.d.ts.map +1 -1
  30. package/esm/client/2_file_manager.js +268 -224
  31. package/esm/client/2_file_manager_test.d.ts.map +1 -1
  32. package/esm/client/2_network_statistics_manager.js +8 -21
  33. package/esm/client/2_payment_manager.js +7 -20
  34. package/esm/client/2_reaction_manager.js +7 -20
  35. package/esm/client/2_translations_manager.js +97 -107
  36. package/esm/client/2_update_manager.js +764 -759
  37. package/esm/client/3_client_encrypted_pool.js +10 -26
  38. package/esm/client/3_client_encrypted_pool_test.d.ts.map +1 -1
  39. package/esm/client/3_message_manager.d.ts +1 -1
  40. package/esm/client/3_message_manager.js +441 -448
  41. package/esm/client/3_message_manager_test.d.ts.map +1 -1
  42. package/esm/client/3_video_chat_manager.d.ts +1 -1
  43. package/esm/client/3_video_chat_manager.js +57 -68
  44. package/esm/client/4_callback_query_manager.js +18 -30
  45. package/esm/client/4_chat_list_manager.js +322 -318
  46. package/esm/client/4_chat_manager.js +118 -131
  47. package/esm/client/4_forum_manager.js +67 -73
  48. package/esm/client/4_gift_manager.js +16 -29
  49. package/esm/client/4_inline_query_manager.js +16 -28
  50. package/esm/client/4_link_preview_manager.js +6 -19
  51. package/esm/client/4_poll_manager.js +44 -57
  52. package/esm/client/4_story_manager.js +41 -53
  53. package/esm/client/5_client.d.ts +7 -0
  54. package/esm/client/5_client.d.ts.map +1 -1
  55. package/esm/client/5_client.js +1269 -1288
  56. package/esm/connection/1_connection_tcp.node.js +48 -65
  57. package/esm/connection/1_connection_web_socket.js +75 -91
  58. package/esm/deps/jsr.io/@roj/tgcrypto/1.0.1/dist/tgcrypto.js +3 -11
  59. package/esm/deps/jsr.io/@std/async/1.0.13/mux_async_iterator.js +31 -47
  60. package/esm/deps/jsr.io/@std/async/1.0.13/tee.js +11 -34
  61. package/esm/deps/jsr.io/@std/datetime/0.225.5/_date_time_formatter.js +4 -17
  62. package/esm/mod.js +2 -12
  63. package/esm/session/0_session_state.js +12 -38
  64. package/esm/session/1_session.js +49 -72
  65. package/esm/session/2_session_encrypted.js +324 -320
  66. package/esm/storage/1_utilities_test.d.ts.map +1 -1
  67. package/esm/storage/2_storage_indexed_db.js +26 -44
  68. package/esm/storage/2_storage_local_storage.node.js +12 -25
  69. package/esm/storage/2_storage_memory.js +24 -41
  70. package/esm/storage/2_storage_session_storage.js +3 -16
  71. package/esm/tl/0_utilities_test.d.ts.map +1 -1
  72. package/esm/tl/1_tl_reader.js +80 -88
  73. package/esm/tl/1_tl_reader_test.d.ts.map +1 -1
  74. package/esm/tl/1_tl_writer.js +155 -164
  75. package/esm/tl/1_tl_writer_test.d.ts.map +1 -1
  76. package/esm/transport/0_transport.js +1 -8
  77. package/esm/transport/1_transport_abridged.js +11 -24
  78. package/esm/transport/1_transport_intermediate.js +10 -23
  79. package/esm/transport/2_transport_provider_web_socket.d.ts.map +1 -1
  80. package/esm/types/0_upload_progress.d.ts +29 -0
  81. package/esm/types/0_upload_progress.d.ts.map +1 -0
  82. package/esm/types/0_upload_progress.js +20 -0
  83. package/esm/types/1_sticker.d.ts +1 -1
  84. package/esm/types/1_sticker.d.ts.map +1 -1
  85. package/esm/types/2_poll_answer.d.ts +19 -0
  86. package/esm/types/2_poll_answer.d.ts.map +1 -1
  87. package/esm/types/2_poll_answer.js +19 -0
  88. package/esm/types/7_update.d.ts +21 -2
  89. package/esm/types/7_update.d.ts.map +1 -1
  90. package/esm/utilities/0_bigint_test.d.ts.map +1 -1
  91. package/esm/utilities/0_buffer.d.ts +1 -1
  92. package/esm/utilities/0_buffer.d.ts.map +1 -1
  93. package/esm/utilities/0_buffer_test.d.ts.map +1 -1
  94. package/esm/utilities/0_hash.d.ts +2 -2
  95. package/esm/utilities/0_hash.d.ts.map +1 -1
  96. package/esm/utilities/0_mutex.js +4 -19
  97. package/esm/utilities/0_part_stream.js +11 -25
  98. package/esm/utilities/0_part_stream_test.d.ts.map +1 -1
  99. package/esm/utilities/0_rle.d.ts +2 -2
  100. package/esm/utilities/0_rle.d.ts.map +1 -1
  101. package/esm/utilities/0_rle_test.d.ts.map +1 -1
  102. package/esm/utilities/0_text.d.ts +20 -1
  103. package/esm/utilities/0_text.d.ts.map +1 -1
  104. package/esm/utilities/0_text.js +19 -0
  105. package/esm/utilities/1_auth.d.ts +1 -1
  106. package/esm/utilities/1_auth.d.ts.map +1 -1
  107. package/esm/utilities/1_base64.d.ts +1 -1
  108. package/esm/utilities/1_base64.d.ts.map +1 -1
  109. package/esm/utilities/1_base64_test.d.ts.map +1 -1
  110. package/esm/utilities/1_crypto.js +42 -53
  111. package/esm/utilities/1_crypto_test.d.ts.map +1 -1
  112. package/esm/utilities/1_logger.d.ts +2 -2
  113. package/esm/utilities/1_math_test.d.ts.map +1 -1
  114. package/esm/utilities/2_queue.js +29 -47
  115. package/package.json +1 -1
  116. package/script/0_errors.js +2 -6
  117. package/script/2_tl.js +17 -7
  118. package/script/3_errors.js +2 -12
  119. package/script/3_types.d.ts +1 -0
  120. package/script/3_types.d.ts.map +1 -1
  121. package/script/3_types.js +1 -0
  122. package/script/4_constants.js +17 -7
  123. package/script/4_errors.js +2 -12
  124. package/script/_dnt.polyfills.d.ts +99 -0
  125. package/script/_dnt.polyfills.d.ts.map +1 -1
  126. package/script/_dnt.polyfills.js +128 -0
  127. package/script/_dnt.test_polyfills.d.ts.map +1 -0
  128. package/script/client/0_filters_test.d.ts.map +1 -1
  129. package/script/client/0_markdown_test.d.ts.map +1 -1
  130. package/script/client/0_params.d.ts +2 -0
  131. package/script/client/0_params.d.ts.map +1 -1
  132. package/script/client/0_password.d.ts +5 -5
  133. package/script/client/0_password.d.ts.map +1 -1
  134. package/script/client/0_password.js +17 -7
  135. package/script/client/0_password_test.d.ts.map +1 -1
  136. package/script/client/0_storage_operations.js +143 -155
  137. package/script/client/0_storage_operations_test.d.ts.map +1 -1
  138. package/script/client/0_utilities.d.ts +1 -1
  139. package/script/client/0_utilities_test.d.ts.map +1 -1
  140. package/script/client/1_client_plain.js +4 -22
  141. package/script/client/1_composer.js +8 -21
  142. package/script/client/2_account_manager.js +85 -97
  143. package/script/client/2_bot_info_manager.js +26 -38
  144. package/script/client/2_business_connection_manager.js +10 -23
  145. package/script/client/2_client_encrypted.js +195 -212
  146. package/script/client/2_file_manager.d.ts +4 -3
  147. package/script/client/2_file_manager.d.ts.map +1 -1
  148. package/script/client/2_file_manager.js +285 -231
  149. package/script/client/2_file_manager_test.d.ts.map +1 -1
  150. package/script/client/2_network_statistics_manager.js +8 -21
  151. package/script/client/2_payment_manager.js +7 -20
  152. package/script/client/2_reaction_manager.js +7 -20
  153. package/script/client/2_translations_manager.js +98 -108
  154. package/script/client/2_update_manager.js +764 -759
  155. package/script/client/3_client_encrypted_pool.js +10 -26
  156. package/script/client/3_client_encrypted_pool_test.d.ts.map +1 -1
  157. package/script/client/3_message_manager.d.ts +1 -1
  158. package/script/client/3_message_manager.js +441 -448
  159. package/script/client/3_message_manager_test.d.ts.map +1 -1
  160. package/script/client/3_video_chat_manager.d.ts +1 -1
  161. package/script/client/3_video_chat_manager.js +57 -68
  162. package/script/client/4_callback_query_manager.js +18 -30
  163. package/script/client/4_chat_list_manager.js +322 -318
  164. package/script/client/4_chat_manager.js +118 -131
  165. package/script/client/4_forum_manager.js +67 -73
  166. package/script/client/4_gift_manager.js +16 -29
  167. package/script/client/4_inline_query_manager.js +16 -28
  168. package/script/client/4_link_preview_manager.js +6 -19
  169. package/script/client/4_poll_manager.js +44 -57
  170. package/script/client/4_story_manager.js +41 -53
  171. package/script/client/5_client.d.ts +7 -0
  172. package/script/client/5_client.d.ts.map +1 -1
  173. package/script/client/5_client.js +1269 -1288
  174. package/script/connection/1_connection_tcp.node.js +48 -65
  175. package/script/connection/1_connection_web_socket.js +92 -98
  176. package/script/deps/jsr.io/@roj/tgcrypto/1.0.1/dist/tgcrypto.js +3 -11
  177. package/script/deps/jsr.io/@std/assert/1.0.13/equal.js +17 -7
  178. package/script/deps/jsr.io/@std/async/1.0.13/delay.js +17 -7
  179. package/script/deps/jsr.io/@std/async/1.0.13/mux_async_iterator.js +31 -47
  180. package/script/deps/jsr.io/@std/async/1.0.13/tee.js +11 -34
  181. package/script/deps/jsr.io/@std/datetime/0.225.5/_date_time_formatter.js +4 -17
  182. package/script/deps/jsr.io/@std/internal/1.0.10/format.js +17 -7
  183. package/script/deps/jsr.io/@std/internal/1.0.10/styles.js +17 -7
  184. package/script/deps/jsr.io/@std/path/1.1.0/_os.js +17 -7
  185. package/script/mod.js +19 -19
  186. package/script/session/0_session_state.js +12 -38
  187. package/script/session/1_session.js +66 -79
  188. package/script/session/2_session_encrypted.js +325 -321
  189. package/script/storage/1_utilities_test.d.ts.map +1 -1
  190. package/script/storage/2_storage_indexed_db.js +26 -44
  191. package/script/storage/2_storage_local_storage.node.js +12 -25
  192. package/script/storage/2_storage_memory.js +24 -41
  193. package/script/storage/2_storage_session_storage.js +3 -16
  194. package/script/tl/0_utilities_test.d.ts.map +1 -1
  195. package/script/tl/1_tl_reader.js +81 -89
  196. package/script/tl/1_tl_reader_test.d.ts.map +1 -1
  197. package/script/tl/1_tl_writer.js +156 -165
  198. package/script/tl/1_tl_writer_test.d.ts.map +1 -1
  199. package/script/tl/2_telegram.js +17 -7
  200. package/script/transport/0_obfuscation.js +17 -7
  201. package/script/transport/0_transport.js +1 -8
  202. package/script/transport/1_transport_abridged.js +11 -24
  203. package/script/transport/1_transport_intermediate.js +10 -23
  204. package/script/transport/2_transport_provider_web_socket.d.ts.map +1 -1
  205. package/script/types/0_upload_progress.d.ts +29 -0
  206. package/script/types/0_upload_progress.d.ts.map +1 -0
  207. package/script/types/0_upload_progress.js +21 -0
  208. package/script/types/1_sticker.d.ts +1 -1
  209. package/script/types/1_sticker.d.ts.map +1 -1
  210. package/script/types/2_poll_answer.d.ts +19 -0
  211. package/script/types/2_poll_answer.d.ts.map +1 -1
  212. package/script/types/2_poll_answer.js +19 -0
  213. package/script/types/7_update.d.ts +21 -2
  214. package/script/types/7_update.d.ts.map +1 -1
  215. package/script/utilities/0_bigint.js +17 -7
  216. package/script/utilities/0_bigint_test.d.ts.map +1 -1
  217. package/script/utilities/0_buffer.d.ts +1 -1
  218. package/script/utilities/0_buffer.d.ts.map +1 -1
  219. package/script/utilities/0_buffer_test.d.ts.map +1 -1
  220. package/script/utilities/0_env.js +17 -7
  221. package/script/utilities/0_hash.d.ts +2 -2
  222. package/script/utilities/0_hash.d.ts.map +1 -1
  223. package/script/utilities/0_hash.js +17 -7
  224. package/script/utilities/0_misc.js +17 -7
  225. package/script/utilities/0_mutex.js +4 -19
  226. package/script/utilities/0_part_stream.js +11 -25
  227. package/script/utilities/0_part_stream_test.d.ts.map +1 -1
  228. package/script/utilities/0_rle.d.ts +2 -2
  229. package/script/utilities/0_rle.d.ts.map +1 -1
  230. package/script/utilities/0_rle_test.d.ts.map +1 -1
  231. package/script/utilities/0_text.d.ts +20 -1
  232. package/script/utilities/0_text.d.ts.map +1 -1
  233. package/script/utilities/0_text.js +19 -0
  234. package/script/utilities/1_auth.d.ts +1 -1
  235. package/script/utilities/1_auth.d.ts.map +1 -1
  236. package/script/utilities/1_auth.js +17 -7
  237. package/script/utilities/1_base64.d.ts +1 -1
  238. package/script/utilities/1_base64.d.ts.map +1 -1
  239. package/script/utilities/1_base64_test.d.ts.map +1 -1
  240. package/script/utilities/1_crypto.js +60 -61
  241. package/script/utilities/1_crypto_test.d.ts.map +1 -1
  242. package/script/utilities/1_logger.d.ts +2 -2
  243. package/script/utilities/1_math_test.d.ts.map +1 -1
  244. 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
- _SessionEncrypted_instances.add(this);
51
- _SessionEncrypted_id.set(this, (0, _1_utilities_js_1.getRandomId)());
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
- __classPrivateFieldSet(this, _SessionEncrypted_authKeyId, (0, _1_utilities_js_1.bigIntFromBuffer)(hash.slice(-8), true, false), "f");
75
- __classPrivateFieldSet(this, _SessionEncrypted_authKey, key, "f");
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 __classPrivateFieldGet(this, _SessionEncrypted_authKey, "f");
56
+ return this.#authKey;
79
57
  }
80
58
  async connect() {
81
59
  if (!this.connected) {
82
- __classPrivateFieldGet(this, _SessionEncrypted_instances, "m", _SessionEncrypted_rejectAllPending).call(this, new _0_errors_js_1.ConnectionError("Not connected."));
60
+ this.#rejectAllPending(new _0_errors_js_1.ConnectionError("Not connected."));
83
61
  }
84
62
  await super.connect();
85
- if (!__classPrivateFieldGet(_a, _a, "f", _SessionEncrypted_TGCRYPTO_INITED)) {
63
+ if (!SessionEncrypted.#TGCRYPTO_INITED) {
86
64
  await (0, _0_deps_js_1.initTgCrypto)();
87
- __classPrivateFieldSet(_a, _a, true, "f", _SessionEncrypted_TGCRYPTO_INITED);
65
+ SessionEncrypted.#TGCRYPTO_INITED = true;
88
66
  }
89
- __classPrivateFieldGet(this, _SessionEncrypted_instances, "m", _SessionEncrypted_startReceiveLoop).call(this);
90
- __classPrivateFieldGet(this, _SessionEncrypted_instances, "m", _SessionEncrypted_startPingLoop).call(this);
67
+ this.#startReceiveLoop();
68
+ this.#startPingLoop();
91
69
  }
92
70
  disconnect() {
93
71
  super.disconnect();
94
72
  this.state.reset();
95
- __classPrivateFieldSet(this, _SessionEncrypted_id, (0, _1_utilities_js_1.getRandomId)(), "f");
96
- __classPrivateFieldGet(this, _SessionEncrypted_pingLoopAbortController, "f")?.abort();
97
- __classPrivateFieldGet(this, _SessionEncrypted_instances, "m", _SessionEncrypted_rejectAllPending).call(this, new _0_errors_js_1.ConnectionError("Not connected."));
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
- __classPrivateFieldGet(this, _SessionEncrypted_instances, "m", _SessionEncrypted_assertNotDisconnected).call(this);
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 (__classPrivateFieldGet(this, _SessionEncrypted_toAcknowledge, "f").length) {
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: __classPrivateFieldGet(this, _SessionEncrypted_toAcknowledge, "f").splice(0, 8192) }),
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
- __classPrivateFieldGet(this, _SessionEncrypted_L, "f").out(message);
130
- const payload = await __classPrivateFieldGet(this, _SessionEncrypted_instances, "m", _SessionEncrypted_encryptMessage).call(this, message);
151
+ this.#L.out(message);
152
+ const payload = await this.#encryptMessage(message);
131
153
  await this.transport.transport.send(payload);
132
- __classPrivateFieldGet(this, _SessionEncrypted_pendingMessages, "f").add(msg_id);
154
+ this.#pendingMessages.add(msg_id);
133
155
  return msg_id;
134
156
  }
135
- }
136
- exports.SessionEncrypted = SessionEncrypted;
137
- _a = SessionEncrypted, _SessionEncrypted_id = new WeakMap(), _SessionEncrypted_authKey = new WeakMap(), _SessionEncrypted_authKeyId = new WeakMap(), _SessionEncrypted_toAcknowledge = new WeakMap(), _SessionEncrypted_pendingMessages = new WeakMap(), _SessionEncrypted_pendingPings = new WeakMap(), _SessionEncrypted_L = new WeakMap(), _SessionEncrypted_receiveLoopActive = new WeakMap(), _SessionEncrypted_pingInterval = new WeakMap(), _SessionEncrypted_pingLoopAbortController = new WeakMap(), _SessionEncrypted_LpingLoop = new WeakMap(), _SessionEncrypted_instances = new WeakSet(), _SessionEncrypted_assertNotDisconnected = function _SessionEncrypted_assertNotDisconnected() {
138
- if (this.disconnected) {
139
- throw new _0_errors_js_1.ConnectionError("Not connected.");
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
- }, _SessionEncrypted_invalidateSession = async function _SessionEncrypted_invalidateSession(reason) {
142
- __classPrivateFieldGet(this, _SessionEncrypted_L, "f").debug("invalidating session because of", reason);
143
- __classPrivateFieldSet(this, _SessionEncrypted_id, (0, _1_utilities_js_1.getRandomId)(), "f");
144
- this.state.reset();
145
- this.disconnect();
146
- await this.connect();
147
- __classPrivateFieldGet(this, _SessionEncrypted_instances, "m", _SessionEncrypted_rejectAllPending).call(this, new _0_session_error_js_1.SessionError("Session invalidated."));
148
- }, _SessionEncrypted_rejectAllPending = function _SessionEncrypted_rejectAllPending(reason) {
149
- for (const id of __classPrivateFieldGet(this, _SessionEncrypted_pendingMessages, "f")) {
150
- __classPrivateFieldGet(this, _SessionEncrypted_instances, "m", _SessionEncrypted_onMessageFailed).call(this, id, reason);
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
- __classPrivateFieldGet(this, _SessionEncrypted_pendingMessages, "f").clear();
153
- for (const pendingPing of __classPrivateFieldGet(this, _SessionEncrypted_pendingPings, "f").values()) {
154
- pendingPing.reject(reason);
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
- __classPrivateFieldGet(this, _SessionEncrypted_pendingPings, "f").clear();
157
- }, _SessionEncrypted_onMessageFailed = function _SessionEncrypted_onMessageFailed(id, reason) {
158
- __classPrivateFieldGet(this, _SessionEncrypted_pendingMessages, "f").delete(id);
159
- const pendingPing = __classPrivateFieldGet(this, _SessionEncrypted_pendingPings, "f").get(id);
160
- if (pendingPing) {
161
- __classPrivateFieldGet(this, _SessionEncrypted_pendingPings, "f").delete(id);
162
- if (reason instanceof _0_session_error_js_1.SessionError) {
163
- (0, _1_utilities_js_1.drop)(__classPrivateFieldGet(this, _SessionEncrypted_instances, "m", _SessionEncrypted_resendPendingPing).call(this, pendingPing));
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
- pendingPing.reject(reason);
295
+ this.#L.debug(`unhandled MTProto type: ${(0, _2_tl_js_1.repr)(type)}`);
167
296
  }
168
297
  }
169
- else {
170
- // message was not sent by us
171
- this.handlers.onMessageFailed?.(id, reason);
172
- }
173
- }, _SessionEncrypted_setServerSalt = function _SessionEncrypted_setServerSalt(newServerSalt) {
174
- this.state.serverSalt = newServerSalt;
175
- this.handlers.onNewServerSalt?.(newServerSalt);
176
- }, _SessionEncrypted_receive = async function _SessionEncrypted_receive() {
177
- __classPrivateFieldGet(this, _SessionEncrypted_instances, "m", _SessionEncrypted_assertNotDisconnected).call(this);
178
- const buffer = await this.transport.transport.receive();
179
- if (buffer.length == 4) {
180
- const int = (0, _1_utilities_js_1.bigIntFromBuffer)(buffer, true, true);
181
- throw new _0_errors_js_1.TransportError(Number(int));
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
- try {
184
- const decrypted = await __classPrivateFieldGet(this, _SessionEncrypted_instances, "m", _SessionEncrypted_decryptMessage).call(this, buffer);
185
- __classPrivateFieldGet(this, _SessionEncrypted_L, "f").in(decrypted);
186
- return decrypted;
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
- catch (err) {
189
- await __classPrivateFieldGet(this, _SessionEncrypted_instances, "m", _SessionEncrypted_invalidateSession).call(this, "decryption error");
190
- throw err;
368
+ #onNewSessionCreated(msgId, newSessionCreated) {
369
+ this.#setServerSalt(newSessionCreated.server_salt);
370
+ this.#toAcknowledge.push(msgId);
191
371
  }
192
- }, _SessionEncrypted_encryptMessage = async function _SessionEncrypted_encryptMessage(message) {
193
- const payloadWriter = new _1_tl_writer_js_1.TLWriter();
194
- payloadWriter.writeInt64(this.state.serverSalt);
195
- payloadWriter.writeInt64(__classPrivateFieldGet(this, _SessionEncrypted_id, "f"));
196
- payloadWriter.write(await (0, _2_tl_js_1.serializeMessage)(message));
197
- payloadWriter.write(new Uint8Array((0, _1_utilities_js_1.mod)(-(payloadWriter.buffer.length + 12), 16) + 12));
198
- const payload = payloadWriter.buffer;
199
- const messageKey = (await (0, _1_utilities_js_1.sha256)((0, _0_deps_js_1.concat)([__classPrivateFieldGet(this, _SessionEncrypted_authKey, "f").subarray(88, 120), payload]))).subarray(8, 24);
200
- const a = await (0, _1_utilities_js_1.sha256)((0, _0_deps_js_1.concat)([messageKey, __classPrivateFieldGet(this, _SessionEncrypted_authKey, "f").subarray(0, 36)]));
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
- }, _SessionEncrypted_receiveLoop = async function _SessionEncrypted_receiveLoop() {
229
- __classPrivateFieldSet(this, _SessionEncrypted_receiveLoopActive, true, "f");
230
- try {
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
- message = await __classPrivateFieldGet(this, _SessionEncrypted_instances, "m", _SessionEncrypted_receive).call(this);
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
- try {
246
- if (message.body instanceof Uint8Array) {
247
- __classPrivateFieldGet(this, _SessionEncrypted_instances, "m", _SessionEncrypted_onMessage).call(this, message.msg_id, message.body);
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
- else {
250
- __classPrivateFieldGet(this, _SessionEncrypted_instances, "m", _SessionEncrypted_onMessageContainer).call(this, message.msg_id, message.body);
404
+ finally {
405
+ timeElapsed = Date.now() - then;
251
406
  }
407
+ controller.signal.throwIfAborted();
252
408
  }
253
409
  catch (err) {
254
- __classPrivateFieldGet(this, _SessionEncrypted_L, "f").error("failed to handle message:", err);
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
- finally {
259
- __classPrivateFieldSet(this, _SessionEncrypted_receiveLoopActive, false, "f");
260
- }
261
- }, _SessionEncrypted_onMessage =
262
- //// RECEIVE LOOP HANDLERS ////
263
- async function _SessionEncrypted_onMessage(msgId, body) {
264
- let reader = new _2_tl_js_1.TLReader(body);
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
- }, _SessionEncrypted_startPingLoop = function _SessionEncrypted_startPingLoop() {
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 (0, _0_deps_js_1.delay)(Math.max(0, __classPrivateFieldGet(this, _SessionEncrypted_pingInterval, "f") - timeElapsed), { signal: controller.signal });
395
- if (!this.connected) {
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
- if (err instanceof DOMException && err.name == "AbortError") {
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
- }, _SessionEncrypted_sendPingDelayDisconnect = async function _SessionEncrypted_sendPingDelayDisconnect(disconnect_delay) {
419
- const ping_id = (0, _1_utilities_js_1.getRandomId)();
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;