@mtcute/dispatcher 0.6.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -3
- package/cjs/callback-data-builder.d.ts +1 -1
- package/cjs/callback-data-builder.js +5 -5
- package/cjs/callback-data-builder.js.map +1 -1
- package/cjs/context/base.d.ts +2 -1
- package/cjs/context/base.js.map +1 -1
- package/cjs/context/callback-query.d.ts +3 -2
- package/cjs/context/callback-query.js +3 -3
- package/cjs/context/callback-query.js.map +1 -1
- package/cjs/context/chat-join-request.d.ts +2 -1
- package/cjs/context/chat-join-request.js +2 -2
- package/cjs/context/chat-join-request.js.map +1 -1
- package/cjs/context/chosen-inline-result.d.ts +2 -1
- package/cjs/context/chosen-inline-result.js +3 -3
- package/cjs/context/chosen-inline-result.js.map +1 -1
- package/cjs/context/inline-query.d.ts +2 -1
- package/cjs/context/inline-query.js +2 -2
- package/cjs/context/inline-query.js.map +1 -1
- package/cjs/context/message.d.ts +4 -6
- package/cjs/context/message.js +3 -3
- package/cjs/context/message.js.map +1 -1
- package/cjs/context/parse.js.map +1 -1
- package/cjs/context/pre-checkout-query.d.ts +2 -1
- package/cjs/context/pre-checkout-query.js +2 -2
- package/cjs/context/pre-checkout-query.js.map +1 -1
- package/cjs/dispatcher.d.ts +15 -9
- package/cjs/dispatcher.js +41 -78
- package/cjs/dispatcher.js.map +1 -1
- package/cjs/filters/bots.d.ts +6 -6
- package/cjs/filters/bots.js +2 -2
- package/cjs/filters/bots.js.map +1 -1
- package/cjs/filters/chat.d.ts +1 -1
- package/cjs/filters/chat.js +1 -1
- package/cjs/filters/chat.js.map +1 -1
- package/cjs/filters/group.d.ts +1 -1
- package/cjs/filters/group.js.map +1 -1
- package/cjs/filters/logic.js.map +1 -1
- package/cjs/filters/message.d.ts +75 -67
- package/cjs/filters/message.js +3 -3
- package/cjs/filters/message.js.map +1 -1
- package/cjs/filters/state.d.ts +2 -2
- package/cjs/filters/state.js.map +1 -1
- package/cjs/filters/text.d.ts +1 -1
- package/cjs/filters/text.js.map +1 -1
- package/cjs/filters/types.d.ts +2 -2
- package/cjs/filters/types.js.map +1 -1
- package/cjs/filters/updates.d.ts +1 -1
- package/cjs/filters/updates.js.map +1 -1
- package/cjs/filters/user.d.ts +1 -1
- package/cjs/filters/user.js +4 -4
- package/cjs/filters/user.js.map +1 -1
- package/cjs/handler.d.ts +4 -3
- package/cjs/handler.js.map +1 -1
- package/cjs/propagation.js +1 -1
- package/cjs/propagation.js.map +1 -1
- package/cjs/state/index.d.ts +3 -1
- package/cjs/state/index.js +3 -1
- package/cjs/state/index.js.map +1 -1
- package/cjs/state/key.d.ts +2 -2
- package/cjs/state/key.js +2 -2
- package/cjs/state/key.js.map +1 -1
- package/cjs/state/provider.d.ts +5 -0
- package/cjs/state/provider.js +3 -0
- package/cjs/state/provider.js.map +1 -0
- package/cjs/state/providers/index.d.ts +2 -0
- package/cjs/state/providers/index.js +19 -0
- package/cjs/state/providers/index.js.map +1 -0
- package/cjs/state/providers/memory.d.ts +29 -0
- package/cjs/state/providers/memory.js +76 -0
- package/cjs/state/providers/memory.js.map +1 -0
- package/cjs/state/providers/sqlite.d.ts +29 -0
- package/cjs/state/providers/sqlite.js +89 -0
- package/cjs/state/providers/sqlite.js.map +1 -0
- package/cjs/state/repository.d.ts +62 -0
- package/cjs/state/repository.js +3 -0
- package/cjs/state/repository.js.map +1 -0
- package/cjs/state/service.d.ts +19 -0
- package/cjs/state/service.js +68 -0
- package/cjs/state/service.js.map +1 -0
- package/cjs/state/update-state.d.ts +3 -3
- package/cjs/state/update-state.js +7 -7
- package/cjs/state/update-state.js.map +1 -1
- package/cjs/wizard.d.ts +2 -2
- package/cjs/wizard.js +1 -1
- package/cjs/wizard.js.map +1 -1
- package/esm/callback-data-builder.d.ts +1 -1
- package/esm/callback-data-builder.js +1 -1
- package/esm/callback-data-builder.js.map +1 -1
- package/esm/context/base.d.ts +2 -1
- package/esm/context/base.js.map +1 -1
- package/esm/context/callback-query.d.ts +3 -2
- package/esm/context/callback-query.js +1 -1
- package/esm/context/callback-query.js.map +1 -1
- package/esm/context/chat-join-request.d.ts +2 -1
- package/esm/context/chat-join-request.js +1 -1
- package/esm/context/chat-join-request.js.map +1 -1
- package/esm/context/chosen-inline-result.d.ts +2 -1
- package/esm/context/chosen-inline-result.js +1 -1
- package/esm/context/chosen-inline-result.js.map +1 -1
- package/esm/context/inline-query.d.ts +2 -1
- package/esm/context/inline-query.js +1 -1
- package/esm/context/inline-query.js.map +1 -1
- package/esm/context/message.d.ts +4 -6
- package/esm/context/message.js +1 -1
- package/esm/context/message.js.map +1 -1
- package/esm/context/parse.js.map +1 -1
- package/esm/context/pre-checkout-query.d.ts +2 -1
- package/esm/context/pre-checkout-query.js +1 -1
- package/esm/context/pre-checkout-query.js.map +1 -1
- package/esm/dispatcher.d.ts +15 -9
- package/esm/dispatcher.js +29 -66
- package/esm/dispatcher.js.map +1 -1
- package/esm/filters/bots.d.ts +6 -6
- package/esm/filters/bots.js +2 -2
- package/esm/filters/bots.js.map +1 -1
- package/esm/filters/chat.d.ts +2 -2
- package/esm/filters/chat.js +1 -1
- package/esm/filters/chat.js.map +1 -1
- package/esm/filters/group.d.ts +1 -1
- package/esm/filters/group.js.map +1 -1
- package/esm/filters/logic.js.map +1 -1
- package/esm/filters/message.d.ts +77 -69
- package/esm/filters/message.js +1 -1
- package/esm/filters/message.js.map +1 -1
- package/esm/filters/state.d.ts +2 -2
- package/esm/filters/state.js.map +1 -1
- package/esm/filters/text.d.ts +1 -1
- package/esm/filters/text.js.map +1 -1
- package/esm/filters/types.d.ts +2 -2
- package/esm/filters/types.js.map +1 -1
- package/esm/filters/updates.d.ts +1 -1
- package/esm/filters/updates.js.map +1 -1
- package/esm/filters/user.d.ts +1 -1
- package/esm/filters/user.js +3 -3
- package/esm/filters/user.js.map +1 -1
- package/esm/handler.d.ts +4 -3
- package/esm/handler.js.map +1 -1
- package/esm/propagation.js +1 -1
- package/esm/propagation.js.map +1 -1
- package/esm/state/index.d.ts +3 -1
- package/esm/state/index.js +3 -1
- package/esm/state/index.js.map +1 -1
- package/esm/state/key.d.ts +2 -2
- package/esm/state/key.js +1 -1
- package/esm/state/key.js.map +1 -1
- package/esm/state/provider.d.ts +5 -0
- package/esm/state/provider.js +2 -0
- package/esm/state/provider.js.map +1 -0
- package/esm/state/providers/index.d.ts +2 -0
- package/esm/state/providers/index.js +3 -0
- package/esm/state/providers/index.js.map +1 -0
- package/esm/state/providers/memory.d.ts +29 -0
- package/esm/state/providers/memory.js +72 -0
- package/esm/state/providers/memory.js.map +1 -0
- package/esm/state/providers/sqlite.d.ts +29 -0
- package/esm/state/providers/sqlite.js +85 -0
- package/esm/state/providers/sqlite.js.map +1 -0
- package/esm/state/repository.d.ts +62 -0
- package/esm/state/repository.js +2 -0
- package/esm/state/repository.js.map +1 -0
- package/esm/state/service.d.ts +19 -0
- package/esm/state/service.js +64 -0
- package/esm/state/service.js.map +1 -0
- package/esm/state/update-state.d.ts +3 -3
- package/esm/state/update-state.js +2 -2
- package/esm/state/update-state.js.map +1 -1
- package/esm/wizard.d.ts +2 -2
- package/esm/wizard.js +1 -1
- package/esm/wizard.js.map +1 -1
- package/package.json +11 -3
- package/cjs/callback-data-builder.test.d.ts +0 -1
- package/cjs/callback-data-builder.test.js +0 -74
- package/cjs/callback-data-builder.test.js.map +0 -1
- package/cjs/filters/bots.test.d.ts +0 -1
- package/cjs/filters/bots.test.js +0 -120
- package/cjs/filters/bots.test.js.map +0 -1
- package/cjs/filters/logic.test.d.ts +0 -1
- package/cjs/filters/logic.test.js +0 -169
- package/cjs/filters/logic.test.js.map +0 -1
- package/cjs/state/storage.d.ts +0 -96
- package/cjs/state/storage.js +0 -17
- package/cjs/state/storage.js.map +0 -1
- package/esm/callback-data-builder.test.d.ts +0 -1
- package/esm/callback-data-builder.test.js +0 -72
- package/esm/callback-data-builder.test.js.map +0 -1
- package/esm/filters/bots.test.d.ts +0 -1
- package/esm/filters/bots.test.js +0 -118
- package/esm/filters/bots.test.js.map +0 -1
- package/esm/filters/logic.test.d.ts +0 -1
- package/esm/filters/logic.test.js +0 -167
- package/esm/filters/logic.test.js.map +0 -1
- package/esm/state/storage.d.ts +0 -96
- package/esm/state/storage.js +0 -13
- package/esm/state/storage.js.map +0 -1
package/esm/filters/user.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"user.js","sourceRoot":"","sources":["../../../src/filters/user.ts"],"names":[],"mappings":"AAAA,OAAO,EAaH,IAAI,GAGP,MAAM,
|
|
1
|
+
{"version":3,"file":"user.js","sourceRoot":"","sources":["../../../src/filters/user.ts"],"names":[],"mappings":"AAAA,OAAO,EAaH,IAAI,GAGP,MAAM,cAAc,CAAA;AAKrB;;GAEG;AACH,MAAM,CAAC,MAAM,EAAE,GAA4C,CAAC,GAAG,EAAE,EAAE,CAC/D,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,KAAK,MAAM,IAAI,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,GAAG,CAAC,UAAU,CAAA;AAEvE;;GAEG;AACH,MAAM,CAAC,MAAM,GAAG,GAA4C,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,WAAW,KAAK,IAAI,IAAI,GAAG,CAAC,MAAM,CAAC,KAAK,CAAA;AAExH,kBAAkB;AAClB;;;;GAIG;AACH,MAAM,CAAC,MAAM,MAAM,GA4Bf,CAAC,EAAE,EAAE,EAAE;IACP,MAAM,OAAO,GAAG,IAAI,GAAG,EAAU,CAAA;IACjC,MAAM,aAAa,GAAG,IAAI,GAAG,EAAU,CAAA;IACvC,IAAI,SAAS,GAAG,KAAK,CAAA;IAErB,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,EAAE,CAAC;QAAE,EAAE,GAAG,CAAC,EAAE,CAAC,CAAA;IACjC,EAAE,CAAC,OAAO,CAAC,CAAC,EAAE,EAAE,EAAE;QACd,IAAI,EAAE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C,SAAS,EAAE,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,aAAa,CAAC,GAAG,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAA;aAC1E;YACD,KAAK,cAAc,CAAC,CAAC;gBACjB,MAAM,EAAE,GAAG,GAAG,CAAC,MAAM,CAAA;gBAErB,OAAO,CAAC,SAAS,IAAI,EAAE,KAAK,GAAG,CAAC,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,SAAS,EAAE,EAAE,MAAM,CAAC;oBACpE,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC,CAAA;aACtB;SACJ;QAED,MAAM,IAAI,GAAG,GAAG,CAAC,IAAI,CAAA;QAErB,OAAO,CACH,CAAC,SAAS,IAAI,IAAI,CAAC,MAAM,CAAC;YAC1B,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC;YACpB,OAAO,CAAC,IAAI,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,aAAa,CAAC,GAAG,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,CACtE,CAAA;IACL,CAAC,CAAA;AACL,CAAC,CAAA","sourcesContent":["import {\n BotChatJoinRequestUpdate,\n CallbackQuery,\n ChatMemberUpdate,\n ChosenInlineResult,\n DeleteStoryUpdate,\n HistoryReadUpdate,\n InlineCallbackQuery,\n InlineQuery,\n MaybeArray,\n Message,\n PollVoteUpdate,\n StoryUpdate,\n User,\n UserStatusUpdate,\n UserTypingUpdate,\n} from '@mtcute/core'\n\nimport { UpdateContextDistributed } from '../context/base.js'\nimport { UpdateFilter } from './types.js'\n\n/**\n * Filter messages generated by yourself (including Saved Messages)\n */\nexport const me: UpdateFilter<Message, { sender: User }> = (msg) =>\n (msg.sender.type === 'user' && msg.sender.isSelf) || msg.isOutgoing\n\n/**\n * Filter messages sent by bots\n */\nexport const bot: UpdateFilter<Message, { sender: User }> = (msg) => msg.sender.constructor === User && msg.sender.isBot\n\n// prettier-ignore\n/**\n * Filter updates by user ID(s) or username(s)\n *\n * Note that only some updates support filtering by username.\n */\nexport const userId: {\n (id: MaybeArray<number>): UpdateFilter<UpdateContextDistributed<\n | Message\n | StoryUpdate\n | DeleteStoryUpdate\n | InlineQuery\n | ChatMemberUpdate\n | ChosenInlineResult\n | CallbackQuery\n | InlineCallbackQuery\n | PollVoteUpdate\n | BotChatJoinRequestUpdate\n >>\n (id: MaybeArray<number | string>): UpdateFilter<UpdateContextDistributed<\n | Message\n | UserStatusUpdate\n | UserTypingUpdate\n | StoryUpdate\n | HistoryReadUpdate\n | DeleteStoryUpdate\n | InlineQuery\n | ChatMemberUpdate\n | ChosenInlineResult\n | CallbackQuery\n | InlineCallbackQuery\n | PollVoteUpdate\n | BotChatJoinRequestUpdate\n >>\n} = (id) => {\n const indexId = new Set<number>()\n const indexUsername = new Set<string>()\n let matchSelf = false\n\n if (!Array.isArray(id)) id = [id]\n id.forEach((id) => {\n if (id === 'me' || id === 'self') {\n matchSelf = true\n } else if (typeof id === 'string') {\n indexUsername.add(id)\n } else {\n indexId.add(id)\n }\n })\n\n return (upd) => {\n switch (upd._name) {\n case 'new_message':\n case 'edit_message': {\n const sender = upd.sender\n\n return (matchSelf && sender.isSelf) ||\n indexId.has(sender.id) ||\n indexUsername.has(sender.username!)\n }\n case 'user_status':\n case 'user_typing': {\n const id = upd.userId\n\n return (matchSelf && id === upd.client.storage.self.getCached()?.userId) ||\n indexId.has(id)\n }\n case 'poll_vote':\n case 'story':\n case 'delete_story': {\n const peer = upd.peer\n if (peer.type !== 'user') return false\n\n return (matchSelf && peer.isSelf) ||\n indexId.has(peer.id) ||\n Boolean(peer.usernames?.some((u) => indexUsername.has(u.username)))\n }\n case 'history_read': {\n const id = upd.chatId\n\n return (matchSelf && id === upd.client.storage.self.getCached()?.userId) ||\n indexId.has(id)\n }\n }\n\n const user = upd.user\n\n return (\n (matchSelf && user.isSelf) ||\n indexId.has(user.id) ||\n Boolean(user.usernames?.some((u) => indexUsername.has(u.username)))\n )\n }\n}\n"]}
|
package/esm/handler.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { BotReactionCountUpdate, BotReactionUpdate, BotStoppedUpdate, ChatJoinRequestUpdate, ChatMemberUpdate, DeleteMessageUpdate, DeleteStoryUpdate, HistoryReadUpdate,
|
|
1
|
+
import { BotReactionCountUpdate, BotReactionUpdate, BotStoppedUpdate, ChatJoinRequestUpdate, ChatMemberUpdate, DeleteMessageUpdate, DeleteStoryUpdate, HistoryReadUpdate, MaybePromise, PeersIndex, PollUpdate, PollVoteUpdate, StoryUpdate, tl, UserStatusUpdate, UserTypingUpdate } from '@mtcute/core';
|
|
2
|
+
import { TelegramClient } from '@mtcute/core/client.js';
|
|
2
3
|
import { UpdateContext } from './context/base.js';
|
|
3
4
|
import { CallbackQueryContext, ChatJoinRequestUpdateContext, ChosenInlineResultContext, InlineCallbackQueryContext, InlineQueryContext, MessageContext, PreCheckoutQueryContext } from './context/index.js';
|
|
4
5
|
import { PropagationAction } from './propagation.js';
|
|
@@ -7,8 +8,8 @@ export interface BaseUpdateHandler<Name, Handler, Checker> {
|
|
|
7
8
|
callback: Handler;
|
|
8
9
|
check?: Checker;
|
|
9
10
|
}
|
|
10
|
-
export type ParsedUpdateHandler<Name, Update, State = never> = BaseUpdateHandler<Name, (update: Update, state: State) =>
|
|
11
|
-
export type RawUpdateHandler = BaseUpdateHandler<'raw', (client: TelegramClient, update: tl.TypeUpdate | tl.TypeMessage, peers: PeersIndex) =>
|
|
11
|
+
export type ParsedUpdateHandler<Name, Update, State = never> = BaseUpdateHandler<Name, (update: Update, state: State) => MaybePromise<void | PropagationAction>, (update: Update, state: State) => MaybePromise<boolean>>;
|
|
12
|
+
export type RawUpdateHandler = BaseUpdateHandler<'raw', (client: TelegramClient, update: tl.TypeUpdate | tl.TypeMessage, peers: PeersIndex) => MaybePromise<void | PropagationAction>, (client: TelegramClient, update: tl.TypeUpdate | tl.TypeMessage, peers: PeersIndex) => MaybePromise<boolean>>;
|
|
12
13
|
export type NewMessageHandler<T = MessageContext, S = never> = ParsedUpdateHandler<'new_message', T, S>;
|
|
13
14
|
export type EditMessageHandler<T = MessageContext, S = never> = ParsedUpdateHandler<'edit_message', T, S>;
|
|
14
15
|
export type MessageGroupHandler<T = MessageContext, S = never> = ParsedUpdateHandler<'message_group', T, S>;
|
package/esm/handler.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handler.js","sourceRoot":"","sources":["../../src/handler.ts"],"names":[],"mappings":";AAqHA,cAAc","sourcesContent":["import {\n BotReactionCountUpdate,\n BotReactionUpdate,\n BotStoppedUpdate,\n ChatJoinRequestUpdate,\n ChatMemberUpdate,\n DeleteMessageUpdate,\n DeleteStoryUpdate,\n HistoryReadUpdate,\n
|
|
1
|
+
{"version":3,"file":"handler.js","sourceRoot":"","sources":["../../src/handler.ts"],"names":[],"mappings":";AAqHA,cAAc","sourcesContent":["import {\n BotReactionCountUpdate,\n BotReactionUpdate,\n BotStoppedUpdate,\n ChatJoinRequestUpdate,\n ChatMemberUpdate,\n DeleteMessageUpdate,\n DeleteStoryUpdate,\n HistoryReadUpdate,\n MaybePromise,\n PeersIndex,\n PollUpdate,\n PollVoteUpdate,\n StoryUpdate,\n tl,\n UserStatusUpdate,\n UserTypingUpdate,\n} from '@mtcute/core'\nimport { TelegramClient } from '@mtcute/core/client.js'\n\nimport { UpdateContext } from './context/base.js'\nimport {\n CallbackQueryContext,\n ChatJoinRequestUpdateContext,\n ChosenInlineResultContext,\n InlineCallbackQueryContext,\n InlineQueryContext,\n MessageContext,\n PreCheckoutQueryContext,\n} from './context/index.js'\nimport { PropagationAction } from './propagation.js'\n\nexport interface BaseUpdateHandler<Name, Handler, Checker> {\n name: Name\n callback: Handler\n\n check?: Checker\n}\n\nexport type ParsedUpdateHandler<Name, Update, State = never> = BaseUpdateHandler<\n Name,\n (update: Update, state: State) => MaybePromise<void | PropagationAction>,\n (update: Update, state: State) => MaybePromise<boolean>\n>\n\nexport type RawUpdateHandler = BaseUpdateHandler<\n 'raw',\n (\n client: TelegramClient,\n update: tl.TypeUpdate | tl.TypeMessage,\n peers: PeersIndex,\n ) => MaybePromise<void | PropagationAction>,\n (client: TelegramClient, update: tl.TypeUpdate | tl.TypeMessage, peers: PeersIndex) => MaybePromise<boolean>\n>\n\n// begin-codegen\nexport type NewMessageHandler<T = MessageContext, S = never> = ParsedUpdateHandler<'new_message', T, S>\nexport type EditMessageHandler<T = MessageContext, S = never> = ParsedUpdateHandler<'edit_message', T, S>\nexport type MessageGroupHandler<T = MessageContext, S = never> = ParsedUpdateHandler<'message_group', T, S>\nexport type DeleteMessageHandler<T = UpdateContext<DeleteMessageUpdate>> = ParsedUpdateHandler<'delete_message', T>\nexport type ChatMemberUpdateHandler<T = UpdateContext<ChatMemberUpdate>> = ParsedUpdateHandler<'chat_member', T>\nexport type InlineQueryHandler<T = InlineQueryContext> = ParsedUpdateHandler<'inline_query', T>\nexport type ChosenInlineResultHandler<T = ChosenInlineResultContext> = ParsedUpdateHandler<'chosen_inline_result', T>\nexport type CallbackQueryHandler<T = CallbackQueryContext, S = never> = ParsedUpdateHandler<'callback_query', T, S>\nexport type InlineCallbackQueryHandler<T = InlineCallbackQueryContext, S = never> = ParsedUpdateHandler<\n 'inline_callback_query',\n T,\n S\n>\nexport type PollUpdateHandler<T = UpdateContext<PollUpdate>> = ParsedUpdateHandler<'poll', T>\nexport type PollVoteHandler<T = UpdateContext<PollVoteUpdate>> = ParsedUpdateHandler<'poll_vote', T>\nexport type UserStatusUpdateHandler<T = UpdateContext<UserStatusUpdate>> = ParsedUpdateHandler<'user_status', T>\nexport type UserTypingHandler<T = UpdateContext<UserTypingUpdate>> = ParsedUpdateHandler<'user_typing', T>\nexport type HistoryReadHandler<T = UpdateContext<HistoryReadUpdate>> = ParsedUpdateHandler<'history_read', T>\nexport type BotStoppedHandler<T = UpdateContext<BotStoppedUpdate>> = ParsedUpdateHandler<'bot_stopped', T>\nexport type BotChatJoinRequestHandler<T = ChatJoinRequestUpdateContext> = ParsedUpdateHandler<\n 'bot_chat_join_request',\n T\n>\nexport type ChatJoinRequestHandler<T = UpdateContext<ChatJoinRequestUpdate>> = ParsedUpdateHandler<\n 'chat_join_request',\n T\n>\nexport type PreCheckoutQueryHandler<T = PreCheckoutQueryContext> = ParsedUpdateHandler<'pre_checkout_query', T>\nexport type StoryUpdateHandler<T = UpdateContext<StoryUpdate>> = ParsedUpdateHandler<'story', T>\nexport type DeleteStoryHandler<T = UpdateContext<DeleteStoryUpdate>> = ParsedUpdateHandler<'delete_story', T>\nexport type BotReactionUpdateHandler<T = UpdateContext<BotReactionUpdate>> = ParsedUpdateHandler<'bot_reaction', T>\nexport type BotReactionCountUpdateHandler<T = UpdateContext<BotReactionCountUpdate>> = ParsedUpdateHandler<\n 'bot_reaction_count',\n T\n>\n\nexport type UpdateHandler =\n | RawUpdateHandler\n | NewMessageHandler\n | EditMessageHandler\n | MessageGroupHandler\n | DeleteMessageHandler\n | ChatMemberUpdateHandler\n | InlineQueryHandler\n | ChosenInlineResultHandler\n | CallbackQueryHandler\n | InlineCallbackQueryHandler\n | PollUpdateHandler\n | PollVoteHandler\n | UserStatusUpdateHandler\n | UserTypingHandler\n | HistoryReadHandler\n | BotStoppedHandler\n | BotChatJoinRequestHandler\n | ChatJoinRequestHandler\n | PreCheckoutQueryHandler\n | StoryUpdateHandler\n | DeleteStoryHandler\n | BotReactionUpdateHandler\n | BotReactionCountUpdateHandler\n\n// end-codegen\n"]}
|
package/esm/propagation.js
CHANGED
|
@@ -19,5 +19,5 @@ export var PropagationAction;
|
|
|
19
19
|
PropagationAction["StopChildren"] = "stop-children";
|
|
20
20
|
PropagationAction["Continue"] = "continue";
|
|
21
21
|
PropagationAction["ToScene"] = "scene";
|
|
22
|
-
})(PropagationAction
|
|
22
|
+
})(PropagationAction || (PropagationAction = {}));
|
|
23
23
|
//# sourceMappingURL=propagation.js.map
|
package/esm/propagation.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"propagation.js","sourceRoot":"","sources":["../../src/propagation.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,MAAM,CAAN,IAAY,iBAKX;AALD,WAAY,iBAAiB;IACzB,kCAAa,CAAA;IACb,mDAA8B,CAAA;IAC9B,0CAAqB,CAAA;IACrB,sCAAiB,CAAA;AACrB,CAAC,EALW,iBAAiB,
|
|
1
|
+
{"version":3,"file":"propagation.js","sourceRoot":"","sources":["../../src/propagation.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,MAAM,CAAN,IAAY,iBAKX;AALD,WAAY,iBAAiB;IACzB,kCAAa,CAAA;IACb,mDAA8B,CAAA;IAC9B,0CAAqB,CAAA;IACrB,sCAAiB,CAAA;AACrB,CAAC,EALW,iBAAiB,KAAjB,iBAAiB,QAK5B","sourcesContent":["/**\n * Propagation action.\n *\n * `Stop`: Stop the propagation of the event through any handler groups\n * in the current dispatcher. Does not prevent child dispatchers from\n * being executed.\n *\n * `StopChildren`: Stop the propagation of the event through any handler groups\n * in the current dispatcher, and any of its children. If current dispatcher\n * is a child, does not prevent from propagating to its siblings.\n *\n * `Continue`: Continue propagating the event inside the same handler group.\n *\n * `ToScene`: Used after using `state.enter()` to dispatch the update to the scene\n */\nexport enum PropagationAction {\n Stop = 'stop',\n StopChildren = 'stop-children',\n Continue = 'continue',\n ToScene = 'scene',\n}\n"]}
|
package/esm/state/index.d.ts
CHANGED
package/esm/state/index.js
CHANGED
package/esm/state/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/state/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAA;AACxB,cAAc,cAAc,CAAA;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/state/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAA;AACxB,cAAc,eAAe,CAAA;AAC7B,cAAc,sBAAsB,CAAA;AACpC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,mBAAmB,CAAA","sourcesContent":["export * from './key.js'\nexport * from './provider.js'\nexport * from './providers/index.js'\nexport * from './repository.js'\nexport * from './update-state.js'\n"]}
|
package/esm/state/key.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { MaybePromise, Peer } from '@mtcute/core';
|
|
2
2
|
import { CallbackQueryContext, MessageContext } from '../context/index.js';
|
|
3
3
|
/**
|
|
4
4
|
* Function that determines how the state key is derived.
|
|
@@ -8,7 +8,7 @@ import { CallbackQueryContext, MessageContext } from '../context/index.js';
|
|
|
8
8
|
* @param msg Message or callback from which to derive the key
|
|
9
9
|
* @param scene Current scene UID, or `null` if none
|
|
10
10
|
*/
|
|
11
|
-
export type StateKeyDelegate = (upd: MessageContext | CallbackQueryContext | Peer) =>
|
|
11
|
+
export type StateKeyDelegate = (upd: MessageContext | CallbackQueryContext | Peer) => MaybePromise<string | null>;
|
|
12
12
|
/**
|
|
13
13
|
* Default state key delegate.
|
|
14
14
|
*
|
package/esm/state/key.js
CHANGED
package/esm/state/key.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"key.js","sourceRoot":"","sources":["../../../src/state/key.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,
|
|
1
|
+
{"version":3,"file":"key.js","sourceRoot":"","sources":["../../../src/state/key.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAsB,MAAM,cAAc,CAAA;AAc9D;;;;;;;;;;GAUG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAqB,CAAC,GAAG,EAAiB,EAAE;IAC5E,IAAI,MAAM,IAAI,GAAG,EAAE;QACf,cAAc;QACd,OAAO,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC,CAAA;KACxB;IAED,IAAI,GAAG,CAAC,KAAK,KAAK,aAAa,EAAE;QAC7B,QAAQ,GAAG,CAAC,IAAI,CAAC,QAAQ,EAAE;YACvB,KAAK,SAAS,CAAC;YACf,KAAK,KAAK,CAAC;YACX,KAAK,SAAS;gBACV,OAAO,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,CAAA;YAC9B,KAAK,OAAO,CAAC;YACb,KAAK,YAAY,CAAC;YAClB,KAAK,WAAW;gBACZ,OAAO,GAAG,GAAG,CAAC,IAAI,CAAC,EAAE,IAAI,GAAG,CAAC,MAAM,CAAC,EAAE,EAAE,CAAA;YAC5C;gBACI,WAAW,CAAC,GAAG,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;SACrC;KACJ;IAED,IAAI,GAAG,CAAC,KAAK,KAAK,gBAAgB,EAAE;QAChC,IAAI,GAAG,CAAC,IAAI,CAAC,QAAQ,KAAK,SAAS;YAAE,OAAO,GAAG,GAAG,CAAC,IAAI,CAAC,EAAE,EAAE,CAAA;QAE5D,OAAO,GAAG,GAAG,CAAC,IAAI,CAAC,EAAE,IAAI,GAAG,CAAC,IAAI,CAAC,EAAE,EAAE,CAAA;KACzC;IAED,OAAO,IAAI,CAAA;AACf,CAAC,CAAA","sourcesContent":["import { assertNever, MaybePromise, Peer } from '@mtcute/core'\n\nimport { CallbackQueryContext, MessageContext } from '../context/index.js'\n\n/**\n * Function that determines how the state key is derived.\n *\n * The key is additionally prefixed with current scene, if any.\n *\n * @param msg Message or callback from which to derive the key\n * @param scene Current scene UID, or `null` if none\n */\nexport type StateKeyDelegate = (upd: MessageContext | CallbackQueryContext | Peer) => MaybePromise<string | null>\n\n/**\n * Default state key delegate.\n *\n * Derives key as follows:\n * - If private chat, `msg.chat.id`\n * - If group chat, `msg.chat.id + '_' + msg.sender.id`\n * - If channel, `msg.chat.id`\n * - If non-inline callback query:\n * - If in private chat (i.e. `upd.chatType === 'user'`), `upd.user.id`\n * - If in group/channel/supergroup (i.e. `upd.chatType !== 'user'`), `upd.chatId + '_' + upd.user.id`\n */\nexport const defaultStateKeyDelegate: StateKeyDelegate = (upd): string | null => {\n if ('type' in upd) {\n // User | Chat\n return String(upd.id)\n }\n\n if (upd._name === 'new_message') {\n switch (upd.chat.chatType) {\n case 'private':\n case 'bot':\n case 'channel':\n return String(upd.chat.id)\n case 'group':\n case 'supergroup':\n case 'gigagroup':\n return `${upd.chat.id}_${upd.sender.id}`\n default:\n assertNever(upd.chat.chatType)\n }\n }\n\n if (upd._name === 'callback_query') {\n if (upd.chat.chatType === 'private') return `${upd.user.id}`\n\n return `${upd.chat.id}_${upd.user.id}`\n }\n\n return null\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider.js","sourceRoot":"","sources":["../../../src/state/provider.ts"],"names":[],"mappings":"","sourcesContent":["import { IStorageProvider } from '@mtcute/core'\n\nimport { IStateRepository } from './repository.js'\n\nexport type IStateStorageProvider = IStorageProvider<{\n state: IStateRepository\n}>\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/state/providers/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA","sourcesContent":["export * from './memory.js'\nexport * from './sqlite.js'\n"]}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { MaybePromise, MemoryStorageDriver } from '@mtcute/core';
|
|
2
|
+
import { IStateStorageProvider } from '../provider.js';
|
|
3
|
+
import { IStateRepository } from '../repository.js';
|
|
4
|
+
interface StateDto {
|
|
5
|
+
value: string;
|
|
6
|
+
expiresAt?: number;
|
|
7
|
+
}
|
|
8
|
+
interface RateLimitDto {
|
|
9
|
+
reset: number;
|
|
10
|
+
remaining: number;
|
|
11
|
+
}
|
|
12
|
+
declare class MemoryStateRepository implements IStateRepository {
|
|
13
|
+
readonly _driver: MemoryStorageDriver;
|
|
14
|
+
constructor(_driver: MemoryStorageDriver);
|
|
15
|
+
readonly state: Map<string, StateDto>;
|
|
16
|
+
setState(key: string, state: string, ttl?: number | undefined): void;
|
|
17
|
+
getState(key: string, now: number): string | null;
|
|
18
|
+
deleteState(key: string): void;
|
|
19
|
+
vacuum(now: number): void;
|
|
20
|
+
readonly rl: Map<string, RateLimitDto>;
|
|
21
|
+
getRateLimit(key: string, now: number, limit: number, window: number): [number, number];
|
|
22
|
+
resetRateLimit(key: string): MaybePromise<void>;
|
|
23
|
+
}
|
|
24
|
+
export declare class MemoryStateStorage implements IStateStorageProvider {
|
|
25
|
+
readonly driver: MemoryStorageDriver;
|
|
26
|
+
constructor(driver?: MemoryStorageDriver);
|
|
27
|
+
readonly state: MemoryStateRepository;
|
|
28
|
+
}
|
|
29
|
+
export {};
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { MemoryStorageDriver } from '@mtcute/core';
|
|
2
|
+
class MemoryStateRepository {
|
|
3
|
+
constructor(_driver) {
|
|
4
|
+
this._driver = _driver;
|
|
5
|
+
this.state = this._driver.getState('dispatcher_fsm', () => new Map());
|
|
6
|
+
this.rl = this._driver.getState('rl', () => new Map());
|
|
7
|
+
}
|
|
8
|
+
setState(key, state, ttl) {
|
|
9
|
+
this.state.set(key, {
|
|
10
|
+
value: state,
|
|
11
|
+
expiresAt: ttl ? Date.now() + ttl * 1000 : undefined,
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
getState(key, now) {
|
|
15
|
+
const state = this.state.get(key);
|
|
16
|
+
if (!state)
|
|
17
|
+
return null;
|
|
18
|
+
if (state.expiresAt && state.expiresAt < now) {
|
|
19
|
+
this.state.delete(key);
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
return state.value;
|
|
23
|
+
}
|
|
24
|
+
deleteState(key) {
|
|
25
|
+
this.state.delete(key);
|
|
26
|
+
}
|
|
27
|
+
vacuum(now) {
|
|
28
|
+
for (const [key, state] of this.state.entries()) {
|
|
29
|
+
if (state.expiresAt && state.expiresAt < now) {
|
|
30
|
+
this.state.delete(key);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
for (const [key, state] of this.rl.entries()) {
|
|
34
|
+
if (state.reset < now) {
|
|
35
|
+
this.rl.delete(key);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
getRateLimit(key, now, limit, window) {
|
|
40
|
+
// leaky bucket
|
|
41
|
+
const item = this.rl.get(key);
|
|
42
|
+
if (!item) {
|
|
43
|
+
const state = {
|
|
44
|
+
reset: now + window * 1000,
|
|
45
|
+
remaining: limit,
|
|
46
|
+
};
|
|
47
|
+
this.rl.set(key, state);
|
|
48
|
+
return [state.remaining, state.reset];
|
|
49
|
+
}
|
|
50
|
+
if (item.reset < now) {
|
|
51
|
+
// expired
|
|
52
|
+
const state = {
|
|
53
|
+
reset: now + window * 1000,
|
|
54
|
+
remaining: limit,
|
|
55
|
+
};
|
|
56
|
+
this.rl.set(key, state);
|
|
57
|
+
return [state.remaining, state.reset];
|
|
58
|
+
}
|
|
59
|
+
item.remaining = item.remaining > 0 ? item.remaining - 1 : 0;
|
|
60
|
+
return [item.remaining, item.reset];
|
|
61
|
+
}
|
|
62
|
+
resetRateLimit(key) {
|
|
63
|
+
this.rl.delete(key);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
export class MemoryStateStorage {
|
|
67
|
+
constructor(driver = new MemoryStorageDriver()) {
|
|
68
|
+
this.driver = driver;
|
|
69
|
+
this.state = new MemoryStateRepository(this.driver);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
//# sourceMappingURL=memory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory.js","sourceRoot":"","sources":["../../../../src/state/providers/memory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,mBAAmB,EAAE,MAAM,cAAc,CAAA;AAehE,MAAM,qBAAqB;IACvB,YAAqB,OAA4B;QAA5B,YAAO,GAAP,OAAO,CAAqB;QAExC,UAAK,GAAG,IA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AAc,CAAC,GAAW;QACtB,IAAI,CAAC,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;IACvB,CAAC;CACJ;AAED,MAAM,OAAO,kBAAkB;IAC3B,YAAqB,SAA8B,IAAI,mBAAmB,EAAE;QAAvD,WAAM,GAAN,MAAM,CAAiD;QAEnE,UAAK,GAAG,IAAI,qBAAqB,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;IAFwB,CAAC;CAGnF","sourcesContent":["import { MaybePromise, MemoryStorageDriver } from '@mtcute/core'\n\nimport { IStateStorageProvider } from '../provider.js'\nimport { IStateRepository } from '../repository.js'\n\ninterface StateDto {\n value: string\n expiresAt?: number\n}\n\ninterface RateLimitDto {\n reset: number\n remaining: number\n}\n\nclass MemoryStateRepository implements IStateRepository {\n constructor(readonly _driver: MemoryStorageDriver) {}\n\n readonly state = this._driver.getState<Map<string, StateDto>>('dispatcher_fsm', () => new Map())\n\n setState(key: string, state: string, ttl?: number | undefined): void {\n this.state.set(key, {\n value: state,\n expiresAt: ttl ? Date.now() + ttl * 1000 : undefined,\n })\n }\n\n getState(key: string, now: number): string | null {\n const state = this.state.get(key)\n if (!state) return null\n\n if (state.expiresAt && state.expiresAt < now) {\n this.state.delete(key)\n\n return null\n }\n\n return state.value\n }\n\n deleteState(key: string): void {\n this.state.delete(key)\n }\n\n vacuum(now: number): void {\n for (const [key, state] of this.state.entries()) {\n if (state.expiresAt && state.expiresAt < now) {\n this.state.delete(key)\n }\n }\n\n for (const [key, state] of this.rl.entries()) {\n if (state.reset < now) {\n this.rl.delete(key)\n }\n }\n }\n\n readonly rl = this._driver.getState<Map<string, RateLimitDto>>('rl', () => new Map())\n\n getRateLimit(key: string, now: number, limit: number, window: number): [number, number] {\n // leaky bucket\n const item = this.rl.get(key)\n\n if (!item) {\n const state: RateLimitDto = {\n reset: now + window * 1000,\n remaining: limit,\n }\n\n this.rl.set(key, state)\n\n return [state.remaining, state.reset]\n }\n\n if (item.reset < now) {\n // expired\n\n const state: RateLimitDto = {\n reset: now + window * 1000,\n remaining: limit,\n }\n\n this.rl.set(key, state)\n\n return [state.remaining, state.reset]\n }\n\n item.remaining = item.remaining > 0 ? item.remaining - 1 : 0\n\n return [item.remaining, item.reset]\n }\n\n resetRateLimit(key: string): MaybePromise<void> {\n this.rl.delete(key)\n }\n}\n\nexport class MemoryStateStorage implements IStateStorageProvider {\n constructor(readonly driver: MemoryStorageDriver = new MemoryStorageDriver()) {}\n\n readonly state = new MemoryStateRepository(this.driver)\n}\n"]}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { MaybePromise } from '@mtcute/core';
|
|
2
|
+
import type { SqliteStorage, SqliteStorageDriver } from '@mtcute/sqlite';
|
|
3
|
+
import { IStateStorageProvider } from '../provider.js';
|
|
4
|
+
import { IStateRepository } from '../repository.js';
|
|
5
|
+
declare class SqliteStateRepository implements IStateRepository {
|
|
6
|
+
readonly _driver: SqliteStorageDriver;
|
|
7
|
+
constructor(_driver: SqliteStorageDriver);
|
|
8
|
+
private _setState;
|
|
9
|
+
setState(key: string, state: string, ttl?: number | undefined): MaybePromise<void>;
|
|
10
|
+
private _getState;
|
|
11
|
+
getState(key: string, now: number): MaybePromise<string | null>;
|
|
12
|
+
private _deleteState;
|
|
13
|
+
deleteState(key: string): MaybePromise<void>;
|
|
14
|
+
private _deleteOldState;
|
|
15
|
+
private _deleteOldRl;
|
|
16
|
+
vacuum(now: number): MaybePromise<void>;
|
|
17
|
+
private _setRl;
|
|
18
|
+
private _getRl;
|
|
19
|
+
private _deleteRl;
|
|
20
|
+
getRateLimit(key: string, now: number, limit: number, window: number): [number, number];
|
|
21
|
+
resetRateLimit(key: string): MaybePromise<void>;
|
|
22
|
+
}
|
|
23
|
+
export declare class SqliteStateStorage implements IStateStorageProvider {
|
|
24
|
+
readonly driver: SqliteStorageDriver;
|
|
25
|
+
constructor(driver: SqliteStorageDriver);
|
|
26
|
+
static from(provider: SqliteStorage): SqliteStateStorage;
|
|
27
|
+
readonly state: SqliteStateRepository;
|
|
28
|
+
}
|
|
29
|
+
export {};
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
class SqliteStateRepository {
|
|
2
|
+
constructor(_driver) {
|
|
3
|
+
this._driver = _driver;
|
|
4
|
+
_driver.registerMigration('state', 1, (db) => {
|
|
5
|
+
db.exec(`
|
|
6
|
+
create table fsm_state (
|
|
7
|
+
key text primary key,
|
|
8
|
+
value text not null,
|
|
9
|
+
expires_at integer
|
|
10
|
+
);
|
|
11
|
+
create table rl_state (
|
|
12
|
+
key text primary key,
|
|
13
|
+
reset integer not null,
|
|
14
|
+
remaining integer not null
|
|
15
|
+
);
|
|
16
|
+
`);
|
|
17
|
+
});
|
|
18
|
+
_driver.onLoad(() => {
|
|
19
|
+
this._setState = _driver.db.prepare('insert or replace into fsm_state (key, value, expires_at) values (?, ?, ?)');
|
|
20
|
+
this._getState = _driver.db.prepare('select value, expires_at from fsm_state where key = ?');
|
|
21
|
+
this._deleteState = _driver.db.prepare('delete from fsm_state where key = ?');
|
|
22
|
+
this._deleteOldState = _driver.db.prepare('delete from fsm_state where expires_at < ?');
|
|
23
|
+
this._setRl = _driver.db.prepare('insert or replace into rl_state (key, reset, remaining) values (?, ?, ?)');
|
|
24
|
+
this._getRl = _driver.db.prepare('select reset, remaining from rl_state where key = ?');
|
|
25
|
+
this._deleteRl = _driver.db.prepare('delete from rl_state where key = ?');
|
|
26
|
+
this._deleteOldRl = _driver.db.prepare('delete from rl_state where reset < ?');
|
|
27
|
+
});
|
|
28
|
+
_driver.registerLegacyMigration('state', (db) => {
|
|
29
|
+
// not too important information, just drop the table
|
|
30
|
+
db.exec('drop table state');
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
setState(key, state, ttl) {
|
|
34
|
+
this._setState.run(key, state, ttl ? Date.now() + ttl * 1000 : undefined);
|
|
35
|
+
}
|
|
36
|
+
getState(key, now) {
|
|
37
|
+
const res_ = this._getState.get(key);
|
|
38
|
+
if (!res_)
|
|
39
|
+
return null;
|
|
40
|
+
const res = res_;
|
|
41
|
+
if (res.expires_at && res.expires_at < now) {
|
|
42
|
+
this._deleteState.run(key);
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
return res.value;
|
|
46
|
+
}
|
|
47
|
+
deleteState(key) {
|
|
48
|
+
this._deleteState.run(key);
|
|
49
|
+
}
|
|
50
|
+
vacuum(now) {
|
|
51
|
+
this._deleteOldState.run(now);
|
|
52
|
+
this._deleteOldRl.run(now);
|
|
53
|
+
}
|
|
54
|
+
getRateLimit(key, now, limit, window) {
|
|
55
|
+
const val = this._getRl.get(key);
|
|
56
|
+
// hot path. rate limit fsm entries always have an expiration date
|
|
57
|
+
if (!val || val.reset < now) {
|
|
58
|
+
// expired or does not exist
|
|
59
|
+
const item = {
|
|
60
|
+
reset: now + window * 1000,
|
|
61
|
+
remaining: limit,
|
|
62
|
+
};
|
|
63
|
+
this._setRl.run(key, item.reset, item.remaining);
|
|
64
|
+
return [item.remaining, item.reset];
|
|
65
|
+
}
|
|
66
|
+
if (val.remaining > 0) {
|
|
67
|
+
val.remaining -= 1;
|
|
68
|
+
this._setRl.run(key, val.reset, val.remaining);
|
|
69
|
+
}
|
|
70
|
+
return [val.remaining, val.reset];
|
|
71
|
+
}
|
|
72
|
+
resetRateLimit(key) {
|
|
73
|
+
this._deleteRl.run(key);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
export class SqliteStateStorage {
|
|
77
|
+
constructor(driver) {
|
|
78
|
+
this.driver = driver;
|
|
79
|
+
this.state = new SqliteStateRepository(this.driver);
|
|
80
|
+
}
|
|
81
|
+
static from(provider) {
|
|
82
|
+
return new SqliteStateStorage(provider.driver);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
//# sourceMappingURL=sqlite.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sqlite.js","sourceRoot":"","sources":["../../../../src/state/providers/sqlite.ts"],"names":[],"mappings":"AAgBA,MAAM,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AE,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,SAAS,CAAC,CAAA;YAEhD,OAAO,CAAC,IAAI,CAAC,SAAS,EAAE,IAAI,CAAC,KAAK,CAAC,CAAA;SACtC;QAED,IAAI,GAAG,CAAC,SAAS,GAAG,CAAC,EAAE;YACnB,GAAG,CAAC,SAAS,IAAI,CAAC,CAAA;YAElB,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,GAAG,CAAC,KAAK,EAAE,GAAG,CAAC,SAAS,CAAC,CAAA;SACjD;QAED,OAAO,CAAC,GAAG,CAAC,SAAS,EAAE,GAAG,CAAC,KAAK,CAAC,CAAA;IACrC,CAAC;IAED,cAAc,CAAC,GAAW;QACtB,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;IAC3B,CAAC;CACJ;AAED,MAAM,OAAO,kBAAkB;IAC3B,YAAqB,MAA2B;QAA3B,WAAM,GAAN,MAAM,CAAqB;QAMvC,UAAK,GAAG,IAAI,qBAAqB,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;IANJ,CAAC;IAEpD,MAAM,CAAC,IAAI,CAAC,QAAuB;QAC/B,OAAO,IAAI,kBAAkB,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAA;IAClD,CAAC;CAGJ","sourcesContent":["import { MaybePromise } from '@mtcute/core'\nimport type { SqliteStorage, SqliteStorageDriver, Statement } from '@mtcute/sqlite'\n\nimport { IStateStorageProvider } from '../provider.js'\nimport { IStateRepository } from '../repository.js'\n\ninterface StateDto {\n value: string\n expires_at: number | null\n}\n\ninterface RateLimitDto {\n reset: number\n remaining: number\n}\n\nclass SqliteStateRepository implements IStateRepository {\n constructor(readonly _driver: SqliteStorageDriver) {\n _driver.registerMigration('state', 1, (db) => {\n db.exec(`\n create table fsm_state (\n key text primary key,\n value text not null,\n expires_at integer\n );\n create table rl_state (\n key text primary key,\n reset integer not null,\n remaining integer not null\n );\n `)\n })\n _driver.onLoad(() => {\n this._setState = _driver.db.prepare(\n 'insert or replace into fsm_state (key, value, expires_at) values (?, ?, ?)',\n )\n this._getState = _driver.db.prepare('select value, expires_at from fsm_state where key = ?')\n this._deleteState = _driver.db.prepare('delete from fsm_state where key = ?')\n this._deleteOldState = _driver.db.prepare('delete from fsm_state where expires_at < ?')\n\n this._setRl = _driver.db.prepare('insert or replace into rl_state (key, reset, remaining) values (?, ?, ?)')\n this._getRl = _driver.db.prepare('select reset, remaining from rl_state where key = ?')\n this._deleteRl = _driver.db.prepare('delete from rl_state where key = ?')\n this._deleteOldRl = _driver.db.prepare('delete from rl_state where reset < ?')\n })\n _driver.registerLegacyMigration('state', (db) => {\n // not too important information, just drop the table\n db.exec('drop table state')\n })\n }\n\n private _setState!: Statement\n setState(key: string, state: string, ttl?: number | undefined): MaybePromise<void> {\n this._setState.run(key, state, ttl ? Date.now() + ttl * 1000 : undefined)\n }\n\n private _getState!: Statement\n getState(key: string, now: number): MaybePromise<string | null> {\n const res_ = this._getState.get(key)\n if (!res_) return null\n const res = res_ as StateDto\n\n if (res.expires_at && res.expires_at < now) {\n this._deleteState.run(key)\n\n return null\n }\n\n return res.value\n }\n\n private _deleteState!: Statement\n deleteState(key: string): MaybePromise<void> {\n this._deleteState.run(key)\n }\n\n private _deleteOldState!: Statement\n private _deleteOldRl!: Statement\n vacuum(now: number): MaybePromise<void> {\n this._deleteOldState.run(now)\n this._deleteOldRl.run(now)\n }\n\n private _setRl!: Statement\n private _getRl!: Statement\n private _deleteRl!: Statement\n\n getRateLimit(key: string, now: number, limit: number, window: number): [number, number] {\n const val = this._getRl.get(key) as RateLimitDto | undefined\n\n // hot path. rate limit fsm entries always have an expiration date\n\n if (!val || val.reset < now) {\n // expired or does not exist\n const item: RateLimitDto = {\n reset: now + window * 1000,\n remaining: limit,\n }\n\n this._setRl.run(key, item.reset, item.remaining)\n\n return [item.remaining, item.reset]\n }\n\n if (val.remaining > 0) {\n val.remaining -= 1\n\n this._setRl.run(key, val.reset, val.remaining)\n }\n\n return [val.remaining, val.reset]\n }\n\n resetRateLimit(key: string): MaybePromise<void> {\n this._deleteRl.run(key)\n }\n}\n\nexport class SqliteStateStorage implements IStateStorageProvider {\n constructor(readonly driver: SqliteStorageDriver) {}\n\n static from(provider: SqliteStorage) {\n return new SqliteStateStorage(provider.driver)\n }\n\n readonly state = new SqliteStateRepository(this.driver)\n}\n"]}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { MaybePromise } from '@mtcute/core';
|
|
2
|
+
/**
|
|
3
|
+
* Interface for FSM storage for the dispatcher.
|
|
4
|
+
*
|
|
5
|
+
* All of the officially supported storages already implement
|
|
6
|
+
* this interface, so you can just re-use it.
|
|
7
|
+
*
|
|
8
|
+
* Current scene is a special case of a `string` state,
|
|
9
|
+
* Most of the time you can just store it the same way
|
|
10
|
+
* as normal state, prefixing with something like `$current_state_`
|
|
11
|
+
* (scene name can't start with `$`).
|
|
12
|
+
* Alternatively, you can store them as simple strings
|
|
13
|
+
*/
|
|
14
|
+
export interface IStateRepository {
|
|
15
|
+
/**
|
|
16
|
+
* Retrieve state from the storage
|
|
17
|
+
* If state is not found or has expired, return `null`
|
|
18
|
+
*
|
|
19
|
+
* @param key Key of the state, as defined by {@link StateKeyDelegate}
|
|
20
|
+
*/
|
|
21
|
+
getState(key: string, now: number): MaybePromise<string | null>;
|
|
22
|
+
/**
|
|
23
|
+
* Save state to the storage
|
|
24
|
+
*
|
|
25
|
+
* @param key Key of the state, as defined by {@link StateKeyDelegate}
|
|
26
|
+
* @param state String representing the state
|
|
27
|
+
* @param ttl TTL for the state, in seconds
|
|
28
|
+
*/
|
|
29
|
+
setState(key: string, state: string, ttl?: number): MaybePromise<void>;
|
|
30
|
+
/**
|
|
31
|
+
* Delete state from the storage
|
|
32
|
+
*
|
|
33
|
+
* @param key Key of the state, as defined by {@link StateKeyDelegate}
|
|
34
|
+
*/
|
|
35
|
+
deleteState(key: string): MaybePromise<void>;
|
|
36
|
+
/**
|
|
37
|
+
* Clean up expired states and rate limits.
|
|
38
|
+
*
|
|
39
|
+
* @param now Current unix time in ms
|
|
40
|
+
*/
|
|
41
|
+
vacuum(now: number): MaybePromise<void>;
|
|
42
|
+
/**
|
|
43
|
+
* Get information about a rate limit.
|
|
44
|
+
*
|
|
45
|
+
* It is recommended that you use sliding window or leaky bucket
|
|
46
|
+
* to implement rate limiting ([learn more](https://konghq.com/blog/how-to-design-a-scalable-rate-limiting-algorithm/)),
|
|
47
|
+
*
|
|
48
|
+
* @param key Key of the rate limit
|
|
49
|
+
* @param now Current unix time in ms
|
|
50
|
+
* @param limit Maximum number of requests in `window`
|
|
51
|
+
* @param window Window size in seconds
|
|
52
|
+
* @returns Tuple containing the number of remaining and
|
|
53
|
+
* unix time in ms when the user can try again
|
|
54
|
+
*/
|
|
55
|
+
getRateLimit(key: string, now: number, limit: number, window: number): MaybePromise<[number, number]>;
|
|
56
|
+
/**
|
|
57
|
+
* Reset a rate limit.
|
|
58
|
+
*
|
|
59
|
+
* @param key Key of the rate limit
|
|
60
|
+
*/
|
|
61
|
+
resetRateLimit(key: string): MaybePromise<void>;
|
|
62
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"repository.js","sourceRoot":"","sources":["../../../src/state/repository.ts"],"names":[],"mappings":"","sourcesContent":["import { MaybePromise } from '@mtcute/core'\n\n/**\n * Interface for FSM storage for the dispatcher.\n *\n * All of the officially supported storages already implement\n * this interface, so you can just re-use it.\n *\n * Current scene is a special case of a `string` state,\n * Most of the time you can just store it the same way\n * as normal state, prefixing with something like `$current_state_`\n * (scene name can't start with `$`).\n * Alternatively, you can store them as simple strings\n */\nexport interface IStateRepository {\n /**\n * Retrieve state from the storage\n * If state is not found or has expired, return `null`\n *\n * @param key Key of the state, as defined by {@link StateKeyDelegate}\n */\n getState(key: string, now: number): MaybePromise<string | null>\n\n /**\n * Save state to the storage\n *\n * @param key Key of the state, as defined by {@link StateKeyDelegate}\n * @param state String representing the state\n * @param ttl TTL for the state, in seconds\n */\n setState(key: string, state: string, ttl?: number): MaybePromise<void>\n\n /**\n * Delete state from the storage\n *\n * @param key Key of the state, as defined by {@link StateKeyDelegate}\n */\n deleteState(key: string): MaybePromise<void>\n\n /**\n * Clean up expired states and rate limits.\n *\n * @param now Current unix time in ms\n */\n vacuum(now: number): MaybePromise<void>\n\n /**\n * Get information about a rate limit.\n *\n * It is recommended that you use sliding window or leaky bucket\n * to implement rate limiting ([learn more](https://konghq.com/blog/how-to-design-a-scalable-rate-limiting-algorithm/)),\n *\n * @param key Key of the rate limit\n * @param now Current unix time in ms\n * @param limit Maximum number of requests in `window`\n * @param window Window size in seconds\n * @returns Tuple containing the number of remaining and\n * unix time in ms when the user can try again\n */\n getRateLimit(key: string, now: number, limit: number, window: number): MaybePromise<[number, number]>\n\n /**\n * Reset a rate limit.\n *\n * @param key Key of the rate limit\n */\n resetRateLimit(key: string): MaybePromise<void>\n}\n"]}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { IStateStorageProvider } from './provider.js';
|
|
2
|
+
export declare class StateService {
|
|
3
|
+
readonly provider: IStateStorageProvider;
|
|
4
|
+
constructor(provider: IStateStorageProvider);
|
|
5
|
+
private _cache;
|
|
6
|
+
private _vacuumTimer?;
|
|
7
|
+
private _loaded;
|
|
8
|
+
private _load;
|
|
9
|
+
load(): Promise<void>;
|
|
10
|
+
destroy(): Promise<void>;
|
|
11
|
+
getState<T>(key: string): Promise<T | null>;
|
|
12
|
+
setState<T>(key: string, state: T, ttl?: number): Promise<void>;
|
|
13
|
+
deleteState(key: string): Promise<void>;
|
|
14
|
+
getCurrentScene(key: string): Promise<string | null>;
|
|
15
|
+
setCurrentScene(key: string, scene: string, ttl?: number): Promise<void>;
|
|
16
|
+
deleteCurrentScene(key: string): Promise<void>;
|
|
17
|
+
getRateLimit(key: string, limit: number, window: number): import("@mtcute/core").MaybePromise<[number, number]>;
|
|
18
|
+
resetRateLimit(key: string): import("@mtcute/core").MaybePromise<void>;
|
|
19
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { asyncResettable, LruMap } from '@mtcute/core/utils.js';
|
|
2
|
+
const makeCurrentSceneKey = (key) => `$current_scene_${key}`;
|
|
3
|
+
export class StateService {
|
|
4
|
+
constructor(provider) {
|
|
5
|
+
this.provider = provider;
|
|
6
|
+
this._cache = new LruMap(100);
|
|
7
|
+
this._loaded = false;
|
|
8
|
+
this._load = asyncResettable(async () => {
|
|
9
|
+
await this.provider.driver.load?.();
|
|
10
|
+
this._loaded = true;
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
async load() {
|
|
14
|
+
await this._load.run();
|
|
15
|
+
this._vacuumTimer = setInterval(() => {
|
|
16
|
+
Promise.resolve(this.provider.state.vacuum(Date.now())).catch(() => { });
|
|
17
|
+
}, 300000);
|
|
18
|
+
}
|
|
19
|
+
async destroy() {
|
|
20
|
+
await this.provider.driver.save?.();
|
|
21
|
+
await this.provider.driver.destroy?.();
|
|
22
|
+
clearInterval(this._vacuumTimer);
|
|
23
|
+
this._loaded = false;
|
|
24
|
+
}
|
|
25
|
+
async getState(key) {
|
|
26
|
+
if (!this._loaded)
|
|
27
|
+
await this.load();
|
|
28
|
+
const cached = this._cache.get(key);
|
|
29
|
+
if (cached)
|
|
30
|
+
return cached;
|
|
31
|
+
const state = await this.provider.state.getState(key, Date.now());
|
|
32
|
+
if (!state)
|
|
33
|
+
return null;
|
|
34
|
+
return JSON.parse(state);
|
|
35
|
+
}
|
|
36
|
+
async setState(key, state, ttl) {
|
|
37
|
+
if (!this._loaded)
|
|
38
|
+
await this.load();
|
|
39
|
+
this._cache.set(key, state);
|
|
40
|
+
await this.provider.state.setState(key, JSON.stringify(state), ttl);
|
|
41
|
+
}
|
|
42
|
+
async deleteState(key) {
|
|
43
|
+
if (!this._loaded)
|
|
44
|
+
await this.load();
|
|
45
|
+
this._cache.delete(key);
|
|
46
|
+
await this.provider.state.deleteState(key);
|
|
47
|
+
}
|
|
48
|
+
getCurrentScene(key) {
|
|
49
|
+
return this.getState(makeCurrentSceneKey(key));
|
|
50
|
+
}
|
|
51
|
+
setCurrentScene(key, scene, ttl) {
|
|
52
|
+
return this.setState(makeCurrentSceneKey(key), scene, ttl);
|
|
53
|
+
}
|
|
54
|
+
deleteCurrentScene(key) {
|
|
55
|
+
return this.deleteState(makeCurrentSceneKey(key));
|
|
56
|
+
}
|
|
57
|
+
getRateLimit(key, limit, window) {
|
|
58
|
+
return this.provider.state.getRateLimit(key, Date.now(), limit, window);
|
|
59
|
+
}
|
|
60
|
+
resetRateLimit(key) {
|
|
61
|
+
return this.provider.state.resetRateLimit(key);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
//# sourceMappingURL=service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"service.js","sourceRoot":"","sources":["../../../src/state/service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAA;AAI/D,MAAM,mBAAmB,GAAG,CAAC,GAAW,EAAE,EAAE,CAAC,kBAAkB,GAAG,EAAE,CAAA;AAEpE,MAAM,OAAO,YAAY;IACrB,YAAqB,QAA+B;QAA/B,aAAQ,GAAR,QAAQ,CAAuB;QAE5C,WAAM,GAA4B,IAAI,MAAM,CAAC,GAAG,CAAC,CAAA;QAGjD,YAAO,GAAG,KAAK,CAAA;QACf,UAAK,GAAG,eAAe,CAAC,KAAK,IAAI,EAAE;YACvC,MAAM,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,EAAE,EAAE,CAAA;YACnC,IAAI,CAAC,OAAO,GAAG,IAAI,CAAA;QACvB,CAAC,CAAC,CAAA;IATqD,CAAC;IAUxD,KAAK,CAAC,IAAI;QACN,MAAM,IAAI,CAAC,KAAK,CAAC,GAAG,EAAE,CAAA;QACtB,IAAI,CAAC,YAAY,GAAG,WAAW,CAAC,GAAG,EAAE;YACjC,OAAO,CAAC,OAAO,CAAC,IA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CAAC,SAAS,CAAC,KAAK,CAAC,EAAE,GAAG,CAAC,CAAA;IACvE,CAAC;IAED,KAAK,CAAC,WAAW,CAAC,GAAW;QACzB,IAAI,CAAC,IAAI,CAAC,OAAO;YAAE,MAAM,IAAI,CAAC,IAAI,EAAE,CAAA;QAEpC,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;QACvB,MAAM,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,WAAW,CAAC,GAAG,CAAC,CAAA;IAC9C,CAAC;IAED,eAAe,CAAC,GAAW;QACvB,OAAO,IAAI,CAAC,QAAQ,CAAC,mBAAmB,CAAC,GAAG,CAAC,CAAC,CAAA;IAClD,CAAC;IAED,eAAe,CAAC,GAAW,EAAE,KAAa,EAAE,GAAY;QACpD,OAAO,IAAI,CAAC,QAAQ,CAAC,mBAAmB,CAAC,GAAG,CAAC,EAAE,KAAK,EAAE,GAAG,CAAC,CAAA;IAC9D,CAAC;IAED,kBAAkB,CAAC,GAAW;QAC1B,OAAO,IAAI,CAAC,WAAW,CAAC,mBAAmB,CAAC,GAAG,CAAC,CAAC,CAAA;IACrD,CAAC;IAED,YAAY,CAAC,GAAW,EAAE,KAAa,EAAE,MAAc;QACnD,OAAO,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,YAAY,CAAC,GAAG,EAAE,IAAI,CAAC,GAAG,EAAE,EAAE,KAAK,EAAE,MAAM,CAAC,CAAA;IAC3E,CAAC;IAED,cAAc,CAAC,GAAW;QACtB,OAAO,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,cAAc,CAAC,GAAG,CAAC,CAAA;IAClD,CAAC;CACJ","sourcesContent":["import { asyncResettable, LruMap } from '@mtcute/core/utils.js'\n\nimport { IStateStorageProvider } from './provider.js'\n\nconst makeCurrentSceneKey = (key: string) => `$current_scene_${key}`\n\nexport class StateService {\n constructor(readonly provider: IStateStorageProvider) {}\n\n private _cache: LruMap<string, unknown> = new LruMap(100)\n private _vacuumTimer?: NodeJS.Timeout\n\n private _loaded = false\n private _load = asyncResettable(async () => {\n await this.provider.driver.load?.()\n this._loaded = true\n })\n async load() {\n await this._load.run()\n this._vacuumTimer = setInterval(() => {\n Promise.resolve(this.provider.state.vacuum(Date.now())).catch(() => {})\n }, 300_000)\n }\n\n async destroy() {\n await this.provider.driver.save?.()\n await this.provider.driver.destroy?.()\n clearInterval(this._vacuumTimer)\n this._loaded = false\n }\n\n async getState<T>(key: string): Promise<T | null> {\n if (!this._loaded) await this.load()\n\n const cached = this._cache.get(key)\n if (cached) return cached as T\n\n const state = await this.provider.state.getState(key, Date.now())\n if (!state) return null\n\n return JSON.parse(state) as T\n }\n\n async setState<T>(key: string, state: T, ttl?: number): Promise<void> {\n if (!this._loaded) await this.load()\n\n this._cache.set(key, state)\n await this.provider.state.setState(key, JSON.stringify(state), ttl)\n }\n\n async deleteState(key: string): Promise<void> {\n if (!this._loaded) await this.load()\n\n this._cache.delete(key)\n await this.provider.state.deleteState(key)\n }\n\n getCurrentScene(key: string): Promise<string | null> {\n return this.getState(makeCurrentSceneKey(key))\n }\n\n setCurrentScene(key: string, scene: string, ttl?: number): Promise<void> {\n return this.setState(makeCurrentSceneKey(key), scene, ttl)\n }\n\n deleteCurrentScene(key: string): Promise<void> {\n return this.deleteState(makeCurrentSceneKey(key))\n }\n\n getRateLimit(key: string, limit: number, window: number) {\n return this.provider.state.getRateLimit(key, Date.now(), limit, window)\n }\n\n resetRateLimit(key: string) {\n return this.provider.state.resetRateLimit(key)\n }\n}\n"]}
|