@mtkruto/node 0.0.822 → 0.0.824

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 (45) hide show
  1. package/esm/client/client.d.ts +12 -6
  2. package/esm/client/client.js +193 -59
  3. package/esm/client/client_abstract.d.ts +2 -1
  4. package/esm/client/client_abstract.js +1 -0
  5. package/esm/constants.d.ts +4 -0
  6. package/esm/constants.js +4 -0
  7. package/esm/mod.d.ts +4 -3
  8. package/esm/mod.js +4 -3
  9. package/esm/storage/storage.d.ts +23 -0
  10. package/esm/storage/storage.js +98 -0
  11. package/esm/storage/storage_local_storage.d.ts +9 -0
  12. package/esm/storage/storage_local_storage.js +36 -0
  13. package/esm/storage/storage_memory.d.ts +8 -0
  14. package/esm/storage/storage_memory.js +25 -0
  15. package/esm/storage/storage_session_storage.d.ts +9 -0
  16. package/esm/storage/storage_session_storage.js +36 -0
  17. package/package.json +1 -1
  18. package/script/client/client.d.ts +12 -6
  19. package/script/client/client.js +191 -57
  20. package/script/client/client_abstract.d.ts +2 -1
  21. package/script/client/client_abstract.js +1 -0
  22. package/script/constants.d.ts +4 -0
  23. package/script/constants.js +5 -1
  24. package/script/mod.d.ts +4 -3
  25. package/script/mod.js +4 -3
  26. package/script/storage/storage.d.ts +23 -0
  27. package/script/storage/storage.js +102 -0
  28. package/script/storage/storage_local_storage.d.ts +9 -0
  29. package/script/storage/storage_local_storage.js +40 -0
  30. package/script/storage/storage_memory.d.ts +8 -0
  31. package/script/storage/storage_memory.js +29 -0
  32. package/script/storage/storage_session_storage.d.ts +9 -0
  33. package/script/storage/storage_session_storage.js +40 -0
  34. package/esm/session/session.d.ts +0 -12
  35. package/esm/session/session.js +0 -36
  36. package/esm/session/session_local_storage.d.ts +0 -7
  37. package/esm/session/session_local_storage.js +0 -26
  38. package/esm/session/session_memory.d.ts +0 -5
  39. package/esm/session/session_memory.js +0 -5
  40. package/script/session/session.d.ts +0 -12
  41. package/script/session/session.js +0 -40
  42. package/script/session/session_local_storage.d.ts +0 -7
  43. package/script/session/session_local_storage.js +0 -30
  44. package/script/session/session_memory.d.ts +0 -5
  45. package/script/session/session_memory.js +0 -9
@@ -0,0 +1,36 @@
1
+ import { Storage } from "./storage.js";
2
+ export class StorageLocalStorage extends Storage {
3
+ constructor(prefix) {
4
+ if (typeof localStorage === "undefined") {
5
+ throw new Error("Unavailable in current environment");
6
+ }
7
+ if (prefix.length <= 0) {
8
+ throw new Error("Empty prefix");
9
+ }
10
+ else if (!/^[0-9a-zA-Z]+$/.test(prefix)) {
11
+ throw new Error("Unallowed prefix");
12
+ }
13
+ super();
14
+ Object.defineProperty(this, "prefix", {
15
+ enumerable: true,
16
+ configurable: true,
17
+ writable: true,
18
+ value: prefix
19
+ });
20
+ }
21
+ init() {
22
+ }
23
+ get(key) {
24
+ key = this.prefix + key;
25
+ return localStorage.getItem(key);
26
+ }
27
+ set(key, value) {
28
+ key = this.prefix + key;
29
+ if (value != null) {
30
+ localStorage.setItem(key, value);
31
+ }
32
+ else {
33
+ localStorage.removeItem(key);
34
+ }
35
+ }
36
+ }
@@ -0,0 +1,8 @@
1
+ import { MaybePromise } from "../types.js";
2
+ import { Storage } from "./storage.js";
3
+ export declare class StorageMemory extends Storage implements Storage {
4
+ protected map: Map<string, string>;
5
+ init(): void;
6
+ get(key: string): string | null;
7
+ set(key: string, value: string | null): MaybePromise<void>;
8
+ }
@@ -0,0 +1,25 @@
1
+ import { Storage } from "./storage.js";
2
+ export class StorageMemory extends Storage {
3
+ constructor() {
4
+ super(...arguments);
5
+ Object.defineProperty(this, "map", {
6
+ enumerable: true,
7
+ configurable: true,
8
+ writable: true,
9
+ value: new Map()
10
+ });
11
+ }
12
+ init() {
13
+ }
14
+ get(key) {
15
+ return this.map.get(key) ?? null;
16
+ }
17
+ set(key, value) {
18
+ if (value != null) {
19
+ this.map.set(key, value);
20
+ }
21
+ else {
22
+ this.map.delete(key);
23
+ }
24
+ }
25
+ }
@@ -0,0 +1,9 @@
1
+ import { MaybePromise } from "../types.js";
2
+ import { Storage } from "./storage.js";
3
+ export declare class StorageSessionStorage extends Storage implements Storage {
4
+ private readonly prefix;
5
+ constructor(prefix: string);
6
+ init(): void;
7
+ get(key: string): string | null;
8
+ set(key: string, value: string | null): MaybePromise<void>;
9
+ }
@@ -0,0 +1,36 @@
1
+ import { Storage } from "./storage.js";
2
+ export class StorageSessionStorage extends Storage {
3
+ constructor(prefix) {
4
+ if (typeof sessionStorage === "undefined") {
5
+ throw new Error("Unavailable in current environment");
6
+ }
7
+ if (prefix.length <= 0) {
8
+ throw new Error("Empty prefix");
9
+ }
10
+ else if (!/^[0-9a-zA-Z]+$/.test(prefix)) {
11
+ throw new Error("Unallowed prefix");
12
+ }
13
+ super();
14
+ Object.defineProperty(this, "prefix", {
15
+ enumerable: true,
16
+ configurable: true,
17
+ writable: true,
18
+ value: prefix
19
+ });
20
+ }
21
+ init() {
22
+ }
23
+ get(key) {
24
+ key = this.prefix + key;
25
+ return sessionStorage.getItem(key);
26
+ }
27
+ set(key, value) {
28
+ key = this.prefix + key;
29
+ if (value != null) {
30
+ sessionStorage.setItem(key, value);
31
+ }
32
+ else {
33
+ sessionStorage.removeItem(key);
34
+ }
35
+ }
36
+ }
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "module": "./esm/mod.js",
3
3
  "main": "./script/mod.js",
4
4
  "name": "@mtkruto/node",
5
- "version": "0.0.822",
5
+ "version": "0.0.824",
6
6
  "description": "MTKruto for Node.js",
7
7
  "author": "Roj <rojvv@icloud.com>",
8
8
  "license": "LGPL-3.0-or-later",
@@ -2,7 +2,7 @@ import { MaybePromise } from "../types.js";
2
2
  import * as types from "../tl/2_types.js";
3
3
  import * as functions from "../tl/3_functions.js";
4
4
  import { ClientAbstract } from "./client_abstract.js";
5
- import { Session } from "../session/session.js";
5
+ import { Storage } from "../storage/storage.js";
6
6
  import { DC, TransportProvider } from "../transport/transport_provider.js";
7
7
  export declare const restartAuth: unique symbol;
8
8
  export interface AuthorizeUserParams<S = string> {
@@ -42,9 +42,10 @@ export interface ClientParams {
42
42
  systemVersion?: string;
43
43
  }
44
44
  export declare class Client extends ClientAbstract {
45
- readonly session: Session;
45
+ readonly storage: Storage;
46
46
  readonly apiId: number;
47
47
  readonly apiHash: string;
48
+ private auth;
48
49
  private sessionId;
49
50
  private state;
50
51
  private promises;
@@ -59,20 +60,21 @@ export declare class Client extends ClientAbstract {
59
60
  /**
60
61
  * Constructs the client.
61
62
  *
62
- * @param session The session provider to use. Defaults to in-memory session.
63
+ * @param storage The storage provider to use. Defaults to memory storage.
63
64
  * @param apiId App's API ID from [my.telegram.org](https://my.telegram.org/apps). Defaults to 0 (unset).
64
65
  * @param apiHash App's API hash from [my.telegram.org/apps](https://my.telegram.org/apps). Default to empty string (unset).
65
66
  * @param params Other parameters.
66
67
  */
67
- constructor(session?: Session, apiId?: number, apiHash?: string, params?: ClientParams);
68
- private shouldLoadSession;
68
+ constructor(storage?: Storage, apiId?: number, apiHash?: string, params?: ClientParams);
69
+ private storageInited;
69
70
  /**
70
71
  * Sets the DC and resets the auth key stored in the session provider
71
72
  * if the stored DC was not the same as the `dc` parameter.
72
73
  *
73
74
  * @param dc The DC to change to.
74
75
  */
75
- setDc(dc: DC): void;
76
+ setDc(dc: DC): Promise<void>;
77
+ private setAuth;
76
78
  /**
77
79
  * Loads the session if `setDc` was not called, initializes and connnects
78
80
  * a `ClientPlain` to generate auth key if there was none, and connects the client.
@@ -111,4 +113,8 @@ export declare class Client extends ClientAbstract {
111
113
  * Alias for `invoke` with its second parameter being `true`.
112
114
  */
113
115
  send<T extends (functions.Function<unknown> | types.Type) = functions.Function<unknown>>(function_: T): Promise<void>;
116
+ private processChats;
117
+ private processUsers;
118
+ private processUpdates;
119
+ getInputPeer(id: string | number): Promise<types.InputPeerChat | types.InputPeerUser | types.InputPeerChannel>;
114
120
  }
@@ -29,6 +29,7 @@ const constants_js_1 = require("../constants.js");
29
29
  const _0_bigint_js_1 = require("../utilities/0_bigint.js");
30
30
  const _1_message_js_1 = require("../utilities/1_message.js");
31
31
  const _1_password_js_1 = require("../utilities/1_password.js");
32
+ const _1_tl_object_js_1 = require("../tl/1_tl_object.js");
32
33
  const types = __importStar(require("../tl/2_types.js"));
33
34
  const functions = __importStar(require("../tl/3_functions.js"));
34
35
  const _3_tl_reader_js_1 = require("../tl/3_tl_reader.js");
@@ -37,24 +38,25 @@ const _5_message_js_1 = require("../tl/5_message.js");
37
38
  const _6_message_container_js_1 = require("../tl/6_message_container.js");
38
39
  const client_abstract_js_1 = require("./client_abstract.js");
39
40
  const client_plain_js_1 = require("./client_plain.js");
40
- const session_memory_js_1 = require("../session/session_memory.js");
41
+ const storage_memory_js_1 = require("../storage/storage_memory.js");
42
+ const _0_hash_js_1 = require("../utilities/0_hash.js");
41
43
  exports.restartAuth = Symbol();
42
44
  class Client extends client_abstract_js_1.ClientAbstract {
43
45
  /**
44
46
  * Constructs the client.
45
47
  *
46
- * @param session The session provider to use. Defaults to in-memory session.
48
+ * @param storage The storage provider to use. Defaults to memory storage.
47
49
  * @param apiId App's API ID from [my.telegram.org](https://my.telegram.org/apps). Defaults to 0 (unset).
48
50
  * @param apiHash App's API hash from [my.telegram.org/apps](https://my.telegram.org/apps). Default to empty string (unset).
49
51
  * @param params Other parameters.
50
52
  */
51
- constructor(session = new session_memory_js_1.SessionMemory(), apiId = 0, apiHash = "", params) {
53
+ constructor(storage = new storage_memory_js_1.StorageMemory(), apiId = 0, apiHash = "", params) {
52
54
  super(params?.transportProvider);
53
- Object.defineProperty(this, "session", {
55
+ Object.defineProperty(this, "storage", {
54
56
  enumerable: true,
55
57
  configurable: true,
56
58
  writable: true,
57
- value: session
59
+ value: storage
58
60
  });
59
61
  Object.defineProperty(this, "apiId", {
60
62
  enumerable: true,
@@ -68,6 +70,12 @@ class Client extends client_abstract_js_1.ClientAbstract {
68
70
  writable: true,
69
71
  value: apiHash
70
72
  });
73
+ Object.defineProperty(this, "auth", {
74
+ enumerable: true,
75
+ configurable: true,
76
+ writable: true,
77
+ value: null
78
+ });
71
79
  Object.defineProperty(this, "sessionId", {
72
80
  enumerable: true,
73
81
  configurable: true,
@@ -134,11 +142,11 @@ class Client extends client_abstract_js_1.ClientAbstract {
134
142
  writable: true,
135
143
  value: void 0
136
144
  });
137
- Object.defineProperty(this, "shouldLoadSession", {
145
+ Object.defineProperty(this, "storageInited", {
138
146
  enumerable: true,
139
147
  configurable: true,
140
148
  writable: true,
141
- value: true
149
+ value: false
142
150
  });
143
151
  this.appVersion = params?.appVersion ?? constants_js_1.DEFAULT_APP_VERSION;
144
152
  this.deviceModel = params?.deviceModel ?? constants_js_1.DEFAULT_DEVICE_MODEL;
@@ -153,42 +161,57 @@ class Client extends client_abstract_js_1.ClientAbstract {
153
161
  *
154
162
  * @param dc The DC to change to.
155
163
  */
156
- setDc(dc) {
157
- if (this.session.dc != dc) {
158
- this.session.dc = dc;
159
- this.session.authKey = null;
160
- if (this.shouldLoadSession) {
161
- this.shouldLoadSession = false;
162
- }
164
+ async setDc(dc) {
165
+ if (!this.storageInited) {
166
+ await this.storage.init();
167
+ this.storageInited = true;
168
+ }
169
+ if (await this.storage.getDc() != dc) {
170
+ await this.storage.setDc(dc);
171
+ await this.storage.setAuthKey(null);
163
172
  }
164
173
  super.setDc(dc);
165
174
  }
175
+ async setAuth(key) {
176
+ const hash = await (0, _0_hash_js_1.sha1)(key);
177
+ const id = (0, _0_bigint_js_1.bigIntFromBuffer)(hash.slice(-8), true, false);
178
+ this.auth = { key, id };
179
+ }
166
180
  /**
167
181
  * Loads the session if `setDc` was not called, initializes and connnects
168
182
  * a `ClientPlain` to generate auth key if there was none, and connects the client.
169
183
  * Before establishing the connection, the session is saved.
170
184
  */
171
185
  async connect() {
172
- if (this.shouldLoadSession) {
173
- await this.session.load();
174
- this.shouldLoadSession = false;
186
+ if (!this.storageInited) {
187
+ await this.storage.init();
188
+ this.storageInited = true;
175
189
  }
176
- if (this.session.authKey == null) {
190
+ const authKey = await this.storage.getAuthKey();
191
+ if (authKey == null) {
177
192
  const plain = new client_plain_js_1.ClientPlain(this.transportProvider);
178
- if (this.session.dc != null) {
179
- plain.setDc(this.session.dc);
193
+ const dc = await this.storage.getDc();
194
+ if (dc != null) {
195
+ plain.setDc(dc);
180
196
  }
181
197
  await plain.connect();
182
198
  const { authKey, salt } = await plain.createAuthKey();
183
199
  await plain.disconnect();
200
+ await this.storage.setAuthKey(authKey);
201
+ await this.setAuth(authKey);
184
202
  this.state.salt = salt;
185
- this.session.authKey = authKey;
186
203
  }
187
- if (this.session.dc != null) {
188
- this.setDc(this.session.dc);
204
+ else {
205
+ await this.setAuth(authKey);
206
+ }
207
+ const dc = await this.storage.getDc();
208
+ if (dc != null) {
209
+ await this.setDc(dc);
189
210
  }
190
- await this.session.save();
191
211
  await super.connect();
212
+ if (dc == null) {
213
+ await this.storage.setDc(constants_js_1.DEFAULT_INITIAL_DC);
214
+ }
192
215
  // logger().debug("Client connected");
193
216
  this.receiveLoop();
194
217
  this.pingLoop();
@@ -230,6 +253,36 @@ class Client extends client_abstract_js_1.ClientAbstract {
230
253
  systemLangCode: this.systemLangCode,
231
254
  systemVersion: this.systemVersion,
232
255
  }));
256
+ const handlePassword = async (err) => {
257
+ params = params;
258
+ if (err instanceof types.RPCError && err.errorMessage == "SESSION_PASSWORD_NEEDED") {
259
+ while (true) {
260
+ const ap = await this.invoke(new functions.AccountGetPassword());
261
+ if (ap.currentAlgo instanceof types.PasswordKdfAlgoSHA256SHA256PBKDF2HMACSHA512iter100000SHA256ModPow) {
262
+ try {
263
+ const password = typeof params.password === "string" ? params.password : await params.password();
264
+ const input = await (0, _1_password_js_1.checkPassword)(password, ap);
265
+ await this.invoke(new functions.AuthCheckPassword({ password: input }));
266
+ break;
267
+ }
268
+ catch (err) {
269
+ if (err instanceof types.RPCError && err.errorMessage == "PASSWORD_HASH_INVALID") {
270
+ continue;
271
+ }
272
+ else {
273
+ throw err;
274
+ }
275
+ }
276
+ }
277
+ else {
278
+ throw new Error(`Handling ${ap.currentAlgo?.constructor.name} not implemented`);
279
+ }
280
+ }
281
+ }
282
+ else {
283
+ throw err;
284
+ }
285
+ };
233
286
  try {
234
287
  await this.invoke(new functions.UpdatesGetState());
235
288
  return;
@@ -287,33 +340,7 @@ class Client extends client_abstract_js_1.ClientAbstract {
287
340
  }
288
341
  }
289
342
  catch (err) {
290
- if (err instanceof types.RPCError && err.errorMessage == "SESSION_PASSWORD_NEEDED") {
291
- while (true) {
292
- const ap = await this.invoke(new functions.AccountGetPassword());
293
- if (ap.currentAlgo instanceof types.PasswordKdfAlgoSHA256SHA256PBKDF2HMACSHA512iter100000SHA256ModPow) {
294
- try {
295
- const password = typeof params.password === "string" ? params.password : await params.password();
296
- const input = await (0, _1_password_js_1.checkPassword)(password, ap);
297
- await this.invoke(new functions.AuthCheckPassword({ password: input }));
298
- break;
299
- }
300
- catch (err) {
301
- if (err instanceof types.RPCError && err.errorMessage == "PASSWORD_HASH_INVALID") {
302
- continue;
303
- }
304
- else {
305
- throw err;
306
- }
307
- }
308
- }
309
- else {
310
- throw new Error(`Handling ${ap.currentAlgo?.constructor.name} not implemented`);
311
- }
312
- }
313
- }
314
- else {
315
- throw err;
316
- }
343
+ await handlePassword(err);
317
344
  }
318
345
  }
319
346
  catch (err) {
@@ -346,7 +373,7 @@ class Client extends client_abstract_js_1.ClientAbstract {
346
373
  await this.authorize(params);
347
374
  }
348
375
  else {
349
- throw err;
376
+ await handlePassword(err);
350
377
  }
351
378
  }
352
379
  else {
@@ -355,7 +382,7 @@ class Client extends client_abstract_js_1.ClientAbstract {
355
382
  }
356
383
  }
357
384
  async receiveLoop() {
358
- if (!this.session.authKey) {
385
+ if (!this.auth) {
359
386
  throw new Error("Not connected");
360
387
  }
361
388
  while (this.connected) {
@@ -377,7 +404,7 @@ class Client extends client_abstract_js_1.ClientAbstract {
377
404
  }
378
405
  let decrypted;
379
406
  try {
380
- decrypted = await (0, _1_message_js_1.decryptMessage)(buffer, this.session.authKey, (await this.session.authKeyId), this.sessionId);
407
+ decrypted = await (0, _1_message_js_1.decryptMessage)(buffer, this.auth.key, this.auth.id, this.sessionId);
381
408
  }
382
409
  catch (_err) {
383
410
  // logger().error(`Failed to decrypt message: ${err}`);
@@ -391,7 +418,7 @@ class Client extends client_abstract_js_1.ClientAbstract {
391
418
  }
392
419
  // logger().debug(`Received ${body.constructor.name}`);
393
420
  if (body instanceof types.Updates) {
394
- this.updatesHandler?.(this, body);
421
+ this.processUpdates(body);
395
422
  }
396
423
  else if (message.body instanceof _4_rpc_result_js_1.RPCResult) {
397
424
  let result = message.body.result;
@@ -442,7 +469,7 @@ class Client extends client_abstract_js_1.ClientAbstract {
442
469
  }
443
470
  }
444
471
  async invoke(function_, noWait) {
445
- if (!this.session.authKey) {
472
+ if (!this.auth) {
446
473
  throw new Error("Not connected");
447
474
  }
448
475
  let seqNo = this.state.seqNo * 2;
@@ -451,7 +478,7 @@ class Client extends client_abstract_js_1.ClientAbstract {
451
478
  this.state.seqNo++;
452
479
  }
453
480
  const message = new _5_message_js_1.Message((0, _1_message_js_1.getMessageId)(), seqNo, function_);
454
- await this.transport.send(await (0, _1_message_js_1.encryptMessage)(message, this.session.authKey, (await this.session.authKeyId), this.state.salt, this.sessionId));
481
+ await this.transport.send(await (0, _1_message_js_1.encryptMessage)(message, this.auth.key, this.auth.id, this.state.salt, this.sessionId));
455
482
  // logger().debug(`Invoked ${function_.constructor.name}`);
456
483
  if (noWait) {
457
484
  return;
@@ -472,5 +499,112 @@ class Client extends client_abstract_js_1.ClientAbstract {
472
499
  send(function_) {
473
500
  return this.invoke(function_, true);
474
501
  }
502
+ async processChats(chats) {
503
+ for (const chat of chats) {
504
+ if (chat instanceof types.Channel && chat.accessHash) {
505
+ await this.storage.setChannelAccessHash(chat.id, chat.accessHash);
506
+ if (chat.username) {
507
+ await this.storage.updateUsernames("channel", chat.id, [chat.username]);
508
+ }
509
+ if (chat.usernames) {
510
+ await this.storage.updateUsernames("channel", chat.id, chat.usernames.map((v) => v[_1_tl_object_js_1.as](types.Username)).map((v) => v.username));
511
+ }
512
+ }
513
+ }
514
+ }
515
+ async processUsers(users) {
516
+ for (const user of users) {
517
+ if (user instanceof types.User && user.accessHash) {
518
+ await this.storage.setUserAccessHash(user.id, user.accessHash);
519
+ if (user.username) {
520
+ await this.storage.updateUsernames("user", user.id, [user.username]);
521
+ }
522
+ if (user.usernames) {
523
+ await this.storage.updateUsernames("user", user.id, user.usernames.map((v) => v[_1_tl_object_js_1.as](types.Username)).map((v) => v.username));
524
+ }
525
+ }
526
+ }
527
+ }
528
+ async processUpdates(updates) {
529
+ try {
530
+ await this.processChats(updates.chats);
531
+ await this.processUsers(updates.users);
532
+ for (const update of updates.updates) {
533
+ if (update instanceof types.UpdateUserName) {
534
+ await this.storage.updateUsernames("user", update.userId, update.usernames.map((v) => v[_1_tl_object_js_1.as](types.Username)).map((v) => v.username));
535
+ }
536
+ }
537
+ await this.updatesHandler?.(this, updates);
538
+ }
539
+ catch (err) {
540
+ console.error("Error processing updates:", err);
541
+ }
542
+ }
543
+ async getInputPeer(id) {
544
+ if (typeof id === "string") {
545
+ if (!id.startsWith("@")) {
546
+ throw new Error("Expected username to start with @");
547
+ }
548
+ else {
549
+ id = id.slice(1);
550
+ if (!id) {
551
+ throw new Error("Empty username");
552
+ }
553
+ let userId = 0n;
554
+ let channelId = 0n;
555
+ const maybeUsername = await this.storage.getUsername(id);
556
+ if (maybeUsername != null && Date.now() - maybeUsername[2].getTime() < constants_js_1.USERNAME_TTL) {
557
+ const [type, id] = maybeUsername;
558
+ if (type == "user") {
559
+ userId = id;
560
+ }
561
+ else {
562
+ channelId = id;
563
+ }
564
+ }
565
+ else {
566
+ const resolved = await this.invoke(new functions.ContactsResolveUsername({ username: id }));
567
+ await this.processChats(resolved.chats);
568
+ await this.processUsers(resolved.users);
569
+ if (resolved.peer instanceof types.PeerUser) {
570
+ userId = resolved.peer.userId;
571
+ }
572
+ else if (resolved.peer instanceof types.PeerChannel) {
573
+ channelId = resolved.peer.channelId;
574
+ }
575
+ else {
576
+ throw new Error("Unreachable");
577
+ }
578
+ }
579
+ if (userId) {
580
+ const accessHash = await this.storage.getUserAccessHash(userId);
581
+ return new types.InputPeerUser({ userId, accessHash: accessHash ?? 0n });
582
+ }
583
+ else if (channelId) {
584
+ const accessHash = await this.storage.getChannelAccessHash(channelId);
585
+ return new types.InputPeerChannel({ channelId, accessHash: accessHash ?? 0n });
586
+ }
587
+ else {
588
+ throw new Error("Unreachable");
589
+ }
590
+ }
591
+ }
592
+ else if (id > 0) {
593
+ const id_ = BigInt(id);
594
+ const accessHash = await this.storage.getUserAccessHash(id_);
595
+ return new types.InputPeerUser({ userId: id_, accessHash: accessHash ?? 0n });
596
+ }
597
+ else if (-constants_js_1.MAX_CHAT_ID <= id) {
598
+ return new types.InputPeerChat({ chatId: BigInt(Math.abs(id)) });
599
+ }
600
+ else if (constants_js_1.ZERO_CHANNEL_ID - constants_js_1.MAX_CHANNEL_ID <= id && id != constants_js_1.ZERO_CHANNEL_ID) {
601
+ const id_ = BigInt(Math.abs(id - constants_js_1.ZERO_CHANNEL_ID));
602
+ const accessHash = await this.storage.getChannelAccessHash(id_);
603
+ return new types.InputPeerChannel({ channelId: id_, accessHash: accessHash ?? 0n });
604
+ }
605
+ else {
606
+ throw new Error("ID format unknown or not implemented");
607
+ }
608
+ }
475
609
  }
476
610
  exports.Client = Client;
@@ -1,6 +1,7 @@
1
1
  import { Connection } from "../connection/connection.js";
2
2
  import { Transport } from "../transport/transport.js";
3
3
  import { DC } from "../transport/transport_provider.js";
4
+ import { MaybePromise } from "../types.js";
4
5
  export declare abstract class ClientAbstract {
5
6
  protected transportProvider: import("../transport/transport_provider.js").TransportProvider;
6
7
  protected connection: Connection;
@@ -9,7 +10,7 @@ export declare abstract class ClientAbstract {
9
10
  protected connected: boolean;
10
11
  constructor(transportProvider?: import("../transport/transport_provider.js").TransportProvider);
11
12
  get dcId(): number;
12
- setDc(dc: DC): void;
13
+ setDc(dc: DC): MaybePromise<void>;
13
14
  connect(): Promise<void>;
14
15
  reconnect(dc?: DC): Promise<void>;
15
16
  disconnect(): Promise<void>;
@@ -44,6 +44,7 @@ class ClientAbstract {
44
44
  get dcId() {
45
45
  return this._dcId;
46
46
  }
47
+ // MaybePromise since `Client` has to deal with `Storage.set()`
47
48
  setDc(dc) {
48
49
  const { connection, transport, dcId } = this.transportProvider({ dc, cdn: false });
49
50
  this.connection = connection;
@@ -10,3 +10,7 @@ export declare const DEFAULT_LANG_CODE = "en";
10
10
  export declare const DEFAULT_LANG_PACK = "";
11
11
  export declare const DEFAULT_SYSTEM_LANG_CODE = "en";
12
12
  export declare const DEFAULT_SYSTEM_VERSION = "1.0";
13
+ export declare const USERNAME_TTL = 86400;
14
+ export declare const MAX_CHAT_ID = 999999999999;
15
+ export declare const MAX_CHANNEL_ID = 997852516352;
16
+ export declare const ZERO_CHANNEL_ID = -1000000000000;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DEFAULT_SYSTEM_VERSION = exports.DEFAULT_SYSTEM_LANG_CODE = exports.DEFAULT_LANG_PACK = exports.DEFAULT_LANG_CODE = exports.DEFAULT_DEVICE_MODEL = exports.DEFAULT_APP_VERSION = exports.LAYER = exports.DEFAULT_INITIAL_DC = exports.VECTOR_CONSTRUCTOR = exports.publicKeys = exports.ackThreshold = void 0;
3
+ exports.ZERO_CHANNEL_ID = exports.MAX_CHANNEL_ID = exports.MAX_CHAT_ID = exports.USERNAME_TTL = exports.DEFAULT_SYSTEM_VERSION = exports.DEFAULT_SYSTEM_LANG_CODE = exports.DEFAULT_LANG_PACK = exports.DEFAULT_LANG_CODE = exports.DEFAULT_DEVICE_MODEL = exports.DEFAULT_APP_VERSION = exports.LAYER = exports.DEFAULT_INITIAL_DC = exports.VECTOR_CONSTRUCTOR = exports.publicKeys = exports.ackThreshold = void 0;
4
4
  exports.ackThreshold = 10;
5
5
  exports.publicKeys = new Map([
6
6
  [
@@ -71,3 +71,7 @@ exports.DEFAULT_LANG_CODE = "en";
71
71
  exports.DEFAULT_LANG_PACK = "";
72
72
  exports.DEFAULT_SYSTEM_LANG_CODE = "en";
73
73
  exports.DEFAULT_SYSTEM_VERSION = "1.0";
74
+ exports.USERNAME_TTL = 86400;
75
+ exports.MAX_CHAT_ID = 999999999999;
76
+ exports.MAX_CHANNEL_ID = 997852516352;
77
+ exports.ZERO_CHANNEL_ID = -1000000000000;
package/script/mod.d.ts CHANGED
@@ -12,9 +12,10 @@ export * from "./tl/5_message.js";
12
12
  export * from "./tl/6_message_container.js";
13
13
  export * from "./client/client_plain.js";
14
14
  export * from "./client/client.js";
15
- export * from "./session/session.js";
16
- export * from "./session/session_memory.js";
17
- export * from "./session/session_local_storage.js";
15
+ export * from "./storage/storage.js";
16
+ export * from "./storage/storage_memory.js";
17
+ export * from "./storage/storage_local_storage.js";
18
+ export * from "./storage/storage_session_storage.js";
18
19
  export * from "./transport/transport_abridged.js";
19
20
  export * from "./transport/transport_intermediate.js";
20
21
  export * from "./transport/transport.js";
package/script/mod.js CHANGED
@@ -39,9 +39,10 @@ __exportStar(require("./tl/5_message.js"), exports);
39
39
  __exportStar(require("./tl/6_message_container.js"), exports);
40
40
  __exportStar(require("./client/client_plain.js"), exports);
41
41
  __exportStar(require("./client/client.js"), exports);
42
- __exportStar(require("./session/session.js"), exports);
43
- __exportStar(require("./session/session_memory.js"), exports);
44
- __exportStar(require("./session/session_local_storage.js"), exports);
42
+ __exportStar(require("./storage/storage.js"), exports);
43
+ __exportStar(require("./storage/storage_memory.js"), exports);
44
+ __exportStar(require("./storage/storage_local_storage.js"), exports);
45
+ __exportStar(require("./storage/storage_session_storage.js"), exports);
45
46
  __exportStar(require("./transport/transport_abridged.js"), exports);
46
47
  __exportStar(require("./transport/transport_intermediate.js"), exports);
47
48
  __exportStar(require("./transport/transport.js"), exports);