@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
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.mtpAssertTypeIs = exports.assertTypeIsNot = exports.assertTypeIs = exports.hasValueAtKey = exports.hasPresentKey = exports.isPresent = void 0;
4
+ const errors_js_1 = require("../types/errors.js");
5
+ // mostly taken from https://github.com/robertmassaioli/ts-is-present
6
+ function isPresent(t) {
7
+ return t !== undefined && t !== null;
8
+ }
9
+ exports.isPresent = isPresent;
10
+ /**
11
+ * Returns a function that can be used to filter down objects
12
+ * to the ones that have a defined non-null value under the key `k`.
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * const filesWithUrl = files.filter(file => file.url);
17
+ * files[0].url // In this case, TS might still treat this as undefined/null
18
+ *
19
+ * const filesWithUrl = files.filter(hasPresentKey("url"));
20
+ * files[0].url // TS will know that this is present
21
+ * ```
22
+ */
23
+ function hasPresentKey(k) {
24
+ return function (a) {
25
+ return a[k] !== undefined && a[k] !== null;
26
+ };
27
+ }
28
+ exports.hasPresentKey = hasPresentKey;
29
+ /**
30
+ * Returns a function that can be used to filter down objects
31
+ * to the ones that have a specific value V under a key `k`.
32
+ *
33
+ * @example
34
+ * ```ts
35
+ * type File = { type: "image", imageUrl: string } | { type: "pdf", pdfUrl: string };
36
+ * const files: File[] = [];
37
+ *
38
+ * const imageFiles = files.filter(file => file.type === "image");
39
+ * files[0].type // In this case, TS will still treat it as `"image" | "pdf"`
40
+ *
41
+ * const filesWithUrl = files.filter(hasValueKey("type", "image" as const));
42
+ * files[0].type // TS will now know that this is "image"
43
+ * files[0].imageUrl // TS will know this is present, because already it excluded the other union members.
44
+ * ```
45
+ */
46
+ function hasValueAtKey(k, v) {
47
+ return function (a) {
48
+ return a[k] === v;
49
+ };
50
+ }
51
+ exports.hasValueAtKey = hasValueAtKey;
52
+ function assertTypeIs(context, obj, expected) {
53
+ if (obj._ !== expected) {
54
+ throw new errors_js_1.MtTypeAssertionError(context, expected, obj._);
55
+ }
56
+ }
57
+ exports.assertTypeIs = assertTypeIs;
58
+ function assertTypeIsNot(context, obj, expectedNot) {
59
+ if (obj._ === expectedNot) {
60
+ throw new errors_js_1.MtTypeAssertionError(context, 'not ' + expectedNot, obj._);
61
+ }
62
+ }
63
+ exports.assertTypeIsNot = assertTypeIsNot;
64
+ function mtpAssertTypeIs(context, obj, expected) {
65
+ if (obj._ !== expected) {
66
+ throw new errors_js_1.MtTypeAssertionError(context, expected, obj._);
67
+ }
68
+ }
69
+ exports.mtpAssertTypeIs = mtpAssertTypeIs;
70
+ //# sourceMappingURL=type-assertions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type-assertions.js","sourceRoot":"","sources":["../../../src/utils/type-assertions.ts"],"names":[],"mappings":";;;AAEA,kDAAyD;AAEzD,qEAAqE;AAErE,SAAgB,SAAS,CAAI,CAA8B;IACvD,OAAO,CAAC,KAAK,SAAS,IAAI,CAAC,KAAK,IAAI,CAAA;AACxC,CAAC;AAFD,8BAEC;AAED;;;;;;;;;;;;GAYG;AACH,SAAgB,aAAa,CAAqC,CAAI;IAClE,OAAO,UAAiB,CAA8B;QAClD,OAAO,CAAC,CAAC,CAAC,CAAC,KAAK,SAAS,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,IAAI,CAAA;IAC9C,CAAC,CAAA;AACL,CAAC;AAJD,sCAIC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,SAAgB,aAAa,CAAoD,CAAI,EAAE,CAAI;IACvF,OAAO,UAAc,CAA4B;QAC7C,OAAO,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAA;IACrB,CAAC,CAAA;AACL,CAAC;AAJD,sCAIC;AAED,SAAgB,YAAY,CACxB,OAAe,EACf,GAAM,EACN,QAAW;IAEX,IAAI,GAAG,CAAC,CAAC,KAAK,QAAQ,EAAE;QACpB,MAAM,IAAI,gCAAoB,CAAC,OAAO,EAAE,QAAQ,EAAE,GAAG,CAAC,CAAC,CAAC,CAAA;KAC3D;AACL,CAAC;AARD,oCAQC;AAED,SAAgB,eAAe,CAC3B,OAAe,EACf,GAAM,EACN,WAAc;IAEd,IAAI,GAAG,CAAC,CAAC,KAAK,WAAW,EAAE;QACvB,MAAM,IAAI,gCAAoB,CAAC,OAAO,EAAE,MAAM,GAAG,WAAW,EAAE,GAAG,CAAC,CAAC,CAAC,CAAA;KACvE;AACL,CAAC;AARD,0CAQC;AAED,SAAgB,eAAe,CAC3B,OAAe,EACf,GAAM,EACN,QAAW;IAEX,IAAI,GAAG,CAAC,CAAC,KAAK,QAAQ,EAAE;QACpB,MAAM,IAAI,gCAAoB,CAAC,OAAO,EAAE,QAAQ,EAAE,GAAG,CAAC,CAAC,CAAC,CAAA;KAC3D;AACL,CAAC;AARD,0CAQC","sourcesContent":["import { mtp, tl } from '@mtcute/tl'\n\nimport { MtTypeAssertionError } from '../types/errors.js'\n\n// mostly taken from https://github.com/robertmassaioli/ts-is-present\n\nexport function isPresent<T>(t: T | undefined | null | void): t is T {\n return t !== undefined && t !== null\n}\n\n/**\n * Returns a function that can be used to filter down objects\n * to the ones that have a defined non-null value under the key `k`.\n *\n * @example\n * ```ts\n * const filesWithUrl = files.filter(file => file.url);\n * files[0].url // In this case, TS might still treat this as undefined/null\n *\n * const filesWithUrl = files.filter(hasPresentKey(\"url\"));\n * files[0].url // TS will know that this is present\n * ```\n */\nexport function hasPresentKey<K extends string | number | symbol>(k: K) {\n return function <T, V> (a: T & { [k in K]?: V | null }): a is T & { [k in K]: V } {\n return a[k] !== undefined && a[k] !== null\n }\n}\n\n/**\n * Returns a function that can be used to filter down objects\n * to the ones that have a specific value V under a key `k`.\n *\n * @example\n * ```ts\n * type File = { type: \"image\", imageUrl: string } | { type: \"pdf\", pdfUrl: string };\n * const files: File[] = [];\n *\n * const imageFiles = files.filter(file => file.type === \"image\");\n * files[0].type // In this case, TS will still treat it as `\"image\" | \"pdf\"`\n *\n * const filesWithUrl = files.filter(hasValueKey(\"type\", \"image\" as const));\n * files[0].type // TS will now know that this is \"image\"\n * files[0].imageUrl // TS will know this is present, because already it excluded the other union members.\n * ```\n */\nexport function hasValueAtKey<const K extends string | number | symbol, const V>(k: K, v: V) {\n return function <T> (a: T & { [k in K]: unknown }): a is T & { [k in K]: V } {\n return a[k] === v\n }\n}\n\nexport function assertTypeIs<T extends tl.TlObject, K extends T['_']>(\n context: string,\n obj: T,\n expected: K,\n): asserts obj is tl.FindByName<T, K> {\n if (obj._ !== expected) {\n throw new MtTypeAssertionError(context, expected, obj._)\n }\n}\n\nexport function assertTypeIsNot<T extends tl.TlObject, K extends T['_']>(\n context: string,\n obj: T,\n expectedNot: K,\n): asserts obj is Exclude<T, tl.FindByName<T, K>> {\n if (obj._ === expectedNot) {\n throw new MtTypeAssertionError(context, 'not ' + expectedNot, obj._)\n }\n}\n\nexport function mtpAssertTypeIs<T extends mtp.TlObject, K extends T['_']>(\n context: string,\n obj: T,\n expected: K,\n): asserts obj is mtp.FindByName<T, K> {\n if (obj._ !== expected) {\n throw new MtTypeAssertionError(context, expected, obj._)\n }\n}\n"]}
@@ -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,317 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+ import EventEmitter from 'events';
3
+ import Long from 'long';
4
+ import { tl } from '@mtcute/tl';
5
+ import { __tlReaderMap as defaultReaderMap } from '@mtcute/tl/binary/reader.js';
6
+ import { __tlWriterMap as defaultWriterMap } from '@mtcute/tl/binary/writer.js';
7
+ import { ConfigManager } from './network/config-manager.js';
8
+ import { NetworkManager } from './network/network-manager.js';
9
+ import { MemoryStorage } from './storage/index.js';
10
+ import { createControllablePromise, defaultCryptoProviderFactory, defaultProductionDc, defaultProductionIpv6Dc, defaultTestDc, defaultTestIpv6Dc, getAllPeersFrom, LogManager, readStringSession, toggleChannelIdMark, writeStringSession, } from './utils/index.js';
11
+ /**
12
+ * Basic Telegram client that only implements the bare minimum
13
+ * to make RPC calls and receive low-level updates.
14
+ */
15
+ export class BaseTelegramClient extends EventEmitter {
16
+ constructor(opts) {
17
+ super();
18
+ /** Unix timestamp when the last update was received */
19
+ this._lastUpdateTime = 0;
20
+ this._config = new ConfigManager(() => this.call({ _: 'help.getConfig' }));
21
+ // not really connected, but rather "connect() was called"
22
+ this._connected = false;
23
+ this.log = new LogManager('client');
24
+ this._beforeStorageSave = [];
25
+ const apiId = typeof opts.apiId === 'string' ? parseInt(opts.apiId) : opts.apiId;
26
+ if (isNaN(apiId)) {
27
+ throw new Error('apiId must be a number or a numeric string!');
28
+ }
29
+ if (opts.logLevel)
30
+ this.log.level = opts.logLevel;
31
+ this.crypto = (opts.crypto ?? defaultCryptoProviderFactory)();
32
+ this.storage = opts.storage ?? new MemoryStorage();
33
+ this._apiHash = opts.apiHash;
34
+ this._useIpv6 = Boolean(opts.useIpv6);
35
+ this._testMode = Boolean(opts.testMode);
36
+ let dc = opts.defaultDcs;
37
+ if (!dc) {
38
+ if (this._testMode) {
39
+ dc = this._useIpv6 ? defaultTestIpv6Dc : defaultTestDc;
40
+ }
41
+ else {
42
+ dc = this._useIpv6 ? defaultProductionIpv6Dc : defaultProductionDc;
43
+ }
44
+ }
45
+ this._defaultDcs = dc;
46
+ this._niceStacks = opts.niceStacks ?? true;
47
+ this._layer = opts.overrideLayer ?? tl.LAYER;
48
+ this._readerMap = opts.readerMap ?? defaultReaderMap;
49
+ this._writerMap = opts.writerMap ?? defaultWriterMap;
50
+ this.network = new NetworkManager({
51
+ apiId,
52
+ crypto: this.crypto,
53
+ disableUpdates: opts.disableUpdates ?? false,
54
+ initConnectionOptions: opts.initConnectionOptions,
55
+ layer: this._layer,
56
+ log: this.log,
57
+ readerMap: this._readerMap,
58
+ writerMap: this._writerMap,
59
+ reconnectionStrategy: opts.reconnectionStrategy,
60
+ storage: this.storage,
61
+ testMode: this._testMode,
62
+ transport: opts.transport,
63
+ _emitError: this._emitError.bind(this),
64
+ floodSleepThreshold: opts.floodSleepThreshold ?? 10000,
65
+ maxRetryCount: opts.maxRetryCount ?? 5,
66
+ isPremium: false,
67
+ useIpv6: Boolean(opts.useIpv6),
68
+ keepAliveAction: this._keepAliveAction.bind(this),
69
+ enableErrorReporting: opts.enableErrorReporting ?? false,
70
+ ...(opts.network ?? {}),
71
+ }, this._config);
72
+ this.storage.setup?.(this.log, this._readerMap, this._writerMap);
73
+ }
74
+ _keepAliveAction() {
75
+ this.emit('keep_alive');
76
+ }
77
+ async _loadStorage() {
78
+ await this.storage.load?.();
79
+ }
80
+ beforeStorageSave(cb) {
81
+ this._beforeStorageSave.push(cb);
82
+ }
83
+ offBeforeStorageSave(cb) {
84
+ this._beforeStorageSave = this._beforeStorageSave.filter((x) => x !== cb);
85
+ }
86
+ async saveStorage() {
87
+ for (const cb of this._beforeStorageSave) {
88
+ await cb();
89
+ }
90
+ await this.storage.save?.();
91
+ }
92
+ /**
93
+ * Initialize the connection to the primary DC.
94
+ *
95
+ * You shouldn't usually call this method directly as it is called
96
+ * implicitly the first time you call {@link call}.
97
+ */
98
+ async connect() {
99
+ if (this._connected) {
100
+ // avoid double-connect
101
+ await this._connected;
102
+ return;
103
+ }
104
+ const promise = (this._connected = createControllablePromise());
105
+ await this._loadStorage();
106
+ const primaryDc = await this.storage.getDefaultDcs();
107
+ if (primaryDc !== null)
108
+ this._defaultDcs = primaryDc;
109
+ const defaultDcAuthKey = await this.storage.getAuthKeyFor(this._defaultDcs.main.id);
110
+ if ((this._importForce || !defaultDcAuthKey) && this._importFrom) {
111
+ const data = readStringSession(this._readerMap, this._importFrom);
112
+ if (data.testMode !== this._testMode) {
113
+ throw new Error('This session string is not for the current backend. ' +
114
+ `Session is ${data.testMode ? 'test' : 'prod'}, but the client is ${this._testMode ? 'test' : 'prod'}`);
115
+ }
116
+ this._defaultDcs = data.primaryDcs;
117
+ await this.storage.setDefaultDcs(data.primaryDcs);
118
+ if (data.self) {
119
+ await this.storage.setSelf(data.self);
120
+ }
121
+ // await this.primaryConnection.setupKeys(data.authKey)
122
+ await this.storage.setAuthKeyFor(data.primaryDcs.main.id, data.authKey);
123
+ await this.saveStorage();
124
+ }
125
+ this.emit('before_connect');
126
+ this.network
127
+ .connect(this._defaultDcs)
128
+ .then(() => {
129
+ promise.resolve();
130
+ this._connected = true;
131
+ })
132
+ .catch((err) => this._emitError(err));
133
+ }
134
+ /**
135
+ * Close all connections and finalize the client.
136
+ */
137
+ async close() {
138
+ this.emit('before_close');
139
+ this._config.destroy();
140
+ this.network.destroy();
141
+ await this.saveStorage();
142
+ await this.storage.destroy?.();
143
+ this.emit('closed');
144
+ }
145
+ /**
146
+ * Make an RPC call to the primary DC.
147
+ * This method handles DC migration, flood waits and retries automatically.
148
+ *
149
+ * If you want more low-level control, use
150
+ * `primaryConnection.sendForResult()` (which is what this method wraps)
151
+ *
152
+ * This method is still quite low-level and you shouldn't use this
153
+ * when using high-level API provided by `@mtcute/client`.
154
+ *
155
+ * @param message RPC method to call
156
+ * @param params Additional call parameters
157
+ */
158
+ async call(message, params) {
159
+ if (this._connected !== true) {
160
+ await this.connect();
161
+ }
162
+ const stack = this._niceStacks ? new Error().stack : undefined;
163
+ const res = await this.network.call(message, params, stack);
164
+ await this._cachePeersFrom(res);
165
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-return
166
+ return res;
167
+ }
168
+ /**
169
+ * Change transport for the client.
170
+ *
171
+ * Can be used, for example, to change proxy at runtime
172
+ *
173
+ * This effectively calls `changeTransport()` on
174
+ * `primaryConnection` and all additional connections.
175
+ *
176
+ * @param factory New transport factory
177
+ */
178
+ changeTransport(factory) {
179
+ this.network.changeTransport(factory);
180
+ }
181
+ /**
182
+ * Register an error handler for the client
183
+ *
184
+ * @param handler
185
+ * Error handler. Called with one or two parameters.
186
+ * The first one is always the error, and the second is
187
+ * the connection in which the error has occurred, in case
188
+ * this was connection-related error.
189
+ */
190
+ onError(handler) {
191
+ this._onError = handler;
192
+ }
193
+ notifyLoggedIn(auth) {
194
+ this.network.notifyLoggedIn(auth);
195
+ this.emit('logged_in', auth);
196
+ }
197
+ _emitError(err, connection) {
198
+ if (this._onError) {
199
+ this._onError(err, connection);
200
+ }
201
+ else {
202
+ console.error(err);
203
+ }
204
+ }
205
+ /**
206
+ * Adds all peers from a given object to entity cache in storage.
207
+ *
208
+ * @returns `true` if there were any `min` peers
209
+ */
210
+ async _cachePeersFrom(obj) {
211
+ const parsedPeers = [];
212
+ let hadMin = false;
213
+ let count = 0;
214
+ for (const peer of getAllPeersFrom(obj)) {
215
+ if (peer.min) {
216
+ // absolutely incredible min peer handling, courtesy of levlam.
217
+ // see this thread: https://t.me/tdlibchat/15084
218
+ hadMin = true;
219
+ this.log.debug('received min peer: %j', peer);
220
+ continue;
221
+ }
222
+ count += 1;
223
+ switch (peer._) {
224
+ case 'user':
225
+ if (!peer.accessHash) {
226
+ this.log.warn('received user without access hash: %j', peer);
227
+ continue;
228
+ }
229
+ parsedPeers.push({
230
+ id: peer.id,
231
+ accessHash: peer.accessHash,
232
+ username: peer.username?.toLowerCase(),
233
+ phone: peer.phone,
234
+ type: 'user',
235
+ full: peer,
236
+ });
237
+ break;
238
+ case 'chat':
239
+ case 'chatForbidden':
240
+ parsedPeers.push({
241
+ id: -peer.id,
242
+ accessHash: Long.ZERO,
243
+ type: 'chat',
244
+ full: peer,
245
+ });
246
+ break;
247
+ case 'channel':
248
+ case 'channelForbidden':
249
+ if (!peer.accessHash) {
250
+ this.log.warn('received user without access hash: %j', peer);
251
+ continue;
252
+ }
253
+ parsedPeers.push({
254
+ id: toggleChannelIdMark(peer.id),
255
+ accessHash: peer.accessHash,
256
+ username: peer._ === 'channel' ? peer.username?.toLowerCase() : undefined,
257
+ type: 'channel',
258
+ full: peer,
259
+ });
260
+ break;
261
+ }
262
+ }
263
+ if (count > 0) {
264
+ await this.storage.updatePeers(parsedPeers);
265
+ this.log.debug('cached %d peers', count);
266
+ }
267
+ return hadMin;
268
+ }
269
+ /**
270
+ * Export current session to a single *LONG* string, containing
271
+ * all the needed information.
272
+ *
273
+ * > **Warning!** Anyone with this string will be able
274
+ * > to authorize as you and do anything. Treat this
275
+ * > as your password, and never give it away!
276
+ * >
277
+ * > In case you have accidentally leaked this string,
278
+ * > make sure to revoke this session in account settings:
279
+ * > "Privacy & Security" > "Active sessions" >
280
+ * > find the one containing `mtcute` > Revoke,
281
+ * > or, in case this is a bot, revoke bot token
282
+ * > with [@BotFather](//t.me/botfather)
283
+ */
284
+ async exportSession() {
285
+ const primaryDcs = await this.storage.getDefaultDcs();
286
+ if (!primaryDcs)
287
+ throw new Error('No default DC set');
288
+ const authKey = await this.storage.getAuthKeyFor(primaryDcs.main.id);
289
+ if (!authKey)
290
+ throw new Error('Auth key is not ready yet');
291
+ return writeStringSession(this._writerMap, {
292
+ version: 2,
293
+ self: await this.storage.getSelf(),
294
+ testMode: this._testMode,
295
+ primaryDcs,
296
+ authKey,
297
+ });
298
+ }
299
+ /**
300
+ * Request the session to be imported from the given session string.
301
+ *
302
+ * Note that the string will not be parsed and imported right away,
303
+ * instead, it will be imported when `connect()` is called
304
+ *
305
+ * Also note that the session will only be imported in case
306
+ * the storage is missing authorization (i.e. does not contain
307
+ * auth key for the primary DC), otherwise it will be ignored (unless `force).
308
+ *
309
+ * @param session Session string to import
310
+ * @param force Whether to overwrite existing session
311
+ */
312
+ importSession(session, force = false) {
313
+ this._importFrom = session;
314
+ this._importForce = force;
315
+ }
316
+ }
317
+ //# sourceMappingURL=base-client.js.map