@mtkruto/node 0.0.830 → 0.0.831

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 (117) 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 +1 -0
  52. package/esm/deps.js +1 -0
  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 +1 -0
  111. package/script/deps.js +3 -1
  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
@@ -23,12 +23,14 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.Client = exports.restartAuth = void 0;
26
+ exports.Client = exports.ParseMode = exports.restartAuth = void 0;
27
27
  const deps_js_1 = require("../deps.js");
28
28
  const constants_js_1 = require("../constants.js");
29
29
  const _0_bigint_js_1 = require("../utilities/0_bigint.js");
30
+ const _0_html_js_1 = require("../utilities/0_html.js");
30
31
  const _1_message_js_1 = require("../utilities/1_message.js");
31
32
  const _1_password_js_1 = require("../utilities/1_password.js");
33
+ const _1_tl_object_js_1 = require("../tl/1_tl_object.js");
32
34
  const types = __importStar(require("../tl/2_types.js"));
33
35
  const functions = __importStar(require("../tl/3_functions.js"));
34
36
  const _3_tl_reader_js_1 = require("../tl/3_tl_reader.js");
@@ -39,7 +41,13 @@ const client_abstract_js_1 = require("./client_abstract.js");
39
41
  const client_plain_js_1 = require("./client_plain.js");
40
42
  const storage_memory_js_1 = require("../storage/storage_memory.js");
41
43
  const _0_hash_js_1 = require("../utilities/0_hash.js");
44
+ const _0_message_entity_js_1 = require("../types/0_message_entity.js");
42
45
  exports.restartAuth = Symbol();
46
+ var ParseMode;
47
+ (function (ParseMode) {
48
+ ParseMode["None"] = "none";
49
+ ParseMode["HTML"] = "html";
50
+ })(ParseMode = exports.ParseMode || (exports.ParseMode = {}));
43
51
  class Client extends client_abstract_js_1.ClientAbstract {
44
52
  /**
45
53
  * Constructs the client.
@@ -105,6 +113,12 @@ class Client extends client_abstract_js_1.ClientAbstract {
105
113
  writable: true,
106
114
  value: null
107
115
  });
116
+ Object.defineProperty(this, "parseMode", {
117
+ enumerable: true,
118
+ configurable: true,
119
+ writable: true,
120
+ value: void 0
121
+ });
108
122
  Object.defineProperty(this, "appVersion", {
109
123
  enumerable: true,
110
124
  configurable: true,
@@ -147,6 +161,7 @@ class Client extends client_abstract_js_1.ClientAbstract {
147
161
  writable: true,
148
162
  value: false
149
163
  });
164
+ this.parseMode = params?.parseMode ?? ParseMode.None;
150
165
  this.appVersion = params?.appVersion ?? constants_js_1.DEFAULT_APP_VERSION;
151
166
  this.deviceModel = params?.deviceModel ?? constants_js_1.DEFAULT_DEVICE_MODEL;
152
167
  this.langCode = params?.langCode ?? constants_js_1.DEFAULT_LANG_CODE;
@@ -417,7 +432,7 @@ class Client extends client_abstract_js_1.ClientAbstract {
417
432
  }
418
433
  // logger().debug(`Received ${body.constructor.name}`);
419
434
  if (body instanceof types.Updates) {
420
- this.updatesHandler?.(this, body);
435
+ this.processUpdates(body);
421
436
  }
422
437
  else if (message.body instanceof _4_rpc_result_js_1.RPCResult) {
423
438
  let result = message.body.result;
@@ -430,6 +445,10 @@ class Client extends client_abstract_js_1.ClientAbstract {
430
445
  promise.reject(result);
431
446
  }
432
447
  else {
448
+ if (result instanceof types.Updates) {
449
+ await this.processChats(result.chats);
450
+ await this.processUsers(result.users);
451
+ }
433
452
  promise.resolve(result);
434
453
  }
435
454
  this.promises.delete(message.body.messageId);
@@ -498,5 +517,149 @@ class Client extends client_abstract_js_1.ClientAbstract {
498
517
  send(function_) {
499
518
  return this.invoke(function_, true);
500
519
  }
520
+ async processChats(chats) {
521
+ for (const chat of chats) {
522
+ if (chat instanceof types.Channel && chat.accessHash) {
523
+ await this.storage.setChannelAccessHash(chat.id, chat.accessHash);
524
+ if (chat.username) {
525
+ await this.storage.updateUsernames("channel", chat.id, [chat.username]);
526
+ }
527
+ if (chat.usernames) {
528
+ await this.storage.updateUsernames("channel", chat.id, chat.usernames.map((v) => v[_1_tl_object_js_1.as](types.Username)).map((v) => v.username));
529
+ }
530
+ }
531
+ }
532
+ }
533
+ async processUsers(users) {
534
+ for (const user of users) {
535
+ if (user instanceof types.User && user.accessHash) {
536
+ await this.storage.setUserAccessHash(user.id, user.accessHash);
537
+ if (user.username) {
538
+ await this.storage.updateUsernames("user", user.id, [user.username]);
539
+ }
540
+ if (user.usernames) {
541
+ await this.storage.updateUsernames("user", user.id, user.usernames.map((v) => v[_1_tl_object_js_1.as](types.Username)).map((v) => v.username));
542
+ }
543
+ }
544
+ }
545
+ }
546
+ async processUpdates(updates) {
547
+ try {
548
+ await this.processChats(updates.chats);
549
+ await this.processUsers(updates.users);
550
+ for (const update of updates.updates) {
551
+ if (update instanceof types.UpdateUserName) {
552
+ await this.storage.updateUsernames("user", update.userId, update.usernames.map((v) => v[_1_tl_object_js_1.as](types.Username)).map((v) => v.username));
553
+ }
554
+ }
555
+ await this.updatesHandler?.(this, updates);
556
+ }
557
+ catch (err) {
558
+ console.error("Error processing updates:", err);
559
+ }
560
+ }
561
+ async getInputPeer(id) {
562
+ if (typeof id === "string") {
563
+ if (!id.startsWith("@")) {
564
+ throw new Error("Expected username to start with @");
565
+ }
566
+ else {
567
+ id = id.slice(1);
568
+ if (!id) {
569
+ throw new Error("Empty username");
570
+ }
571
+ let userId = 0n;
572
+ let channelId = 0n;
573
+ const maybeUsername = await this.storage.getUsername(id);
574
+ if (maybeUsername != null && Date.now() - maybeUsername[2].getTime() < constants_js_1.USERNAME_TTL) {
575
+ const [type, id] = maybeUsername;
576
+ if (type == "user") {
577
+ userId = id;
578
+ }
579
+ else {
580
+ channelId = id;
581
+ }
582
+ }
583
+ else {
584
+ const resolved = await this.invoke(new functions.ContactsResolveUsername({ username: id }));
585
+ await this.processChats(resolved.chats);
586
+ await this.processUsers(resolved.users);
587
+ if (resolved.peer instanceof types.PeerUser) {
588
+ userId = resolved.peer.userId;
589
+ }
590
+ else if (resolved.peer instanceof types.PeerChannel) {
591
+ channelId = resolved.peer.channelId;
592
+ }
593
+ else {
594
+ throw new Error("Unreachable");
595
+ }
596
+ }
597
+ if (userId) {
598
+ const accessHash = await this.storage.getUserAccessHash(userId);
599
+ return new types.InputPeerUser({ userId, accessHash: accessHash ?? 0n });
600
+ }
601
+ else if (channelId) {
602
+ const accessHash = await this.storage.getChannelAccessHash(channelId);
603
+ return new types.InputPeerChannel({ channelId, accessHash: accessHash ?? 0n });
604
+ }
605
+ else {
606
+ throw new Error("Unreachable");
607
+ }
608
+ }
609
+ }
610
+ else if (id > 0) {
611
+ const id_ = BigInt(id);
612
+ const accessHash = await this.storage.getUserAccessHash(id_);
613
+ return new types.InputPeerUser({ userId: id_, accessHash: accessHash ?? 0n });
614
+ }
615
+ else if (-constants_js_1.MAX_CHAT_ID <= id) {
616
+ return new types.InputPeerChat({ chatId: BigInt(Math.abs(id)) });
617
+ }
618
+ else if (constants_js_1.ZERO_CHANNEL_ID - constants_js_1.MAX_CHANNEL_ID <= id && id != constants_js_1.ZERO_CHANNEL_ID) {
619
+ const id_ = BigInt(Math.abs(id - constants_js_1.ZERO_CHANNEL_ID));
620
+ const accessHash = await this.storage.getChannelAccessHash(id_);
621
+ return new types.InputPeerChannel({ channelId: id_, accessHash: accessHash ?? 0n });
622
+ }
623
+ else {
624
+ throw new Error("ID format unknown or not implemented");
625
+ }
626
+ }
627
+ async sendMessage(chatId, text, params) {
628
+ const entities_ = params?.entities ?? [];
629
+ const parseMode = params?.parseMode ?? this.parseMode;
630
+ switch (parseMode) {
631
+ case ParseMode.None:
632
+ break;
633
+ case ParseMode.HTML: {
634
+ const [newText, entitiesToPush] = (0, _0_html_js_1.parseHtml)(text);
635
+ text = newText;
636
+ for (const entity of entitiesToPush) {
637
+ entities_.push(entity);
638
+ }
639
+ }
640
+ }
641
+ const peer = await this.getInputPeer(chatId);
642
+ const randomId = (0, _0_bigint_js_1.getRandomId)();
643
+ const message = text;
644
+ const noWebpage = params?.disableWebPagePreview ? true : undefined;
645
+ const silent = params?.disableNotification ? true : undefined;
646
+ const noforwards = params?.protectContent ? true : undefined;
647
+ const replyToMsgId = params?.replyToMessageId;
648
+ const topMsgId = params?.messageThreadId;
649
+ const sendAs = params?.sendAs ? await this.getInputPeer(params.sendAs) : undefined;
650
+ const entities = entities_?.length > 0 ? entities_.map((v) => (0, _0_message_entity_js_1.messageEntityToTlObject)(v)) : undefined;
651
+ return await this.invoke(new functions.MessagesSendMessage({
652
+ peer,
653
+ randomId,
654
+ message,
655
+ noWebpage,
656
+ silent,
657
+ noforwards,
658
+ replyToMsgId,
659
+ topMsgId,
660
+ sendAs,
661
+ entities,
662
+ }));
663
+ }
501
664
  }
502
665
  exports.Client = Client;
@@ -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;
@@ -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,207 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DomHandler = void 0;
4
+ const Node_js_1 = require("./Node.js");
5
+ const ElementType_js_1 = require("./ElementType.js");
6
+ const reWhitespace = /\s+/g;
7
+ // Default options
8
+ const defaultOpts = {
9
+ normalizeWhitespace: false,
10
+ withStartIndices: false,
11
+ withEndIndices: false,
12
+ };
13
+ class DomHandler {
14
+ /**
15
+ * @param callback Called once parsing has completed.
16
+ * @param options Settings for the handler.
17
+ * @param elementCB Callback whenever a tag is closed.
18
+ */
19
+ constructor(callback, options, elementCB) {
20
+ /** The elements of the DOM */
21
+ Object.defineProperty(this, "dom", {
22
+ enumerable: true,
23
+ configurable: true,
24
+ writable: true,
25
+ value: []
26
+ });
27
+ /** The root element for the DOM */
28
+ Object.defineProperty(this, "root", {
29
+ enumerable: true,
30
+ configurable: true,
31
+ writable: true,
32
+ value: new Node_js_1.Document(this.dom)
33
+ });
34
+ /** Called once parsing has completed. */
35
+ Object.defineProperty(this, "callback", {
36
+ enumerable: true,
37
+ configurable: true,
38
+ writable: true,
39
+ value: void 0
40
+ });
41
+ /** Settings for the handler. */
42
+ Object.defineProperty(this, "options", {
43
+ enumerable: true,
44
+ configurable: true,
45
+ writable: true,
46
+ value: void 0
47
+ });
48
+ /** Callback whenever a tag is closed. */
49
+ Object.defineProperty(this, "elementCB", {
50
+ enumerable: true,
51
+ configurable: true,
52
+ writable: true,
53
+ value: void 0
54
+ });
55
+ /** Indicated whether parsing has been completed. */
56
+ Object.defineProperty(this, "done", {
57
+ enumerable: true,
58
+ configurable: true,
59
+ writable: true,
60
+ value: false
61
+ });
62
+ /** Stack of open tags. */
63
+ Object.defineProperty(this, "tagStack", {
64
+ enumerable: true,
65
+ configurable: true,
66
+ writable: true,
67
+ value: [this.root]
68
+ });
69
+ /** A data node that is still being written to. */
70
+ Object.defineProperty(this, "lastNode", {
71
+ enumerable: true,
72
+ configurable: true,
73
+ writable: true,
74
+ value: null
75
+ });
76
+ /** Reference to the parser instance. Used for location information. */
77
+ Object.defineProperty(this, "parser", {
78
+ enumerable: true,
79
+ configurable: true,
80
+ writable: true,
81
+ value: null
82
+ });
83
+ // Make it possible to skip arguments, for backwards-compatibility
84
+ if (typeof options === 'function') {
85
+ elementCB = options;
86
+ options = defaultOpts;
87
+ }
88
+ if (typeof callback === 'object') {
89
+ options = callback;
90
+ callback = undefined;
91
+ }
92
+ this.callback = callback ?? null;
93
+ this.options = options ?? defaultOpts;
94
+ this.elementCB = elementCB ?? null;
95
+ }
96
+ onparserinit(parser) {
97
+ this.parser = parser;
98
+ }
99
+ // Resets the handler back to starting state
100
+ onreset() {
101
+ this.dom = [];
102
+ this.root = new Node_js_1.Document(this.dom);
103
+ this.done = false;
104
+ this.tagStack = [this.root];
105
+ this.lastNode = null;
106
+ this.parser = this.parser ?? null;
107
+ }
108
+ // Signals the handler that parsing is done
109
+ onend() {
110
+ if (this.done)
111
+ return;
112
+ this.done = true;
113
+ this.parser = null;
114
+ this.handleCallback(null);
115
+ }
116
+ onerror(error) {
117
+ this.handleCallback(error);
118
+ }
119
+ onclosetag() {
120
+ this.lastNode = null;
121
+ const elem = this.tagStack.pop();
122
+ if (this.options.withEndIndices) {
123
+ elem.endIndex = this.parser.endIndex;
124
+ }
125
+ if (this.elementCB)
126
+ this.elementCB(elem);
127
+ }
128
+ onopentag(name, attribs) {
129
+ const type = this.options.xmlMode ? ElementType_js_1.ElementType.Tag : undefined;
130
+ const element = new Node_js_1.Element(name, attribs, undefined, type);
131
+ this.addNode(element);
132
+ this.tagStack.push(element);
133
+ }
134
+ ontext(data) {
135
+ const { normalizeWhitespace } = this.options;
136
+ const { lastNode } = this;
137
+ if (lastNode && lastNode.type === ElementType_js_1.ElementType.Text) {
138
+ if (normalizeWhitespace) {
139
+ lastNode.data = (lastNode.data + data).replace(reWhitespace, ' ');
140
+ }
141
+ else {
142
+ lastNode.data += data;
143
+ }
144
+ }
145
+ else {
146
+ if (normalizeWhitespace) {
147
+ data = data.replace(reWhitespace, ' ');
148
+ }
149
+ const node = new Node_js_1.Text(data);
150
+ this.addNode(node);
151
+ this.lastNode = node;
152
+ }
153
+ }
154
+ oncomment(data) {
155
+ if (this.lastNode && this.lastNode.type === ElementType_js_1.ElementType.Comment) {
156
+ this.lastNode.data += data;
157
+ return;
158
+ }
159
+ const node = new Node_js_1.Comment(data);
160
+ this.addNode(node);
161
+ this.lastNode = node;
162
+ }
163
+ oncommentend() {
164
+ this.lastNode = null;
165
+ }
166
+ oncdatastart() {
167
+ const text = new Node_js_1.Text('');
168
+ const node = new Node_js_1.NodeWithChildren(ElementType_js_1.ElementType.CDATA, [text]);
169
+ this.addNode(node);
170
+ text.parent = node;
171
+ this.lastNode = text;
172
+ }
173
+ oncdataend() {
174
+ this.lastNode = null;
175
+ }
176
+ onprocessinginstruction(name, data) {
177
+ const node = new Node_js_1.ProcessingInstruction(name, data);
178
+ this.addNode(node);
179
+ }
180
+ handleCallback(error) {
181
+ if (typeof this.callback === 'function') {
182
+ this.callback(error, this.dom);
183
+ }
184
+ else if (error) {
185
+ throw error;
186
+ }
187
+ }
188
+ addNode(node) {
189
+ const parent = this.tagStack[this.tagStack.length - 1];
190
+ const previousSibling = parent.children[parent.children.length - 1];
191
+ if (this.options.withStartIndices) {
192
+ node.startIndex = this.parser.startIndex;
193
+ }
194
+ if (this.options.withEndIndices) {
195
+ node.endIndex = this.parser.endIndex;
196
+ }
197
+ parent.children.push(node);
198
+ if (previousSibling) {
199
+ node.prev = previousSibling;
200
+ previousSibling.next = node;
201
+ }
202
+ node.parent = parent;
203
+ this.lastNode = null;
204
+ }
205
+ }
206
+ exports.DomHandler = DomHandler;
207
+ exports.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;