teleproto 203.0.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.
Files changed (224) hide show
  1. package/CryptoFile.d.ts +2 -0
  2. package/CryptoFile.js +23 -0
  3. package/Helpers.d.ts +151 -0
  4. package/Helpers.js +477 -0
  5. package/LICENSE +23 -0
  6. package/Password.d.ts +15 -0
  7. package/Password.js +271 -0
  8. package/README.md +111 -0
  9. package/Utils.d.ts +228 -0
  10. package/Utils.js +1248 -0
  11. package/Version.d.ts +1 -0
  12. package/Version.js +4 -0
  13. package/client/2fa.d.ts +48 -0
  14. package/client/2fa.js +109 -0
  15. package/client/TelegramClient.d.ts +1042 -0
  16. package/client/TelegramClient.js +1167 -0
  17. package/client/account.d.ts +0 -0
  18. package/client/account.js +1 -0
  19. package/client/auth.d.ts +93 -0
  20. package/client/auth.js +375 -0
  21. package/client/bots.d.ts +6 -0
  22. package/client/bots.js +24 -0
  23. package/client/buttons.d.ts +4 -0
  24. package/client/buttons.js +87 -0
  25. package/client/chats.d.ts +46 -0
  26. package/client/chats.js +350 -0
  27. package/client/dialogs.d.ts +52 -0
  28. package/client/dialogs.js +174 -0
  29. package/client/downloads.d.ts +157 -0
  30. package/client/downloads.js +598 -0
  31. package/client/fs.d.ts +1 -0
  32. package/client/fs.js +13 -0
  33. package/client/index.d.ts +15 -0
  34. package/client/index.js +50 -0
  35. package/client/messageParse.d.ts +17 -0
  36. package/client/messageParse.js +191 -0
  37. package/client/messages.d.ts +259 -0
  38. package/client/messages.js +804 -0
  39. package/client/os.d.ts +2 -0
  40. package/client/os.js +23 -0
  41. package/client/path.d.ts +2 -0
  42. package/client/path.js +7 -0
  43. package/client/telegramBaseClient.d.ts +238 -0
  44. package/client/telegramBaseClient.js +368 -0
  45. package/client/updates.d.ts +31 -0
  46. package/client/updates.js +254 -0
  47. package/client/uploads.d.ts +137 -0
  48. package/client/uploads.js +514 -0
  49. package/client/users.d.ts +29 -0
  50. package/client/users.js +491 -0
  51. package/crypto/AuthKey.d.ts +20 -0
  52. package/crypto/AuthKey.js +76 -0
  53. package/crypto/CTR.d.ts +6 -0
  54. package/crypto/CTR.js +35 -0
  55. package/crypto/Factorizator.d.ts +19 -0
  56. package/crypto/Factorizator.js +74 -0
  57. package/crypto/IGE.d.ts +18 -0
  58. package/crypto/IGE.js +34 -0
  59. package/crypto/RSA.d.ts +15 -0
  60. package/crypto/RSA.js +70 -0
  61. package/crypto/converters.d.ts +19 -0
  62. package/crypto/converters.js +52 -0
  63. package/crypto/crypto.d.ts +27 -0
  64. package/crypto/crypto.js +117 -0
  65. package/crypto/words.d.ts +6 -0
  66. package/crypto/words.js +48 -0
  67. package/define.d.ts +77 -0
  68. package/entityCache.d.ts +7 -0
  69. package/entityCache.js +79 -0
  70. package/errors/Common.d.ts +72 -0
  71. package/errors/Common.js +122 -0
  72. package/errors/RPCBaseErrors.d.ts +84 -0
  73. package/errors/RPCBaseErrors.js +134 -0
  74. package/errors/RPCErrorList.d.ts +37 -0
  75. package/errors/RPCErrorList.js +117 -0
  76. package/errors/index.d.ts +11 -0
  77. package/errors/index.js +29 -0
  78. package/events/Album.d.ts +36 -0
  79. package/events/Album.js +104 -0
  80. package/events/CallbackQuery.d.ts +74 -0
  81. package/events/CallbackQuery.js +193 -0
  82. package/events/DeletedMessage.d.ts +45 -0
  83. package/events/DeletedMessage.js +63 -0
  84. package/events/EditedMessage.d.ts +30 -0
  85. package/events/EditedMessage.js +41 -0
  86. package/events/NewMessage.d.ts +89 -0
  87. package/events/NewMessage.js +182 -0
  88. package/events/Raw.d.ts +29 -0
  89. package/events/Raw.js +43 -0
  90. package/events/common.d.ts +66 -0
  91. package/events/common.js +141 -0
  92. package/events/index.d.ts +2 -0
  93. package/events/index.js +8 -0
  94. package/extensions/AsyncQueue.d.ts +10 -0
  95. package/extensions/AsyncQueue.js +32 -0
  96. package/extensions/BinaryReader.d.ts +100 -0
  97. package/extensions/BinaryReader.js +242 -0
  98. package/extensions/BinaryWriter.d.ts +7 -0
  99. package/extensions/BinaryWriter.js +15 -0
  100. package/extensions/Deferred.d.ts +8 -0
  101. package/extensions/Deferred.js +16 -0
  102. package/extensions/Logger.d.ts +56 -0
  103. package/extensions/Logger.js +107 -0
  104. package/extensions/MessagePacker.d.ts +22 -0
  105. package/extensions/MessagePacker.js +154 -0
  106. package/extensions/PendingState.d.ts +12 -0
  107. package/extensions/PendingState.js +29 -0
  108. package/extensions/PromisedNetSockets.d.ts +25 -0
  109. package/extensions/PromisedNetSockets.js +178 -0
  110. package/extensions/PromisedWebSockets.d.ts +19 -0
  111. package/extensions/PromisedWebSockets.js +124 -0
  112. package/extensions/html.d.ts +5 -0
  113. package/extensions/html.js +213 -0
  114. package/extensions/index.d.ts +7 -0
  115. package/extensions/index.js +17 -0
  116. package/extensions/markdown.d.ts +5 -0
  117. package/extensions/markdown.js +76 -0
  118. package/extensions/markdownv2.d.ts +5 -0
  119. package/extensions/markdownv2.js +51 -0
  120. package/extensions/net.d.ts +1 -0
  121. package/extensions/net.js +13 -0
  122. package/extensions/socks.d.ts +1 -0
  123. package/extensions/socks.js +13 -0
  124. package/index.d.ts +14 -0
  125. package/index.js +48 -0
  126. package/inspect.d.ts +1 -0
  127. package/inspect.js +5 -0
  128. package/network/Authenticator.d.ts +12 -0
  129. package/network/Authenticator.js +193 -0
  130. package/network/MTProtoPlainSender.d.ts +19 -0
  131. package/network/MTProtoPlainSender.js +74 -0
  132. package/network/MTProtoSender.d.ts +291 -0
  133. package/network/MTProtoSender.js +854 -0
  134. package/network/MTProtoState.d.ts +103 -0
  135. package/network/MTProtoState.js +267 -0
  136. package/network/RequestState.d.ts +19 -0
  137. package/network/RequestState.js +35 -0
  138. package/network/connection/Connection.d.ts +71 -0
  139. package/network/connection/Connection.js +157 -0
  140. package/network/connection/TCPAbridged.d.ts +20 -0
  141. package/network/connection/TCPAbridged.js +58 -0
  142. package/network/connection/TCPFull.d.ts +17 -0
  143. package/network/connection/TCPFull.js +61 -0
  144. package/network/connection/TCPMTProxy.d.ts +50 -0
  145. package/network/connection/TCPMTProxy.js +121 -0
  146. package/network/connection/TCPObfuscated.d.ts +19 -0
  147. package/network/connection/TCPObfuscated.js +78 -0
  148. package/network/connection/index.d.ts +4 -0
  149. package/network/connection/index.js +11 -0
  150. package/network/index.d.ts +11 -0
  151. package/network/index.js +23 -0
  152. package/package.json +69 -0
  153. package/requestIter.d.ts +24 -0
  154. package/requestIter.js +107 -0
  155. package/sessions/Abstract.d.ts +103 -0
  156. package/sessions/Abstract.js +6 -0
  157. package/sessions/CacheApiSession.d.ts +18 -0
  158. package/sessions/CacheApiSession.js +99 -0
  159. package/sessions/Memory.d.ts +38 -0
  160. package/sessions/Memory.js +272 -0
  161. package/sessions/StoreSession.d.ts +14 -0
  162. package/sessions/StoreSession.js +77 -0
  163. package/sessions/StringSession.d.ts +33 -0
  164. package/sessions/StringSession.js +116 -0
  165. package/sessions/index.d.ts +4 -0
  166. package/sessions/index.js +13 -0
  167. package/sessions/localStorage.d.ts +1 -0
  168. package/sessions/localStorage.js +4 -0
  169. package/tl/AllTLObjects.d.ts +3 -0
  170. package/tl/AllTLObjects.js +17 -0
  171. package/tl/MTProtoRequest.d.ts +19 -0
  172. package/tl/MTProtoRequest.js +38 -0
  173. package/tl/api.d.ts +31425 -0
  174. package/tl/api.js +507 -0
  175. package/tl/apiTl.d.ts +2 -0
  176. package/tl/apiTl.js +2142 -0
  177. package/tl/core/GZIPPacked.d.ts +16 -0
  178. package/tl/core/GZIPPacked.js +51 -0
  179. package/tl/core/MessageContainer.d.ts +12 -0
  180. package/tl/core/MessageContainer.js +42 -0
  181. package/tl/core/RPCResult.d.ts +15 -0
  182. package/tl/core/RPCResult.js +32 -0
  183. package/tl/core/TLMessage.d.ts +10 -0
  184. package/tl/core/TLMessage.js +14 -0
  185. package/tl/core/index.d.ts +6 -0
  186. package/tl/core/index.js +16 -0
  187. package/tl/custom/button.d.ts +25 -0
  188. package/tl/custom/button.js +78 -0
  189. package/tl/custom/chatGetter.d.ts +30 -0
  190. package/tl/custom/chatGetter.js +114 -0
  191. package/tl/custom/dialog.d.ts +31 -0
  192. package/tl/custom/dialog.js +40 -0
  193. package/tl/custom/draft.d.ts +22 -0
  194. package/tl/custom/draft.js +48 -0
  195. package/tl/custom/file.d.ts +22 -0
  196. package/tl/custom/file.js +68 -0
  197. package/tl/custom/forward.d.ts +16 -0
  198. package/tl/custom/forward.js +47 -0
  199. package/tl/custom/index.d.ts +1 -0
  200. package/tl/custom/index.js +5 -0
  201. package/tl/custom/inlineResult.d.ts +33 -0
  202. package/tl/custom/inlineResult.js +87 -0
  203. package/tl/custom/inlineResults.d.ts +21 -0
  204. package/tl/custom/inlineResults.js +26 -0
  205. package/tl/custom/message.d.ts +428 -0
  206. package/tl/custom/message.js +702 -0
  207. package/tl/custom/messageButton.d.ts +55 -0
  208. package/tl/custom/messageButton.js +152 -0
  209. package/tl/custom/senderGetter.d.ts +29 -0
  210. package/tl/custom/senderGetter.js +55 -0
  211. package/tl/generateModule.d.ts +1 -0
  212. package/tl/generateModule.js +17 -0
  213. package/tl/generationHelpers.d.ts +12 -0
  214. package/tl/generationHelpers.js +289 -0
  215. package/tl/index.d.ts +3 -0
  216. package/tl/index.js +10 -0
  217. package/tl/patched/index.d.ts +2 -0
  218. package/tl/patched/index.js +77 -0
  219. package/tl/schemaTl.d.ts +2 -0
  220. package/tl/schemaTl.js +64 -0
  221. package/tl/types-generator/generate.d.ts +1 -0
  222. package/tl/types-generator/generate.js +84 -0
  223. package/tl/types-generator/template.d.ts +6 -0
  224. package/tl/types-generator/template.js +257 -0
@@ -0,0 +1,598 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
21
+ var __asyncValues = (this && this.__asyncValues) || function (o) {
22
+ if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
23
+ var m = o[Symbol.asyncIterator], i;
24
+ return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
25
+ function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
26
+ function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
27
+ };
28
+ var __importDefault = (this && this.__importDefault) || function (mod) {
29
+ return (mod && mod.__esModule) ? mod : { "default": mod };
30
+ };
31
+ Object.defineProperty(exports, "__esModule", { value: true });
32
+ exports.downloadProfilePhoto = exports._downloadPhoto = exports._downloadCachedPhotoSize = exports._downloadWebDocument = exports._downloadContact = exports._downloadDocument = exports.downloadMedia = exports.downloadFileV2 = exports.iterDownload = exports.GenericDownloadIter = exports.DirectDownloadIter = void 0;
33
+ const tl_1 = require("../tl");
34
+ const Utils_1 = require("../Utils");
35
+ const Helpers_1 = require("../Helpers");
36
+ const __1 = require("..");
37
+ const requestIter_1 = require("../requestIter");
38
+ const errors_1 = require("../errors");
39
+ const fs_1 = require("./fs");
40
+ const extensions_1 = require("../extensions");
41
+ const fs = __importStar(require("./fs"));
42
+ const path_1 = __importDefault(require("./path"));
43
+ const big_integer_1 = __importDefault(require("big-integer"));
44
+ // All types
45
+ const sizeTypes = ["w", "y", "d", "x", "c", "m", "b", "a", "s"];
46
+ // Chunk sizes for `upload.getFile` must be multiple of the smallest size
47
+ const MIN_CHUNK_SIZE = 4096;
48
+ const DEFAULT_CHUNK_SIZE = 64; // kb
49
+ const ONE_MB = 1024 * 1024;
50
+ const REQUEST_TIMEOUT = 15000;
51
+ const DISCONNECT_SLEEP = 1000;
52
+ const TIMED_OUT_SLEEP = 1000;
53
+ const MAX_CHUNK_SIZE = 512 * 1024;
54
+ class DirectDownloadIter extends requestIter_1.RequestIter {
55
+ constructor() {
56
+ super(...arguments);
57
+ this._timedOut = false;
58
+ }
59
+ async _init({ fileLocation, dcId, offset, stride, chunkSize, requestSize, fileSize, msgData, }) {
60
+ this.request = new tl_1.Api.upload.GetFile({
61
+ location: fileLocation,
62
+ offset,
63
+ limit: requestSize,
64
+ });
65
+ this.total = fileSize;
66
+ this._stride = stride;
67
+ this._chunkSize = chunkSize;
68
+ this._lastPart = undefined;
69
+ //this._msgData = msgData;
70
+ this._timedOut = false;
71
+ this._sender = await this.client.getSender(dcId);
72
+ }
73
+ async _loadNextChunk() {
74
+ const current = await this._request();
75
+ this.buffer.push(current);
76
+ if (current.length < this.request.limit) {
77
+ // we finished downloading
78
+ this.left = this.buffer.length;
79
+ await this.close();
80
+ return true;
81
+ }
82
+ else {
83
+ this.request.offset = this.request.offset.add(this._stride);
84
+ }
85
+ }
86
+ async _request() {
87
+ try {
88
+ this._sender = await this.client.getSender(this._sender.dcId);
89
+ const result = await this.client.invokeWithSender(this.request, this._sender);
90
+ this._timedOut = false;
91
+ if (result instanceof tl_1.Api.upload.FileCdnRedirect) {
92
+ throw new Error("CDN Not supported. Please Add an issue in github");
93
+ }
94
+ return result.bytes;
95
+ }
96
+ catch (e) {
97
+ if (e.errorMessage == "TIMEOUT") {
98
+ if (this._timedOut) {
99
+ this.client._log.warn("Got two timeouts in a row while downloading file");
100
+ throw e;
101
+ }
102
+ this._timedOut = true;
103
+ this.client._log.info("Got timeout while downloading file, retrying once");
104
+ await (0, Helpers_1.sleep)(TIMED_OUT_SLEEP);
105
+ return await this._request();
106
+ }
107
+ else if (e instanceof errors_1.FileMigrateError) {
108
+ this.client._log.info("File lives in another DC");
109
+ this._sender = await this.client.getSender(e.newDc);
110
+ return await this._request();
111
+ }
112
+ else if (e.errorMessage == "FILEREF_UPGRADE_NEEDED") {
113
+ // TODO later
114
+ throw e;
115
+ }
116
+ else {
117
+ throw e;
118
+ }
119
+ }
120
+ }
121
+ async close() {
122
+ this.client._log.debug("Finished downloading file ...");
123
+ }
124
+ [Symbol.asyncIterator]() {
125
+ return super[Symbol.asyncIterator]();
126
+ }
127
+ }
128
+ exports.DirectDownloadIter = DirectDownloadIter;
129
+ class GenericDownloadIter extends DirectDownloadIter {
130
+ async _loadNextChunk() {
131
+ // 1. Fetch enough for one chunk
132
+ let data = Buffer.alloc(0);
133
+ // 1.1. ``bad`` is how much into the data we have we need to offset
134
+ const bad = this.request.offset.mod(this.request.limit).toJSNumber();
135
+ const before = this.request.offset;
136
+ // 1.2. We have to fetch from a valid offset, so remove that bad part
137
+ this.request.offset = this.request.offset.subtract(bad);
138
+ let done = false;
139
+ while (!done && data.length - bad < this._chunkSize) {
140
+ const current = await this._request();
141
+ this.request.offset = this.request.offset.add(this.request.limit);
142
+ data = Buffer.concat([data, current]);
143
+ done = current.length < this.request.limit;
144
+ }
145
+ // 1.3 Restore our last desired offset
146
+ this.request.offset = before;
147
+ // 2. Fill the buffer with the data we have
148
+ // 2.1. The current chunk starts at ``bad`` offset into the data,
149
+ // and each new chunk is ``stride`` bytes apart of the other
150
+ for (let i = bad; i < data.length; i += this._stride) {
151
+ this.buffer.push(data.slice(i, i + this._chunkSize));
152
+ // 2.2. We will yield this offset, so move to the next one
153
+ this.request.offset = this.request.offset.add(this._stride);
154
+ }
155
+ // 2.3. If we are in the last chunk, we will return the last partial data
156
+ if (done) {
157
+ this.left = this.buffer.length;
158
+ await this.close();
159
+ return;
160
+ }
161
+ // 2.4 If we are not done, we can't return incomplete chunks.
162
+ if (this.buffer[this.buffer.length - 1].length != this._chunkSize) {
163
+ this._lastPart = this.buffer.pop();
164
+ // 3. Be careful with the offsets. Re-fetching a bit of data
165
+ // is fine, since it greatly simplifies things.
166
+ // TODO Try to not re-fetch data
167
+ this.request.offset = this.request.offset.subtract(this._stride);
168
+ }
169
+ }
170
+ }
171
+ exports.GenericDownloadIter = GenericDownloadIter;
172
+ /** @hidden */
173
+ function iterDownload(client, { file, offset = big_integer_1.default.zero, stride, limit, chunkSize, requestSize = MAX_CHUNK_SIZE, fileSize, dcId, msgData, }) {
174
+ // we're ignoring here to make it more flexible (which is probably a bad idea)
175
+ // @ts-ignore
176
+ const info = __1.utils.getFileInfo(file);
177
+ if (info.dcId != undefined) {
178
+ dcId = info.dcId;
179
+ }
180
+ if (fileSize == undefined) {
181
+ fileSize = info.size;
182
+ }
183
+ file = info.location;
184
+ if (chunkSize == undefined) {
185
+ chunkSize = requestSize;
186
+ }
187
+ if (limit == undefined && fileSize != undefined) {
188
+ limit = Math.floor(fileSize.add(chunkSize).subtract(1).divide(chunkSize).toJSNumber());
189
+ }
190
+ if (stride == undefined) {
191
+ stride = chunkSize;
192
+ }
193
+ else if (stride < chunkSize) {
194
+ throw new Error("Stride must be >= chunkSize");
195
+ }
196
+ requestSize -= requestSize % MIN_CHUNK_SIZE;
197
+ if (requestSize < MIN_CHUNK_SIZE) {
198
+ requestSize = MIN_CHUNK_SIZE;
199
+ }
200
+ else if (requestSize > MAX_CHUNK_SIZE) {
201
+ requestSize = MAX_CHUNK_SIZE;
202
+ }
203
+ let cls;
204
+ if (chunkSize == requestSize &&
205
+ offset.divide(MAX_CHUNK_SIZE).eq(big_integer_1.default.zero) &&
206
+ stride % MIN_CHUNK_SIZE == 0 &&
207
+ (limit == undefined || offset.divide(limit).eq(big_integer_1.default.zero))) {
208
+ cls = DirectDownloadIter;
209
+ client._log.info(`Starting direct file download in chunks of ${requestSize} at ${offset}, stride ${stride}`);
210
+ }
211
+ else {
212
+ cls = GenericDownloadIter;
213
+ client._log.info(`Starting indirect file download in chunks of ${requestSize} at ${offset}, stride ${stride}`);
214
+ }
215
+ return new cls(client, limit, {}, {
216
+ fileLocation: file,
217
+ dcId,
218
+ offset,
219
+ stride,
220
+ chunkSize,
221
+ requestSize,
222
+ fileSize,
223
+ msgData,
224
+ });
225
+ }
226
+ exports.iterDownload = iterDownload;
227
+ function getWriter(outputFile) {
228
+ if (!outputFile || Buffer.isBuffer(outputFile)) {
229
+ return new extensions_1.BinaryWriter(Buffer.alloc(0));
230
+ }
231
+ else if (typeof outputFile == "string") {
232
+ // We want to make sure that the path exists.
233
+ return (0, fs_1.createWriteStream)(outputFile);
234
+ }
235
+ else {
236
+ return outputFile;
237
+ }
238
+ }
239
+ function closeWriter(writer) {
240
+ if ("close" in writer && writer.close) {
241
+ writer.close();
242
+ }
243
+ }
244
+ function returnWriterValue(writer) {
245
+ if (writer instanceof extensions_1.BinaryWriter) {
246
+ return writer.getValue();
247
+ }
248
+ if (writer instanceof fs.WriteStream) {
249
+ if (typeof writer.path == "string") {
250
+ return path_1.default.resolve(writer.path);
251
+ }
252
+ else {
253
+ return Buffer.from(writer.path);
254
+ }
255
+ }
256
+ }
257
+ /** @hidden */
258
+ async function downloadFileV2(client, inputLocation, { outputFile = undefined, partSizeKb = undefined, fileSize = undefined, progressCallback = undefined, dcId = undefined, msgData = undefined, }) {
259
+ var e_1, _a;
260
+ if (!partSizeKb) {
261
+ if (!fileSize) {
262
+ partSizeKb = 64;
263
+ }
264
+ else {
265
+ partSizeKb = __1.utils.getAppropriatedPartSize(fileSize);
266
+ }
267
+ }
268
+ const partSize = Math.floor(partSizeKb * 1024);
269
+ if (partSize % MIN_CHUNK_SIZE != 0) {
270
+ throw new Error("The part size must be evenly divisible by 4096");
271
+ }
272
+ const writer = getWriter(outputFile);
273
+ let downloaded = big_integer_1.default.zero;
274
+ try {
275
+ try {
276
+ for (var _b = __asyncValues(iterDownload(client, {
277
+ file: inputLocation,
278
+ requestSize: partSize,
279
+ dcId: dcId,
280
+ msgData: msgData,
281
+ })), _c; _c = await _b.next(), !_c.done;) {
282
+ const chunk = _c.value;
283
+ await writer.write(chunk);
284
+ downloaded = downloaded.add(chunk.length);
285
+ if (progressCallback) {
286
+ await progressCallback(downloaded, (0, big_integer_1.default)(fileSize || big_integer_1.default.zero));
287
+ }
288
+ }
289
+ }
290
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
291
+ finally {
292
+ try {
293
+ if (_c && !_c.done && (_a = _b.return)) await _a.call(_b);
294
+ }
295
+ finally { if (e_1) throw e_1.error; }
296
+ }
297
+ return returnWriterValue(writer);
298
+ }
299
+ finally {
300
+ closeWriter(writer);
301
+ }
302
+ }
303
+ exports.downloadFileV2 = downloadFileV2;
304
+ class Foreman {
305
+ constructor(maxWorkers) {
306
+ this.maxWorkers = maxWorkers;
307
+ this.activeWorkers = 0;
308
+ }
309
+ requestWorker() {
310
+ this.activeWorkers++;
311
+ if (this.activeWorkers > this.maxWorkers) {
312
+ this.deferred = createDeferred();
313
+ return this.deferred.promise;
314
+ }
315
+ return Promise.resolve();
316
+ }
317
+ releaseWorker() {
318
+ this.activeWorkers--;
319
+ if (this.deferred && this.activeWorkers <= this.maxWorkers) {
320
+ this.deferred.resolve();
321
+ }
322
+ }
323
+ }
324
+ function createDeferred() {
325
+ let resolve;
326
+ const promise = new Promise((_resolve) => {
327
+ resolve = _resolve;
328
+ });
329
+ return {
330
+ promise,
331
+ resolve: resolve,
332
+ };
333
+ }
334
+ /** @hidden */
335
+ async function downloadMedia(client, messageOrMedia, outputFile, thumb, progressCallback) {
336
+ /*
337
+ Downloading large documents may be slow enough to require a new file reference
338
+ to be obtained mid-download. Store (input chat, message id) so that the message
339
+ can be re-fetched.
340
+ */
341
+ let msgData;
342
+ let date;
343
+ let media;
344
+ if (messageOrMedia instanceof tl_1.Api.Message) {
345
+ media = messageOrMedia.media;
346
+ date = messageOrMedia.date;
347
+ msgData = messageOrMedia.inputChat
348
+ ? [messageOrMedia.inputChat, messageOrMedia.id]
349
+ : undefined;
350
+ }
351
+ else {
352
+ media = messageOrMedia;
353
+ date = Date.now();
354
+ }
355
+ if (typeof media == "string") {
356
+ throw new Error("not implemented");
357
+ }
358
+ if (media instanceof tl_1.Api.MessageMediaWebPage) {
359
+ if (media.webpage instanceof tl_1.Api.WebPage) {
360
+ media = media.webpage.document || media.webpage.photo;
361
+ }
362
+ }
363
+ if (media instanceof tl_1.Api.MessageMediaPhoto || media instanceof tl_1.Api.Photo) {
364
+ return _downloadPhoto(client, media, outputFile, date, thumb, progressCallback);
365
+ }
366
+ else if (media instanceof tl_1.Api.MessageMediaDocument ||
367
+ media instanceof tl_1.Api.Document) {
368
+ return _downloadDocument(client, media, outputFile, date, thumb, progressCallback, msgData);
369
+ }
370
+ else if (media instanceof tl_1.Api.MessageMediaContact) {
371
+ return _downloadContact(client, media, {});
372
+ }
373
+ else if (media instanceof tl_1.Api.WebDocument ||
374
+ media instanceof tl_1.Api.WebDocumentNoProxy) {
375
+ return _downloadWebDocument(client, media, {});
376
+ }
377
+ else {
378
+ return Buffer.alloc(0);
379
+ }
380
+ }
381
+ exports.downloadMedia = downloadMedia;
382
+ /** @hidden */
383
+ async function _downloadDocument(client, doc, outputFile, date, thumb, progressCallback, msgData) {
384
+ if (doc instanceof tl_1.Api.MessageMediaDocument) {
385
+ if (!doc.document) {
386
+ return Buffer.alloc(0);
387
+ }
388
+ doc = doc.document;
389
+ }
390
+ if (!(doc instanceof tl_1.Api.Document)) {
391
+ return Buffer.alloc(0);
392
+ }
393
+ let size;
394
+ if (thumb == undefined) {
395
+ outputFile = getProperFilename(outputFile, "document", "." + (__1.utils.getExtension(doc) || "bin"), date);
396
+ }
397
+ else {
398
+ outputFile = getProperFilename(outputFile, "photo", ".jpg", date);
399
+ size = getThumb(doc.thumbs || [], thumb);
400
+ if (size instanceof tl_1.Api.PhotoCachedSize ||
401
+ size instanceof tl_1.Api.PhotoStrippedSize) {
402
+ return _downloadCachedPhotoSize(size, outputFile);
403
+ }
404
+ }
405
+ return await downloadFileV2(client, new tl_1.Api.InputDocumentFileLocation({
406
+ id: doc.id,
407
+ accessHash: doc.accessHash,
408
+ fileReference: doc.fileReference,
409
+ thumbSize: size && "type" in size ? size.type : "",
410
+ }), {
411
+ outputFile: outputFile,
412
+ fileSize: size && "size" in size ? (0, big_integer_1.default)(size.size) : doc.size,
413
+ progressCallback: progressCallback,
414
+ msgData: msgData,
415
+ });
416
+ }
417
+ exports._downloadDocument = _downloadDocument;
418
+ /** @hidden */
419
+ async function _downloadContact(client, media, args) {
420
+ throw new Error("not implemented");
421
+ }
422
+ exports._downloadContact = _downloadContact;
423
+ /** @hidden */
424
+ async function _downloadWebDocument(client, media, args) {
425
+ throw new Error("not implemented");
426
+ }
427
+ exports._downloadWebDocument = _downloadWebDocument;
428
+ function pickFileSize(sizes, sizeType) {
429
+ if (!sizeType || !sizes || !sizes.length) {
430
+ return undefined;
431
+ }
432
+ const indexOfSize = sizeTypes.indexOf(sizeType);
433
+ let size;
434
+ for (let i = indexOfSize; i < sizeTypes.length; i++) {
435
+ size = sizes.find((s) => s.type === sizeTypes[i]);
436
+ if (size && !(size instanceof tl_1.Api.PhotoPathSize)) {
437
+ return size;
438
+ }
439
+ }
440
+ return undefined;
441
+ }
442
+ /** @hidden */
443
+ function getThumb(thumbs, thumb) {
444
+ function sortThumb(thumb) {
445
+ if (thumb instanceof tl_1.Api.PhotoStrippedSize) {
446
+ return thumb.bytes.length;
447
+ }
448
+ if (thumb instanceof tl_1.Api.PhotoCachedSize) {
449
+ return thumb.bytes.length;
450
+ }
451
+ if (thumb instanceof tl_1.Api.PhotoSize) {
452
+ return thumb.size;
453
+ }
454
+ if (thumb instanceof tl_1.Api.PhotoSizeProgressive) {
455
+ return Math.max(...thumb.sizes);
456
+ }
457
+ if (thumb instanceof tl_1.Api.VideoSize) {
458
+ return thumb.size;
459
+ }
460
+ return 0;
461
+ }
462
+ thumbs = thumbs.sort((a, b) => sortThumb(a) - sortThumb(b));
463
+ const correctThumbs = [];
464
+ for (const t of thumbs) {
465
+ if (!(t instanceof tl_1.Api.PhotoPathSize)) {
466
+ correctThumbs.push(t);
467
+ }
468
+ }
469
+ if (thumb == undefined) {
470
+ return correctThumbs.pop();
471
+ }
472
+ else if (typeof thumb == "number") {
473
+ return correctThumbs[thumb];
474
+ }
475
+ else if (typeof thumb == "string") {
476
+ for (const t of correctThumbs) {
477
+ if ("type" in t && t.type == thumb) {
478
+ return t;
479
+ }
480
+ }
481
+ }
482
+ else if (thumb instanceof tl_1.Api.PhotoSize ||
483
+ thumb instanceof tl_1.Api.PhotoCachedSize ||
484
+ thumb instanceof tl_1.Api.PhotoStrippedSize ||
485
+ thumb instanceof tl_1.Api.VideoSize) {
486
+ return thumb;
487
+ }
488
+ }
489
+ /** @hidden */
490
+ async function _downloadCachedPhotoSize(size, outputFile) {
491
+ // No need to download anything, simply write the bytes
492
+ let data;
493
+ if (size instanceof tl_1.Api.PhotoStrippedSize) {
494
+ data = (0, Utils_1.strippedPhotoToJpg)(size.bytes);
495
+ }
496
+ else {
497
+ data = size.bytes;
498
+ }
499
+ const writer = getWriter(outputFile);
500
+ try {
501
+ await writer.write(data);
502
+ }
503
+ finally {
504
+ closeWriter(writer);
505
+ }
506
+ return returnWriterValue(writer);
507
+ }
508
+ exports._downloadCachedPhotoSize = _downloadCachedPhotoSize;
509
+ /** @hidden */
510
+ function getProperFilename(file, fileType, extension, date) {
511
+ if (!file || typeof file != "string") {
512
+ return file;
513
+ }
514
+ if (fs.existsSync(file) && fs.lstatSync(file).isDirectory()) {
515
+ let fullName = fileType + date + extension;
516
+ return path_1.default.join(file, fullName);
517
+ }
518
+ return file;
519
+ }
520
+ /** @hidden */
521
+ async function _downloadPhoto(client, photo, file, date, thumb, progressCallback) {
522
+ if (photo instanceof tl_1.Api.MessageMediaPhoto) {
523
+ if (photo.photo instanceof tl_1.Api.PhotoEmpty || !photo.photo) {
524
+ return Buffer.alloc(0);
525
+ }
526
+ photo = photo.photo;
527
+ }
528
+ if (!(photo instanceof tl_1.Api.Photo)) {
529
+ return Buffer.alloc(0);
530
+ }
531
+ const photoSizes = [...(photo.sizes || []), ...(photo.videoSizes || [])];
532
+ const size = getThumb(photoSizes, thumb);
533
+ if (!size || size instanceof tl_1.Api.PhotoSizeEmpty) {
534
+ return Buffer.alloc(0);
535
+ }
536
+ if (!date) {
537
+ date = Date.now();
538
+ }
539
+ file = getProperFilename(file, "photo", ".jpg", date);
540
+ if (size instanceof tl_1.Api.PhotoCachedSize ||
541
+ size instanceof tl_1.Api.PhotoStrippedSize) {
542
+ return _downloadCachedPhotoSize(size, file);
543
+ }
544
+ let fileSize;
545
+ if (size instanceof tl_1.Api.PhotoSizeProgressive) {
546
+ fileSize = Math.max(...size.sizes);
547
+ }
548
+ else {
549
+ fileSize = "size" in size ? size.size : 512;
550
+ }
551
+ return downloadFileV2(client, new tl_1.Api.InputPhotoFileLocation({
552
+ id: photo.id,
553
+ accessHash: photo.accessHash,
554
+ fileReference: photo.fileReference,
555
+ thumbSize: "type" in size ? size.type : "",
556
+ }), {
557
+ outputFile: file,
558
+ fileSize: (0, big_integer_1.default)(fileSize),
559
+ progressCallback: progressCallback,
560
+ dcId: photo.dcId,
561
+ });
562
+ }
563
+ exports._downloadPhoto = _downloadPhoto;
564
+ /** @hidden */
565
+ async function downloadProfilePhoto(client, entity, fileParams) {
566
+ let photo;
567
+ if (typeof entity == "object" && "photo" in entity) {
568
+ photo = entity.photo;
569
+ }
570
+ else {
571
+ entity = await client.getEntity(entity);
572
+ if ("photo" in entity) {
573
+ photo = entity.photo;
574
+ }
575
+ else {
576
+ throw new Error(`Could not get photo from ${entity ? entity.className : undefined}`);
577
+ }
578
+ }
579
+ let dcId;
580
+ let loc;
581
+ if (photo instanceof tl_1.Api.UserProfilePhoto ||
582
+ photo instanceof tl_1.Api.ChatPhoto) {
583
+ dcId = photo.dcId;
584
+ loc = new tl_1.Api.InputPeerPhotoFileLocation({
585
+ peer: __1.utils.getInputPeer(entity),
586
+ photoId: photo.photoId,
587
+ big: fileParams.isBig,
588
+ });
589
+ }
590
+ else {
591
+ return Buffer.alloc(0);
592
+ }
593
+ return client.downloadFile(loc, {
594
+ outputFile: fileParams.outputFile,
595
+ dcId,
596
+ });
597
+ }
598
+ exports.downloadProfilePhoto = downloadProfilePhoto;
package/client/fs.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from "fs";
package/client/fs.js ADDED
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ __exportStar(require("fs"), exports);
@@ -0,0 +1,15 @@
1
+ import * as twoFA from "./2fa";
2
+ import * as auth from "./auth";
3
+ import * as bots from "./bots";
4
+ import * as buttons from "./buttons";
5
+ import * as chats from "./chats";
6
+ import * as dialogs from "./dialogs";
7
+ import * as downloads from "./downloads";
8
+ import * as messageParse from "./messageParse";
9
+ import * as message from "./messages";
10
+ import * as telegramBaseClient from "./telegramBaseClient";
11
+ import * as tgClient from "./TelegramClient";
12
+ import * as updates from "./updates";
13
+ import * as uploads from "./uploads";
14
+ import * as users from "./users";
15
+ export { twoFA, auth, bots, buttons, chats, dialogs, downloads, messageParse, message, telegramBaseClient, tgClient, updates, uploads, users, };
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
21
+ Object.defineProperty(exports, "__esModule", { value: true });
22
+ exports.users = exports.uploads = exports.updates = exports.tgClient = exports.telegramBaseClient = exports.message = exports.messageParse = exports.downloads = exports.dialogs = exports.chats = exports.buttons = exports.bots = exports.auth = exports.twoFA = void 0;
23
+ const twoFA = __importStar(require("./2fa"));
24
+ exports.twoFA = twoFA;
25
+ const auth = __importStar(require("./auth"));
26
+ exports.auth = auth;
27
+ const bots = __importStar(require("./bots"));
28
+ exports.bots = bots;
29
+ const buttons = __importStar(require("./buttons"));
30
+ exports.buttons = buttons;
31
+ const chats = __importStar(require("./chats"));
32
+ exports.chats = chats;
33
+ const dialogs = __importStar(require("./dialogs"));
34
+ exports.dialogs = dialogs;
35
+ const downloads = __importStar(require("./downloads"));
36
+ exports.downloads = downloads;
37
+ const messageParse = __importStar(require("./messageParse"));
38
+ exports.messageParse = messageParse;
39
+ const message = __importStar(require("./messages"));
40
+ exports.message = message;
41
+ const telegramBaseClient = __importStar(require("./telegramBaseClient"));
42
+ exports.telegramBaseClient = telegramBaseClient;
43
+ const tgClient = __importStar(require("./TelegramClient"));
44
+ exports.tgClient = tgClient;
45
+ const updates = __importStar(require("./updates"));
46
+ exports.updates = updates;
47
+ const uploads = __importStar(require("./uploads"));
48
+ exports.uploads = uploads;
49
+ const users = __importStar(require("./users"));
50
+ exports.users = users;
@@ -0,0 +1,17 @@
1
+ import { Api } from "../tl/api";
2
+ import type { EntityLike } from "../define";
3
+ import type { TelegramClient } from "./TelegramClient";
4
+ export declare type messageEntities = typeof Api.MessageEntityBold | typeof Api.MessageEntityItalic | typeof Api.MessageEntityStrike | typeof Api.MessageEntityCode | typeof Api.MessageEntityPre;
5
+ export declare const DEFAULT_DELIMITERS: {
6
+ [key: string]: messageEntities;
7
+ };
8
+ export interface ParseInterface {
9
+ parse: (message: string) => [string, Api.TypeMessageEntity[]];
10
+ unparse: (text: string, entities: Api.TypeMessageEntity[]) => string;
11
+ }
12
+ /** @hidden */
13
+ export declare function _replaceWithMention(client: TelegramClient, entities: Api.TypeMessageEntity[], i: number, user: EntityLike): Promise<boolean>;
14
+ /** @hidden */
15
+ export declare function _parseMessageText(client: TelegramClient, message: string, parseMode: false | string | ParseInterface): Promise<[string, Api.TypeMessageEntity[]]>;
16
+ /** @hidden */
17
+ export declare function _getResponseMessage(client: TelegramClient, request: any, result: any, inputChat: any): Api.TypeMessage | Map<number, Api.Message> | (Api.Message | undefined)[] | undefined;