@mtkruto/node 0.0.821 → 0.0.822
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.
- package/esm/client/client.d.ts +78 -14
- package/esm/client/client.js +126 -1
- package/esm/tl/2_types.d.ts +48 -48
- package/esm/tl/2_types.js +180 -180
- package/esm/tl/3_functions.d.ts +13 -13
- package/esm/tl/3_functions.js +39 -39
- package/esm/utilities/1_password.d.ts +1 -1
- package/esm/utilities/1_password.js +2 -1
- package/package.json +1 -1
- package/script/client/client.d.ts +78 -14
- package/script/client/client.js +127 -2
- package/script/tl/2_types.d.ts +48 -48
- package/script/tl/2_types.js +180 -180
- package/script/tl/3_functions.d.ts +13 -13
- package/script/tl/3_functions.js +39 -39
- package/script/utilities/1_password.d.ts +1 -1
- package/script/utilities/1_password.js +2 -1
package/esm/client/client.d.ts
CHANGED
|
@@ -4,16 +4,43 @@ import * as functions from "../tl/3_functions.js";
|
|
|
4
4
|
import { ClientAbstract } from "./client_abstract.js";
|
|
5
5
|
import { Session } from "../session/session.js";
|
|
6
6
|
import { DC, TransportProvider } from "../transport/transport_provider.js";
|
|
7
|
-
export
|
|
8
|
-
|
|
9
|
-
last: S;
|
|
10
|
-
}> {
|
|
7
|
+
export declare const restartAuth: unique symbol;
|
|
8
|
+
export interface AuthorizeUserParams<S = string> {
|
|
11
9
|
phone: S | (() => MaybePromise<S>);
|
|
12
10
|
code: S | (() => MaybePromise<S>);
|
|
13
11
|
password: S | (() => MaybePromise<S>);
|
|
14
|
-
names: N | (() => MaybePromise<N>);
|
|
15
12
|
}
|
|
16
13
|
export type UpdatesHandler = null | ((client: Client, update: types.Updates) => MaybePromise<void>);
|
|
14
|
+
export interface ClientParams {
|
|
15
|
+
/**
|
|
16
|
+
* The transport provider to use. Defaults to `defaultTransportProvider`.
|
|
17
|
+
*/
|
|
18
|
+
transportProvider?: TransportProvider;
|
|
19
|
+
/**
|
|
20
|
+
* The app_version parameter to be passed to initConnection when calling `authorize`.
|
|
21
|
+
*/
|
|
22
|
+
appVersion?: string;
|
|
23
|
+
/**
|
|
24
|
+
* The device_version parameter to be passed to initConnection when calling `authorize`.
|
|
25
|
+
*/
|
|
26
|
+
deviceModel?: string;
|
|
27
|
+
/**
|
|
28
|
+
* The lang_code parameter to be passed to initConnection when calling `authorize`.
|
|
29
|
+
*/
|
|
30
|
+
langCode?: string;
|
|
31
|
+
/**
|
|
32
|
+
* The lang_pack parameter to be passed to initConnection when calling `authorize`.
|
|
33
|
+
*/
|
|
34
|
+
langPack?: string;
|
|
35
|
+
/**
|
|
36
|
+
* The system_lang_cde parameter to be passed to initConnection when calling `authorize`.
|
|
37
|
+
*/
|
|
38
|
+
systemLangCode?: string;
|
|
39
|
+
/**
|
|
40
|
+
* The system_version parameter to be passed to initConnection when calling `authorize`.
|
|
41
|
+
*/
|
|
42
|
+
systemVersion?: string;
|
|
43
|
+
}
|
|
17
44
|
export declare class Client extends ClientAbstract {
|
|
18
45
|
readonly session: Session;
|
|
19
46
|
readonly apiId: number;
|
|
@@ -29,22 +56,59 @@ export declare class Client extends ClientAbstract {
|
|
|
29
56
|
readonly langPack: string;
|
|
30
57
|
readonly systemLangCode: string;
|
|
31
58
|
readonly systemVersion: string;
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
59
|
+
/**
|
|
60
|
+
* Constructs the client.
|
|
61
|
+
*
|
|
62
|
+
* @param session The session provider to use. Defaults to in-memory session.
|
|
63
|
+
* @param apiId App's API ID from [my.telegram.org](https://my.telegram.org/apps). Defaults to 0 (unset).
|
|
64
|
+
* @param apiHash App's API hash from [my.telegram.org/apps](https://my.telegram.org/apps). Default to empty string (unset).
|
|
65
|
+
* @param params Other parameters.
|
|
66
|
+
*/
|
|
67
|
+
constructor(session?: Session, apiId?: number, apiHash?: string, params?: ClientParams);
|
|
41
68
|
private shouldLoadSession;
|
|
69
|
+
/**
|
|
70
|
+
* Sets the DC and resets the auth key stored in the session provider
|
|
71
|
+
* if the stored DC was not the same as the `dc` parameter.
|
|
72
|
+
*
|
|
73
|
+
* @param dc The DC to change to.
|
|
74
|
+
*/
|
|
42
75
|
setDc(dc: DC): void;
|
|
76
|
+
/**
|
|
77
|
+
* Loads the session if `setDc` was not called, initializes and connnects
|
|
78
|
+
* a `ClientPlain` to generate auth key if there was none, and connects the client.
|
|
79
|
+
* Before establishing the connection, the session is saved.
|
|
80
|
+
*/
|
|
43
81
|
connect(): Promise<void>;
|
|
82
|
+
/**
|
|
83
|
+
* Calls [initConnection](1) and authorizes the client with one of the following:
|
|
84
|
+
*
|
|
85
|
+
* - Bot token (`string`)
|
|
86
|
+
* - Exported authorization (`types.AuthExportedAuthorization`)
|
|
87
|
+
* - User authorization handlers (`AuthorizeUserParams`)
|
|
88
|
+
*
|
|
89
|
+
* if the current auth key doesn't throw AUTH_KEY_UNREGISTERED when calling [updates.getState](2).
|
|
90
|
+
*
|
|
91
|
+
* Notes:
|
|
92
|
+
* 1. Requires the `apiId` and `apiHash` paramters to be passed when constructing the client.
|
|
93
|
+
* 2. Reconnects the client to the appropriate DC in case of MIGRATE_X errors.
|
|
94
|
+
* 3. The parameters passed to the [initConnection][1] call can be configured with the last parameter of the constructor.
|
|
95
|
+
*
|
|
96
|
+
* [1]: https://core.telegram.org/method/initConnection
|
|
97
|
+
* [2]: https://core.telegram.org/method/updates.getState
|
|
98
|
+
*/
|
|
44
99
|
authorize(params: string | types.AuthExportedAuthorization | AuthorizeUserParams): Promise<void>;
|
|
45
100
|
private receiveLoop;
|
|
46
101
|
private pingLoop;
|
|
102
|
+
/**
|
|
103
|
+
* Invokes a function waiting and returning its reply if the second parameter is not `true`. Requires the client
|
|
104
|
+
* to be connected.
|
|
105
|
+
*
|
|
106
|
+
* @param function_ The function to invoke.
|
|
107
|
+
*/
|
|
47
108
|
invoke<T extends (functions.Function<unknown> | types.Type) = functions.Function<unknown>>(function_: T): Promise<T extends functions.Function<unknown> ? T["__R"] : void>;
|
|
48
109
|
invoke<T extends (functions.Function<unknown> | types.Type) = functions.Function<unknown>>(function_: T, noWait: true): Promise<void>;
|
|
110
|
+
/**
|
|
111
|
+
* Alias for `invoke` with its second parameter being `true`.
|
|
112
|
+
*/
|
|
49
113
|
send<T extends (functions.Function<unknown> | types.Type) = functions.Function<unknown>>(function_: T): Promise<void>;
|
|
50
114
|
}
|
package/esm/client/client.js
CHANGED
|
@@ -2,6 +2,7 @@ import { gunzip } from "../deps.js";
|
|
|
2
2
|
import { ackThreshold, DEFAULT_APP_VERSION, DEFAULT_DEVICE_MODEL, DEFAULT_LANG_CODE, DEFAULT_LANG_PACK, DEFAULT_SYSTEM_LANG_CODE, DEFAULT_SYSTEM_VERSION, LAYER } from "../constants.js";
|
|
3
3
|
import { getRandomBigInt } from "../utilities/0_bigint.js";
|
|
4
4
|
import { decryptMessage, encryptMessage, getMessageId } from "../utilities/1_message.js";
|
|
5
|
+
import { checkPassword } from "../utilities/1_password.js";
|
|
5
6
|
import * as types from "../tl/2_types.js";
|
|
6
7
|
import * as functions from "../tl/3_functions.js";
|
|
7
8
|
import { TLReader } from "../tl/3_tl_reader.js";
|
|
@@ -11,7 +12,16 @@ import { MessageContainer } from "../tl/6_message_container.js";
|
|
|
11
12
|
import { ClientAbstract } from "./client_abstract.js";
|
|
12
13
|
import { ClientPlain } from "./client_plain.js";
|
|
13
14
|
import { SessionMemory } from "../session/session_memory.js";
|
|
15
|
+
export const restartAuth = Symbol();
|
|
14
16
|
export class Client extends ClientAbstract {
|
|
17
|
+
/**
|
|
18
|
+
* Constructs the client.
|
|
19
|
+
*
|
|
20
|
+
* @param session The session provider to use. Defaults to in-memory session.
|
|
21
|
+
* @param apiId App's API ID from [my.telegram.org](https://my.telegram.org/apps). Defaults to 0 (unset).
|
|
22
|
+
* @param apiHash App's API hash from [my.telegram.org/apps](https://my.telegram.org/apps). Default to empty string (unset).
|
|
23
|
+
* @param params Other parameters.
|
|
24
|
+
*/
|
|
15
25
|
constructor(session = new SessionMemory(), apiId = 0, apiHash = "", params) {
|
|
16
26
|
super(params?.transportProvider);
|
|
17
27
|
Object.defineProperty(this, "session", {
|
|
@@ -111,6 +121,12 @@ export class Client extends ClientAbstract {
|
|
|
111
121
|
this.systemLangCode = params?.systemLangCode ?? DEFAULT_SYSTEM_LANG_CODE;
|
|
112
122
|
this.systemVersion = params?.systemVersion ?? DEFAULT_SYSTEM_VERSION;
|
|
113
123
|
}
|
|
124
|
+
/**
|
|
125
|
+
* Sets the DC and resets the auth key stored in the session provider
|
|
126
|
+
* if the stored DC was not the same as the `dc` parameter.
|
|
127
|
+
*
|
|
128
|
+
* @param dc The DC to change to.
|
|
129
|
+
*/
|
|
114
130
|
setDc(dc) {
|
|
115
131
|
if (this.session.dc != dc) {
|
|
116
132
|
this.session.dc = dc;
|
|
@@ -121,6 +137,11 @@ export class Client extends ClientAbstract {
|
|
|
121
137
|
}
|
|
122
138
|
super.setDc(dc);
|
|
123
139
|
}
|
|
140
|
+
/**
|
|
141
|
+
* Loads the session if `setDc` was not called, initializes and connnects
|
|
142
|
+
* a `ClientPlain` to generate auth key if there was none, and connects the client.
|
|
143
|
+
* Before establishing the connection, the session is saved.
|
|
144
|
+
*/
|
|
124
145
|
async connect() {
|
|
125
146
|
if (this.shouldLoadSession) {
|
|
126
147
|
await this.session.load();
|
|
@@ -146,6 +167,23 @@ export class Client extends ClientAbstract {
|
|
|
146
167
|
this.receiveLoop();
|
|
147
168
|
this.pingLoop();
|
|
148
169
|
}
|
|
170
|
+
/**
|
|
171
|
+
* Calls [initConnection](1) and authorizes the client with one of the following:
|
|
172
|
+
*
|
|
173
|
+
* - Bot token (`string`)
|
|
174
|
+
* - Exported authorization (`types.AuthExportedAuthorization`)
|
|
175
|
+
* - User authorization handlers (`AuthorizeUserParams`)
|
|
176
|
+
*
|
|
177
|
+
* if the current auth key doesn't throw AUTH_KEY_UNREGISTERED when calling [updates.getState](2).
|
|
178
|
+
*
|
|
179
|
+
* Notes:
|
|
180
|
+
* 1. Requires the `apiId` and `apiHash` paramters to be passed when constructing the client.
|
|
181
|
+
* 2. Reconnects the client to the appropriate DC in case of MIGRATE_X errors.
|
|
182
|
+
* 3. The parameters passed to the [initConnection][1] call can be configured with the last parameter of the constructor.
|
|
183
|
+
*
|
|
184
|
+
* [1]: https://core.telegram.org/method/initConnection
|
|
185
|
+
* [2]: https://core.telegram.org/method/updates.getState
|
|
186
|
+
*/
|
|
149
187
|
async authorize(params) {
|
|
150
188
|
if (!this.apiId) {
|
|
151
189
|
throw new Error("apiId not set");
|
|
@@ -175,12 +213,92 @@ export class Client extends ClientAbstract {
|
|
|
175
213
|
throw err;
|
|
176
214
|
}
|
|
177
215
|
}
|
|
216
|
+
let signedIn = false;
|
|
217
|
+
let phoneNumber = null;
|
|
178
218
|
try {
|
|
179
219
|
if (params instanceof types.AuthExportedAuthorization) {
|
|
180
220
|
await this.invoke(new functions.AuthImportAuthorization({ id: params.id, bytes: params.bytes }));
|
|
181
221
|
}
|
|
182
222
|
else if (typeof params == "object") {
|
|
183
|
-
|
|
223
|
+
while (true) {
|
|
224
|
+
if (signedIn) {
|
|
225
|
+
break;
|
|
226
|
+
}
|
|
227
|
+
try {
|
|
228
|
+
try {
|
|
229
|
+
phoneNumber = typeof params.phone === "string" ? params.phone : await params.phone();
|
|
230
|
+
const sentCode = await this.invoke(new functions.AuthSendCode({
|
|
231
|
+
apiId: this.apiId,
|
|
232
|
+
apiHash: this.apiHash,
|
|
233
|
+
phoneNumber,
|
|
234
|
+
settings: new types.CodeSettings(),
|
|
235
|
+
}));
|
|
236
|
+
if (sentCode instanceof types.AuthSentCode) {
|
|
237
|
+
while (true) {
|
|
238
|
+
const phoneCode = typeof params.code === "string" ? params.code : await params.code();
|
|
239
|
+
try {
|
|
240
|
+
const auth = await this.invoke(new functions.AuthSignIn({ phoneNumber, phoneCode, phoneCodeHash: sentCode.phoneCodeHash }));
|
|
241
|
+
if (auth instanceof types.AuthAuthorizationSignUpRequired) {
|
|
242
|
+
throw new Error("Sign up not supported");
|
|
243
|
+
}
|
|
244
|
+
else {
|
|
245
|
+
signedIn = true;
|
|
246
|
+
break;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
catch (err) {
|
|
250
|
+
if (err instanceof types.RPCError && err.errorMessage == "PHONE_CODE_INVALID") {
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
else {
|
|
254
|
+
throw err;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
else {
|
|
260
|
+
throw new Error(`Handling ${sentCode.constructor.name} not implemented`);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
catch (err) {
|
|
264
|
+
if (err instanceof types.RPCError && err.errorMessage == "SESSION_PASSWORD_NEEDED") {
|
|
265
|
+
while (true) {
|
|
266
|
+
const ap = await this.invoke(new functions.AccountGetPassword());
|
|
267
|
+
if (ap.currentAlgo instanceof types.PasswordKdfAlgoSHA256SHA256PBKDF2HMACSHA512iter100000SHA256ModPow) {
|
|
268
|
+
try {
|
|
269
|
+
const password = typeof params.password === "string" ? params.password : await params.password();
|
|
270
|
+
const input = await checkPassword(password, ap);
|
|
271
|
+
await this.invoke(new functions.AuthCheckPassword({ password: input }));
|
|
272
|
+
break;
|
|
273
|
+
}
|
|
274
|
+
catch (err) {
|
|
275
|
+
if (err instanceof types.RPCError && err.errorMessage == "PASSWORD_HASH_INVALID") {
|
|
276
|
+
continue;
|
|
277
|
+
}
|
|
278
|
+
else {
|
|
279
|
+
throw err;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
else {
|
|
284
|
+
throw new Error(`Handling ${ap.currentAlgo?.constructor.name} not implemented`);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
else {
|
|
289
|
+
throw err;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
catch (err) {
|
|
294
|
+
if (err == restartAuth) {
|
|
295
|
+
continue;
|
|
296
|
+
}
|
|
297
|
+
else {
|
|
298
|
+
throw err;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
184
302
|
}
|
|
185
303
|
else {
|
|
186
304
|
await this.invoke(new functions.AuthImportBotAuthorization({ apiId: this.apiId, apiHash: this.apiHash, botAuthToken: params, flags: 0 }));
|
|
@@ -195,6 +313,10 @@ export class Client extends ClientAbstract {
|
|
|
195
313
|
newDc += "-test";
|
|
196
314
|
}
|
|
197
315
|
await this.reconnect(newDc);
|
|
316
|
+
if (typeof params === "object" && phoneNumber != null) {
|
|
317
|
+
params = Object.assign({}, params);
|
|
318
|
+
params.phone = phoneNumber;
|
|
319
|
+
}
|
|
198
320
|
await this.authorize(params);
|
|
199
321
|
}
|
|
200
322
|
else {
|
|
@@ -318,6 +440,9 @@ export class Client extends ClientAbstract {
|
|
|
318
440
|
return result;
|
|
319
441
|
}
|
|
320
442
|
}
|
|
443
|
+
/**
|
|
444
|
+
* Alias for `invoke` with its second parameter being `true`.
|
|
445
|
+
*/
|
|
321
446
|
send(function_) {
|
|
322
447
|
return this.invoke(function_, true);
|
|
323
448
|
}
|