@mtkruto/browser 0.119.0 → 0.119.1

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 (127) hide show
  1. package/esm/0_errors.d.ts.map +1 -1
  2. package/esm/0_errors.js +9 -31
  3. package/esm/3_errors.js +2 -12
  4. package/esm/4_errors.js +2 -12
  5. package/esm/_dnt.polyfills.d.ts +0 -99
  6. package/esm/_dnt.polyfills.d.ts.map +1 -1
  7. package/esm/_dnt.polyfills.js +1 -127
  8. package/esm/client/0_abortable_loop.js +26 -39
  9. package/esm/client/0_storage_operations.js +179 -218
  10. package/esm/client/1_client_plain.js +4 -22
  11. package/esm/client/2_account_manager.js +140 -149
  12. package/esm/client/2_bot_info_manager.js +26 -38
  13. package/esm/client/2_business_connection_manager.js +10 -23
  14. package/esm/client/2_client_encrypted.js +198 -215
  15. package/esm/client/2_file_manager.js +255 -262
  16. package/esm/client/2_network_statistics_manager.js +31 -44
  17. package/esm/client/2_payment_manager.js +7 -20
  18. package/esm/client/2_reaction_manager.js +7 -20
  19. package/esm/client/2_translations_manager.js +101 -111
  20. package/esm/client/2_update_manager.js +750 -745
  21. package/esm/client/3_client_encrypted_pool.js +10 -26
  22. package/esm/client/3_message_manager.js +503 -508
  23. package/esm/client/3_video_chat_manager.js +57 -68
  24. package/esm/client/4_callback_query_manager.js +18 -30
  25. package/esm/client/4_chat_list_manager.js +140 -146
  26. package/esm/client/4_chat_manager.js +161 -169
  27. package/esm/client/4_checklist_manager.js +26 -39
  28. package/esm/client/4_context.js +244 -259
  29. package/esm/client/4_forum_manager.js +67 -73
  30. package/esm/client/4_gift_manager.js +22 -35
  31. package/esm/client/4_inline_query_manager.js +16 -28
  32. package/esm/client/4_link_preview_manager.js +6 -19
  33. package/esm/client/4_poll_manager.js +44 -57
  34. package/esm/client/4_story_manager.js +41 -53
  35. package/esm/client/5_composer.js +13 -26
  36. package/esm/client/6_client.js +866 -896
  37. package/esm/client/6_client_dispatcher.js +308 -325
  38. package/esm/client/7_client_worker.js +16 -29
  39. package/esm/connection/1_connection_tcp.js +55 -82
  40. package/esm/connection/1_connection_web_socket.js +75 -91
  41. package/esm/deps/jsr.io/@roj/tgcrypto/1.0.1/dist/tgcrypto.js +3 -11
  42. package/esm/deps/jsr.io/@std/async/1.2.0/mux_async_iterator.js +31 -47
  43. package/esm/deps/jsr.io/@std/async/1.2.0/tee.js +11 -34
  44. package/esm/deps/jsr.io/@std/cache/0.2.2/lru_cache.js +30 -47
  45. package/esm/deps/jsr.io/@std/datetime/0.225.7/_date_time_formatter.js +4 -17
  46. package/esm/session/0_session_state.js +12 -38
  47. package/esm/session/1_session.js +49 -72
  48. package/esm/session/2_session_encrypted.js +422 -420
  49. package/esm/storage/2_storage_indexed_db.js +26 -44
  50. package/esm/storage/2_storage_local_storage.js +3 -16
  51. package/esm/storage/2_storage_memory.js +24 -41
  52. package/esm/storage/2_storage_session_storage.js +3 -16
  53. package/esm/tl/1_tl_reader.d.ts +1 -1
  54. package/esm/tl/1_tl_reader.d.ts.map +1 -1
  55. package/esm/tl/1_tl_reader.js +95 -103
  56. package/esm/tl/1_tl_writer.js +169 -178
  57. package/esm/transport/0_transport.js +1 -8
  58. package/esm/transport/1_transport_abridged.js +11 -24
  59. package/esm/transport/1_transport_intermediate.js +10 -23
  60. package/esm/utilities/0_mutex.js +4 -19
  61. package/esm/utilities/0_part_stream.js +11 -25
  62. package/esm/utilities/1_crypto.js +42 -53
  63. package/esm/utilities/2_queue.js +29 -47
  64. package/package.json +1 -1
  65. package/script/0_errors.d.ts.map +1 -1
  66. package/script/0_errors.js +9 -31
  67. package/script/3_errors.js +2 -12
  68. package/script/4_errors.js +2 -12
  69. package/script/_dnt.polyfills.d.ts +0 -99
  70. package/script/_dnt.polyfills.d.ts.map +1 -1
  71. package/script/_dnt.polyfills.js +0 -128
  72. package/script/client/0_abortable_loop.js +27 -40
  73. package/script/client/0_storage_operations.js +179 -218
  74. package/script/client/1_client_plain.js +4 -22
  75. package/script/client/2_account_manager.js +140 -149
  76. package/script/client/2_bot_info_manager.js +26 -38
  77. package/script/client/2_business_connection_manager.js +10 -23
  78. package/script/client/2_client_encrypted.js +199 -216
  79. package/script/client/2_file_manager.js +255 -262
  80. package/script/client/2_network_statistics_manager.js +32 -45
  81. package/script/client/2_payment_manager.js +7 -20
  82. package/script/client/2_reaction_manager.js +7 -20
  83. package/script/client/2_translations_manager.js +102 -112
  84. package/script/client/2_update_manager.js +750 -745
  85. package/script/client/3_client_encrypted_pool.js +10 -26
  86. package/script/client/3_message_manager.js +503 -508
  87. package/script/client/3_video_chat_manager.js +57 -68
  88. package/script/client/4_callback_query_manager.js +18 -30
  89. package/script/client/4_chat_list_manager.js +140 -146
  90. package/script/client/4_chat_manager.js +161 -169
  91. package/script/client/4_checklist_manager.js +26 -39
  92. package/script/client/4_context.js +244 -259
  93. package/script/client/4_forum_manager.js +67 -73
  94. package/script/client/4_gift_manager.js +22 -35
  95. package/script/client/4_inline_query_manager.js +16 -28
  96. package/script/client/4_link_preview_manager.js +6 -19
  97. package/script/client/4_poll_manager.js +44 -57
  98. package/script/client/4_story_manager.js +41 -53
  99. package/script/client/5_composer.js +13 -26
  100. package/script/client/6_client.js +866 -896
  101. package/script/client/6_client_dispatcher.js +308 -325
  102. package/script/client/7_client_worker.js +16 -29
  103. package/script/connection/1_connection_tcp.js +55 -82
  104. package/script/connection/1_connection_web_socket.js +75 -91
  105. package/script/deps/jsr.io/@roj/tgcrypto/1.0.1/dist/tgcrypto.js +3 -11
  106. package/script/deps/jsr.io/@std/async/1.2.0/mux_async_iterator.js +31 -47
  107. package/script/deps/jsr.io/@std/async/1.2.0/tee.js +11 -34
  108. package/script/deps/jsr.io/@std/cache/0.2.2/lru_cache.js +30 -47
  109. package/script/deps/jsr.io/@std/datetime/0.225.7/_date_time_formatter.js +4 -17
  110. package/script/session/0_session_state.js +12 -38
  111. package/script/session/1_session.js +49 -72
  112. package/script/session/2_session_encrypted.js +423 -421
  113. package/script/storage/2_storage_indexed_db.js +26 -44
  114. package/script/storage/2_storage_local_storage.js +3 -16
  115. package/script/storage/2_storage_memory.js +24 -41
  116. package/script/storage/2_storage_session_storage.js +3 -16
  117. package/script/tl/1_tl_reader.d.ts +1 -1
  118. package/script/tl/1_tl_reader.d.ts.map +1 -1
  119. package/script/tl/1_tl_reader.js +96 -104
  120. package/script/tl/1_tl_writer.js +170 -179
  121. package/script/transport/0_transport.js +1 -8
  122. package/script/transport/1_transport_abridged.js +11 -24
  123. package/script/transport/1_transport_intermediate.js +10 -23
  124. package/script/utilities/0_mutex.js +4 -19
  125. package/script/utilities/0_part_stream.js +11 -25
  126. package/script/utilities/1_crypto.js +43 -54
  127. package/script/utilities/2_queue.js +30 -48
@@ -18,18 +18,7 @@
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 _ClientEncrypted_instances, _a, _ClientEncrypted_SEND_MAX_TRIES, _ClientEncrypted_AUTH_KEY_CREATION_MAX_TRIES, _ClientEncrypted_L, _ClientEncrypted_plain, _ClientEncrypted_sentRequests, _ClientEncrypted_apiId, _ClientEncrypted_appVersion, _ClientEncrypted_deviceModel, _ClientEncrypted_langCode, _ClientEncrypted_langPack, _ClientEncrypted_systemLangCode, _ClientEncrypted_systemVersion, _ClientEncrypted_disableUpdates, _ClientEncrypted_createAuthKeyPromise, _ClientEncrypted_createAuthKey, _ClientEncrypted_createAuthKeyInner, _ClientEncrypted_connectionInited, _ClientEncrypted_send, _ClientEncrypted_resend, _ClientEncrypted_onUpdate, _ClientEncrypted_onNewServerSalt, _ClientEncrypted_onMessageFailed, _ClientEncrypted_onRpcError, _ClientEncrypted_onRpcResult, _ClientEncrypted_onPong;
21
+ var _a;
33
22
  Object.defineProperty(exports, "__esModule", { value: true });
34
23
  exports.ClientEncrypted = void 0;
35
24
  const _0_errors_js_1 = require("../0_errors.js");
@@ -45,61 +34,44 @@ const _1_client_plain_js_1 = require("./1_client_plain.js");
45
34
  // global ClientEncrypted ID counter for logs
46
35
  let id = 0;
47
36
  class ClientEncrypted extends _0_client_abstract_js_1.ClientAbstract {
37
+ static #SEND_MAX_TRIES = 10;
38
+ static #AUTH_KEY_CREATION_MAX_TRIES = 10;
39
+ handlers = {};
40
+ #L;
41
+ #plain;
42
+ session;
43
+ #sentRequests = new Map();
44
+ #apiId;
45
+ #appVersion;
46
+ #deviceModel;
47
+ #langCode;
48
+ #langPack;
49
+ #systemLangCode;
50
+ #systemVersion;
51
+ #disableUpdates;
48
52
  constructor(dc, apiId, params) {
49
53
  super();
50
- _ClientEncrypted_instances.add(this);
51
- Object.defineProperty(this, "handlers", {
52
- enumerable: true,
53
- configurable: true,
54
- writable: true,
55
- value: {}
56
- });
57
- _ClientEncrypted_L.set(this, void 0);
58
- _ClientEncrypted_plain.set(this, void 0);
59
- Object.defineProperty(this, "session", {
60
- enumerable: true,
61
- configurable: true,
62
- writable: true,
63
- value: void 0
64
- });
65
- _ClientEncrypted_sentRequests.set(this, new Map());
66
- _ClientEncrypted_apiId.set(this, void 0);
67
- _ClientEncrypted_appVersion.set(this, void 0);
68
- _ClientEncrypted_deviceModel.set(this, void 0);
69
- _ClientEncrypted_langCode.set(this, void 0);
70
- _ClientEncrypted_langPack.set(this, void 0);
71
- _ClientEncrypted_systemLangCode.set(this, void 0);
72
- _ClientEncrypted_systemVersion.set(this, void 0);
73
- _ClientEncrypted_disableUpdates.set(this, void 0);
74
- _ClientEncrypted_createAuthKeyPromise.set(this, void 0);
75
- _ClientEncrypted_connectionInited.set(this, false);
76
- Object.defineProperty(this, "lastRequest", {
77
- enumerable: true,
78
- configurable: true,
79
- writable: true,
80
- value: void 0
81
- });
82
- __classPrivateFieldSet(this, _ClientEncrypted_L, (0, _1_utilities_js_1.getLogger)("ClientEncrypted").client(id++), "f");
83
- __classPrivateFieldSet(this, _ClientEncrypted_plain, new _1_client_plain_js_1.ClientPlain(dc, params), "f");
54
+ this.#L = (0, _1_utilities_js_1.getLogger)("ClientEncrypted").client(id++);
55
+ this.#plain = new _1_client_plain_js_1.ClientPlain(dc, params);
84
56
  this.session = new _4_session_js_1.SessionEncrypted(dc, params);
85
- this.session.handlers.onUpdate = __classPrivateFieldGet(this, _ClientEncrypted_instances, "m", _ClientEncrypted_onUpdate).bind(this);
86
- this.session.handlers.onNewServerSalt = __classPrivateFieldGet(this, _ClientEncrypted_instances, "m", _ClientEncrypted_onNewServerSalt).bind(this);
87
- this.session.handlers.onMessageFailed = __classPrivateFieldGet(this, _ClientEncrypted_instances, "m", _ClientEncrypted_onMessageFailed).bind(this);
88
- this.session.handlers.onRpcError = __classPrivateFieldGet(this, _ClientEncrypted_instances, "m", _ClientEncrypted_onRpcError).bind(this);
89
- this.session.handlers.onRpcResult = __classPrivateFieldGet(this, _ClientEncrypted_instances, "m", _ClientEncrypted_onRpcResult).bind(this);
90
- this.session.handlers.onPong = __classPrivateFieldGet(this, _ClientEncrypted_instances, "m", _ClientEncrypted_onPong).bind(this);
91
- __classPrivateFieldSet(this, _ClientEncrypted_apiId, apiId, "f");
92
- __classPrivateFieldSet(this, _ClientEncrypted_appVersion, params?.appVersion ?? _4_constants_js_1.APP_VERSION, "f");
93
- __classPrivateFieldSet(this, _ClientEncrypted_deviceModel, params?.deviceModel ?? _4_constants_js_1.DEVICE_MODEL, "f");
94
- __classPrivateFieldSet(this, _ClientEncrypted_langCode, params?.langCode ?? _4_constants_js_1.LANG_CODE, "f");
95
- __classPrivateFieldSet(this, _ClientEncrypted_langPack, params?.langPack ?? _4_constants_js_1.LANG_PACK, "f");
96
- __classPrivateFieldSet(this, _ClientEncrypted_systemLangCode, params?.systemLangCode ?? _4_constants_js_1.SYSTEM_LANG_CODE, "f");
97
- __classPrivateFieldSet(this, _ClientEncrypted_systemVersion, params?.systemVersion ?? _4_constants_js_1.SYSTEM_VERSION, "f");
98
- __classPrivateFieldSet(this, _ClientEncrypted_disableUpdates, params?.disableUpdates ?? false, "f");
57
+ this.session.handlers.onUpdate = this.#onUpdate.bind(this);
58
+ this.session.handlers.onNewServerSalt = this.#onNewServerSalt.bind(this);
59
+ this.session.handlers.onMessageFailed = this.#onMessageFailed.bind(this);
60
+ this.session.handlers.onRpcError = this.#onRpcError.bind(this);
61
+ this.session.handlers.onRpcResult = this.#onRpcResult.bind(this);
62
+ this.session.handlers.onPong = this.#onPong.bind(this);
63
+ this.#apiId = apiId;
64
+ this.#appVersion = params?.appVersion ?? _4_constants_js_1.APP_VERSION;
65
+ this.#deviceModel = params?.deviceModel ?? _4_constants_js_1.DEVICE_MODEL;
66
+ this.#langCode = params?.langCode ?? _4_constants_js_1.LANG_CODE;
67
+ this.#langPack = params?.langPack ?? _4_constants_js_1.LANG_PACK;
68
+ this.#systemLangCode = params?.systemLangCode ?? _4_constants_js_1.SYSTEM_LANG_CODE;
69
+ this.#systemVersion = params?.systemVersion ?? _4_constants_js_1.SYSTEM_VERSION;
70
+ this.#disableUpdates = params?.disableUpdates ?? false;
99
71
  }
100
72
  async connect() {
101
73
  if (!this.authKey.length) {
102
- await __classPrivateFieldGet(this, _ClientEncrypted_instances, "m", _ClientEncrypted_createAuthKey).call(this);
74
+ await this.#createAuthKey();
103
75
  }
104
76
  await super.connect();
105
77
  }
@@ -107,188 +79,199 @@ class ClientEncrypted extends _0_client_abstract_js_1.ClientAbstract {
107
79
  super.disconnect();
108
80
  this.lastRequest = undefined;
109
81
  }
110
- get authKey() {
111
- return this.session.authKey;
112
- }
113
- async setAuthKey(authKey) {
114
- await this.session.setAuthKey(authKey);
115
- }
116
- async invoke(function_) {
117
- const messageId = await __classPrivateFieldGet(this, _ClientEncrypted_instances, "m", _ClientEncrypted_send).call(this, function_);
118
- __classPrivateFieldGet(this, _ClientEncrypted_L, "f").debug("sent", function_._, "with msg_id", messageId);
119
- const sentRequest = { call: function_, promiseWithResolvers: Promise.withResolvers() };
120
- __classPrivateFieldGet(this, _ClientEncrypted_sentRequests, "f").set(messageId, sentRequest);
121
- return await sentRequest.promiseWithResolvers.promise;
82
+ #createAuthKeyPromise;
83
+ #createAuthKey() {
84
+ return this.#createAuthKeyPromise ??= this.#createAuthKeyInner().finally(() => {
85
+ this.#createAuthKeyPromise = undefined;
86
+ });
122
87
  }
123
- }
124
- exports.ClientEncrypted = ClientEncrypted;
125
- _a = ClientEncrypted, _ClientEncrypted_L = new WeakMap(), _ClientEncrypted_plain = new WeakMap(), _ClientEncrypted_sentRequests = new WeakMap(), _ClientEncrypted_apiId = new WeakMap(), _ClientEncrypted_appVersion = new WeakMap(), _ClientEncrypted_deviceModel = new WeakMap(), _ClientEncrypted_langCode = new WeakMap(), _ClientEncrypted_langPack = new WeakMap(), _ClientEncrypted_systemLangCode = new WeakMap(), _ClientEncrypted_systemVersion = new WeakMap(), _ClientEncrypted_disableUpdates = new WeakMap(), _ClientEncrypted_createAuthKeyPromise = new WeakMap(), _ClientEncrypted_connectionInited = new WeakMap(), _ClientEncrypted_instances = new WeakSet(), _ClientEncrypted_createAuthKey = function _ClientEncrypted_createAuthKey() {
126
- return __classPrivateFieldSet(this, _ClientEncrypted_createAuthKeyPromise, __classPrivateFieldGet(this, _ClientEncrypted_createAuthKeyPromise, "f") ?? __classPrivateFieldGet(this, _ClientEncrypted_instances, "m", _ClientEncrypted_createAuthKeyInner).call(this).finally(() => {
127
- __classPrivateFieldSet(this, _ClientEncrypted_createAuthKeyPromise, undefined, "f");
128
- }), "f");
129
- }, _ClientEncrypted_createAuthKeyInner = async function _ClientEncrypted_createAuthKeyInner() {
130
- let lastErr;
131
- let errored = false;
132
- for (let i = 0; i < __classPrivateFieldGet(_a, _a, "f", _ClientEncrypted_AUTH_KEY_CREATION_MAX_TRIES); ++i) {
133
- try {
134
- await __classPrivateFieldGet(this, _ClientEncrypted_plain, "f").connect();
135
- const [authKey, serverSalt] = await __classPrivateFieldGet(this, _ClientEncrypted_plain, "f").createAuthKey();
136
- await this.setAuthKey(authKey);
137
- this.serverSalt = serverSalt;
138
- errored = false;
139
- break;
140
- }
141
- catch (err) {
142
- errored = true;
143
- lastErr = err;
144
- if (this.isDisconnected) {
88
+ async #createAuthKeyInner() {
89
+ let lastErr;
90
+ let errored = false;
91
+ for (let i = 0; i < _a.#AUTH_KEY_CREATION_MAX_TRIES; ++i) {
92
+ try {
93
+ await this.#plain.connect();
94
+ const [authKey, serverSalt] = await this.#plain.createAuthKey();
95
+ await this.setAuthKey(authKey);
96
+ this.serverSalt = serverSalt;
97
+ errored = false;
145
98
  break;
146
99
  }
147
- __classPrivateFieldGet(this, _ClientEncrypted_L, "f").error("failed to create auth key:", err);
100
+ catch (err) {
101
+ errored = true;
102
+ lastErr = err;
103
+ if (this.isDisconnected) {
104
+ break;
105
+ }
106
+ this.#L.error("failed to create auth key:", err);
107
+ }
108
+ finally {
109
+ this.#plain.disconnect();
110
+ }
148
111
  }
149
- finally {
150
- __classPrivateFieldGet(this, _ClientEncrypted_plain, "f").disconnect();
112
+ if (errored) {
113
+ throw lastErr;
151
114
  }
152
115
  }
153
- if (errored) {
154
- throw lastErr;
116
+ get authKey() {
117
+ return this.session.authKey;
155
118
  }
156
- }, _ClientEncrypted_send = async function _ClientEncrypted_send(function_) {
157
- this.lastRequest = new Date();
158
- let body;
159
- if (_2_tl_js_1.Mtproto.is("ping", function_)) {
160
- body = _2_tl_js_1.Mtproto.serializeObject(function_);
119
+ async setAuthKey(authKey) {
120
+ await this.session.setAuthKey(authKey);
161
121
  }
162
- else {
163
- if (__classPrivateFieldGet(this, _ClientEncrypted_disableUpdates, "f") && !(0, _0_utilities_js_1.isCdnFunction)(function_)) {
164
- function_ = { _: "invokeWithoutUpdates", query: function_ };
122
+ #connectionInited = false;
123
+ lastRequest;
124
+ async #send(function_) {
125
+ this.lastRequest = new Date();
126
+ let body;
127
+ if (_2_tl_js_1.Mtproto.is("ping", function_)) {
128
+ body = _2_tl_js_1.Mtproto.serializeObject(function_);
165
129
  }
166
- if (!__classPrivateFieldGet(this, _ClientEncrypted_connectionInited, "f")) {
167
- if (!__classPrivateFieldGet(this, _ClientEncrypted_apiId, "f")) {
168
- throw new _0_errors_js_1.InputError("apiId not set");
130
+ else {
131
+ if (this.#disableUpdates && !(0, _0_utilities_js_1.isCdnFunction)(function_)) {
132
+ function_ = { _: "invokeWithoutUpdates", query: function_ };
169
133
  }
170
- function_ = {
171
- _: "initConnection",
172
- api_id: __classPrivateFieldGet(this, _ClientEncrypted_apiId, "f"),
173
- app_version: __classPrivateFieldGet(this, _ClientEncrypted_appVersion, "f"),
174
- device_model: __classPrivateFieldGet(this, _ClientEncrypted_deviceModel, "f"),
175
- lang_code: __classPrivateFieldGet(this, _ClientEncrypted_langCode, "f"),
176
- lang_pack: __classPrivateFieldGet(this, _ClientEncrypted_langPack, "f"),
177
- query: {
178
- _: "invokeWithLayer",
179
- layer: _2_tl_js_1.Api.LAYER,
180
- query: function_,
181
- },
182
- system_lang_code: __classPrivateFieldGet(this, _ClientEncrypted_systemLangCode, "f"),
183
- system_version: __classPrivateFieldGet(this, _ClientEncrypted_systemVersion, "f"),
184
- };
185
- }
186
- body = _2_tl_js_1.Api.serializeObject(function_);
187
- }
188
- let lastErr;
189
- for (let i = 0; i < __classPrivateFieldGet(_a, _a, "f", _ClientEncrypted_SEND_MAX_TRIES); ++i) {
190
- let errored = false;
191
- try {
192
- return await this.session.send(body);
193
- }
194
- catch (err) {
195
- errored = true;
196
- lastErr = err;
197
- if (this.isDisconnected) {
198
- break;
134
+ if (!this.#connectionInited) {
135
+ if (!this.#apiId) {
136
+ throw new _0_errors_js_1.InputError("apiId not set");
137
+ }
138
+ function_ = {
139
+ _: "initConnection",
140
+ api_id: this.#apiId,
141
+ app_version: this.#appVersion,
142
+ device_model: this.#deviceModel,
143
+ lang_code: this.#langCode,
144
+ lang_pack: this.#langPack,
145
+ query: {
146
+ _: "invokeWithLayer",
147
+ layer: _2_tl_js_1.Api.LAYER,
148
+ query: function_,
149
+ },
150
+ system_lang_code: this.#systemLangCode,
151
+ system_version: this.#systemVersion,
152
+ };
199
153
  }
200
- __classPrivateFieldGet(this, _ClientEncrypted_L, "f").error("send failed:", err);
154
+ body = _2_tl_js_1.Api.serializeObject(function_);
201
155
  }
202
- finally {
203
- if (!errored) {
204
- __classPrivateFieldGet(this, _ClientEncrypted_L, "f").debug("invoked", (0, _0_utilities_js_1.repr)(function_));
205
- __classPrivateFieldGet(this, _ClientEncrypted_L, "f").out(function_);
156
+ let lastErr;
157
+ for (let i = 0; i < _a.#SEND_MAX_TRIES; ++i) {
158
+ let errored = false;
159
+ try {
160
+ return await this.session.send(body);
161
+ }
162
+ catch (err) {
163
+ errored = true;
164
+ lastErr = err;
165
+ if (this.isDisconnected) {
166
+ break;
167
+ }
168
+ this.#L.error("send failed:", err);
169
+ }
170
+ finally {
171
+ if (!errored) {
172
+ this.#L.debug("invoked", (0, _0_utilities_js_1.repr)(function_));
173
+ this.#L.out(function_);
174
+ }
206
175
  }
207
176
  }
177
+ throw new Error(`Failed to invoke function after ${_a.#SEND_MAX_TRIES} tries.`, { cause: lastErr });
208
178
  }
209
- throw new Error(`Failed to invoke function after ${__classPrivateFieldGet(_a, _a, "f", _ClientEncrypted_SEND_MAX_TRIES)} tries.`, { cause: lastErr });
210
- }, _ClientEncrypted_resend = async function _ClientEncrypted_resend(request) {
211
- try {
212
- const messageId = await __classPrivateFieldGet(this, _ClientEncrypted_instances, "m", _ClientEncrypted_send).call(this, request.call);
213
- __classPrivateFieldGet(this, _ClientEncrypted_sentRequests, "f").set(messageId, request);
214
- }
215
- catch (err) {
216
- __classPrivateFieldGet(this, _ClientEncrypted_L, "f").error("rejecting message because of resend error:", err);
217
- request.promiseWithResolvers.reject(err);
218
- }
219
- }, _ClientEncrypted_onUpdate = async function _ClientEncrypted_onUpdate(body) {
220
- let type;
221
- try {
222
- type = await _2_tl_js_1.Api.deserializeType(_2_tl_js_1.X, body);
223
- }
224
- catch (err) {
225
- __classPrivateFieldGet(this, _ClientEncrypted_L, "f").error("failed to deserialize update:", err);
226
- this.handlers.onDeserializationError?.();
227
- return;
228
- }
229
- if (_2_tl_js_1.Api.isOfEnum("Update", type) || _2_tl_js_1.Api.isOfEnum("Updates", type)) {
230
- this.handlers.onUpdate?.(type);
231
- }
232
- else {
233
- __classPrivateFieldGet(this, _ClientEncrypted_L, "f").warning("received unknown type:", (0, _0_utilities_js_1.repr)(type));
234
- }
235
- }, _ClientEncrypted_onNewServerSalt = function _ClientEncrypted_onNewServerSalt(serverSalt) {
236
- this.handlers.onNewServerSalt?.(serverSalt);
237
- }, _ClientEncrypted_onMessageFailed = async function _ClientEncrypted_onMessageFailed(msgId, error) {
238
- const request = __classPrivateFieldGet(this, _ClientEncrypted_sentRequests, "f").get(msgId);
239
- if (request) {
240
- __classPrivateFieldGet(this, _ClientEncrypted_sentRequests, "f").delete(msgId);
241
- if (error instanceof _4_session_js_1.SessionError) {
242
- await __classPrivateFieldGet(this, _ClientEncrypted_instances, "m", _ClientEncrypted_resend).call(this, request);
179
+ async #resend(request) {
180
+ try {
181
+ const messageId = await this.#send(request.call);
182
+ this.#sentRequests.set(messageId, request);
243
183
  }
244
- else {
245
- request.promiseWithResolvers.reject(error);
184
+ catch (err) {
185
+ this.#L.error("rejecting message because of resend error:", err);
186
+ request.promiseWithResolvers.reject(err);
246
187
  }
247
188
  }
248
- }, _ClientEncrypted_onRpcError = async function _ClientEncrypted_onRpcError(msgId, error) {
249
- const request = __classPrivateFieldGet(this, _ClientEncrypted_sentRequests, "f").get(msgId);
250
- __classPrivateFieldGet(this, _ClientEncrypted_L, "f").debug("received rpc_error with req_msg_id =", msgId, "for", request === undefined ? "unknown" : "known", "request");
251
- if (request) {
252
- __classPrivateFieldGet(this, _ClientEncrypted_sentRequests, "f").delete(msgId);
253
- const reason = (0, _4_errors_js_1.constructTelegramError)(error, request.call);
254
- if (reason instanceof _3_errors_js_1.ConnectionNotInited) {
255
- __classPrivateFieldSet(this, _ClientEncrypted_connectionInited, false, "f");
256
- await __classPrivateFieldGet(this, _ClientEncrypted_instances, "m", _ClientEncrypted_resend).call(this, request);
257
- }
258
- else {
259
- request.promiseWithResolvers.reject((0, _4_errors_js_1.constructTelegramError)(error, request.call));
260
- }
189
+ async invoke(function_) {
190
+ const messageId = await this.#send(function_);
191
+ this.#L.debug("sent", function_._, "with msg_id", messageId);
192
+ const sentRequest = { call: function_, promiseWithResolvers: Promise.withResolvers() };
193
+ this.#sentRequests.set(messageId, sentRequest);
194
+ return await sentRequest.promiseWithResolvers.promise;
261
195
  }
262
- }, _ClientEncrypted_onRpcResult = async function _ClientEncrypted_onRpcResult(msgId, body) {
263
- const sentRequest = __classPrivateFieldGet(this, _ClientEncrypted_sentRequests, "f").get(msgId);
264
- __classPrivateFieldGet(this, _ClientEncrypted_L, "f").debug("received rpc_result with req_msg_id =", msgId, "for", sentRequest === undefined ? "unknown" : "known", "request");
265
- if (sentRequest) {
196
+ async #onUpdate(body) {
266
197
  let type;
267
198
  try {
268
- type = await _2_tl_js_1.Api.deserializeType(_2_tl_js_1.Api.mustGetReturnType(sentRequest.call._), body);
269
- __classPrivateFieldGet(this, _ClientEncrypted_L, "f").in(type);
270
- __classPrivateFieldGet(this, _ClientEncrypted_L, "f").debug("received rpc_result", (0, _0_utilities_js_1.repr)(type));
271
- sentRequest.promiseWithResolvers.resolve(type);
199
+ type = await _2_tl_js_1.Api.deserializeType(_2_tl_js_1.X, body);
272
200
  }
273
201
  catch (err) {
274
- sentRequest.promiseWithResolvers.reject(err);
275
- __classPrivateFieldGet(this, _ClientEncrypted_L, "f").error("failed to deserialize rpc_result body:", err);
202
+ this.#L.error("failed to deserialize update:", err);
276
203
  this.handlers.onDeserializationError?.();
277
204
  return;
278
205
  }
279
- finally {
280
- __classPrivateFieldGet(this, _ClientEncrypted_sentRequests, "f").delete(msgId);
206
+ if (_2_tl_js_1.Api.isOfEnum("Update", type) || _2_tl_js_1.Api.isOfEnum("Updates", type)) {
207
+ this.handlers.onUpdate?.(type);
208
+ }
209
+ else {
210
+ this.#L.warning("received unknown type:", (0, _0_utilities_js_1.repr)(type));
211
+ }
212
+ }
213
+ #onNewServerSalt(serverSalt) {
214
+ this.handlers.onNewServerSalt?.(serverSalt);
215
+ }
216
+ async #onMessageFailed(msgId, error) {
217
+ const request = this.#sentRequests.get(msgId);
218
+ if (request) {
219
+ this.#sentRequests.delete(msgId);
220
+ if (error instanceof _4_session_js_1.SessionError) {
221
+ await this.#resend(request);
222
+ }
223
+ else {
224
+ request.promiseWithResolvers.reject(error);
225
+ }
281
226
  }
282
227
  }
283
- if (!__classPrivateFieldGet(this, _ClientEncrypted_connectionInited, "f")) {
284
- __classPrivateFieldSet(this, _ClientEncrypted_connectionInited, true, "f");
228
+ async #onRpcError(msgId, error) {
229
+ const request = this.#sentRequests.get(msgId);
230
+ this.#L.debug("received rpc_error with req_msg_id =", msgId, "for", request === undefined ? "unknown" : "known", "request");
231
+ if (request) {
232
+ this.#sentRequests.delete(msgId);
233
+ const reason = (0, _4_errors_js_1.constructTelegramError)(error, request.call);
234
+ if (reason instanceof _3_errors_js_1.ConnectionNotInited) {
235
+ this.#connectionInited = false;
236
+ await this.#resend(request);
237
+ }
238
+ else {
239
+ request.promiseWithResolvers.reject((0, _4_errors_js_1.constructTelegramError)(error, request.call));
240
+ }
241
+ }
285
242
  }
286
- }, _ClientEncrypted_onPong = function _ClientEncrypted_onPong(pong) {
287
- const sentRequest = __classPrivateFieldGet(this, _ClientEncrypted_sentRequests, "f").get(pong.msg_id);
288
- if (sentRequest) {
289
- sentRequest.promiseWithResolvers.resolve(pong);
290
- __classPrivateFieldGet(this, _ClientEncrypted_sentRequests, "f").delete(pong.msg_id);
243
+ async #onRpcResult(msgId, body) {
244
+ const sentRequest = this.#sentRequests.get(msgId);
245
+ this.#L.debug("received rpc_result with req_msg_id =", msgId, "for", sentRequest === undefined ? "unknown" : "known", "request");
246
+ if (sentRequest) {
247
+ let type;
248
+ try {
249
+ type = await _2_tl_js_1.Api.deserializeType(_2_tl_js_1.Api.mustGetReturnType(sentRequest.call._), body);
250
+ this.#L.in(type);
251
+ this.#L.debug("received rpc_result", (0, _0_utilities_js_1.repr)(type));
252
+ sentRequest.promiseWithResolvers.resolve(type);
253
+ }
254
+ catch (err) {
255
+ sentRequest.promiseWithResolvers.reject(err);
256
+ this.#L.error("failed to deserialize rpc_result body:", err);
257
+ this.handlers.onDeserializationError?.();
258
+ return;
259
+ }
260
+ finally {
261
+ this.#sentRequests.delete(msgId);
262
+ }
263
+ }
264
+ if (!this.#connectionInited) {
265
+ this.#connectionInited = true;
266
+ }
291
267
  }
292
- };
293
- _ClientEncrypted_SEND_MAX_TRIES = { value: 10 };
294
- _ClientEncrypted_AUTH_KEY_CREATION_MAX_TRIES = { value: 10 };
268
+ #onPong(pong) {
269
+ const sentRequest = this.#sentRequests.get(pong.msg_id);
270
+ if (sentRequest) {
271
+ sentRequest.promiseWithResolvers.resolve(pong);
272
+ this.#sentRequests.delete(pong.msg_id);
273
+ }
274
+ }
275
+ }
276
+ exports.ClientEncrypted = ClientEncrypted;
277
+ _a = ClientEncrypted;