@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 _StorageOperations_instances, _StorageOperations_storage, _StorageOperations_supportsFiles, _StorageOperations_mustSerialize, _StorageOperations_L, _StorageOperations_maps, _StorageOperations_values, _StorageOperations_addMap, _StorageOperations_addValue, _StorageOperations_lastCommit, _StorageOperations_accountId, _StorageOperations_getUpdateId, _StorageMap_storage, _StorageMap_path, _StorageMap_pendingUpdates, _StorageMap_cache, _StorageValue_storage, _StorageValue_key, _StorageValue_updatePending, _StorageValue_value, _StorageAuth_instances, _StorageAuth_authKeyId, _StorageAuth_resetAuthKeyId;
|
|
32
20
|
import { MINUTE } from "../0_deps.js";
|
|
33
21
|
import { LruCache, unreachable } from "../0_deps.js";
|
|
34
22
|
import { InputError } from "../0_errors.js";
|
|
@@ -93,74 +81,54 @@ export const K = {
|
|
|
93
81
|
},
|
|
94
82
|
};
|
|
95
83
|
export class StorageOperations {
|
|
84
|
+
#storage;
|
|
85
|
+
#supportsFiles;
|
|
86
|
+
#mustSerialize;
|
|
87
|
+
#L;
|
|
88
|
+
#maps = new Array();
|
|
89
|
+
#values = new Array();
|
|
90
|
+
auth;
|
|
91
|
+
channelPts;
|
|
92
|
+
peers;
|
|
93
|
+
usernames;
|
|
94
|
+
translations;
|
|
96
95
|
constructor(storage) {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
value: void 0
|
|
115
|
-
});
|
|
116
|
-
Object.defineProperty(this, "peers", {
|
|
117
|
-
enumerable: true,
|
|
118
|
-
configurable: true,
|
|
119
|
-
writable: true,
|
|
120
|
-
value: void 0
|
|
121
|
-
});
|
|
122
|
-
Object.defineProperty(this, "usernames", {
|
|
123
|
-
enumerable: true,
|
|
124
|
-
configurable: true,
|
|
125
|
-
writable: true,
|
|
126
|
-
value: void 0
|
|
127
|
-
});
|
|
128
|
-
Object.defineProperty(this, "translations", {
|
|
129
|
-
enumerable: true,
|
|
130
|
-
configurable: true,
|
|
131
|
-
writable: true,
|
|
132
|
-
value: void 0
|
|
133
|
-
});
|
|
134
|
-
_StorageOperations_lastCommit.set(this, null);
|
|
135
|
-
_StorageOperations_accountId.set(this, null);
|
|
136
|
-
__classPrivateFieldSet(this, _StorageOperations_storage, storage, "f");
|
|
137
|
-
__classPrivateFieldSet(this, _StorageOperations_supportsFiles, storage.supportsFiles, "f");
|
|
138
|
-
__classPrivateFieldSet(this, _StorageOperations_mustSerialize, storage.mustSerialize, "f");
|
|
139
|
-
__classPrivateFieldSet(this, _StorageOperations_L, getLogger("StorageOperations"), "f");
|
|
140
|
-
this.auth = __classPrivateFieldGet(this, _StorageOperations_instances, "m", _StorageOperations_addValue).call(this, new StorageAuth(storage));
|
|
141
|
-
this.channelPts = __classPrivateFieldGet(this, _StorageOperations_instances, "m", _StorageOperations_addMap).call(this, new StorageMap(storage, "channelPts"));
|
|
142
|
-
this.peers = __classPrivateFieldGet(this, _StorageOperations_instances, "m", _StorageOperations_addMap).call(this, new StorageMap(storage, "peers"));
|
|
143
|
-
this.usernames = __classPrivateFieldGet(this, _StorageOperations_instances, "m", _StorageOperations_addMap).call(this, new StorageMap(storage, "usernames"));
|
|
144
|
-
this.translations = __classPrivateFieldGet(this, _StorageOperations_instances, "m", _StorageOperations_addMap).call(this, new StorageMap(storage, "translations"));
|
|
96
|
+
this.#storage = storage;
|
|
97
|
+
this.#supportsFiles = storage.supportsFiles;
|
|
98
|
+
this.#mustSerialize = storage.mustSerialize;
|
|
99
|
+
this.#L = getLogger("StorageOperations");
|
|
100
|
+
this.auth = this.#addValue(new StorageAuth(storage));
|
|
101
|
+
this.channelPts = this.#addMap(new StorageMap(storage, "channelPts"));
|
|
102
|
+
this.peers = this.#addMap(new StorageMap(storage, "peers"));
|
|
103
|
+
this.usernames = this.#addMap(new StorageMap(storage, "usernames"));
|
|
104
|
+
this.translations = this.#addMap(new StorageMap(storage, "translations"));
|
|
105
|
+
}
|
|
106
|
+
#addMap(map) {
|
|
107
|
+
this.#maps.push(map);
|
|
108
|
+
return map;
|
|
109
|
+
}
|
|
110
|
+
#addValue(value) {
|
|
111
|
+
this.#values.push(value);
|
|
112
|
+
return value;
|
|
145
113
|
}
|
|
146
114
|
get provider() {
|
|
147
|
-
return
|
|
115
|
+
return this.#storage;
|
|
148
116
|
}
|
|
149
117
|
get supportsFiles() {
|
|
150
|
-
return
|
|
118
|
+
return this.#storage.supportsFiles;
|
|
151
119
|
}
|
|
152
120
|
async initialize() {
|
|
153
|
-
await
|
|
121
|
+
await this.#storage.initialize();
|
|
154
122
|
await this.auth.get();
|
|
155
123
|
}
|
|
156
124
|
set(...args) {
|
|
157
|
-
return
|
|
125
|
+
return this.#storage.set(...args);
|
|
158
126
|
}
|
|
159
127
|
incr(...args) {
|
|
160
|
-
return
|
|
128
|
+
return this.#storage.incr(...args);
|
|
161
129
|
}
|
|
162
130
|
get(...args) {
|
|
163
|
-
return
|
|
131
|
+
return this.#storage.get(...args);
|
|
164
132
|
}
|
|
165
133
|
async exportAuthString(apiId_) {
|
|
166
134
|
if (typeof apiId_ === "number") {
|
|
@@ -195,56 +163,57 @@ export class StorageOperations {
|
|
|
195
163
|
userId,
|
|
196
164
|
});
|
|
197
165
|
}
|
|
166
|
+
#lastCommit = null;
|
|
198
167
|
async commit(force = false) {
|
|
199
|
-
if (
|
|
168
|
+
if (this.#storage.isMemory) {
|
|
200
169
|
return;
|
|
201
170
|
}
|
|
202
|
-
const pending =
|
|
171
|
+
const pending = this.#values.filter((v) => v.isUpdatePending).length + this.#maps.filter((v) => v.pendingUpdateCount > 0).length;
|
|
203
172
|
if (pending <= 0) {
|
|
204
|
-
|
|
173
|
+
this.#L.debug("nothing to commit");
|
|
205
174
|
return;
|
|
206
175
|
}
|
|
207
176
|
let commit = false;
|
|
208
177
|
if (force) {
|
|
209
|
-
|
|
178
|
+
this.#L.debug("committing because force = true");
|
|
210
179
|
commit = true;
|
|
211
180
|
}
|
|
212
181
|
else {
|
|
213
182
|
if (!commit && pending >= 1_000) {
|
|
214
|
-
|
|
183
|
+
this.#L.debug("committing because pending writes >= threshold");
|
|
215
184
|
commit = true;
|
|
216
185
|
}
|
|
217
|
-
else if (
|
|
218
|
-
|
|
186
|
+
else if (this.#lastCommit === null) {
|
|
187
|
+
this.#L.debug("committing because there is no last commit");
|
|
219
188
|
commit = true;
|
|
220
189
|
}
|
|
221
|
-
else if (Date.now() -
|
|
222
|
-
|
|
190
|
+
else if (Date.now() - this.#lastCommit.getTime() >= 5 * MINUTE) {
|
|
191
|
+
this.#L.debug("committing because last commit is older than threshold");
|
|
223
192
|
commit = true;
|
|
224
193
|
}
|
|
225
194
|
else {
|
|
226
|
-
|
|
195
|
+
this.#L.debug("not committing");
|
|
227
196
|
}
|
|
228
197
|
}
|
|
229
198
|
if (commit) {
|
|
230
|
-
const values =
|
|
231
|
-
const maps =
|
|
199
|
+
const values = this.#values.filter((v) => v.isUpdatePending).map((v) => v.commit());
|
|
200
|
+
const maps = this.#maps.filter((v) => v.pendingUpdateCount > 0).map((v) => v.commit());
|
|
232
201
|
await Promise.all(values.concat(maps));
|
|
233
|
-
|
|
234
|
-
|
|
202
|
+
this.#L.debug("committed", values.length, "value(s) and", maps.length, "map(s)");
|
|
203
|
+
this.#lastCommit = new Date();
|
|
235
204
|
}
|
|
236
205
|
}
|
|
237
206
|
async setTlObject(key, value) {
|
|
238
207
|
if (value === null) {
|
|
239
|
-
await
|
|
208
|
+
await this.#storage.set(key, null);
|
|
240
209
|
}
|
|
241
210
|
else {
|
|
242
|
-
await
|
|
211
|
+
await this.#storage.set(key, this.#mustSerialize ? [value._, rleEncode(Api.serializeObject(value))] : value);
|
|
243
212
|
}
|
|
244
213
|
}
|
|
245
214
|
async getTlObject(keyOrBuffer) {
|
|
246
215
|
// @ts-ignore: TBD
|
|
247
|
-
const buffer = (keyOrBuffer instanceof Uint8Array || Api.isValidObject(keyOrBuffer)) ? keyOrBuffer : await
|
|
216
|
+
const buffer = (keyOrBuffer instanceof Uint8Array || Api.isValidObject(keyOrBuffer)) ? keyOrBuffer : await this.#storage.get(keyOrBuffer);
|
|
248
217
|
if (buffer !== null) {
|
|
249
218
|
if (buffer instanceof Uint8Array) {
|
|
250
219
|
return await Api.deserializeType(X, rleDecode(buffer));
|
|
@@ -268,19 +237,19 @@ export class StorageOperations {
|
|
|
268
237
|
}
|
|
269
238
|
async setMessage(chatId, messageId, message) {
|
|
270
239
|
if (chatId > ZERO_CHANNEL_ID) {
|
|
271
|
-
await
|
|
240
|
+
await this.#storage.set(K.messages.messageRef(messageId), message === null ? null : chatId);
|
|
272
241
|
}
|
|
273
242
|
await this.setTlObject(K.messages.message(chatId, messageId), message);
|
|
274
243
|
}
|
|
275
244
|
async deleteMessages() {
|
|
276
245
|
const maybePromises = new Array();
|
|
277
|
-
for await (const [k, o] of await
|
|
278
|
-
maybePromises.push(Promise.all([
|
|
246
|
+
for await (const [k, o] of await this.#storage.getMany({ prefix: K.messages.allMessageRefs() })) {
|
|
247
|
+
maybePromises.push(Promise.all([this.#storage.set(k, null), o === null ? Promise.resolve() : this.#storage.set(K.messages.message(o, k[1]), null)]));
|
|
279
248
|
}
|
|
280
249
|
await Promise.all(maybePromises.filter((v) => v instanceof Promise));
|
|
281
250
|
}
|
|
282
251
|
getMessageChat(messageId) {
|
|
283
|
-
return
|
|
252
|
+
return this.#storage.get(K.messages.messageRef(messageId));
|
|
284
253
|
}
|
|
285
254
|
async getMessage(chatId, messageId) {
|
|
286
255
|
return await this.getTlObject(K.messages.message(chatId, messageId));
|
|
@@ -292,31 +261,32 @@ export class StorageOperations {
|
|
|
292
261
|
setPeer2(chatP, accessHash) {
|
|
293
262
|
this.peers.set([chatP.id], [chatP, accessHash]);
|
|
294
263
|
}
|
|
264
|
+
#accountId = null;
|
|
295
265
|
async getAccountId() {
|
|
296
|
-
if (
|
|
297
|
-
return
|
|
266
|
+
if (this.#accountId !== null) {
|
|
267
|
+
return this.#accountId;
|
|
298
268
|
}
|
|
299
269
|
else {
|
|
300
|
-
return
|
|
270
|
+
return this.#accountId = (await this.auth.get())?.userId ?? null;
|
|
301
271
|
}
|
|
302
272
|
}
|
|
303
273
|
async setIsPremium(isPremium) {
|
|
304
|
-
await
|
|
274
|
+
await this.#storage.set(K.auth.isPremium(), isPremium);
|
|
305
275
|
}
|
|
306
276
|
async getIsPremium() {
|
|
307
|
-
return await
|
|
277
|
+
return await this.#storage.get(K.auth.isPremium());
|
|
308
278
|
}
|
|
309
279
|
async updateStickerSetName(id, accessHash, name) {
|
|
310
|
-
await
|
|
280
|
+
await this.#storage.set(K.cache.stickerSetName(id, accessHash), [name, new Date()]);
|
|
311
281
|
}
|
|
312
282
|
getStickerSetName(id, accessHash) {
|
|
313
|
-
return
|
|
283
|
+
return this.#storage.get(K.cache.stickerSetName(id, accessHash));
|
|
314
284
|
}
|
|
315
285
|
async setServerSalt(serverSalt) {
|
|
316
|
-
await
|
|
286
|
+
await this.#storage.set(K.session.serverSalt(), serverSalt);
|
|
317
287
|
}
|
|
318
288
|
getServerSalt() {
|
|
319
|
-
return
|
|
289
|
+
return this.#storage.get(K.session.serverSalt());
|
|
320
290
|
}
|
|
321
291
|
async getHistory(chatId, offsetId, limit) {
|
|
322
292
|
if (offsetId === 0) {
|
|
@@ -324,7 +294,7 @@ export class StorageOperations {
|
|
|
324
294
|
}
|
|
325
295
|
++limit;
|
|
326
296
|
const messages = new Array();
|
|
327
|
-
for await (const [_, buffer] of await
|
|
297
|
+
for await (const [_, buffer] of await this.#storage.getMany({ start: K.messages.message(chatId, 0), end: K.messages.message(chatId, offsetId) }, { limit, reverse: true })) {
|
|
328
298
|
const message = await this.getTlObject(buffer);
|
|
329
299
|
if ("id" in message && message.id === offsetId) {
|
|
330
300
|
continue;
|
|
@@ -334,18 +304,18 @@ export class StorageOperations {
|
|
|
334
304
|
return messages;
|
|
335
305
|
}
|
|
336
306
|
async getFile(id) {
|
|
337
|
-
if (!
|
|
307
|
+
if (!this.#supportsFiles) {
|
|
338
308
|
return null;
|
|
339
309
|
}
|
|
340
|
-
return await
|
|
310
|
+
return await this.#storage.get(K.cache.file(id));
|
|
341
311
|
}
|
|
342
312
|
async *iterFileParts(id, partCount, offset, signal) {
|
|
343
|
-
if (!
|
|
313
|
+
if (!this.#supportsFiles) {
|
|
344
314
|
return;
|
|
345
315
|
}
|
|
346
316
|
for (let i = offset; i < partCount; i++) {
|
|
347
317
|
signal?.throwIfAborted();
|
|
348
|
-
const part = await
|
|
318
|
+
const part = await this.#storage.get(K.cache.filePart(id, i));
|
|
349
319
|
if (part === null) {
|
|
350
320
|
continue;
|
|
351
321
|
}
|
|
@@ -353,22 +323,22 @@ export class StorageOperations {
|
|
|
353
323
|
}
|
|
354
324
|
}
|
|
355
325
|
async saveFilePart(id, index, bytes) {
|
|
356
|
-
if (!
|
|
326
|
+
if (!this.#supportsFiles) {
|
|
357
327
|
return;
|
|
358
328
|
}
|
|
359
|
-
await
|
|
329
|
+
await this.#storage.set(K.cache.filePart(id, index), bytes);
|
|
360
330
|
}
|
|
361
331
|
async setFilePartCount(id, partCount, chunkSize) {
|
|
362
|
-
if (!
|
|
332
|
+
if (!this.#supportsFiles) {
|
|
363
333
|
return;
|
|
364
334
|
}
|
|
365
|
-
await
|
|
335
|
+
await this.#storage.set(K.cache.file(id), [partCount, chunkSize]);
|
|
366
336
|
}
|
|
367
337
|
async setCustomEmojiDocument(id, document) {
|
|
368
|
-
await
|
|
338
|
+
await this.#storage.set(K.cache.customEmojiDocument(id), [this.#mustSerialize ? rleEncode(Api.serializeObject(document)) : document, new Date()]);
|
|
369
339
|
}
|
|
370
340
|
async getCustomEmojiDocument(id) {
|
|
371
|
-
const v = await
|
|
341
|
+
const v = await this.#storage.get(K.cache.customEmojiDocument(id));
|
|
372
342
|
if (v !== null) {
|
|
373
343
|
return [await this.getTlObject(v[0]), v[1]];
|
|
374
344
|
}
|
|
@@ -377,10 +347,10 @@ export class StorageOperations {
|
|
|
377
347
|
}
|
|
378
348
|
}
|
|
379
349
|
async setBusinessConnection(id, connection) {
|
|
380
|
-
await
|
|
350
|
+
await this.#storage.set(K.cache.businessConnection(id), connection === null ? null : this.#mustSerialize ? rleEncode(Api.serializeObject(connection)) : connection);
|
|
381
351
|
}
|
|
382
352
|
async getBusinessConnection(id) {
|
|
383
|
-
const v = await
|
|
353
|
+
const v = await this.#storage.get(K.cache.businessConnection(id));
|
|
384
354
|
if (v !== null) {
|
|
385
355
|
return await this.getTlObject(v);
|
|
386
356
|
}
|
|
@@ -389,10 +359,10 @@ export class StorageOperations {
|
|
|
389
359
|
}
|
|
390
360
|
}
|
|
391
361
|
async setInlineQueryAnswer(userId, chatId, query, offset, results, date) {
|
|
392
|
-
await
|
|
362
|
+
await this.#storage.set(K.cache.inlineQueryAnswer(userId, chatId, query, offset), [this.#mustSerialize ? rleEncode(Api.serializeObject(results)) : results, date]);
|
|
393
363
|
}
|
|
394
364
|
async getInlineQueryAnswer(userId, chatId, query, offset) {
|
|
395
|
-
const peer_ = await
|
|
365
|
+
const peer_ = await this.#storage.get(K.cache.inlineQueryAnswer(userId, chatId, query, offset));
|
|
396
366
|
if (peer_ !== null) {
|
|
397
367
|
const [obj_, date] = peer_;
|
|
398
368
|
return [Api.as("messages.botResults", await this.getTlObject(obj_)), date];
|
|
@@ -402,10 +372,10 @@ export class StorageOperations {
|
|
|
402
372
|
}
|
|
403
373
|
}
|
|
404
374
|
async setCallbackQueryAnswer(chatId, messageId, question, answer) {
|
|
405
|
-
await
|
|
375
|
+
await this.#storage.set(K.cache.callbackQueryAnswer(chatId, messageId, question), [this.#mustSerialize ? rleEncode(Api.serializeObject(answer)) : answer, new Date()]);
|
|
406
376
|
}
|
|
407
377
|
async getCallbackQueryAnswer(chatId, messageId, question) {
|
|
408
|
-
const peer_ = await
|
|
378
|
+
const peer_ = await this.#storage.get(K.cache.callbackQueryAnswer(chatId, messageId, question));
|
|
409
379
|
if (peer_ !== null) {
|
|
410
380
|
const [obj_, date] = peer_;
|
|
411
381
|
return [Api.as("messages.botCallbackAnswer", await this.getTlObject(obj_)), date];
|
|
@@ -427,23 +397,33 @@ export class StorageOperations {
|
|
|
427
397
|
return await this.getTlObject(K.cache.groupCall(id));
|
|
428
398
|
}
|
|
429
399
|
async setGroupCallAccessHash(id, accessHash) {
|
|
430
|
-
await
|
|
400
|
+
await this.#storage.set(K.cache.groupCallAccessHash(id), accessHash);
|
|
431
401
|
}
|
|
432
402
|
async getGroupCallAccessHash(id) {
|
|
433
|
-
return await
|
|
403
|
+
return await this.#storage.get(K.cache.groupCallAccessHash(id));
|
|
404
|
+
}
|
|
405
|
+
#getUpdateId(update) {
|
|
406
|
+
let id = BigInt(Date.now()) << 32n;
|
|
407
|
+
if ("pts" in update && update.pts) {
|
|
408
|
+
id |= BigInt(update.pts);
|
|
409
|
+
}
|
|
410
|
+
else {
|
|
411
|
+
id |= BigInt(0xffffffffn);
|
|
412
|
+
}
|
|
413
|
+
return id;
|
|
434
414
|
}
|
|
435
415
|
async setUpdate(boxId, update) {
|
|
436
|
-
await this.setTlObject(K.updates.update(boxId,
|
|
416
|
+
await this.setTlObject(K.updates.update(boxId, this.#getUpdateId(update)), update);
|
|
437
417
|
}
|
|
438
418
|
async deleteUpdates() {
|
|
439
419
|
const maybePromises = new Array();
|
|
440
|
-
for await (const [k] of await
|
|
441
|
-
maybePromises.push(
|
|
420
|
+
for await (const [k] of await this.#storage.getMany({ prefix: K.updates.all() })) {
|
|
421
|
+
maybePromises.push(this.#storage.set(k, null));
|
|
442
422
|
}
|
|
443
423
|
await Promise.all(maybePromises.filter((v) => v instanceof Promise));
|
|
444
424
|
}
|
|
445
425
|
async getFirstUpdate(boxId) {
|
|
446
|
-
for await (const [key, update] of await
|
|
426
|
+
for await (const [key, update] of await this.#storage.getMany({ prefix: K.updates.updates(boxId) }, { limit: 1 })) {
|
|
447
427
|
return [key, (await this.getTlObject(update))];
|
|
448
428
|
}
|
|
449
429
|
return null;
|
|
@@ -482,49 +462,49 @@ export class StorageOperations {
|
|
|
482
462
|
}
|
|
483
463
|
}
|
|
484
464
|
async deleteFiles() {
|
|
485
|
-
if (!
|
|
465
|
+
if (!this.#supportsFiles) {
|
|
486
466
|
return;
|
|
487
467
|
}
|
|
488
|
-
for await (const [key] of await
|
|
489
|
-
await
|
|
468
|
+
for await (const [key] of await this.#storage.getMany({ prefix: K.cache.fileParts() })) {
|
|
469
|
+
await this.#storage.set(key, null);
|
|
490
470
|
}
|
|
491
|
-
for await (const [key] of await
|
|
492
|
-
await
|
|
471
|
+
for await (const [key] of await this.#storage.getMany({ prefix: K.cache.files() })) {
|
|
472
|
+
await this.#storage.set(key, null);
|
|
493
473
|
}
|
|
494
474
|
}
|
|
495
475
|
async deleteCustomEmojiDocuments() {
|
|
496
|
-
for await (const [key] of await
|
|
497
|
-
await
|
|
476
|
+
for await (const [key] of await this.#storage.getMany({ prefix: K.cache.customEmojiDocuments() })) {
|
|
477
|
+
await this.#storage.set(key, null);
|
|
498
478
|
}
|
|
499
479
|
}
|
|
500
480
|
async deleteBusinessConnections() {
|
|
501
|
-
for await (const [key] of await
|
|
502
|
-
await
|
|
481
|
+
for await (const [key] of await this.#storage.getMany({ prefix: K.cache.businessConnections() })) {
|
|
482
|
+
await this.#storage.set(key, null);
|
|
503
483
|
}
|
|
504
484
|
}
|
|
505
485
|
async deleteInlineQueryAnswers() {
|
|
506
|
-
for await (const [key] of await
|
|
507
|
-
await
|
|
486
|
+
for await (const [key] of await this.#storage.getMany({ prefix: K.cache.inlineQueryAnswers() })) {
|
|
487
|
+
await this.#storage.set(key, null);
|
|
508
488
|
}
|
|
509
489
|
}
|
|
510
490
|
async deleteCallbackQueryAnswers() {
|
|
511
|
-
for await (const [key] of await
|
|
512
|
-
await
|
|
491
|
+
for await (const [key] of await this.#storage.getMany({ prefix: K.cache.callbackQueryAnswers() })) {
|
|
492
|
+
await this.#storage.set(key, null);
|
|
513
493
|
}
|
|
514
494
|
}
|
|
515
495
|
async deleteFullChats() {
|
|
516
|
-
for await (const [key] of await
|
|
517
|
-
await
|
|
496
|
+
for await (const [key] of await this.#storage.getMany({ prefix: K.cache.fullChats() })) {
|
|
497
|
+
await this.#storage.set(key, null);
|
|
518
498
|
}
|
|
519
499
|
}
|
|
520
500
|
async deleteGroupCalls() {
|
|
521
|
-
for await (const [key] of await
|
|
522
|
-
await
|
|
501
|
+
for await (const [key] of await this.#storage.getMany({ prefix: K.cache.groupCalls() })) {
|
|
502
|
+
await this.#storage.set(key, null);
|
|
523
503
|
}
|
|
524
504
|
}
|
|
525
505
|
async deleteStickerSetNames() {
|
|
526
|
-
for await (const [key] of await
|
|
527
|
-
await
|
|
506
|
+
for await (const [key] of await this.#storage.getMany({ prefix: K.cache.stickerSetNames() })) {
|
|
507
|
+
await this.#storage.set(key, null);
|
|
528
508
|
}
|
|
529
509
|
}
|
|
530
510
|
async clear() {
|
|
@@ -549,8 +529,8 @@ export class StorageOperations {
|
|
|
549
529
|
]);
|
|
550
530
|
}
|
|
551
531
|
async reset() {
|
|
552
|
-
for await (const [key] of await
|
|
553
|
-
await
|
|
532
|
+
for await (const [key] of await this.#storage.getMany({ prefix: [] })) {
|
|
533
|
+
await this.#storage.set(key, null);
|
|
554
534
|
}
|
|
555
535
|
}
|
|
556
536
|
async setPollResults(pollId, pollResults) {
|
|
@@ -561,8 +541,8 @@ export class StorageOperations {
|
|
|
561
541
|
}
|
|
562
542
|
async deletePollResults() {
|
|
563
543
|
const maybePromises = new Array();
|
|
564
|
-
for await (const [key] of await
|
|
565
|
-
maybePromises.push(
|
|
544
|
+
for await (const [key] of await this.#storage.getMany({ prefix: K.cache.pollResults() })) {
|
|
545
|
+
maybePromises.push(this.#storage.set(key, null));
|
|
566
546
|
}
|
|
567
547
|
await Promise.all(maybePromises);
|
|
568
548
|
}
|
|
@@ -574,75 +554,59 @@ export class StorageOperations {
|
|
|
574
554
|
}
|
|
575
555
|
async deletePolls() {
|
|
576
556
|
const maybePromises = new Array();
|
|
577
|
-
for await (const [key] of await
|
|
578
|
-
maybePromises.push(
|
|
557
|
+
for await (const [key] of await this.#storage.getMany({ prefix: K.cache.polls() })) {
|
|
558
|
+
maybePromises.push(this.#storage.set(key, null));
|
|
579
559
|
}
|
|
580
560
|
await Promise.all(maybePromises);
|
|
581
561
|
}
|
|
582
562
|
async setVoiceTranscription(voiceTranscription) {
|
|
583
|
-
await
|
|
563
|
+
await this.#storage.set(K.cache.voiceTranscription(BigInt(voiceTranscription.id)), voiceTranscription);
|
|
584
564
|
}
|
|
585
565
|
async getVoiceTranscription(transcriptionId) {
|
|
586
|
-
return await
|
|
566
|
+
return await this.#storage.get(K.cache.voiceTranscription(transcriptionId));
|
|
587
567
|
}
|
|
588
568
|
async deleteVoiceTranscriptions() {
|
|
589
569
|
const maybePromises = new Array();
|
|
590
|
-
for await (const [key] of await
|
|
591
|
-
maybePromises.push(
|
|
570
|
+
for await (const [key] of await this.#storage.getMany({ prefix: K.cache.voiceTranscriptions() })) {
|
|
571
|
+
maybePromises.push(this.#storage.set(key, null));
|
|
592
572
|
}
|
|
593
573
|
await Promise.all(maybePromises);
|
|
594
574
|
}
|
|
595
575
|
async setVoiceTranscriptionReference(chatId, messageId, messageEditDate, transcriptionId) {
|
|
596
|
-
await
|
|
576
|
+
await this.#storage.set(K.cache.voiceTranscriptionReference(chatId, messageId, messageEditDate.getTime()), transcriptionId);
|
|
597
577
|
}
|
|
598
578
|
async getVoiceTranscriptionReference(chatId, messageId, messageEditDate) {
|
|
599
|
-
return await
|
|
579
|
+
return await this.#storage.get(K.cache.voiceTranscriptionReference(chatId, messageId, messageEditDate.getTime()));
|
|
600
580
|
}
|
|
601
581
|
async deleteVoiceTranscriptionReferences() {
|
|
602
582
|
const maybePromises = new Array();
|
|
603
|
-
for await (const [key] of await
|
|
604
|
-
maybePromises.push(
|
|
583
|
+
for await (const [key] of await this.#storage.getMany({ prefix: K.cache.voiceTranscriptions() })) {
|
|
584
|
+
maybePromises.push(this.#storage.set(key, null));
|
|
605
585
|
}
|
|
606
586
|
await Promise.all(maybePromises);
|
|
607
587
|
}
|
|
608
588
|
}
|
|
609
|
-
_StorageOperations_storage = new WeakMap(), _StorageOperations_supportsFiles = new WeakMap(), _StorageOperations_mustSerialize = new WeakMap(), _StorageOperations_L = new WeakMap(), _StorageOperations_maps = new WeakMap(), _StorageOperations_values = new WeakMap(), _StorageOperations_lastCommit = new WeakMap(), _StorageOperations_accountId = new WeakMap(), _StorageOperations_instances = new WeakSet(), _StorageOperations_addMap = function _StorageOperations_addMap(map) {
|
|
610
|
-
__classPrivateFieldGet(this, _StorageOperations_maps, "f").push(map);
|
|
611
|
-
return map;
|
|
612
|
-
}, _StorageOperations_addValue = function _StorageOperations_addValue(value) {
|
|
613
|
-
__classPrivateFieldGet(this, _StorageOperations_values, "f").push(value);
|
|
614
|
-
return value;
|
|
615
|
-
}, _StorageOperations_getUpdateId = function _StorageOperations_getUpdateId(update) {
|
|
616
|
-
let id = BigInt(Date.now()) << 32n;
|
|
617
|
-
if ("pts" in update && update.pts) {
|
|
618
|
-
id |= BigInt(update.pts);
|
|
619
|
-
}
|
|
620
|
-
else {
|
|
621
|
-
id |= BigInt(0xffffffffn);
|
|
622
|
-
}
|
|
623
|
-
return id;
|
|
624
|
-
};
|
|
625
589
|
class StorageMap {
|
|
590
|
+
#storage;
|
|
591
|
+
#path;
|
|
626
592
|
constructor(storage, path) {
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
_StorageMap_pendingUpdates.set(this, new Map());
|
|
630
|
-
_StorageMap_cache.set(this, new LruCache(20_000));
|
|
631
|
-
__classPrivateFieldSet(this, _StorageMap_storage, storage, "f");
|
|
632
|
-
__classPrivateFieldSet(this, _StorageMap_path, path, "f");
|
|
593
|
+
this.#storage = storage;
|
|
594
|
+
this.#path = path;
|
|
633
595
|
}
|
|
596
|
+
#pendingUpdates = new Map();
|
|
597
|
+
#cache = new LruCache(20_000);
|
|
634
598
|
set(key, value) {
|
|
635
599
|
const key_ = toString(key);
|
|
636
|
-
|
|
637
|
-
if (!
|
|
638
|
-
|
|
600
|
+
this.#cache.set(key_, value);
|
|
601
|
+
if (!this.#storage.isMemory) {
|
|
602
|
+
this.#pendingUpdates.set(key_, value);
|
|
639
603
|
}
|
|
640
604
|
}
|
|
641
605
|
get pendingUpdateCount() {
|
|
642
|
-
return
|
|
606
|
+
return this.#pendingUpdates.size;
|
|
643
607
|
}
|
|
644
608
|
mustGet(key) {
|
|
645
|
-
const value =
|
|
609
|
+
const value = this.#cache.get(toString(key));
|
|
646
610
|
if (value === undefined) {
|
|
647
611
|
unreachable();
|
|
648
612
|
}
|
|
@@ -651,71 +615,67 @@ class StorageMap {
|
|
|
651
615
|
}
|
|
652
616
|
}
|
|
653
617
|
async clear() {
|
|
654
|
-
await awaitablePooledMap(10, await
|
|
618
|
+
await awaitablePooledMap(10, await this.#storage.getMany({ prefix: [this.#path] }), async ([key]) => await this.#storage.set(key, null));
|
|
655
619
|
}
|
|
656
620
|
async get(key) {
|
|
657
621
|
const key_ = toString(key);
|
|
658
|
-
let value =
|
|
622
|
+
let value = this.#cache.get(key_);
|
|
659
623
|
if (value === undefined) {
|
|
660
|
-
value = await
|
|
661
|
-
|
|
624
|
+
value = await this.#storage.get([this.#path, ...key]);
|
|
625
|
+
this.#cache.set(key_, value);
|
|
662
626
|
}
|
|
663
627
|
return value;
|
|
664
628
|
}
|
|
665
629
|
async commit() {
|
|
666
|
-
if (
|
|
630
|
+
if (this.#storage.isMemory) {
|
|
667
631
|
return;
|
|
668
632
|
}
|
|
669
|
-
await awaitablePooledMap(2,
|
|
670
|
-
|
|
633
|
+
await awaitablePooledMap(2, this.#pendingUpdates, async ([key, value]) => await this.#storage.set([this.#path, ...fromString(key)], value));
|
|
634
|
+
this.#pendingUpdates.clear();
|
|
671
635
|
}
|
|
672
636
|
}
|
|
673
|
-
_StorageMap_storage = new WeakMap(), _StorageMap_path = new WeakMap(), _StorageMap_pendingUpdates = new WeakMap(), _StorageMap_cache = new WeakMap();
|
|
674
637
|
class StorageValue {
|
|
638
|
+
#storage;
|
|
639
|
+
#key;
|
|
675
640
|
constructor(storage, path) {
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
_StorageValue_updatePending.set(this, false);
|
|
679
|
-
_StorageValue_value.set(this, void 0);
|
|
680
|
-
__classPrivateFieldSet(this, _StorageValue_storage, storage, "f");
|
|
681
|
-
__classPrivateFieldSet(this, _StorageValue_key, [path], "f");
|
|
641
|
+
this.#storage = storage;
|
|
642
|
+
this.#key = [path];
|
|
682
643
|
}
|
|
644
|
+
#updatePending = false;
|
|
645
|
+
#value;
|
|
683
646
|
set(value) {
|
|
684
|
-
|
|
685
|
-
if (!
|
|
686
|
-
|
|
647
|
+
this.#value = value;
|
|
648
|
+
if (!this.#storage.isMemory) {
|
|
649
|
+
this.#updatePending = true;
|
|
687
650
|
}
|
|
688
651
|
}
|
|
689
652
|
get isUpdatePending() {
|
|
690
|
-
return
|
|
653
|
+
return this.#updatePending;
|
|
691
654
|
}
|
|
692
655
|
mustGet() {
|
|
693
|
-
return
|
|
656
|
+
return this.#value === undefined ? unreachable() : this.#value;
|
|
694
657
|
}
|
|
695
658
|
async get() {
|
|
696
|
-
if (
|
|
697
|
-
|
|
659
|
+
if (this.#value === undefined) {
|
|
660
|
+
this.#value = await this.#storage.get(this.#key);
|
|
698
661
|
}
|
|
699
|
-
return
|
|
662
|
+
return this.#value;
|
|
700
663
|
}
|
|
701
664
|
async commit() {
|
|
702
|
-
if (
|
|
665
|
+
if (this.#storage.isMemory || this.#value === undefined) {
|
|
703
666
|
return;
|
|
704
667
|
}
|
|
705
|
-
await
|
|
706
|
-
|
|
668
|
+
await this.#storage.set(this.#key, this.#value);
|
|
669
|
+
this.#updatePending = false;
|
|
707
670
|
}
|
|
708
671
|
}
|
|
709
|
-
_StorageValue_storage = new WeakMap(), _StorageValue_key = new WeakMap(), _StorageValue_updatePending = new WeakMap(), _StorageValue_value = new WeakMap();
|
|
710
672
|
class StorageAuth extends StorageValue {
|
|
711
673
|
constructor(storage) {
|
|
712
674
|
super(storage, "auth");
|
|
713
|
-
_StorageAuth_instances.add(this);
|
|
714
|
-
_StorageAuth_authKeyId.set(this, null);
|
|
715
675
|
}
|
|
716
676
|
async get() {
|
|
717
677
|
const value = await super.get();
|
|
718
|
-
await
|
|
678
|
+
await this.#resetAuthKeyId(value);
|
|
719
679
|
return value;
|
|
720
680
|
}
|
|
721
681
|
mustGet() {
|
|
@@ -729,22 +689,23 @@ class StorageAuth extends StorageValue {
|
|
|
729
689
|
}
|
|
730
690
|
async set(auth) {
|
|
731
691
|
super.set(auth);
|
|
732
|
-
await
|
|
692
|
+
await this.#resetAuthKeyId(auth);
|
|
733
693
|
}
|
|
734
694
|
async update(fn) {
|
|
735
695
|
const auth = this.mustGet();
|
|
736
696
|
fn(auth);
|
|
737
697
|
await this.set(auth);
|
|
738
698
|
}
|
|
699
|
+
#authKeyId = null;
|
|
700
|
+
async #resetAuthKeyId(auth) {
|
|
701
|
+
if (auth?.authKey) {
|
|
702
|
+
this.#authKeyId = intFromBytes((await sha1(auth.authKey)).subarray(-8));
|
|
703
|
+
}
|
|
704
|
+
else {
|
|
705
|
+
this.#authKeyId = null;
|
|
706
|
+
}
|
|
707
|
+
}
|
|
739
708
|
get authKeyId() {
|
|
740
|
-
return
|
|
709
|
+
return this.#authKeyId;
|
|
741
710
|
}
|
|
742
711
|
}
|
|
743
|
-
_StorageAuth_authKeyId = new WeakMap(), _StorageAuth_instances = new WeakSet(), _StorageAuth_resetAuthKeyId = async function _StorageAuth_resetAuthKeyId(auth) {
|
|
744
|
-
if (auth?.authKey) {
|
|
745
|
-
__classPrivateFieldSet(this, _StorageAuth_authKeyId, intFromBytes((await sha1(auth.authKey)).subarray(-8)), "f");
|
|
746
|
-
}
|
|
747
|
-
else {
|
|
748
|
-
__classPrivateFieldSet(this, _StorageAuth_authKeyId, null, "f");
|
|
749
|
-
}
|
|
750
|
-
};
|