@wabot-dev/framework 0.1.0-beta.9 → 0.2.0-beta.1

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 (212) hide show
  1. package/dist/src/addon/async/pg/PgJobRepository.js +26 -0
  2. package/dist/src/addon/auth/api-key/@apiKeyConnectionGuard.js +16 -0
  3. package/dist/src/addon/auth/api-key/@apiKeyGuard.js +17 -0
  4. package/dist/src/addon/auth/api-key/ApiKey.js +45 -0
  5. package/dist/src/addon/auth/api-key/ApiKeyConnectionGuardMiddleware.js +57 -0
  6. package/dist/src/addon/auth/api-key/ApiKeyGuardMiddleware.js +45 -0
  7. package/dist/src/addon/auth/api-key/ApiKeyRepository.js +22 -0
  8. package/dist/src/addon/auth/api-key/PgApiKeyRepository.js +53 -0
  9. package/dist/src/addon/auth/api-key/RemoteApiKeyRepository.js +62 -0
  10. package/dist/src/addon/auth/jwt/@jwtConnectionGuard.js +16 -0
  11. package/dist/src/addon/auth/jwt/@jwtGuard.js +17 -0
  12. package/dist/src/addon/auth/jwt/Jwt.js +53 -0
  13. package/dist/src/addon/auth/jwt/JwtAccessAndRefreshTokenDto.js +20 -0
  14. package/dist/src/addon/auth/jwt/JwtConfig.js +28 -0
  15. package/dist/src/addon/auth/jwt/JwtConnectionGuardMiddleware.js +57 -0
  16. package/dist/src/addon/auth/jwt/JwtGuardMiddleware.js +45 -0
  17. package/dist/src/addon/auth/jwt/JwtRefreshToken.js +56 -0
  18. package/dist/src/addon/auth/jwt/JwtRefreshTokenRepository.js +25 -0
  19. package/dist/src/addon/auth/jwt/JwtSigner.js +36 -0
  20. package/dist/src/addon/auth/jwt/JwtTokenDto.js +22 -0
  21. package/dist/src/addon/auth/jwt/PgJwtRefreshTokenRepository.js +21 -0
  22. package/dist/src/addon/chat-bot/anthropic/AnthropicChatAdapter.js +135 -0
  23. package/dist/src/addon/chat-bot/deepseek/DeepSeekChatAdapter.js +137 -0
  24. package/dist/src/addon/chat-bot/google/GoogleChatAdapter.js +128 -0
  25. package/dist/src/addon/chat-bot/openia/OpenaiChatAdapter.js +117 -0
  26. package/dist/src/{pre-made/repository/chat → addon/chat-bot}/pg/PgChatMemory.js +6 -4
  27. package/dist/src/{pre-made/repository/chat → addon/chat-bot}/pg/PgChatRepository.js +6 -5
  28. package/dist/src/{pre-made/repository/chat → addon/chat-bot}/ram/RamChatRepository.js +2 -2
  29. package/dist/src/addon/chat-bot/wabot/WabotChatAdapter.js +41 -0
  30. package/dist/src/addon/chat-controller/cmd/@cmd.js +24 -0
  31. package/dist/src/addon/chat-controller/cmd/CmdChannel.js +91 -0
  32. package/dist/src/{channels → addon/chat-controller}/socket/@socket.js +10 -4
  33. package/dist/src/{channels → addon/chat-controller}/socket/SocketChannel.js +9 -23
  34. package/dist/src/{channels → addon/chat-controller}/socket/SocketChannelConfig.js +1 -1
  35. package/dist/src/{channels → addon/chat-controller}/telegram/@telegram.js +10 -4
  36. package/dist/src/{channels → addon/chat-controller}/telegram/TelegramChannel.js +4 -22
  37. package/dist/src/addon/chat-controller/whatsapp/@whatsApp.js +26 -0
  38. package/dist/src/{channels → addon/chat-controller}/whatsapp/EnvWhatsAppRepository.js +3 -3
  39. package/dist/src/{channels → addon/chat-controller}/whatsapp/PgWhatsAppRepository.js +2 -2
  40. package/dist/src/{channels → addon/chat-controller}/whatsapp/WhatsApp.js +2 -4
  41. package/dist/src/{channels → addon/chat-controller}/whatsapp/WhatsAppChannel.js +6 -18
  42. package/dist/src/addon/chat-controller/whatsapp/WhatsAppReceiver.js +10 -0
  43. package/dist/src/{channels → addon/chat-controller}/whatsapp/WhatsAppSender.js +15 -39
  44. package/dist/src/addon/chat-controller/whatsapp/cloud-api/WhatsAppReceiverByCloudApi.js +97 -0
  45. package/dist/src/{channels/whatsapp → addon/chat-controller/whatsapp/cloud-api}/WhatsAppSenderByCloudApi.js +27 -20
  46. package/dist/src/addon/chat-controller/whatsapp/proxy/WhatsAppProxyContracts.js +5 -0
  47. package/dist/src/addon/chat-controller/whatsapp/proxy/WhatsAppReceiverByWabotProxy.js +65 -0
  48. package/dist/src/addon/chat-controller/whatsapp/proxy/WhatsAppSenderByWabotProxy.js +61 -0
  49. package/dist/src/addon/chat-controller/whatsapp/proxy/WhatsAppWabotProxyConnection.js +45 -0
  50. package/dist/src/{pre-made/module → addon/mindset}/html/HtmlModule.js +7 -7
  51. package/dist/src/core/auth/Auth.js +33 -0
  52. package/dist/src/core/{Persistent.js → entity/Entity.js} +24 -12
  53. package/dist/src/core/env/Env.js +39 -0
  54. package/dist/src/core/error/CustomError.js +15 -0
  55. package/dist/src/core/injection/index.js +4 -0
  56. package/dist/src/core/mapper/Mapper.js +42 -0
  57. package/dist/src/core/password/Password.js +30 -0
  58. package/dist/src/core/random/Random.js +65 -0
  59. package/dist/src/core/storable/Storable.js +8 -0
  60. package/dist/src/core/validation/core/validateArray.js +51 -0
  61. package/dist/src/core/validation/core/validateModel.js +36 -0
  62. package/dist/src/{validation/metadata/@isNumber.js → core/validation/metadata/@isArray.js} +5 -4
  63. package/dist/src/{validation/metadata/@isDate.js → core/validation/metadata/@isModel.js} +5 -4
  64. package/dist/src/{validation → core/validation}/metadata/@isOptional.js +1 -1
  65. package/dist/src/core/validation/metadata/ValidationMetadataStore.js +98 -0
  66. package/dist/src/core/validation/modelInfo.js +9 -0
  67. package/dist/src/{validation/validateModel2.js → core/validation/validate.js} +3 -3
  68. package/dist/src/{validation/metadata → core/validation/validators/is-boolean}/@isBoolean.js +3 -3
  69. package/dist/src/core/validation/validators/is-date/@isDate.js +17 -0
  70. package/dist/src/core/validation/validators/is-in/@isIn.js +18 -0
  71. package/dist/src/core/validation/validators/is-in/validateIsIn.js +12 -0
  72. package/dist/src/{validation/metadata → core/validation/validators/is-not-empty}/@isNotEmpty.js +3 -3
  73. package/dist/src/core/validation/validators/is-number/@isNumber.js +17 -0
  74. package/dist/src/{validation/metadata → core/validation/validators/is-present}/@isPresent.js +3 -3
  75. package/dist/src/{validation/metadata → core/validation/validators/is-string}/@isString.js +3 -3
  76. package/dist/src/{validation/metadata → core/validation/validators/max}/@max.js +3 -3
  77. package/dist/src/{validation/metadata → core/validation/validators/min}/@min.js +3 -3
  78. package/dist/src/feature/async/@command.js +11 -0
  79. package/dist/src/feature/async/@commandHandler.js +12 -0
  80. package/dist/src/feature/async/Async.js +38 -0
  81. package/dist/src/feature/async/Command.js +9 -0
  82. package/dist/src/feature/async/CommandMetadataStore.js +38 -0
  83. package/dist/src/feature/async/Job.js +27 -0
  84. package/dist/src/feature/async/JobRepository.js +31 -0
  85. package/dist/src/feature/async/JobRunner.js +48 -0
  86. package/dist/src/feature/async/JobsEventsHub.js +36 -0
  87. package/dist/src/feature/async/runCommandHandlers.js +29 -0
  88. package/dist/src/{core/chat → feature/chat-bot}/Chat.js +2 -2
  89. package/dist/src/feature/chat-bot/ChatAdapter.js +7 -0
  90. package/dist/src/feature/chat-bot/ChatBot.js +73 -0
  91. package/dist/src/feature/chat-bot/ChatItem.js +24 -0
  92. package/dist/src/feature/chat-bot/ChatMemory.js +10 -0
  93. package/dist/src/{core/chat/repository/IChatRepository.js → feature/chat-bot/ChatRepository.js} +2 -8
  94. package/dist/src/feature/chat-bot/IChatItem.js +3 -0
  95. package/dist/src/{chatbot → feature/chat-bot}/metadata/@chatBot.js +1 -1
  96. package/dist/src/{chatbot → feature/chat-bot}/metadata/ChatBotMetadataStore.js +1 -1
  97. package/dist/src/{controller/channel → feature/chat-controller}/ChatResolver.js +6 -4
  98. package/dist/src/{controller → feature/chat-controller}/metadata/ControllerMetadataStore.js +1 -1
  99. package/dist/src/{controller → feature/chat-controller}/metadata/controller/@chatController.js +1 -1
  100. package/dist/src/feature/chat-controller/runChatControllers.js +83 -0
  101. package/dist/src/{channels → feature}/express/ExpressProvider.js +2 -4
  102. package/dist/src/{channels → feature}/http/HttpServerProvider.js +2 -2
  103. package/dist/src/{mindset → feature/mindset}/IMindset.js +6 -0
  104. package/dist/src/feature/mindset/MindsetOperator.js +180 -0
  105. package/dist/src/{mindset → feature/mindset}/metadata/MindsetMetadataStore.js +1 -1
  106. package/dist/src/{mindset → feature/mindset}/metadata/functions/@mindsetFunction.js +1 -1
  107. package/dist/src/{mindset → feature/mindset}/metadata/mindsets/@mindset.js +1 -1
  108. package/dist/src/{mindset → feature/mindset}/metadata/modules/@mindsetModule.js +1 -2
  109. package/dist/src/{mindset → feature/mindset}/metadata/params/@param.js +1 -1
  110. package/dist/src/feature/money/Money.js +61 -0
  111. package/dist/src/feature/money/MoneyDto.js +22 -0
  112. package/dist/src/{repository → feature}/pg/PgCrudRepository.js +24 -10
  113. package/dist/src/{repository → feature}/pg/PgRepositoryBase.js +2 -2
  114. package/dist/src/feature/rest-controller/injection-tokens.js +4 -0
  115. package/dist/src/{rest-controller/metadata/@post.js → feature/rest-controller/metadata/@middleware.js} +5 -8
  116. package/dist/src/feature/rest-controller/metadata/@onDelete.js +7 -0
  117. package/dist/src/feature/rest-controller/metadata/@onGet.js +7 -0
  118. package/dist/src/feature/rest-controller/metadata/@onPost.js +7 -0
  119. package/dist/src/feature/rest-controller/metadata/@onPut.js +7 -0
  120. package/dist/src/{rest-controller → feature/rest-controller}/metadata/@restController.js +2 -2
  121. package/dist/src/{rest-controller → feature/rest-controller}/metadata/RestControllerMetadataStore.js +14 -1
  122. package/dist/src/{rest-controller/metadata/@get.js → feature/rest-controller/metadata/methodDecorator.js} +5 -5
  123. package/dist/src/feature/rest-controller/runRestControllers.js +103 -0
  124. package/dist/src/{channels → feature}/socket/SocketServerProvider.js +2 -2
  125. package/dist/src/feature/socket-controller/metadata/@connectionMiddleware.js +16 -0
  126. package/dist/src/feature/socket-controller/metadata/@socketConnection.js +18 -0
  127. package/dist/src/feature/socket-controller/metadata/@socketController.js +15 -0
  128. package/dist/src/feature/socket-controller/metadata/@socketEvent.js +18 -0
  129. package/dist/src/feature/socket-controller/metadata/SocketControllerMetadataStore.js +65 -0
  130. package/dist/src/feature/socket-controller/runSocketControllers.js +99 -0
  131. package/dist/src/index.d.ts +1219 -718
  132. package/dist/src/index.js +147 -102
  133. package/package.json +8 -2
  134. package/dist/src/_virtual/_commonjsHelpers.js +0 -5
  135. package/dist/src/_virtual/cjs.js +0 -7
  136. package/dist/src/ai/deepseek/DeepSeekChatBotAdapter.js +0 -107
  137. package/dist/src/ai/openia/OpenaiChatBotAdapter.js +0 -88
  138. package/dist/src/channels/cmd/@cmd.js +0 -18
  139. package/dist/src/channels/cmd/CmdChannel.js +0 -73
  140. package/dist/src/channels/wabot/WabotDevConnection.js +0 -57
  141. package/dist/src/channels/wabot/WabotDevSocketContracts.js +0 -10
  142. package/dist/src/channels/whatsapp/@whatsapp.js +0 -20
  143. package/dist/src/channels/whatsapp/WhatsAppReceiver.js +0 -59
  144. package/dist/src/channels/whatsapp/WhatsAppReceiverByDevConnection.js +0 -32
  145. package/dist/src/channels/whatsapp/WhatsAppReceiverByWebHook.js +0 -63
  146. package/dist/src/channels/whatsapp/WhatsAppSenderByDevConnection.js +0 -61
  147. package/dist/src/chatbot/ChatBot.js +0 -51
  148. package/dist/src/chatbot/ChatBotAdapter.js +0 -72
  149. package/dist/src/controller/channel/UserResolver.js +0 -21
  150. package/dist/src/core/IMessageContext.js +0 -12
  151. package/dist/src/core/chat/ChatItem.js +0 -15
  152. package/dist/src/core/chat/repository/IChatMemory.js +0 -10
  153. package/dist/src/core/user/IUserRepository.js +0 -19
  154. package/dist/src/core/user/User.js +0 -26
  155. package/dist/src/env/WabotEnv.js +0 -27
  156. package/dist/src/injection/index.js +0 -4
  157. package/dist/src/mindset/MindsetOperator.js +0 -101
  158. package/dist/src/node_modules/@selderee/plugin-htmlparser2/lib/hp2-builder.js +0 -90
  159. package/dist/src/node_modules/deepmerge/dist/cjs.js +0 -142
  160. package/dist/src/node_modules/dom-serializer/lib/esm/foreignNames.js +0 -102
  161. package/dist/src/node_modules/dom-serializer/lib/esm/index.js +0 -186
  162. package/dist/src/node_modules/domelementtype/lib/esm/index.js +0 -53
  163. package/dist/src/node_modules/domhandler/lib/esm/index.js +0 -148
  164. package/dist/src/node_modules/domhandler/lib/esm/node.js +0 -334
  165. package/dist/src/node_modules/entities/lib/esm/decode.js +0 -458
  166. package/dist/src/node_modules/entities/lib/esm/decode_codepoint.js +0 -62
  167. package/dist/src/node_modules/entities/lib/esm/escape.js +0 -99
  168. package/dist/src/node_modules/entities/lib/esm/generated/decode-data-html.js +0 -8
  169. package/dist/src/node_modules/entities/lib/esm/generated/decode-data-xml.js +0 -8
  170. package/dist/src/node_modules/html-to-text/lib/html-to-text.js +0 -2147
  171. package/dist/src/node_modules/htmlparser2/lib/esm/Parser.js +0 -491
  172. package/dist/src/node_modules/htmlparser2/lib/esm/Tokenizer.js +0 -928
  173. package/dist/src/node_modules/htmlparser2/lib/esm/index.js +0 -18
  174. package/dist/src/node_modules/leac/lib/leac.js +0 -3
  175. package/dist/src/node_modules/parseley/lib/parseley.js +0 -270
  176. package/dist/src/node_modules/peberminta/lib/core.js +0 -171
  177. package/dist/src/node_modules/selderee/lib/selderee.js +0 -380
  178. package/dist/src/pre-made/module/authentication/AuthenticationModule.js +0 -97
  179. package/dist/src/pre-made/module/authentication/requests/SendOneTimePasswordRequest.js +0 -25
  180. package/dist/src/pre-made/module/authentication/requests/ValidateOneTimePasswordRequest.js +0 -25
  181. package/dist/src/pre-made/module/register-user/RegisterUserModule.js +0 -56
  182. package/dist/src/pre-made/module/register-user/requests/RegisterUserWithEmailRequest.js +0 -25
  183. package/dist/src/pre-made/repository/user/pg/PgUserRepository.js +0 -33
  184. package/dist/src/pre-made/repository/user/ram/RamUserRepository.js +0 -27
  185. package/dist/src/pre-made/service/EmailService.js +0 -13
  186. package/dist/src/pre-made/service/OtpService.js +0 -14
  187. package/dist/src/rest-controller/runRestControllers.js +0 -74
  188. package/dist/src/server/prepareChatContainer.js +0 -43
  189. package/dist/src/server/runChannel.js +0 -27
  190. package/dist/src/server/runServer.js +0 -40
  191. package/dist/src/validation/metadata/@validable.js +0 -14
  192. package/dist/src/validation/metadata/ValidationMetadataStore.js +0 -55
  193. package/dist/src/validation/validators/validateModel.js +0 -47
  194. /package/dist/src/{pre-made/repository/chat → addon/chat-bot}/ram/RamChatMemory.js +0 -0
  195. /package/dist/src/{channels → addon/chat-controller}/telegram/TelegramChannelConfig.js +0 -0
  196. /package/dist/src/{channels → addon/chat-controller}/whatsapp/WhatsAppChannelConfig.js +0 -0
  197. /package/dist/src/{channels → addon/chat-controller}/whatsapp/WhatsAppRepository.js +0 -0
  198. /package/dist/src/{injection → core/injection}/Container.js +0 -0
  199. /package/dist/src/{logger → core/logger}/Logger.js +0 -0
  200. /package/dist/src/{validation/validators → core/validation/core}/validateIsOptional.js +0 -0
  201. /package/dist/src/{validation/validators → core/validation/validators/is-boolean}/validateIsBoolean.js +0 -0
  202. /package/dist/src/{validation/validators → core/validation/validators/is-date}/validateIsDate.js +0 -0
  203. /package/dist/src/{validation/validators → core/validation/validators/is-not-empty}/validateIsNotEmpty.js +0 -0
  204. /package/dist/src/{validation/validators → core/validation/validators/is-number}/validateIsNumber.js +0 -0
  205. /package/dist/src/{validation/validators → core/validation/validators/is-present}/validateIsPresent.js +0 -0
  206. /package/dist/src/{validation/validators → core/validation/validators/is-string}/validateIsString.js +0 -0
  207. /package/dist/src/{validation/validators → core/validation/validators/max}/validateMax.js +0 -0
  208. /package/dist/src/{validation/validators → core/validation/validators/min}/validateMin.js +0 -0
  209. /package/dist/src/{mindset → feature/mindset}/metadata/functions/decoratorNames.js +0 -0
  210. /package/dist/src/{mindset → feature/mindset}/metadata/mindsets/decoratorNames.js +0 -0
  211. /package/dist/src/{mindset → feature/mindset}/metadata/modules/decoratorNames.js +0 -0
  212. /package/dist/src/{mindset → feature/mindset}/metadata/params/decoratorNames.js +0 -0
@@ -1,458 +0,0 @@
1
- import htmlDecodeTree from './generated/decode-data-html.js';
2
- import xmlDecodeTree from './generated/decode-data-xml.js';
3
- import { fromCodePoint, replaceCodePoint } from './decode_codepoint.js';
4
-
5
- var CharCodes;
6
- (function (CharCodes) {
7
- CharCodes[CharCodes["NUM"] = 35] = "NUM";
8
- CharCodes[CharCodes["SEMI"] = 59] = "SEMI";
9
- CharCodes[CharCodes["EQUALS"] = 61] = "EQUALS";
10
- CharCodes[CharCodes["ZERO"] = 48] = "ZERO";
11
- CharCodes[CharCodes["NINE"] = 57] = "NINE";
12
- CharCodes[CharCodes["LOWER_A"] = 97] = "LOWER_A";
13
- CharCodes[CharCodes["LOWER_F"] = 102] = "LOWER_F";
14
- CharCodes[CharCodes["LOWER_X"] = 120] = "LOWER_X";
15
- CharCodes[CharCodes["LOWER_Z"] = 122] = "LOWER_Z";
16
- CharCodes[CharCodes["UPPER_A"] = 65] = "UPPER_A";
17
- CharCodes[CharCodes["UPPER_F"] = 70] = "UPPER_F";
18
- CharCodes[CharCodes["UPPER_Z"] = 90] = "UPPER_Z";
19
- })(CharCodes || (CharCodes = {}));
20
- /** Bit that needs to be set to convert an upper case ASCII character to lower case */
21
- const TO_LOWER_BIT = 0b100000;
22
- var BinTrieFlags;
23
- (function (BinTrieFlags) {
24
- BinTrieFlags[BinTrieFlags["VALUE_LENGTH"] = 49152] = "VALUE_LENGTH";
25
- BinTrieFlags[BinTrieFlags["BRANCH_LENGTH"] = 16256] = "BRANCH_LENGTH";
26
- BinTrieFlags[BinTrieFlags["JUMP_TABLE"] = 127] = "JUMP_TABLE";
27
- })(BinTrieFlags || (BinTrieFlags = {}));
28
- function isNumber(code) {
29
- return code >= CharCodes.ZERO && code <= CharCodes.NINE;
30
- }
31
- function isHexadecimalCharacter(code) {
32
- return ((code >= CharCodes.UPPER_A && code <= CharCodes.UPPER_F) ||
33
- (code >= CharCodes.LOWER_A && code <= CharCodes.LOWER_F));
34
- }
35
- function isAsciiAlphaNumeric(code) {
36
- return ((code >= CharCodes.UPPER_A && code <= CharCodes.UPPER_Z) ||
37
- (code >= CharCodes.LOWER_A && code <= CharCodes.LOWER_Z) ||
38
- isNumber(code));
39
- }
40
- /**
41
- * Checks if the given character is a valid end character for an entity in an attribute.
42
- *
43
- * Attribute values that aren't terminated properly aren't parsed, and shouldn't lead to a parser error.
44
- * See the example in https://html.spec.whatwg.org/multipage/parsing.html#named-character-reference-state
45
- */
46
- function isEntityInAttributeInvalidEnd(code) {
47
- return code === CharCodes.EQUALS || isAsciiAlphaNumeric(code);
48
- }
49
- var EntityDecoderState;
50
- (function (EntityDecoderState) {
51
- EntityDecoderState[EntityDecoderState["EntityStart"] = 0] = "EntityStart";
52
- EntityDecoderState[EntityDecoderState["NumericStart"] = 1] = "NumericStart";
53
- EntityDecoderState[EntityDecoderState["NumericDecimal"] = 2] = "NumericDecimal";
54
- EntityDecoderState[EntityDecoderState["NumericHex"] = 3] = "NumericHex";
55
- EntityDecoderState[EntityDecoderState["NamedEntity"] = 4] = "NamedEntity";
56
- })(EntityDecoderState || (EntityDecoderState = {}));
57
- var DecodingMode;
58
- (function (DecodingMode) {
59
- /** Entities in text nodes that can end with any character. */
60
- DecodingMode[DecodingMode["Legacy"] = 0] = "Legacy";
61
- /** Only allow entities terminated with a semicolon. */
62
- DecodingMode[DecodingMode["Strict"] = 1] = "Strict";
63
- /** Entities in attributes have limitations on ending characters. */
64
- DecodingMode[DecodingMode["Attribute"] = 2] = "Attribute";
65
- })(DecodingMode || (DecodingMode = {}));
66
- /**
67
- * Token decoder with support of writing partial entities.
68
- */
69
- class EntityDecoder {
70
- constructor(
71
- /** The tree used to decode entities. */
72
- decodeTree,
73
- /**
74
- * The function that is called when a codepoint is decoded.
75
- *
76
- * For multi-byte named entities, this will be called multiple times,
77
- * with the second codepoint, and the same `consumed` value.
78
- *
79
- * @param codepoint The decoded codepoint.
80
- * @param consumed The number of bytes consumed by the decoder.
81
- */
82
- emitCodePoint,
83
- /** An object that is used to produce errors. */
84
- errors) {
85
- this.decodeTree = decodeTree;
86
- this.emitCodePoint = emitCodePoint;
87
- this.errors = errors;
88
- /** The current state of the decoder. */
89
- this.state = EntityDecoderState.EntityStart;
90
- /** Characters that were consumed while parsing an entity. */
91
- this.consumed = 1;
92
- /**
93
- * The result of the entity.
94
- *
95
- * Either the result index of a numeric entity, or the codepoint of a
96
- * numeric entity.
97
- */
98
- this.result = 0;
99
- /** The current index in the decode tree. */
100
- this.treeIndex = 0;
101
- /** The number of characters that were consumed in excess. */
102
- this.excess = 1;
103
- /** The mode in which the decoder is operating. */
104
- this.decodeMode = DecodingMode.Strict;
105
- }
106
- /** Resets the instance to make it reusable. */
107
- startEntity(decodeMode) {
108
- this.decodeMode = decodeMode;
109
- this.state = EntityDecoderState.EntityStart;
110
- this.result = 0;
111
- this.treeIndex = 0;
112
- this.excess = 1;
113
- this.consumed = 1;
114
- }
115
- /**
116
- * Write an entity to the decoder. This can be called multiple times with partial entities.
117
- * If the entity is incomplete, the decoder will return -1.
118
- *
119
- * Mirrors the implementation of `getDecoder`, but with the ability to stop decoding if the
120
- * entity is incomplete, and resume when the next string is written.
121
- *
122
- * @param string The string containing the entity (or a continuation of the entity).
123
- * @param offset The offset at which the entity begins. Should be 0 if this is not the first call.
124
- * @returns The number of characters that were consumed, or -1 if the entity is incomplete.
125
- */
126
- write(str, offset) {
127
- switch (this.state) {
128
- case EntityDecoderState.EntityStart: {
129
- if (str.charCodeAt(offset) === CharCodes.NUM) {
130
- this.state = EntityDecoderState.NumericStart;
131
- this.consumed += 1;
132
- return this.stateNumericStart(str, offset + 1);
133
- }
134
- this.state = EntityDecoderState.NamedEntity;
135
- return this.stateNamedEntity(str, offset);
136
- }
137
- case EntityDecoderState.NumericStart: {
138
- return this.stateNumericStart(str, offset);
139
- }
140
- case EntityDecoderState.NumericDecimal: {
141
- return this.stateNumericDecimal(str, offset);
142
- }
143
- case EntityDecoderState.NumericHex: {
144
- return this.stateNumericHex(str, offset);
145
- }
146
- case EntityDecoderState.NamedEntity: {
147
- return this.stateNamedEntity(str, offset);
148
- }
149
- }
150
- }
151
- /**
152
- * Switches between the numeric decimal and hexadecimal states.
153
- *
154
- * Equivalent to the `Numeric character reference state` in the HTML spec.
155
- *
156
- * @param str The string containing the entity (or a continuation of the entity).
157
- * @param offset The current offset.
158
- * @returns The number of characters that were consumed, or -1 if the entity is incomplete.
159
- */
160
- stateNumericStart(str, offset) {
161
- if (offset >= str.length) {
162
- return -1;
163
- }
164
- if ((str.charCodeAt(offset) | TO_LOWER_BIT) === CharCodes.LOWER_X) {
165
- this.state = EntityDecoderState.NumericHex;
166
- this.consumed += 1;
167
- return this.stateNumericHex(str, offset + 1);
168
- }
169
- this.state = EntityDecoderState.NumericDecimal;
170
- return this.stateNumericDecimal(str, offset);
171
- }
172
- addToNumericResult(str, start, end, base) {
173
- if (start !== end) {
174
- const digitCount = end - start;
175
- this.result =
176
- this.result * Math.pow(base, digitCount) +
177
- parseInt(str.substr(start, digitCount), base);
178
- this.consumed += digitCount;
179
- }
180
- }
181
- /**
182
- * Parses a hexadecimal numeric entity.
183
- *
184
- * Equivalent to the `Hexademical character reference state` in the HTML spec.
185
- *
186
- * @param str The string containing the entity (or a continuation of the entity).
187
- * @param offset The current offset.
188
- * @returns The number of characters that were consumed, or -1 if the entity is incomplete.
189
- */
190
- stateNumericHex(str, offset) {
191
- const startIdx = offset;
192
- while (offset < str.length) {
193
- const char = str.charCodeAt(offset);
194
- if (isNumber(char) || isHexadecimalCharacter(char)) {
195
- offset += 1;
196
- }
197
- else {
198
- this.addToNumericResult(str, startIdx, offset, 16);
199
- return this.emitNumericEntity(char, 3);
200
- }
201
- }
202
- this.addToNumericResult(str, startIdx, offset, 16);
203
- return -1;
204
- }
205
- /**
206
- * Parses a decimal numeric entity.
207
- *
208
- * Equivalent to the `Decimal character reference state` in the HTML spec.
209
- *
210
- * @param str The string containing the entity (or a continuation of the entity).
211
- * @param offset The current offset.
212
- * @returns The number of characters that were consumed, or -1 if the entity is incomplete.
213
- */
214
- stateNumericDecimal(str, offset) {
215
- const startIdx = offset;
216
- while (offset < str.length) {
217
- const char = str.charCodeAt(offset);
218
- if (isNumber(char)) {
219
- offset += 1;
220
- }
221
- else {
222
- this.addToNumericResult(str, startIdx, offset, 10);
223
- return this.emitNumericEntity(char, 2);
224
- }
225
- }
226
- this.addToNumericResult(str, startIdx, offset, 10);
227
- return -1;
228
- }
229
- /**
230
- * Validate and emit a numeric entity.
231
- *
232
- * Implements the logic from the `Hexademical character reference start
233
- * state` and `Numeric character reference end state` in the HTML spec.
234
- *
235
- * @param lastCp The last code point of the entity. Used to see if the
236
- * entity was terminated with a semicolon.
237
- * @param expectedLength The minimum number of characters that should be
238
- * consumed. Used to validate that at least one digit
239
- * was consumed.
240
- * @returns The number of characters that were consumed.
241
- */
242
- emitNumericEntity(lastCp, expectedLength) {
243
- var _a;
244
- // Ensure we consumed at least one digit.
245
- if (this.consumed <= expectedLength) {
246
- (_a = this.errors) === null || _a === void 0 ? void 0 : _a.absenceOfDigitsInNumericCharacterReference(this.consumed);
247
- return 0;
248
- }
249
- // Figure out if this is a legit end of the entity
250
- if (lastCp === CharCodes.SEMI) {
251
- this.consumed += 1;
252
- }
253
- else if (this.decodeMode === DecodingMode.Strict) {
254
- return 0;
255
- }
256
- this.emitCodePoint(replaceCodePoint(this.result), this.consumed);
257
- if (this.errors) {
258
- if (lastCp !== CharCodes.SEMI) {
259
- this.errors.missingSemicolonAfterCharacterReference();
260
- }
261
- this.errors.validateNumericCharacterReference(this.result);
262
- }
263
- return this.consumed;
264
- }
265
- /**
266
- * Parses a named entity.
267
- *
268
- * Equivalent to the `Named character reference state` in the HTML spec.
269
- *
270
- * @param str The string containing the entity (or a continuation of the entity).
271
- * @param offset The current offset.
272
- * @returns The number of characters that were consumed, or -1 if the entity is incomplete.
273
- */
274
- stateNamedEntity(str, offset) {
275
- const { decodeTree } = this;
276
- let current = decodeTree[this.treeIndex];
277
- // The mask is the number of bytes of the value, including the current byte.
278
- let valueLength = (current & BinTrieFlags.VALUE_LENGTH) >> 14;
279
- for (; offset < str.length; offset++, this.excess++) {
280
- const char = str.charCodeAt(offset);
281
- this.treeIndex = determineBranch(decodeTree, current, this.treeIndex + Math.max(1, valueLength), char);
282
- if (this.treeIndex < 0) {
283
- return this.result === 0 ||
284
- // If we are parsing an attribute
285
- (this.decodeMode === DecodingMode.Attribute &&
286
- // We shouldn't have consumed any characters after the entity,
287
- (valueLength === 0 ||
288
- // And there should be no invalid characters.
289
- isEntityInAttributeInvalidEnd(char)))
290
- ? 0
291
- : this.emitNotTerminatedNamedEntity();
292
- }
293
- current = decodeTree[this.treeIndex];
294
- valueLength = (current & BinTrieFlags.VALUE_LENGTH) >> 14;
295
- // If the branch is a value, store it and continue
296
- if (valueLength !== 0) {
297
- // If the entity is terminated by a semicolon, we are done.
298
- if (char === CharCodes.SEMI) {
299
- return this.emitNamedEntityData(this.treeIndex, valueLength, this.consumed + this.excess);
300
- }
301
- // If we encounter a non-terminated (legacy) entity while parsing strictly, then ignore it.
302
- if (this.decodeMode !== DecodingMode.Strict) {
303
- this.result = this.treeIndex;
304
- this.consumed += this.excess;
305
- this.excess = 0;
306
- }
307
- }
308
- }
309
- return -1;
310
- }
311
- /**
312
- * Emit a named entity that was not terminated with a semicolon.
313
- *
314
- * @returns The number of characters consumed.
315
- */
316
- emitNotTerminatedNamedEntity() {
317
- var _a;
318
- const { result, decodeTree } = this;
319
- const valueLength = (decodeTree[result] & BinTrieFlags.VALUE_LENGTH) >> 14;
320
- this.emitNamedEntityData(result, valueLength, this.consumed);
321
- (_a = this.errors) === null || _a === void 0 ? void 0 : _a.missingSemicolonAfterCharacterReference();
322
- return this.consumed;
323
- }
324
- /**
325
- * Emit a named entity.
326
- *
327
- * @param result The index of the entity in the decode tree.
328
- * @param valueLength The number of bytes in the entity.
329
- * @param consumed The number of characters consumed.
330
- *
331
- * @returns The number of characters consumed.
332
- */
333
- emitNamedEntityData(result, valueLength, consumed) {
334
- const { decodeTree } = this;
335
- this.emitCodePoint(valueLength === 1
336
- ? decodeTree[result] & ~BinTrieFlags.VALUE_LENGTH
337
- : decodeTree[result + 1], consumed);
338
- if (valueLength === 3) {
339
- // For multi-byte values, we need to emit the second byte.
340
- this.emitCodePoint(decodeTree[result + 2], consumed);
341
- }
342
- return consumed;
343
- }
344
- /**
345
- * Signal to the parser that the end of the input was reached.
346
- *
347
- * Remaining data will be emitted and relevant errors will be produced.
348
- *
349
- * @returns The number of characters consumed.
350
- */
351
- end() {
352
- var _a;
353
- switch (this.state) {
354
- case EntityDecoderState.NamedEntity: {
355
- // Emit a named entity if we have one.
356
- return this.result !== 0 &&
357
- (this.decodeMode !== DecodingMode.Attribute ||
358
- this.result === this.treeIndex)
359
- ? this.emitNotTerminatedNamedEntity()
360
- : 0;
361
- }
362
- // Otherwise, emit a numeric entity if we have one.
363
- case EntityDecoderState.NumericDecimal: {
364
- return this.emitNumericEntity(0, 2);
365
- }
366
- case EntityDecoderState.NumericHex: {
367
- return this.emitNumericEntity(0, 3);
368
- }
369
- case EntityDecoderState.NumericStart: {
370
- (_a = this.errors) === null || _a === void 0 ? void 0 : _a.absenceOfDigitsInNumericCharacterReference(this.consumed);
371
- return 0;
372
- }
373
- case EntityDecoderState.EntityStart: {
374
- // Return 0 if we have no entity.
375
- return 0;
376
- }
377
- }
378
- }
379
- }
380
- /**
381
- * Creates a function that decodes entities in a string.
382
- *
383
- * @param decodeTree The decode tree.
384
- * @returns A function that decodes entities in a string.
385
- */
386
- function getDecoder(decodeTree) {
387
- let ret = "";
388
- const decoder = new EntityDecoder(decodeTree, (str) => (ret += fromCodePoint(str)));
389
- return function decodeWithTrie(str, decodeMode) {
390
- let lastIndex = 0;
391
- let offset = 0;
392
- while ((offset = str.indexOf("&", offset)) >= 0) {
393
- ret += str.slice(lastIndex, offset);
394
- decoder.startEntity(decodeMode);
395
- const len = decoder.write(str,
396
- // Skip the "&"
397
- offset + 1);
398
- if (len < 0) {
399
- lastIndex = offset + decoder.end();
400
- break;
401
- }
402
- lastIndex = offset + len;
403
- // If `len` is 0, skip the current `&` and continue.
404
- offset = len === 0 ? lastIndex + 1 : lastIndex;
405
- }
406
- const result = ret + str.slice(lastIndex);
407
- // Make sure we don't keep a reference to the final string.
408
- ret = "";
409
- return result;
410
- };
411
- }
412
- /**
413
- * Determines the branch of the current node that is taken given the current
414
- * character. This function is used to traverse the trie.
415
- *
416
- * @param decodeTree The trie.
417
- * @param current The current node.
418
- * @param nodeIdx The index right after the current node and its value.
419
- * @param char The current character.
420
- * @returns The index of the next node, or -1 if no branch is taken.
421
- */
422
- function determineBranch(decodeTree, current, nodeIdx, char) {
423
- const branchCount = (current & BinTrieFlags.BRANCH_LENGTH) >> 7;
424
- const jumpOffset = current & BinTrieFlags.JUMP_TABLE;
425
- // Case 1: Single branch encoded in jump offset
426
- if (branchCount === 0) {
427
- return jumpOffset !== 0 && char === jumpOffset ? nodeIdx : -1;
428
- }
429
- // Case 2: Multiple branches encoded in jump table
430
- if (jumpOffset) {
431
- const value = char - jumpOffset;
432
- return value < 0 || value >= branchCount
433
- ? -1
434
- : decodeTree[nodeIdx + value] - 1;
435
- }
436
- // Case 3: Multiple branches encoded in dictionary
437
- // Binary search for the character.
438
- let lo = nodeIdx;
439
- let hi = lo + branchCount - 1;
440
- while (lo <= hi) {
441
- const mid = (lo + hi) >>> 1;
442
- const midVal = decodeTree[mid];
443
- if (midVal < char) {
444
- lo = mid + 1;
445
- }
446
- else if (midVal > char) {
447
- hi = mid - 1;
448
- }
449
- else {
450
- return decodeTree[mid + branchCount];
451
- }
452
- }
453
- return -1;
454
- }
455
- getDecoder(htmlDecodeTree);
456
- getDecoder(xmlDecodeTree);
457
-
458
- export { BinTrieFlags, DecodingMode, EntityDecoder, determineBranch, fromCodePoint, htmlDecodeTree, replaceCodePoint, xmlDecodeTree };
@@ -1,62 +0,0 @@
1
- // Adapted from https://github.com/mathiasbynens/he/blob/36afe179392226cf1b6ccdb16ebbb7a5a844d93a/src/he.js#L106-L134
2
- var _a;
3
- const decodeMap = new Map([
4
- [0, 65533],
5
- // C1 Unicode control character reference replacements
6
- [128, 8364],
7
- [130, 8218],
8
- [131, 402],
9
- [132, 8222],
10
- [133, 8230],
11
- [134, 8224],
12
- [135, 8225],
13
- [136, 710],
14
- [137, 8240],
15
- [138, 352],
16
- [139, 8249],
17
- [140, 338],
18
- [142, 381],
19
- [145, 8216],
20
- [146, 8217],
21
- [147, 8220],
22
- [148, 8221],
23
- [149, 8226],
24
- [150, 8211],
25
- [151, 8212],
26
- [152, 732],
27
- [153, 8482],
28
- [154, 353],
29
- [155, 8250],
30
- [156, 339],
31
- [158, 382],
32
- [159, 376],
33
- ]);
34
- /**
35
- * Polyfill for `String.fromCodePoint`. It is used to create a string from a Unicode code point.
36
- */
37
- const fromCodePoint =
38
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition, node/no-unsupported-features/es-builtins
39
- (_a = String.fromCodePoint) !== null && _a !== void 0 ? _a : function (codePoint) {
40
- let output = "";
41
- if (codePoint > 0xffff) {
42
- codePoint -= 0x10000;
43
- output += String.fromCharCode(((codePoint >>> 10) & 0x3ff) | 0xd800);
44
- codePoint = 0xdc00 | (codePoint & 0x3ff);
45
- }
46
- output += String.fromCharCode(codePoint);
47
- return output;
48
- };
49
- /**
50
- * Replace the given code point with a replacement character if it is a
51
- * surrogate or is outside the valid range. Otherwise return the code
52
- * point unchanged.
53
- */
54
- function replaceCodePoint(codePoint) {
55
- var _a;
56
- if ((codePoint >= 0xd800 && codePoint <= 0xdfff) || codePoint > 0x10ffff) {
57
- return 0xfffd;
58
- }
59
- return (_a = decodeMap.get(codePoint)) !== null && _a !== void 0 ? _a : codePoint;
60
- }
61
-
62
- export { fromCodePoint, replaceCodePoint };
@@ -1,99 +0,0 @@
1
- const xmlReplacer = /["&'<>$\x80-\uFFFF]/g;
2
- const xmlCodeMap = new Map([
3
- [34, "&quot;"],
4
- [38, "&amp;"],
5
- [39, "&apos;"],
6
- [60, "&lt;"],
7
- [62, "&gt;"],
8
- ]);
9
- // For compatibility with node < 4, we wrap `codePointAt`
10
- const getCodePoint =
11
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
12
- String.prototype.codePointAt != null
13
- ? (str, index) => str.codePointAt(index)
14
- : // http://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
15
- (c, index) => (c.charCodeAt(index) & 0xfc00) === 0xd800
16
- ? (c.charCodeAt(index) - 0xd800) * 0x400 +
17
- c.charCodeAt(index + 1) -
18
- 0xdc00 +
19
- 0x10000
20
- : c.charCodeAt(index);
21
- /**
22
- * Encodes all non-ASCII characters, as well as characters not valid in XML
23
- * documents using XML entities.
24
- *
25
- * If a character has no equivalent entity, a
26
- * numeric hexadecimal reference (eg. `&#xfc;`) will be used.
27
- */
28
- function encodeXML(str) {
29
- let ret = "";
30
- let lastIdx = 0;
31
- let match;
32
- while ((match = xmlReplacer.exec(str)) !== null) {
33
- const i = match.index;
34
- const char = str.charCodeAt(i);
35
- const next = xmlCodeMap.get(char);
36
- if (next !== undefined) {
37
- ret += str.substring(lastIdx, i) + next;
38
- lastIdx = i + 1;
39
- }
40
- else {
41
- ret += `${str.substring(lastIdx, i)}&#x${getCodePoint(str, i).toString(16)};`;
42
- // Increase by 1 if we have a surrogate pair
43
- lastIdx = xmlReplacer.lastIndex += Number((char & 0xfc00) === 0xd800);
44
- }
45
- }
46
- return ret + str.substr(lastIdx);
47
- }
48
- /**
49
- * Creates a function that escapes all characters matched by the given regular
50
- * expression using the given map of characters to escape to their entities.
51
- *
52
- * @param regex Regular expression to match characters to escape.
53
- * @param map Map of characters to escape to their entities.
54
- *
55
- * @returns Function that escapes all characters matched by the given regular
56
- * expression using the given map of characters to escape to their entities.
57
- */
58
- function getEscaper(regex, map) {
59
- return function escape(data) {
60
- let match;
61
- let lastIdx = 0;
62
- let result = "";
63
- while ((match = regex.exec(data))) {
64
- if (lastIdx !== match.index) {
65
- result += data.substring(lastIdx, match.index);
66
- }
67
- // We know that this character will be in the map.
68
- result += map.get(match[0].charCodeAt(0));
69
- // Every match will be of length 1
70
- lastIdx = match.index + 1;
71
- }
72
- return result + data.substring(lastIdx);
73
- };
74
- }
75
- /**
76
- * Encodes all characters that have to be escaped in HTML attributes,
77
- * following {@link https://html.spec.whatwg.org/multipage/parsing.html#escapingString}.
78
- *
79
- * @param data String to escape.
80
- */
81
- const escapeAttribute = getEscaper(/["&\u00A0]/g, new Map([
82
- [34, "&quot;"],
83
- [38, "&amp;"],
84
- [160, "&nbsp;"],
85
- ]));
86
- /**
87
- * Encodes all characters that have to be escaped in HTML text,
88
- * following {@link https://html.spec.whatwg.org/multipage/parsing.html#escapingString}.
89
- *
90
- * @param data String to escape.
91
- */
92
- const escapeText = getEscaper(/[&<>\u00A0]/g, new Map([
93
- [38, "&amp;"],
94
- [60, "&lt;"],
95
- [62, "&gt;"],
96
- [160, "&nbsp;"],
97
- ]));
98
-
99
- export { encodeXML, escapeAttribute, escapeText, getCodePoint, xmlReplacer };