@mtkruto/node 0.0.830 → 0.0.832

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 (133) hide show
  1. package/esm/client/client.d.ts +24 -0
  2. package/esm/client/client.js +166 -3
  3. package/esm/constants.d.ts +4 -0
  4. package/esm/constants.js +4 -0
  5. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/DomHandler.d.ts +83 -0
  6. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/DomHandler.js +203 -0
  7. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/DomSerializer.d.ts +50 -0
  8. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/DomSerializer.js +274 -0
  9. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/ElementType.d.ts +47 -0
  10. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/ElementType.js +51 -0
  11. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/FeedHandler.d.ts +66 -0
  12. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/FeedHandler.js +191 -0
  13. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/Node.d.ts +168 -0
  14. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/Node.js +385 -0
  15. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/Parser.d.ts +159 -0
  16. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/Parser.js +431 -0
  17. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/Tokenizer.d.ts +181 -0
  18. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/Tokenizer.js +1046 -0
  19. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/mod.d.ts +42 -0
  20. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/mod.js +52 -0
  21. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/decode.d.ts +11 -0
  22. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/decode.js +122 -0
  23. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/decode_codepoint.d.ts +1 -0
  24. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/decode_codepoint.js +24 -0
  25. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/encode.d.ts +46 -0
  26. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/encode.js +121 -0
  27. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/maps/decode.d.ts +31 -0
  28. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/maps/decode.js +30 -0
  29. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/maps/entities.d.ts +2128 -0
  30. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/maps/entities.js +2127 -0
  31. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/maps/legacy.d.ts +109 -0
  32. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/maps/legacy.js +108 -0
  33. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/maps/xml.d.ts +8 -0
  34. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/maps/xml.js +1 -0
  35. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/mod.d.ts +90 -0
  36. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/mod.js +95 -0
  37. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/helpers.d.ts +50 -0
  38. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/helpers.js +128 -0
  39. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/legacy.d.ts +46 -0
  40. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/legacy.js +110 -0
  41. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/manipulation.d.ts +42 -0
  42. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/manipulation.js +120 -0
  43. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/mod.d.ts +6 -0
  44. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/mod.js +6 -0
  45. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/querying.d.ts +54 -0
  46. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/querying.js +110 -0
  47. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/stringify.d.ts +40 -0
  48. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/stringify.js +75 -0
  49. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/traversal.d.ts +58 -0
  50. package/esm/deps/deno.land/x/html_parser@v0.1.3/src/utils/traversal.js +101 -0
  51. package/esm/deps.d.ts +2 -1
  52. package/esm/deps.js +2 -1
  53. package/esm/storage/storage.d.ts +9 -0
  54. package/esm/storage/storage.js +59 -0
  55. package/esm/types/0_message_entity.d.ts +92 -0
  56. package/esm/types/0_message_entity.js +128 -0
  57. package/esm/utilities/0_html.d.ts +2 -0
  58. package/esm/utilities/0_html.js +74 -0
  59. package/package.json +1 -1
  60. package/script/client/client.d.ts +24 -0
  61. package/script/client/client.js +165 -2
  62. package/script/constants.d.ts +4 -0
  63. package/script/constants.js +5 -1
  64. package/script/deps/deno.land/x/html_parser@v0.1.3/src/DomHandler.d.ts +83 -0
  65. package/script/deps/deno.land/x/html_parser@v0.1.3/src/DomHandler.js +207 -0
  66. package/script/deps/deno.land/x/html_parser@v0.1.3/src/DomSerializer.d.ts +50 -0
  67. package/script/deps/deno.land/x/html_parser@v0.1.3/src/DomSerializer.js +301 -0
  68. package/script/deps/deno.land/x/html_parser@v0.1.3/src/ElementType.d.ts +47 -0
  69. package/script/deps/deno.land/x/html_parser@v0.1.3/src/ElementType.js +55 -0
  70. package/script/deps/deno.land/x/html_parser@v0.1.3/src/FeedHandler.d.ts +66 -0
  71. package/script/deps/deno.land/x/html_parser@v0.1.3/src/FeedHandler.js +222 -0
  72. package/script/deps/deno.land/x/html_parser@v0.1.3/src/Node.d.ts +168 -0
  73. package/script/deps/deno.land/x/html_parser@v0.1.3/src/Node.js +404 -0
  74. package/script/deps/deno.land/x/html_parser@v0.1.3/src/Parser.d.ts +159 -0
  75. package/script/deps/deno.land/x/html_parser@v0.1.3/src/Parser.js +438 -0
  76. package/script/deps/deno.land/x/html_parser@v0.1.3/src/Tokenizer.d.ts +181 -0
  77. package/script/deps/deno.land/x/html_parser@v0.1.3/src/Tokenizer.js +1052 -0
  78. package/script/deps/deno.land/x/html_parser@v0.1.3/src/mod.d.ts +42 -0
  79. package/script/deps/deno.land/x/html_parser@v0.1.3/src/mod.js +88 -0
  80. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/decode.d.ts +11 -0
  81. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/decode.js +128 -0
  82. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/decode_codepoint.d.ts +1 -0
  83. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/decode_codepoint.js +30 -0
  84. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/encode.d.ts +46 -0
  85. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/encode.js +129 -0
  86. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/maps/decode.d.ts +31 -0
  87. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/maps/decode.js +32 -0
  88. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/maps/entities.d.ts +2128 -0
  89. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/maps/entities.js +2129 -0
  90. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/maps/legacy.d.ts +109 -0
  91. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/maps/legacy.js +110 -0
  92. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/maps/xml.d.ts +8 -0
  93. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/maps/xml.js +3 -0
  94. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/mod.d.ts +90 -0
  95. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/entities/mod.js +114 -0
  96. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/helpers.d.ts +50 -0
  97. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/helpers.js +134 -0
  98. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/legacy.d.ts +46 -0
  99. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/legacy.js +118 -0
  100. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/manipulation.d.ts +42 -0
  101. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/manipulation.js +129 -0
  102. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/mod.d.ts +6 -0
  103. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/mod.js +22 -0
  104. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/querying.d.ts +54 -0
  105. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/querying.js +119 -0
  106. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/stringify.d.ts +40 -0
  107. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/stringify.js +86 -0
  108. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/traversal.d.ts +58 -0
  109. package/script/deps/deno.land/x/html_parser@v0.1.3/src/utils/traversal.js +112 -0
  110. package/script/deps.d.ts +2 -1
  111. package/script/deps.js +4 -2
  112. package/script/storage/storage.d.ts +9 -0
  113. package/script/storage/storage.js +59 -0
  114. package/script/types/0_message_entity.d.ts +92 -0
  115. package/script/types/0_message_entity.js +156 -0
  116. package/script/utilities/0_html.d.ts +2 -0
  117. package/script/utilities/0_html.js +78 -0
  118. /package/esm/deps/deno.land/{std@0.186.0 → std@0.190.0}/fmt/colors.d.ts +0 -0
  119. /package/esm/deps/deno.land/{std@0.186.0 → std@0.190.0}/fmt/colors.js +0 -0
  120. /package/esm/deps/deno.land/{std@0.186.0 → std@0.190.0}/testing/_diff.d.ts +0 -0
  121. /package/esm/deps/deno.land/{std@0.186.0 → std@0.190.0}/testing/_diff.js +0 -0
  122. /package/esm/deps/deno.land/{std@0.186.0 → std@0.190.0}/testing/_format.d.ts +0 -0
  123. /package/esm/deps/deno.land/{std@0.186.0 → std@0.190.0}/testing/_format.js +0 -0
  124. /package/esm/deps/deno.land/{std@0.186.0 → std@0.190.0}/testing/asserts.d.ts +0 -0
  125. /package/esm/deps/deno.land/{std@0.186.0 → std@0.190.0}/testing/asserts.js +0 -0
  126. /package/script/deps/deno.land/{std@0.186.0 → std@0.190.0}/fmt/colors.d.ts +0 -0
  127. /package/script/deps/deno.land/{std@0.186.0 → std@0.190.0}/fmt/colors.js +0 -0
  128. /package/script/deps/deno.land/{std@0.186.0 → std@0.190.0}/testing/_diff.d.ts +0 -0
  129. /package/script/deps/deno.land/{std@0.186.0 → std@0.190.0}/testing/_diff.js +0 -0
  130. /package/script/deps/deno.land/{std@0.186.0 → std@0.190.0}/testing/_format.d.ts +0 -0
  131. /package/script/deps/deno.land/{std@0.186.0 → std@0.190.0}/testing/_format.js +0 -0
  132. /package/script/deps/deno.land/{std@0.186.0 → std@0.190.0}/testing/asserts.d.ts +0 -0
  133. /package/script/deps/deno.land/{std@0.186.0 → std@0.190.0}/testing/asserts.js +0 -0
@@ -4,7 +4,12 @@ import * as functions from "../tl/3_functions.js";
4
4
  import { ClientAbstract } from "./client_abstract.js";
5
5
  import { Storage } from "../storage/storage.js";
6
6
  import { DC, TransportProvider } from "../transport/transport_provider.js";
7
+ import { MessageEntity } from "../types/0_message_entity.js";
7
8
  export declare const restartAuth: unique symbol;
9
+ export declare enum ParseMode {
10
+ None = "none",
11
+ HTML = "html"
12
+ }
8
13
  export interface AuthorizeUserParams<S = string> {
9
14
  phone: S | (() => MaybePromise<S>);
10
15
  code: S | (() => MaybePromise<S>);
@@ -12,6 +17,10 @@ export interface AuthorizeUserParams<S = string> {
12
17
  }
13
18
  export type UpdatesHandler = null | ((client: Client, update: types.Updates) => MaybePromise<void>);
14
19
  export interface ClientParams {
20
+ /**
21
+ * Default parse mode. Defauls to `ParseMode.None`.
22
+ */
23
+ parseMode?: ParseMode;
15
24
  /**
16
25
  * The transport provider to use. Defaults to `defaultTransportProvider`.
17
26
  */
@@ -51,6 +60,7 @@ export declare class Client extends ClientAbstract {
51
60
  private promises;
52
61
  private toAcknowledge;
53
62
  updatesHandler: UpdatesHandler;
63
+ readonly parseMode: ParseMode;
54
64
  readonly appVersion: string;
55
65
  readonly deviceModel: string;
56
66
  readonly langCode: string;
@@ -113,4 +123,18 @@ export declare class Client extends ClientAbstract {
113
123
  * Alias for `invoke` with its second parameter being `true`.
114
124
  */
115
125
  send<T extends (functions.Function<unknown> | types.Type) = functions.Function<unknown>>(function_: T): Promise<void>;
126
+ private processChats;
127
+ private processUsers;
128
+ private processUpdates;
129
+ getInputPeer(id: string | number): Promise<types.InputPeerChat | types.InputPeerUser | types.InputPeerChannel>;
130
+ sendMessage(chatId: number | string, text: string, params?: {
131
+ parseMode?: ParseMode;
132
+ entities?: MessageEntity[];
133
+ disableWebPagePreview?: boolean;
134
+ disableNotification?: boolean;
135
+ protectContent?: boolean;
136
+ replyToMessageId?: number;
137
+ messageThreadId?: number;
138
+ sendAs?: number | string;
139
+ }): Promise<types.TypeUpdates>;
116
140
  }
@@ -1,8 +1,10 @@
1
1
  import { gunzip } from "../deps.js";
2
- import { ackThreshold, DEFAULT_APP_VERSION, DEFAULT_DEVICE_MODEL, DEFAULT_INITIAL_DC, DEFAULT_LANG_CODE, DEFAULT_LANG_PACK, DEFAULT_SYSTEM_LANG_CODE, DEFAULT_SYSTEM_VERSION, LAYER } from "../constants.js";
3
- import { bigIntFromBuffer, getRandomBigInt } from "../utilities/0_bigint.js";
2
+ import { ackThreshold, DEFAULT_APP_VERSION, DEFAULT_DEVICE_MODEL, DEFAULT_INITIAL_DC, DEFAULT_LANG_CODE, DEFAULT_LANG_PACK, DEFAULT_SYSTEM_LANG_CODE, DEFAULT_SYSTEM_VERSION, LAYER, MAX_CHANNEL_ID, MAX_CHAT_ID, USERNAME_TTL, ZERO_CHANNEL_ID } from "../constants.js";
3
+ import { bigIntFromBuffer, getRandomBigInt, getRandomId } from "../utilities/0_bigint.js";
4
+ import { parseHtml } from "../utilities/0_html.js";
4
5
  import { decryptMessage, encryptMessage, getMessageId } from "../utilities/1_message.js";
5
6
  import { checkPassword } from "../utilities/1_password.js";
7
+ import { as } from "../tl/1_tl_object.js";
6
8
  import * as types from "../tl/2_types.js";
7
9
  import * as functions from "../tl/3_functions.js";
8
10
  import { TLReader } from "../tl/3_tl_reader.js";
@@ -13,7 +15,13 @@ import { ClientAbstract } from "./client_abstract.js";
13
15
  import { ClientPlain } from "./client_plain.js";
14
16
  import { StorageMemory } from "../storage/storage_memory.js";
15
17
  import { sha1 } from "../utilities/0_hash.js";
18
+ import { messageEntityToTlObject } from "../types/0_message_entity.js";
16
19
  export const restartAuth = Symbol();
20
+ export var ParseMode;
21
+ (function (ParseMode) {
22
+ ParseMode["None"] = "none";
23
+ ParseMode["HTML"] = "html";
24
+ })(ParseMode || (ParseMode = {}));
17
25
  export class Client extends ClientAbstract {
18
26
  /**
19
27
  * Constructs the client.
@@ -79,6 +87,12 @@ export class Client extends ClientAbstract {
79
87
  writable: true,
80
88
  value: null
81
89
  });
90
+ Object.defineProperty(this, "parseMode", {
91
+ enumerable: true,
92
+ configurable: true,
93
+ writable: true,
94
+ value: void 0
95
+ });
82
96
  Object.defineProperty(this, "appVersion", {
83
97
  enumerable: true,
84
98
  configurable: true,
@@ -121,6 +135,7 @@ export class Client extends ClientAbstract {
121
135
  writable: true,
122
136
  value: false
123
137
  });
138
+ this.parseMode = params?.parseMode ?? ParseMode.None;
124
139
  this.appVersion = params?.appVersion ?? DEFAULT_APP_VERSION;
125
140
  this.deviceModel = params?.deviceModel ?? DEFAULT_DEVICE_MODEL;
126
141
  this.langCode = params?.langCode ?? DEFAULT_LANG_CODE;
@@ -391,7 +406,7 @@ export class Client extends ClientAbstract {
391
406
  }
392
407
  // logger().debug(`Received ${body.constructor.name}`);
393
408
  if (body instanceof types.Updates) {
394
- this.updatesHandler?.(this, body);
409
+ this.processUpdates(body);
395
410
  }
396
411
  else if (message.body instanceof RPCResult) {
397
412
  let result = message.body.result;
@@ -404,6 +419,10 @@ export class Client extends ClientAbstract {
404
419
  promise.reject(result);
405
420
  }
406
421
  else {
422
+ if (result instanceof types.Updates) {
423
+ await this.processChats(result.chats);
424
+ await this.processUsers(result.users);
425
+ }
407
426
  promise.resolve(result);
408
427
  }
409
428
  this.promises.delete(message.body.messageId);
@@ -472,4 +491,148 @@ export class Client extends ClientAbstract {
472
491
  send(function_) {
473
492
  return this.invoke(function_, true);
474
493
  }
494
+ async processChats(chats) {
495
+ for (const chat of chats) {
496
+ if (chat instanceof types.Channel && chat.accessHash) {
497
+ await this.storage.setChannelAccessHash(chat.id, chat.accessHash);
498
+ if (chat.username) {
499
+ await this.storage.updateUsernames("channel", chat.id, [chat.username]);
500
+ }
501
+ if (chat.usernames) {
502
+ await this.storage.updateUsernames("channel", chat.id, chat.usernames.map((v) => v[as](types.Username)).map((v) => v.username));
503
+ }
504
+ }
505
+ }
506
+ }
507
+ async processUsers(users) {
508
+ for (const user of users) {
509
+ if (user instanceof types.User && user.accessHash) {
510
+ await this.storage.setUserAccessHash(user.id, user.accessHash);
511
+ if (user.username) {
512
+ await this.storage.updateUsernames("user", user.id, [user.username]);
513
+ }
514
+ if (user.usernames) {
515
+ await this.storage.updateUsernames("user", user.id, user.usernames.map((v) => v[as](types.Username)).map((v) => v.username));
516
+ }
517
+ }
518
+ }
519
+ }
520
+ async processUpdates(updates) {
521
+ try {
522
+ await this.processChats(updates.chats);
523
+ await this.processUsers(updates.users);
524
+ for (const update of updates.updates) {
525
+ if (update instanceof types.UpdateUserName) {
526
+ await this.storage.updateUsernames("user", update.userId, update.usernames.map((v) => v[as](types.Username)).map((v) => v.username));
527
+ }
528
+ }
529
+ await this.updatesHandler?.(this, updates);
530
+ }
531
+ catch (err) {
532
+ console.error("Error processing updates:", err);
533
+ }
534
+ }
535
+ async getInputPeer(id) {
536
+ if (typeof id === "string") {
537
+ if (!id.startsWith("@")) {
538
+ throw new Error("Expected username to start with @");
539
+ }
540
+ else {
541
+ id = id.slice(1);
542
+ if (!id) {
543
+ throw new Error("Empty username");
544
+ }
545
+ let userId = 0n;
546
+ let channelId = 0n;
547
+ const maybeUsername = await this.storage.getUsername(id);
548
+ if (maybeUsername != null && Date.now() - maybeUsername[2].getTime() < USERNAME_TTL) {
549
+ const [type, id] = maybeUsername;
550
+ if (type == "user") {
551
+ userId = id;
552
+ }
553
+ else {
554
+ channelId = id;
555
+ }
556
+ }
557
+ else {
558
+ const resolved = await this.invoke(new functions.ContactsResolveUsername({ username: id }));
559
+ await this.processChats(resolved.chats);
560
+ await this.processUsers(resolved.users);
561
+ if (resolved.peer instanceof types.PeerUser) {
562
+ userId = resolved.peer.userId;
563
+ }
564
+ else if (resolved.peer instanceof types.PeerChannel) {
565
+ channelId = resolved.peer.channelId;
566
+ }
567
+ else {
568
+ throw new Error("Unreachable");
569
+ }
570
+ }
571
+ if (userId) {
572
+ const accessHash = await this.storage.getUserAccessHash(userId);
573
+ return new types.InputPeerUser({ userId, accessHash: accessHash ?? 0n });
574
+ }
575
+ else if (channelId) {
576
+ const accessHash = await this.storage.getChannelAccessHash(channelId);
577
+ return new types.InputPeerChannel({ channelId, accessHash: accessHash ?? 0n });
578
+ }
579
+ else {
580
+ throw new Error("Unreachable");
581
+ }
582
+ }
583
+ }
584
+ else if (id > 0) {
585
+ const id_ = BigInt(id);
586
+ const accessHash = await this.storage.getUserAccessHash(id_);
587
+ return new types.InputPeerUser({ userId: id_, accessHash: accessHash ?? 0n });
588
+ }
589
+ else if (-MAX_CHAT_ID <= id) {
590
+ return new types.InputPeerChat({ chatId: BigInt(Math.abs(id)) });
591
+ }
592
+ else if (ZERO_CHANNEL_ID - MAX_CHANNEL_ID <= id && id != ZERO_CHANNEL_ID) {
593
+ const id_ = BigInt(Math.abs(id - ZERO_CHANNEL_ID));
594
+ const accessHash = await this.storage.getChannelAccessHash(id_);
595
+ return new types.InputPeerChannel({ channelId: id_, accessHash: accessHash ?? 0n });
596
+ }
597
+ else {
598
+ throw new Error("ID format unknown or not implemented");
599
+ }
600
+ }
601
+ async sendMessage(chatId, text, params) {
602
+ const entities_ = params?.entities ?? [];
603
+ const parseMode = params?.parseMode ?? this.parseMode;
604
+ switch (parseMode) {
605
+ case ParseMode.None:
606
+ break;
607
+ case ParseMode.HTML: {
608
+ const [newText, entitiesToPush] = parseHtml(text);
609
+ text = newText;
610
+ for (const entity of entitiesToPush) {
611
+ entities_.push(entity);
612
+ }
613
+ }
614
+ }
615
+ const peer = await this.getInputPeer(chatId);
616
+ const randomId = getRandomId();
617
+ const message = text;
618
+ const noWebpage = params?.disableWebPagePreview ? true : undefined;
619
+ const silent = params?.disableNotification ? true : undefined;
620
+ const noforwards = params?.protectContent ? true : undefined;
621
+ const replyToMsgId = params?.replyToMessageId;
622
+ const topMsgId = params?.messageThreadId;
623
+ const sendAs = params?.sendAs ? await this.getInputPeer(params.sendAs) : undefined;
624
+ const entities = entities_?.length > 0 ? entities_.map((v) => messageEntityToTlObject(v)) : undefined;
625
+ return await this.invoke(new functions.MessagesSendMessage({
626
+ peer,
627
+ randomId,
628
+ message,
629
+ noWebpage,
630
+ silent,
631
+ noforwards,
632
+ replyToMsgId,
633
+ topMsgId,
634
+ sendAs,
635
+ entities,
636
+ }));
637
+ }
475
638
  }
@@ -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;
package/esm/constants.js CHANGED
@@ -68,3 +68,7 @@ export const DEFAULT_LANG_CODE = "en";
68
68
  export const DEFAULT_LANG_PACK = "";
69
69
  export const DEFAULT_SYSTEM_LANG_CODE = "en";
70
70
  export const DEFAULT_SYSTEM_VERSION = "1.0";
71
+ export const USERNAME_TTL = 86400;
72
+ export const MAX_CHAT_ID = 999999999999;
73
+ export const MAX_CHANNEL_ID = 997852516352;
74
+ export const ZERO_CHANNEL_ID = -1000000000000;
@@ -0,0 +1,83 @@
1
+ import { Node, Element, Document, NodeWithChildren, DataNode } from './Node.js';
2
+ export interface DomHandlerOptions {
3
+ /**
4
+ * Add a `startIndex` property to nodes.
5
+ * When the parser is used in a non-streaming fashion, `startIndex` is an integer
6
+ * indicating the position of the start of the node in the document.
7
+ *
8
+ * @default false
9
+ */
10
+ withStartIndices?: boolean;
11
+ /**
12
+ * Add an `endIndex` property to nodes.
13
+ * When the parser is used in a non-streaming fashion, `endIndex` is an integer
14
+ * indicating the position of the end of the node in the document.
15
+ *
16
+ * @default false
17
+ */
18
+ withEndIndices?: boolean;
19
+ /**
20
+ * Replace all whitespace with single spaces.
21
+ *
22
+ * **Note:** Enabling this might break your markup.
23
+ *
24
+ * @default false
25
+ * @deprecated
26
+ */
27
+ normalizeWhitespace?: boolean;
28
+ /**
29
+ * Treat the markup as XML.
30
+ *
31
+ * @default false
32
+ */
33
+ xmlMode?: boolean;
34
+ }
35
+ interface ParserInterface {
36
+ startIndex: number | null;
37
+ endIndex: number | null;
38
+ }
39
+ type Callback = (error: Error | null, dom: Node[]) => void;
40
+ type ElementCallback = (element: Element) => void;
41
+ export declare class DomHandler {
42
+ /** The elements of the DOM */
43
+ dom: Node[];
44
+ /** The root element for the DOM */
45
+ root: Document;
46
+ /** Called once parsing has completed. */
47
+ private readonly callback;
48
+ /** Settings for the handler. */
49
+ private readonly options;
50
+ /** Callback whenever a tag is closed. */
51
+ private readonly elementCB;
52
+ /** Indicated whether parsing has been completed. */
53
+ private done;
54
+ /** Stack of open tags. */
55
+ protected tagStack: NodeWithChildren[];
56
+ /** A data node that is still being written to. */
57
+ protected lastNode: DataNode | null;
58
+ /** Reference to the parser instance. Used for location information. */
59
+ private parser;
60
+ /**
61
+ * @param callback Called once parsing has completed.
62
+ * @param options Settings for the handler.
63
+ * @param elementCB Callback whenever a tag is closed.
64
+ */
65
+ constructor(callback?: Callback | null, options?: DomHandlerOptions | null, elementCB?: ElementCallback);
66
+ onparserinit(parser: ParserInterface): void;
67
+ onreset(): void;
68
+ onend(): void;
69
+ onerror(error: Error): void;
70
+ onclosetag(): void;
71
+ onopentag(name: string, attribs: {
72
+ [key: string]: string;
73
+ }): void;
74
+ ontext(data: string): void;
75
+ oncomment(data: string): void;
76
+ oncommentend(): void;
77
+ oncdatastart(): void;
78
+ oncdataend(): void;
79
+ onprocessinginstruction(name: string, data: string): void;
80
+ protected handleCallback(error: Error | null): void;
81
+ protected addNode(node: Node): void;
82
+ }
83
+ export default DomHandler;
@@ -0,0 +1,203 @@
1
+ import { Element, Document, NodeWithChildren, Comment, Text, ProcessingInstruction, } from './Node.js';
2
+ import { ElementType } from './ElementType.js';
3
+ const reWhitespace = /\s+/g;
4
+ // Default options
5
+ const defaultOpts = {
6
+ normalizeWhitespace: false,
7
+ withStartIndices: false,
8
+ withEndIndices: false,
9
+ };
10
+ export class DomHandler {
11
+ /**
12
+ * @param callback Called once parsing has completed.
13
+ * @param options Settings for the handler.
14
+ * @param elementCB Callback whenever a tag is closed.
15
+ */
16
+ constructor(callback, options, elementCB) {
17
+ /** The elements of the DOM */
18
+ Object.defineProperty(this, "dom", {
19
+ enumerable: true,
20
+ configurable: true,
21
+ writable: true,
22
+ value: []
23
+ });
24
+ /** The root element for the DOM */
25
+ Object.defineProperty(this, "root", {
26
+ enumerable: true,
27
+ configurable: true,
28
+ writable: true,
29
+ value: new Document(this.dom)
30
+ });
31
+ /** Called once parsing has completed. */
32
+ Object.defineProperty(this, "callback", {
33
+ enumerable: true,
34
+ configurable: true,
35
+ writable: true,
36
+ value: void 0
37
+ });
38
+ /** Settings for the handler. */
39
+ Object.defineProperty(this, "options", {
40
+ enumerable: true,
41
+ configurable: true,
42
+ writable: true,
43
+ value: void 0
44
+ });
45
+ /** Callback whenever a tag is closed. */
46
+ Object.defineProperty(this, "elementCB", {
47
+ enumerable: true,
48
+ configurable: true,
49
+ writable: true,
50
+ value: void 0
51
+ });
52
+ /** Indicated whether parsing has been completed. */
53
+ Object.defineProperty(this, "done", {
54
+ enumerable: true,
55
+ configurable: true,
56
+ writable: true,
57
+ value: false
58
+ });
59
+ /** Stack of open tags. */
60
+ Object.defineProperty(this, "tagStack", {
61
+ enumerable: true,
62
+ configurable: true,
63
+ writable: true,
64
+ value: [this.root]
65
+ });
66
+ /** A data node that is still being written to. */
67
+ Object.defineProperty(this, "lastNode", {
68
+ enumerable: true,
69
+ configurable: true,
70
+ writable: true,
71
+ value: null
72
+ });
73
+ /** Reference to the parser instance. Used for location information. */
74
+ Object.defineProperty(this, "parser", {
75
+ enumerable: true,
76
+ configurable: true,
77
+ writable: true,
78
+ value: null
79
+ });
80
+ // Make it possible to skip arguments, for backwards-compatibility
81
+ if (typeof options === 'function') {
82
+ elementCB = options;
83
+ options = defaultOpts;
84
+ }
85
+ if (typeof callback === 'object') {
86
+ options = callback;
87
+ callback = undefined;
88
+ }
89
+ this.callback = callback ?? null;
90
+ this.options = options ?? defaultOpts;
91
+ this.elementCB = elementCB ?? null;
92
+ }
93
+ onparserinit(parser) {
94
+ this.parser = parser;
95
+ }
96
+ // Resets the handler back to starting state
97
+ onreset() {
98
+ this.dom = [];
99
+ this.root = new Document(this.dom);
100
+ this.done = false;
101
+ this.tagStack = [this.root];
102
+ this.lastNode = null;
103
+ this.parser = this.parser ?? null;
104
+ }
105
+ // Signals the handler that parsing is done
106
+ onend() {
107
+ if (this.done)
108
+ return;
109
+ this.done = true;
110
+ this.parser = null;
111
+ this.handleCallback(null);
112
+ }
113
+ onerror(error) {
114
+ this.handleCallback(error);
115
+ }
116
+ onclosetag() {
117
+ this.lastNode = null;
118
+ const elem = this.tagStack.pop();
119
+ if (this.options.withEndIndices) {
120
+ elem.endIndex = this.parser.endIndex;
121
+ }
122
+ if (this.elementCB)
123
+ this.elementCB(elem);
124
+ }
125
+ onopentag(name, attribs) {
126
+ const type = this.options.xmlMode ? ElementType.Tag : undefined;
127
+ const element = new Element(name, attribs, undefined, type);
128
+ this.addNode(element);
129
+ this.tagStack.push(element);
130
+ }
131
+ ontext(data) {
132
+ const { normalizeWhitespace } = this.options;
133
+ const { lastNode } = this;
134
+ if (lastNode && lastNode.type === ElementType.Text) {
135
+ if (normalizeWhitespace) {
136
+ lastNode.data = (lastNode.data + data).replace(reWhitespace, ' ');
137
+ }
138
+ else {
139
+ lastNode.data += data;
140
+ }
141
+ }
142
+ else {
143
+ if (normalizeWhitespace) {
144
+ data = data.replace(reWhitespace, ' ');
145
+ }
146
+ const node = new Text(data);
147
+ this.addNode(node);
148
+ this.lastNode = node;
149
+ }
150
+ }
151
+ oncomment(data) {
152
+ if (this.lastNode && this.lastNode.type === ElementType.Comment) {
153
+ this.lastNode.data += data;
154
+ return;
155
+ }
156
+ const node = new Comment(data);
157
+ this.addNode(node);
158
+ this.lastNode = node;
159
+ }
160
+ oncommentend() {
161
+ this.lastNode = null;
162
+ }
163
+ oncdatastart() {
164
+ const text = new Text('');
165
+ const node = new NodeWithChildren(ElementType.CDATA, [text]);
166
+ this.addNode(node);
167
+ text.parent = node;
168
+ this.lastNode = text;
169
+ }
170
+ oncdataend() {
171
+ this.lastNode = null;
172
+ }
173
+ onprocessinginstruction(name, data) {
174
+ const node = new ProcessingInstruction(name, data);
175
+ this.addNode(node);
176
+ }
177
+ handleCallback(error) {
178
+ if (typeof this.callback === 'function') {
179
+ this.callback(error, this.dom);
180
+ }
181
+ else if (error) {
182
+ throw error;
183
+ }
184
+ }
185
+ addNode(node) {
186
+ const parent = this.tagStack[this.tagStack.length - 1];
187
+ const previousSibling = parent.children[parent.children.length - 1];
188
+ if (this.options.withStartIndices) {
189
+ node.startIndex = this.parser.startIndex;
190
+ }
191
+ if (this.options.withEndIndices) {
192
+ node.endIndex = this.parser.endIndex;
193
+ }
194
+ parent.children.push(node);
195
+ if (previousSibling) {
196
+ node.prev = previousSibling;
197
+ previousSibling.next = node;
198
+ }
199
+ node.parent = parent;
200
+ this.lastNode = null;
201
+ }
202
+ }
203
+ export default DomHandler;
@@ -0,0 +1,50 @@
1
+ import type { Node } from './Node.js';
2
+ /**
3
+ * Mixed-case SVG and MathML tags & attributes
4
+ * recognized by the HTML parser.
5
+ *
6
+ * @see https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-inforeign
7
+ */
8
+ export declare const elementNames: Map<string, string>;
9
+ export declare const attributeNames: Map<string, string>;
10
+ export interface DomSerializerOptions {
11
+ /**
12
+ * Print an empty attribute's value.
13
+ *
14
+ * @default xmlMode
15
+ * @example With <code>emptyAttrs: false</code>: <code>&lt;input checked&gt;</code>
16
+ * @example With <code>emptyAttrs: true</code>: <code>&lt;input checked=""&gt;</code>
17
+ */
18
+ emptyAttrs?: boolean;
19
+ /**
20
+ * Print self-closing tags for tags without contents.
21
+ *
22
+ * @default xmlMode
23
+ * @example With <code>selfClosingTags: false</code>: <code>&lt;foo&gt;&lt;/foo&gt;</code>
24
+ * @example With <code>selfClosingTags: true</code>: <code>&lt;foo /&gt;</code>
25
+ */
26
+ selfClosingTags?: boolean;
27
+ /**
28
+ * Treat the input as an XML document; enables the `emptyAttrs` and `selfClosingTags` options.
29
+ *
30
+ * If the value is `"foreign"`, it will try to correct mixed-case attribute names.
31
+ *
32
+ * @default false
33
+ */
34
+ xmlMode?: boolean | 'foreign';
35
+ /**
36
+ * Encode characters that are either reserved in HTML or XML, or are outside of the ASCII range.
37
+ *
38
+ * @default true
39
+ */
40
+ decodeEntities?: boolean;
41
+ }
42
+ /**
43
+ * Renders a DOM node or an array of DOM nodes to a string.
44
+ *
45
+ * Can be thought of as the equivalent of the `outerHTML` of the passed node(s).
46
+ *
47
+ * @param node Node to be rendered.
48
+ * @param options Changes serialization behavior
49
+ */
50
+ export default function render(node: Node | ArrayLike<Node>, options?: DomSerializerOptions): string;