@mtkruto/node 0.0.999 → 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 (77) hide show
  1. package/esm/0_deps.d.ts +1 -1
  2. package/esm/0_deps.js +1 -1
  3. package/esm/3_errors.d.ts +1531 -0
  4. package/esm/3_errors.js +1531 -0
  5. package/esm/4_constants.d.ts +1 -1
  6. package/esm/4_constants.js +1 -1
  7. package/esm/4_errors.d.ts +25 -0
  8. package/esm/4_errors.js +64 -0
  9. package/esm/client/2_client_plain.js +1 -1
  10. package/esm/client/4_client.d.ts +2 -1
  11. package/esm/client/4_client.js +24 -41
  12. package/esm/deps/deno.land/x/{tgcrypto@0.1.3 → tgcrypto@0.2.1}/mod.d.ts +2 -2
  13. package/esm/deps/deno.land/x/{tgcrypto@0.1.3 → tgcrypto@0.2.1}/mod.js +19 -6
  14. package/esm/deps/deno.land/x/tgcrypto@0.2.1/tgcrypto.d.ts +2 -0
  15. package/esm/deps/deno.land/x/tgcrypto@0.2.1/tgcrypto.js +991 -0
  16. package/esm/mod.d.ts +3 -2
  17. package/esm/mod.js +3 -2
  18. package/esm/tl/2_types.d.ts +1 -1
  19. package/esm/tl/2_types.js +1 -2
  20. package/esm/tl/3_functions.d.ts +1 -1
  21. package/esm/tl/3_functions.js +1 -2
  22. package/esm/types/0__file_id.js +0 -1
  23. package/esm/types/1_bot_command_scope.d.ts +1 -1
  24. package/esm/types/1_bot_command_scope.js +1 -1
  25. package/esm/types/1_chat.d.ts +1 -1
  26. package/esm/types/1_chat.js +1 -1
  27. package/esm/types/1_sticker.d.ts +1 -1
  28. package/esm/types/1_sticker.js +1 -1
  29. package/esm/types/1_user.d.ts +1 -1
  30. package/esm/types/1_user.js +1 -1
  31. package/esm/types/2_inline_keyboard_button.d.ts +1 -1
  32. package/esm/types/3_message.d.ts +12 -12
  33. package/esm/types/3_message.js +12 -12
  34. package/esm/types/5_inline_query_result.d.ts +1 -1
  35. package/esm/types/5_inline_query_result.js +1 -1
  36. package/esm/utilities/0_crypto.js +6 -4
  37. package/package.json +1 -1
  38. package/script/0_deps.d.ts +1 -1
  39. package/script/0_deps.js +1 -1
  40. package/script/3_errors.d.ts +1531 -0
  41. package/script/3_errors.js +2053 -0
  42. package/script/4_constants.d.ts +1 -1
  43. package/script/4_constants.js +1 -1
  44. package/script/4_errors.d.ts +25 -0
  45. package/script/4_errors.js +88 -0
  46. package/script/client/2_client_plain.js +1 -1
  47. package/script/client/4_client.d.ts +2 -1
  48. package/script/client/4_client.js +24 -41
  49. package/script/deps/deno.land/x/{tgcrypto@0.1.3 → tgcrypto@0.2.1}/mod.d.ts +2 -2
  50. package/script/deps/deno.land/x/{tgcrypto@0.1.3 → tgcrypto@0.2.1}/mod.js +19 -6
  51. package/script/deps/deno.land/x/tgcrypto@0.2.1/tgcrypto.d.ts +2 -0
  52. package/script/deps/deno.land/x/tgcrypto@0.2.1/tgcrypto.js +993 -0
  53. package/script/mod.d.ts +3 -2
  54. package/script/mod.js +16 -3
  55. package/script/tl/2_types.d.ts +1 -1
  56. package/script/tl/2_types.js +0 -1
  57. package/script/tl/3_functions.d.ts +1 -1
  58. package/script/tl/3_functions.js +0 -1
  59. package/script/types/0__file_id.js +0 -1
  60. package/script/types/1_bot_command_scope.d.ts +1 -1
  61. package/script/types/1_bot_command_scope.js +1 -1
  62. package/script/types/1_chat.d.ts +1 -1
  63. package/script/types/1_chat.js +1 -1
  64. package/script/types/1_sticker.d.ts +1 -1
  65. package/script/types/1_sticker.js +1 -1
  66. package/script/types/1_user.d.ts +1 -1
  67. package/script/types/1_user.js +1 -1
  68. package/script/types/2_inline_keyboard_button.d.ts +1 -1
  69. package/script/types/3_message.d.ts +12 -12
  70. package/script/types/3_message.js +12 -12
  71. package/script/types/5_inline_query_result.d.ts +1 -1
  72. package/script/types/5_inline_query_result.js +1 -1
  73. package/script/utilities/0_crypto.js +6 -4
  74. package/esm/deps/deno.land/x/tgcrypto@0.1.3/tgcrypto.d.ts +0 -2
  75. package/esm/deps/deno.land/x/tgcrypto@0.1.3/tgcrypto.js +0 -1078
  76. package/script/deps/deno.land/x/tgcrypto@0.1.3/tgcrypto.d.ts +0 -2
  77. package/script/deps/deno.land/x/tgcrypto@0.1.3/tgcrypto.js +0 -1080
@@ -5,7 +5,7 @@ export declare const PUBLIC_KEYS: PublicKeys;
5
5
  export declare const VECTOR_CONSTRUCTOR = 481674261;
6
6
  export declare const INITIAL_DC: DC;
7
7
  export declare const LAYER = 161;
8
- export declare const APP_VERSION = "MTKruto 0.0.999";
8
+ export declare const APP_VERSION = "MTKruto 0.1.0";
9
9
  export declare const DEVICE_MODEL: string;
10
10
  export declare const LANG_CODE: string;
11
11
  export declare const LANG_PACK = "";
@@ -54,7 +54,7 @@ export const PUBLIC_KEYS = Object.freeze([
54
54
  export const VECTOR_CONSTRUCTOR = 0x1CB5C415;
55
55
  export const INITIAL_DC = "2-test";
56
56
  export const LAYER = 161;
57
- export const APP_VERSION = "MTKruto 0.0.999";
57
+ export const APP_VERSION = "MTKruto 0.1.0";
58
58
  // @ts-ignore: lib
59
59
  export const DEVICE_MODEL = typeof dntShim.Deno === "undefined" ? typeof navigator === "undefined" ? typeof process === "undefined" ? "Unknown" : process.platform + "-" + process.arch : navigator.userAgent.split(" ")[0] : dntShim.Deno.build.os + "-" + dntShim.Deno.build.arch;
60
60
  export const LANG_CODE = typeof navigator === "undefined" ? "en" : navigator.language.split("-")[0];
@@ -0,0 +1,25 @@
1
+ import { types } from "./2_tl.js";
2
+ export * from "./3_errors.js";
3
+ export declare class FloodWait extends types.RPCError {
4
+ seconds: number;
5
+ constructor(params: {
6
+ errorCode: number;
7
+ errorMessage: string;
8
+ });
9
+ }
10
+ export declare class Migrate extends types.RPCError {
11
+ dc: number;
12
+ constructor(params: {
13
+ errorCode: number;
14
+ errorMessage: string;
15
+ });
16
+ }
17
+ export declare class UserMigrate extends Migrate {
18
+ }
19
+ export declare class PhoneMigrate extends Migrate {
20
+ }
21
+ export declare class FileMigrate extends Migrate {
22
+ }
23
+ export declare class StatsMigrate extends Migrate {
24
+ }
25
+ export declare function upgradeInstance(error: types.RPCError): types.RPCError;
@@ -0,0 +1,64 @@
1
+ import { UNREACHABLE } from "./1_utilities.js";
2
+ import { map } from "./3_errors.js";
3
+ import { types } from "./2_tl.js";
4
+ export * from "./3_errors.js";
5
+ export class FloodWait extends types.RPCError {
6
+ constructor(params) {
7
+ super(params);
8
+ Object.defineProperty(this, "seconds", {
9
+ enumerable: true,
10
+ configurable: true,
11
+ writable: true,
12
+ value: void 0
13
+ });
14
+ const p = params.errorMessage.split("_");
15
+ this.seconds = Number(p[p.length - 1]);
16
+ if (isNaN(this.seconds)) {
17
+ UNREACHABLE();
18
+ }
19
+ }
20
+ }
21
+ export class Migrate extends types.RPCError {
22
+ constructor(params) {
23
+ super(params);
24
+ Object.defineProperty(this, "dc", {
25
+ enumerable: true,
26
+ configurable: true,
27
+ writable: true,
28
+ value: void 0
29
+ });
30
+ const p = params.errorMessage.split("_");
31
+ this.dc = Number(p[p.length - 1]);
32
+ if (isNaN(this.dc)) {
33
+ UNREACHABLE();
34
+ }
35
+ }
36
+ }
37
+ export class UserMigrate extends Migrate {
38
+ }
39
+ export class PhoneMigrate extends Migrate {
40
+ }
41
+ export class FileMigrate extends Migrate {
42
+ }
43
+ export class StatsMigrate extends Migrate {
44
+ }
45
+ const prefixMap = {
46
+ "FILE_MIGRATE_": FileMigrate,
47
+ "PHONE_MIGRATE_": PhoneMigrate,
48
+ "USER_MIGRATE_": UserMigrate,
49
+ "STATS_MIGRATE_": StatsMigrate,
50
+ "FLOOD_WAIT_": FloodWait,
51
+ };
52
+ export function upgradeInstance(error) {
53
+ for (const [k, v] of Object.entries(prefixMap)) {
54
+ if (error.errorMessage.startsWith(k)) {
55
+ return new v(error);
56
+ }
57
+ }
58
+ for (const [k, v] of Object.entries(map)) {
59
+ if (error.errorMessage == k) {
60
+ return new v(error);
61
+ }
62
+ }
63
+ return error;
64
+ }
@@ -2,8 +2,8 @@ import { assertEquals, assertInstanceOf, debug, factorize, ige256Decrypt, ige256
2
2
  import { bigIntFromBuffer, bufferFromBigInt, concat, getRandomBigInt, modExp, rsaPad, sha1, UNREACHABLE } from "../1_utilities.js";
3
3
  import { functions, serialize, TLReader, types } from "../2_tl.js";
4
4
  import { PUBLIC_KEYS } from "../4_constants.js";
5
- import { ClientAbstract } from "./1_client_abstract.js";
6
5
  import { getMessageId, packUnencryptedMessage, unpackUnencryptedMessage } from "./0_message.js";
6
+ import { ClientAbstract } from "./1_client_abstract.js";
7
7
  const d = debug("ClientPlain/createAuthKey");
8
8
  export class ClientPlain extends ClientAbstract {
9
9
  constructor(params) {
@@ -3,6 +3,7 @@ import { functions, ReadObject, types } from "../2_tl.js";
3
3
  import { Storage } from "../3_storage.js";
4
4
  import { DC } from "../3_transport.js";
5
5
  import { BotCommand, BotCommandScope, ChatAction, ChatID, InlineQueryResult, InlineQueryResultButton, Message } from "../3_types.js";
6
+ import { Migrate } from "../4_errors.js";
6
7
  import { With } from "./0_utilities.js";
7
8
  import { ClientAbstract } from "./1_client_abstract.js";
8
9
  import { AnswerCallbackQueryParams, AuthorizeUserParams, ClientParams, EditMessageParams, FilterableUpdates, FilterUpdate, ForwardMessagesParams, Handler, ParseMode, SendMessagesParams, SendPollParams, Update } from "./3_types.js";
@@ -60,7 +61,7 @@ export declare class Client extends ClientAbstract {
60
61
  private assertUser;
61
62
  private assertBot;
62
63
  private fetchState;
63
- [handleMigrationError](err: types.RPCError): Promise<void>;
64
+ [handleMigrationError](err: Migrate): Promise<void>;
64
65
  private connectionInited;
65
66
  disconnect(): Promise<void>;
66
67
  private initConnection;
@@ -4,12 +4,13 @@ import { as, functions, getChannelChatId, Message_, MessageContainer, peerToChat
4
4
  import { StorageMemory } from "../3_storage.js";
5
5
  import { botCommandScopeToTlObject, constructCallbackQuery, constructInlineQuery, constructMessage, constructUser, FileID, FileType, inlineQueryResultToTlObject, messageEntityToTlObject, replyMarkupToTlObject, ThumbnailSource } from "../3_types.js";
6
6
  import { ACK_THRESHOLD, APP_VERSION, CHANNEL_DIFFERENCE_LIMIT_BOT, CHANNEL_DIFFERENCE_LIMIT_USER, DEVICE_MODEL, LANG_CODE, LANG_PACK, LAYER, MAX_CHANNEL_ID, MAX_CHAT_ID, STICKER_SET_NAME_TTL, SYSTEM_LANG_CODE, SYSTEM_VERSION, USERNAME_TTL, ZERO_CHANNEL_ID } from "../4_constants.js";
7
- import { isChannelPtsUpdate, isPtsUpdate, resolve } from "./0_utilities.js";
7
+ import { AuthKeyUnregistered, Migrate, PasswordHashInvalid, PhoneNumberInvalid, SessionPasswordNeeded, upgradeInstance } from "../4_errors.js";
8
+ import { parseHtml } from "./0_html.js";
8
9
  import { decryptMessage, encryptMessage, getMessageId } from "./0_message.js";
9
10
  import { checkPassword } from "./0_password.js";
10
- import { parseHtml } from "./0_html.js";
11
- import { ClientPlain } from "./2_client_plain.js";
11
+ import { isChannelPtsUpdate, isPtsUpdate, resolve } from "./0_utilities.js";
12
12
  import { ClientAbstract } from "./1_client_abstract.js";
13
+ import { ClientPlain } from "./2_client_plain.js";
13
14
  import { skip } from "./3_types.js";
14
15
  const d = debug("Client");
15
16
  const dGap = debug("Client/recoverUpdateGap");
@@ -327,18 +328,12 @@ export class Client extends ClientAbstract {
327
328
  d("state fetched [%s]", source);
328
329
  }
329
330
  async [handleMigrationError](err) {
330
- const match = err.errorMessage.match(/MIGRATE_(\d)$/);
331
- if (match) {
332
- let newDc = match[1];
333
- if (Math.abs(this.dcId) >= 10000) {
334
- newDc += "-test";
335
- }
336
- await this.reconnect(newDc);
337
- d("migrated to DC%s", newDc);
338
- }
339
- else {
340
- UNREACHABLE();
331
+ let newDc = String(err.dc);
332
+ if (Math.abs(this.dcId) >= 10000) {
333
+ newDc += "-test";
341
334
  }
335
+ await this.reconnect(newDc);
336
+ d("migrated to DC%s", newDc);
342
337
  }
343
338
  disconnect() {
344
339
  this.connectionInited = false;
@@ -411,7 +406,7 @@ export class Client extends ClientAbstract {
411
406
  return;
412
407
  }
413
408
  catch (err) {
414
- if (!(err instanceof types.RPCError) || err.errorMessage != "AUTH_KEY_UNREGISTERED") {
409
+ if (!(err instanceof AuthKeyUnregistered)) {
415
410
  throw err;
416
411
  }
417
412
  }
@@ -423,16 +418,10 @@ export class Client extends ClientAbstract {
423
418
  break;
424
419
  }
425
420
  catch (err) {
426
- if (err instanceof types.RPCError) {
427
- const match = err.errorMessage.match(/MIGRATE_(\d)$/);
428
- if (match) {
429
- await this[handleMigrationError](err);
430
- await this.initConnection();
431
- continue;
432
- }
433
- else {
434
- throw err;
435
- }
421
+ if (err instanceof Migrate) {
422
+ await this[handleMigrationError](err);
423
+ await this.initConnection();
424
+ continue;
436
425
  }
437
426
  else {
438
427
  throw err;
@@ -464,16 +453,10 @@ export class Client extends ClientAbstract {
464
453
  sentCode = await sendCode();
465
454
  }
466
455
  catch (err) {
467
- if (err instanceof types.RPCError) {
468
- const match = err.errorMessage.match(/MIGRATE_(\d)$/);
469
- if (match) {
470
- await this[handleMigrationError](err);
471
- await this.initConnection();
472
- sentCode = await sendCode();
473
- }
474
- else {
475
- throw err;
476
- }
456
+ if (err instanceof Migrate) {
457
+ await this[handleMigrationError](err);
458
+ await this.initConnection();
459
+ sentCode = await sendCode();
477
460
  }
478
461
  else {
479
462
  throw err;
@@ -482,7 +465,7 @@ export class Client extends ClientAbstract {
482
465
  break;
483
466
  }
484
467
  catch (err) {
485
- if (err instanceof types.RPCError && err.errorMessage == "PHONE_NUMBER_INVALID") {
468
+ if (err instanceof PhoneNumberInvalid) {
486
469
  continue;
487
470
  }
488
471
  else {
@@ -516,7 +499,7 @@ export class Client extends ClientAbstract {
516
499
  }
517
500
  }
518
501
  }
519
- if (!(err instanceof types.RPCError && err.errorMessage == "SESSION_PASSWORD_NEEDED")) {
502
+ if (!(err instanceof SessionPasswordNeeded)) {
520
503
  throw err;
521
504
  }
522
505
  while (true) {
@@ -535,7 +518,7 @@ export class Client extends ClientAbstract {
535
518
  break;
536
519
  }
537
520
  catch (err) {
538
- if (err instanceof types.RPCError && err.errorMessage == "PASSWORD_HASH_INVALID") {
521
+ if (err instanceof PasswordHashInvalid) {
539
522
  continue;
540
523
  }
541
524
  else {
@@ -557,7 +540,7 @@ export class Client extends ClientAbstract {
557
540
  return;
558
541
  }
559
542
  catch (err) {
560
- if (!(err instanceof types.RPCError) || err.errorMessage != "AUTH_KEY_UNREGISTERED") {
543
+ if (!(err instanceof AuthKeyUnregistered)) {
561
544
  throw err;
562
545
  }
563
546
  }
@@ -609,7 +592,7 @@ export class Client extends ClientAbstract {
609
592
  const promise = this.promises.get(messageId);
610
593
  if (promise) {
611
594
  if (result instanceof types.RPCError) {
612
- promise.reject(result);
595
+ promise.reject(upgradeInstance(result));
613
596
  }
614
597
  else {
615
598
  promise.resolve(result);
@@ -703,7 +686,7 @@ export class Client extends ClientAbstract {
703
686
  });
704
687
  }
705
688
  catch (err) {
706
- if (err instanceof types.RPCError && err.errorMessage == "AUTH_KEY_UNREGISTERED") {
689
+ if (err instanceof AuthKeyUnregistered) {
707
690
  await this.propagateAuthorizationState(false);
708
691
  }
709
692
  throw err;
@@ -23,11 +23,11 @@ export declare function ige256Decrypt(data: Uint8Array, key: Uint8Array, iv: Uin
23
23
  * @param iv 16-byte initialization vector
24
24
  * @param state 1-byte state
25
25
  */
26
- export declare function ctr256Encrypt(data: Uint8Array, key: Uint8Array, iv: Uint8Array, state: Uint8Array): [Uint8Array, Uint8Array, Uint8Array];
26
+ export declare function ctr256Encrypt(data: Uint8Array, key: Uint8Array, iv: Uint8Array, state: Uint8Array): [Uint8Array, Uint8Array];
27
27
  /**
28
28
  * Alias of `ctr256Encrypt`
29
29
  */
30
- export declare function ctr256Decrypt(data: Uint8Array, key: Uint8Array, iv: Uint8Array, state: Uint8Array): [Uint8Array, Uint8Array, Uint8Array];
30
+ export declare function ctr256Decrypt(data: Uint8Array, key: Uint8Array, iv: Uint8Array, state: Uint8Array): [Uint8Array, Uint8Array];
31
31
  /**
32
32
  * Performs CBC-256 encryption.
33
33
  *
@@ -1,5 +1,14 @@
1
1
  import { default as init_ } from "./tgcrypto.js";
2
2
  // deno-lint-ignore no-explicit-any
3
+ function collectVector(vector) {
4
+ const p = new Uint8Array(vector.size());
5
+ for (let i = 0; i < vector.size(); i++) {
6
+ p[i] = vector.get(i);
7
+ }
8
+ vector.delete();
9
+ return p;
10
+ }
11
+ // deno-lint-ignore no-explicit-any
3
12
  let module_;
4
13
  const promise = init_().then((v) => module_ = v);
5
14
  export async function init() {
@@ -13,7 +22,7 @@ export async function init() {
13
22
  * @param iv 32-byte initialization vector
14
23
  */
15
24
  export function ige256Encrypt(data, key, iv) {
16
- return module_.ige256Encrypt(data, key, iv);
25
+ return collectVector(module_.ige256Encrypt(data, key, iv));
17
26
  }
18
27
  /**
19
28
  * Performs IGE-256 decryption.
@@ -23,7 +32,7 @@ export function ige256Encrypt(data, key, iv) {
23
32
  * @param iv 32-byte initialization vector
24
33
  */
25
34
  export function ige256Decrypt(data, key, iv) {
26
- return module_.ige256Decrypt(data, key, iv);
35
+ return collectVector(module_.ige256Decrypt(data, key, iv));
27
36
  }
28
37
  /**
29
38
  * Performs CTR-256 encryption.
@@ -34,13 +43,15 @@ export function ige256Decrypt(data, key, iv) {
34
43
  * @param state 1-byte state
35
44
  */
36
45
  export function ctr256Encrypt(data, key, iv, state) {
37
- return module_.ctr256Encrypt(data, key, iv, state);
46
+ const r = module_.ctr256Encrypt(data, data, key, iv, state);
47
+ return r.map(collectVector);
38
48
  }
39
49
  /**
40
50
  * Alias of `ctr256Encrypt`
41
51
  */
42
52
  export function ctr256Decrypt(data, key, iv, state) {
43
- return module_.ctr256Decrypt(data, key, iv, state);
53
+ const r = module_.ctr256Decrypt(data, data, key, iv, state);
54
+ return r.map(collectVector);
44
55
  }
45
56
  /**
46
57
  * Performs CBC-256 encryption.
@@ -50,7 +61,8 @@ export function ctr256Decrypt(data, key, iv, state) {
50
61
  * @param iv 16-byte initialization vector
51
62
  */
52
63
  export function cbc256Encrypt(data, key, iv) {
53
- return module_.cbc256Encrypt(data, key, iv);
64
+ module_.cbc256Encrypt(data, data, key, iv);
65
+ return data;
54
66
  }
55
67
  /**
56
68
  * Performs CBC-256 decryption.
@@ -60,7 +72,8 @@ export function cbc256Encrypt(data, key, iv) {
60
72
  * @param iv 16-byte initialization vector
61
73
  */
62
74
  export function cbc256Decrypt(data, key, iv) {
63
- return module_.cbc256Decrypt(data, key, iv);
75
+ module_.cbc256Decrypt(data, data, key, iv);
76
+ return data;
64
77
  }
65
78
  export function factorize(pq) {
66
79
  const vector = module_.factorize(pq);
@@ -0,0 +1,2 @@
1
+ export default Module;
2
+ declare function Module(moduleArg?: {}): any;