@mtkruto/node 0.0.957 → 0.0.960

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (93) hide show
  1. package/esm/client/0_utilities.d.ts +3 -0
  2. package/esm/client/0_utilities.js +20 -0
  3. package/esm/client/3_client.d.ts +2 -2
  4. package/esm/client/3_client.js +57 -16
  5. package/esm/constants.d.ts +1 -1
  6. package/esm/constants.js +1 -1
  7. package/esm/storage/0_storage.d.ts +16 -17
  8. package/esm/storage/0_storage.js +65 -131
  9. package/esm/storage/0_utilities.d.ts +13 -0
  10. package/esm/storage/0_utilities.js +62 -0
  11. package/esm/storage/1_storage_indexed_db.d.ts +3 -3
  12. package/esm/storage/1_storage_indexed_db.js +4 -3
  13. package/esm/storage/1_storage_local_storage.d.ts +3 -3
  14. package/esm/storage/1_storage_local_storage.js +13 -6
  15. package/esm/storage/1_storage_memory.d.ts +4 -4
  16. package/esm/storage/1_storage_memory.js +4 -2
  17. package/esm/storage/1_storage_session_storage.d.ts +3 -3
  18. package/esm/storage/1_storage_session_storage.js +13 -6
  19. package/esm/types/0_audio.d.ts +9 -0
  20. package/esm/types/0_chat_administrator_rights.d.ts +4 -4
  21. package/esm/types/0_contact.d.ts +6 -0
  22. package/esm/types/0_dice.d.ts +3 -0
  23. package/esm/types/0_force_reply.d.ts +1 -1
  24. package/esm/types/0_keyboard_button_poll_type.d.ts +1 -1
  25. package/esm/types/0_location.d.ts +7 -0
  26. package/esm/types/0_login_url.d.ts +3 -3
  27. package/esm/types/0_mask_position.d.ts +5 -0
  28. package/esm/types/0_message_entity.d.ts +4 -4
  29. package/esm/types/0_reply_keyboard_remove.d.ts +1 -1
  30. package/esm/types/0_thumbnail.d.ts +2 -1
  31. package/esm/types/0_venue.d.ts +6 -0
  32. package/esm/types/0_voice.d.ts +6 -0
  33. package/esm/types/1_animation.d.ts +10 -0
  34. package/esm/types/1_chat.d.ts +18 -18
  35. package/esm/types/1_document.d.ts +7 -0
  36. package/esm/types/1_inline_keyboard_button.d.ts +7 -7
  37. package/esm/types/1_keyboard_button.d.ts +14 -14
  38. package/esm/types/1_photo.d.ts +2 -1
  39. package/esm/types/1_sticker.d.ts +16 -0
  40. package/esm/types/1_user.d.ts +7 -7
  41. package/esm/types/1_video.d.ts +10 -0
  42. package/esm/types/1_video_note.d.ts +8 -0
  43. package/esm/types/2_game.d.ts +7 -0
  44. package/esm/types/2_reply_keyboard_markup.d.ts +5 -5
  45. package/esm/types/3_message.d.ts +57 -25
  46. package/esm/types/3_message.js +0 -1
  47. package/package.json +1 -1
  48. package/script/client/0_utilities.d.ts +3 -0
  49. package/script/client/0_utilities.js +48 -0
  50. package/script/client/3_client.d.ts +2 -2
  51. package/script/client/3_client.js +57 -16
  52. package/script/constants.d.ts +1 -1
  53. package/script/constants.js +1 -1
  54. package/script/storage/0_storage.d.ts +16 -17
  55. package/script/storage/0_storage.js +65 -131
  56. package/script/storage/0_utilities.d.ts +13 -0
  57. package/script/storage/0_utilities.js +68 -0
  58. package/script/storage/1_storage_indexed_db.d.ts +3 -3
  59. package/script/storage/1_storage_indexed_db.js +4 -3
  60. package/script/storage/1_storage_local_storage.d.ts +3 -3
  61. package/script/storage/1_storage_local_storage.js +13 -6
  62. package/script/storage/1_storage_memory.d.ts +4 -4
  63. package/script/storage/1_storage_memory.js +4 -2
  64. package/script/storage/1_storage_session_storage.d.ts +3 -3
  65. package/script/storage/1_storage_session_storage.js +13 -6
  66. package/script/types/0_audio.d.ts +9 -0
  67. package/script/types/0_chat_administrator_rights.d.ts +4 -4
  68. package/script/types/0_contact.d.ts +6 -0
  69. package/script/types/0_dice.d.ts +3 -0
  70. package/script/types/0_force_reply.d.ts +1 -1
  71. package/script/types/0_keyboard_button_poll_type.d.ts +1 -1
  72. package/script/types/0_location.d.ts +7 -0
  73. package/script/types/0_login_url.d.ts +3 -3
  74. package/script/types/0_mask_position.d.ts +5 -0
  75. package/script/types/0_message_entity.d.ts +4 -4
  76. package/script/types/0_reply_keyboard_remove.d.ts +1 -1
  77. package/script/types/0_thumbnail.d.ts +2 -1
  78. package/script/types/0_venue.d.ts +6 -0
  79. package/script/types/0_voice.d.ts +6 -0
  80. package/script/types/1_animation.d.ts +10 -0
  81. package/script/types/1_chat.d.ts +18 -18
  82. package/script/types/1_document.d.ts +7 -0
  83. package/script/types/1_inline_keyboard_button.d.ts +7 -7
  84. package/script/types/1_keyboard_button.d.ts +14 -14
  85. package/script/types/1_photo.d.ts +2 -1
  86. package/script/types/1_sticker.d.ts +16 -0
  87. package/script/types/1_user.d.ts +7 -7
  88. package/script/types/1_video.d.ts +10 -0
  89. package/script/types/1_video_note.d.ts +8 -0
  90. package/script/types/2_game.d.ts +7 -0
  91. package/script/types/2_reply_keyboard_markup.d.ts +5 -5
  92. package/script/types/3_message.d.ts +57 -25
  93. package/script/types/3_message.js +0 -1
@@ -0,0 +1,3 @@
1
+ import * as types from "../tl/2_types.js";
2
+ export declare function getChannelChatId(channelId: bigint): number;
3
+ export declare function peerToChatId(peer: types.TypePeer | types.TypeInputPeer): number;
@@ -0,0 +1,20 @@
1
+ import { ZERO_CHANNEL_ID } from "../constants.js";
2
+ import * as types from "../tl/2_types.js";
3
+ import { UNREACHABLE } from "../utilities/0_control.js";
4
+ export function getChannelChatId(channelId) {
5
+ return ZERO_CHANNEL_ID + -Number(channelId);
6
+ }
7
+ export function peerToChatId(peer) {
8
+ if (peer instanceof types.PeerUser || peer instanceof types.InputPeerUser) {
9
+ return Number(peer.userId);
10
+ }
11
+ else if (peer instanceof types.PeerChat || peer instanceof types.InputPeerChat) {
12
+ return -Number(peer.chatId);
13
+ }
14
+ else if (peer instanceof types.PeerChannel || peer instanceof types.InputPeerChannel) {
15
+ return getChannelChatId(peer.channelId);
16
+ }
17
+ else {
18
+ UNREACHABLE();
19
+ }
20
+ }
@@ -172,11 +172,11 @@ export declare class Client extends ClientAbstract {
172
172
  sendAs?: number | string;
173
173
  replyMarkup?: InlineKeyboardMarkup | ReplyKeyboardMarkup | ReplyKeyboardRemove | ForceReply;
174
174
  }): Promise<Message>;
175
- getMessages(chatId: number | string, messageIds: number[]): Promise<Omit<Message, "replyToMessage">[]>;
175
+ getMessages(chatId_: number | string, messageIds: number[]): Promise<Omit<Message, "replyToMessage">[]>;
176
176
  getMessage(chatId: number | string, messageId: number): Promise<Omit<Message, "replyToMessage"> | null>;
177
177
  private downloadInner;
178
178
  download(fileId_: string): Promise<AsyncGenerator<Uint8Array, void, unknown>>;
179
179
  [getStickerSetName](inputStickerSet: types.InputStickerSetID, hash?: number): Promise<string>;
180
180
  forwardMessages(from: number | string, to: number | string, messageIds: number[], params?: ForwardMessagesParams): Promise<Message[]>;
181
- forwardMessage(from: number | string, to: number | string, messageId: number, params?: ForwardMessagesParams): Promise<Message[]>;
181
+ forwardMessage(from: number | string, to: number | string, messageId: number, params?: ForwardMessagesParams): Promise<Message>;
182
182
  }
@@ -23,6 +23,7 @@ import { parseHtml } from "./0_html.js";
23
23
  import { checkPassword } from "./0_password.js";
24
24
  import { ClientAbstract } from "./1_client_abstract.js";
25
25
  import { ClientPlain } from "./2_client_plain.js";
26
+ import { getChannelChatId, peerToChatId } from "./0_utilities.js";
26
27
  const d = debug("Client");
27
28
  const dGap = debug("Client/recoverUpdateGap");
28
29
  const dGapC = debug("Client/recoverChannelUpdateGap");
@@ -507,7 +508,7 @@ export class Client extends ClientAbstract {
507
508
  this.promises.delete(messageId);
508
509
  }
509
510
  };
510
- if (result instanceof types.Updates || result instanceof types.TypeUpdate) {
511
+ if (result instanceof types.TypeUpdates || result instanceof types.TypeUpdate) {
511
512
  this.processUpdates(result).then(resolvePromise);
512
513
  }
513
514
  else {
@@ -663,6 +664,24 @@ export class Client extends ClientAbstract {
663
664
  d("applied update with pts %d", update.pts);
664
665
  await this.storage.setChannelPts(channelId, update.pts);
665
666
  }
667
+ if (update instanceof types.UpdateNewMessage || update instanceof types.UpdateNewMessage || update instanceof types.UpdateNewChannelMessage || update instanceof types.UpdateNewChannelMessage) {
668
+ if (update.message instanceof types.Message || update.message instanceof types.MessageService) {
669
+ await this.storage.setMessage(peerToChatId(update.message.peerId), update.message.id, update.message);
670
+ }
671
+ }
672
+ else if (update instanceof types.UpdateDeleteChannelMessages) {
673
+ for (const message of update.messages) {
674
+ await this.storage.setMessage(getChannelChatId(update.channelId), message, null);
675
+ }
676
+ }
677
+ else if (update instanceof types.UpdateDeleteMessages) {
678
+ for (const message of update.messages) {
679
+ const chatId = await this.storage.getMessageChat(message);
680
+ if (chatId) {
681
+ await this.storage.setMessage(chatId, message, null);
682
+ }
683
+ }
684
+ }
666
685
  // apply update (call listeners)
667
686
  this.updateHandler?.(this, update);
668
687
  }
@@ -1040,6 +1059,13 @@ export class Client extends ClientAbstract {
1040
1059
  await this.processChats(result.chats);
1041
1060
  await this.processUsers(result.users);
1042
1061
  }
1062
+ if (result instanceof types.MessagesMessages) {
1063
+ for (const message of result.messages) {
1064
+ if (message instanceof types.Message || message instanceof types.MessageService) {
1065
+ await this.storage.setMessage(peerToChatId(message.peerId), message.id, message);
1066
+ }
1067
+ }
1068
+ }
1043
1069
  }
1044
1070
  async updatesToMessages(chatId, updates) {
1045
1071
  const messages = new Array();
@@ -1121,22 +1147,37 @@ export class Client extends ClientAbstract {
1121
1147
  }));
1122
1148
  return await this.updatesToMessages(chatId, result).then((v) => v[0]);
1123
1149
  }
1124
- async getMessages(chatId, messageIds) {
1125
- const peer = await this.getInputPeer(chatId);
1126
- let messages_;
1127
- if (peer instanceof types.InputPeerChannel) {
1128
- messages_ = await this.invoke(new functions.ChannelsGetMessages({
1129
- channel: new types.InputChannel({ channelId: peer.channelId, accessHash: peer.accessHash }),
1130
- id: messageIds.map((v) => new types.InputMessageID({ id: v })),
1131
- })).then((v) => v[as](types.MessagesChannelMessages));
1150
+ async getMessages(chatId_, messageIds) {
1151
+ const peer = await this.getInputPeer(chatId_);
1152
+ let messages_ = new Array();
1153
+ const chatId = peerToChatId(peer);
1154
+ let shouldFetch = false;
1155
+ for (const messageId of messageIds) {
1156
+ const message = await this.storage.getMessage(chatId, messageId);
1157
+ if (message == null) {
1158
+ messages_ = [];
1159
+ shouldFetch = true;
1160
+ break;
1161
+ }
1162
+ else {
1163
+ messages_.push(message);
1164
+ }
1132
1165
  }
1133
- else {
1134
- messages_ = await this.invoke(new functions.MessagesGetMessages({
1135
- id: messageIds.map((v) => new types.InputMessageID({ id: v })),
1136
- })).then((v) => v[as](types.MessagesMessages));
1166
+ if (shouldFetch) {
1167
+ if (peer instanceof types.InputPeerChannel) {
1168
+ messages_ = await this.invoke(new functions.ChannelsGetMessages({
1169
+ channel: new types.InputChannel({ channelId: peer.channelId, accessHash: peer.accessHash }),
1170
+ id: messageIds.map((v) => new types.InputMessageID({ id: v })),
1171
+ })).then((v) => v[as](types.MessagesChannelMessages).messages);
1172
+ }
1173
+ else {
1174
+ messages_ = await this.invoke(new functions.MessagesGetMessages({
1175
+ id: messageIds.map((v) => new types.InputMessageID({ id: v })),
1176
+ })).then((v) => v[as](types.MessagesMessages).messages);
1177
+ }
1137
1178
  }
1138
1179
  const messages = new Array();
1139
- for (const message_ of messages_.messages) {
1180
+ for (const message_ of messages_) {
1140
1181
  messages.push(await constructMessage(message_, this[getEntity].bind(this), null, this[getStickerSetName].bind(this)));
1141
1182
  }
1142
1183
  return messages;
@@ -1236,7 +1277,7 @@ export class Client extends ClientAbstract {
1236
1277
  }));
1237
1278
  return await this.updatesToMessages(to, result);
1238
1279
  }
1239
- forwardMessage(from, to, messageId, params) {
1240
- return this.forwardMessages(from, to, [messageId], params);
1280
+ async forwardMessage(from, to, messageId, params) {
1281
+ return await this.forwardMessages(from, to, [messageId], params).then((v) => v[0]);
1241
1282
  }
1242
1283
  }
@@ -4,7 +4,7 @@ export declare const publicKeys: Map<bigint, [bigint, bigint]>;
4
4
  export declare const VECTOR_CONSTRUCTOR = 481674261;
5
5
  export declare const DEFAULT_INITIAL_DC: DC;
6
6
  export declare const LAYER = 158;
7
- export declare const DEFAULT_APP_VERSION = "MTKruto 0.0.957";
7
+ export declare const DEFAULT_APP_VERSION = "MTKruto 0.0.960";
8
8
  export declare const DEFAULT_DEVICE_MODEL: string;
9
9
  export declare const DEFAULT_LANG_CODE: string;
10
10
  export declare const DEFAULT_LANG_PACK = "";
package/esm/constants.js CHANGED
@@ -62,7 +62,7 @@ export const publicKeys = new Map([
62
62
  export const VECTOR_CONSTRUCTOR = 0x1CB5C415;
63
63
  export const DEFAULT_INITIAL_DC = "2-test";
64
64
  export const LAYER = 158;
65
- export const DEFAULT_APP_VERSION = "MTKruto 0.0.957";
65
+ export const DEFAULT_APP_VERSION = "MTKruto 0.0.960";
66
66
  // @ts-ignore: lib
67
67
  export const DEFAULT_DEVICE_MODEL = typeof dntShim.Deno === "undefined" ? typeof navigator === "undefined" ? typeof process === "undefined" ? "Unknown" : process.platform + "-" + process.arch : navigator.userAgent.split(" ")[0] : dntShim.Deno.build.os + "-" + dntShim.Deno.build.arch;
68
68
  export const DEFAULT_LANG_CODE = typeof navigator === "undefined" ? "en" : navigator.language.split("-")[0];
@@ -1,33 +1,34 @@
1
1
  import { MaybePromise } from "../utilities/0_types.js";
2
2
  import { DC } from "../transport/2_transport_provider.js";
3
+ import { TLObject } from "../tl/1_tl_object.js";
3
4
  import * as types from "../tl/2_types.js";
5
+ export type StorageKeyPart = string | number | bigint | Uint8Array;
4
6
  export declare abstract class Storage {
5
7
  private _authKeyId;
6
8
  abstract init(): MaybePromise<void>;
7
- abstract set(key: string, value: string | null): MaybePromise<void>;
8
- abstract get(key: string): MaybePromise<string | null>;
9
+ abstract set(key: readonly StorageKeyPart[], value: unknown): MaybePromise<void>;
10
+ abstract get<T>(key: readonly StorageKeyPart[]): MaybePromise<T | null>;
9
11
  setDc(dc: DC | null): MaybePromise<void>;
10
- getDc(): Promise<DC | null>;
12
+ getDc(): MaybePromise<DC | null>;
11
13
  private resetAuthKeyId;
12
14
  getAuthKey(): Promise<Uint8Array | null>;
13
15
  setAuthKey(authKey: Uint8Array | null): Promise<void>;
14
16
  get authKeyId(): bigint | null;
15
- private readonly channelAccessHash__;
16
17
  setChannelAccessHash(id: bigint, accessHash: bigint): MaybePromise<void>;
17
- getChannelAccessHash(id: bigint): Promise<bigint | null>;
18
- private readonly userAccessHash__;
18
+ getChannelAccessHash(id: bigint): MaybePromise<bigint | null>;
19
19
  setUserAccessHash(id: bigint, accessHash: bigint): MaybePromise<void>;
20
- getUserAccessHash(id: bigint): Promise<bigint | null>;
21
- private readonly username__;
20
+ getUserAccessHash(id: bigint): MaybePromise<bigint | null>;
22
21
  updateUsernames(type: "user" | "channel", id: bigint, usernames: string[]): Promise<void>;
23
- getUsername(username: string): Promise<readonly ["channel" | "user", bigint, Date] | null>;
24
- private readonly state__;
22
+ getUsername(username: string): MaybePromise<["channel" | "user", bigint, Date] | null>;
23
+ setTlObject(key: readonly StorageKeyPart[], value: TLObject | null): Promise<void>;
24
+ getTLObject(key: readonly StorageKeyPart[]): Promise<import("../tl/3_tl_reader.js").ReadObject | null>;
25
25
  setState(state: types.UpdatesState): Promise<void>;
26
26
  getState(): Promise<types.UpdatesState | null>;
27
- private readonly channelPts__;
27
+ setMessage(chatId: number, messageId: number, message: types.TypeMessage | null): Promise<void>;
28
+ getMessageChat(messageId: number): MaybePromise<number | null>;
29
+ getMessage(chatId: number, messageId: number): Promise<types.TypeMessage | null>;
28
30
  setChannelPts(channelId: bigint, pts: number): Promise<void>;
29
- getChannelPts(channelId: bigint): Promise<number | null>;
30
- private readonly peer__;
31
+ getChannelPts(channelId: bigint): MaybePromise<number | null>;
31
32
  setEntity(peer: types.Channel): Promise<void>;
32
33
  setEntity(peer: types.Chat): Promise<void>;
33
34
  setEntity(peer: types.User): Promise<void>;
@@ -35,10 +36,8 @@ export declare abstract class Storage {
35
36
  getEntity(type: "chat", id: bigint): Promise<types.Chat | null>;
36
37
  getEntity(type: "user", id: bigint): Promise<types.User | null>;
37
38
  getEntity(type: "channel" | "chat" | "user", id: bigint): Promise<types.Channel | types.Chat | types.User | null>;
38
- private readonly accountType__;
39
39
  setAccountType(type: "user" | "bot"): Promise<void>;
40
- getAccountType(): Promise<"bot" | "user">;
41
- private readonly stickerSetName__;
40
+ getAccountType(): MaybePromise<"bot" | "user" | null>;
42
41
  updateStickerSetName(id: bigint, accessHash: bigint, name: string): Promise<void>;
43
- getStickerSetName(id: bigint, accessHash: bigint): Promise<[string, Date] | null>;
42
+ getStickerSetName(id: bigint, accessHash: bigint): MaybePromise<[string, Date] | null>;
44
43
  }
@@ -1,4 +1,3 @@
1
- import { base64Decode, base64Encode } from "../deps.js";
2
1
  import { UNREACHABLE } from "../utilities/0_control.js";
3
2
  import { sha1 } from "../utilities/0_hash.js";
4
3
  import { bigIntFromBuffer } from "../utilities/0_bigint.js";
@@ -6,6 +5,19 @@ import { serialize } from "../tl/1_tl_object.js";
6
5
  import * as types from "../tl/2_types.js";
7
6
  import { TLReader } from "../tl/3_tl_reader.js";
8
7
  import { rleDecode, rleEncode } from "../utilities/0_rle.js";
8
+ import { ZERO_CHANNEL_ID } from "../constants.js";
9
+ const KPARTS__DC = ["dc"];
10
+ const KPARTS__AUTH_KEY = ["authKey"];
11
+ const KPARTS__CHANNEL_ACCESS_HASH = (v) => ["channelAccessHash", v];
12
+ const KPARTS__USER_ACCESS_HASH = (v) => ["userAccessHash", v];
13
+ const KPARTS__USERNAME = (v) => ["username", v];
14
+ const KPARTS__STATE = ["state"];
15
+ const KPARTS__CHANNEL_PTS = (v) => ["channelPts", v];
16
+ const KPARTS__PEER = (type, id) => ["peer", type, id];
17
+ const KPARTS__ACCOUNT_TYPE = ["accountType"];
18
+ const KPARTS__STICKER_SET_NAME = (id, accessHash) => ["stickerSetName", id, accessHash];
19
+ const KPARTS_MESSAGE = (chatId, messageId) => ["messages", chatId, messageId];
20
+ const KPARTS_MESSAGE_REF = (messageId) => ["messageRefs", messageId];
9
21
  export class Storage {
10
22
  constructor() {
11
23
  Object.defineProperty(this, "_authKeyId", {
@@ -14,60 +26,12 @@ export class Storage {
14
26
  writable: true,
15
27
  value: null
16
28
  });
17
- Object.defineProperty(this, "channelAccessHash__", {
18
- enumerable: true,
19
- configurable: true,
20
- writable: true,
21
- value: "channelAccessHash__"
22
- });
23
- Object.defineProperty(this, "userAccessHash__", {
24
- enumerable: true,
25
- configurable: true,
26
- writable: true,
27
- value: "userAccessHash__"
28
- });
29
- Object.defineProperty(this, "username__", {
30
- enumerable: true,
31
- configurable: true,
32
- writable: true,
33
- value: "username__"
34
- });
35
- Object.defineProperty(this, "state__", {
36
- enumerable: true,
37
- configurable: true,
38
- writable: true,
39
- value: "state__"
40
- });
41
- Object.defineProperty(this, "channelPts__", {
42
- enumerable: true,
43
- configurable: true,
44
- writable: true,
45
- value: "channelPts__"
46
- });
47
- Object.defineProperty(this, "peer__", {
48
- enumerable: true,
49
- configurable: true,
50
- writable: true,
51
- value: "peer__"
52
- });
53
- Object.defineProperty(this, "accountType__", {
54
- enumerable: true,
55
- configurable: true,
56
- writable: true,
57
- value: "accountType__"
58
- });
59
- Object.defineProperty(this, "stickerSetName__", {
60
- enumerable: true,
61
- configurable: true,
62
- writable: true,
63
- value: "stickerSetName__"
64
- });
65
29
  }
66
30
  setDc(dc) {
67
- return this.set("dc", dc);
31
+ return this.set(KPARTS__DC, dc);
68
32
  }
69
- async getDc() {
70
- return await this.get("dc");
33
+ getDc() {
34
+ return this.get(KPARTS__DC);
71
35
  }
72
36
  async resetAuthKeyId(authKey) {
73
37
  if (authKey != null) {
@@ -78,105 +42,88 @@ export class Storage {
78
42
  }
79
43
  }
80
44
  async getAuthKey() {
81
- const authKey_ = await this.get("authKey");
82
- const authKey = authKey_ == null ? null : new Uint8Array(authKey_.split(/([0-9a-f]{2})/).filter((v) => v).map((v) => parseInt(v, 16)));
45
+ const authKey = await this.get(KPARTS__AUTH_KEY);
83
46
  await this.resetAuthKeyId(authKey);
84
47
  return authKey;
85
48
  }
86
49
  async setAuthKey(authKey) {
87
- await this.set("authKey", authKey == null ? null : Array.from(authKey).map((v) => v.toString(16)).map((v) => v.padStart(2, "0")).join(""));
50
+ await this.set(KPARTS__AUTH_KEY, authKey);
88
51
  await this.resetAuthKeyId(authKey);
89
52
  }
90
53
  get authKeyId() {
91
54
  return this._authKeyId;
92
55
  }
93
56
  setChannelAccessHash(id, accessHash) {
94
- return this.set(`${this.channelAccessHash__}${id}`, String(accessHash));
57
+ return this.set(KPARTS__CHANNEL_ACCESS_HASH(id), accessHash);
95
58
  }
96
- async getChannelAccessHash(id) {
97
- const accessHash = await this.get(`${this.channelAccessHash__}${id}`);
98
- if (accessHash != null) {
99
- return BigInt(accessHash);
100
- }
101
- else {
102
- return null;
103
- }
59
+ getChannelAccessHash(id) {
60
+ return this.get(KPARTS__CHANNEL_ACCESS_HASH(id));
104
61
  }
105
62
  setUserAccessHash(id, accessHash) {
106
- return this.set(`${this.userAccessHash__}${id}`, String(accessHash));
63
+ return this.set(KPARTS__USER_ACCESS_HASH(id), accessHash);
107
64
  }
108
- async getUserAccessHash(id) {
109
- const accessHash = await this.get(`${this.userAccessHash__}${id}`);
110
- if (accessHash != null) {
111
- return BigInt(accessHash);
112
- }
113
- else {
114
- return null;
115
- }
65
+ getUserAccessHash(id) {
66
+ return this.get(KPARTS__USER_ACCESS_HASH(id));
116
67
  }
117
68
  async updateUsernames(type, id, usernames) {
118
69
  for (let username of usernames) {
119
70
  username = username.toLowerCase();
120
- await this.set(`${this.username__}${username}`, JSON.stringify([type, String(id), new Date()]));
71
+ await this.set(KPARTS__USERNAME(username), [type, String(id), new Date()]);
121
72
  }
122
73
  }
123
- async getUsername(username) {
74
+ getUsername(username) {
124
75
  username = username.toLowerCase();
125
- const username_ = await this.get(`${this.username__}${username}`);
126
- if (username_ != null) {
127
- const [type, id, updatedAt] = JSON.parse(username_);
128
- return [type, BigInt(id), new Date(updatedAt)];
76
+ return this.get(KPARTS__USERNAME(username));
77
+ }
78
+ async setTlObject(key, value) {
79
+ if (value == null) {
80
+ await this.set(key, null);
81
+ }
82
+ else {
83
+ await this.set(key, rleEncode(value[serialize]()));
84
+ }
85
+ }
86
+ async getTLObject(key) {
87
+ const buffer = await this.get(key);
88
+ if (buffer != null) {
89
+ return new TLReader(rleDecode(buffer)).readObject();
129
90
  }
130
91
  else {
131
92
  return null;
132
93
  }
133
94
  }
134
95
  async setState(state) {
135
- await this.set(this.state__, JSON.stringify({
136
- date: state.date,
137
- pts: state.pts,
138
- qts: state.qts,
139
- seq: state.seq,
140
- unreadCount: state.unreadCount,
141
- }));
96
+ await this.setTlObject(KPARTS__STATE, state);
142
97
  }
143
98
  async getState() {
144
- const state__ = await this.get(this.state__);
145
- if (state__ != null) {
146
- const state_ = JSON.parse(state__);
147
- return new types.UpdatesState({
148
- date: state_.date,
149
- pts: state_.pts,
150
- qts: state_.qts,
151
- seq: state_.seq,
152
- unreadCount: state_.unreadCount,
153
- });
154
- }
155
- else {
156
- return null;
99
+ return await this.getTLObject(KPARTS__STATE);
100
+ }
101
+ async setMessage(chatId, messageId, message) {
102
+ if (chatId > ZERO_CHANNEL_ID) {
103
+ await this.set(KPARTS_MESSAGE_REF(messageId), message == null ? null : chatId);
157
104
  }
105
+ await this.setTlObject(KPARTS_MESSAGE(chatId, messageId), message);
106
+ }
107
+ getMessageChat(messageId) {
108
+ return this.get(KPARTS_MESSAGE_REF(messageId));
109
+ }
110
+ async getMessage(chatId, messageId) {
111
+ return await this.getTLObject(KPARTS_MESSAGE(chatId, messageId));
158
112
  }
159
113
  async setChannelPts(channelId, pts) {
160
- await this.set(`${this.channelPts__}${channelId}`, String(pts));
114
+ await this.set(KPARTS__CHANNEL_PTS(channelId), pts);
161
115
  }
162
- async getChannelPts(channelId) {
163
- const pts = await this.get(`${this.channelPts__}${channelId}`);
164
- if (pts != null) {
165
- return Number(pts);
166
- }
167
- else {
168
- return null;
169
- }
116
+ getChannelPts(channelId) {
117
+ return this.get(KPARTS__CHANNEL_PTS(channelId));
170
118
  }
171
119
  async setEntity(peer) {
172
120
  const type = peer instanceof types.Channel ? "channel" : peer instanceof types.Chat ? "chat" : peer instanceof types.User ? "user" : UNREACHABLE();
173
- await this.set(`${this.peer__}${type}${peer.id}`, base64Encode(rleEncode(peer[serialize]())));
121
+ await this.set(KPARTS__PEER(type, peer.id), peer[serialize]());
174
122
  }
175
123
  async getEntity(type, id) {
176
- const peer_ = await this.get(`${this.peer__}${type}${id}`);
124
+ const peer_ = await this.get(KPARTS__PEER(type, id));
177
125
  if (peer_ != null) {
178
- const reader = new TLReader(rleDecode(base64Decode(peer_)));
179
- return reader.readObject();
126
+ return new TLReader(peer_).readObject();
180
127
  }
181
128
  else {
182
129
  return null;
@@ -192,30 +139,17 @@ export class Storage {
192
139
  throw err;
193
140
  }
194
141
  else {
195
- await this.set(this.accountType__, type);
142
+ await this.set(KPARTS__ACCOUNT_TYPE, type);
196
143
  }
197
144
  }
198
145
  }
199
- async getAccountType() {
200
- const accountType = await this.get(this.accountType__);
201
- if (accountType != null) {
202
- return accountType;
203
- }
204
- else {
205
- UNREACHABLE();
206
- }
146
+ getAccountType() {
147
+ return this.get(KPARTS__ACCOUNT_TYPE);
207
148
  }
208
149
  async updateStickerSetName(id, accessHash, name) {
209
- await this.set(`${this.stickerSetName__}${id}${accessHash}`, JSON.stringify([name, new Date()]));
150
+ await this.set(KPARTS__STICKER_SET_NAME(id, accessHash), [name, new Date()]);
210
151
  }
211
- async getStickerSetName(id, accessHash) {
212
- const stickerSetName_ = await this.get(`${this.stickerSetName__}${id}${accessHash}`);
213
- if (stickerSetName_ != null) {
214
- const [name, updatedAt] = JSON.parse(stickerSetName_);
215
- return [name, new Date(updatedAt)];
216
- }
217
- else {
218
- return null;
219
- }
152
+ getStickerSetName(id, accessHash) {
153
+ return this.get(KPARTS__STICKER_SET_NAME(id, accessHash));
220
154
  }
221
155
  }
@@ -0,0 +1,13 @@
1
+ import { StorageKeyPart } from "./0_storage.js";
2
+ export declare enum ValueType {
3
+ Boolean = 0,
4
+ Number = 1,
5
+ String = 2,
6
+ BigInt = 3,
7
+ Date = 4,
8
+ Uint8Array = 5,
9
+ Array = 6
10
+ }
11
+ export declare function toString(value: unknown): string;
12
+ export declare function fromString<T>(string: string): any;
13
+ export declare function fixKey(key: StorageKeyPart[]): (string | number | Uint8Array)[];
@@ -0,0 +1,62 @@
1
+ import { base64Decode, base64Encode } from "../deps.js";
2
+ import { UNREACHABLE } from "../utilities/0_control.js";
3
+ export var ValueType;
4
+ (function (ValueType) {
5
+ ValueType[ValueType["Boolean"] = 0] = "Boolean";
6
+ ValueType[ValueType["Number"] = 1] = "Number";
7
+ ValueType[ValueType["String"] = 2] = "String";
8
+ ValueType[ValueType["BigInt"] = 3] = "BigInt";
9
+ ValueType[ValueType["Date"] = 4] = "Date";
10
+ ValueType[ValueType["Uint8Array"] = 5] = "Uint8Array";
11
+ ValueType[ValueType["Array"] = 6] = "Array";
12
+ })(ValueType || (ValueType = {}));
13
+ export function toString(value) {
14
+ if (typeof value === "boolean") {
15
+ return JSON.stringify([ValueType.Boolean, value]);
16
+ }
17
+ else if (typeof value === "number") {
18
+ return JSON.stringify([ValueType.Number, value]);
19
+ }
20
+ else if (typeof value === "string") {
21
+ return JSON.stringify([ValueType.String, value]);
22
+ }
23
+ else if (typeof value == "bigint") {
24
+ return JSON.stringify([ValueType.BigInt, String(value)]);
25
+ }
26
+ else if (value instanceof Date) {
27
+ return JSON.stringify([ValueType.Date, value.getTime()]);
28
+ }
29
+ else if (value instanceof Uint8Array) {
30
+ return JSON.stringify([ValueType.Uint8Array, base64Encode(value)]);
31
+ }
32
+ else if (Array.isArray(value)) {
33
+ return JSON.stringify([ValueType.Array, value.map(toString)]);
34
+ }
35
+ else {
36
+ UNREACHABLE();
37
+ }
38
+ }
39
+ export function fromString(string) {
40
+ const [type, value] = JSON.parse(string);
41
+ if (type == ValueType.Boolean || type == ValueType.Number || type == ValueType.String) {
42
+ return value;
43
+ }
44
+ else if (type == ValueType.BigInt) {
45
+ return BigInt(value);
46
+ }
47
+ else if (type == ValueType.Date) {
48
+ return new Date(value);
49
+ }
50
+ else if (type == ValueType.Uint8Array) {
51
+ return base64Decode(value);
52
+ }
53
+ else if (type == ValueType.Array) {
54
+ return value.map(fromString);
55
+ }
56
+ else {
57
+ UNREACHABLE();
58
+ }
59
+ }
60
+ export function fixKey(key) {
61
+ return key.map((v) => typeof v === "bigint" ? String(v) : v);
62
+ }
@@ -1,9 +1,9 @@
1
- import { Storage } from "./0_storage.js";
1
+ import { Storage, StorageKeyPart } from "./0_storage.js";
2
2
  export declare class StorageIndexedDB extends Storage {
3
3
  readonly name: string;
4
4
  database: IDBDatabase | null;
5
5
  constructor(name: string);
6
6
  init(): Promise<void>;
7
- set(k: string, v: string | null): Promise<void>;
8
- get(k: string): Promise<string | null>;
7
+ set(k: StorageKeyPart[], v: unknown): Promise<void>;
8
+ get<T>(k: StorageKeyPart[]): Promise<T | null>;
9
9
  }
@@ -1,4 +1,5 @@
1
1
  import { Storage } from "./0_storage.js";
2
+ import { fixKey } from "./0_utilities.js";
2
3
  const VERSION = 1;
3
4
  const KV_OBJECT_STORE = "kv";
4
5
  export class StorageIndexedDB extends Storage {
@@ -44,10 +45,10 @@ export class StorageIndexedDB extends Storage {
44
45
  // deno-lint-ignore no-explicit-any
45
46
  let tx;
46
47
  if (v == null) {
47
- tx = store.delete(k);
48
+ tx = store.delete(fixKey(k));
48
49
  }
49
50
  else {
50
- tx = store.put(v, k);
51
+ tx = store.put(v, fixKey(k));
51
52
  }
52
53
  return new Promise((res, rej) => {
53
54
  tx.onerror = rej;
@@ -63,7 +64,7 @@ export class StorageIndexedDB extends Storage {
63
64
  const tx = this.database
64
65
  .transaction(KV_OBJECT_STORE, "readonly")
65
66
  .objectStore(KV_OBJECT_STORE)
66
- .get(k);
67
+ .get(fixKey(k));
67
68
  return new Promise((res, rej) => {
68
69
  tx.onerror = rej;
69
70
  tx.onsuccess = () => {
@@ -1,9 +1,9 @@
1
1
  import { MaybePromise } from "../utilities/0_types.js";
2
- import { Storage } from "./0_storage.js";
2
+ import { Storage, StorageKeyPart } from "./0_storage.js";
3
3
  export declare class StorageLocalStorage extends Storage implements Storage {
4
4
  private readonly prefix;
5
5
  constructor(prefix: string);
6
6
  init(): void;
7
- get(key: string): string | null;
8
- set(key: string, value: string | null): MaybePromise<void>;
7
+ get(key_: readonly StorageKeyPart[]): any;
8
+ set(key_: readonly StorageKeyPart[], value: unknown): MaybePromise<void>;
9
9
  }