@mtkruto/browser 0.119.0 → 0.120.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/README.md +1 -1
- package/esm/0_errors.d.ts.map +1 -1
- package/esm/0_errors.js +9 -31
- package/esm/3_errors.js +2 -12
- package/esm/4_errors.js +2 -12
- package/esm/_dnt.polyfills.d.ts +0 -99
- package/esm/_dnt.polyfills.d.ts.map +1 -1
- package/esm/_dnt.polyfills.js +1 -127
- package/esm/client/0_abortable_loop.js +26 -39
- package/esm/client/0_storage_operations.js +179 -218
- package/esm/client/1_client_plain.js +4 -22
- package/esm/client/2_account_manager.js +140 -149
- 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 +198 -215
- package/esm/client/2_file_manager.js +255 -262
- package/esm/client/2_network_statistics_manager.js +31 -44
- 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 +101 -111
- package/esm/client/2_update_manager.js +750 -745
- package/esm/client/3_client_encrypted_pool.js +10 -26
- package/esm/client/3_message_manager.js +503 -508
- 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 +140 -146
- package/esm/client/4_chat_manager.js +161 -169
- package/esm/client/4_checklist_manager.js +26 -39
- package/esm/client/4_context.js +244 -259
- package/esm/client/4_forum_manager.js +67 -73
- package/esm/client/4_gift_manager.js +22 -35
- 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_composer.js +13 -26
- package/esm/client/6_client.js +866 -896
- package/esm/client/6_client_dispatcher.js +308 -325
- package/esm/client/7_client_worker.js +16 -29
- package/esm/connection/1_connection_tcp.js +55 -82
- 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.2.0/mux_async_iterator.js +31 -47
- package/esm/deps/jsr.io/@std/async/1.2.0/tee.js +11 -34
- package/esm/deps/jsr.io/@std/cache/0.2.2/lru_cache.js +30 -47
- package/esm/deps/jsr.io/@std/datetime/0.225.7/_date_time_formatter.js +4 -17
- package/esm/session/0_session_state.js +12 -38
- package/esm/session/1_session.js +49 -72
- package/esm/session/2_session_encrypted.js +422 -420
- package/esm/storage/2_storage_indexed_db.js +26 -44
- package/esm/storage/2_storage_local_storage.js +3 -16
- package/esm/storage/2_storage_memory.js +24 -41
- package/esm/storage/2_storage_session_storage.js +3 -16
- package/esm/tl/1_tl_reader.d.ts +1 -1
- package/esm/tl/1_tl_reader.d.ts.map +1 -1
- package/esm/tl/1_tl_reader.js +95 -103
- package/esm/tl/1_tl_writer.js +169 -178
- 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/utilities/0_mutex.js +4 -19
- package/esm/utilities/0_part_stream.js +11 -25
- package/esm/utilities/1_crypto.js +42 -53
- package/esm/utilities/2_queue.js +29 -47
- package/package.json +1 -1
- package/script/0_errors.d.ts.map +1 -1
- package/script/0_errors.js +9 -31
- package/script/3_errors.js +2 -12
- package/script/4_errors.js +2 -12
- package/script/_dnt.polyfills.d.ts +0 -99
- package/script/_dnt.polyfills.d.ts.map +1 -1
- package/script/_dnt.polyfills.js +0 -128
- package/script/client/0_abortable_loop.js +27 -40
- package/script/client/0_storage_operations.js +179 -218
- package/script/client/1_client_plain.js +4 -22
- package/script/client/2_account_manager.js +140 -149
- 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 +199 -216
- package/script/client/2_file_manager.js +255 -262
- package/script/client/2_network_statistics_manager.js +32 -45
- 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 +102 -112
- package/script/client/2_update_manager.js +750 -745
- package/script/client/3_client_encrypted_pool.js +10 -26
- package/script/client/3_message_manager.js +503 -508
- 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 +140 -146
- package/script/client/4_chat_manager.js +161 -169
- package/script/client/4_checklist_manager.js +26 -39
- package/script/client/4_context.js +244 -259
- package/script/client/4_forum_manager.js +67 -73
- package/script/client/4_gift_manager.js +22 -35
- 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_composer.js +13 -26
- package/script/client/6_client.js +866 -896
- package/script/client/6_client_dispatcher.js +308 -325
- package/script/client/7_client_worker.js +16 -29
- package/script/connection/1_connection_tcp.js +55 -82
- package/script/connection/1_connection_web_socket.js +75 -91
- package/script/deps/jsr.io/@roj/tgcrypto/1.0.1/dist/tgcrypto.js +3 -11
- package/script/deps/jsr.io/@std/async/1.2.0/mux_async_iterator.js +31 -47
- package/script/deps/jsr.io/@std/async/1.2.0/tee.js +11 -34
- package/script/deps/jsr.io/@std/cache/0.2.2/lru_cache.js +30 -47
- package/script/deps/jsr.io/@std/datetime/0.225.7/_date_time_formatter.js +4 -17
- package/script/session/0_session_state.js +12 -38
- package/script/session/1_session.js +49 -72
- package/script/session/2_session_encrypted.js +423 -421
- package/script/storage/2_storage_indexed_db.js +26 -44
- package/script/storage/2_storage_local_storage.js +3 -16
- package/script/storage/2_storage_memory.js +24 -41
- package/script/storage/2_storage_session_storage.js +3 -16
- package/script/tl/1_tl_reader.d.ts +1 -1
- package/script/tl/1_tl_reader.d.ts.map +1 -1
- package/script/tl/1_tl_reader.js +96 -104
- package/script/tl/1_tl_writer.js +170 -179
- 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/utilities/0_mutex.js +4 -19
- package/script/utilities/0_part_stream.js +11 -25
- package/script/utilities/1_crypto.js +43 -54
- package/script/utilities/2_queue.js +30 -48
|
@@ -17,18 +17,6 @@
|
|
|
17
17
|
* You should have received a copy of the GNU Lesser General Public License
|
|
18
18
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
19
19
|
*/
|
|
20
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
21
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
22
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
23
|
-
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");
|
|
24
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
25
|
-
};
|
|
26
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
27
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
28
|
-
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");
|
|
29
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
30
|
-
};
|
|
31
|
-
var _BotInfoManager_instances, _BotInfoManager_c, _BotInfoManager_setMyInfo, _BotInfoManager_getMyInfo;
|
|
32
20
|
import { Api } from "../2_tl.js";
|
|
33
21
|
import { botCommandScopeToTlObject } from "../3_types.js";
|
|
34
22
|
import { peerToChatId } from "../tl/2_telegram.js";
|
|
@@ -36,54 +24,59 @@ const botInfoManagerUpdates = [
|
|
|
36
24
|
"updateBotCommands",
|
|
37
25
|
];
|
|
38
26
|
export class BotInfoManager {
|
|
27
|
+
#c;
|
|
39
28
|
constructor(c) {
|
|
40
|
-
|
|
41
|
-
_BotInfoManager_c.set(this, void 0);
|
|
42
|
-
__classPrivateFieldSet(this, _BotInfoManager_c, c, "f");
|
|
29
|
+
this.#c = c;
|
|
43
30
|
}
|
|
44
31
|
canHandleUpdate(update) {
|
|
45
32
|
return Api.isOneOf(botInfoManagerUpdates, update);
|
|
46
33
|
}
|
|
34
|
+
async #setMyInfo(info) {
|
|
35
|
+
await this.#c.invoke({ _: "bots.setBotInfo", ...info });
|
|
36
|
+
}
|
|
47
37
|
async setMyDescription(params) {
|
|
48
|
-
|
|
49
|
-
await
|
|
38
|
+
this.#c.storage.assertBot("setMyDescription");
|
|
39
|
+
await this.#setMyInfo({ description: params?.description, lang_code: params?.languageCode ?? "" });
|
|
50
40
|
}
|
|
51
41
|
async setMyName(params) {
|
|
52
|
-
|
|
53
|
-
await
|
|
42
|
+
this.#c.storage.assertBot("setMyName");
|
|
43
|
+
await this.#setMyInfo({ name: params?.name, lang_code: params?.languageCode ?? "" });
|
|
54
44
|
}
|
|
55
45
|
async setMyShortDescription(params) {
|
|
56
|
-
|
|
57
|
-
await
|
|
46
|
+
this.#c.storage.assertBot("setMyShortDescription");
|
|
47
|
+
await this.#setMyInfo({ about: params?.shortDescription, lang_code: params?.languageCode ?? "" });
|
|
48
|
+
}
|
|
49
|
+
#getMyInfo(languageCode) {
|
|
50
|
+
return this.#c.invoke({ _: "bots.getBotInfo", lang_code: languageCode ?? "" });
|
|
58
51
|
}
|
|
59
52
|
async getMyDescription(params) {
|
|
60
|
-
|
|
61
|
-
return (await
|
|
53
|
+
this.#c.storage.assertBot("getMyDescription");
|
|
54
|
+
return (await this.#getMyInfo(params?.languageCode)).description;
|
|
62
55
|
}
|
|
63
56
|
async getMyName(params) {
|
|
64
|
-
|
|
65
|
-
return (await
|
|
57
|
+
this.#c.storage.assertBot("getMyName");
|
|
58
|
+
return (await this.#getMyInfo(params?.languageCode)).description;
|
|
66
59
|
}
|
|
67
60
|
async getMyShortDescription(params) {
|
|
68
|
-
|
|
69
|
-
return (await
|
|
61
|
+
this.#c.storage.assertBot("getMyShortDescription");
|
|
62
|
+
return (await this.#getMyInfo(params?.languageCode)).about;
|
|
70
63
|
}
|
|
71
64
|
async getMyCommands(params) {
|
|
72
|
-
|
|
73
|
-
const commands_ = await
|
|
65
|
+
this.#c.storage.assertBot("getMyCommands");
|
|
66
|
+
const commands_ = await this.#c.invoke({
|
|
74
67
|
_: "bots.getBotCommands",
|
|
75
68
|
lang_code: params?.languageCode ?? "",
|
|
76
|
-
scope: await botCommandScopeToTlObject(params?.scope ?? { type: "default" },
|
|
69
|
+
scope: await botCommandScopeToTlObject(params?.scope ?? { type: "default" }, this.#c.getInputPeer),
|
|
77
70
|
});
|
|
78
71
|
return commands_.map((v) => ({ command: v.command, description: v.description }));
|
|
79
72
|
}
|
|
80
73
|
async setMyCommands(commands, params) {
|
|
81
|
-
|
|
82
|
-
await
|
|
74
|
+
this.#c.storage.assertBot("setMyCommands");
|
|
75
|
+
await this.#c.invoke({
|
|
83
76
|
_: "bots.setBotCommands",
|
|
84
77
|
commands: commands.map((v) => ({ ...v, _: "botCommand" })),
|
|
85
78
|
lang_code: params?.languageCode ?? "",
|
|
86
|
-
scope: await botCommandScopeToTlObject(params?.scope ?? { type: "default" },
|
|
79
|
+
scope: await botCommandScopeToTlObject(params?.scope ?? { type: "default" }, this.#c.getInputPeer),
|
|
87
80
|
});
|
|
88
81
|
}
|
|
89
82
|
handleUpdate(update) {
|
|
@@ -93,8 +86,3 @@ export class BotInfoManager {
|
|
|
93
86
|
return { botCommands: { botId, chatId, commands } };
|
|
94
87
|
}
|
|
95
88
|
}
|
|
96
|
-
_BotInfoManager_c = new WeakMap(), _BotInfoManager_instances = new WeakSet(), _BotInfoManager_setMyInfo = async function _BotInfoManager_setMyInfo(info) {
|
|
97
|
-
await __classPrivateFieldGet(this, _BotInfoManager_c, "f").invoke({ _: "bots.setBotInfo", ...info });
|
|
98
|
-
}, _BotInfoManager_getMyInfo = function _BotInfoManager_getMyInfo(languageCode) {
|
|
99
|
-
return __classPrivateFieldGet(this, _BotInfoManager_c, "f").invoke({ _: "bots.getBotInfo", lang_code: languageCode ?? "" });
|
|
100
|
-
};
|
|
@@ -17,39 +17,27 @@
|
|
|
17
17
|
* You should have received a copy of the GNU Lesser General Public License
|
|
18
18
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
19
19
|
*/
|
|
20
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
21
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
22
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
23
|
-
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");
|
|
24
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
25
|
-
};
|
|
26
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
27
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
28
|
-
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");
|
|
29
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
30
|
-
};
|
|
31
|
-
var _BusinessConnectionManager_c;
|
|
32
20
|
import { Api } from "../2_tl.js";
|
|
33
21
|
import { constructBusinessConnection } from "../3_types.js";
|
|
34
22
|
const businessConnectionManagerUpdates = [
|
|
35
23
|
"updateBotBusinessConnect",
|
|
36
24
|
];
|
|
37
25
|
export class BusinessConnectionManager {
|
|
26
|
+
#c;
|
|
38
27
|
constructor(c) {
|
|
39
|
-
|
|
40
|
-
__classPrivateFieldSet(this, _BusinessConnectionManager_c, c, "f");
|
|
28
|
+
this.#c = c;
|
|
41
29
|
}
|
|
42
30
|
async getBusinessConnection(id) {
|
|
43
|
-
const connection_ = await
|
|
31
|
+
const connection_ = await this.#c.messageStorage.getBusinessConnection(id);
|
|
44
32
|
if (!connection_) {
|
|
45
|
-
const connection_ = await
|
|
33
|
+
const connection_ = await this.#c.invoke({ _: "account.getBotBusinessConnection", connection_id: id })
|
|
46
34
|
.then((v) => Api.as("updates", v))
|
|
47
35
|
.then((v) => Api.as("updateBotBusinessConnect", v.updates[0]).connection);
|
|
48
|
-
await
|
|
49
|
-
return constructBusinessConnection(connection_,
|
|
36
|
+
await this.#c.messageStorage.setBusinessConnection(id, connection_);
|
|
37
|
+
return constructBusinessConnection(connection_, this.#c.getPeer);
|
|
50
38
|
}
|
|
51
39
|
else {
|
|
52
|
-
return constructBusinessConnection(connection_,
|
|
40
|
+
return constructBusinessConnection(connection_, this.#c.getPeer);
|
|
53
41
|
}
|
|
54
42
|
}
|
|
55
43
|
canHandleUpdate(update) {
|
|
@@ -57,13 +45,12 @@ export class BusinessConnectionManager {
|
|
|
57
45
|
}
|
|
58
46
|
async handleUpdate(update) {
|
|
59
47
|
if (update.connection.disabled) {
|
|
60
|
-
await
|
|
48
|
+
await this.#c.messageStorage.setBusinessConnection(update.connection.connection_id, null);
|
|
61
49
|
}
|
|
62
50
|
else {
|
|
63
|
-
await
|
|
51
|
+
await this.#c.messageStorage.setBusinessConnection(update.connection.connection_id, update.connection);
|
|
64
52
|
}
|
|
65
|
-
const businessConnection = constructBusinessConnection(update.connection,
|
|
53
|
+
const businessConnection = constructBusinessConnection(update.connection, this.#c.getPeer);
|
|
66
54
|
return { businessConnection };
|
|
67
55
|
}
|
|
68
56
|
}
|
|
69
|
-
_BusinessConnectionManager_c = new WeakMap();
|
|
@@ -17,18 +17,7 @@
|
|
|
17
17
|
* You should have received a copy of the GNU Lesser General Public License
|
|
18
18
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
19
19
|
*/
|
|
20
|
-
var
|
|
21
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
22
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
23
|
-
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");
|
|
24
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
25
|
-
};
|
|
26
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
27
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
28
|
-
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");
|
|
29
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
30
|
-
};
|
|
31
|
-
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;
|
|
20
|
+
var _a;
|
|
32
21
|
import { InputError } from "../0_errors.js";
|
|
33
22
|
import { getLogger } from "../1_utilities.js";
|
|
34
23
|
import { Api, Mtproto, X } from "../2_tl.js";
|
|
@@ -42,61 +31,44 @@ import { ClientPlain } from "./1_client_plain.js";
|
|
|
42
31
|
// global ClientEncrypted ID counter for logs
|
|
43
32
|
let id = 0;
|
|
44
33
|
export class ClientEncrypted extends ClientAbstract {
|
|
34
|
+
static #SEND_MAX_TRIES = 10;
|
|
35
|
+
static #AUTH_KEY_CREATION_MAX_TRIES = 10;
|
|
36
|
+
handlers = {};
|
|
37
|
+
#L;
|
|
38
|
+
#plain;
|
|
39
|
+
session;
|
|
40
|
+
#sentRequests = new Map();
|
|
41
|
+
#apiId;
|
|
42
|
+
#appVersion;
|
|
43
|
+
#deviceModel;
|
|
44
|
+
#langCode;
|
|
45
|
+
#langPack;
|
|
46
|
+
#systemLangCode;
|
|
47
|
+
#systemVersion;
|
|
48
|
+
#disableUpdates;
|
|
45
49
|
constructor(dc, apiId, params) {
|
|
46
50
|
super();
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
enumerable: true,
|
|
50
|
-
configurable: true,
|
|
51
|
-
writable: true,
|
|
52
|
-
value: {}
|
|
53
|
-
});
|
|
54
|
-
_ClientEncrypted_L.set(this, void 0);
|
|
55
|
-
_ClientEncrypted_plain.set(this, void 0);
|
|
56
|
-
Object.defineProperty(this, "session", {
|
|
57
|
-
enumerable: true,
|
|
58
|
-
configurable: true,
|
|
59
|
-
writable: true,
|
|
60
|
-
value: void 0
|
|
61
|
-
});
|
|
62
|
-
_ClientEncrypted_sentRequests.set(this, new Map());
|
|
63
|
-
_ClientEncrypted_apiId.set(this, void 0);
|
|
64
|
-
_ClientEncrypted_appVersion.set(this, void 0);
|
|
65
|
-
_ClientEncrypted_deviceModel.set(this, void 0);
|
|
66
|
-
_ClientEncrypted_langCode.set(this, void 0);
|
|
67
|
-
_ClientEncrypted_langPack.set(this, void 0);
|
|
68
|
-
_ClientEncrypted_systemLangCode.set(this, void 0);
|
|
69
|
-
_ClientEncrypted_systemVersion.set(this, void 0);
|
|
70
|
-
_ClientEncrypted_disableUpdates.set(this, void 0);
|
|
71
|
-
_ClientEncrypted_createAuthKeyPromise.set(this, void 0);
|
|
72
|
-
_ClientEncrypted_connectionInited.set(this, false);
|
|
73
|
-
Object.defineProperty(this, "lastRequest", {
|
|
74
|
-
enumerable: true,
|
|
75
|
-
configurable: true,
|
|
76
|
-
writable: true,
|
|
77
|
-
value: void 0
|
|
78
|
-
});
|
|
79
|
-
__classPrivateFieldSet(this, _ClientEncrypted_L, getLogger("ClientEncrypted").client(id++), "f");
|
|
80
|
-
__classPrivateFieldSet(this, _ClientEncrypted_plain, new ClientPlain(dc, params), "f");
|
|
51
|
+
this.#L = getLogger("ClientEncrypted").client(id++);
|
|
52
|
+
this.#plain = new ClientPlain(dc, params);
|
|
81
53
|
this.session = new SessionEncrypted(dc, params);
|
|
82
|
-
this.session.handlers.onUpdate =
|
|
83
|
-
this.session.handlers.onNewServerSalt =
|
|
84
|
-
this.session.handlers.onMessageFailed =
|
|
85
|
-
this.session.handlers.onRpcError =
|
|
86
|
-
this.session.handlers.onRpcResult =
|
|
87
|
-
this.session.handlers.onPong =
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
54
|
+
this.session.handlers.onUpdate = this.#onUpdate.bind(this);
|
|
55
|
+
this.session.handlers.onNewServerSalt = this.#onNewServerSalt.bind(this);
|
|
56
|
+
this.session.handlers.onMessageFailed = this.#onMessageFailed.bind(this);
|
|
57
|
+
this.session.handlers.onRpcError = this.#onRpcError.bind(this);
|
|
58
|
+
this.session.handlers.onRpcResult = this.#onRpcResult.bind(this);
|
|
59
|
+
this.session.handlers.onPong = this.#onPong.bind(this);
|
|
60
|
+
this.#apiId = apiId;
|
|
61
|
+
this.#appVersion = params?.appVersion ?? APP_VERSION;
|
|
62
|
+
this.#deviceModel = params?.deviceModel ?? DEVICE_MODEL;
|
|
63
|
+
this.#langCode = params?.langCode ?? LANG_CODE;
|
|
64
|
+
this.#langPack = params?.langPack ?? LANG_PACK;
|
|
65
|
+
this.#systemLangCode = params?.systemLangCode ?? SYSTEM_LANG_CODE;
|
|
66
|
+
this.#systemVersion = params?.systemVersion ?? SYSTEM_VERSION;
|
|
67
|
+
this.#disableUpdates = params?.disableUpdates ?? false;
|
|
96
68
|
}
|
|
97
69
|
async connect() {
|
|
98
70
|
if (!this.authKey.length) {
|
|
99
|
-
await
|
|
71
|
+
await this.#createAuthKey();
|
|
100
72
|
}
|
|
101
73
|
await super.connect();
|
|
102
74
|
}
|
|
@@ -104,187 +76,198 @@ export class ClientEncrypted extends ClientAbstract {
|
|
|
104
76
|
super.disconnect();
|
|
105
77
|
this.lastRequest = undefined;
|
|
106
78
|
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
}
|
|
113
|
-
async invoke(function_) {
|
|
114
|
-
const messageId = await __classPrivateFieldGet(this, _ClientEncrypted_instances, "m", _ClientEncrypted_send).call(this, function_);
|
|
115
|
-
__classPrivateFieldGet(this, _ClientEncrypted_L, "f").debug("sent", function_._, "with msg_id", messageId);
|
|
116
|
-
const sentRequest = { call: function_, promiseWithResolvers: Promise.withResolvers() };
|
|
117
|
-
__classPrivateFieldGet(this, _ClientEncrypted_sentRequests, "f").set(messageId, sentRequest);
|
|
118
|
-
return await sentRequest.promiseWithResolvers.promise;
|
|
79
|
+
#createAuthKeyPromise;
|
|
80
|
+
#createAuthKey() {
|
|
81
|
+
return this.#createAuthKeyPromise ??= this.#createAuthKeyInner().finally(() => {
|
|
82
|
+
this.#createAuthKeyPromise = undefined;
|
|
83
|
+
});
|
|
119
84
|
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
await __classPrivateFieldGet(this, _ClientEncrypted_plain, "f").connect();
|
|
131
|
-
const [authKey, serverSalt] = await __classPrivateFieldGet(this, _ClientEncrypted_plain, "f").createAuthKey();
|
|
132
|
-
await this.setAuthKey(authKey);
|
|
133
|
-
this.serverSalt = serverSalt;
|
|
134
|
-
errored = false;
|
|
135
|
-
break;
|
|
136
|
-
}
|
|
137
|
-
catch (err) {
|
|
138
|
-
errored = true;
|
|
139
|
-
lastErr = err;
|
|
140
|
-
if (this.isDisconnected) {
|
|
85
|
+
async #createAuthKeyInner() {
|
|
86
|
+
let lastErr;
|
|
87
|
+
let errored = false;
|
|
88
|
+
for (let i = 0; i < _a.#AUTH_KEY_CREATION_MAX_TRIES; ++i) {
|
|
89
|
+
try {
|
|
90
|
+
await this.#plain.connect();
|
|
91
|
+
const [authKey, serverSalt] = await this.#plain.createAuthKey();
|
|
92
|
+
await this.setAuthKey(authKey);
|
|
93
|
+
this.serverSalt = serverSalt;
|
|
94
|
+
errored = false;
|
|
141
95
|
break;
|
|
142
96
|
}
|
|
143
|
-
|
|
97
|
+
catch (err) {
|
|
98
|
+
errored = true;
|
|
99
|
+
lastErr = err;
|
|
100
|
+
if (this.isDisconnected) {
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
103
|
+
this.#L.error("failed to create auth key:", err);
|
|
104
|
+
}
|
|
105
|
+
finally {
|
|
106
|
+
this.#plain.disconnect();
|
|
107
|
+
}
|
|
144
108
|
}
|
|
145
|
-
|
|
146
|
-
|
|
109
|
+
if (errored) {
|
|
110
|
+
throw lastErr;
|
|
147
111
|
}
|
|
148
112
|
}
|
|
149
|
-
|
|
150
|
-
|
|
113
|
+
get authKey() {
|
|
114
|
+
return this.session.authKey;
|
|
151
115
|
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
let body;
|
|
155
|
-
if (Mtproto.is("ping", function_)) {
|
|
156
|
-
body = Mtproto.serializeObject(function_);
|
|
116
|
+
async setAuthKey(authKey) {
|
|
117
|
+
await this.session.setAuthKey(authKey);
|
|
157
118
|
}
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
119
|
+
#connectionInited = false;
|
|
120
|
+
lastRequest;
|
|
121
|
+
async #send(function_) {
|
|
122
|
+
this.lastRequest = new Date();
|
|
123
|
+
let body;
|
|
124
|
+
if (Mtproto.is("ping", function_)) {
|
|
125
|
+
body = Mtproto.serializeObject(function_);
|
|
161
126
|
}
|
|
162
|
-
|
|
163
|
-
if (
|
|
164
|
-
|
|
127
|
+
else {
|
|
128
|
+
if (this.#disableUpdates && !isCdnFunction(function_)) {
|
|
129
|
+
function_ = { _: "invokeWithoutUpdates", query: function_ };
|
|
165
130
|
}
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
for (let i = 0; i < __classPrivateFieldGet(_a, _a, "f", _ClientEncrypted_SEND_MAX_TRIES); ++i) {
|
|
186
|
-
let errored = false;
|
|
187
|
-
try {
|
|
188
|
-
return await this.session.send(body);
|
|
189
|
-
}
|
|
190
|
-
catch (err) {
|
|
191
|
-
errored = true;
|
|
192
|
-
lastErr = err;
|
|
193
|
-
if (this.isDisconnected) {
|
|
194
|
-
break;
|
|
131
|
+
if (!this.#connectionInited) {
|
|
132
|
+
if (!this.#apiId) {
|
|
133
|
+
throw new InputError("apiId not set");
|
|
134
|
+
}
|
|
135
|
+
function_ = {
|
|
136
|
+
_: "initConnection",
|
|
137
|
+
api_id: this.#apiId,
|
|
138
|
+
app_version: this.#appVersion,
|
|
139
|
+
device_model: this.#deviceModel,
|
|
140
|
+
lang_code: this.#langCode,
|
|
141
|
+
lang_pack: this.#langPack,
|
|
142
|
+
query: {
|
|
143
|
+
_: "invokeWithLayer",
|
|
144
|
+
layer: Api.LAYER,
|
|
145
|
+
query: function_,
|
|
146
|
+
},
|
|
147
|
+
system_lang_code: this.#systemLangCode,
|
|
148
|
+
system_version: this.#systemVersion,
|
|
149
|
+
};
|
|
195
150
|
}
|
|
196
|
-
|
|
151
|
+
body = Api.serializeObject(function_);
|
|
197
152
|
}
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
153
|
+
let lastErr;
|
|
154
|
+
for (let i = 0; i < _a.#SEND_MAX_TRIES; ++i) {
|
|
155
|
+
let errored = false;
|
|
156
|
+
try {
|
|
157
|
+
return await this.session.send(body);
|
|
158
|
+
}
|
|
159
|
+
catch (err) {
|
|
160
|
+
errored = true;
|
|
161
|
+
lastErr = err;
|
|
162
|
+
if (this.isDisconnected) {
|
|
163
|
+
break;
|
|
164
|
+
}
|
|
165
|
+
this.#L.error("send failed:", err);
|
|
166
|
+
}
|
|
167
|
+
finally {
|
|
168
|
+
if (!errored) {
|
|
169
|
+
this.#L.debug("invoked", repr(function_));
|
|
170
|
+
this.#L.out(function_);
|
|
171
|
+
}
|
|
202
172
|
}
|
|
203
173
|
}
|
|
174
|
+
throw new Error(`Failed to invoke function after ${_a.#SEND_MAX_TRIES} tries.`, { cause: lastErr });
|
|
204
175
|
}
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
__classPrivateFieldGet(this, _ClientEncrypted_sentRequests, "f").set(messageId, request);
|
|
210
|
-
}
|
|
211
|
-
catch (err) {
|
|
212
|
-
__classPrivateFieldGet(this, _ClientEncrypted_L, "f").error("rejecting message because of resend error:", err);
|
|
213
|
-
request.promiseWithResolvers.reject(err);
|
|
214
|
-
}
|
|
215
|
-
}, _ClientEncrypted_onUpdate = async function _ClientEncrypted_onUpdate(body) {
|
|
216
|
-
let type;
|
|
217
|
-
try {
|
|
218
|
-
type = await Api.deserializeType(X, body);
|
|
219
|
-
}
|
|
220
|
-
catch (err) {
|
|
221
|
-
__classPrivateFieldGet(this, _ClientEncrypted_L, "f").error("failed to deserialize update:", err);
|
|
222
|
-
this.handlers.onDeserializationError?.();
|
|
223
|
-
return;
|
|
224
|
-
}
|
|
225
|
-
if (Api.isOfEnum("Update", type) || Api.isOfEnum("Updates", type)) {
|
|
226
|
-
this.handlers.onUpdate?.(type);
|
|
227
|
-
}
|
|
228
|
-
else {
|
|
229
|
-
__classPrivateFieldGet(this, _ClientEncrypted_L, "f").warning("received unknown type:", repr(type));
|
|
230
|
-
}
|
|
231
|
-
}, _ClientEncrypted_onNewServerSalt = function _ClientEncrypted_onNewServerSalt(serverSalt) {
|
|
232
|
-
this.handlers.onNewServerSalt?.(serverSalt);
|
|
233
|
-
}, _ClientEncrypted_onMessageFailed = async function _ClientEncrypted_onMessageFailed(msgId, error) {
|
|
234
|
-
const request = __classPrivateFieldGet(this, _ClientEncrypted_sentRequests, "f").get(msgId);
|
|
235
|
-
if (request) {
|
|
236
|
-
__classPrivateFieldGet(this, _ClientEncrypted_sentRequests, "f").delete(msgId);
|
|
237
|
-
if (error instanceof SessionError) {
|
|
238
|
-
await __classPrivateFieldGet(this, _ClientEncrypted_instances, "m", _ClientEncrypted_resend).call(this, request);
|
|
176
|
+
async #resend(request) {
|
|
177
|
+
try {
|
|
178
|
+
const messageId = await this.#send(request.call);
|
|
179
|
+
this.#sentRequests.set(messageId, request);
|
|
239
180
|
}
|
|
240
|
-
|
|
241
|
-
|
|
181
|
+
catch (err) {
|
|
182
|
+
this.#L.error("rejecting message because of resend error:", err);
|
|
183
|
+
request.promiseWithResolvers.reject(err);
|
|
242
184
|
}
|
|
243
185
|
}
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
if (reason instanceof ConnectionNotInited) {
|
|
251
|
-
__classPrivateFieldSet(this, _ClientEncrypted_connectionInited, false, "f");
|
|
252
|
-
await __classPrivateFieldGet(this, _ClientEncrypted_instances, "m", _ClientEncrypted_resend).call(this, request);
|
|
253
|
-
}
|
|
254
|
-
else {
|
|
255
|
-
request.promiseWithResolvers.reject(constructTelegramError(error, request.call));
|
|
256
|
-
}
|
|
186
|
+
async invoke(function_) {
|
|
187
|
+
const messageId = await this.#send(function_);
|
|
188
|
+
this.#L.debug("sent", function_._, "with msg_id", messageId);
|
|
189
|
+
const sentRequest = { call: function_, promiseWithResolvers: Promise.withResolvers() };
|
|
190
|
+
this.#sentRequests.set(messageId, sentRequest);
|
|
191
|
+
return await sentRequest.promiseWithResolvers.promise;
|
|
257
192
|
}
|
|
258
|
-
|
|
259
|
-
const sentRequest = __classPrivateFieldGet(this, _ClientEncrypted_sentRequests, "f").get(msgId);
|
|
260
|
-
__classPrivateFieldGet(this, _ClientEncrypted_L, "f").debug("received rpc_result with req_msg_id =", msgId, "for", sentRequest === undefined ? "unknown" : "known", "request");
|
|
261
|
-
if (sentRequest) {
|
|
193
|
+
async #onUpdate(body) {
|
|
262
194
|
let type;
|
|
263
195
|
try {
|
|
264
|
-
type = await Api.deserializeType(
|
|
265
|
-
__classPrivateFieldGet(this, _ClientEncrypted_L, "f").in(type);
|
|
266
|
-
__classPrivateFieldGet(this, _ClientEncrypted_L, "f").debug("received rpc_result", repr(type));
|
|
267
|
-
sentRequest.promiseWithResolvers.resolve(type);
|
|
196
|
+
type = await Api.deserializeType(X, body);
|
|
268
197
|
}
|
|
269
198
|
catch (err) {
|
|
270
|
-
|
|
271
|
-
__classPrivateFieldGet(this, _ClientEncrypted_L, "f").error("failed to deserialize rpc_result body:", err);
|
|
199
|
+
this.#L.error("failed to deserialize update:", err);
|
|
272
200
|
this.handlers.onDeserializationError?.();
|
|
273
201
|
return;
|
|
274
202
|
}
|
|
275
|
-
|
|
276
|
-
|
|
203
|
+
if (Api.isOfEnum("Update", type) || Api.isOfEnum("Updates", type)) {
|
|
204
|
+
this.handlers.onUpdate?.(type);
|
|
205
|
+
}
|
|
206
|
+
else {
|
|
207
|
+
this.#L.warning("received unknown type:", repr(type));
|
|
277
208
|
}
|
|
278
209
|
}
|
|
279
|
-
|
|
280
|
-
|
|
210
|
+
#onNewServerSalt(serverSalt) {
|
|
211
|
+
this.handlers.onNewServerSalt?.(serverSalt);
|
|
281
212
|
}
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
213
|
+
async #onMessageFailed(msgId, error) {
|
|
214
|
+
const request = this.#sentRequests.get(msgId);
|
|
215
|
+
if (request) {
|
|
216
|
+
this.#sentRequests.delete(msgId);
|
|
217
|
+
if (error instanceof SessionError) {
|
|
218
|
+
await this.#resend(request);
|
|
219
|
+
}
|
|
220
|
+
else {
|
|
221
|
+
request.promiseWithResolvers.reject(error);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
async #onRpcError(msgId, error) {
|
|
226
|
+
const request = this.#sentRequests.get(msgId);
|
|
227
|
+
this.#L.debug("received rpc_error with req_msg_id =", msgId, "for", request === undefined ? "unknown" : "known", "request");
|
|
228
|
+
if (request) {
|
|
229
|
+
this.#sentRequests.delete(msgId);
|
|
230
|
+
const reason = constructTelegramError(error, request.call);
|
|
231
|
+
if (reason instanceof ConnectionNotInited) {
|
|
232
|
+
this.#connectionInited = false;
|
|
233
|
+
await this.#resend(request);
|
|
234
|
+
}
|
|
235
|
+
else {
|
|
236
|
+
request.promiseWithResolvers.reject(constructTelegramError(error, request.call));
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
async #onRpcResult(msgId, body) {
|
|
241
|
+
const sentRequest = this.#sentRequests.get(msgId);
|
|
242
|
+
this.#L.debug("received rpc_result with req_msg_id =", msgId, "for", sentRequest === undefined ? "unknown" : "known", "request");
|
|
243
|
+
if (sentRequest) {
|
|
244
|
+
let type;
|
|
245
|
+
try {
|
|
246
|
+
type = await Api.deserializeType(Api.mustGetReturnType(sentRequest.call._), body);
|
|
247
|
+
this.#L.in(type);
|
|
248
|
+
this.#L.debug("received rpc_result", repr(type));
|
|
249
|
+
sentRequest.promiseWithResolvers.resolve(type);
|
|
250
|
+
}
|
|
251
|
+
catch (err) {
|
|
252
|
+
sentRequest.promiseWithResolvers.reject(err);
|
|
253
|
+
this.#L.error("failed to deserialize rpc_result body:", err);
|
|
254
|
+
this.handlers.onDeserializationError?.();
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
finally {
|
|
258
|
+
this.#sentRequests.delete(msgId);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
if (!this.#connectionInited) {
|
|
262
|
+
this.#connectionInited = true;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
#onPong(pong) {
|
|
266
|
+
const sentRequest = this.#sentRequests.get(pong.msg_id);
|
|
267
|
+
if (sentRequest) {
|
|
268
|
+
sentRequest.promiseWithResolvers.resolve(pong);
|
|
269
|
+
this.#sentRequests.delete(pong.msg_id);
|
|
270
|
+
}
|
|
287
271
|
}
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
_ClientEncrypted_AUTH_KEY_CREATION_MAX_TRIES = { value: 10 };
|
|
272
|
+
}
|
|
273
|
+
_a = ClientEncrypted;
|