@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.
- 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,35 +17,20 @@
|
|
|
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 _NetworkStatisticsManager_instances, _NetworkStatisticsManager_c, _NetworkStatisticsManager_L, _NetworkStatisticsManager_pendingWrites, _NetworkStatisticsManager_writing, _NetworkStatisticsManager_write;
|
|
32
20
|
import { getLogger } from "../utilities/1_logger.js";
|
|
33
21
|
export class NetworkStatisticsManager {
|
|
22
|
+
#c;
|
|
23
|
+
#L;
|
|
34
24
|
constructor(c) {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
_NetworkStatisticsManager_L.set(this, void 0);
|
|
38
|
-
_NetworkStatisticsManager_pendingWrites.set(this, {});
|
|
39
|
-
_NetworkStatisticsManager_writing.set(this, false);
|
|
40
|
-
__classPrivateFieldSet(this, _NetworkStatisticsManager_c, c, "f");
|
|
41
|
-
__classPrivateFieldSet(this, _NetworkStatisticsManager_L, getLogger("NetworkStatisticsManager"), "f");
|
|
25
|
+
this.#c = c;
|
|
26
|
+
this.#L = getLogger("NetworkStatisticsManager");
|
|
42
27
|
}
|
|
43
28
|
async getNetworkStatistics() {
|
|
44
29
|
const [messagesRead, messagesWrite, cdnRead, cdnWrite] = await Promise.all([
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
30
|
+
this.#c.storage.get(["netstat_messages_read"]),
|
|
31
|
+
this.#c.storage.get(["netstat_messages_write"]),
|
|
32
|
+
this.#c.storage.get(["netstat_cdn_read"]),
|
|
33
|
+
this.#c.storage.get(["netstat_cdn_write"]),
|
|
49
34
|
]);
|
|
50
35
|
const messages = {
|
|
51
36
|
sent: Number(messagesWrite || 0),
|
|
@@ -57,37 +42,39 @@ export class NetworkStatisticsManager {
|
|
|
57
42
|
};
|
|
58
43
|
return { messages, cdn };
|
|
59
44
|
}
|
|
45
|
+
#pendingWrites = {};
|
|
60
46
|
getTransportReadWriteCallback(isCcdn) {
|
|
61
47
|
return {
|
|
62
48
|
read: (count) => {
|
|
63
49
|
const key = isCcdn ? "netstat_cdn_read" : "netstat_messages_read";
|
|
64
|
-
|
|
65
|
-
|
|
50
|
+
this.#pendingWrites[key] = (this.#pendingWrites[key] ?? 0) + count;
|
|
51
|
+
this.#write();
|
|
66
52
|
},
|
|
67
53
|
write: (count) => {
|
|
68
54
|
const key = isCcdn ? "netstat_cdn_write" : "netstat_messages_write";
|
|
69
|
-
|
|
70
|
-
|
|
55
|
+
this.#pendingWrites[key] = (this.#pendingWrites[key] ?? 0) + count;
|
|
56
|
+
this.#write();
|
|
71
57
|
},
|
|
72
58
|
};
|
|
73
59
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
}
|
|
79
|
-
__classPrivateFieldSet(this, _NetworkStatisticsManager_writing, true, "f");
|
|
80
|
-
for (const [k, v] of Object.entries(__classPrivateFieldGet(this, _NetworkStatisticsManager_pendingWrites, "f"))) {
|
|
81
|
-
if (v < 1) {
|
|
82
|
-
continue;
|
|
60
|
+
#writing = false;
|
|
61
|
+
async #write() {
|
|
62
|
+
if (this.#writing) {
|
|
63
|
+
return;
|
|
83
64
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
65
|
+
this.#writing = true;
|
|
66
|
+
for (const [k, v] of Object.entries(this.#pendingWrites)) {
|
|
67
|
+
if (v < 1) {
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
try {
|
|
71
|
+
await this.#c.messageStorage.incr([k], v);
|
|
72
|
+
this.#pendingWrites[k] -= v;
|
|
73
|
+
}
|
|
74
|
+
catch (err) {
|
|
75
|
+
this.#L.error("write failed:", err);
|
|
76
|
+
}
|
|
90
77
|
}
|
|
78
|
+
this.#writing = false;
|
|
91
79
|
}
|
|
92
|
-
|
|
93
|
-
};
|
|
80
|
+
}
|
|
@@ -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 _PaymentManager_c;
|
|
32
20
|
import { InputError } from "../0_errors.js";
|
|
33
21
|
import { Api } from "../2_tl.js";
|
|
34
22
|
import { constructPreCheckoutQuery } from "../3_types.js";
|
|
@@ -36,22 +24,22 @@ const paymentManagerUpdates = [
|
|
|
36
24
|
"updateBotPrecheckoutQuery",
|
|
37
25
|
];
|
|
38
26
|
export class PaymentManager {
|
|
27
|
+
#c;
|
|
39
28
|
constructor(c) {
|
|
40
|
-
|
|
41
|
-
__classPrivateFieldSet(this, _PaymentManager_c, c, "f");
|
|
29
|
+
this.#c = c;
|
|
42
30
|
}
|
|
43
31
|
canHandleUpdate(update) {
|
|
44
32
|
return Api.isOneOf(paymentManagerUpdates, update);
|
|
45
33
|
}
|
|
46
34
|
handleUpdate(update) {
|
|
47
35
|
if (Api.is("updateBotPrecheckoutQuery", update)) {
|
|
48
|
-
const preCheckoutQuery = constructPreCheckoutQuery(update,
|
|
36
|
+
const preCheckoutQuery = constructPreCheckoutQuery(update, this.#c.getPeer);
|
|
49
37
|
return { preCheckoutQuery };
|
|
50
38
|
}
|
|
51
39
|
return null;
|
|
52
40
|
}
|
|
53
41
|
async answerPreCheckoutQuery(preCheckoutQueryId, ok, params) {
|
|
54
|
-
|
|
42
|
+
this.#c.storage.assertBot("answerPreCheckoutQuery");
|
|
55
43
|
if (!ok && !params?.error) {
|
|
56
44
|
throw new InputError("error is required when ok is false");
|
|
57
45
|
}
|
|
@@ -59,11 +47,10 @@ export class PaymentManager {
|
|
|
59
47
|
if (!queryId) {
|
|
60
48
|
throw new InputError("Invalid pre-checkout query ID");
|
|
61
49
|
}
|
|
62
|
-
await
|
|
50
|
+
await this.#c.invoke({ _: "messages.setBotPrecheckoutResults", query_id: queryId, error: params?.error, success: ok ? true : undefined });
|
|
63
51
|
}
|
|
64
52
|
async refundStarPayment(userId, telegramPaymentChargeId) {
|
|
65
|
-
|
|
66
|
-
await
|
|
53
|
+
this.#c.storage.assertBot("refundStarPayment");
|
|
54
|
+
await this.#c.invoke({ _: "payments.refundStarsCharge", user_id: await this.#c.getInputUser(userId), charge_id: telegramPaymentChargeId });
|
|
67
55
|
}
|
|
68
56
|
}
|
|
69
|
-
_PaymentManager_c = new WeakMap();
|
|
@@ -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 _ReactionManager_c;
|
|
32
20
|
import { unreachable } from "../0_deps.js";
|
|
33
21
|
import { Api } from "../2_tl.js";
|
|
34
22
|
import { constructMessageReaction, constructMessageReactionCount, constructMessageReactions } from "../3_types.js";
|
|
@@ -40,16 +28,16 @@ const reactionManagerUpdates = [
|
|
|
40
28
|
"updateChannelMessageForwards",
|
|
41
29
|
];
|
|
42
30
|
export class ReactionManager {
|
|
31
|
+
#c;
|
|
43
32
|
constructor(c) {
|
|
44
|
-
|
|
45
|
-
__classPrivateFieldSet(this, _ReactionManager_c, c, "f");
|
|
33
|
+
this.#c = c;
|
|
46
34
|
}
|
|
47
35
|
canHandleUpdate(update) {
|
|
48
36
|
return Api.isOneOf(reactionManagerUpdates, update);
|
|
49
37
|
}
|
|
50
38
|
async handleUpdate(update) {
|
|
51
39
|
if (Api.is("updateBotMessageReactions", update)) {
|
|
52
|
-
const messageReactionCount = constructMessageReactionCount(update,
|
|
40
|
+
const messageReactionCount = constructMessageReactionCount(update, this.#c.getPeer);
|
|
53
41
|
if (messageReactionCount) {
|
|
54
42
|
return { messageReactionCount };
|
|
55
43
|
}
|
|
@@ -58,7 +46,7 @@ export class ReactionManager {
|
|
|
58
46
|
}
|
|
59
47
|
}
|
|
60
48
|
else if (Api.is("updateBotMessageReaction", update)) {
|
|
61
|
-
const messageReactions = constructMessageReactions(update,
|
|
49
|
+
const messageReactions = constructMessageReactions(update, this.#c.getPeer);
|
|
62
50
|
if (messageReactions) {
|
|
63
51
|
return { messageReactions };
|
|
64
52
|
}
|
|
@@ -68,10 +56,10 @@ export class ReactionManager {
|
|
|
68
56
|
}
|
|
69
57
|
else if (Api.is("updateMessageReactions", update)) {
|
|
70
58
|
const chatId = Api.peerToChatId(update.peer);
|
|
71
|
-
const message = await
|
|
59
|
+
const message = await this.#c.messageStorage.getMessage(chatId, update.msg_id);
|
|
72
60
|
if (Api.is("message", message)) {
|
|
73
61
|
message.reactions = update.reactions;
|
|
74
|
-
await
|
|
62
|
+
await this.#c.messageStorage.setMessage(chatId, update.msg_id, message);
|
|
75
63
|
const views = message.views ?? 0;
|
|
76
64
|
const forwards = message.forwards ?? 0;
|
|
77
65
|
const recentReactions = update.reactions.recent_reactions ?? [];
|
|
@@ -84,7 +72,7 @@ export class ReactionManager {
|
|
|
84
72
|
}
|
|
85
73
|
else if (Api.isOneOf(["updateChannelMessageViews", "updateChannelMessageForwards"], update)) {
|
|
86
74
|
const chatId = Api.peerToChatId({ ...update, _: "peerChannel" });
|
|
87
|
-
const message = await
|
|
75
|
+
const message = await this.#c.messageStorage.getMessage(chatId, update.id);
|
|
88
76
|
if (Api.is("message", message)) {
|
|
89
77
|
if ("views" in update) {
|
|
90
78
|
message.views = update.views;
|
|
@@ -107,4 +95,3 @@ export class ReactionManager {
|
|
|
107
95
|
}
|
|
108
96
|
}
|
|
109
97
|
}
|
|
110
|
-
_ReactionManager_c = new WeakMap();
|
|
@@ -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 _TranslationsManager_instances, _TranslationsManager_c, _TranslationsManager_getTranslationsInner, _TranslationsManager_updateTranslationsQueue, _TranslationsManager_updateTranslations, _TranslationsManager_applyLangPackDifferenceAndSave, _TranslationsManager_applyLangPackDifference;
|
|
32
20
|
import { unreachable } from "../0_deps.js";
|
|
33
21
|
import { InputError } from "../0_errors.js";
|
|
34
22
|
import { Queue } from "../1_utilities.js";
|
|
@@ -39,133 +27,135 @@ const translationsManagerUpdates = [
|
|
|
39
27
|
"updateLangPack",
|
|
40
28
|
];
|
|
41
29
|
export class TranslationsManager {
|
|
30
|
+
#c;
|
|
42
31
|
constructor(c) {
|
|
43
|
-
|
|
44
|
-
_TranslationsManager_c.set(this, void 0);
|
|
45
|
-
_TranslationsManager_updateTranslationsQueue.set(this, new Queue("updateTranslations"));
|
|
46
|
-
__classPrivateFieldSet(this, _TranslationsManager_c, c, "f");
|
|
32
|
+
this.#c = c;
|
|
47
33
|
}
|
|
48
34
|
canHandleUpdate(update) {
|
|
49
35
|
return Api.isOneOf(translationsManagerUpdates, update);
|
|
50
36
|
}
|
|
51
37
|
async getTranslations(params) {
|
|
52
|
-
|
|
53
|
-
const platform = params?.platform ??
|
|
38
|
+
this.#c.storage.assertUser("getTranslations");
|
|
39
|
+
const platform = params?.platform ?? this.#c.langPack;
|
|
54
40
|
if (!platform) {
|
|
55
41
|
throw new InputError("No platform specified.");
|
|
56
42
|
}
|
|
57
|
-
const language = params?.language ??
|
|
43
|
+
const language = params?.language ?? this.#c.langCode;
|
|
58
44
|
if (!language) {
|
|
59
45
|
throw new InputError("No language specified.");
|
|
60
46
|
}
|
|
61
|
-
return await
|
|
47
|
+
return await this.#getTranslationsInner(platform, language);
|
|
62
48
|
}
|
|
63
|
-
async
|
|
64
|
-
|
|
65
|
-
|
|
49
|
+
async #getTranslationsInner(platform, language, assert = false) {
|
|
50
|
+
const maybeTranslations = await this.#c.messageStorage.translations.get([platform, language]);
|
|
51
|
+
if (maybeTranslations !== null) {
|
|
52
|
+
return maybeTranslations.translations;
|
|
66
53
|
}
|
|
67
|
-
if (
|
|
68
|
-
|
|
69
|
-
const translations = await __classPrivateFieldGet(this, _TranslationsManager_instances, "m", _TranslationsManager_getTranslationsInner).call(this, __classPrivateFieldGet(this, _TranslationsManager_c, "f").langPack, update.lang_code, true);
|
|
70
|
-
return { platform: __classPrivateFieldGet(this, _TranslationsManager_c, "f").langPack, language: update.lang_code, translations };
|
|
54
|
+
else if (assert) {
|
|
55
|
+
unreachable();
|
|
71
56
|
}
|
|
72
|
-
else
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
}
|
|
76
|
-
const translations = await __classPrivateFieldGet(this, _TranslationsManager_instances, "m", _TranslationsManager_applyLangPackDifferenceAndSave).call(this, __classPrivateFieldGet(this, _TranslationsManager_c, "f").langPack, __classPrivateFieldGet(this, _TranslationsManager_c, "f").langCode, update.difference.version, update.difference.from_version, update.difference.strings);
|
|
77
|
-
if (!translations) {
|
|
78
|
-
return null;
|
|
79
|
-
}
|
|
80
|
-
else {
|
|
81
|
-
return { platform: __classPrivateFieldGet(this, _TranslationsManager_c, "f").langPack, language: __classPrivateFieldGet(this, _TranslationsManager_c, "f").langCode, translations };
|
|
82
|
-
}
|
|
57
|
+
else {
|
|
58
|
+
await this.#updateTranslations(platform, language);
|
|
59
|
+
return await this.#getTranslationsInner(platform, language, true);
|
|
83
60
|
}
|
|
84
|
-
return null;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
_TranslationsManager_c = new WeakMap(), _TranslationsManager_updateTranslationsQueue = new WeakMap(), _TranslationsManager_instances = new WeakSet(), _TranslationsManager_getTranslationsInner = async function _TranslationsManager_getTranslationsInner(platform, language, assert = false) {
|
|
88
|
-
const maybeTranslations = await __classPrivateFieldGet(this, _TranslationsManager_c, "f").messageStorage.translations.get([platform, language]);
|
|
89
|
-
if (maybeTranslations !== null) {
|
|
90
|
-
return maybeTranslations.translations;
|
|
91
61
|
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
62
|
+
#updateTranslationsQueue = new Queue("updateTranslations");
|
|
63
|
+
async #updateTranslations(platform, language) {
|
|
64
|
+
await new Promise((resolve, reject) => {
|
|
65
|
+
this.#updateTranslationsQueue.add(async () => {
|
|
66
|
+
try {
|
|
67
|
+
const maybeTranslations = await this.#c.messageStorage.translations.get([platform, language]);
|
|
68
|
+
if (maybeTranslations !== null) {
|
|
69
|
+
const difference = await this.#c.invoke({ _: "langpack.getDifference", lang_pack: platform, lang_code: language, from_version: maybeTranslations.version });
|
|
70
|
+
const newTranslations = this.#applyLangPackDifference(maybeTranslations.translations, difference.strings);
|
|
71
|
+
this.#c.messageStorage.translations.set([platform, language], { date: new Date(), version: difference.version, translations: newTranslations });
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
const pack = await this.#c.invoke({ _: "langpack.getLangPack", lang_pack: platform, lang_code: language });
|
|
75
|
+
const version = pack.version;
|
|
76
|
+
const translations = pack.strings.map(constructTranslation);
|
|
77
|
+
this.#c.messageStorage.translations.set([platform, language], { date: new Date(), version, translations });
|
|
78
|
+
}
|
|
79
|
+
resolve();
|
|
108
80
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
const version = pack.version;
|
|
112
|
-
const translations = pack.strings.map(constructTranslation);
|
|
113
|
-
__classPrivateFieldGet(this, _TranslationsManager_c, "f").messageStorage.translations.set([platform, language], { date: new Date(), version, translations });
|
|
81
|
+
catch (err) {
|
|
82
|
+
reject(err);
|
|
114
83
|
}
|
|
115
|
-
|
|
116
|
-
}
|
|
117
|
-
catch (err) {
|
|
118
|
-
reject(err);
|
|
119
|
-
}
|
|
84
|
+
});
|
|
120
85
|
});
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
86
|
+
}
|
|
87
|
+
async #applyLangPackDifferenceAndSave(platform, language, version, fromVersion, strings) {
|
|
88
|
+
const result = await new Promise((resolve, reject) => {
|
|
89
|
+
this.#updateTranslationsQueue.add(async () => {
|
|
90
|
+
try {
|
|
91
|
+
const maybeTranslations = await this.#c.messageStorage.translations.get([platform, language]);
|
|
92
|
+
let newTranslations = null;
|
|
93
|
+
if (maybeTranslations) {
|
|
94
|
+
if (fromVersion !== maybeTranslations.version) {
|
|
95
|
+
resolve("mustUpdate");
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
newTranslations = this.#applyLangPackDifference(maybeTranslations.translations, strings);
|
|
99
|
+
this.#c.messageStorage.translations.set([platform, language], {
|
|
100
|
+
date: new Date(),
|
|
101
|
+
version,
|
|
102
|
+
translations: newTranslations,
|
|
103
|
+
});
|
|
132
104
|
}
|
|
133
|
-
newTranslations
|
|
134
|
-
__classPrivateFieldGet(this, _TranslationsManager_c, "f").messageStorage.translations.set([platform, language], {
|
|
135
|
-
date: new Date(),
|
|
136
|
-
version,
|
|
137
|
-
translations: newTranslations,
|
|
138
|
-
});
|
|
105
|
+
resolve(newTranslations);
|
|
139
106
|
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
}
|
|
107
|
+
catch (err) {
|
|
108
|
+
reject(err);
|
|
109
|
+
}
|
|
110
|
+
});
|
|
145
111
|
});
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
112
|
+
if (result === "mustUpdate") {
|
|
113
|
+
await this.#updateTranslations(platform, language);
|
|
114
|
+
return await this.#getTranslationsInner(platform, language, true);
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
return result;
|
|
118
|
+
}
|
|
150
119
|
}
|
|
151
|
-
|
|
152
|
-
|
|
120
|
+
#applyLangPackDifference(translations, strings) {
|
|
121
|
+
for (const string of strings) {
|
|
122
|
+
if (Api.is("langPackStringDeleted", string)) {
|
|
123
|
+
translations = translations.filter((v) => v.key !== string.key);
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
const newTranslation = constructTranslation(string);
|
|
127
|
+
const currentTranslation = translations.find((v) => v.key === string.key);
|
|
128
|
+
if (currentTranslation) {
|
|
129
|
+
Object.assign(currentTranslation, newTranslation);
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
translations.push(newTranslation);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return translations;
|
|
153
137
|
}
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
translations = translations.filter((v) => v.key !== string.key);
|
|
138
|
+
async handleUpdate(update) {
|
|
139
|
+
if (!this.#c.langPack) {
|
|
140
|
+
return null;
|
|
158
141
|
}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
const
|
|
162
|
-
|
|
163
|
-
|
|
142
|
+
if (Api.is("updateLangPackTooLong", update)) {
|
|
143
|
+
await this.#updateTranslations(this.#c.langPack, update.lang_code);
|
|
144
|
+
const translations = await this.#getTranslationsInner(this.#c.langPack, update.lang_code, true);
|
|
145
|
+
return { platform: this.#c.langPack, language: update.lang_code, translations };
|
|
146
|
+
}
|
|
147
|
+
else if (Api.is("updateLangPack", update)) {
|
|
148
|
+
if (!this.#c.langCode) {
|
|
149
|
+
return null;
|
|
150
|
+
}
|
|
151
|
+
const translations = await this.#applyLangPackDifferenceAndSave(this.#c.langPack, this.#c.langCode, update.difference.version, update.difference.from_version, update.difference.strings);
|
|
152
|
+
if (!translations) {
|
|
153
|
+
return null;
|
|
164
154
|
}
|
|
165
155
|
else {
|
|
166
|
-
translations
|
|
156
|
+
return { platform: this.#c.langPack, language: this.#c.langCode, translations };
|
|
167
157
|
}
|
|
168
158
|
}
|
|
159
|
+
return null;
|
|
169
160
|
}
|
|
170
|
-
|
|
171
|
-
};
|
|
161
|
+
}
|