@mtcute/core 0.1.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 (502) hide show
  1. package/LICENSE +8 -0
  2. package/README.md +26 -0
  3. package/cjs/base-client.d.ts +297 -0
  4. package/cjs/base-client.js +324 -0
  5. package/cjs/base-client.js.map +1 -0
  6. package/cjs/index.d.ts +10 -0
  7. package/cjs/index.js +30 -0
  8. package/cjs/index.js.map +1 -0
  9. package/cjs/network/auth-key.d.ts +21 -0
  10. package/cjs/network/auth-key.js +101 -0
  11. package/cjs/network/auth-key.js.map +1 -0
  12. package/cjs/network/authorization.d.ts +9 -0
  13. package/cjs/network/authorization.js +332 -0
  14. package/cjs/network/authorization.js.map +1 -0
  15. package/cjs/network/config-manager.d.ts +33 -0
  16. package/cjs/network/config-manager.js +93 -0
  17. package/cjs/network/config-manager.js.map +1 -0
  18. package/cjs/network/index.d.ts +4 -0
  19. package/cjs/network/index.js +20 -0
  20. package/cjs/network/index.js.map +1 -0
  21. package/cjs/network/mtproto-session.d.ts +116 -0
  22. package/cjs/network/mtproto-session.js +209 -0
  23. package/cjs/network/mtproto-session.js.map +1 -0
  24. package/cjs/network/multi-session-connection.d.ts +33 -0
  25. package/cjs/network/multi-session-connection.js +243 -0
  26. package/cjs/network/multi-session-connection.js.map +1 -0
  27. package/cjs/network/network-manager.d.ts +190 -0
  28. package/cjs/network/network-manager.js +547 -0
  29. package/cjs/network/network-manager.js.map +1 -0
  30. package/cjs/network/persistent-connection.d.ts +51 -0
  31. package/cjs/network/persistent-connection.js +173 -0
  32. package/cjs/network/persistent-connection.js.map +1 -0
  33. package/cjs/network/reconnection.d.ts +11 -0
  34. package/cjs/network/reconnection.js +18 -0
  35. package/cjs/network/reconnection.js.map +1 -0
  36. package/cjs/network/session-connection.d.ts +72 -0
  37. package/cjs/network/session-connection.js +1363 -0
  38. package/cjs/network/session-connection.js.map +1 -0
  39. package/cjs/network/transports/abstract.d.ts +99 -0
  40. package/cjs/network/transports/abstract.js +38 -0
  41. package/cjs/network/transports/abstract.js.map +1 -0
  42. package/cjs/network/transports/index.d.ts +8 -0
  43. package/cjs/network/transports/index.js +26 -0
  44. package/cjs/network/transports/index.js.map +1 -0
  45. package/cjs/network/transports/intermediate.d.ts +20 -0
  46. package/cjs/network/transports/intermediate.js +67 -0
  47. package/cjs/network/transports/intermediate.js.map +1 -0
  48. package/cjs/network/transports/obfuscated.d.ts +18 -0
  49. package/cjs/network/transports/obfuscated.js +83 -0
  50. package/cjs/network/transports/obfuscated.js.map +1 -0
  51. package/cjs/network/transports/streamed.d.ts +24 -0
  52. package/cjs/network/transports/streamed.js +32 -0
  53. package/cjs/network/transports/streamed.js.map +1 -0
  54. package/cjs/network/transports/tcp.d.ts +33 -0
  55. package/cjs/network/transports/tcp.js +130 -0
  56. package/cjs/network/transports/tcp.js.map +1 -0
  57. package/cjs/network/transports/websocket.d.ts +47 -0
  58. package/cjs/network/transports/websocket.js +124 -0
  59. package/cjs/network/transports/websocket.js.map +1 -0
  60. package/cjs/network/transports/wrapped.d.ts +11 -0
  61. package/cjs/network/transports/wrapped.js +26 -0
  62. package/cjs/network/transports/wrapped.js.map +1 -0
  63. package/cjs/package.json +3 -0
  64. package/cjs/storage/abstract.d.ts +174 -0
  65. package/cjs/storage/abstract.js +3 -0
  66. package/cjs/storage/abstract.js.map +1 -0
  67. package/cjs/storage/index.d.ts +5 -0
  68. package/cjs/storage/index.js +22 -0
  69. package/cjs/storage/index.js.map +1 -0
  70. package/cjs/storage/json-file.d.ts +31 -0
  71. package/cjs/storage/json-file.js +90 -0
  72. package/cjs/storage/json-file.js.map +1 -0
  73. package/cjs/storage/json.d.ts +9 -0
  74. package/cjs/storage/json.js +65 -0
  75. package/cjs/storage/json.js.map +1 -0
  76. package/cjs/storage/localstorage.d.ts +7 -0
  77. package/cjs/storage/localstorage.js +25 -0
  78. package/cjs/storage/localstorage.js.map +1 -0
  79. package/cjs/storage/memory.d.ts +98 -0
  80. package/cjs/storage/memory.js +299 -0
  81. package/cjs/storage/memory.js.map +1 -0
  82. package/cjs/types/errors.d.ts +53 -0
  83. package/cjs/types/errors.js +58 -0
  84. package/cjs/types/errors.js.map +1 -0
  85. package/cjs/types/index.d.ts +3 -0
  86. package/cjs/types/index.js +20 -0
  87. package/cjs/types/index.js.map +1 -0
  88. package/cjs/types/peers.d.ts +4 -0
  89. package/cjs/types/peers.js +3 -0
  90. package/cjs/types/peers.js.map +1 -0
  91. package/cjs/types/utils.d.ts +6 -0
  92. package/cjs/types/utils.js +9 -0
  93. package/cjs/types/utils.js.map +1 -0
  94. package/cjs/utils/async-lock.d.ts +10 -0
  95. package/cjs/utils/async-lock.js +43 -0
  96. package/cjs/utils/async-lock.js.map +1 -0
  97. package/cjs/utils/bigint-utils.d.ts +40 -0
  98. package/cjs/utils/bigint-utils.js +104 -0
  99. package/cjs/utils/bigint-utils.js.map +1 -0
  100. package/cjs/utils/binary/asn1-parser.d.ts +17 -0
  101. package/cjs/utils/binary/asn1-parser.js +121 -0
  102. package/cjs/utils/binary/asn1-parser.js.map +1 -0
  103. package/cjs/utils/buffer-utils.d.ts +21 -0
  104. package/cjs/utils/buffer-utils.js +63 -0
  105. package/cjs/utils/buffer-utils.js.map +1 -0
  106. package/cjs/utils/condition-variable.d.ts +9 -0
  107. package/cjs/utils/condition-variable.js +28 -0
  108. package/cjs/utils/condition-variable.js.map +1 -0
  109. package/cjs/utils/controllable-promise.d.ts +16 -0
  110. package/cjs/utils/controllable-promise.js +25 -0
  111. package/cjs/utils/controllable-promise.js.map +1 -0
  112. package/cjs/utils/crypto/abstract.d.ts +23 -0
  113. package/cjs/utils/crypto/abstract.js +19 -0
  114. package/cjs/utils/crypto/abstract.js.map +1 -0
  115. package/cjs/utils/crypto/common.d.ts +12 -0
  116. package/cjs/utils/crypto/common.js +54 -0
  117. package/cjs/utils/crypto/common.js.map +1 -0
  118. package/cjs/utils/crypto/factorization.d.ts +5 -0
  119. package/cjs/utils/crypto/factorization.js +69 -0
  120. package/cjs/utils/crypto/factorization.js.map +1 -0
  121. package/cjs/utils/crypto/index.d.ts +5 -0
  122. package/cjs/utils/crypto/index.js +23 -0
  123. package/cjs/utils/crypto/index.js.map +1 -0
  124. package/cjs/utils/crypto/keys.d.ts +27 -0
  125. package/cjs/utils/crypto/keys.js +70 -0
  126. package/cjs/utils/crypto/keys.js.map +1 -0
  127. package/cjs/utils/crypto/miller-rabin.d.ts +2 -0
  128. package/cjs/utils/crypto/miller-rabin.js +44 -0
  129. package/cjs/utils/crypto/miller-rabin.js.map +1 -0
  130. package/cjs/utils/crypto/mtproto.d.ts +31 -0
  131. package/cjs/utils/crypto/mtproto.js +66 -0
  132. package/cjs/utils/crypto/mtproto.js.map +1 -0
  133. package/cjs/utils/crypto/node-crypto.d.ts +10 -0
  134. package/cjs/utils/crypto/node-crypto.js +46 -0
  135. package/cjs/utils/crypto/node-crypto.js.map +1 -0
  136. package/cjs/utils/crypto/password.d.ts +29 -0
  137. package/cjs/utils/crypto/password.js +106 -0
  138. package/cjs/utils/crypto/password.js.map +1 -0
  139. package/cjs/utils/crypto/subtle.d.ts +12 -0
  140. package/cjs/utils/crypto/subtle.js +86 -0
  141. package/cjs/utils/crypto/subtle.js.map +1 -0
  142. package/cjs/utils/crypto/utils.d.ts +14 -0
  143. package/cjs/utils/crypto/utils.js +30 -0
  144. package/cjs/utils/crypto/utils.js.map +1 -0
  145. package/cjs/utils/default-dcs.d.ts +7 -0
  146. package/cjs/utils/default-dcs.js +76 -0
  147. package/cjs/utils/default-dcs.js.map +1 -0
  148. package/cjs/utils/deque.d.ts +29 -0
  149. package/cjs/utils/deque.js +210 -0
  150. package/cjs/utils/deque.js.map +1 -0
  151. package/cjs/utils/early-timer.d.ts +42 -0
  152. package/cjs/utils/early-timer.js +79 -0
  153. package/cjs/utils/early-timer.js.map +1 -0
  154. package/cjs/utils/flood-control.d.ts +14 -0
  155. package/cjs/utils/flood-control.js +65 -0
  156. package/cjs/utils/flood-control.js.map +1 -0
  157. package/cjs/utils/function-utils.d.ts +13 -0
  158. package/cjs/utils/function-utils.js +30 -0
  159. package/cjs/utils/function-utils.js.map +1 -0
  160. package/cjs/utils/index.d.ts +24 -0
  161. package/cjs/utils/index.js +41 -0
  162. package/cjs/utils/index.js.map +1 -0
  163. package/cjs/utils/linked-list.d.ts +21 -0
  164. package/cjs/utils/linked-list.js +73 -0
  165. package/cjs/utils/linked-list.js.map +1 -0
  166. package/cjs/utils/links/bots.d.ts +71 -0
  167. package/cjs/utils/links/bots.js +259 -0
  168. package/cjs/utils/links/bots.js.map +1 -0
  169. package/cjs/utils/links/bundle.d.ts +7 -0
  170. package/cjs/utils/links/bundle.js +24 -0
  171. package/cjs/utils/links/bundle.js.map +1 -0
  172. package/cjs/utils/links/calls.d.ts +16 -0
  173. package/cjs/utils/links/calls.js +59 -0
  174. package/cjs/utils/links/calls.js.map +1 -0
  175. package/cjs/utils/links/chat.d.ts +46 -0
  176. package/cjs/utils/links/chat.js +160 -0
  177. package/cjs/utils/links/chat.js.map +1 -0
  178. package/cjs/utils/links/common.d.ts +31 -0
  179. package/cjs/utils/links/common.js +60 -0
  180. package/cjs/utils/links/common.js.map +1 -0
  181. package/cjs/utils/links/index.d.ts +2 -0
  182. package/cjs/utils/links/index.js +29 -0
  183. package/cjs/utils/links/index.js.map +1 -0
  184. package/cjs/utils/links/misc.d.ts +19 -0
  185. package/cjs/utils/links/misc.js +77 -0
  186. package/cjs/utils/links/misc.js.map +1 -0
  187. package/cjs/utils/links/proxy.d.ts +17 -0
  188. package/cjs/utils/links/proxy.js +61 -0
  189. package/cjs/utils/links/proxy.js.map +1 -0
  190. package/cjs/utils/links/stickers.d.ts +9 -0
  191. package/cjs/utils/links/stickers.js +28 -0
  192. package/cjs/utils/links/stickers.js.map +1 -0
  193. package/cjs/utils/links/user.d.ts +27 -0
  194. package/cjs/utils/links/user.js +81 -0
  195. package/cjs/utils/links/user.js.map +1 -0
  196. package/cjs/utils/logger.d.ts +54 -0
  197. package/cjs/utils/logger.js +158 -0
  198. package/cjs/utils/logger.js.map +1 -0
  199. package/cjs/utils/long-utils.d.ts +75 -0
  200. package/cjs/utils/long-utils.js +161 -0
  201. package/cjs/utils/long-utils.js.map +1 -0
  202. package/cjs/utils/lru-map.d.ts +21 -0
  203. package/cjs/utils/lru-map.js +107 -0
  204. package/cjs/utils/lru-map.js.map +1 -0
  205. package/cjs/utils/lru-set.d.ts +22 -0
  206. package/cjs/utils/lru-set.js +48 -0
  207. package/cjs/utils/lru-set.js.map +1 -0
  208. package/cjs/utils/misc-utils.d.ts +7 -0
  209. package/cjs/utils/misc-utils.js +15 -0
  210. package/cjs/utils/misc-utils.js.map +1 -0
  211. package/cjs/utils/peer-utils.d.ts +35 -0
  212. package/cjs/utils/peer-utils.js +188 -0
  213. package/cjs/utils/peer-utils.js.map +1 -0
  214. package/cjs/utils/platform/crypto.d.ts +1 -0
  215. package/cjs/utils/platform/crypto.js +8 -0
  216. package/cjs/utils/platform/crypto.js.map +1 -0
  217. package/cjs/utils/platform/crypto.web.d.ts +1 -0
  218. package/cjs/utils/platform/crypto.web.js +14 -0
  219. package/cjs/utils/platform/crypto.web.js.map +1 -0
  220. package/cjs/utils/platform/error-reporting.d.ts +3 -0
  221. package/cjs/utils/platform/error-reporting.js +22 -0
  222. package/cjs/utils/platform/error-reporting.js.map +1 -0
  223. package/cjs/utils/platform/logging.d.ts +1 -0
  224. package/cjs/utils/platform/logging.js +35 -0
  225. package/cjs/utils/platform/logging.js.map +1 -0
  226. package/cjs/utils/platform/logging.web.d.ts +1 -0
  227. package/cjs/utils/platform/logging.web.js +35 -0
  228. package/cjs/utils/platform/logging.web.js.map +1 -0
  229. package/cjs/utils/platform/random.d.ts +1 -0
  230. package/cjs/utils/platform/random.js +7 -0
  231. package/cjs/utils/platform/random.js.map +1 -0
  232. package/cjs/utils/platform/random.web.d.ts +1 -0
  233. package/cjs/utils/platform/random.web.js +10 -0
  234. package/cjs/utils/platform/random.web.js.map +1 -0
  235. package/cjs/utils/platform/transport.d.ts +1 -0
  236. package/cjs/utils/platform/transport.js +8 -0
  237. package/cjs/utils/platform/transport.js.map +1 -0
  238. package/cjs/utils/platform/transport.web.d.ts +1 -0
  239. package/cjs/utils/platform/transport.web.js +15 -0
  240. package/cjs/utils/platform/transport.web.js.map +1 -0
  241. package/cjs/utils/sorted-array.d.ts +17 -0
  242. package/cjs/utils/sorted-array.js +83 -0
  243. package/cjs/utils/sorted-array.js.map +1 -0
  244. package/cjs/utils/string-session.d.ts +11 -0
  245. package/cjs/utils/string-session.js +73 -0
  246. package/cjs/utils/string-session.js.map +1 -0
  247. package/cjs/utils/tl-json.d.ts +13 -0
  248. package/cjs/utils/tl-json.js +62 -0
  249. package/cjs/utils/tl-json.js.map +1 -0
  250. package/cjs/utils/type-assertions.d.ts +37 -0
  251. package/cjs/utils/type-assertions.js +70 -0
  252. package/cjs/utils/type-assertions.js.map +1 -0
  253. package/esm/base-client.d.ts +297 -0
  254. package/esm/base-client.js +317 -0
  255. package/esm/base-client.js.map +1 -0
  256. package/esm/index.d.ts +10 -0
  257. package/esm/index.js +11 -0
  258. package/esm/index.js.map +1 -0
  259. package/esm/network/auth-key.d.ts +21 -0
  260. package/esm/network/auth-key.js +97 -0
  261. package/esm/network/auth-key.js.map +1 -0
  262. package/esm/network/authorization.d.ts +9 -0
  263. package/esm/network/authorization.js +325 -0
  264. package/esm/network/authorization.js.map +1 -0
  265. package/esm/network/config-manager.d.ts +33 -0
  266. package/esm/network/config-manager.js +89 -0
  267. package/esm/network/config-manager.js.map +1 -0
  268. package/esm/network/index.d.ts +4 -0
  269. package/esm/network/index.js +4 -0
  270. package/esm/network/index.js.map +1 -0
  271. package/esm/network/mtproto-session.d.ts +116 -0
  272. package/esm/network/mtproto-session.js +202 -0
  273. package/esm/network/mtproto-session.js.map +1 -0
  274. package/esm/network/multi-session-connection.d.ts +33 -0
  275. package/esm/network/multi-session-connection.js +236 -0
  276. package/esm/network/multi-session-connection.js.map +1 -0
  277. package/esm/network/network-manager.d.ts +190 -0
  278. package/esm/network/network-manager.js +542 -0
  279. package/esm/network/network-manager.js.map +1 -0
  280. package/esm/network/persistent-connection.d.ts +51 -0
  281. package/esm/network/persistent-connection.js +166 -0
  282. package/esm/network/persistent-connection.js.map +1 -0
  283. package/esm/network/reconnection.d.ts +11 -0
  284. package/esm/network/reconnection.js +14 -0
  285. package/esm/network/reconnection.js.map +1 -0
  286. package/esm/network/session-connection.d.ts +72 -0
  287. package/esm/network/session-connection.js +1356 -0
  288. package/esm/network/session-connection.js.map +1 -0
  289. package/esm/network/transports/abstract.d.ts +99 -0
  290. package/esm/network/transports/abstract.js +34 -0
  291. package/esm/network/transports/abstract.js.map +1 -0
  292. package/esm/network/transports/index.d.ts +8 -0
  293. package/esm/network/transports/index.js +9 -0
  294. package/esm/network/transports/index.js.map +1 -0
  295. package/esm/network/transports/intermediate.d.ts +20 -0
  296. package/esm/network/transports/intermediate.js +62 -0
  297. package/esm/network/transports/intermediate.js.map +1 -0
  298. package/esm/network/transports/obfuscated.d.ts +18 -0
  299. package/esm/network/transports/obfuscated.js +79 -0
  300. package/esm/network/transports/obfuscated.js.map +1 -0
  301. package/esm/network/transports/streamed.d.ts +24 -0
  302. package/esm/network/transports/streamed.js +25 -0
  303. package/esm/network/transports/streamed.js.map +1 -0
  304. package/esm/network/transports/tcp.d.ts +33 -0
  305. package/esm/network/transports/tcp.js +122 -0
  306. package/esm/network/transports/tcp.js.map +1 -0
  307. package/esm/network/transports/websocket.d.ts +47 -0
  308. package/esm/network/transports/websocket.js +116 -0
  309. package/esm/network/transports/websocket.js.map +1 -0
  310. package/esm/network/transports/wrapped.d.ts +11 -0
  311. package/esm/network/transports/wrapped.js +19 -0
  312. package/esm/network/transports/wrapped.js.map +1 -0
  313. package/esm/storage/abstract.d.ts +174 -0
  314. package/esm/storage/abstract.js +2 -0
  315. package/esm/storage/abstract.js.map +1 -0
  316. package/esm/storage/index.d.ts +5 -0
  317. package/esm/storage/index.js +6 -0
  318. package/esm/storage/index.js.map +1 -0
  319. package/esm/storage/json-file.d.ts +31 -0
  320. package/esm/storage/json-file.js +63 -0
  321. package/esm/storage/json-file.js.map +1 -0
  322. package/esm/storage/json.d.ts +9 -0
  323. package/esm/storage/json.js +61 -0
  324. package/esm/storage/json.js.map +1 -0
  325. package/esm/storage/localstorage.d.ts +7 -0
  326. package/esm/storage/localstorage.js +21 -0
  327. package/esm/storage/localstorage.js.map +1 -0
  328. package/esm/storage/memory.d.ts +98 -0
  329. package/esm/storage/memory.js +295 -0
  330. package/esm/storage/memory.js.map +1 -0
  331. package/esm/types/errors.d.ts +53 -0
  332. package/esm/types/errors.js +49 -0
  333. package/esm/types/errors.js.map +1 -0
  334. package/esm/types/index.d.ts +3 -0
  335. package/esm/types/index.js +4 -0
  336. package/esm/types/index.js.map +1 -0
  337. package/esm/types/peers.d.ts +4 -0
  338. package/esm/types/peers.js +2 -0
  339. package/esm/types/peers.js.map +1 -0
  340. package/esm/types/utils.d.ts +6 -0
  341. package/esm/types/utils.js +5 -0
  342. package/esm/types/utils.js.map +1 -0
  343. package/esm/utils/async-lock.d.ts +10 -0
  344. package/esm/utils/async-lock.js +39 -0
  345. package/esm/utils/async-lock.js.map +1 -0
  346. package/esm/utils/bigint-utils.d.ts +40 -0
  347. package/esm/utils/bigint-utils.js +92 -0
  348. package/esm/utils/bigint-utils.js.map +1 -0
  349. package/esm/utils/binary/asn1-parser.d.ts +17 -0
  350. package/esm/utils/binary/asn1-parser.js +116 -0
  351. package/esm/utils/binary/asn1-parser.js.map +1 -0
  352. package/esm/utils/buffer-utils.d.ts +21 -0
  353. package/esm/utils/buffer-utils.js +55 -0
  354. package/esm/utils/buffer-utils.js.map +1 -0
  355. package/esm/utils/condition-variable.d.ts +9 -0
  356. package/esm/utils/condition-variable.js +24 -0
  357. package/esm/utils/condition-variable.js.map +1 -0
  358. package/esm/utils/controllable-promise.d.ts +16 -0
  359. package/esm/utils/controllable-promise.js +20 -0
  360. package/esm/utils/controllable-promise.js.map +1 -0
  361. package/esm/utils/crypto/abstract.d.ts +23 -0
  362. package/esm/utils/crypto/abstract.js +15 -0
  363. package/esm/utils/crypto/abstract.js.map +1 -0
  364. package/esm/utils/crypto/common.d.ts +12 -0
  365. package/esm/utils/crypto/common.js +50 -0
  366. package/esm/utils/crypto/common.js.map +1 -0
  367. package/esm/utils/crypto/factorization.d.ts +5 -0
  368. package/esm/utils/crypto/factorization.js +62 -0
  369. package/esm/utils/crypto/factorization.js.map +1 -0
  370. package/esm/utils/crypto/index.d.ts +5 -0
  371. package/esm/utils/crypto/index.js +6 -0
  372. package/esm/utils/crypto/index.js.map +1 -0
  373. package/esm/utils/crypto/keys.d.ts +27 -0
  374. package/esm/utils/crypto/keys.js +64 -0
  375. package/esm/utils/crypto/keys.js.map +1 -0
  376. package/esm/utils/crypto/miller-rabin.d.ts +2 -0
  377. package/esm/utils/crypto/miller-rabin.js +37 -0
  378. package/esm/utils/crypto/miller-rabin.js.map +1 -0
  379. package/esm/utils/crypto/mtproto.d.ts +31 -0
  380. package/esm/utils/crypto/mtproto.js +60 -0
  381. package/esm/utils/crypto/mtproto.js.map +1 -0
  382. package/esm/utils/crypto/node-crypto.d.ts +10 -0
  383. package/esm/utils/crypto/node-crypto.js +42 -0
  384. package/esm/utils/crypto/node-crypto.js.map +1 -0
  385. package/esm/utils/crypto/password.d.ts +29 -0
  386. package/esm/utils/crypto/password.js +97 -0
  387. package/esm/utils/crypto/password.js.map +1 -0
  388. package/esm/utils/crypto/subtle.d.ts +12 -0
  389. package/esm/utils/crypto/subtle.js +59 -0
  390. package/esm/utils/crypto/subtle.js.map +1 -0
  391. package/esm/utils/crypto/utils.d.ts +14 -0
  392. package/esm/utils/crypto/utils.js +25 -0
  393. package/esm/utils/crypto/utils.js.map +1 -0
  394. package/esm/utils/default-dcs.d.ts +7 -0
  395. package/esm/utils/default-dcs.js +73 -0
  396. package/esm/utils/default-dcs.js.map +1 -0
  397. package/esm/utils/deque.d.ts +29 -0
  398. package/esm/utils/deque.js +206 -0
  399. package/esm/utils/deque.js.map +1 -0
  400. package/esm/utils/early-timer.d.ts +42 -0
  401. package/esm/utils/early-timer.js +75 -0
  402. package/esm/utils/early-timer.js.map +1 -0
  403. package/esm/utils/flood-control.d.ts +14 -0
  404. package/esm/utils/flood-control.js +61 -0
  405. package/esm/utils/flood-control.js.map +1 -0
  406. package/esm/utils/function-utils.d.ts +13 -0
  407. package/esm/utils/function-utils.js +26 -0
  408. package/esm/utils/function-utils.js.map +1 -0
  409. package/esm/utils/index.d.ts +24 -0
  410. package/esm/utils/index.js +25 -0
  411. package/esm/utils/index.js.map +1 -0
  412. package/esm/utils/linked-list.d.ts +21 -0
  413. package/esm/utils/linked-list.js +69 -0
  414. package/esm/utils/linked-list.js.map +1 -0
  415. package/esm/utils/links/bots.d.ts +71 -0
  416. package/esm/utils/links/bots.js +256 -0
  417. package/esm/utils/links/bots.js.map +1 -0
  418. package/esm/utils/links/bundle.d.ts +7 -0
  419. package/esm/utils/links/bundle.js +8 -0
  420. package/esm/utils/links/bundle.js.map +1 -0
  421. package/esm/utils/links/calls.d.ts +16 -0
  422. package/esm/utils/links/calls.js +56 -0
  423. package/esm/utils/links/calls.js.map +1 -0
  424. package/esm/utils/links/chat.d.ts +46 -0
  425. package/esm/utils/links/chat.js +157 -0
  426. package/esm/utils/links/chat.js.map +1 -0
  427. package/esm/utils/links/common.d.ts +31 -0
  428. package/esm/utils/links/common.js +56 -0
  429. package/esm/utils/links/common.js.map +1 -0
  430. package/esm/utils/links/index.d.ts +2 -0
  431. package/esm/utils/links/index.js +3 -0
  432. package/esm/utils/links/index.js.map +1 -0
  433. package/esm/utils/links/misc.d.ts +19 -0
  434. package/esm/utils/links/misc.js +74 -0
  435. package/esm/utils/links/misc.js.map +1 -0
  436. package/esm/utils/links/proxy.d.ts +17 -0
  437. package/esm/utils/links/proxy.js +58 -0
  438. package/esm/utils/links/proxy.js.map +1 -0
  439. package/esm/utils/links/stickers.d.ts +9 -0
  440. package/esm/utils/links/stickers.js +25 -0
  441. package/esm/utils/links/stickers.js.map +1 -0
  442. package/esm/utils/links/user.d.ts +27 -0
  443. package/esm/utils/links/user.js +78 -0
  444. package/esm/utils/links/user.js.map +1 -0
  445. package/esm/utils/logger.d.ts +54 -0
  446. package/esm/utils/logger.js +154 -0
  447. package/esm/utils/logger.js.map +1 -0
  448. package/esm/utils/long-utils.d.ts +75 -0
  449. package/esm/utils/long-utils.js +148 -0
  450. package/esm/utils/long-utils.js.map +1 -0
  451. package/esm/utils/lru-map.d.ts +21 -0
  452. package/esm/utils/lru-map.js +103 -0
  453. package/esm/utils/lru-map.js.map +1 -0
  454. package/esm/utils/lru-set.d.ts +22 -0
  455. package/esm/utils/lru-set.js +44 -0
  456. package/esm/utils/lru-set.js.map +1 -0
  457. package/esm/utils/misc-utils.d.ts +7 -0
  458. package/esm/utils/misc-utils.js +10 -0
  459. package/esm/utils/misc-utils.js.map +1 -0
  460. package/esm/utils/peer-utils.d.ts +35 -0
  461. package/esm/utils/peer-utils.js +179 -0
  462. package/esm/utils/peer-utils.js.map +1 -0
  463. package/esm/utils/platform/crypto.d.ts +1 -0
  464. package/esm/utils/platform/crypto.js +4 -0
  465. package/esm/utils/platform/crypto.js.map +1 -0
  466. package/esm/utils/platform/crypto.web.d.ts +1 -0
  467. package/esm/utils/platform/crypto.web.js +10 -0
  468. package/esm/utils/platform/crypto.web.js.map +1 -0
  469. package/esm/utils/platform/error-reporting.d.ts +3 -0
  470. package/esm/utils/platform/error-reporting.js +18 -0
  471. package/esm/utils/platform/error-reporting.js.map +1 -0
  472. package/esm/utils/platform/logging.d.ts +1 -0
  473. package/esm/utils/platform/logging.js +32 -0
  474. package/esm/utils/platform/logging.js.map +1 -0
  475. package/esm/utils/platform/logging.web.d.ts +1 -0
  476. package/esm/utils/platform/logging.web.js +31 -0
  477. package/esm/utils/platform/logging.web.js.map +1 -0
  478. package/esm/utils/platform/random.d.ts +1 -0
  479. package/esm/utils/platform/random.js +4 -0
  480. package/esm/utils/platform/random.js.map +1 -0
  481. package/esm/utils/platform/random.web.d.ts +1 -0
  482. package/esm/utils/platform/random.web.js +6 -0
  483. package/esm/utils/platform/random.web.js.map +1 -0
  484. package/esm/utils/platform/transport.d.ts +1 -0
  485. package/esm/utils/platform/transport.js +4 -0
  486. package/esm/utils/platform/transport.js.map +1 -0
  487. package/esm/utils/platform/transport.web.d.ts +1 -0
  488. package/esm/utils/platform/transport.web.js +12 -0
  489. package/esm/utils/platform/transport.web.js.map +1 -0
  490. package/esm/utils/sorted-array.d.ts +17 -0
  491. package/esm/utils/sorted-array.js +79 -0
  492. package/esm/utils/sorted-array.js.map +1 -0
  493. package/esm/utils/string-session.d.ts +11 -0
  494. package/esm/utils/string-session.js +68 -0
  495. package/esm/utils/string-session.js.map +1 -0
  496. package/esm/utils/tl-json.d.ts +13 -0
  497. package/esm/utils/tl-json.js +57 -0
  498. package/esm/utils/tl-json.js.map +1 -0
  499. package/esm/utils/type-assertions.d.ts +37 -0
  500. package/esm/utils/type-assertions.js +61 -0
  501. package/esm/utils/type-assertions.js.map +1 -0
  502. package/package.json +50 -0
package/LICENSE ADDED
@@ -0,0 +1,8 @@
1
+ Copyright 2023 Alina Sireneva
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8
+
package/README.md ADDED
@@ -0,0 +1,26 @@
1
+ # @mtcute/core
2
+
3
+ 📖 [API Reference](https://ref.mtcute.dev/modules/_mtcute_core.html)
4
+
5
+ Basic low-level MTProto implementation and auxiliary utilities.
6
+
7
+ ## Features
8
+ - **MTProto 2.0**: Implements the full MTProto protocol, including all the encryption and serialization
9
+ - **2FA support**: Provides utilities for 2-step verification
10
+ - **Hackable**: Bring your own storage, transport, and other components to customize the library to your needs
11
+ - **Magical**: Handles reconnections, connection pooling, DC redirections and other stuff for you
12
+ - **Web support**: Works in the browser with no additional configuration
13
+
14
+ ## Usage
15
+
16
+ ```ts
17
+ import { BaseTelegramClient } from '@mtcute/core'
18
+
19
+ const tg = new BaseTelegramClient({
20
+ apiId: 12345,
21
+ apiHash: '0123456789abcdef0123456789abcdef',
22
+ })
23
+
24
+ tg.call({ _: 'help.getConfig' })
25
+ .then(console.log)
26
+ ```
@@ -0,0 +1,297 @@
1
+ /// <reference types="node" resolution-mode="require"/>
2
+ import EventEmitter from 'events';
3
+ import { tl } from '@mtcute/tl';
4
+ import { TlReaderMap, TlWriterMap } from '@mtcute/tl-runtime';
5
+ import { ConfigManager } from './network/config-manager.js';
6
+ import { ReconnectionStrategy, SessionConnection, TransportFactory } from './network/index.js';
7
+ import { NetworkManager, NetworkManagerExtraParams, RpcCallOptions } from './network/network-manager.js';
8
+ import { PersistentConnectionParams } from './network/persistent-connection.js';
9
+ import { ITelegramStorage } from './storage/index.js';
10
+ import { MustEqual } from './types/index.js';
11
+ import { CryptoProviderFactory, ICryptoProvider, LogManager } from './utils/index.js';
12
+ /** Options for {@link BaseTelegramClient} */
13
+ export interface BaseTelegramClientOptions {
14
+ /**
15
+ * API ID from my.telegram.org
16
+ */
17
+ apiId: number | string;
18
+ /**
19
+ * API hash from my.telegram.org
20
+ */
21
+ apiHash: string;
22
+ /**
23
+ * Telegram storage to use.
24
+ * If omitted, {@link MemoryStorage} is used
25
+ */
26
+ storage?: ITelegramStorage;
27
+ /**
28
+ * Cryptography provider factory to allow delegating
29
+ * crypto to native addon, worker, etc.
30
+ */
31
+ crypto?: CryptoProviderFactory;
32
+ /**
33
+ * Whether to use IPv6 datacenters
34
+ * (IPv6 will be preferred when choosing a DC by id)
35
+ * (default: false)
36
+ */
37
+ useIpv6?: boolean;
38
+ /**
39
+ * Primary DC to use for initial connection.
40
+ * This does not mean this will be the only DC used,
41
+ * nor that this DC will actually be primary, this only
42
+ * determines the first DC the library will try to connect to.
43
+ * Can be used to connect to other networks (like test DCs).
44
+ *
45
+ * When session already contains primary DC, this parameter is ignored.
46
+ *
47
+ * @default Production DC 2.
48
+ */
49
+ defaultDcs?: ITelegramStorage.DcOptions;
50
+ /**
51
+ * Whether to connect to test servers.
52
+ *
53
+ * If passed, {@link defaultDc} defaults to Test DC 2.
54
+ *
55
+ * **Must** be passed if using test servers, even if
56
+ * you passed custom {@link defaultDc}
57
+ */
58
+ testMode?: boolean;
59
+ /**
60
+ * Additional options for initConnection call.
61
+ * `apiId` and `query` are not available and will be ignored.
62
+ * Omitted values will be filled with defaults
63
+ */
64
+ initConnectionOptions?: Partial<Omit<tl.RawInitConnectionRequest, 'apiId' | 'query'>>;
65
+ /**
66
+ * Transport factory to use in the client.
67
+ *
68
+ * @default platform-specific transport: WebSocket on the web, TCP in node
69
+ */
70
+ transport?: TransportFactory;
71
+ /**
72
+ * Reconnection strategy.
73
+ *
74
+ * @default simple reconnection strategy: first 0ms, then up to 5s (increasing by 1s)
75
+ */
76
+ reconnectionStrategy?: ReconnectionStrategy<PersistentConnectionParams>;
77
+ /**
78
+ * Maximum duration of a flood_wait that will be waited automatically.
79
+ * Flood waits above this threshold will throw a FloodWaitError.
80
+ * Set to 0 to disable. Can be overridden with `throwFlood` parameter in call() params
81
+ *
82
+ * @default 10000
83
+ */
84
+ floodSleepThreshold?: number;
85
+ /**
86
+ * Maximum number of retries when calling RPC methods.
87
+ * Call is retried when InternalError or FloodWaitError is encountered.
88
+ * Can be set to Infinity.
89
+ *
90
+ * @default 5
91
+ */
92
+ maxRetryCount?: number;
93
+ /**
94
+ * If true, every single API call will be wrapped with `tl.invokeWithoutUpdates`,
95
+ * effectively disabling the server-sent events for the clients.
96
+ * May be useful in some cases.
97
+ *
98
+ * Note that this only wraps calls made with `.call()` within the primary
99
+ * connection. Additional connections and direct `.sendForResult()` calls
100
+ * must be wrapped manually.
101
+ *
102
+ * @default false
103
+ */
104
+ disableUpdates?: boolean;
105
+ /**
106
+ * mtcute can send all unknown RPC errors to [danog](https://github.com/danog)'s
107
+ * [error reporting service](https://rpc.pwrtelegram.xyz/).
108
+ *
109
+ * This is fully anonymous (except maybe IP) and is only used to improve the library
110
+ * and developer experience for everyone working with MTProto. This is fully opt-in,
111
+ * and if you're too paranoid, you can disable it by manually passing `enableErrorReporting: false` to the client.
112
+ *
113
+ * @default false
114
+ */
115
+ enableErrorReporting?: boolean;
116
+ /**
117
+ * If true, RPC errors will have a stack trace of the initial `.call()`
118
+ * or `.sendForResult()` call position, which drastically improves
119
+ * debugging experience.<br>
120
+ * If false, they will have a stack trace of mtcute internals.
121
+ *
122
+ * Internally this creates a stack capture before every RPC call
123
+ * and stores it until the result is received. This might
124
+ * use a lot more memory than normal, thus can be disabled here.
125
+ *
126
+ * @default true
127
+ */
128
+ niceStacks?: boolean;
129
+ /**
130
+ * Extra parameters for {@link NetworkManager}
131
+ */
132
+ network?: NetworkManagerExtraParams;
133
+ /**
134
+ * Set logging level for the client.
135
+ *
136
+ * See static members of {@link LogManager} for possible values.
137
+ */
138
+ logLevel?: number;
139
+ /**
140
+ * **EXPERT USE ONLY!**
141
+ *
142
+ * Override TL layer used for the connection.
143
+ *
144
+ * **Does not** change the schema used.
145
+ */
146
+ overrideLayer?: number;
147
+ /**
148
+ * **EXPERT USE ONLY**
149
+ *
150
+ * Override reader map used for the connection.
151
+ */
152
+ readerMap?: TlReaderMap;
153
+ /**
154
+ * **EXPERT USE ONLY**
155
+ *
156
+ * Override writer map used for the connection.
157
+ */
158
+ writerMap?: TlWriterMap;
159
+ }
160
+ /**
161
+ * Basic Telegram client that only implements the bare minimum
162
+ * to make RPC calls and receive low-level updates.
163
+ */
164
+ export declare class BaseTelegramClient extends EventEmitter {
165
+ /**
166
+ * Crypto provider taken from {@link BaseTelegramClientOptions.crypto}
167
+ */
168
+ readonly crypto: ICryptoProvider;
169
+ /**
170
+ * Telegram storage taken from {@link BaseTelegramClientOptions.storage}
171
+ */
172
+ readonly storage: ITelegramStorage;
173
+ /**
174
+ * API hash taken from {@link BaseTelegramClientOptions.apiHash}
175
+ */
176
+ protected readonly _apiHash: string;
177
+ /**
178
+ * "Use IPv6" taken from {@link BaseTelegramClientOptions.useIpv6}
179
+ */
180
+ protected readonly _useIpv6: boolean;
181
+ /**
182
+ * "Test mode" taken from {@link BaseTelegramClientOptions.testMode}
183
+ */
184
+ protected readonly _testMode: boolean;
185
+ /**
186
+ * Primary DCs taken from {@link BaseTelegramClientOptions.defaultDcs},
187
+ * loaded from session or changed by other means (like redirecting).
188
+ */
189
+ protected _defaultDcs: ITelegramStorage.DcOptions;
190
+ private _niceStacks;
191
+ /** TL layer used by the client */
192
+ readonly _layer: number;
193
+ /** TL readers map used by the client */
194
+ readonly _readerMap: TlReaderMap;
195
+ /** TL writers map used by the client */
196
+ readonly _writerMap: TlWriterMap;
197
+ /** Unix timestamp when the last update was received */
198
+ protected _lastUpdateTime: number;
199
+ readonly _config: ConfigManager;
200
+ private _connected;
201
+ private _onError?;
202
+ private _importFrom?;
203
+ private _importForce?;
204
+ readonly log: LogManager;
205
+ readonly network: NetworkManager;
206
+ constructor(opts: BaseTelegramClientOptions);
207
+ protected _keepAliveAction(): void;
208
+ protected _loadStorage(): Promise<void>;
209
+ _beforeStorageSave: (() => Promise<void>)[];
210
+ beforeStorageSave(cb: () => Promise<void>): void;
211
+ offBeforeStorageSave(cb: () => Promise<void>): void;
212
+ saveStorage(): Promise<void>;
213
+ /**
214
+ * Initialize the connection to the primary DC.
215
+ *
216
+ * You shouldn't usually call this method directly as it is called
217
+ * implicitly the first time you call {@link call}.
218
+ */
219
+ connect(): Promise<void>;
220
+ /**
221
+ * Close all connections and finalize the client.
222
+ */
223
+ close(): Promise<void>;
224
+ /**
225
+ * Make an RPC call to the primary DC.
226
+ * This method handles DC migration, flood waits and retries automatically.
227
+ *
228
+ * If you want more low-level control, use
229
+ * `primaryConnection.sendForResult()` (which is what this method wraps)
230
+ *
231
+ * This method is still quite low-level and you shouldn't use this
232
+ * when using high-level API provided by `@mtcute/client`.
233
+ *
234
+ * @param message RPC method to call
235
+ * @param params Additional call parameters
236
+ */
237
+ call<T extends tl.RpcMethod>(message: MustEqual<T, tl.RpcMethod>, params?: RpcCallOptions): Promise<tl.RpcCallReturn[T['_']]>;
238
+ /**
239
+ * Change transport for the client.
240
+ *
241
+ * Can be used, for example, to change proxy at runtime
242
+ *
243
+ * This effectively calls `changeTransport()` on
244
+ * `primaryConnection` and all additional connections.
245
+ *
246
+ * @param factory New transport factory
247
+ */
248
+ changeTransport(factory: TransportFactory): void;
249
+ /**
250
+ * Register an error handler for the client
251
+ *
252
+ * @param handler
253
+ * Error handler. Called with one or two parameters.
254
+ * The first one is always the error, and the second is
255
+ * the connection in which the error has occurred, in case
256
+ * this was connection-related error.
257
+ */
258
+ onError(handler: (err: unknown, connection?: SessionConnection) => void): void;
259
+ notifyLoggedIn(auth: tl.auth.RawAuthorization): void;
260
+ _emitError(err: unknown, connection?: SessionConnection): void;
261
+ /**
262
+ * Adds all peers from a given object to entity cache in storage.
263
+ *
264
+ * @returns `true` if there were any `min` peers
265
+ */
266
+ _cachePeersFrom(obj: object): Promise<boolean>;
267
+ /**
268
+ * Export current session to a single *LONG* string, containing
269
+ * all the needed information.
270
+ *
271
+ * > **Warning!** Anyone with this string will be able
272
+ * > to authorize as you and do anything. Treat this
273
+ * > as your password, and never give it away!
274
+ * >
275
+ * > In case you have accidentally leaked this string,
276
+ * > make sure to revoke this session in account settings:
277
+ * > "Privacy & Security" > "Active sessions" >
278
+ * > find the one containing `mtcute` > Revoke,
279
+ * > or, in case this is a bot, revoke bot token
280
+ * > with [@BotFather](//t.me/botfather)
281
+ */
282
+ exportSession(): Promise<string>;
283
+ /**
284
+ * Request the session to be imported from the given session string.
285
+ *
286
+ * Note that the string will not be parsed and imported right away,
287
+ * instead, it will be imported when `connect()` is called
288
+ *
289
+ * Also note that the session will only be imported in case
290
+ * the storage is missing authorization (i.e. does not contain
291
+ * auth key for the primary DC), otherwise it will be ignored (unless `force).
292
+ *
293
+ * @param session Session string to import
294
+ * @param force Whether to overwrite existing session
295
+ */
296
+ importSession(session: string, force?: boolean): void;
297
+ }
@@ -0,0 +1,324 @@
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.BaseTelegramClient = void 0;
7
+ /* eslint-disable @typescript-eslint/no-explicit-any */
8
+ const events_1 = __importDefault(require("events"));
9
+ const long_1 = __importDefault(require("long"));
10
+ const tl_1 = require("@mtcute/tl");
11
+ const reader_js_1 = require("@mtcute/tl/binary/reader.js");
12
+ const writer_js_1 = require("@mtcute/tl/binary/writer.js");
13
+ const config_manager_js_1 = require("./network/config-manager.js");
14
+ const network_manager_js_1 = require("./network/network-manager.js");
15
+ const index_js_1 = require("./storage/index.js");
16
+ const index_js_2 = require("./utils/index.js");
17
+ /**
18
+ * Basic Telegram client that only implements the bare minimum
19
+ * to make RPC calls and receive low-level updates.
20
+ */
21
+ class BaseTelegramClient extends events_1.default {
22
+ constructor(opts) {
23
+ super();
24
+ /** Unix timestamp when the last update was received */
25
+ this._lastUpdateTime = 0;
26
+ this._config = new config_manager_js_1.ConfigManager(() => this.call({ _: 'help.getConfig' }));
27
+ // not really connected, but rather "connect() was called"
28
+ this._connected = false;
29
+ this.log = new index_js_2.LogManager('client');
30
+ this._beforeStorageSave = [];
31
+ const apiId = typeof opts.apiId === 'string' ? parseInt(opts.apiId) : opts.apiId;
32
+ if (isNaN(apiId)) {
33
+ throw new Error('apiId must be a number or a numeric string!');
34
+ }
35
+ if (opts.logLevel)
36
+ this.log.level = opts.logLevel;
37
+ this.crypto = (opts.crypto ?? index_js_2.defaultCryptoProviderFactory)();
38
+ this.storage = opts.storage ?? new index_js_1.MemoryStorage();
39
+ this._apiHash = opts.apiHash;
40
+ this._useIpv6 = Boolean(opts.useIpv6);
41
+ this._testMode = Boolean(opts.testMode);
42
+ let dc = opts.defaultDcs;
43
+ if (!dc) {
44
+ if (this._testMode) {
45
+ dc = this._useIpv6 ? index_js_2.defaultTestIpv6Dc : index_js_2.defaultTestDc;
46
+ }
47
+ else {
48
+ dc = this._useIpv6 ? index_js_2.defaultProductionIpv6Dc : index_js_2.defaultProductionDc;
49
+ }
50
+ }
51
+ this._defaultDcs = dc;
52
+ this._niceStacks = opts.niceStacks ?? true;
53
+ this._layer = opts.overrideLayer ?? tl_1.tl.LAYER;
54
+ this._readerMap = opts.readerMap ?? reader_js_1.__tlReaderMap;
55
+ this._writerMap = opts.writerMap ?? writer_js_1.__tlWriterMap;
56
+ this.network = new network_manager_js_1.NetworkManager({
57
+ apiId,
58
+ crypto: this.crypto,
59
+ disableUpdates: opts.disableUpdates ?? false,
60
+ initConnectionOptions: opts.initConnectionOptions,
61
+ layer: this._layer,
62
+ log: this.log,
63
+ readerMap: this._readerMap,
64
+ writerMap: this._writerMap,
65
+ reconnectionStrategy: opts.reconnectionStrategy,
66
+ storage: this.storage,
67
+ testMode: this._testMode,
68
+ transport: opts.transport,
69
+ _emitError: this._emitError.bind(this),
70
+ floodSleepThreshold: opts.floodSleepThreshold ?? 10000,
71
+ maxRetryCount: opts.maxRetryCount ?? 5,
72
+ isPremium: false,
73
+ useIpv6: Boolean(opts.useIpv6),
74
+ keepAliveAction: this._keepAliveAction.bind(this),
75
+ enableErrorReporting: opts.enableErrorReporting ?? false,
76
+ ...(opts.network ?? {}),
77
+ }, this._config);
78
+ this.storage.setup?.(this.log, this._readerMap, this._writerMap);
79
+ }
80
+ _keepAliveAction() {
81
+ this.emit('keep_alive');
82
+ }
83
+ async _loadStorage() {
84
+ await this.storage.load?.();
85
+ }
86
+ beforeStorageSave(cb) {
87
+ this._beforeStorageSave.push(cb);
88
+ }
89
+ offBeforeStorageSave(cb) {
90
+ this._beforeStorageSave = this._beforeStorageSave.filter((x) => x !== cb);
91
+ }
92
+ async saveStorage() {
93
+ for (const cb of this._beforeStorageSave) {
94
+ await cb();
95
+ }
96
+ await this.storage.save?.();
97
+ }
98
+ /**
99
+ * Initialize the connection to the primary DC.
100
+ *
101
+ * You shouldn't usually call this method directly as it is called
102
+ * implicitly the first time you call {@link call}.
103
+ */
104
+ async connect() {
105
+ if (this._connected) {
106
+ // avoid double-connect
107
+ await this._connected;
108
+ return;
109
+ }
110
+ const promise = (this._connected = (0, index_js_2.createControllablePromise)());
111
+ await this._loadStorage();
112
+ const primaryDc = await this.storage.getDefaultDcs();
113
+ if (primaryDc !== null)
114
+ this._defaultDcs = primaryDc;
115
+ const defaultDcAuthKey = await this.storage.getAuthKeyFor(this._defaultDcs.main.id);
116
+ if ((this._importForce || !defaultDcAuthKey) && this._importFrom) {
117
+ const data = (0, index_js_2.readStringSession)(this._readerMap, this._importFrom);
118
+ if (data.testMode !== this._testMode) {
119
+ throw new Error('This session string is not for the current backend. ' +
120
+ `Session is ${data.testMode ? 'test' : 'prod'}, but the client is ${this._testMode ? 'test' : 'prod'}`);
121
+ }
122
+ this._defaultDcs = data.primaryDcs;
123
+ await this.storage.setDefaultDcs(data.primaryDcs);
124
+ if (data.self) {
125
+ await this.storage.setSelf(data.self);
126
+ }
127
+ // await this.primaryConnection.setupKeys(data.authKey)
128
+ await this.storage.setAuthKeyFor(data.primaryDcs.main.id, data.authKey);
129
+ await this.saveStorage();
130
+ }
131
+ this.emit('before_connect');
132
+ this.network
133
+ .connect(this._defaultDcs)
134
+ .then(() => {
135
+ promise.resolve();
136
+ this._connected = true;
137
+ })
138
+ .catch((err) => this._emitError(err));
139
+ }
140
+ /**
141
+ * Close all connections and finalize the client.
142
+ */
143
+ async close() {
144
+ this.emit('before_close');
145
+ this._config.destroy();
146
+ this.network.destroy();
147
+ await this.saveStorage();
148
+ await this.storage.destroy?.();
149
+ this.emit('closed');
150
+ }
151
+ /**
152
+ * Make an RPC call to the primary DC.
153
+ * This method handles DC migration, flood waits and retries automatically.
154
+ *
155
+ * If you want more low-level control, use
156
+ * `primaryConnection.sendForResult()` (which is what this method wraps)
157
+ *
158
+ * This method is still quite low-level and you shouldn't use this
159
+ * when using high-level API provided by `@mtcute/client`.
160
+ *
161
+ * @param message RPC method to call
162
+ * @param params Additional call parameters
163
+ */
164
+ async call(message, params) {
165
+ if (this._connected !== true) {
166
+ await this.connect();
167
+ }
168
+ const stack = this._niceStacks ? new Error().stack : undefined;
169
+ const res = await this.network.call(message, params, stack);
170
+ await this._cachePeersFrom(res);
171
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-return
172
+ return res;
173
+ }
174
+ /**
175
+ * Change transport for the client.
176
+ *
177
+ * Can be used, for example, to change proxy at runtime
178
+ *
179
+ * This effectively calls `changeTransport()` on
180
+ * `primaryConnection` and all additional connections.
181
+ *
182
+ * @param factory New transport factory
183
+ */
184
+ changeTransport(factory) {
185
+ this.network.changeTransport(factory);
186
+ }
187
+ /**
188
+ * Register an error handler for the client
189
+ *
190
+ * @param handler
191
+ * Error handler. Called with one or two parameters.
192
+ * The first one is always the error, and the second is
193
+ * the connection in which the error has occurred, in case
194
+ * this was connection-related error.
195
+ */
196
+ onError(handler) {
197
+ this._onError = handler;
198
+ }
199
+ notifyLoggedIn(auth) {
200
+ this.network.notifyLoggedIn(auth);
201
+ this.emit('logged_in', auth);
202
+ }
203
+ _emitError(err, connection) {
204
+ if (this._onError) {
205
+ this._onError(err, connection);
206
+ }
207
+ else {
208
+ console.error(err);
209
+ }
210
+ }
211
+ /**
212
+ * Adds all peers from a given object to entity cache in storage.
213
+ *
214
+ * @returns `true` if there were any `min` peers
215
+ */
216
+ async _cachePeersFrom(obj) {
217
+ const parsedPeers = [];
218
+ let hadMin = false;
219
+ let count = 0;
220
+ for (const peer of (0, index_js_2.getAllPeersFrom)(obj)) {
221
+ if (peer.min) {
222
+ // absolutely incredible min peer handling, courtesy of levlam.
223
+ // see this thread: https://t.me/tdlibchat/15084
224
+ hadMin = true;
225
+ this.log.debug('received min peer: %j', peer);
226
+ continue;
227
+ }
228
+ count += 1;
229
+ switch (peer._) {
230
+ case 'user':
231
+ if (!peer.accessHash) {
232
+ this.log.warn('received user without access hash: %j', peer);
233
+ continue;
234
+ }
235
+ parsedPeers.push({
236
+ id: peer.id,
237
+ accessHash: peer.accessHash,
238
+ username: peer.username?.toLowerCase(),
239
+ phone: peer.phone,
240
+ type: 'user',
241
+ full: peer,
242
+ });
243
+ break;
244
+ case 'chat':
245
+ case 'chatForbidden':
246
+ parsedPeers.push({
247
+ id: -peer.id,
248
+ accessHash: long_1.default.ZERO,
249
+ type: 'chat',
250
+ full: peer,
251
+ });
252
+ break;
253
+ case 'channel':
254
+ case 'channelForbidden':
255
+ if (!peer.accessHash) {
256
+ this.log.warn('received user without access hash: %j', peer);
257
+ continue;
258
+ }
259
+ parsedPeers.push({
260
+ id: (0, index_js_2.toggleChannelIdMark)(peer.id),
261
+ accessHash: peer.accessHash,
262
+ username: peer._ === 'channel' ? peer.username?.toLowerCase() : undefined,
263
+ type: 'channel',
264
+ full: peer,
265
+ });
266
+ break;
267
+ }
268
+ }
269
+ if (count > 0) {
270
+ await this.storage.updatePeers(parsedPeers);
271
+ this.log.debug('cached %d peers', count);
272
+ }
273
+ return hadMin;
274
+ }
275
+ /**
276
+ * Export current session to a single *LONG* string, containing
277
+ * all the needed information.
278
+ *
279
+ * > **Warning!** Anyone with this string will be able
280
+ * > to authorize as you and do anything. Treat this
281
+ * > as your password, and never give it away!
282
+ * >
283
+ * > In case you have accidentally leaked this string,
284
+ * > make sure to revoke this session in account settings:
285
+ * > "Privacy & Security" > "Active sessions" >
286
+ * > find the one containing `mtcute` > Revoke,
287
+ * > or, in case this is a bot, revoke bot token
288
+ * > with [@BotFather](//t.me/botfather)
289
+ */
290
+ async exportSession() {
291
+ const primaryDcs = await this.storage.getDefaultDcs();
292
+ if (!primaryDcs)
293
+ throw new Error('No default DC set');
294
+ const authKey = await this.storage.getAuthKeyFor(primaryDcs.main.id);
295
+ if (!authKey)
296
+ throw new Error('Auth key is not ready yet');
297
+ return (0, index_js_2.writeStringSession)(this._writerMap, {
298
+ version: 2,
299
+ self: await this.storage.getSelf(),
300
+ testMode: this._testMode,
301
+ primaryDcs,
302
+ authKey,
303
+ });
304
+ }
305
+ /**
306
+ * Request the session to be imported from the given session string.
307
+ *
308
+ * Note that the string will not be parsed and imported right away,
309
+ * instead, it will be imported when `connect()` is called
310
+ *
311
+ * Also note that the session will only be imported in case
312
+ * the storage is missing authorization (i.e. does not contain
313
+ * auth key for the primary DC), otherwise it will be ignored (unless `force).
314
+ *
315
+ * @param session Session string to import
316
+ * @param force Whether to overwrite existing session
317
+ */
318
+ importSession(session, force = false) {
319
+ this._importFrom = session;
320
+ this._importForce = force;
321
+ }
322
+ }
323
+ exports.BaseTelegramClient = BaseTelegramClient;
324
+ //# sourceMappingURL=base-client.js.map