@mtcute/core 0.19.3 → 0.19.6

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.
@@ -33,7 +33,14 @@ class BaseTelegramClient {
33
33
  this.platform = this.params.platform;
34
34
  this.mt = new client.MtClient({
35
35
  ...this.params,
36
- logger: this.log.create("mtproto")
36
+ logger: this.log.create("mtproto"),
37
+ onError: (err) => {
38
+ if (this.onError.length > 0) {
39
+ this.onError.emit(utils.unknownToError(err));
40
+ } else if (this._connect.finished()) {
41
+ this.log.error("unhandled error:", err);
42
+ }
43
+ }
37
44
  });
38
45
  if (!params.disableUpdates && params.updates !== false) {
39
46
  this.updates = new manager.UpdatesManager(this, params.updates);
package/highlevel/base.js CHANGED
@@ -26,7 +26,14 @@ class BaseTelegramClient {
26
26
  this.platform = this.params.platform;
27
27
  this.mt = new MtClient({
28
28
  ...this.params,
29
- logger: this.log.create("mtproto")
29
+ logger: this.log.create("mtproto"),
30
+ onError: (err) => {
31
+ if (this.onError.length > 0) {
32
+ this.onError.emit(unknownToError(err));
33
+ } else if (this._connect.finished()) {
34
+ this.log.error("unhandled error:", err);
35
+ }
36
+ }
30
37
  });
31
38
  if (!params.disableUpdates && params.updates !== false) {
32
39
  this.updates = new UpdatesManager(this, params.updates);
@@ -144,11 +144,17 @@ export interface TelegramClient extends ITelegramClient {
144
144
  *
145
145
  * **Available**: 👤 users only
146
146
  *
147
- * @param password Your Two-Step verification password
148
147
  * @returns The authorized user
149
148
  * @throws BadRequestError In case the password is invalid
150
149
  */
151
- checkPassword(password: string): Promise<User>;
150
+ checkPassword(options: string | {
151
+ /** Your Two-Step verification password */
152
+ password: string;
153
+ /** Existing response from `account.getPassword`, if available (to avoid extra API calls) */
154
+ passwordObj?: tl.account.TypePassword;
155
+ /** Abort signal */
156
+ abortSignal?: AbortSignal;
157
+ }): Promise<User>;
152
158
  /**
153
159
  * Get your Two-Step Verification password hint.
154
160
  *
@@ -3819,7 +3825,9 @@ export interface TelegramClient extends ITelegramClient {
3819
3825
  *
3820
3826
  * @returns Whether the action was successful
3821
3827
  */
3822
- acceptStarGift(params: InputMessageId & {
3828
+ acceptStarGift(params: {
3829
+ /** ID of the message containing the gift */
3830
+ message: number | Message;
3823
3831
  /**
3824
3832
  * Action to perform on the gift.
3825
3833
  * - `save` - save the gift to your profile
@@ -144,11 +144,17 @@ export interface TelegramClient extends ITelegramClient {
144
144
  *
145
145
  * **Available**: 👤 users only
146
146
  *
147
- * @param password Your Two-Step verification password
148
147
  * @returns The authorized user
149
148
  * @throws BadRequestError In case the password is invalid
150
149
  */
151
- checkPassword(password: string): Promise<User>;
150
+ checkPassword(options: string | {
151
+ /** Your Two-Step verification password */
152
+ password: string;
153
+ /** Existing response from `account.getPassword`, if available (to avoid extra API calls) */
154
+ passwordObj?: tl.account.TypePassword;
155
+ /** Abort signal */
156
+ abortSignal?: AbortSignal;
157
+ }): Promise<User>;
152
158
  /**
153
159
  * Get your Two-Step Verification password hint.
154
160
  *
@@ -3819,7 +3825,9 @@ export interface TelegramClient extends ITelegramClient {
3819
3825
  *
3820
3826
  * @returns Whether the action was successful
3821
3827
  */
3822
- acceptStarGift(params: InputMessageId & {
3828
+ acceptStarGift(params: {
3829
+ /** ID of the message containing the gift */
3830
+ message: number | Message;
3823
3831
  /**
3824
3832
  * Action to perform on the gift.
3825
3833
  * - `save` - save the gift to your profile
@@ -6,16 +6,18 @@ if (typeof globalThis !== "undefined" && !globalThis._MTCUTE_CJS_DEPRECATION_WAR
6
6
  "use strict";
7
7
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
8
8
  const utils = require("./utils.cjs");
9
- async function checkPassword(client, password) {
9
+ async function checkPassword(client, options) {
10
+ const opts = typeof options === "string" ? { password: options } : options;
11
+ const passwordObj = opts.passwordObj ?? await client.call({
12
+ _: "account.getPassword"
13
+ }, { abortSignal: opts.abortSignal });
10
14
  const res = await client.call({
11
15
  _: "auth.checkPassword",
12
16
  password: await client.computeSrpParams(
13
- await client.call({
14
- _: "account.getPassword"
15
- }),
16
- password
17
+ passwordObj,
18
+ opts.password
17
19
  )
18
- });
20
+ }, { abortSignal: opts.abortSignal });
19
21
  return utils._onAuthorization(client, res);
20
22
  }
21
23
  exports.checkPassword = checkPassword;
@@ -1,10 +1,17 @@
1
+ import { tl } from '@mtcute/tl';
1
2
  import { ITelegramClient } from '../../client.types.js';
2
3
  import { User } from '../../types/index.js';
3
4
  /**
4
5
  * Check your Two-Step verification password and log in
5
6
  *
6
- * @param password Your Two-Step verification password
7
7
  * @returns The authorized user
8
8
  * @throws BadRequestError In case the password is invalid
9
9
  */
10
- export declare function checkPassword(client: ITelegramClient, password: string): Promise<User>;
10
+ export declare function checkPassword(client: ITelegramClient, options: string | {
11
+ /** Your Two-Step verification password */
12
+ password: string;
13
+ /** Existing response from `account.getPassword`, if available (to avoid extra API calls) */
14
+ passwordObj?: tl.account.TypePassword;
15
+ /** Abort signal */
16
+ abortSignal?: AbortSignal;
17
+ }): Promise<User>;
@@ -1,10 +1,17 @@
1
+ import { tl } from '@mtcute/tl';
1
2
  import { ITelegramClient } from '../../client.types.js';
2
3
  import { User } from '../../types/index.js';
3
4
  /**
4
5
  * Check your Two-Step verification password and log in
5
6
  *
6
- * @param password Your Two-Step verification password
7
7
  * @returns The authorized user
8
8
  * @throws BadRequestError In case the password is invalid
9
9
  */
10
- export declare function checkPassword(client: ITelegramClient, password: string): Promise<User>;
10
+ export declare function checkPassword(client: ITelegramClient, options: string | {
11
+ /** Your Two-Step verification password */
12
+ password: string;
13
+ /** Existing response from `account.getPassword`, if available (to avoid extra API calls) */
14
+ passwordObj?: tl.account.TypePassword;
15
+ /** Abort signal */
16
+ abortSignal?: AbortSignal;
17
+ }): Promise<User>;
@@ -1,14 +1,16 @@
1
1
  import { _onAuthorization } from "./utils.js";
2
- async function checkPassword(client, password) {
2
+ async function checkPassword(client, options) {
3
+ const opts = typeof options === "string" ? { password: options } : options;
4
+ const passwordObj = opts.passwordObj ?? await client.call({
5
+ _: "account.getPassword"
6
+ }, { abortSignal: opts.abortSignal });
3
7
  const res = await client.call({
4
8
  _: "auth.checkPassword",
5
9
  password: await client.computeSrpParams(
6
- await client.call({
7
- _: "account.getPassword"
8
- }),
9
- password
10
+ passwordObj,
11
+ opts.password
10
12
  )
11
- });
13
+ }, { abortSignal: opts.abortSignal });
12
14
  return _onAuthorization(client, res);
13
15
  }
14
16
  export {
@@ -32,7 +32,10 @@ async function signInQr(client, params) {
32
32
  while (true) {
33
33
  const password = await miscUtils.resolveMaybeDynamic(input);
34
34
  try {
35
- return await checkPassword.checkPassword(client, password);
35
+ return await checkPassword.checkPassword(client, {
36
+ password,
37
+ abortSignal
38
+ });
36
39
  } catch (e) {
37
40
  if (tl.tl.RpcError.is(e, "PASSWORD_HASH_INVALID")) {
38
41
  if (!isDynamic) {
@@ -25,7 +25,10 @@ async function signInQr(client, params) {
25
25
  while (true) {
26
26
  const password = await resolveMaybeDynamic(input);
27
27
  try {
28
- return await checkPassword(client, password);
28
+ return await checkPassword(client, {
29
+ password,
30
+ abortSignal
31
+ });
29
32
  } catch (e) {
30
33
  if (tl.RpcError.is(e, "PASSWORD_HASH_INVALID")) {
31
34
  if (!isDynamic) {
@@ -10,6 +10,7 @@ const errors = require("../../../types/errors.cjs");
10
10
  const miscUtils = require("../../utils/misc-utils.cjs");
11
11
  const getMe = require("../users/get-me.cjs");
12
12
  const checkPassword = require("./check-password.cjs");
13
+ const logOut = require("./log-out.cjs");
13
14
  const resendCode = require("./resend-code.cjs");
14
15
  const sendCode = require("./send-code.cjs");
15
16
  const signInBot = require("./sign-in-bot.cjs");
@@ -30,8 +31,15 @@ async function start(client, params) {
30
31
  return me;
31
32
  } catch (e) {
32
33
  if (tl.tl.RpcError.is(e)) {
33
- if (e.text === "SESSION_PASSWORD_NEEDED") has2fa = true;
34
- else if (e.text !== "AUTH_KEY_UNREGISTERED") throw e;
34
+ if (e.text === "SESSION_PASSWORD_NEEDED") {
35
+ has2fa = true;
36
+ } else if (e.text === "SESSION_REVOKED" || e.text === "USER_DEACTIVATED" || e.text === "USER_DEACTIVATED_BAN") {
37
+ await logOut.logOut(client).catch((err) => {
38
+ client.log.warn("failed to log out: %e", err);
39
+ });
40
+ } else if (e.text !== "AUTH_KEY_UNREGISTERED") {
41
+ throw e;
42
+ }
35
43
  } else {
36
44
  throw e;
37
45
  }
@@ -123,7 +131,10 @@ async function start(client, params) {
123
131
  for (; ; ) {
124
132
  const password = await miscUtils.resolveMaybeDynamic(params.password);
125
133
  try {
126
- return await checkPassword.checkPassword(client, password);
134
+ return await checkPassword.checkPassword(client, {
135
+ password,
136
+ abortSignal
137
+ });
127
138
  } catch (e) {
128
139
  if (typeof params.password !== "function") {
129
140
  throw new errors.MtArgumentError("Provided password was invalid");
@@ -3,6 +3,7 @@ import { MtArgumentError, MtcuteError } from "../../../types/errors.js";
3
3
  import { resolveMaybeDynamic, normalizePhoneNumber } from "../../utils/misc-utils.js";
4
4
  import { getMe } from "../users/get-me.js";
5
5
  import { checkPassword } from "./check-password.js";
6
+ import { logOut } from "./log-out.js";
6
7
  import { resendCode } from "./resend-code.js";
7
8
  import { sendCode } from "./send-code.js";
8
9
  import { signInBot } from "./sign-in-bot.js";
@@ -23,8 +24,15 @@ async function start(client, params) {
23
24
  return me;
24
25
  } catch (e) {
25
26
  if (tl.RpcError.is(e)) {
26
- if (e.text === "SESSION_PASSWORD_NEEDED") has2fa = true;
27
- else if (e.text !== "AUTH_KEY_UNREGISTERED") throw e;
27
+ if (e.text === "SESSION_PASSWORD_NEEDED") {
28
+ has2fa = true;
29
+ } else if (e.text === "SESSION_REVOKED" || e.text === "USER_DEACTIVATED" || e.text === "USER_DEACTIVATED_BAN") {
30
+ await logOut(client).catch((err) => {
31
+ client.log.warn("failed to log out: %e", err);
32
+ });
33
+ } else if (e.text !== "AUTH_KEY_UNREGISTERED") {
34
+ throw e;
35
+ }
28
36
  } else {
29
37
  throw e;
30
38
  }
@@ -116,7 +124,10 @@ async function start(client, params) {
116
124
  for (; ; ) {
117
125
  const password = await resolveMaybeDynamic(params.password);
118
126
  try {
119
- return await checkPassword(client, password);
127
+ return await checkPassword(client, {
128
+ password,
129
+ abortSignal
130
+ });
120
131
  } catch (e) {
121
132
  if (typeof params.password !== "function") {
122
133
  throw new MtArgumentError("Provided password was invalid");
@@ -62,7 +62,7 @@ class MtClient {
62
62
  storage: this.storage,
63
63
  testMode: Boolean(params.testMode),
64
64
  transport: params.transport,
65
- emitError: this.emitError.bind(this),
65
+ emitError: params.onError,
66
66
  isPremium: false,
67
67
  useIpv6: Boolean(params.useIpv6),
68
68
  enableErrorReporting: params.enableErrorReporting ?? false,
@@ -103,7 +103,6 @@ class MtClient {
103
103
  if (typeAssertions.isTlRpcError(res)) throw new Error(`Failed to get config: ${res.errorMessage}`);
104
104
  return res;
105
105
  });
106
- _emitError;
107
106
  log;
108
107
  network;
109
108
  _abortController;
@@ -112,13 +111,6 @@ class MtClient {
112
111
  onConnecting = new utils.Emitter();
113
112
  onNetworkChanged = new utils.Emitter();
114
113
  onUpdate = new utils.Emitter();
115
- emitError(err) {
116
- if (this._emitError) {
117
- this._emitError(err);
118
- } else if (this._connect.finished()) {
119
- this.log.error("unhandled error:", err);
120
- }
121
- }
122
114
  _prepare = functionUtils.asyncResettable(async () => {
123
115
  await this.crypto.initialize?.();
124
116
  await this.storage.load();
@@ -179,13 +171,5 @@ class MtClient {
179
171
  async call(message, params) {
180
172
  return this.network.call(message, params);
181
173
  }
182
- /**
183
- * Register an error handler for the client
184
- *
185
- * @param handler Error handler.
186
- */
187
- onError(handler) {
188
- this._emitError = handler;
189
- }
190
174
  }
191
175
  exports.MtClient = MtClient;
@@ -139,7 +139,9 @@ export interface MtClientOptions {
139
139
  * some APIs to manage that.
140
140
  */
141
141
  export declare class MtClient {
142
- readonly params: MtClientOptions;
142
+ readonly params: MtClientOptions & {
143
+ onError: (err: unknown) => void;
144
+ };
143
145
  /**
144
146
  * Crypto provider taken from {@link MtClientOptions.crypto}
145
147
  */
@@ -162,7 +164,6 @@ export declare class MtClient {
162
164
  /** TL writers map used by the client */
163
165
  readonly _writerMap: TlWriterMap;
164
166
  readonly _config: ConfigManager;
165
- private _emitError?;
166
167
  readonly log: Logger;
167
168
  readonly network: NetworkManager;
168
169
  private _abortController;
@@ -171,8 +172,9 @@ export declare class MtClient {
171
172
  readonly onConnecting: Emitter<void>;
172
173
  readonly onNetworkChanged: Emitter<boolean>;
173
174
  readonly onUpdate: Emitter<tl.TypeUpdates>;
174
- constructor(params: MtClientOptions);
175
- emitError(err: unknown): void;
175
+ constructor(params: MtClientOptions & {
176
+ onError: (err: unknown) => void;
177
+ });
176
178
  private _prepare;
177
179
  /**
178
180
  * **ADVANCED**
@@ -209,10 +211,4 @@ export declare class MtClient {
209
211
  * @param params Additional call parameters
210
212
  */
211
213
  call<T extends tl.RpcMethod>(message: MustEqual<T, tl.RpcMethod>, params?: RpcCallOptions): Promise<tl.RpcCallReturn[T['_']] | mtp.RawMt_rpc_error>;
212
- /**
213
- * Register an error handler for the client
214
- *
215
- * @param handler Error handler.
216
- */
217
- onError(handler: (err: unknown) => void): void;
218
214
  }
@@ -139,7 +139,9 @@ export interface MtClientOptions {
139
139
  * some APIs to manage that.
140
140
  */
141
141
  export declare class MtClient {
142
- readonly params: MtClientOptions;
142
+ readonly params: MtClientOptions & {
143
+ onError: (err: unknown) => void;
144
+ };
143
145
  /**
144
146
  * Crypto provider taken from {@link MtClientOptions.crypto}
145
147
  */
@@ -162,7 +164,6 @@ export declare class MtClient {
162
164
  /** TL writers map used by the client */
163
165
  readonly _writerMap: TlWriterMap;
164
166
  readonly _config: ConfigManager;
165
- private _emitError?;
166
167
  readonly log: Logger;
167
168
  readonly network: NetworkManager;
168
169
  private _abortController;
@@ -171,8 +172,9 @@ export declare class MtClient {
171
172
  readonly onConnecting: Emitter<void>;
172
173
  readonly onNetworkChanged: Emitter<boolean>;
173
174
  readonly onUpdate: Emitter<tl.TypeUpdates>;
174
- constructor(params: MtClientOptions);
175
- emitError(err: unknown): void;
175
+ constructor(params: MtClientOptions & {
176
+ onError: (err: unknown) => void;
177
+ });
176
178
  private _prepare;
177
179
  /**
178
180
  * **ADVANCED**
@@ -209,10 +211,4 @@ export declare class MtClient {
209
211
  * @param params Additional call parameters
210
212
  */
211
213
  call<T extends tl.RpcMethod>(message: MustEqual<T, tl.RpcMethod>, params?: RpcCallOptions): Promise<tl.RpcCallReturn[T['_']] | mtp.RawMt_rpc_error>;
212
- /**
213
- * Register an error handler for the client
214
- *
215
- * @param handler Error handler.
216
- */
217
- onError(handler: (err: unknown) => void): void;
218
214
  }
package/network/client.js CHANGED
@@ -55,7 +55,7 @@ class MtClient {
55
55
  storage: this.storage,
56
56
  testMode: Boolean(params.testMode),
57
57
  transport: params.transport,
58
- emitError: this.emitError.bind(this),
58
+ emitError: params.onError,
59
59
  isPremium: false,
60
60
  useIpv6: Boolean(params.useIpv6),
61
61
  enableErrorReporting: params.enableErrorReporting ?? false,
@@ -96,7 +96,6 @@ class MtClient {
96
96
  if (isTlRpcError(res)) throw new Error(`Failed to get config: ${res.errorMessage}`);
97
97
  return res;
98
98
  });
99
- _emitError;
100
99
  log;
101
100
  network;
102
101
  _abortController;
@@ -105,13 +104,6 @@ class MtClient {
105
104
  onConnecting = new Emitter();
106
105
  onNetworkChanged = new Emitter();
107
106
  onUpdate = new Emitter();
108
- emitError(err) {
109
- if (this._emitError) {
110
- this._emitError(err);
111
- } else if (this._connect.finished()) {
112
- this.log.error("unhandled error:", err);
113
- }
114
- }
115
107
  _prepare = asyncResettable(async () => {
116
108
  await this.crypto.initialize?.();
117
109
  await this.storage.load();
@@ -172,14 +164,6 @@ class MtClient {
172
164
  async call(message, params) {
173
165
  return this.network.call(message, params);
174
166
  }
175
- /**
176
- * Register an error handler for the client
177
- *
178
- * @param handler Error handler.
179
- */
180
- onError(handler) {
181
- this._emitError = handler;
182
- }
183
167
  }
184
168
  export {
185
169
  MtClient
@@ -220,7 +220,7 @@ class NetworkManager {
220
220
  _: "initConnection",
221
221
  deviceModel,
222
222
  systemVersion: "1.0",
223
- appVersion: "0.19.3",
223
+ appVersion: "0.19.6",
224
224
  systemLangCode: "en",
225
225
  langPack: "",
226
226
  // "langPacks are for official apps only"
@@ -213,7 +213,7 @@ class NetworkManager {
213
213
  _: "initConnection",
214
214
  deviceModel,
215
215
  systemVersion: "1.0",
216
- appVersion: "0.19.3",
216
+ appVersion: "0.19.6",
217
217
  systemLangCode: "en",
218
218
  langPack: "",
219
219
  // "langPacks are for official apps only"
@@ -172,6 +172,7 @@ class SessionConnection extends persistentConnection.PersistentConnection {
172
172
  return;
173
173
  }
174
174
  this.log.warn("transport error 404, reauthorizing");
175
+ this.onError.emit(error);
175
176
  this._session.resetAuthKey();
176
177
  this._resetSession();
177
178
  this.onKeyChange.emit(null);
@@ -165,6 +165,7 @@ class SessionConnection extends PersistentConnection {
165
165
  return;
166
166
  }
167
167
  this.log.warn("transport error 404, reauthorizing");
168
+ this.onError.emit(error);
168
169
  this._session.resetAuthKey();
169
170
  this._resetSession();
170
171
  this.onKeyChange.emit(null);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mtcute/core",
3
3
  "type": "module",
4
- "version": "0.19.3",
4
+ "version": "0.19.6",
5
5
  "description": "Type-safe library for MTProto (Telegram API)",
6
6
  "license": "MIT",
7
7
  "scripts": {},