@mtkruto/node 0.1.500 → 0.1.700

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 (179) hide show
  1. package/esm/3_types.d.ts +6 -1
  2. package/esm/3_types.js +6 -1
  3. package/esm/4_errors.js +2 -4
  4. package/esm/5_client.d.ts +1 -1
  5. package/esm/5_client.js +1 -1
  6. package/esm/client/0_params.d.ts +33 -7
  7. package/esm/client/0_storage_operations.d.ts +182 -0
  8. package/esm/client/0_storage_operations.js +611 -0
  9. package/esm/client/1_composer.d.ts +2 -0
  10. package/esm/client/1_composer.js +28 -0
  11. package/{script/client/0_types.d.ts → esm/client/1_types.d.ts} +3 -3
  12. package/{script/client/1_account_manager.d.ts → esm/client/2_account_manager.d.ts} +1 -1
  13. package/{script/client/1_bot_info_manager.d.ts → esm/client/2_bot_info_manager.d.ts} +1 -1
  14. package/esm/client/{1_business_connection_manager.d.ts → 2_business_connection_manager.d.ts} +1 -1
  15. package/{script/client/1_file_manager.d.ts → esm/client/2_file_manager.d.ts} +6 -2
  16. package/esm/client/{1_file_manager.js → 2_file_manager.js} +55 -52
  17. package/esm/client/{1_network_statistics_manager.d.ts → 2_network_statistics_manager.d.ts} +1 -1
  18. package/{script/client/1_reaction_manager.d.ts → esm/client/2_reaction_manager.d.ts} +1 -1
  19. package/{script/client/1_update_manager.d.ts → esm/client/2_update_manager.d.ts} +1 -1
  20. package/esm/client/{2_message_manager.d.ts → 3_message_manager.d.ts} +2 -2
  21. package/esm/client/3_video_chat_manager.d.ts +43 -0
  22. package/esm/client/3_video_chat_manager.js +194 -0
  23. package/esm/client/{3_callback_query_manager.d.ts → 4_callback_query_manager.d.ts} +2 -2
  24. package/esm/client/{3_callback_query_manager.js → 4_callback_query_manager.js} +1 -0
  25. package/{script/client/3_chat_list_manager.d.ts → esm/client/4_chat_list_manager.d.ts} +6 -6
  26. package/esm/client/{3_chat_list_manager.js → 4_chat_list_manager.js} +75 -98
  27. package/esm/client/{3_inline_query_manager.d.ts → 4_inline_query_manager.d.ts} +2 -2
  28. package/esm/client/{3_inline_query_manager.js → 4_inline_query_manager.js} +1 -0
  29. package/esm/client/{3_story_manager.d.ts → 4_story_manager.d.ts} +3 -3
  30. package/{script/client/4_client.d.ts → esm/client/5_client.d.ts} +105 -31
  31. package/esm/client/{4_client.js → 5_client.js} +172 -73
  32. package/esm/connection/0_connection.d.ts +6 -15
  33. package/esm/connection/0_connection.js +1 -38
  34. package/esm/connection/1_connection_web_socket.d.ts +3 -2
  35. package/esm/connection/1_connection_web_socket.js +7 -3
  36. package/esm/mod.d.ts +1 -1
  37. package/esm/mod.js +1 -1
  38. package/esm/storage/0_storage.d.ts +9 -145
  39. package/esm/storage/0_storage.js +1 -526
  40. package/esm/storage/2_storage_indexed_db.d.ts +3 -2
  41. package/esm/storage/2_storage_indexed_db.js +9 -8
  42. package/esm/storage/2_storage_local_storage.d.ts +3 -2
  43. package/esm/storage/2_storage_local_storage.js +5 -4
  44. package/esm/storage/2_storage_memory.d.ts +4 -4
  45. package/esm/storage/2_storage_memory.js +12 -19
  46. package/esm/storage/2_storage_session_storage.d.ts +2 -1
  47. package/esm/storage/2_storage_session_storage.js +5 -4
  48. package/esm/tl/3_utilities.d.ts +1 -1
  49. package/esm/tl/3_utilities.js +3 -3
  50. package/esm/types/0_birthday.d.ts +27 -0
  51. package/esm/types/0_birthday.js +26 -0
  52. package/esm/types/0_giveaway_parameters.d.ts +1 -1
  53. package/esm/types/0_id.d.ts +1 -1
  54. package/esm/types/0_live_stream_channel.d.ts +30 -0
  55. package/esm/types/0_live_stream_channel.js +26 -0
  56. package/esm/types/0_opening_hours.d.ts +25 -0
  57. package/esm/types/0_opening_hours.js +25 -0
  58. package/esm/types/0_video_chat.d.ts +60 -0
  59. package/esm/types/0_video_chat.js +53 -0
  60. package/esm/types/1_chat_p.d.ts +4 -0
  61. package/esm/types/1_chat_p.js +1 -0
  62. package/esm/types/2_business_connection.d.ts +6 -0
  63. package/esm/types/2_chat.d.ts +62 -0
  64. package/esm/types/2_chat.js +66 -0
  65. package/esm/types/2_inactive_chat.d.ts +1 -1
  66. package/esm/types/2_invite_link.d.ts +1 -1
  67. package/esm/types/2_message_reaction_count.d.ts +1 -1
  68. package/esm/types/2_message_reactions.d.ts +1 -1
  69. package/esm/types/3_chat_member_updated.d.ts +1 -1
  70. package/esm/types/4_message.d.ts +3 -3
  71. package/esm/types/5_chat_list_item.d.ts +35 -0
  72. package/esm/types/5_chat_list_item.js +86 -0
  73. package/esm/types/6_update.d.ts +14 -5
  74. package/esm/types/_file_id.d.ts +1 -0
  75. package/esm/utilities/0_bigint.d.ts +1 -0
  76. package/esm/utilities/0_bigint.js +7 -3
  77. package/package.json +1 -1
  78. package/script/3_types.d.ts +6 -1
  79. package/script/3_types.js +6 -1
  80. package/script/4_errors.js +2 -4
  81. package/script/5_client.d.ts +1 -1
  82. package/script/5_client.js +1 -1
  83. package/script/client/0_params.d.ts +33 -7
  84. package/script/client/0_storage_operations.d.ts +182 -0
  85. package/script/client/0_storage_operations.js +615 -0
  86. package/script/client/1_composer.d.ts +2 -0
  87. package/script/client/1_composer.js +28 -0
  88. package/{esm/client/0_types.d.ts → script/client/1_types.d.ts} +3 -3
  89. package/{esm/client/1_account_manager.d.ts → script/client/2_account_manager.d.ts} +1 -1
  90. package/{esm/client/1_bot_info_manager.d.ts → script/client/2_bot_info_manager.d.ts} +1 -1
  91. package/script/client/{1_business_connection_manager.d.ts → 2_business_connection_manager.d.ts} +1 -1
  92. package/{esm/client/1_file_manager.d.ts → script/client/2_file_manager.d.ts} +6 -2
  93. package/script/client/{1_file_manager.js → 2_file_manager.js} +54 -51
  94. package/script/client/{1_network_statistics_manager.d.ts → 2_network_statistics_manager.d.ts} +1 -1
  95. package/{esm/client/1_reaction_manager.d.ts → script/client/2_reaction_manager.d.ts} +1 -1
  96. package/{esm/client/1_update_manager.d.ts → script/client/2_update_manager.d.ts} +1 -1
  97. package/script/client/{2_message_manager.d.ts → 3_message_manager.d.ts} +2 -2
  98. package/script/client/3_video_chat_manager.d.ts +43 -0
  99. package/script/client/3_video_chat_manager.js +198 -0
  100. package/script/client/{3_callback_query_manager.d.ts → 4_callback_query_manager.d.ts} +2 -2
  101. package/script/client/{3_callback_query_manager.js → 4_callback_query_manager.js} +1 -0
  102. package/{esm/client/3_chat_list_manager.d.ts → script/client/4_chat_list_manager.d.ts} +6 -6
  103. package/script/client/{3_chat_list_manager.js → 4_chat_list_manager.js} +72 -95
  104. package/script/client/{3_inline_query_manager.d.ts → 4_inline_query_manager.d.ts} +2 -2
  105. package/script/client/{3_inline_query_manager.js → 4_inline_query_manager.js} +1 -0
  106. package/script/client/{3_story_manager.d.ts → 4_story_manager.d.ts} +3 -3
  107. package/{esm/client/4_client.d.ts → script/client/5_client.d.ts} +105 -31
  108. package/script/client/{4_client.js → 5_client.js} +192 -93
  109. package/script/connection/0_connection.d.ts +6 -15
  110. package/script/connection/0_connection.js +0 -41
  111. package/script/connection/1_connection_web_socket.d.ts +3 -2
  112. package/script/connection/1_connection_web_socket.js +7 -3
  113. package/script/mod.d.ts +1 -1
  114. package/script/mod.js +1 -1
  115. package/script/storage/0_storage.d.ts +9 -145
  116. package/script/storage/0_storage.js +0 -528
  117. package/script/storage/2_storage_indexed_db.d.ts +3 -2
  118. package/script/storage/2_storage_indexed_db.js +9 -8
  119. package/script/storage/2_storage_local_storage.d.ts +3 -2
  120. package/script/storage/2_storage_local_storage.js +5 -4
  121. package/script/storage/2_storage_memory.d.ts +4 -4
  122. package/script/storage/2_storage_memory.js +12 -19
  123. package/script/storage/2_storage_session_storage.d.ts +2 -1
  124. package/script/storage/2_storage_session_storage.js +5 -4
  125. package/script/tl/3_utilities.d.ts +1 -1
  126. package/script/tl/3_utilities.js +3 -3
  127. package/script/types/0_birthday.d.ts +27 -0
  128. package/script/types/0_birthday.js +30 -0
  129. package/script/types/0_giveaway_parameters.d.ts +1 -1
  130. package/script/types/0_id.d.ts +1 -1
  131. package/script/types/0_live_stream_channel.d.ts +30 -0
  132. package/script/types/0_live_stream_channel.js +30 -0
  133. package/script/types/0_opening_hours.d.ts +25 -0
  134. package/script/types/0_opening_hours.js +29 -0
  135. package/script/types/0_video_chat.d.ts +60 -0
  136. package/script/types/0_video_chat.js +57 -0
  137. package/script/types/1_chat_p.d.ts +4 -0
  138. package/script/types/1_chat_p.js +1 -0
  139. package/script/types/2_business_connection.d.ts +6 -0
  140. package/script/types/2_chat.d.ts +62 -0
  141. package/script/types/2_chat.js +70 -0
  142. package/script/types/2_inactive_chat.d.ts +1 -1
  143. package/script/types/2_invite_link.d.ts +1 -1
  144. package/script/types/2_message_reaction_count.d.ts +1 -1
  145. package/script/types/2_message_reactions.d.ts +1 -1
  146. package/script/types/3_chat_member_updated.d.ts +1 -1
  147. package/script/types/4_message.d.ts +3 -3
  148. package/script/types/5_chat_list_item.d.ts +35 -0
  149. package/script/types/5_chat_list_item.js +94 -0
  150. package/script/types/6_update.d.ts +14 -5
  151. package/script/types/_file_id.d.ts +1 -0
  152. package/script/utilities/0_bigint.d.ts +1 -0
  153. package/script/utilities/0_bigint.js +7 -3
  154. package/esm/types/5_chat.d.ts +0 -59
  155. package/esm/types/5_chat.js +0 -126
  156. package/script/types/5_chat.d.ts +0 -59
  157. package/script/types/5_chat.js +0 -134
  158. /package/esm/{3_storage.d.ts → 2_storage.d.ts} +0 -0
  159. /package/esm/{3_storage.js → 2_storage.js} +0 -0
  160. /package/esm/client/{0_types.js → 1_types.js} +0 -0
  161. /package/esm/client/{1_account_manager.js → 2_account_manager.js} +0 -0
  162. /package/esm/client/{1_bot_info_manager.js → 2_bot_info_manager.js} +0 -0
  163. /package/esm/client/{1_business_connection_manager.js → 2_business_connection_manager.js} +0 -0
  164. /package/esm/client/{1_network_statistics_manager.js → 2_network_statistics_manager.js} +0 -0
  165. /package/esm/client/{1_reaction_manager.js → 2_reaction_manager.js} +0 -0
  166. /package/esm/client/{1_update_manager.js → 2_update_manager.js} +0 -0
  167. /package/esm/client/{2_message_manager.js → 3_message_manager.js} +0 -0
  168. /package/esm/client/{3_story_manager.js → 4_story_manager.js} +0 -0
  169. /package/script/{3_storage.d.ts → 2_storage.d.ts} +0 -0
  170. /package/script/{3_storage.js → 2_storage.js} +0 -0
  171. /package/script/client/{0_types.js → 1_types.js} +0 -0
  172. /package/script/client/{1_account_manager.js → 2_account_manager.js} +0 -0
  173. /package/script/client/{1_bot_info_manager.js → 2_bot_info_manager.js} +0 -0
  174. /package/script/client/{1_business_connection_manager.js → 2_business_connection_manager.js} +0 -0
  175. /package/script/client/{1_network_statistics_manager.js → 2_network_statistics_manager.js} +0 -0
  176. /package/script/client/{1_reaction_manager.js → 2_reaction_manager.js} +0 -0
  177. /package/script/client/{1_update_manager.js → 2_update_manager.js} +0 -0
  178. /package/script/client/{2_message_manager.js → 3_message_manager.js} +0 -0
  179. /package/script/client/{3_story_manager.js → 4_story_manager.js} +0 -0
@@ -18,7 +18,7 @@
18
18
  * along with this program. If not, see <https://www.gnu.org/licenses/>.
19
19
  */
20
20
  import { ID } from "../3_types.js";
21
- import { C } from "./0_types.js";
21
+ import { C } from "./1_types.js";
22
22
  export declare class AccountManager {
23
23
  #private;
24
24
  constructor(c: C);
@@ -19,7 +19,7 @@
19
19
  */
20
20
  import { BotCommand } from "../3_types.js";
21
21
  import { GetMyCommandsParams, SetMyCommandsParams } from "./0_params.js";
22
- import { C } from "./0_types.js";
22
+ import { C } from "./1_types.js";
23
23
  export declare class BotInfoManager {
24
24
  #private;
25
25
  constructor(c: C);
@@ -19,7 +19,7 @@
19
19
  */
20
20
  import { enums, types } from "../2_tl.js";
21
21
  import { Update } from "../3_types.js";
22
- import { C } from "./0_types.js";
22
+ import { C } from "./1_types.js";
23
23
  export type BusinessConnectionManagerUpdate = types.UpdateBotBusinessConnect;
24
24
  export declare class BusinessConnectionManager {
25
25
  #private;
@@ -1,11 +1,15 @@
1
- import { types } from "../2_tl.js";
1
+ import { enums, types } from "../2_tl.js";
2
2
  import { FileSource, Sticker } from "../3_types.js";
3
3
  import { DownloadParams, UploadParams } from "./0_params.js";
4
- import { C } from "./0_types.js";
4
+ import { C } from "./1_types.js";
5
5
  export declare class FileManager {
6
6
  #private;
7
7
  constructor(c: C);
8
8
  upload(file: FileSource, params?: UploadParams, checkName?: null | ((name: string) => string), allowStream?: boolean): Promise<import("../tl/2_types.js").InputFile_ | import("../tl/2_types.js").InputFileBig_>;
9
+ downloadInner(location: enums.InputFileLocation, dcId: number, params?: {
10
+ chunkSize?: number;
11
+ offset?: number;
12
+ }): AsyncGenerator<Uint8Array, void, unknown>;
9
13
  static validateChunkSize(chunkSize: number, max: number): void;
10
14
  download(fileId: string, params?: DownloadParams): AsyncGenerator<Uint8Array, void, unknown>;
11
15
  getStickerSetName(inputStickerSet: types.InputStickerSetID, hash?: number): Promise<string>;
@@ -9,7 +9,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
9
9
  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");
10
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
11
  };
12
- var _FileManager_instances, _a, _FileManager_c, _FileManager_Lupload, _FileManager_UPLOAD_MAX_CHUNK_SIZE, _FileManager_DOWNLOAD_MAX_CHUNK_SIZE, _FileManager_BIG_FILE_THRESHOLD, _FileManager_UPLOAD_REQUEST_PER_CONNECTION, _FileManager_uploadStream, _FileManager_uploadBuffer, _FileManager_handleUploadError, _FileManager_getFileContents, _FileManager_downloadInner, _FileManager_CUSTOM_EMOJI_TTL;
12
+ var _FileManager_instances, _a, _FileManager_c, _FileManager_Lupload, _FileManager_UPLOAD_MAX_CHUNK_SIZE, _FileManager_DOWNLOAD_MAX_CHUNK_SIZE, _FileManager_BIG_FILE_THRESHOLD, _FileManager_UPLOAD_REQUEST_PER_CONNECTION, _FileManager_uploadStream, _FileManager_uploadBuffer, _FileManager_handleUploadError, _FileManager_getFileContents, _FileManager_CUSTOM_EMOJI_TTL;
13
13
  /**
14
14
  * MTKruto - Cross-runtime JavaScript library for building Telegram clients
15
15
  * Copyright (C) 2023-2024 Roj <https://roj.im/>
@@ -30,7 +30,7 @@ var _FileManager_instances, _a, _FileManager_c, _FileManager_Lupload, _FileManag
30
30
  * along with this program. If not, see <https://www.gnu.org/licenses/>.
31
31
  */
32
32
  import * as dntShim from "../_dnt.shims.js";
33
- import { assert, extension, path, unreachable } from "../0_deps.js";
33
+ import { extension, path, unreachable } from "../0_deps.js";
34
34
  import { InputError } from "../0_errors.js";
35
35
  import { drop, getLogger, getRandomId, iterateReadableStream, kilobyte, megabyte, minute, mod, PartStream } from "../1_utilities.js";
36
36
  import { as, types } from "../2_tl.js";
@@ -84,6 +84,51 @@ export class FileManager {
84
84
  return new types.InputFileBig({ id: fileId, parts: result.parts, name });
85
85
  }
86
86
  }
87
+ async *downloadInner(location, dcId, params) {
88
+ const id = "id" in location ? location.id : "photo_id" in location ? location.photo_id : null;
89
+ if (id != null && __classPrivateFieldGet(this, _FileManager_c, "f").storage.supportsFiles) {
90
+ const file = await __classPrivateFieldGet(this, _FileManager_c, "f").storage.getFile(id);
91
+ const partOffset = file == null ? 0 : params?.offset ? Math.ceil(10 / file[1]) - 1 : 0;
92
+ if (file != null && file[0] > 0) {
93
+ yield* __classPrivateFieldGet(this, _FileManager_c, "f").storage.iterFileParts(id, file[0], partOffset);
94
+ return;
95
+ }
96
+ }
97
+ const chunkSize = params?.chunkSize ?? __classPrivateFieldGet(_a, _a, "f", _FileManager_DOWNLOAD_MAX_CHUNK_SIZE);
98
+ _a.validateChunkSize(chunkSize, __classPrivateFieldGet(_a, _a, "f", _FileManager_DOWNLOAD_MAX_CHUNK_SIZE));
99
+ const connection = __classPrivateFieldGet(this, _FileManager_c, "f").getCdnConnection(dcId);
100
+ await connection.connect();
101
+ const limit = chunkSize;
102
+ let offset = params?.offset ? BigInt(params.offset) : 0n;
103
+ let part = 0;
104
+ try {
105
+ while (true) {
106
+ const file = await connection.api.upload.getFile({ location, offset, limit });
107
+ if (file instanceof types.upload.File) {
108
+ yield file.bytes;
109
+ if (id != null) {
110
+ await __classPrivateFieldGet(this, _FileManager_c, "f").storage.saveFilePart(id, part, file.bytes);
111
+ }
112
+ ++part;
113
+ if (file.bytes.length < limit) {
114
+ if (id != null) {
115
+ await __classPrivateFieldGet(this, _FileManager_c, "f").storage.setFilePartCount(id, part + 1, chunkSize);
116
+ }
117
+ break;
118
+ }
119
+ else {
120
+ offset += BigInt(file.bytes.length);
121
+ }
122
+ }
123
+ else {
124
+ unreachable();
125
+ }
126
+ }
127
+ }
128
+ finally {
129
+ drop(connection.disconnect());
130
+ }
131
+ }
87
132
  static validateChunkSize(chunkSize, max) {
88
133
  if (chunkSize <= 0) {
89
134
  throw new InputError("chunkSize must be bigger than zero.");
@@ -109,7 +154,7 @@ export class FileManager {
109
154
  const big = fileId_.location.source.type == PhotoSourceType.ChatPhotoBig;
110
155
  const peer = await __classPrivateFieldGet(this, _FileManager_c, "f").getInputPeer(Number(fileId_.location.source.chatId)); // TODO: use access hash from source?
111
156
  const location = new types.InputPeerPhotoFileLocation({ big: big ? true : undefined, peer, photo_id: fileId_.location.id });
112
- yield* __classPrivateFieldGet(this, _FileManager_instances, "m", _FileManager_downloadInner).call(this, location, fileId_.dcId, params);
157
+ yield* this.downloadInner(location, fileId_.dcId, params);
113
158
  break;
114
159
  }
115
160
  case FileType.Photo: {
@@ -119,7 +164,7 @@ export class FileManager {
119
164
  file_reference: fileId_.fileReference ?? new Uint8Array(),
120
165
  thumb_size: "thumbnailType" in fileId_.location.source ? String.fromCharCode(fileId_.location.source.thumbnailType) : "",
121
166
  });
122
- yield* __classPrivateFieldGet(this, _FileManager_instances, "m", _FileManager_downloadInner).call(this, location, fileId_.dcId, params);
167
+ yield* this.downloadInner(location, fileId_.dcId, params);
123
168
  break;
124
169
  }
125
170
  case FileType.Thumbnail: {
@@ -129,7 +174,7 @@ export class FileManager {
129
174
  file_reference: fileId_.fileReference ?? new Uint8Array(),
130
175
  thumb_size: "thumbnailType" in fileId_.location.source ? String.fromCharCode(fileId_.location.source.thumbnailType) : unreachable(),
131
176
  });
132
- yield* __classPrivateFieldGet(this, _FileManager_instances, "m", _FileManager_downloadInner).call(this, location, fileId_.dcId, params);
177
+ yield* this.downloadInner(location, fileId_.dcId, params);
133
178
  break;
134
179
  }
135
180
  }
@@ -141,7 +186,7 @@ export class FileManager {
141
186
  file_reference: fileId_.fileReference ?? new Uint8Array(),
142
187
  thumb_size: "",
143
188
  });
144
- yield* __classPrivateFieldGet(this, _FileManager_instances, "m", _FileManager_downloadInner).call(this, location, fileId_.dcId, params);
189
+ yield* this.downloadInner(location, fileId_.dcId, params);
145
190
  }
146
191
  else {
147
192
  unreachable();
@@ -246,14 +291,16 @@ _a = FileManager, _FileManager_c = new WeakMap(), _FileManager_Lupload = new Wea
246
291
  const isBig = buffer.byteLength > __classPrivateFieldGet(_a, _a, "f", _FileManager_BIG_FILE_THRESHOLD);
247
292
  const partCount = Math.ceil(buffer.byteLength / chunkSize);
248
293
  let promises = new Array();
249
- for (let part = 0; part < partCount;) {
294
+ main: for (let part = 0; part < partCount;) {
250
295
  for (let i = 0; i < pool.size; ++i) {
251
296
  const api = pool.api();
252
297
  for (let i = 0; i < __classPrivateFieldGet(_a, _a, "f", _FileManager_UPLOAD_REQUEST_PER_CONNECTION); ++i) {
253
298
  const start = part * chunkSize;
254
299
  const end = start + chunkSize;
255
300
  const bytes = buffer.subarray(start, end);
256
- assert(bytes.length != 0);
301
+ if (!bytes.length) {
302
+ break main;
303
+ }
257
304
  const thisPart = part++; // `thisPart` must be used instead of `part` in the promise body
258
305
  promises.push(Promise.resolve().then(async () => {
259
306
  while (true) {
@@ -373,50 +420,6 @@ _a = FileManager, _FileManager_c = new WeakMap(), _FileManager_Lupload = new Wea
373
420
  }
374
421
  }
375
422
  return { size: params?.fileSize ? params.fileSize : size, name, contents };
376
- }, _FileManager_downloadInner = async function* _FileManager_downloadInner(location, dcId, params) {
377
- const id = "id" in location ? location.id : "photo_id" in location ? location.photo_id : null;
378
- if (id != null && __classPrivateFieldGet(this, _FileManager_c, "f").storage.supportsFiles) {
379
- const file = await __classPrivateFieldGet(this, _FileManager_c, "f").storage.getFile(id);
380
- const partOffset = file == null ? 0 : params?.offset ? Math.ceil(10 / file[1]) - 1 : 0;
381
- if (file != null && file[0] > 0) {
382
- yield* __classPrivateFieldGet(this, _FileManager_c, "f").storage.iterFileParts(id, file[0], partOffset);
383
- return;
384
- }
385
- }
386
- const chunkSize = params?.chunkSize ?? __classPrivateFieldGet(_a, _a, "f", _FileManager_DOWNLOAD_MAX_CHUNK_SIZE);
387
- _a.validateChunkSize(chunkSize, __classPrivateFieldGet(_a, _a, "f", _FileManager_DOWNLOAD_MAX_CHUNK_SIZE));
388
- const connection = __classPrivateFieldGet(this, _FileManager_c, "f").getCdnConnection(dcId);
389
- await connection.connect();
390
- const limit = chunkSize;
391
- let offset = params?.offset ? BigInt(params.offset) : 0n;
392
- let part = 0;
393
- try {
394
- while (true) {
395
- const file = await connection.api.upload.getFile({ location, offset, limit });
396
- if (file instanceof types.upload.File) {
397
- yield file.bytes;
398
- if (id != null) {
399
- await __classPrivateFieldGet(this, _FileManager_c, "f").storage.saveFilePart(id, part, file.bytes);
400
- }
401
- ++part;
402
- if (file.bytes.length < limit) {
403
- if (id != null) {
404
- await __classPrivateFieldGet(this, _FileManager_c, "f").storage.setFilePartCount(id, part + 1, chunkSize);
405
- }
406
- break;
407
- }
408
- else {
409
- offset += BigInt(file.bytes.length);
410
- }
411
- }
412
- else {
413
- unreachable();
414
- }
415
- }
416
- }
417
- finally {
418
- drop(connection.disconnect());
419
- }
420
423
  };
421
424
  _FileManager_UPLOAD_MAX_CHUNK_SIZE = { value: 512 * kilobyte };
422
425
  _FileManager_DOWNLOAD_MAX_CHUNK_SIZE = { value: 1 * megabyte };
@@ -17,7 +17,7 @@
17
17
  * You should have received a copy of the GNU Lesser General Public License
18
18
  * along with this program. If not, see <https://www.gnu.org/licenses/>.
19
19
  */
20
- import { C } from "./0_types.js";
20
+ import { C } from "./1_types.js";
21
21
  export declare class NetworkStatisticsManager {
22
22
  #private;
23
23
  constructor(c: C);
@@ -19,7 +19,7 @@
19
19
  */
20
20
  import { enums, types } from "../2_tl.js";
21
21
  import { Update } from "../3_types.js";
22
- import { C } from "./0_types.js";
22
+ import { C } from "./1_types.js";
23
23
  type ReactionManagerUpdate = types.UpdateBotMessageReactions | types.UpdateBotMessageReaction | types.UpdateMessageReactions | types.UpdateChannelMessageViews | types.UpdateChannelMessageForwards;
24
24
  export declare class ReactionManager {
25
25
  #private;
@@ -19,7 +19,7 @@
19
19
  */
20
20
  import { Queue } from "../1_utilities.js";
21
21
  import { enums, ReadObject, TLObject, types } from "../2_tl.js";
22
- import { C } from "./0_types.js";
22
+ import { C } from "./1_types.js";
23
23
  type UpdateHandler = (update: enums.Update) => Promise<(() => Promise<unknown>)>;
24
24
  export type PtsUpdate = types.UpdateNewMessage | types.UpdateDeleteMessages | types.UpdateReadHistoryInbox | types.UpdateReadHistoryOutbox | types.UpdatePinnedChannelMessages | types.UpdatePinnedMessages | types.UpdateFolderPeers | types.UpdateChannelWebPage | types.UpdateEditMessage | types.UpdateReadMessagesContents | types.UpdateWebPage;
25
25
  export type ChannelPtsUpdate = types.UpdateNewChannelMessage | types.UpdateEditChannelMessage | types.UpdateDeleteChannelMessages | types.UpdateChannelTooLong;
@@ -21,8 +21,8 @@ import { enums, types } from "../2_tl.js";
21
21
  import { InputMedia } from "../3_types.js";
22
22
  import { ChatAction, ChatMember, FileSource, FileType, ID, Message, MessageEntity, ParseMode, Reaction, Update, UsernameResolver } from "../3_types.js";
23
23
  import { AddReactionParams, BanChatMemberParams, CreateInviteLinkParams, DeleteMessagesParams, EditMessageLiveLocationParams, EditMessageMediaParams, EditMessageParams, EditMessageReplyMarkupParams, ForwardMessagesParams, GetCreatedInviteLinksParams, GetHistoryParams, PinMessageParams, SearchMessagesParams, SendAnimationParams, SendAudioParams, SendChatActionParams, SendContactParams, SendDiceParams, SendDocumentParams, SendLocationParams, SendMessageParams, SendPhotoParams, SendPollParams, SendStickerParams, SendVenueParams, SendVideoNoteParams, SendVideoParams, SendVoiceParams, SetChatMemberRightsParams, SetChatPhotoParams, SetReactionsParams, StopPollParams } from "./0_params.js";
24
- import { C as C_ } from "./0_types.js";
25
- import { FileManager } from "./1_file_manager.js";
24
+ import { C as C_ } from "./1_types.js";
25
+ import { FileManager } from "./2_file_manager.js";
26
26
  interface C extends C_ {
27
27
  fileManager: FileManager;
28
28
  }
@@ -0,0 +1,43 @@
1
+ /**
2
+ * MTKruto - Cross-runtime JavaScript library for building Telegram clients
3
+ * Copyright (C) 2023-2024 Roj <https://roj.im/>
4
+ *
5
+ * This file is part of MTKruto.
6
+ *
7
+ * This program is free software: you can redistribute it and/or modify
8
+ * it under the terms of the GNU Lesser General Public License as published by
9
+ * the Free Software Foundation, either version 3 of the License, or
10
+ * (at your option) any later version.
11
+ *
12
+ * This program is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ * GNU Lesser General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU Lesser General Public License
18
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
19
+ */
20
+ import { enums, types } from "../2_tl.js";
21
+ import { ID, Update, VideoChatActive, VideoChatScheduled } from "../3_types.js";
22
+ import { DownloadLiveStreamChunkParams, JoinVideoChatParams, StartVideoChatParams } from "./0_params.js";
23
+ import { C as C_ } from "./1_types.js";
24
+ import { FileManager } from "./2_file_manager.js";
25
+ interface C extends C_ {
26
+ fileManager: FileManager;
27
+ }
28
+ type VideoChatManagerUpdate = types.UpdateGroupCall;
29
+ export declare class VideoChatManager {
30
+ #private;
31
+ constructor(c: C);
32
+ startVideoChat(chatId: ID, params?: StartVideoChatParams): Promise<VideoChatActive>;
33
+ scheduleVideoChat(chatId: ID, startAt: Date, params?: StartVideoChatParams): Promise<VideoChatScheduled>;
34
+ joinVideoChat(id: string, params: string, params_?: JoinVideoChatParams): Promise<string>;
35
+ leaveVideoChat(id: string): Promise<void>;
36
+ joinLiveStream(id: string): Promise<void>;
37
+ getVideoChat(id: string): Promise<import("../3_types.js").VideoChat>;
38
+ static canHandleUpdate(update: enums.Update): update is VideoChatManagerUpdate;
39
+ handleUpdate(update: VideoChatManagerUpdate): Promise<Update>;
40
+ getLiveStreamChannels(id: string): Promise<import("../3_types.js").LiveStreamChannel[]>;
41
+ downloadLiveStreamChunk(id: string, channel: number, scale: number, timestamp: number, params?: DownloadLiveStreamChunkParams): AsyncGenerator<Uint8Array, void, unknown>;
42
+ }
43
+ export {};
@@ -0,0 +1,194 @@
1
+ /**
2
+ * MTKruto - Cross-runtime JavaScript library for building Telegram clients
3
+ * Copyright (C) 2023-2024 Roj <https://roj.im/>
4
+ *
5
+ * This file is part of MTKruto.
6
+ *
7
+ * This program is free software: you can redistribute it and/or modify
8
+ * it under the terms of the GNU Lesser General Public License as published by
9
+ * the Free Software Foundation, either version 3 of the License, or
10
+ * (at your option) any later version.
11
+ *
12
+ * This program is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ * GNU Lesser General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU Lesser General Public License
18
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
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 _VideoChatManager_instances, _VideoChatManager_c, _VideoChatManager_createGroupCall, _VideoChatManager_getInputGroupCall, _VideoChatManager_getCall;
32
+ import { unreachable } from "../0_deps.js";
33
+ import { InputError } from "../0_errors.js";
34
+ import { getRandomId, toUnixTimestamp, ZERO_CHANNEL_ID } from "../1_utilities.js";
35
+ import { as, types } from "../2_tl.js";
36
+ import { constructLiveStreamChannel, constructVideoChat } from "../3_types.js";
37
+ export class VideoChatManager {
38
+ constructor(c) {
39
+ _VideoChatManager_instances.add(this);
40
+ _VideoChatManager_c.set(this, void 0);
41
+ __classPrivateFieldSet(this, _VideoChatManager_c, c, "f");
42
+ }
43
+ async startVideoChat(chatId, params) {
44
+ await __classPrivateFieldGet(this, _VideoChatManager_c, "f").storage.assertUser("startVideoChat");
45
+ return await __classPrivateFieldGet(this, _VideoChatManager_instances, "m", _VideoChatManager_createGroupCall).call(this, chatId, params?.title, params?.liveStream || undefined);
46
+ }
47
+ async scheduleVideoChat(chatId, startAt, params) {
48
+ await __classPrivateFieldGet(this, _VideoChatManager_c, "f").storage.assertUser("scheduleVideoChat");
49
+ return await __classPrivateFieldGet(this, _VideoChatManager_instances, "m", _VideoChatManager_createGroupCall).call(this, chatId, params?.title, params?.liveStream || undefined, startAt);
50
+ }
51
+ async joinVideoChat(id, params, params_) {
52
+ await __classPrivateFieldGet(this, _VideoChatManager_c, "f").storage.assertUser("joinVideoChat");
53
+ const call = await __classPrivateFieldGet(this, _VideoChatManager_instances, "m", _VideoChatManager_getInputGroupCall).call(this, id);
54
+ const { updates } = await __classPrivateFieldGet(this, _VideoChatManager_c, "f").api.phone.joinGroupCall({
55
+ call,
56
+ join_as: params_?.joinAs ? await __classPrivateFieldGet(this, _VideoChatManager_c, "f").getInputPeer(params_.joinAs) : new types.InputPeerSelf(),
57
+ params: new types.DataJSON({ data: params }),
58
+ invite_hash: params_?.inviteHash,
59
+ muted: params_?.audio ? undefined : true,
60
+ video_stopped: params_?.video ? undefined : true,
61
+ }).then((v) => v[as](types.Updates));
62
+ const updateGroupCall = updates
63
+ .find((v) => v instanceof types.UpdateGroupCallConnection);
64
+ if (!updateGroupCall)
65
+ unreachable();
66
+ return updateGroupCall.params.data;
67
+ }
68
+ async leaveVideoChat(id) {
69
+ await __classPrivateFieldGet(this, _VideoChatManager_c, "f").storage.assertUser("leaveVideoChat");
70
+ await __classPrivateFieldGet(this, _VideoChatManager_c, "f").api.phone.leaveGroupCall({ call: await __classPrivateFieldGet(this, _VideoChatManager_instances, "m", _VideoChatManager_getInputGroupCall).call(this, id), source: 0 });
71
+ }
72
+ async joinLiveStream(id) {
73
+ await __classPrivateFieldGet(this, _VideoChatManager_c, "f").storage.assertUser("joinLiveStream");
74
+ const call = await __classPrivateFieldGet(this, _VideoChatManager_instances, "m", _VideoChatManager_getInputGroupCall).call(this, id);
75
+ const { updates } = await __classPrivateFieldGet(this, _VideoChatManager_c, "f").api.phone.joinGroupCall({
76
+ call,
77
+ join_as: new types.InputPeerSelf(),
78
+ params: new types.DataJSON({
79
+ data: JSON.stringify({
80
+ fingerprints: [],
81
+ pwd: "",
82
+ ssrc: getRandomId(true),
83
+ "ssrc-groups": [],
84
+ ufrag: "",
85
+ }),
86
+ }),
87
+ }).then((v) => v[as](types.Updates));
88
+ const updateGroupCall = updates
89
+ .find((v) => v instanceof types.UpdateGroupCallConnection);
90
+ if (!updateGroupCall)
91
+ unreachable();
92
+ }
93
+ async getVideoChat(id) {
94
+ await __classPrivateFieldGet(this, _VideoChatManager_c, "f").storage.assertUser("getVideoChat");
95
+ return constructVideoChat(await __classPrivateFieldGet(this, _VideoChatManager_instances, "m", _VideoChatManager_getCall).call(this, id));
96
+ }
97
+ static canHandleUpdate(update) {
98
+ return update instanceof types.UpdateGroupCall;
99
+ }
100
+ async handleUpdate(update) {
101
+ let chatId = Number(-update.chat_id);
102
+ const fullChat = await __classPrivateFieldGet(this, _VideoChatManager_c, "f").storage.getFullChat(chatId).then((v) => v == null ? __classPrivateFieldGet(this, _VideoChatManager_c, "f").storage.getFullChat(chatId = ZERO_CHANNEL_ID - Number(update.chat_id)) : v);
103
+ let updateFullChat = false;
104
+ if (update.call instanceof types.GroupCallDiscarded) {
105
+ await __classPrivateFieldGet(this, _VideoChatManager_c, "f").storage.setGroupCall(update.call.id, null);
106
+ await __classPrivateFieldGet(this, _VideoChatManager_c, "f").storage.setGroupCallAccessHash(update.call.id, null);
107
+ if (fullChat != null) {
108
+ fullChat.call = undefined;
109
+ updateFullChat = true;
110
+ }
111
+ }
112
+ else {
113
+ await __classPrivateFieldGet(this, _VideoChatManager_c, "f").storage.setGroupCall(update.call.id, update.call);
114
+ await __classPrivateFieldGet(this, _VideoChatManager_c, "f").storage.setGroupCallAccessHash(update.call.id, update.call.access_hash);
115
+ if (fullChat != null) {
116
+ if (!("call" in fullChat) || !fullChat.call || fullChat.call.id != update.call.id) {
117
+ fullChat.call = new types.InputGroupCall(update.call);
118
+ updateFullChat = true;
119
+ }
120
+ }
121
+ }
122
+ if (updateFullChat) {
123
+ await __classPrivateFieldGet(this, _VideoChatManager_c, "f").storage.setFullChat(chatId, fullChat);
124
+ }
125
+ return { videoChat: constructVideoChat(update.call) };
126
+ }
127
+ async getLiveStreamChannels(id) {
128
+ await __classPrivateFieldGet(this, _VideoChatManager_c, "f").storage.assertUser("getLiveStreamChannels");
129
+ const call = await __classPrivateFieldGet(this, _VideoChatManager_instances, "m", _VideoChatManager_getCall).call(this, id);
130
+ if (!(call instanceof types.GroupCall) || !call.rtmp_stream) {
131
+ throw new InputError("Not a live stream.");
132
+ }
133
+ const client = __classPrivateFieldGet(this, _VideoChatManager_c, "f").getCdnConnection(call.stream_dc_id);
134
+ await client.connect();
135
+ try {
136
+ const streams = await client.api.phone.getGroupCallStreamChannels({ call: await __classPrivateFieldGet(this, _VideoChatManager_instances, "m", _VideoChatManager_getInputGroupCall).call(this, id) });
137
+ return streams.channels.map(constructLiveStreamChannel);
138
+ }
139
+ finally {
140
+ await client.disconnect();
141
+ }
142
+ }
143
+ async *downloadLiveStreamChunk(id, channel, scale, timestamp, params) {
144
+ await __classPrivateFieldGet(this, _VideoChatManager_c, "f").storage.assertUser("downloadLiveStreamChunk");
145
+ const call = await __classPrivateFieldGet(this, _VideoChatManager_instances, "m", _VideoChatManager_getCall).call(this, id);
146
+ if (!(call instanceof types.GroupCall) || !call.rtmp_stream) {
147
+ throw new InputError("Not a live stream.");
148
+ }
149
+ const quality = params?.quality ?? "low";
150
+ const location = new types.InputGroupCallStream({
151
+ call: new types.InputGroupCall(call),
152
+ scale,
153
+ time_ms: BigInt(timestamp),
154
+ video_channel: channel,
155
+ video_quality: quality == "low" ? 0 : quality == "medium" ? 1 : quality == "high" ? 2 : (() => {
156
+ throw new InputError("Got invalid quality.");
157
+ })(),
158
+ });
159
+ yield* __classPrivateFieldGet(this, _VideoChatManager_c, "f").fileManager.downloadInner(location, call.stream_dc_id ?? unreachable());
160
+ }
161
+ }
162
+ _VideoChatManager_c = new WeakMap(), _VideoChatManager_instances = new WeakSet(), _VideoChatManager_createGroupCall = async function _VideoChatManager_createGroupCall(chatId, title, liveStream, scheduleDate) {
163
+ const peer = await __classPrivateFieldGet(this, _VideoChatManager_c, "f").getInputPeer(chatId);
164
+ if (peer instanceof types.InputPeerUser) {
165
+ throw new InputError("Video chats are only available for groups and channels.");
166
+ }
167
+ const { updates } = await __classPrivateFieldGet(this, _VideoChatManager_c, "f").api.phone.createGroupCall({
168
+ peer,
169
+ random_id: getRandomId(true),
170
+ title,
171
+ rtmp_stream: liveStream,
172
+ schedule_date: scheduleDate ? toUnixTimestamp(scheduleDate) : undefined,
173
+ }).then((v) => v[as](types.Updates));
174
+ const updateGroupCall = updates
175
+ .find((v) => v instanceof types.UpdateGroupCall);
176
+ if (!updateGroupCall) {
177
+ unreachable();
178
+ }
179
+ return constructVideoChat(updateGroupCall.call);
180
+ }, _VideoChatManager_getInputGroupCall = async function _VideoChatManager_getInputGroupCall(id_) {
181
+ const id = BigInt(id_);
182
+ const accessHash = await __classPrivateFieldGet(this, _VideoChatManager_c, "f").storage.getGroupCallAccessHash(id);
183
+ if (accessHash == null) {
184
+ throw new InputError("Video chat not found.");
185
+ }
186
+ return new types.InputGroupCall({ id, access_hash: accessHash });
187
+ }, _VideoChatManager_getCall = async function _VideoChatManager_getCall(id) {
188
+ let groupCall = await __classPrivateFieldGet(this, _VideoChatManager_c, "f").storage.getGroupCall(BigInt(id));
189
+ if (groupCall == null) {
190
+ const call = await __classPrivateFieldGet(this, _VideoChatManager_instances, "m", _VideoChatManager_getInputGroupCall).call(this, id);
191
+ groupCall = await __classPrivateFieldGet(this, _VideoChatManager_c, "f").api.phone.getGroupCall({ call, limit: 1 }).then((v) => v.call);
192
+ }
193
+ return groupCall;
194
+ };
@@ -20,8 +20,8 @@
20
20
  import { enums, types } from "../2_tl.js";
21
21
  import { CallbackQueryQuestion, ID, Update } from "../3_types.js";
22
22
  import { AnswerCallbackQueryParams } from "./0_params.js";
23
- import { C as C_ } from "./0_types.js";
24
- import { MessageManager } from "./2_message_manager.js";
23
+ import { C as C_ } from "./1_types.js";
24
+ import { MessageManager } from "./3_message_manager.js";
25
25
  type C = C_ & {
26
26
  messageManager: MessageManager;
27
27
  };
@@ -50,6 +50,7 @@ export class CallbackQueryManager {
50
50
  });
51
51
  }
52
52
  async sendCallbackQuery(chatId, messageId, question) {
53
+ await __classPrivateFieldGet(this, _CallbackQueryManager_c, "f").storage.assertUser("sendCallbackQuery");
53
54
  checkMessageId(messageId);
54
55
  validateCallbackQueryQuestion(question);
55
56
  const peer = await __classPrivateFieldGet(this, _CallbackQueryManager_c, "f").getInputPeer(chatId), peerId = peerToChatId(peer), questionKey = JSON.stringify(question);
@@ -18,10 +18,10 @@
18
18
  * along with this program. If not, see <https://www.gnu.org/licenses/>.
19
19
  */
20
20
  import { enums, types } from "../2_tl.js";
21
- import { Chat, ID } from "../3_types.js";
22
- import { C as C_ } from "./0_types.js";
23
- import { FileManager } from "./1_file_manager.js";
24
- import { MessageManager } from "./2_message_manager.js";
21
+ import { ChatListItem, ID } from "../3_types.js";
22
+ import { C as C_ } from "./1_types.js";
23
+ import { FileManager } from "./2_file_manager.js";
24
+ import { MessageManager } from "./3_message_manager.js";
25
25
  type C = C_ & {
26
26
  fileManager: FileManager;
27
27
  messageManager: MessageManager;
@@ -31,9 +31,9 @@ export declare class ChatListManager {
31
31
  #private;
32
32
  constructor(c: C);
33
33
  reassignChatLastMessage(chatId: number, add?: boolean, sendUpdate?: boolean): Promise<() => Promise<void>>;
34
- getChats(from?: "archived" | "main", after?: Chat, limit?: number): Promise<Chat[]>;
34
+ getChats(from?: "archived" | "main", after?: ChatListItem, limit?: number): Promise<ChatListItem[]>;
35
35
  static canHandleUpdate(update: enums.Update): update is ChatListManagerUpdate;
36
36
  handleUpdate(update: ChatListManagerUpdate): Promise<void>;
37
- getChat(chatId: ID): Promise<Chat>;
37
+ getChat(chatId: ID): Promise<import("../3_types.js").Chat>;
38
38
  }
39
39
  export {};