@mtkruto/node 0.0.834 → 0.0.900

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 (145) hide show
  1. package/esm/client/client.d.ts +29 -4
  2. package/esm/client/client.js +528 -71
  3. package/esm/client/client_abstract.d.ts +1 -1
  4. package/esm/connection/connection.d.ts +1 -1
  5. package/esm/constants.d.ts +2 -0
  6. package/esm/constants.js +2 -0
  7. package/esm/deps/deno.land/std@0.190.0/encoding/base64.d.ts +11 -0
  8. package/esm/deps/deno.land/std@0.190.0/encoding/base64.js +140 -0
  9. package/esm/deps/deno.land/x/q@v0.0.1/mod.d.ts +6 -0
  10. package/esm/deps/deno.land/x/q@v0.0.1/mod.js +71 -0
  11. package/esm/deps.d.ts +2 -0
  12. package/esm/deps.js +2 -0
  13. package/esm/mod.d.ts +3 -3
  14. package/esm/mod.js +3 -3
  15. package/esm/storage/storage.d.ts +19 -1
  16. package/esm/storage/storage.js +103 -0
  17. package/esm/storage/storage_local_storage.d.ts +1 -1
  18. package/esm/storage/storage_memory.d.ts +1 -1
  19. package/esm/storage/storage_session_storage.d.ts +1 -1
  20. package/esm/tl/1_tl_object.d.ts +0 -1
  21. package/esm/tl/1_tl_object.js +1 -1
  22. package/esm/tl/4_tl_writer.d.ts +5 -0
  23. package/esm/tl/5_rpc_result.d.ts +9 -0
  24. package/esm/tl/6_message.d.ts +10 -0
  25. package/esm/tl/{5_message.js → 6_message.js} +2 -2
  26. package/esm/tl/{6_message_container.d.ts → 7_message_container.d.ts} +1 -1
  27. package/esm/tl/{6_message_container.js → 7_message_container.js} +2 -2
  28. package/esm/transport/transport.d.ts +1 -1
  29. package/esm/types/0_chat_administrator_rights.d.ts +17 -0
  30. package/esm/types/0_chat_administrator_rights.js +38 -0
  31. package/esm/types/0_chat_photo.d.ts +24 -0
  32. package/esm/types/0_chat_photo.js +28 -0
  33. package/esm/types/0_force_reply.d.ts +8 -0
  34. package/esm/types/0_force_reply.js +17 -0
  35. package/esm/types/0_keyboard_button_poll_type.d.ts +3 -0
  36. package/esm/types/0_login_url.d.ts +6 -0
  37. package/esm/types/0_login_url.js +1 -0
  38. package/esm/types/0_message_entity.d.ts +1 -1
  39. package/esm/types/0_message_entity.js +2 -2
  40. package/esm/types/0_reply_keyboard_remove.d.ts +7 -0
  41. package/esm/types/0_reply_keyboard_remove.js +8 -0
  42. package/esm/types/0_web_app_info.d.ts +4 -0
  43. package/esm/types/0_web_app_info.js +3 -0
  44. package/esm/types/1_chat.d.ts +60 -0
  45. package/esm/types/1_chat.js +70 -0
  46. package/esm/types/1_inline_keyboard_button.d.ts +33 -0
  47. package/esm/types/1_inline_keyboard_button.js +63 -0
  48. package/esm/types/1_keyboard_button.d.ts +43 -0
  49. package/esm/types/1_keyboard_button.js +133 -0
  50. package/esm/types/1_user.d.ts +19 -0
  51. package/esm/types/1_user.js +25 -0
  52. package/esm/types/2_inline_keyboard_markup.d.ts +7 -0
  53. package/esm/types/2_inline_keyboard_markup.js +25 -0
  54. package/esm/types/2_reply_keyboard_markup.d.ts +12 -0
  55. package/esm/types/2_reply_keyboard_markup.js +37 -0
  56. package/esm/types/3_message.d.ts +26 -0
  57. package/esm/types/3_message.js +91 -0
  58. package/esm/utilities/0_control.d.ts +1 -0
  59. package/esm/utilities/0_control.js +3 -0
  60. package/esm/utilities/0_object.d.ts +1 -0
  61. package/esm/utilities/0_object.js +14 -0
  62. package/esm/utilities/0_types.js +1 -0
  63. package/esm/utilities/1_message.d.ts +2 -2
  64. package/esm/utilities/1_message.js +3 -3
  65. package/esm/utilities/1_password.js +1 -1
  66. package/package.json +1 -1
  67. package/script/client/client.d.ts +29 -4
  68. package/script/client/client.js +528 -71
  69. package/script/client/client_abstract.d.ts +1 -1
  70. package/script/connection/connection.d.ts +1 -1
  71. package/script/constants.d.ts +2 -0
  72. package/script/constants.js +3 -1
  73. package/script/deps/deno.land/std@0.190.0/encoding/base64.d.ts +11 -0
  74. package/script/deps/deno.land/std@0.190.0/encoding/base64.js +145 -0
  75. package/script/deps/deno.land/x/q@v0.0.1/mod.d.ts +6 -0
  76. package/script/deps/deno.land/x/q@v0.0.1/mod.js +75 -0
  77. package/script/deps.d.ts +2 -0
  78. package/script/deps.js +6 -1
  79. package/script/mod.d.ts +3 -3
  80. package/script/mod.js +3 -3
  81. package/script/storage/storage.d.ts +19 -1
  82. package/script/storage/storage.js +126 -0
  83. package/script/storage/storage_local_storage.d.ts +1 -1
  84. package/script/storage/storage_memory.d.ts +1 -1
  85. package/script/storage/storage_session_storage.d.ts +1 -1
  86. package/script/tl/1_tl_object.d.ts +0 -1
  87. package/script/tl/1_tl_object.js +1 -1
  88. package/script/tl/4_tl_writer.d.ts +5 -0
  89. package/script/tl/5_rpc_result.d.ts +9 -0
  90. package/script/tl/6_message.d.ts +10 -0
  91. package/script/tl/{5_message.js → 6_message.js} +6 -6
  92. package/script/tl/{6_message_container.d.ts → 7_message_container.d.ts} +1 -1
  93. package/script/tl/{6_message_container.js → 7_message_container.js} +4 -4
  94. package/script/transport/transport.d.ts +1 -1
  95. package/script/types/0_chat_administrator_rights.d.ts +17 -0
  96. package/script/types/0_chat_administrator_rights.js +66 -0
  97. package/script/types/0_chat_photo.d.ts +24 -0
  98. package/script/types/0_chat_photo.js +55 -0
  99. package/script/types/0_force_reply.d.ts +8 -0
  100. package/script/types/0_force_reply.js +45 -0
  101. package/script/types/0_keyboard_button_poll_type.d.ts +3 -0
  102. package/script/types/0_login_url.d.ts +6 -0
  103. package/script/types/0_login_url.js +2 -0
  104. package/script/types/0_message_entity.d.ts +1 -1
  105. package/script/types/0_message_entity.js +2 -2
  106. package/script/types/0_reply_keyboard_remove.d.ts +7 -0
  107. package/script/types/0_reply_keyboard_remove.js +36 -0
  108. package/script/types/0_web_app_info.d.ts +4 -0
  109. package/script/types/0_web_app_info.js +7 -0
  110. package/script/types/1_chat.d.ts +60 -0
  111. package/script/types/1_chat.js +97 -0
  112. package/script/types/1_inline_keyboard_button.d.ts +33 -0
  113. package/script/types/1_inline_keyboard_button.js +91 -0
  114. package/script/types/1_keyboard_button.d.ts +43 -0
  115. package/script/types/1_keyboard_button.js +161 -0
  116. package/script/types/1_user.d.ts +19 -0
  117. package/script/types/1_user.js +52 -0
  118. package/script/types/2_inline_keyboard_markup.d.ts +7 -0
  119. package/script/types/2_inline_keyboard_markup.js +53 -0
  120. package/script/types/2_reply_keyboard_markup.d.ts +12 -0
  121. package/script/types/2_reply_keyboard_markup.js +65 -0
  122. package/script/types/3_message.d.ts +26 -0
  123. package/script/types/3_message.js +118 -0
  124. package/script/utilities/0_control.d.ts +1 -0
  125. package/script/utilities/0_control.js +7 -0
  126. package/script/utilities/0_object.d.ts +1 -0
  127. package/script/utilities/0_object.js +18 -0
  128. package/script/utilities/0_types.js +2 -0
  129. package/script/utilities/1_message.d.ts +2 -2
  130. package/script/utilities/1_message.js +9 -9
  131. package/script/utilities/1_password.js +1 -1
  132. package/esm/tl/3_tl_writer.d.ts +0 -5
  133. package/esm/tl/4_rpc_result.d.ts +0 -8
  134. package/esm/tl/5_message.d.ts +0 -11
  135. package/script/tl/3_tl_writer.d.ts +0 -5
  136. package/script/tl/4_rpc_result.d.ts +0 -8
  137. package/script/tl/5_message.d.ts +0 -11
  138. /package/esm/tl/{3_tl_writer.js → 4_tl_writer.js} +0 -0
  139. /package/esm/tl/{4_rpc_result.js → 5_rpc_result.js} +0 -0
  140. /package/esm/{types.js → types/0_keyboard_button_poll_type.js} +0 -0
  141. /package/esm/{types.d.ts → utilities/0_types.d.ts} +0 -0
  142. /package/script/tl/{3_tl_writer.js → 4_tl_writer.js} +0 -0
  143. /package/script/tl/{4_rpc_result.js → 5_rpc_result.js} +0 -0
  144. /package/script/{types.js → types/0_keyboard_button_poll_type.js} +0 -0
  145. /package/script/{types.d.ts → utilities/0_types.d.ts} +0 -0
@@ -0,0 +1,37 @@
1
+ import { as } from "../tl/1_tl_object.js";
2
+ import * as types from "../tl/2_types.js";
3
+ import { constructKeyboardButton, keyboardButtonToTlObject } from "./1_keyboard_button.js";
4
+ export function constructReplyKeyboardMarkup(keyboard_) {
5
+ const rows = new Array();
6
+ for (const row_ of keyboard_.rows.map((v) => v[as](types.KeyboardButtonRow))) {
7
+ const row = new Array();
8
+ for (const button_ of row_.buttons) {
9
+ row.push(constructKeyboardButton(button_));
10
+ }
11
+ rows.push(row);
12
+ }
13
+ return {
14
+ resizeKeyboard: keyboard_.resize || false,
15
+ oneTimeKeyboard: keyboard_.singleUse || false,
16
+ selective: keyboard_.selective || false,
17
+ isPersistent: keyboard_.persistent || false,
18
+ keyboard: rows,
19
+ };
20
+ }
21
+ export function replyKeyboardMarkupToTlObject(replyMarkup) {
22
+ const rows_ = new Array();
23
+ for (const row of replyMarkup.keyboard) {
24
+ const row_ = new Array();
25
+ for (const button of row) {
26
+ row_.push(keyboardButtonToTlObject(button));
27
+ }
28
+ rows_.push(new types.KeyboardButtonRow({ buttons: row_ }));
29
+ }
30
+ return new types.ReplyKeyboardMarkup({
31
+ resize: replyMarkup.resizeKeyboard || undefined,
32
+ singleUse: replyMarkup.oneTimeKeyboard || undefined,
33
+ selective: replyMarkup.selective || undefined,
34
+ persistent: replyMarkup.isPersistent || undefined,
35
+ rows: rows_,
36
+ });
37
+ }
@@ -0,0 +1,26 @@
1
+ import * as types from "../tl/2_types.js";
2
+ import { ForceReply } from "./0_force_reply.js";
3
+ import { MessageEntity } from "./0_message_entity.js";
4
+ import { ReplyKeyboardRemove } from "./0_reply_keyboard_remove.js";
5
+ import { Chat } from "./1_chat.js";
6
+ import { User } from "./1_user.js";
7
+ import { InlineKeyboardMarkup } from "./2_inline_keyboard_markup.js";
8
+ import { ReplyKeyboardMarkup } from "./2_reply_keyboard_markup.js";
9
+ export interface Message {
10
+ id: number;
11
+ from?: User;
12
+ chat: Chat;
13
+ text?: string;
14
+ caption?: string;
15
+ entities?: MessageEntity[];
16
+ captionEntities?: MessageEntity[];
17
+ date?: Date;
18
+ editDate?: Date;
19
+ views?: number;
20
+ replyMarkup?: InlineKeyboardMarkup | ReplyKeyboardMarkup | ReplyKeyboardRemove | ForceReply;
21
+ }
22
+ export declare function constructMessage(message_: types.Message, getEntity: {
23
+ (peer: types.PeerUser): Promise<types.User | null>;
24
+ (peer: types.PeerChat): Promise<types.Chat | null>;
25
+ (peer: types.PeerChannel): Promise<types.Channel | null>;
26
+ }): Promise<Message>;
@@ -0,0 +1,91 @@
1
+ import { UNREACHABLE } from "../utilities/0_control.js";
2
+ import { cleanObject } from "../utilities/0_object.js";
3
+ import * as types from "../tl/2_types.js";
4
+ import { constructForceReply } from "./0_force_reply.js";
5
+ import { constructMessageEntity } from "./0_message_entity.js";
6
+ import { constructReplyKeyboardRemove } from "./0_reply_keyboard_remove.js";
7
+ import { constructChat } from "./1_chat.js";
8
+ import { constructUser } from "./1_user.js";
9
+ import { constructInlineKeyboardMarkup } from "./2_inline_keyboard_markup.js";
10
+ import { constructReplyKeyboardMarkup } from "./2_reply_keyboard_markup.js";
11
+ export async function constructMessage(message_, getEntity) {
12
+ let chat_ = null;
13
+ if (message_.peerId instanceof types.PeerUser) {
14
+ const entity = await getEntity(message_.peerId);
15
+ if (entity) {
16
+ chat_ = constructChat(entity);
17
+ }
18
+ else {
19
+ UNREACHABLE();
20
+ }
21
+ }
22
+ else if (message_.peerId instanceof types.PeerChat) {
23
+ const entity = await getEntity(message_.peerId);
24
+ if (entity) {
25
+ chat_ = constructChat(entity);
26
+ }
27
+ else {
28
+ UNREACHABLE();
29
+ }
30
+ }
31
+ else if (message_.peerId instanceof types.PeerChannel) {
32
+ const entity = await getEntity(message_.peerId);
33
+ if (entity) {
34
+ chat_ = constructChat(entity);
35
+ }
36
+ else {
37
+ UNREACHABLE();
38
+ }
39
+ }
40
+ else {
41
+ UNREACHABLE();
42
+ }
43
+ const message = { id: message_.id, chat: chat_, views: message_.views };
44
+ if (message_.fromId instanceof types.PeerUser) {
45
+ const entity = await getEntity(message_.fromId);
46
+ if (entity) {
47
+ message.from = constructUser(entity);
48
+ }
49
+ else {
50
+ UNREACHABLE();
51
+ }
52
+ }
53
+ if (message_.message) {
54
+ if (message_.media == undefined) {
55
+ message.text = message_.message;
56
+ }
57
+ else {
58
+ message.caption = message_.message;
59
+ }
60
+ }
61
+ if (message_.entities != undefined) {
62
+ if (message_.media == undefined) {
63
+ message.entities = message_.entities.map(constructMessageEntity).filter((v) => v);
64
+ }
65
+ else {
66
+ message.captionEntities = message_.entities.map(constructMessageEntity).filter((v) => v);
67
+ }
68
+ }
69
+ message.date = new Date(message_.date * 1000);
70
+ if (message_.editDate != undefined) {
71
+ message.editDate = new Date(message_.editDate * 1000);
72
+ }
73
+ if (message_.replyMarkup) {
74
+ if (message_.replyMarkup instanceof types.ReplyKeyboardMarkup) {
75
+ message.replyMarkup = constructReplyKeyboardMarkup(message_.replyMarkup);
76
+ }
77
+ else if (message_.replyMarkup instanceof types.ReplyInlineMarkup) {
78
+ message.replyMarkup = constructInlineKeyboardMarkup(message_.replyMarkup);
79
+ }
80
+ else if (message_.replyMarkup instanceof types.ReplyKeyboardHide) {
81
+ message.replyMarkup = constructReplyKeyboardRemove(message_.replyMarkup);
82
+ }
83
+ else if (message_.replyMarkup instanceof types.ReplyKeyboardForceReply) {
84
+ message.replyMarkup = constructForceReply(message_.replyMarkup);
85
+ }
86
+ else {
87
+ UNREACHABLE();
88
+ }
89
+ }
90
+ return cleanObject(message);
91
+ }
@@ -0,0 +1 @@
1
+ export declare function UNREACHABLE(): never;
@@ -0,0 +1,3 @@
1
+ export function UNREACHABLE() {
2
+ throw new Error("Unreachable");
3
+ }
@@ -0,0 +1 @@
1
+ export declare function cleanObject<T extends Record<string, any>>(object: T): T;
@@ -0,0 +1,14 @@
1
+ // deno-lint-ignore no-explicit-any
2
+ export function cleanObject(object) {
3
+ for (const [k, v] of Object.entries(object)) {
4
+ switch (typeof v) {
5
+ case "undefined":
6
+ delete object[k];
7
+ break;
8
+ case "object":
9
+ // @ts-ignore: this works, no idea why it complains
10
+ object[k] = cleanObject(v);
11
+ }
12
+ }
13
+ return object;
14
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -1,5 +1,5 @@
1
- import { Message } from "../tl/5_message.js";
2
- import { MessageContainer } from "../tl/6_message_container.js";
1
+ import { Message } from "../tl/6_message.js";
2
+ import { MessageContainer } from "../tl/7_message_container.js";
3
3
  export declare function getMessageId(): bigint;
4
4
  export declare function packUnencryptedMessage(data: Uint8Array): Uint8Array;
5
5
  export declare function unpackUnencryptedMessage(buffer: Uint8Array): {
@@ -3,9 +3,9 @@ import { TLRawReader } from "../tl/0_tl_raw_reader.js";
3
3
  import { TLRawWriter } from "../tl/0_tl_raw_writer.js";
4
4
  import { id, serialize } from "../tl/1_tl_object.js";
5
5
  import { TLReader } from "../tl/3_tl_reader.js";
6
- import { RPCResult } from "../tl/4_rpc_result.js";
7
- import { Message } from "../tl/5_message.js";
8
- import { MessageContainer } from "../tl/6_message_container.js";
6
+ import { RPCResult } from "../tl/5_rpc_result.js";
7
+ import { Message } from "../tl/6_message.js";
8
+ import { MessageContainer } from "../tl/7_message_container.js";
9
9
  import { bufferFromBigInt, concat } from "./0_buffer.js";
10
10
  import { sha256 } from "./0_hash.js";
11
11
  let lastMsgId = 0n;
@@ -30,7 +30,7 @@ export function isSafePrime(primeBytes, g) {
30
30
  0xB9, 0x2F, 0xCC, 0x5B,
31
31
  ]);
32
32
  if (goodPrime.every((v, i) => v == primeBytes[i])) {
33
- if ([3, 4, 5, 7].includes(g)) { // It's good
33
+ if ([3, 4, 5, 7].includes(g)) {
34
34
  return true;
35
35
  }
36
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.834",
5
+ "version": "0.0.900",
6
6
  "description": "MTKruto for Node.js",
7
7
  "author": "Roj <rojvv@icloud.com>",
8
8
  "license": "LGPL-3.0-or-later",
@@ -1,10 +1,17 @@
1
- import { MaybePromise } from "../types.js";
1
+ import { MaybePromise } from "../utilities/0_types.js";
2
2
  import * as types from "../tl/2_types.js";
3
3
  import * as functions from "../tl/3_functions.js";
4
+ import { ReadObject } from "../tl/3_tl_reader.js";
4
5
  import { ClientAbstract } from "./client_abstract.js";
5
6
  import { Storage } from "../storage/storage.js";
6
7
  import { DC, TransportProvider } from "../transport/transport_provider.js";
7
8
  import { MessageEntity } from "../types/0_message_entity.js";
9
+ import { ReplyKeyboardRemove } from "../types/0_reply_keyboard_remove.js";
10
+ import { ForceReply } from "../types/0_force_reply.js";
11
+ import { ReplyKeyboardMarkup } from "../types/2_reply_keyboard_markup.js";
12
+ import { InlineKeyboardMarkup } from "../types/2_inline_keyboard_markup.js";
13
+ import { Message } from "../types/3_message.js";
14
+ export declare const getEntity: unique symbol;
8
15
  export declare const restartAuth: unique symbol;
9
16
  export declare enum ParseMode {
10
17
  None = "none",
@@ -15,7 +22,7 @@ export interface AuthorizeUserParams<S = string> {
15
22
  code: S | (() => MaybePromise<S>);
16
23
  password: S | (() => MaybePromise<S>);
17
24
  }
18
- export type UpdatesHandler = null | ((client: Client, update: types.Updates) => MaybePromise<void>);
25
+ export type UpdateHandler = null | ((client: Client, update: types.TypeUpdate) => MaybePromise<void>);
19
26
  export interface ClientParams {
20
27
  /**
21
28
  * Default parse mode. Defauls to `ParseMode.None`.
@@ -59,7 +66,8 @@ export declare class Client extends ClientAbstract {
59
66
  private state;
60
67
  private promises;
61
68
  private toAcknowledge;
62
- updatesHandler: UpdatesHandler;
69
+ private updateState?;
70
+ updateHandler: UpdateHandler;
63
71
  readonly parseMode: ParseMode;
64
72
  readonly appVersion: string;
65
73
  readonly deviceModel: string;
@@ -91,6 +99,7 @@ export declare class Client extends ClientAbstract {
91
99
  * Before establishing the connection, the session is saved.
92
100
  */
93
101
  connect(): Promise<void>;
102
+ private fetchState;
94
103
  /**
95
104
  * Calls [initConnection](1) and authorizes the client with one of the following:
96
105
  *
@@ -109,6 +118,7 @@ export declare class Client extends ClientAbstract {
109
118
  * [2]: https://core.telegram.org/method/updates.getState
110
119
  */
111
120
  authorize(params: string | types.AuthExportedAuthorization | AuthorizeUserParams): Promise<void>;
121
+ private messageProcessQueue;
112
122
  private receiveLoop;
113
123
  private pingLoop;
114
124
  /**
@@ -125,8 +135,20 @@ export declare class Client extends ClientAbstract {
125
135
  send<T extends (functions.Function<unknown> | types.Type) = functions.Function<unknown>>(function_: T): Promise<void>;
126
136
  private processChats;
127
137
  private processUsers;
138
+ private updateApplicationMutex;
139
+ private applyUpdateNoGap;
140
+ private applyUpdate;
128
141
  private processUpdates;
142
+ private setUpdateStateDate;
143
+ private getLocalState;
144
+ private updateGapRecoveryMutex;
145
+ private recoverUpdateGap;
146
+ private recoverChannelUpdateGap;
129
147
  getInputPeer(id: string | number): Promise<types.InputPeerChat | types.InputPeerUser | types.InputPeerChannel>;
148
+ [getEntity](peer: types.PeerUser): Promise<types.User | null>;
149
+ [getEntity](peer: types.PeerChat): Promise<types.Chat | null>;
150
+ [getEntity](peer: types.PeerChannel): Promise<types.Channel | null>;
151
+ processResult(result: ReadObject): Promise<void>;
130
152
  sendMessage(chatId: number | string, text: string, params?: {
131
153
  parseMode?: ParseMode;
132
154
  entities?: MessageEntity[];
@@ -136,5 +158,8 @@ export declare class Client extends ClientAbstract {
136
158
  replyToMessageId?: number;
137
159
  messageThreadId?: number;
138
160
  sendAs?: number | string;
139
- }): Promise<types.TypeUpdates>;
161
+ replyMarkup?: InlineKeyboardMarkup | ReplyKeyboardMarkup | ReplyKeyboardRemove | ForceReply;
162
+ }): Promise<Message>;
163
+ getMessages(chatId: number | string, messageIds: number[]): Promise<Message[]>;
164
+ getMessage(chatId: number | string, messageId: number): Promise<Message | null>;
140
165
  }