sroosh 1.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 (250) hide show
  1. package/CryptoFile-BROWSER.d.ts +2 -0
  2. package/CryptoFile-BROWSER.js +23 -0
  3. package/CryptoFile.d.ts +2 -0
  4. package/CryptoFile.js +23 -0
  5. package/Helpers.d.ts +151 -0
  6. package/Helpers.js +480 -0
  7. package/Password.d.ts +15 -0
  8. package/Password.js +271 -0
  9. package/README.md +105 -0
  10. package/Utils.d.ts +228 -0
  11. package/Utils.js +1248 -0
  12. package/Version.d.ts +1 -0
  13. package/Version.js +4 -0
  14. package/client/2fa.d.ts +48 -0
  15. package/client/2fa.js +109 -0
  16. package/client/TelegramClient.d.ts +1042 -0
  17. package/client/TelegramClient.js +1244 -0
  18. package/client/account.d.ts +0 -0
  19. package/client/account.js +1 -0
  20. package/client/auth.d.ts +93 -0
  21. package/client/auth.js +363 -0
  22. package/client/bots.d.ts +6 -0
  23. package/client/bots.js +24 -0
  24. package/client/buttons.d.ts +4 -0
  25. package/client/buttons.js +87 -0
  26. package/client/chats.d.ts +46 -0
  27. package/client/chats.js +350 -0
  28. package/client/dialogs.d.ts +52 -0
  29. package/client/dialogs.js +174 -0
  30. package/client/downloads.d.ts +157 -0
  31. package/client/downloads.js +598 -0
  32. package/client/fs-BROWSER.d.ts +10 -0
  33. package/client/fs-BROWSER.js +13 -0
  34. package/client/fs.d.ts +1 -0
  35. package/client/fs.js +13 -0
  36. package/client/index.d.ts +15 -0
  37. package/client/index.js +50 -0
  38. package/client/messageParse.d.ts +17 -0
  39. package/client/messageParse.js +191 -0
  40. package/client/messages.d.ts +259 -0
  41. package/client/messages.js +804 -0
  42. package/client/os-BROWSER.d.ts +5 -0
  43. package/client/os-BROWSER.js +10 -0
  44. package/client/os.d.ts +2 -0
  45. package/client/os.js +23 -0
  46. package/client/path-BROWSER.d.ts +7 -0
  47. package/client/path-BROWSER.js +8 -0
  48. package/client/path.d.ts +2 -0
  49. package/client/path.js +7 -0
  50. package/client/telegramBaseClient.d.ts +239 -0
  51. package/client/telegramBaseClient.js +369 -0
  52. package/client/updates.d.ts +31 -0
  53. package/client/updates.js +254 -0
  54. package/client/uploads.d.ts +137 -0
  55. package/client/uploads.js +514 -0
  56. package/client/users.d.ts +29 -0
  57. package/client/users.js +491 -0
  58. package/crypto/AuthKey.d.ts +20 -0
  59. package/crypto/AuthKey.js +76 -0
  60. package/crypto/CTR.d.ts +6 -0
  61. package/crypto/CTR.js +35 -0
  62. package/crypto/Factorizator.d.ts +19 -0
  63. package/crypto/Factorizator.js +74 -0
  64. package/crypto/IGE.d.ts +18 -0
  65. package/crypto/IGE.js +34 -0
  66. package/crypto/RSA.d.ts +15 -0
  67. package/crypto/RSA.js +150 -0
  68. package/crypto/converters.d.ts +19 -0
  69. package/crypto/converters.js +52 -0
  70. package/crypto/crypto.d.ts +27 -0
  71. package/crypto/crypto.js +117 -0
  72. package/crypto/words.d.ts +6 -0
  73. package/crypto/words.js +48 -0
  74. package/entityCache.d.ts +7 -0
  75. package/entityCache.js +79 -0
  76. package/errors/Common.d.ts +72 -0
  77. package/errors/Common.js +122 -0
  78. package/errors/RPCBaseErrors.d.ts +84 -0
  79. package/errors/RPCBaseErrors.js +134 -0
  80. package/errors/RPCErrorList.d.ts +37 -0
  81. package/errors/RPCErrorList.js +117 -0
  82. package/errors/index.d.ts +11 -0
  83. package/errors/index.js +29 -0
  84. package/events/Album.d.ts +36 -0
  85. package/events/Album.js +104 -0
  86. package/events/CallbackQuery.d.ts +74 -0
  87. package/events/CallbackQuery.js +193 -0
  88. package/events/DeletedMessage.d.ts +45 -0
  89. package/events/DeletedMessage.js +63 -0
  90. package/events/EditedMessage.d.ts +30 -0
  91. package/events/EditedMessage.js +41 -0
  92. package/events/NewMessage.d.ts +89 -0
  93. package/events/NewMessage.js +182 -0
  94. package/events/Raw.d.ts +29 -0
  95. package/events/Raw.js +43 -0
  96. package/events/common.d.ts +66 -0
  97. package/events/common.js +141 -0
  98. package/events/index.d.ts +2 -0
  99. package/events/index.js +8 -0
  100. package/extensions/AsyncQueue.d.ts +10 -0
  101. package/extensions/AsyncQueue.js +32 -0
  102. package/extensions/BinaryReader.d.ts +100 -0
  103. package/extensions/BinaryReader.js +242 -0
  104. package/extensions/BinaryWriter.d.ts +7 -0
  105. package/extensions/BinaryWriter.js +15 -0
  106. package/extensions/Deferred.d.ts +8 -0
  107. package/extensions/Deferred.js +16 -0
  108. package/extensions/Logger.d.ts +58 -0
  109. package/extensions/Logger.js +127 -0
  110. package/extensions/MessagePacker.d.ts +22 -0
  111. package/extensions/MessagePacker.js +154 -0
  112. package/extensions/PendingState.d.ts +12 -0
  113. package/extensions/PendingState.js +29 -0
  114. package/extensions/PromisedNetSockets-BROWSER.d.ts +8 -0
  115. package/extensions/PromisedNetSockets-BROWSER.js +12 -0
  116. package/extensions/PromisedNetSockets.d.ts +25 -0
  117. package/extensions/PromisedNetSockets.js +178 -0
  118. package/extensions/PromisedWebSockets.d.ts +19 -0
  119. package/extensions/PromisedWebSockets.js +134 -0
  120. package/extensions/html.d.ts +5 -0
  121. package/extensions/html.js +213 -0
  122. package/extensions/index.d.ts +7 -0
  123. package/extensions/index.js +17 -0
  124. package/extensions/markdown.d.ts +5 -0
  125. package/extensions/markdown.js +76 -0
  126. package/extensions/markdownv2.d.ts +5 -0
  127. package/extensions/markdownv2.js +51 -0
  128. package/extensions/net-BROWSER.d.ts +8 -0
  129. package/extensions/net-BROWSER.js +11 -0
  130. package/extensions/net.d.ts +1 -0
  131. package/extensions/net.js +13 -0
  132. package/extensions/socks-BROWSER.d.ts +3 -0
  133. package/extensions/socks-BROWSER.js +6 -0
  134. package/extensions/socks.d.ts +1 -0
  135. package/extensions/socks.js +13 -0
  136. package/index.d.ts +14 -0
  137. package/index.js +48 -0
  138. package/inspect-BROWSER.d.ts +3 -0
  139. package/inspect-BROWSER.js +6 -0
  140. package/inspect.d.ts +1 -0
  141. package/inspect.js +5 -0
  142. package/network/Authenticator.d.ts +12 -0
  143. package/network/Authenticator.js +193 -0
  144. package/network/MTProtoPlainSender.d.ts +19 -0
  145. package/network/MTProtoPlainSender.js +74 -0
  146. package/network/MTProtoSender.d.ts +291 -0
  147. package/network/MTProtoSender.js +854 -0
  148. package/network/MTProtoState.d.ts +103 -0
  149. package/network/MTProtoState.js +267 -0
  150. package/network/RequestState.d.ts +19 -0
  151. package/network/RequestState.js +35 -0
  152. package/network/connection/Connection.d.ts +71 -0
  153. package/network/connection/Connection.js +157 -0
  154. package/network/connection/ConnectionWebSocket.d.ts +19 -0
  155. package/network/connection/ConnectionWebSocket.js +77 -0
  156. package/network/connection/CustomIntermediatePacketCodec.d.ts +8 -0
  157. package/network/connection/CustomIntermediatePacketCodec.js +25 -0
  158. package/network/connection/CustomPromisedWebSockets.d.ts +22 -0
  159. package/network/connection/CustomPromisedWebSockets.js +170 -0
  160. package/network/connection/ObfuscatedIO.d.ts +12 -0
  161. package/network/connection/ObfuscatedIO.js +64 -0
  162. package/network/connection/TCPAbridged.d.ts +20 -0
  163. package/network/connection/TCPAbridged.js +58 -0
  164. package/network/connection/TCPFull.d.ts +17 -0
  165. package/network/connection/TCPFull.js +61 -0
  166. package/network/connection/TCPMTProxy.d.ts +50 -0
  167. package/network/connection/TCPMTProxy.js +121 -0
  168. package/network/connection/TCPObfuscated.d.ts +18 -0
  169. package/network/connection/TCPObfuscated.js +79 -0
  170. package/network/connection/index.d.ts +5 -0
  171. package/network/connection/index.js +13 -0
  172. package/network/index.d.ts +11 -0
  173. package/network/index.js +24 -0
  174. package/package.json +73 -0
  175. package/platform.d.ts +3 -0
  176. package/platform.js +6 -0
  177. package/requestIter.d.ts +24 -0
  178. package/requestIter.js +107 -0
  179. package/sessions/Abstract.d.ts +103 -0
  180. package/sessions/Abstract.js +6 -0
  181. package/sessions/CacheApiSession.d.ts +18 -0
  182. package/sessions/CacheApiSession.js +99 -0
  183. package/sessions/Memory.d.ts +38 -0
  184. package/sessions/Memory.js +272 -0
  185. package/sessions/StoreSession.d.ts +14 -0
  186. package/sessions/StoreSession.js +77 -0
  187. package/sessions/StringSession.d.ts +33 -0
  188. package/sessions/StringSession.js +116 -0
  189. package/sessions/index.d.ts +4 -0
  190. package/sessions/index.js +13 -0
  191. package/sessions/localStorage-BROWSER.d.ts +3 -0
  192. package/sessions/localStorage-BROWSER.js +9 -0
  193. package/sessions/localStorage.d.ts +1 -0
  194. package/sessions/localStorage.js +4 -0
  195. package/tl/AllTLObjects.d.ts +3 -0
  196. package/tl/AllTLObjects.js +17 -0
  197. package/tl/MTProtoRequest.d.ts +19 -0
  198. package/tl/MTProtoRequest.js +38 -0
  199. package/tl/api.d.ts +2 -0
  200. package/tl/api.js +507 -0
  201. package/tl/apiTl.d.ts +2 -0
  202. package/tl/apiTl.js +1638 -0
  203. package/tl/core/GZIPPacked.d.ts +16 -0
  204. package/tl/core/GZIPPacked.js +51 -0
  205. package/tl/core/MessageContainer.d.ts +12 -0
  206. package/tl/core/MessageContainer.js +42 -0
  207. package/tl/core/RPCResult.d.ts +15 -0
  208. package/tl/core/RPCResult.js +32 -0
  209. package/tl/core/TLMessage.d.ts +10 -0
  210. package/tl/core/TLMessage.js +14 -0
  211. package/tl/core/index.d.ts +6 -0
  212. package/tl/core/index.js +16 -0
  213. package/tl/custom/button.d.ts +25 -0
  214. package/tl/custom/button.js +78 -0
  215. package/tl/custom/chatGetter.d.ts +30 -0
  216. package/tl/custom/chatGetter.js +114 -0
  217. package/tl/custom/dialog.d.ts +31 -0
  218. package/tl/custom/dialog.js +40 -0
  219. package/tl/custom/draft.d.ts +22 -0
  220. package/tl/custom/draft.js +48 -0
  221. package/tl/custom/file.d.ts +22 -0
  222. package/tl/custom/file.js +68 -0
  223. package/tl/custom/forward.d.ts +16 -0
  224. package/tl/custom/forward.js +47 -0
  225. package/tl/custom/index.d.ts +1 -0
  226. package/tl/custom/index.js +5 -0
  227. package/tl/custom/inlineResult.d.ts +33 -0
  228. package/tl/custom/inlineResult.js +87 -0
  229. package/tl/custom/inlineResults.d.ts +21 -0
  230. package/tl/custom/inlineResults.js +26 -0
  231. package/tl/custom/message.d.ts +428 -0
  232. package/tl/custom/message.js +702 -0
  233. package/tl/custom/messageButton.d.ts +55 -0
  234. package/tl/custom/messageButton.js +152 -0
  235. package/tl/custom/senderGetter.d.ts +29 -0
  236. package/tl/custom/senderGetter.js +55 -0
  237. package/tl/generateModule.d.ts +1 -0
  238. package/tl/generateModule.js +17 -0
  239. package/tl/generationHelpers.d.ts +12 -0
  240. package/tl/generationHelpers.js +289 -0
  241. package/tl/index.d.ts +3 -0
  242. package/tl/index.js +10 -0
  243. package/tl/patched/index.d.ts +2 -0
  244. package/tl/patched/index.js +77 -0
  245. package/tl/schemaTl.d.ts +2 -0
  246. package/tl/schemaTl.js +64 -0
  247. package/tl/types-generator/generate.d.ts +1 -0
  248. package/tl/types-generator/generate.js +84 -0
  249. package/tl/types-generator/template.d.ts +6 -0
  250. package/tl/types-generator/template.js +257 -0
@@ -0,0 +1,99 @@
1
+ "use strict";
2
+ const MemorySession = require("./Memory");
3
+ const AuthKey = require("../crypto/AuthKey");
4
+ const utils = require("../Utils");
5
+ const CACHE_NAME = "GramJs";
6
+ const STORAGE_KEY_BASE = "GramJs-session-";
7
+ class CacheApi extends MemorySession {
8
+ constructor(sessionId) {
9
+ super();
10
+ this._storageKey = sessionId;
11
+ this._authKeys = {};
12
+ }
13
+ async load() {
14
+ if (!this._storageKey) {
15
+ return;
16
+ }
17
+ try {
18
+ const json = await fetchFromCache(this._storageKey);
19
+ const { mainDcId, keys, hashes } = JSON.parse(json);
20
+ const { ipAddress, port } = await utils.getDC(mainDcId);
21
+ this.setDC(mainDcId, ipAddress, port, true);
22
+ Object.keys(keys).forEach((dcId) => {
23
+ if (keys[dcId] && hashes[dcId]) {
24
+ this._authKeys[dcId] = new AuthKey(Buffer.from(keys[dcId].data), Buffer.from(hashes[dcId].data));
25
+ }
26
+ });
27
+ }
28
+ catch (err) {
29
+ throw new Error(`Failed to retrieve or parse JSON from Cache for key ${this._storageKey}`);
30
+ }
31
+ }
32
+ setDC(dcId, serverAddress, port, skipUpdateStorage = false) {
33
+ this._dcId = dcId;
34
+ this._serverAddress = serverAddress;
35
+ this._port = port;
36
+ delete this._authKeys[dcId];
37
+ if (!skipUpdateStorage) {
38
+ void this._updateStorage();
39
+ }
40
+ }
41
+ async save() {
42
+ if (!this._storageKey) {
43
+ this._storageKey = generateStorageKey();
44
+ }
45
+ await this._updateStorage();
46
+ return this._storageKey;
47
+ }
48
+ get authKey() {
49
+ throw new Error("Not supported");
50
+ }
51
+ set authKey(value) {
52
+ throw new Error("Not supported");
53
+ }
54
+ getAuthKey(dcId = this._dcId) {
55
+ return this._authKeys[dcId];
56
+ }
57
+ setAuthKey(authKey, dcId = this._dcId) {
58
+ this._authKeys[dcId] = authKey;
59
+ void this._updateStorage();
60
+ }
61
+ async _updateStorage() {
62
+ if (!this._storageKey) {
63
+ return;
64
+ }
65
+ const sessionData = {
66
+ mainDcId: this._dcId,
67
+ keys: {},
68
+ hashes: {},
69
+ };
70
+ Object.keys(this._authKeys).map((dcId) => {
71
+ const authKey = this._authKeys[dcId];
72
+ sessionData.keys[dcId] = authKey._key;
73
+ sessionData.hashes[dcId] = authKey._hash;
74
+ });
75
+ await saveToCache(this._storageKey, JSON.stringify(sessionData));
76
+ }
77
+ async delete() {
78
+ const request = new Request(this._storageKey);
79
+ const cache = await self.caches.open(CACHE_NAME);
80
+ await cache.delete(request);
81
+ }
82
+ }
83
+ function generateStorageKey() {
84
+ // Creating two sessions at the same moment is not expected nor supported.
85
+ return `${STORAGE_KEY_BASE}${Date.now()}`;
86
+ }
87
+ async function fetchFromCache(key) {
88
+ const request = new Request(key);
89
+ const cache = await self.caches.open(CACHE_NAME);
90
+ const cached = await cache.match(request);
91
+ return cached ? cached.text() : null;
92
+ }
93
+ async function saveToCache(key, data) {
94
+ const request = new Request(key);
95
+ const response = new Response(data);
96
+ const cache = await self.caches.open(CACHE_NAME);
97
+ return cache.put(request, response);
98
+ }
99
+ module.exports = CacheApi;
@@ -0,0 +1,38 @@
1
+ import { Session } from "./Abstract";
2
+ import type { AuthKey } from "../crypto/AuthKey";
3
+ import { Api } from "../tl";
4
+ import bigInt from "big-integer";
5
+ import type { EntityLike } from "../define";
6
+ export declare class MemorySession extends Session {
7
+ protected _serverAddress?: string;
8
+ protected _dcId: number;
9
+ protected _port?: number;
10
+ protected _takeoutId: undefined;
11
+ protected _entities: Set<any>;
12
+ protected _updateStates: {};
13
+ protected _authKey?: AuthKey;
14
+ constructor();
15
+ setDC(dcId: number, serverAddress: string, port: number): void;
16
+ get dcId(): number;
17
+ get serverAddress(): string;
18
+ get port(): number;
19
+ get authKey(): AuthKey | undefined;
20
+ set authKey(value: AuthKey | undefined);
21
+ get takeoutId(): undefined;
22
+ set takeoutId(value: undefined);
23
+ getAuthKey(dcId?: number): AuthKey | undefined;
24
+ setAuthKey(authKey?: AuthKey, dcId?: number): undefined;
25
+ close(): void;
26
+ save(): void;
27
+ load(): Promise<void>;
28
+ delete(): void;
29
+ _entityValuesToRow(id: bigInt.BigInteger | string, hash: bigInt.BigInteger | string, username: string, phone: string, name: string): (string | bigInt.BigInteger)[];
30
+ _entityToRow(e: any): (string | bigInt.BigInteger)[] | undefined;
31
+ _entitiesToRows(tlo: any): (string | bigInt.BigInteger)[][];
32
+ processEntities(tlo: any): void;
33
+ getEntityRowsByPhone(phone: string): any[] | undefined;
34
+ getEntityRowsByUsername(username: string): any[] | undefined;
35
+ getEntityRowsByName(name: string): any[] | undefined;
36
+ getEntityRowsById(id: string | bigInt.BigInteger, exact?: boolean): any[] | undefined;
37
+ getInputEntity(key: EntityLike): Api.TypeInputPeer;
38
+ }
@@ -0,0 +1,272 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.MemorySession = void 0;
7
+ const Abstract_1 = require("./Abstract");
8
+ const tl_1 = require("../tl");
9
+ const big_integer_1 = __importDefault(require("big-integer"));
10
+ const Utils_1 = require("../Utils");
11
+ const Helpers_1 = require("../Helpers");
12
+ const __1 = require("../");
13
+ class MemorySession extends Abstract_1.Session {
14
+ constructor() {
15
+ super();
16
+ this._serverAddress = undefined;
17
+ this._dcId = 0;
18
+ this._port = undefined;
19
+ this._takeoutId = undefined;
20
+ this._entities = new Set();
21
+ this._updateStates = {};
22
+ }
23
+ setDC(dcId, serverAddress, port) {
24
+ this._dcId = dcId | 0;
25
+ this._serverAddress = serverAddress;
26
+ this._port = port;
27
+ }
28
+ get dcId() {
29
+ return this._dcId;
30
+ }
31
+ get serverAddress() {
32
+ return this._serverAddress;
33
+ }
34
+ get port() {
35
+ return this._port;
36
+ }
37
+ get authKey() {
38
+ return this._authKey;
39
+ }
40
+ set authKey(value) {
41
+ this._authKey = value;
42
+ }
43
+ get takeoutId() {
44
+ return this._takeoutId;
45
+ }
46
+ set takeoutId(value) {
47
+ this._takeoutId = value;
48
+ }
49
+ getAuthKey(dcId) {
50
+ if (dcId && dcId !== this.dcId) {
51
+ // Not supported.
52
+ return undefined;
53
+ }
54
+ return this.authKey;
55
+ }
56
+ setAuthKey(authKey, dcId) {
57
+ if (dcId && dcId !== this.dcId) {
58
+ // Not supported.
59
+ return undefined;
60
+ }
61
+ this.authKey = authKey;
62
+ }
63
+ close() { }
64
+ save() { }
65
+ async load() { }
66
+ delete() { }
67
+ _entityValuesToRow(id, hash, username, phone, name) {
68
+ // While this is a simple implementation it might be overrode by,
69
+ // other classes so they don't need to implement the plural form
70
+ // of the method. Don't remove.
71
+ return [id, hash, username, phone, name];
72
+ }
73
+ _entityToRow(e) {
74
+ if (!(e.classType === "constructor")) {
75
+ return;
76
+ }
77
+ let p;
78
+ let markedId;
79
+ try {
80
+ p = (0, Utils_1.getInputPeer)(e, false);
81
+ markedId = (0, Utils_1.getPeerId)(p);
82
+ }
83
+ catch (e) {
84
+ return;
85
+ }
86
+ let pHash;
87
+ if (p instanceof tl_1.Api.InputPeerUser ||
88
+ p instanceof tl_1.Api.InputPeerChannel) {
89
+ pHash = p.accessHash;
90
+ }
91
+ else if (p instanceof tl_1.Api.InputPeerChat) {
92
+ pHash = big_integer_1.default.zero;
93
+ }
94
+ else {
95
+ return;
96
+ }
97
+ let username = e.username;
98
+ if (username) {
99
+ username = username.toLowerCase();
100
+ }
101
+ const phone = e.phone;
102
+ const name = (0, Utils_1.getDisplayName)(e);
103
+ return this._entityValuesToRow(markedId, pHash, username, phone, name);
104
+ }
105
+ _entitiesToRows(tlo) {
106
+ let entities = [];
107
+ if (!(tlo.classType === "constructor") && (0, Helpers_1.isArrayLike)(tlo)) {
108
+ // This may be a list of users already for instance
109
+ entities = tlo;
110
+ }
111
+ else {
112
+ if (typeof tlo === "object") {
113
+ if ("user" in tlo) {
114
+ entities.push(tlo.user);
115
+ }
116
+ if ("chat" in tlo) {
117
+ entities.push(tlo.chat);
118
+ }
119
+ if ("channel" in tlo) {
120
+ entities.push(tlo.channel);
121
+ }
122
+ if ("chats" in tlo && (0, Helpers_1.isArrayLike)(tlo.chats)) {
123
+ entities = entities.concat(tlo.chats);
124
+ }
125
+ if ("users" in tlo && (0, Helpers_1.isArrayLike)(tlo.users)) {
126
+ entities = entities.concat(tlo.users);
127
+ }
128
+ }
129
+ }
130
+ const rows = []; // Rows to add (id, hash, username, phone, name)
131
+ for (const e of entities) {
132
+ const row = this._entityToRow(e);
133
+ if (row) {
134
+ rows.push(row);
135
+ }
136
+ }
137
+ return rows;
138
+ }
139
+ processEntities(tlo) {
140
+ const entitiesSet = this._entitiesToRows(tlo);
141
+ for (const e of entitiesSet) {
142
+ this._entities.add(e);
143
+ }
144
+ }
145
+ getEntityRowsByPhone(phone) {
146
+ for (const e of this._entities) {
147
+ // id, hash, username, phone, name
148
+ if (e[3] === phone) {
149
+ return [e[0], e[1]];
150
+ }
151
+ }
152
+ }
153
+ getEntityRowsByUsername(username) {
154
+ for (const e of this._entities) {
155
+ // id, hash, username, phone, name
156
+ if (e[2] === username) {
157
+ return [e[0], e[1]];
158
+ }
159
+ }
160
+ }
161
+ getEntityRowsByName(name) {
162
+ for (const e of this._entities) {
163
+ // id, hash, username, phone, name
164
+ if (e[4] === name) {
165
+ return [e[0], e[1]];
166
+ }
167
+ }
168
+ }
169
+ getEntityRowsById(id, exact = true) {
170
+ if (exact) {
171
+ for (const e of this._entities) {
172
+ // id, hash, username, phone, name
173
+ if (e[0] === id) {
174
+ return [e[0], e[1]];
175
+ }
176
+ }
177
+ }
178
+ else {
179
+ const ids = [
180
+ __1.utils.getPeerId(new tl_1.Api.PeerUser({ userId: (0, Helpers_1.returnBigInt)(id) })),
181
+ __1.utils.getPeerId(new tl_1.Api.PeerChat({ chatId: (0, Helpers_1.returnBigInt)(id) })),
182
+ __1.utils.getPeerId(new tl_1.Api.PeerChannel({ channelId: (0, Helpers_1.returnBigInt)(id) })),
183
+ ];
184
+ for (const e of this._entities) {
185
+ // id, hash, username, phone, name
186
+ if (ids.includes(e[0])) {
187
+ return [e[0], e[1]];
188
+ }
189
+ }
190
+ }
191
+ }
192
+ getInputEntity(key) {
193
+ let exact;
194
+ if (typeof key === "object" &&
195
+ !big_integer_1.default.isInstance(key) &&
196
+ key.SUBCLASS_OF_ID) {
197
+ if (key.SUBCLASS_OF_ID == 0xc91c90b6 ||
198
+ key.SUBCLASS_OF_ID == 0xe669bf46 ||
199
+ key.SUBCLASS_OF_ID == 0x40f202fd) {
200
+ // @ts-ignore
201
+ return key;
202
+ }
203
+ // Try to early return if this key can be casted as input peer
204
+ return __1.utils.getInputPeer(key);
205
+ }
206
+ else {
207
+ // Not a TLObject or can't be cast into InputPeer
208
+ if (typeof key === "object") {
209
+ key = __1.utils.getPeerId(key);
210
+ exact = true;
211
+ }
212
+ else {
213
+ exact = false;
214
+ }
215
+ }
216
+ if (big_integer_1.default.isInstance(key) ||
217
+ typeof key == "bigint" ||
218
+ typeof key == "number") {
219
+ key = key.toString();
220
+ }
221
+ let result = undefined;
222
+ if (typeof key === "string") {
223
+ const phone = __1.utils.parsePhone(key);
224
+ if (phone) {
225
+ result = this.getEntityRowsByPhone(phone);
226
+ }
227
+ else {
228
+ const { username, isInvite } = __1.utils.parseUsername(key);
229
+ if (username && !isInvite) {
230
+ result = this.getEntityRowsByUsername(username);
231
+ }
232
+ }
233
+ if (!result) {
234
+ const id = __1.utils.parseID(key);
235
+ if (id) {
236
+ result = this.getEntityRowsById(id, exact);
237
+ }
238
+ }
239
+ if (!result) {
240
+ result = this.getEntityRowsByName(key);
241
+ }
242
+ }
243
+ if (result) {
244
+ let entityId = result[0]; // unpack resulting tuple
245
+ const entityHash = (0, big_integer_1.default)(result[1]);
246
+ const resolved = __1.utils.resolveId((0, Helpers_1.returnBigInt)(entityId));
247
+ entityId = resolved[0];
248
+ const kind = resolved[1];
249
+ // removes the mark and returns type of entity
250
+ if (kind === tl_1.Api.PeerUser) {
251
+ return new tl_1.Api.InputPeerUser({
252
+ userId: entityId,
253
+ accessHash: entityHash,
254
+ });
255
+ }
256
+ else if (kind === tl_1.Api.PeerChat) {
257
+ return new tl_1.Api.InputPeerChat({ chatId: entityId });
258
+ }
259
+ else if (kind === tl_1.Api.PeerChannel) {
260
+ return new tl_1.Api.InputPeerChannel({
261
+ channelId: entityId,
262
+ accessHash: entityHash,
263
+ });
264
+ }
265
+ }
266
+ else {
267
+ throw new Error("Could not find input entity with key " + key);
268
+ }
269
+ throw new Error("Could not find input entity with key " + key);
270
+ }
271
+ }
272
+ exports.MemorySession = MemorySession;
@@ -0,0 +1,14 @@
1
+ import { MemorySession } from "./Memory";
2
+ import { AuthKey } from "../crypto/AuthKey";
3
+ import bigInt from "big-integer";
4
+ export declare class StoreSession extends MemorySession {
5
+ private readonly sessionName;
6
+ private store;
7
+ constructor(sessionName: string, divider?: string);
8
+ load(): Promise<void>;
9
+ setDC(dcId: number, serverAddress: string, port: number): void;
10
+ set authKey(value: AuthKey | undefined);
11
+ get authKey(): AuthKey | undefined;
12
+ processEntities(tlo: any): void;
13
+ getEntityRowsById(id: string | bigInt.BigInteger, exact?: boolean): any;
14
+ }
@@ -0,0 +1,77 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.StoreSession = void 0;
7
+ const Memory_1 = require("./Memory");
8
+ const store2_1 = __importDefault(require("store2"));
9
+ const AuthKey_1 = require("../crypto/AuthKey");
10
+ class StoreSession extends Memory_1.MemorySession {
11
+ constructor(sessionName, divider = ":") {
12
+ super();
13
+ if (sessionName === "session") {
14
+ throw new Error("Session name can't be 'session'. Please use a different name.");
15
+ }
16
+ if (typeof localStorage === "undefined" || localStorage === null) {
17
+ const LocalStorage = require("./localStorage").LocalStorage;
18
+ this.store = store2_1.default.area(sessionName, new LocalStorage("./" + sessionName));
19
+ }
20
+ else {
21
+ this.store = store2_1.default.area(sessionName, localStorage);
22
+ }
23
+ if (divider == undefined) {
24
+ divider = ":";
25
+ }
26
+ this.sessionName = sessionName + divider;
27
+ }
28
+ async load() {
29
+ let authKey = this.store.get(this.sessionName + "authKey");
30
+ if (authKey && typeof authKey === "object") {
31
+ this._authKey = new AuthKey_1.AuthKey();
32
+ if ("data" in authKey) {
33
+ authKey = Buffer.from(authKey.data);
34
+ }
35
+ await this._authKey.setKey(authKey);
36
+ }
37
+ const dcId = this.store.get(this.sessionName + "dcId");
38
+ if (dcId) {
39
+ this._dcId = dcId;
40
+ }
41
+ const port = this.store.get(this.sessionName + "port");
42
+ if (port) {
43
+ this._port = port;
44
+ }
45
+ const serverAddress = this.store.get(this.sessionName + "serverAddress");
46
+ if (serverAddress) {
47
+ this._serverAddress = serverAddress;
48
+ }
49
+ }
50
+ setDC(dcId, serverAddress, port) {
51
+ this.store.set(this.sessionName + "dcId", dcId);
52
+ this.store.set(this.sessionName + "port", port);
53
+ this.store.set(this.sessionName + "serverAddress", serverAddress);
54
+ super.setDC(dcId, serverAddress, port);
55
+ }
56
+ set authKey(value) {
57
+ this._authKey = value;
58
+ this.store.set(this.sessionName + "authKey", value === null || value === void 0 ? void 0 : value.getKey());
59
+ }
60
+ get authKey() {
61
+ return this._authKey;
62
+ }
63
+ processEntities(tlo) {
64
+ const rows = this._entitiesToRows(tlo);
65
+ if (!rows) {
66
+ return;
67
+ }
68
+ for (const row of rows) {
69
+ row.push(new Date().getTime().toString());
70
+ this.store.set(this.sessionName + row[0], row);
71
+ }
72
+ }
73
+ getEntityRowsById(id, exact = true) {
74
+ return this.store.get(this.sessionName + id.toString());
75
+ }
76
+ }
77
+ exports.StoreSession = StoreSession;
@@ -0,0 +1,33 @@
1
+ /// <reference types="node" />
2
+ import { MemorySession } from "./Memory";
3
+ export declare class StringSession extends MemorySession {
4
+ _key?: Buffer;
5
+ /**
6
+ * This session file can be easily saved and loaded as a string. According
7
+ * to the initial design, it contains only the data that is necessary for
8
+ * successful connection and authentication, so takeout ID is not stored.
9
+
10
+ * It is thought to be used where you don't want to create any on-disk
11
+ * files but would still like to be able to save and load existing sessions
12
+ * by other means.
13
+
14
+ * You can use custom `encode` and `decode` functions, if present:
15
+
16
+ * `encode` definition must be ``function encode(value: Buffer) -> string:``.
17
+ * `decode` definition must be ``function decode(value: string) -> Buffer:``.
18
+ * @param session {string|null}
19
+ */
20
+ constructor(session?: string);
21
+ /**
22
+ * @param x {Buffer}
23
+ * @returns {string}
24
+ */
25
+ static encode(x: Buffer): string;
26
+ /**
27
+ * @param x {string}
28
+ * @returns {Buffer}
29
+ */
30
+ static decode(x: string): Buffer;
31
+ load(): Promise<void>;
32
+ save(): string;
33
+ }
@@ -0,0 +1,116 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StringSession = void 0;
4
+ const Memory_1 = require("./Memory");
5
+ const extensions_1 = require("../extensions");
6
+ const AuthKey_1 = require("../crypto/AuthKey");
7
+ const CURRENT_VERSION = "1";
8
+ class StringSession extends Memory_1.MemorySession {
9
+ /**
10
+ * This session file can be easily saved and loaded as a string. According
11
+ * to the initial design, it contains only the data that is necessary for
12
+ * successful connection and authentication, so takeout ID is not stored.
13
+
14
+ * It is thought to be used where you don't want to create any on-disk
15
+ * files but would still like to be able to save and load existing sessions
16
+ * by other means.
17
+
18
+ * You can use custom `encode` and `decode` functions, if present:
19
+
20
+ * `encode` definition must be ``function encode(value: Buffer) -> string:``.
21
+ * `decode` definition must be ``function decode(value: string) -> Buffer:``.
22
+ * @param session {string|null}
23
+ */
24
+ constructor(session) {
25
+ super();
26
+ if (session) {
27
+ if (session[0] !== CURRENT_VERSION) {
28
+ throw new Error("Not a valid string");
29
+ }
30
+ session = session.slice(1);
31
+ const r = StringSession.decode(session);
32
+ const reader = new extensions_1.BinaryReader(r);
33
+ this._dcId = reader.read(1).readUInt8(0);
34
+ if (session.length == 352) {
35
+ // Telethon session
36
+ const ip_v4 = reader.read(4);
37
+ // TODO looks ugly smh
38
+ this._serverAddress =
39
+ ip_v4[0].toString() +
40
+ "." +
41
+ ip_v4[1].toString() +
42
+ "." +
43
+ ip_v4[2].toString() +
44
+ "." +
45
+ ip_v4[3].toString();
46
+ }
47
+ else {
48
+ // TODO find a better of doing this
49
+ const serverAddressLen = reader.read(2).readInt16BE(0);
50
+ if (serverAddressLen > 100) {
51
+ reader.offset -= 2;
52
+ this._serverAddress = reader
53
+ .read(16)
54
+ .toString("hex")
55
+ .match(/.{1,4}/g)
56
+ .map((val) => val.replace(/^0+/, ""))
57
+ .join(":")
58
+ .replace(/0000\:/g, ":")
59
+ .replace(/:{2,}/g, "::");
60
+ }
61
+ else {
62
+ this._serverAddress = reader
63
+ .read(serverAddressLen)
64
+ .toString();
65
+ }
66
+ }
67
+ this._port = reader.read(2).readInt16BE(0);
68
+ this._key = reader.read(-1);
69
+ }
70
+ }
71
+ /**
72
+ * @param x {Buffer}
73
+ * @returns {string}
74
+ */
75
+ static encode(x) {
76
+ return x.toString("base64");
77
+ }
78
+ /**
79
+ * @param x {string}
80
+ * @returns {Buffer}
81
+ */
82
+ static decode(x) {
83
+ return Buffer.from(x, "base64");
84
+ }
85
+ async load() {
86
+ if (this._key) {
87
+ this._authKey = new AuthKey_1.AuthKey();
88
+ await this._authKey.setKey(this._key);
89
+ }
90
+ }
91
+ save() {
92
+ if (!this.authKey || !this.serverAddress || !this.port) {
93
+ return "";
94
+ }
95
+ // TS is weird
96
+ const key = this.authKey.getKey();
97
+ if (!key) {
98
+ return "";
99
+ }
100
+ const dcBuffer = Buffer.from([this.dcId]);
101
+ const addressBuffer = Buffer.from(this.serverAddress);
102
+ const addressLengthBuffer = Buffer.alloc(2);
103
+ addressLengthBuffer.writeInt16BE(addressBuffer.length, 0);
104
+ const portBuffer = Buffer.alloc(2);
105
+ portBuffer.writeInt16BE(this.port, 0);
106
+ return (CURRENT_VERSION +
107
+ StringSession.encode(Buffer.concat([
108
+ dcBuffer,
109
+ addressLengthBuffer,
110
+ addressBuffer,
111
+ portBuffer,
112
+ key,
113
+ ])));
114
+ }
115
+ }
116
+ exports.StringSession = StringSession;
@@ -0,0 +1,4 @@
1
+ export { MemorySession } from "./Memory";
2
+ export { StringSession } from "./StringSession";
3
+ export { StoreSession } from "./StoreSession";
4
+ export { Session } from "./Abstract";
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Session = exports.StoreSession = exports.StringSession = exports.MemorySession = void 0;
4
+ var Memory_1 = require("./Memory");
5
+ Object.defineProperty(exports, "MemorySession", { enumerable: true, get: function () { return Memory_1.MemorySession; } });
6
+ var StringSession_1 = require("./StringSession");
7
+ Object.defineProperty(exports, "StringSession", { enumerable: true, get: function () { return StringSession_1.StringSession; } });
8
+ var StoreSession_1 = require("./StoreSession");
9
+ Object.defineProperty(exports, "StoreSession", { enumerable: true, get: function () { return StoreSession_1.StoreSession; } });
10
+ var Abstract_1 = require("./Abstract");
11
+ Object.defineProperty(exports, "Session", { enumerable: true, get: function () { return Abstract_1.Session; } });
12
+ // @ts-ignore
13
+ //export {CacheApiSession} from './CacheApiSession';
@@ -0,0 +1,3 @@
1
+ export declare class LocalStorage {
2
+ constructor(location: string);
3
+ }
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LocalStorage = void 0;
4
+ class LocalStorage {
5
+ constructor(location) {
6
+ throw new Error("Do not call me");
7
+ }
8
+ }
9
+ exports.LocalStorage = LocalStorage;
@@ -0,0 +1 @@
1
+ export declare const LocalStorage: any;