chz-telegram-bot 0.7.19 → 0.7.21

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 (34) hide show
  1. package/dist/entities/actions/commandAction.d.ts +1 -0
  2. package/dist/entities/actions/commandAction.d.ts.map +1 -1
  3. package/dist/entities/actions/commandAction.js +5 -3
  4. package/dist/entities/actions/inlineQueryAction.d.ts.map +1 -1
  5. package/dist/entities/actions/inlineQueryAction.js +2 -1
  6. package/dist/entities/actions/replyCaptureAction.d.ts.map +1 -1
  7. package/dist/entities/actions/replyCaptureAction.js +2 -1
  8. package/dist/entities/actions/scheduledAction.d.ts +1 -0
  9. package/dist/entities/actions/scheduledAction.d.ts.map +1 -1
  10. package/dist/entities/actions/scheduledAction.js +3 -2
  11. package/dist/helpers/constants.d.ts +8 -0
  12. package/dist/helpers/constants.d.ts.map +1 -0
  13. package/dist/helpers/constants.js +7 -0
  14. package/dist/helpers/mapUtils.d.ts +1 -0
  15. package/dist/helpers/mapUtils.d.ts.map +1 -1
  16. package/dist/helpers/mapUtils.js +8 -0
  17. package/dist/services/actionProcessingService.d.ts.map +1 -1
  18. package/dist/services/actionProcessingService.js +3 -3
  19. package/dist/services/actionProcessors/commandActionProcessor.d.ts.map +1 -1
  20. package/dist/services/actionProcessors/commandActionProcessor.js +21 -23
  21. package/dist/services/actionProcessors/inlineQueryActionProcessor.d.ts +1 -2
  22. package/dist/services/actionProcessors/inlineQueryActionProcessor.d.ts.map +1 -1
  23. package/dist/services/actionProcessors/inlineQueryActionProcessor.js +30 -54
  24. package/dist/services/actionProcessors/scheduledActionProcessor.d.ts.map +1 -1
  25. package/dist/services/actionProcessors/scheduledActionProcessor.js +16 -24
  26. package/dist/services/jsonFileStorage.d.ts +2 -1
  27. package/dist/services/jsonFileStorage.d.ts.map +1 -1
  28. package/dist/services/jsonFileStorage.js +7 -5
  29. package/dist/services/responseProcessingQueue.d.ts.map +1 -1
  30. package/dist/services/responseProcessingQueue.js +11 -11
  31. package/dist/services/telegramApi.d.ts +0 -1
  32. package/dist/services/telegramApi.d.ts.map +1 -1
  33. package/dist/services/telegramApi.js +62 -58
  34. package/package.json +5 -4
@@ -22,6 +22,7 @@ export declare class CommandAction<TActionState extends IActionState> implements
22
22
  readonly condition: CommandCondition<TActionState>;
23
23
  readonly stateConstructor: () => TActionState;
24
24
  readonly readmeFactory: (botName: string) => string;
25
+ readonly semaphoreFactory: () => Semaphore;
25
26
  private lastCustomCooldown;
26
27
  constructor(trigger: CommandTrigger | CommandTrigger[], handler: CommandHandler<TActionState>, name: string, providers: CommandActionProviders, maxAllowedSimultaniousExecutions: number, condition: CommandCondition<TActionState>, stateConstructor: () => TActionState, readmeFactory: (botName: string) => string);
27
28
  exec(ctx: MessageContextInternal<TActionState>): Promise<BotResponse[]>;
@@ -1 +1 @@
1
- {"version":3,"file":"commandAction.d.ts","sourceRoot":"","sources":["../../../src/entities/actions/commandAction.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACtD,OAAO,EAAE,gBAAgB,EAAE,MAAM,8BAA8B,CAAC;AAGhE,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,EAAE,gBAAgB,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAEjE,OAAO,EAAE,sBAAsB,EAAE,MAAM,2BAA2B,CAAC;AACnE,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAC;AAG5D,OAAO,EAAE,IAAI,IAAI,SAAS,EAAE,MAAM,YAAY,CAAC;AAO/C,OAAO,EAAE,sBAAsB,EAAE,MAAM,iCAAiC,CAAC;AACzE,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAKnD,qBAAa,aAAa,CACtB,YAAY,SAAS,YAAY,CACnC,YAAW,gBAAgB,CAAC,YAAY,CAAC;IACvC,OAAO,CAAC,QAAQ,CAAC,oBAAoB,CAA8C;IACnF,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OAAO,EAAE,MAAM,KAAK,MAAM,CAAC;IACpD,QAAQ,CAAC,gBAAgB,EAAE,MAAM,SAAS,CAAC;IAE3C,OAAO,CAAC,kBAAkB,CAAsB;gBAG5C,OAAO,EAAE,cAAc,GAAG,cAAc,EAAE,EAC1C,OAAO,EAAE,cAAc,CAAC,YAAY,CAAC,EACrC,IAAI,EAAE,MAAM,EACZ,SAAS,EAAE,sBAAsB,EACjC,gCAAgC,EAAE,MAAM,EACxC,SAAS,EAAE,gBAAgB,CAAC,YAAY,CAAC,EACzC,gBAAgB,EAAE,MAAM,YAAY,EACpC,aAAa,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,MAAM;IAyBxC,IAAI,CACN,GAAG,EAAE,sBAAsB,CAAC,YAAY,CAAC,GAC1C,OAAO,CAAC,WAAW,EAAE,CAAC;IA2FzB,OAAO,CAAC,uBAAuB;IA6D/B,OAAO,CAAC,YAAY;CAwCvB"}
@@ -5,11 +5,11 @@ import { CommandTriggerCheckResult } from '../../dtos/commandTriggerCheckResult'
5
5
  import { Noop } from '../../helpers/noop';
6
6
  import { MessageType } from '../../types/messageTypes';
7
7
  import { Sema as Semaphore } from 'async-sema';
8
- import { getOrSetIfNotExists } from '../../helpers/mapUtils';
8
+ import { getOrCreateIfNotExists } from '../../helpers/mapUtils';
9
9
  import { TextMessage } from '../../dtos/responses/textMessage';
10
10
  import { ReplyInfo } from '../../dtos/replyInfo';
11
11
  import { BotEventType } from '../../types/events';
12
- const REGEX_MATCH_LIMIT = 100;
12
+ import { REGEX_MATCH_LIMIT } from '../../helpers/constants';
13
13
  export class CommandAction {
14
14
  cooldownInfoProvider;
15
15
  isActiveProvider;
@@ -25,6 +25,7 @@ export class CommandAction {
25
25
  condition;
26
26
  stateConstructor;
27
27
  readmeFactory;
28
+ semaphoreFactory;
28
29
  lastCustomCooldown;
29
30
  constructor(trigger, handler, name, providers, maxAllowedSimultaniousExecutions, condition, stateConstructor, readmeFactory) {
30
31
  this.triggers = toArray(trigger);
@@ -40,12 +41,13 @@ export class CommandAction {
40
41
  this.readmeFactory = readmeFactory;
41
42
  this.maxAllowedSimultaniousExecutions =
42
43
  maxAllowedSimultaniousExecutions;
44
+ this.semaphoreFactory = () => new Semaphore(this.maxAllowedSimultaniousExecutions);
43
45
  this.key = `command:${this.name.replace('.', '-')}`;
44
46
  }
45
47
  async exec(ctx) {
46
48
  let lock;
47
49
  if (this.maxAllowedSimultaniousExecutions != 0) {
48
- lock = getOrSetIfNotExists(this.ratelimitSemaphores, ctx.chatInfo.id, new Semaphore(this.maxAllowedSimultaniousExecutions));
50
+ lock = getOrCreateIfNotExists(this.ratelimitSemaphores, ctx.chatInfo.id, this.semaphoreFactory);
49
51
  await lock.acquire();
50
52
  }
51
53
  try {
@@ -1 +1 @@
1
- {"version":3,"file":"inlineQueryAction.d.ts","sourceRoot":"","sources":["../../../src/entities/actions/inlineQueryAction.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AACxD,OAAO,EAAE,0BAA0B,EAAE,MAAM,+BAA+B,CAAC;AAC3E,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAC1D,OAAO,EAAE,4BAA4B,EAAE,MAAM,8BAA8B,CAAC;AAI5E,qBAAa,iBAAkB,YAAW,OAAO;IAC7C,QAAQ,CAAC,GAAG,EAAE,SAAS,CAAC;IACxB,QAAQ,CAAC,gBAAgB,EAAE,4BAA4B,CAAC,OAAO,CAAC,CAAC;IACjE,QAAQ,CAAC,OAAO,EAAE,kBAAkB,CAAC;IACrC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;gBAGrB,OAAO,EAAE,kBAAkB,EAC3B,IAAI,EAAE,MAAM,EACZ,cAAc,EAAE,4BAA4B,CAAC,OAAO,CAAC,EACrD,OAAO,EAAE,MAAM;IAUb,IAAI,CAAC,GAAG,EAAE,0BAA0B;CAqD7C"}
1
+ {"version":3,"file":"inlineQueryAction.d.ts","sourceRoot":"","sources":["../../../src/entities/actions/inlineQueryAction.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AACxD,OAAO,EAAE,0BAA0B,EAAE,MAAM,+BAA+B,CAAC;AAC3E,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAC1D,OAAO,EAAE,4BAA4B,EAAE,MAAM,8BAA8B,CAAC;AAK5E,qBAAa,iBAAkB,YAAW,OAAO;IAC7C,QAAQ,CAAC,GAAG,EAAE,SAAS,CAAC;IACxB,QAAQ,CAAC,gBAAgB,EAAE,4BAA4B,CAAC,OAAO,CAAC,CAAC;IACjE,QAAQ,CAAC,OAAO,EAAE,kBAAkB,CAAC;IACrC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;gBAGrB,OAAO,EAAE,kBAAkB,EAC3B,IAAI,EAAE,MAAM,EACZ,cAAc,EAAE,4BAA4B,CAAC,OAAO,CAAC,EACrD,OAAO,EAAE,MAAM;IAUb,IAAI,CAAC,GAAG,EAAE,0BAA0B;CAqD7C"}
@@ -1,6 +1,7 @@
1
1
  import { Noop } from '../../helpers/noop';
2
2
  import { BotEventType } from '../../types/events';
3
3
  import { InlineQueryResponse } from '../../dtos/responses/inlineQueryResponse';
4
+ import { REGEX_MATCH_LIMIT } from '../../helpers/constants';
4
5
  export class InlineQueryAction {
5
6
  key;
6
7
  isActiveProvider;
@@ -21,7 +22,7 @@ export class InlineQueryAction {
21
22
  this.pattern.lastIndex = 0;
22
23
  const execResult = this.pattern.exec(ctx.queryText);
23
24
  if (execResult != null) {
24
- let regexMatchLimit = 100;
25
+ let regexMatchLimit = REGEX_MATCH_LIMIT;
25
26
  matchResults.push(execResult);
26
27
  if (this.pattern.global) {
27
28
  while (regexMatchLimit > 0) {
@@ -1 +1 @@
1
- {"version":3,"file":"replyCaptureAction.d.ts","sourceRoot":"","sources":["../../../src/entities/actions/replyCaptureAction.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAC;AAC5D,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AACxD,OAAO,EAAE,oBAAoB,EAAE,MAAM,yBAAyB,CAAC;AAG/D,qBAAa,kBAAkB,CAC3B,kBAAkB,SAAS,YAAY,CACzC,YAAW,OAAO;IAChB,QAAQ,CAAC,eAAe,EAAE,MAAM,CAAC;IACjC,QAAQ,CAAC,GAAG,EAAE,SAAS,CAAC;IACxB,QAAQ,CAAC,OAAO,EAAE,CACd,YAAY,EAAE,oBAAoB,CAAC,kBAAkB,CAAC,KACrD,OAAO,CAAC,IAAI,CAAC,CAAC;IACnB,QAAQ,CAAC,QAAQ,EAAE,cAAc,EAAE,CAAC;IACpC,QAAQ,CAAC,eAAe,EAAE,eAAe,CAAC;gBAGtC,eAAe,EAAE,MAAM,EACvB,YAAY,EAAE,OAAO,EACrB,OAAO,EAAE,CACL,YAAY,EAAE,oBAAoB,CAAC,kBAAkB,CAAC,KACrD,OAAO,CAAC,IAAI,CAAC,EAClB,QAAQ,EAAE,cAAc,EAAE,EAC1B,eAAe,EAAE,eAAe;IAY9B,IAAI,CAAC,GAAG,EAAE,oBAAoB,CAAC,kBAAkB,CAAC;IA2BxD,OAAO,CAAC,uBAAuB;CA4ClC"}
1
+ {"version":3,"file":"replyCaptureAction.d.ts","sourceRoot":"","sources":["../../../src/entities/actions/replyCaptureAction.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAC;AAC5D,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AACxD,OAAO,EAAE,oBAAoB,EAAE,MAAM,yBAAyB,CAAC;AAI/D,qBAAa,kBAAkB,CAC3B,kBAAkB,SAAS,YAAY,CACzC,YAAW,OAAO;IAChB,QAAQ,CAAC,eAAe,EAAE,MAAM,CAAC;IACjC,QAAQ,CAAC,GAAG,EAAE,SAAS,CAAC;IACxB,QAAQ,CAAC,OAAO,EAAE,CACd,YAAY,EAAE,oBAAoB,CAAC,kBAAkB,CAAC,KACrD,OAAO,CAAC,IAAI,CAAC,CAAC;IACnB,QAAQ,CAAC,QAAQ,EAAE,cAAc,EAAE,CAAC;IACpC,QAAQ,CAAC,eAAe,EAAE,eAAe,CAAC;gBAGtC,eAAe,EAAE,MAAM,EACvB,YAAY,EAAE,OAAO,EACrB,OAAO,EAAE,CACL,YAAY,EAAE,oBAAoB,CAAC,kBAAkB,CAAC,KACrD,OAAO,CAAC,IAAI,CAAC,EAClB,QAAQ,EAAE,cAAc,EAAE,EAC1B,eAAe,EAAE,eAAe;IAY9B,IAAI,CAAC,GAAG,EAAE,oBAAoB,CAAC,kBAAkB,CAAC;IA2BxD,OAAO,CAAC,uBAAuB;CA4ClC"}
@@ -1,6 +1,7 @@
1
1
  import { CommandTriggerCheckResult } from '../../dtos/commandTriggerCheckResult';
2
2
  import { Noop } from '../../helpers/noop';
3
3
  import { BotEventType } from '../../types/events';
4
+ import { REGEX_MATCH_LIMIT } from '../../helpers/constants';
4
5
  export class ReplyCaptureAction {
5
6
  parentMessageId;
6
7
  key;
@@ -49,7 +50,7 @@ export class ReplyCaptureAction {
49
50
  trigger.lastIndex = 0;
50
51
  const execResult = trigger.exec(ctx.messageInfo.text);
51
52
  if (execResult != null) {
52
- let regexMatchLimit = 100;
53
+ let regexMatchLimit = REGEX_MATCH_LIMIT;
53
54
  matchResults.push(execResult);
54
55
  if (trigger.global) {
55
56
  while (regexMatchLimit > 0) {
@@ -7,6 +7,7 @@ import { ChatContextInternal } from '../context/chatContext';
7
7
  import { ScheduledActionProviders } from '../../dtos/propertyProviderSets';
8
8
  export declare class ScheduledAction<TActionState extends IActionState> implements IActionWithState<TActionState> {
9
9
  static readonly locks: Map<string, Semaphore>;
10
+ static readonly semaphoreFactory: () => Semaphore;
10
11
  readonly name: string;
11
12
  readonly key: ActionKey;
12
13
  private readonly timeinHoursProvider;
@@ -1 +1 @@
1
- {"version":3,"file":"scheduledAction.d.ts","sourceRoot":"","sources":["../../../src/entities/actions/scheduledAction.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,IAAI,SAAS,EAAE,MAAM,YAAY,CAAC;AAC/C,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAGxD,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,EAAE,gBAAgB,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AACjE,OAAO,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAC3D,OAAO,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAC;AAI7D,OAAO,EAAE,wBAAwB,EAAE,MAAM,iCAAiC,CAAC;AAG3E,qBAAa,eAAe,CACxB,YAAY,SAAS,YAAY,CACnC,YAAW,gBAAgB,CAAC,YAAY,CAAC;IACvC,MAAM,CAAC,QAAQ,CAAC,KAAK,yBAAgC;IAErD,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,GAAG,EAAE,SAAS,CAAC;IAExB,OAAO,CAAC,QAAQ,CAAC,mBAAmB,CAA8C;IAClF,OAAO,CAAC,QAAQ,CAAC,cAAc,CAA2C;IAC1E,OAAO,CAAC,QAAQ,CAAC,sBAAsB,CAErC;IAEF,QAAQ,CAAC,WAAW,uBAA8B;IAClD,QAAQ,CAAC,gBAAgB,EAAE,MAAM,YAAY,CAAC;IAC9C,QAAQ,CAAC,oBAAoB,EAAE,GAAG,CAAC,MAAM,EAAE,kBAAkB,CAAC,CAAC;IAC/D,QAAQ,CAAC,OAAO,EAAE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AEF,QAAQ,CAAC,WAAW,uBAA8B;IAClD,QAAQ,CAAC,gBAAgB,EAAE,MAAM,YAAY,CAAC;IAC9C,QAAQ,CAAC,oBAAoB,EAAE,GAAG,CAAC,MAAM,EAAE,kBAAkB,CAAC,CAAC;IAC/D,QAAQ,CAAC,OAAO,EAAE,gBAAgB,CAAC,YAAY,CAAC,CAAC;gBAG7C,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,gBAAgB,CAAC,YAAY,CAAC,EACvC,SAAS,EAAE,wBAAwB,EACnC,oBAAoB,EAAE,GAAG,CAAC,MAAM,EAAE,kBAAkB,CAAC,EACrD,gBAAgB,EAAE,MAAM,YAAY;IAclC,IAAI,CAAC,GAAG,EAAE,mBAAmB,CAAC,YAAY,CAAC;YAmDnC,cAAc;IA6D5B,OAAO,CAAC,uBAAuB;CAgBlC"}
@@ -2,10 +2,11 @@ import moment from 'moment';
2
2
  import { Sema as Semaphore } from 'async-sema';
3
3
  import { hoursToMilliseconds } from '../../helpers/timeConvertions';
4
4
  import { Noop } from '../../helpers/noop';
5
- import { getOrSetIfNotExists, getOrThrow } from '../../helpers/mapUtils';
5
+ import { getOrCreateIfNotExists, getOrThrow } from '../../helpers/mapUtils';
6
6
  import { BotEventType } from '../../types/events';
7
7
  export class ScheduledAction {
8
8
  static locks = new Map();
9
+ static semaphoreFactory = () => new Semaphore(1);
9
10
  name;
10
11
  key;
11
12
  timeinHoursProvider;
@@ -53,7 +54,7 @@ export class ScheduledAction {
53
54
  async getCachedValue(key, ctx) {
54
55
  const cachedItemFactory = getOrThrow(this.cachedStateFactories, key, `No shared cache was set up for the key [${key}] in action '${this.name}'`);
55
56
  const semaphoreKey = `${this.key}_cached:${key}`;
56
- const semaphore = getOrSetIfNotExists(ScheduledAction.locks, semaphoreKey, new Semaphore(1));
57
+ const semaphore = getOrCreateIfNotExists(ScheduledAction.locks, semaphoreKey, ScheduledAction.semaphoreFactory);
57
58
  await semaphore.acquire();
58
59
  try {
59
60
  if (this.cachedState.has(key)) {
@@ -0,0 +1,8 @@
1
+ import { Milliseconds } from '../types/timeValues';
2
+ export declare const REGEX_MATCH_LIMIT = 100;
3
+ export declare const MESSAGE_HISTORY_LENGTH_LIMIT = 100;
4
+ export declare const DEFAULT_SCHEDULED_ACTION_PERIOD_SECONDS: import("..").Seconds;
5
+ export declare const DEFAULT_STORAGE_DIRECTORY = "storage";
6
+ export declare const TELEGRAM_RATELIMIT_DELAY: Milliseconds;
7
+ export declare const TELEGRAM_ERROR_QUOTE_INVALID = "QUOTE_TEXT_INVALID";
8
+ //# sourceMappingURL=constants.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/helpers/constants.ts"],"names":[],"mappings":"AAAA,OAAO,EAAS,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAG1D,eAAO,MAAM,iBAAiB,MAAM,CAAC;AACrC,eAAO,MAAM,4BAA4B,MAAM,CAAC;AAChD,eAAO,MAAM,uCAAuC,sBAEnD,CAAC;AACF,eAAO,MAAM,yBAAyB,YAAY,CAAC;AACnD,eAAO,MAAM,wBAAwB,EAAS,YAAY,CAAC;AAC3D,eAAO,MAAM,4BAA4B,uBAAuB,CAAC"}
@@ -0,0 +1,7 @@
1
+ import { hoursToSeconds } from './timeConvertions';
2
+ export const REGEX_MATCH_LIMIT = 100;
3
+ export const MESSAGE_HISTORY_LENGTH_LIMIT = 100;
4
+ export const DEFAULT_SCHEDULED_ACTION_PERIOD_SECONDS = hoursToSeconds(1);
5
+ export const DEFAULT_STORAGE_DIRECTORY = 'storage';
6
+ export const TELEGRAM_RATELIMIT_DELAY = 35;
7
+ export const TELEGRAM_ERROR_QUOTE_INVALID = 'QUOTE_TEXT_INVALID';
@@ -4,6 +4,7 @@ type KeyedReadonlyCollection<K, V> = {
4
4
  type KeyedWriteableCollection<K, V> = KeyedReadonlyCollection<K, V> & {
5
5
  set: (key: K, value: V) => KeyedWriteableCollection<K, V>;
6
6
  };
7
+ export declare function getOrCreateIfNotExists<K, V>(map: KeyedWriteableCollection<K, V>, key: K, fallbackFactory: () => V): V;
7
8
  export declare function getOrSetIfNotExists<K, V>(map: KeyedWriteableCollection<K, V>, key: K, fallback: V): V;
8
9
  export declare function getOrThrow<K, V>(map: KeyedReadonlyCollection<K, V>, key: K, error?: string): NonNullable<V>;
9
10
  export {};
@@ -1 +1 @@
1
- {"version":3,"file":"mapUtils.d.ts","sourceRoot":"","sources":["../../src/helpers/mapUtils.ts"],"names":[],"mappings":"AAAA,KAAK,uBAAuB,CAAC,CAAC,EAAE,CAAC,IAAI;IAAE,GAAG,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,GAAG,SAAS,CAAA;CAAE,CAAC;AACxE,KAAK,wBAAwB,CAAC,CAAC,EAAE,CAAC,IAAI,uBAAuB,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG;IAClE,GAAG,EAAE,CAAC,GAAG,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,KAAK,wBAAwB,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;CAC7D,CAAC;AAEF,wBAAgB,mBAAmB,CAAC,CAAC,EAAE,CAAC,EACpC,GAAG,EAAE,wBAAwB,CAAC,CAAC,EAAE,CAAC,CAAC,EACnC,GAAG,EAAE,CAAC,EACN,QAAQ,EAAE,CAAC,KAQd;AAED,wBAAgB,UAAU,CAAC,CAAC,EAAE,CAAC,EAC3B,GAAG,EAAE,uBAAuB,CAAC,CAAC,EAAE,CAAC,CAAC,EAClC,GAAG,EAAE,CAAC,EACN,KAAK,GAAE,MAAsC,kBAMhD"}
1
+ {"version":3,"file":"mapUtils.d.ts","sourceRoot":"","sources":["../../src/helpers/mapUtils.ts"],"names":[],"mappings":"AAAA,KAAK,uBAAuB,CAAC,CAAC,EAAE,CAAC,IAAI;IAAE,GAAG,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,GAAG,SAAS,CAAA;CAAE,CAAC;AACxE,KAAK,wBAAwB,CAAC,CAAC,EAAE,CAAC,IAAI,uBAAuB,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG;IAClE,GAAG,EAAE,CAAC,GAAG,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,KAAK,wBAAwB,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;CAC7D,CAAC;AAEF,wBAAgB,sBAAsB,CAAC,CAAC,EAAE,CAAC,EACvC,GAAG,EAAE,wBAAwB,CAAC,CAAC,EAAE,CAAC,CAAC,EACnC,GAAG,EAAE,CAAC,EACN,eAAe,EAAE,MAAM,CAAC,KAS3B;AAED,wBAAgB,mBAAmB,CAAC,CAAC,EAAE,CAAC,EACpC,GAAG,EAAE,wBAAwB,CAAC,CAAC,EAAE,CAAC,CAAC,EACnC,GAAG,EAAE,CAAC,EACN,QAAQ,EAAE,CAAC,KAQd;AAED,wBAAgB,UAAU,CAAC,CAAC,EAAE,CAAC,EAC3B,GAAG,EAAE,uBAAuB,CAAC,CAAC,EAAE,CAAC,CAAC,EAClC,GAAG,EAAE,CAAC,EACN,KAAK,GAAE,MAAsC,kBAMhD"}
@@ -1,3 +1,11 @@
1
+ export function getOrCreateIfNotExists(map, key, fallbackFactory) {
2
+ const existingValue = map.get(key);
3
+ if (existingValue)
4
+ return existingValue;
5
+ const fallback = fallbackFactory();
6
+ map.set(key, fallback);
7
+ return fallback;
8
+ }
1
9
  export function getOrSetIfNotExists(map, key, fallback) {
2
10
  const existingValue = map.get(key);
3
11
  if (existingValue)
@@ -1 +1 @@
1
- {"version":3,"file":"actionProcessingService.d.ts","sourceRoot":"","sources":["../../src/services/actionProcessingService.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAuB,MAAM,qBAAqB,CAAC;AACnE,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAChD,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAElD,OAAO,EAAE,iBAAiB,EAAE,MAAM,uCAAuC,CAAC;AAC1E,O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;AAC1E,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACpD,OAAO,EAAE,aAAa,EAAE,MAAM,mCAAmC,CAAC;AAClE,OAAO,EAAE,eAAe,EAAE,MAAM,qCAAqC,CAAC;AAOtE,OAAO,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAGpD,qBAAa,uBAAuB;IAChC,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAoB;IACjD,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAiB;IACzC,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAAyB;IAC1D,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAA2B;IAC9D,OAAO,CAAC,QAAQ,CAAC,oBAAoB,CAA6B;IAElE,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;IAEjC,OAAO,CAAC,WAAW,CAAe;gBAG9B,OAAO,EAAE,MAAM,EACf,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAC7B,OAAO,EAAE,cAAc,EACvB,SAAS,EAAE,UAAU,EACrB,YAAY,EAAE,iBAAiB;IA4B7B,UAAU,CACZ,KAAK,EAAE,MAAM,EACb,OAAO,EAAE;QACL,QAAQ,EAAE,aAAa,CAAC,YAAY,CAAC,EAAE,CAAC;QACxC,SAAS,EAAE,eAAe,CAAC,YAAY,CAAC,EAAE,CAAC;QAC3C,aAAa,EAAE,iBAAiB,EAAE,CAAC;KACtC,EACD,eAAe,CAAC,EAAE,OAAO;IAoD7B,IAAI;CAGP"}
@@ -1,10 +1,10 @@
1
- import { hoursToSeconds } from '../helpers/timeConvertions';
2
1
  import { TelegramApiService } from './telegramApi';
3
2
  import { buildHelpCommand } from '../builtin/helpAction';
4
3
  import { CommandActionProcessor } from './actionProcessors/commandActionProcessor';
5
4
  import { InlineQueryActionProcessor } from './actionProcessors/inlineQueryActionProcessor';
6
5
  import { ScheduledActionProcessor } from './actionProcessors/scheduledActionProcessor';
7
6
  import { Telegraf } from 'telegraf';
7
+ import { DEFAULT_SCHEDULED_ACTION_PERIOD_SECONDS } from '../helpers/constants';
8
8
  export class ActionProcessingService {
9
9
  eventEmitter;
10
10
  storage;
@@ -36,8 +36,8 @@ export class ActionProcessingService {
36
36
  ]
37
37
  : [];
38
38
  this.commandProcessor.initialize(api, this.telegramBot, commandActions, botInfo);
39
- this.inlineQueryProcessor.initialize(api, this.telegramBot, actions.inlineQueries, 300);
40
- this.scheduledProcessor.initialize(api, actions.scheduled, scheduledPeriod ?? hoursToSeconds(1));
39
+ this.inlineQueryProcessor.initialize(api, this.telegramBot, actions.inlineQueries);
40
+ this.scheduledProcessor.initialize(api, actions.scheduled, scheduledPeriod ?? DEFAULT_SCHEDULED_ACTION_PERIOD_SECONDS);
41
41
  void this.telegramBot.launch();
42
42
  }
43
43
  stop() {
@@ -1 +1 @@
1
- {"version":3,"file":"commandActionProcessor.d.ts","sourceRoot":"","sources":["../../../src/services/actionProcessors/commandActionProcessor.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,sCAAsC,CAAC;AAIrE,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AACpD,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAM/C,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAGtD,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAC;AAEnE,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAI5C,qBAAa,sBAAuB,SAAQ,mBAAmB;IAC3D,OAAO,CAAC,QAAQ,CAAC,aAAa,CAA0C;IACxE,OAAO,CAAC,QAAQ,CAAC,WAAW,CAA2C;IACvE,OAAO,CAAC,OAAO,CAAW;IAE1B,OAAO,CAAC,QAAQ,C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Q,EAClB,OAAO,EAAE,OAAO;YA2CN,sBAAsB;CA2FvC"}
@@ -8,7 +8,7 @@ import { BaseActionProcessor } from './baseProcessor';
8
8
  import { getOrSetIfNotExists } from '../../helpers/mapUtils';
9
9
  import { ChatHistoryMessage } from '../../dtos/chatHistoryMessage';
10
10
  import { BotEventType } from '../../types/events';
11
- const MESSAGE_HISTORY_LENGTH_LIMIT = 100;
11
+ import { MESSAGE_HISTORY_LENGTH_LIMIT } from '../../helpers/constants';
12
12
  export class CommandActionProcessor extends BaseActionProcessor {
13
13
  replyCaptures = [];
14
14
  chatHistory = new Map();
@@ -40,7 +40,7 @@ export class CommandActionProcessor extends BaseActionProcessor {
40
40
  message: internalMessage,
41
41
  traceId: internalMessage.traceId
42
42
  });
43
- this.startMessageProcessing(internalMessage);
43
+ void this.startMessageProcessing(internalMessage);
44
44
  });
45
45
  }
46
46
  }
@@ -65,7 +65,7 @@ export class CommandActionProcessor extends BaseActionProcessor {
65
65
  }
66
66
  }, { once: true });
67
67
  }
68
- startMessageProcessing(msg) {
68
+ async startMessageProcessing(msg) {
69
69
  this.eventEmitter.emit(BotEventType.messageProcessingStarted, {
70
70
  botInfo: this.botInfo,
71
71
  message: msg,
@@ -81,42 +81,40 @@ export class CommandActionProcessor extends BaseActionProcessor {
81
81
  commandsToCheck.add(command);
82
82
  }
83
83
  }
84
- const promises = [...commandsToCheck].map((command) => {
84
+ const actionPromises = [...commandsToCheck].map(async (command) => {
85
85
  const ctx = new MessageContextInternal(this.storage, this.scheduler, this.eventEmitter, command, msg, this.botName, this.botInfo);
86
86
  const { proxy, revoke } = Proxy.revocable(ctx, {});
87
- const executePromise = this.executeAction(command, proxy);
88
- return executePromise.finally(() => {
87
+ try {
88
+ await this.executeAction(command, proxy);
89
+ }
90
+ finally {
89
91
  revoke();
90
92
  this.api.flushResponses();
91
- });
93
+ }
92
94
  });
93
95
  if (this.replyCaptures.length != 0) {
94
- const replyPromises = this.replyCaptures.map((capture) => {
96
+ const replyPromises = this.replyCaptures.map(async (capture) => {
95
97
  const replyCtx = new ReplyContextInternal(this.storage, this.scheduler, this.eventEmitter, capture, msg, this.botName, this.botInfo);
96
98
  const { proxy, revoke } = Proxy.revocable(replyCtx, {});
97
- const executePromise = this.executeAction(capture, proxy);
98
- return executePromise.finally(() => {
99
+ try {
100
+ await this.executeAction(capture, proxy);
101
+ }
102
+ finally {
99
103
  revoke();
100
104
  this.api.flushResponses();
101
- });
105
+ }
102
106
  });
103
- promises.push(...replyPromises);
107
+ actionPromises.push(...replyPromises);
108
+ }
109
+ try {
110
+ await Promise.allSettled(actionPromises);
104
111
  }
105
- void Promise.allSettled(promises)
106
- .then(() => {
112
+ finally {
107
113
  this.eventEmitter.emit(BotEventType.messageProcessingFinished, {
108
114
  botInfo: this.botInfo,
109
115
  message: msg,
110
116
  traceId: msg.traceId
111
117
  });
112
- })
113
- .catch((reason) => {
114
- this.eventEmitter.emit(BotEventType.error, {
115
- error: reason instanceof Error
116
- ? reason
117
- : new Error('Unknown error'),
118
- traceId: msg.traceId
119
- });
120
- });
118
+ }
121
119
  }
122
120
  }
@@ -1,12 +1,11 @@
1
1
  import { InlineQueryAction } from '../../entities/actions/inlineQueryAction';
2
2
  import { TelegramBot } from '../../types/externalAliases';
3
- import { Milliseconds } from '../../types/timeValues';
4
3
  import { TelegramApiService } from '../telegramApi';
5
4
  import { BaseActionProcessor } from './baseProcessor';
6
5
  export declare class InlineQueryActionProcessor extends BaseActionProcessor {
7
6
  private inlineQueries;
8
7
  /** Fake chat info, since inline queries are chat-less */
9
8
  private readonly fakeChatInfo;
10
- initialize(api: TelegramApiService, telegram: TelegramBot, inlineQueries: InlineQueryAction[], period: Milliseconds): void;
9
+ initialize(api: TelegramApiService, telegram: TelegramBot, inlineQueries: InlineQueryAction[]): void;
11
10
  }
12
11
  //# sourceMappingURL=inlineQueryActionProcessor.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"inlineQueryActionProcessor.d.ts","sourceRoot":"","sources":["../../../src/services/actionProcessors/inlineQueryActionProcessor.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,iBAAiB,EAAE,MAAM,0CAA0C,CAAC;AAI7E,OAAO,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAC;AAC1D,OAAO,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AACtD,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AACpD,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAEtD,qBAAa,0BAA2B,SAAQ,mBAAmB;IAC/D,OAAO,CAAC,aAAa,CAAuB;IAC5C,yDAAyD;IACzD,OAAO,CAAC,QAAQ,CAAC,YAAY,CAI3B;IAEF,UAAU,CACN,GAAG,EAAE,kBAAkB,EACvB,QAAQ,EAAE,WAAW,EACrB,aAAa,EAAE,iBAAiB,EAAE,EAClC,MAAM,EAAE,YAAY;CAmK3B"}
1
+ {"version":3,"file":"inlineQueryActionProcessor.d.ts","sourceRoot":"","sources":["../../../src/services/actionProcessors/inlineQueryActionProcessor.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,iBAAiB,EAAE,MAAM,0CAA0C,CAAC;AAI7E,OAAO,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAC;AAC1D,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AACpD,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAEtD,qBAAa,0BAA2B,SAAQ,mBAAmB;IAC/D,OAAO,CAAC,aAAa,CAAuB;IAC5C,yDAAyD;IACzD,OAAO,CAAC,QAAQ,CAAC,YAAY,CAI3B;IAEF,UAAU,CACN,GAAG,EAAE,kBAAkB,EACvB,QAAQ,EAAE,WAAW,EACrB,aAAa,EAAE,iBAAiB,EAAE;CAyGzC"}
@@ -8,13 +8,12 @@ export class InlineQueryActionProcessor extends BaseActionProcessor {
8
8
  inlineQueries;
9
9
  /** Fake chat info, since inline queries are chat-less */
10
10
  fakeChatInfo = new ChatInfo(Math.random(), 'Inline Query', []);
11
- initialize(api, telegram, inlineQueries, period) {
11
+ initialize(api, telegram, inlineQueries) {
12
12
  this.initializeDependencies(api);
13
13
  this.inlineQueries = inlineQueries;
14
- let pendingInlineQueries = [];
15
14
  const queriesInProcessing = new Map();
16
15
  if (this.inlineQueries.length > 0) {
17
- telegram.on('inline_query', ({ inlineQuery }) => {
16
+ telegram.on('inline_query', async ({ inlineQuery }) => {
18
17
  const query = new IncomingInlineQuery(inlineQuery.id, inlineQuery.query, inlineQuery.from.id, createTrace('InlineQuery', this.botName, inlineQuery.id));
19
18
  this.eventEmitter.emit(BotEventType.inlineQueryRecieved, {
20
19
  query,
@@ -30,69 +29,46 @@ export class InlineQueryActionProcessor extends BaseActionProcessor {
30
29
  queryBeingProcessed.abortController.abort();
31
30
  queriesInProcessing.delete(query.userId);
32
31
  }
33
- pendingInlineQueries = pendingInlineQueries.filter((q) => q.userId != query.userId);
34
- pendingInlineQueries.push(query);
35
- });
36
- this.scheduler.createTask('InlineQueryProcessing', () => {
37
- const queriesToProcess = [...pendingInlineQueries];
38
- pendingInlineQueries = [];
39
- const promises = [];
40
- for (const inlineQuery of queriesToProcess) {
41
- this.eventEmitter.emit(BotEventType.inlineProcessingStarted, {
42
- botName: this.botName,
43
- traceId: inlineQuery.traceId
44
- });
45
- queriesInProcessing.set(inlineQuery.userId, inlineQuery);
46
- const actionPromises = this.inlineQueries.map((inlineQueryAction) => {
47
- const ctx = new InlineQueryContextInternal(this.storage, this.scheduler, this.eventEmitter, inlineQueryAction, inlineQuery, this.fakeChatInfo, this.botName);
48
- const { proxy, revoke } = Proxy.revocable(ctx, {});
49
- const executePromise = this.executeAction(inlineQueryAction, proxy, (error, _) => {
32
+ this.eventEmitter.emit(BotEventType.inlineProcessingStarted, {
33
+ botName: this.botName,
34
+ traceId: query.traceId
35
+ });
36
+ queriesInProcessing.set(query.userId, query);
37
+ const actionPromises = this.inlineQueries.map(async (inlineQueryAction) => {
38
+ const ctx = new InlineQueryContextInternal(this.storage, this.scheduler, this.eventEmitter, inlineQueryAction, query, this.fakeChatInfo, this.botName);
39
+ const { proxy, revoke } = Proxy.revocable(ctx, {});
40
+ try {
41
+ await this.executeAction(inlineQueryAction, proxy, (error, _) => {
50
42
  if (error.name == 'AbortError') {
51
43
  this.eventEmitter.emit(BotEventType.inlineProcessingAborted, {
52
- abortedQuery: inlineQuery,
53
- traceId: inlineQuery.traceId
44
+ abortedQuery: query,
45
+ traceId: query.traceId
54
46
  });
55
47
  }
56
48
  else {
57
49
  this.eventEmitter.emit(BotEventType.error, {
58
50
  error,
59
- traceId: inlineQuery.traceId
51
+ traceId: query.traceId
60
52
  });
61
53
  }
62
54
  });
63
- return executePromise.finally(() => {
64
- revoke();
65
- this.api.flushResponses();
66
- });
67
- });
68
- const queryPromise = Promise.allSettled(actionPromises)
69
- .then(() => {
70
- queriesInProcessing.delete(inlineQuery.userId);
71
- this.eventEmitter.emit(BotEventType.inlineProcessingFinished, {
72
- botName: this.botName,
73
- traceId: inlineQuery.traceId
74
- });
75
- })
76
- .catch((reason) => {
77
- queriesInProcessing.delete(inlineQuery.userId);
78
- this.eventEmitter.emit(BotEventType.error, {
79
- error: reason instanceof Error
80
- ? reason
81
- : new Error('Unknown error'),
82
- traceId: inlineQuery.traceId
83
- });
84
- });
85
- promises.push(queryPromise);
55
+ }
56
+ finally {
57
+ revoke();
58
+ this.api.flushResponses();
59
+ }
60
+ });
61
+ try {
62
+ await Promise.allSettled(actionPromises);
86
63
  }
87
- void Promise.allSettled(promises).catch((reason) => {
88
- this.eventEmitter.emit(BotEventType.error, {
89
- error: reason instanceof Error
90
- ? reason
91
- : new Error('Unknown error'),
92
- traceId: createTrace(this, this.botName, 'Error')
64
+ finally {
65
+ queriesInProcessing.delete(query.userId);
66
+ this.eventEmitter.emit(BotEventType.inlineProcessingFinished, {
67
+ botName: this.botName,
68
+ traceId: query.traceId
93
69
  });
94
- });
95
- }, period, false, this.botName);
70
+ }
71
+ });
96
72
  }
97
73
  }
98
74
  }
@@ -1 +1 @@
1
- {"version":3,"file":"scheduledActionProcessor.d.ts","sourceRoot":"","sources":["../../../src/services/actionProcessors/scheduledActionProcessor.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAE,MAAM,wCAAwC,CAAC;AAIzE,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AACnD,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,EAAE,OAAO,EAAgB,MAAM,wBAAwB,CAAC;AAC/D,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AACpD,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AACtD,OAAO,EAAgB,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AAErE,qBAAa,wBAAyB,SAAQ,mBAAmB;IAC7D,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAyB;IAC/C,OAAO,CAAC,QAAQ,CAAC,SAAS,CAIxB;IAEF,OAAO,CAAC,SAAS,CAAmC;gBAGhD,OAAO,EAAE,MAAM,EACf,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAC7B,OAAO,EAAE,cAAc,EACvB,SAAS,EAAE,UAAU,EACrB,YAAY,EAAE,iBAAiB;IAMnC,UAAU,CACN,GAAG,EAAE,kBAAkB,EACvB,SAAS,EAAE,eAAe,CAAC,YAAY,CAAC,EAAE,EAC1C,MAAM,EAAE,OAAO;IAkDnB,OAAO,CAAC,YAAY;CA4DvB"}
1
+ {"version":3,"file":"scheduledActionProcessor.d.ts","sourceRoot":"","sources":["../../../src/services/actionProcessors/scheduledActionProcessor.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAE,MAAM,wCAAwC,CAAC;AAIzE,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AACnD,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,EAAE,OAAO,EAAgB,MAAM,wBAAwB,CAAC;AAC/D,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AACpD,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AACtD,OAAO,EAAgB,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AAErE,qBAAa,wBAAyB,SAAQ,mBAAmB;IAC7D,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAyB;IAC/C,OAAO,CAAC,QAAQ,CAAC,SAAS,CAIxB;IAEF,OAAO,CAAC,SAAS,CAAmC;gBAGhD,OAAO,EAAE,MAAM,EACf,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAC7B,OAAO,EAAE,cAAc,EACvB,SAAS,EAAE,UAAU,EACrB,YAAY,EAAE,iBAAiB;IAMnC,UAAU,CACN,GAAG,EAAE,kBAAkB,EACvB,SAAS,EAAE,eAAe,CAAC,YAAY,CAAC,EAAE,EAC1C,MAAM,EAAE,OAAO;YA8CL,YAAY;CA8C7B"}
@@ -19,9 +19,7 @@ export class ScheduledActionProcessor extends BaseActionProcessor {
19
19
  if (this.scheduled.length > 0) {
20
20
  const now = moment();
21
21
  if (now.minute() == 0 && now.second() == 0) {
22
- this.scheduler.createTask('ScheduledProcessing', () => {
23
- this.runScheduled();
24
- }, secondsToMilliseconds(period), true, this.botName);
22
+ this.scheduler.createTask('ScheduledProcessing', () => void this.runScheduled(), secondsToMilliseconds(period), true, this.botName);
25
23
  return;
26
24
  }
27
25
  let nextExecutionTime = now.clone().startOf('hour');
@@ -30,44 +28,38 @@ export class ScheduledActionProcessor extends BaseActionProcessor {
30
28
  }
31
29
  const delay = nextExecutionTime.diff(now);
32
30
  this.scheduler.createOnetimeTask('ScheduledProcessing_OneTime', () => {
33
- this.scheduler.createTask('ScheduledProcessing', () => {
34
- this.runScheduled();
35
- }, secondsToMilliseconds(period), true, this.botName);
31
+ this.scheduler.createTask('ScheduledProcessing', () => void this.runScheduled(), secondsToMilliseconds(period), true, this.botName);
36
32
  }, delay, this.botName);
37
- this.runScheduled();
33
+ void this.runScheduled();
38
34
  }
39
35
  }
40
- runScheduled() {
36
+ async runScheduled() {
41
37
  this.eventEmitter.emit(BotEventType.scheduledProcessingStarted, {
42
38
  botName: this.botName,
43
39
  traceId: this.taskTrace
44
40
  });
45
- const promises = Object.entries(this.chats).flatMap(([chatName, chatId]) => {
41
+ const actionPromises = Object.entries(this.chats).flatMap(([chatName, chatId]) => {
46
42
  const chatInfo = new ChatInfo(chatId, chatName, []);
47
- return this.scheduled.map((scheduledAction) => {
43
+ return this.scheduled.map(async (scheduledAction) => {
48
44
  const ctx = new ChatContextInternal(this.storage, this.scheduler, this.eventEmitter, scheduledAction, chatInfo, createTrace(scheduledAction, this.botName, `${scheduledAction.key}-${chatId}`), this.botName);
49
45
  const { proxy, revoke } = Proxy.revocable(ctx, {});
50
- const executePromise = this.executeAction(scheduledAction, proxy);
51
- return executePromise.finally(() => {
46
+ try {
47
+ await this.executeAction(scheduledAction, proxy);
48
+ }
49
+ finally {
52
50
  revoke();
53
51
  this.api.flushResponses();
54
- });
52
+ }
55
53
  });
56
54
  });
57
- void Promise.allSettled(promises)
58
- .then(() => {
55
+ try {
56
+ await Promise.allSettled(actionPromises);
57
+ }
58
+ finally {
59
59
  this.eventEmitter.emit(BotEventType.scheduledProcessingFinished, {
60
60
  botName: this.botName,
61
61
  traceId: this.taskTrace
62
62
  });
63
- })
64
- .catch((reason) => {
65
- this.eventEmitter.emit(BotEventType.error, {
66
- error: reason instanceof Error
67
- ? reason
68
- : new Error('Unknown error'),
69
- traceId: this.taskTrace
70
- });
71
- });
63
+ }
72
64
  }
73
65
  }
@@ -2,8 +2,9 @@ import { IStorageClient } from '../types/storage';
2
2
  import { IActionState } from '../types/actionState';
3
3
  import { IActionWithState } from '../types/action';
4
4
  export declare class JsonFileStorage implements IStorageClient {
5
- private readonly locks;
6
5
  private readonly data;
6
+ private readonly locks;
7
+ private readonly semaphoreFactory;
7
8
  constructor(botName: string, actions: IActionWithState<IActionState>[], path?: string);
8
9
  private lock;
9
10
  load<TActionState extends IActionState>(action: IActionWithState<TActionState>): Record<number, TActionState>;
@@ -1 +1 @@
1
- {"version":3,"file":"jsonFileStorage.d.ts","sourceRoot":"","sources":["../../src/services/jsonFileStorage.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACpD,OAAO,EAAE,gBAAgB,EAAa,MAAM,iBAAiB,CAAC;AAsG9D,qBAAa,eAAgB,YAAW,cAAc;IAClD,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAmC;IAEzD,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAmB;gBAGpC,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,gBAAgB,CAAC,YAAY,CAAC,EAAE,EACzC,IAAI,CAAC,EAAE,MAAM;YASH,IAAI;IAYlB,IAAI,CAAC,YAAY,SAAS,YAAY,EAClC,MAAM,EAAE,gBAAgB,CAAC,YAAY,CAAC;IAK1C,cAAc,CAAC,YAAY,SAAS,YAAY,EAC5C,MAAM,EAAE,gBAAgB,CAAC,YAAY,CAAC,EACtC,MAAM,EAAE,MAAM;IAOZ,yBAAyB,CAAC,YAAY,SAAS,YAAY,EAC7D,MAAM,EAAE,gBAAgB,CAAC,YAAY,CAAC,EACtC,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,YAAY;IAWjB,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAMtB,cAAc,CAAC,YAAY,SAAS,YAAY,EAClD,MAAM,EAAE,gBAAgB,CAAC,YAAY,CAAC,EACtC,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,CAAC,KAAK,EAAE,YAAY,KAAK,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI;CAa5D"}
1
+ {"version":3,"file":"jsonFileStorage.d.ts","sourceRoot":"","sources":["../../src/services/jsonFileStorage.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACpD,OAAO,EAAE,gBAAgB,EAAa,MAAM,iBAAiB,CAAC;AA0G9D,qBAAa,eAAgB,YAAW,cAAc;IAClD,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAmB;IAExC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAmC;IACzD,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAA0B;gBAGvD,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,gBAAgB,CAAC,YAAY,CAAC,EAAE,EACzC,IAAI,CAAC,EAAE,MAAM;YAaH,IAAI;IAgBlB,IAAI,CAAC,YAAY,SAAS,YAAY,EAClC,MAAM,EAAE,gBAAgB,CAAC,YAAY,CAAC;IAK1C,cAAc,CAAC,YAAY,SAAS,YAAY,EAC5C,MAAM,EAAE,gBAAgB,CAAC,YAAY,CAAC,EACtC,MAAM,EAAE,MAAM;IAOZ,yBAAyB,CAAC,YAAY,SAAS,YAAY,EAC7D,MAAM,EAAE,gBAAgB,CAAC,YAAY,CAAC,EACtC,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,YAAY;IAWjB,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAMtB,cAAc,CAAC,YAAY,SAAS,YAAY,EAClD,MAAM,EAAE,gBAAgB,CAAC,YAAY,CAAC,EACtC,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,CAAC,KAAK,EAAE,YAAY,KAAK,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI;CAa5D"}
@@ -2,7 +2,8 @@ import { existsSync, mkdirSync, readFileSync } from 'fs';
2
2
  import { writeFile } from 'fs/promises';
3
3
  import { dirname } from 'path';
4
4
  import { Sema as Semaphore } from 'async-sema';
5
- import { getOrSetIfNotExists } from '../helpers/mapUtils';
5
+ import { getOrCreateIfNotExists, getOrSetIfNotExists } from '../helpers/mapUtils';
6
+ import { DEFAULT_STORAGE_DIRECTORY } from '../helpers/constants';
6
7
  function buildPath(storagePath, botName, actionKey) {
7
8
  return `${storagePath}/${botName}/${actionKey.replaceAll(':', '/')}.json`;
8
9
  }
@@ -14,7 +15,7 @@ class CachedDataSource {
14
15
  constructor(botName, actions, path) {
15
16
  this.cache = new Map();
16
17
  this.botName = botName;
17
- this.storagePath = path ?? 'storage';
18
+ this.storagePath = path ?? DEFAULT_STORAGE_DIRECTORY;
18
19
  if (!existsSync(`${this.storagePath}/${this.botName}/`)) {
19
20
  mkdirSync(`${this.storagePath}/${this.botName}/`, {
20
21
  recursive: true
@@ -59,16 +60,17 @@ class CachedDataSource {
59
60
  }
60
61
  }
61
62
  export class JsonFileStorage {
62
- locks = new Map();
63
63
  data;
64
+ locks = new Map();
65
+ semaphoreFactory = () => new Semaphore(1);
64
66
  constructor(botName, actions, path) {
65
- this.data = new CachedDataSource(botName, actions, path ?? 'storage');
67
+ this.data = new CachedDataSource(botName, actions, path ?? DEFAULT_STORAGE_DIRECTORY);
66
68
  for (const action of actions) {
67
69
  this.locks.set(action.key, new Semaphore(1));
68
70
  }
69
71
  }
70
72
  async lock(key, action) {
71
- const lock = getOrSetIfNotExists(this.locks, key, new Semaphore(1));
73
+ const lock = getOrCreateIfNotExists(this.locks, key, this.semaphoreFactory);
72
74
  await lock.acquire();
73
75
  try {
74
76
  return await action();
@@ -1 +1 @@
1
- {"version":3,"file":"responseProcessingQueue.d.ts","sourceRoot":"","sources":["../../src/services/responseProcessingQueue.ts"],"names":[],"mappings":"AAGA,MAAM,MAAM,SAAS,GAAG;IACpB,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;CACjC,CAAC;AAQF,qBAAa,uBAAuB;IAChC,OAAO,CAAC,QAAQ,CAAC,WAAW,CAEzB;IACH,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAmB;IACzC,OAAO,CAAC,UAAU,CAAS;IAE3B,OAAO,CAAC,IAAI,EAAE,SAAS;IAmBjB,eAAe;CAkBxB"}
1
+ {"version":3,"file":"responseProcessingQueue.d.ts","sourceRoot":"","sources":["../../src/services/responseProcessingQueue.ts"],"names":[],"mappings":"AAGA,MAAM,MAAM,SAAS,GAAG;IACpB,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;CACjC,CAAC;AAEF,qBAAa,uBAAuB;IAChC,OAAO,CAAC,QAAQ,CAAC,WAAW,CAEzB;IACH,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAmB;IACzC,OAAO,CAAC,UAAU,CAAS;IAE3B,OAAO,CAAC,IAAI,EAAE,SAAS;IAmBjB,eAAe;CAkBxB"}
@@ -1,8 +1,5 @@
1
1
  import { RateLimit } from 'async-sema';
2
- function notEmpty(arr) {
3
- return arr.length > 0;
4
- }
5
- const TELEGRAM_RATELIMIT_DELAY = 35;
2
+ import { TELEGRAM_RATELIMIT_DELAY } from '../helpers/constants';
6
3
  export class ResponseProcessingQueue {
7
4
  rateLimiter = RateLimit(1, {
8
5
  timeUnit: TELEGRAM_RATELIMIT_DELAY
@@ -26,14 +23,17 @@ export class ResponseProcessingQueue {
26
23
  if (this.isFlushing)
27
24
  return;
28
25
  this.isFlushing = true;
29
- while (notEmpty(this.items)) {
30
- await this.rateLimiter();
31
- if (Date.now() >= this.items[0].priority) {
32
- const [item] = this.items;
33
- this.items.shift();
34
- await item.callback();
26
+ try {
27
+ while (this.items.length > 0) {
28
+ await this.rateLimiter();
29
+ if (Date.now() >= this.items[0].priority) {
30
+ const item = this.items.shift();
31
+ await item?.callback();
32
+ }
35
33
  }
36
34
  }
37
- this.isFlushing = false;
35
+ finally {
36
+ this.isFlushing = false;
37
+ }
38
38
  }
39
39
  }
@@ -5,7 +5,6 @@ import { TraceId } from '../types/trace';
5
5
  import { ChatInfo } from '../dtos/chatInfo';
6
6
  import { TelegramApiClient } from '../types/externalAliases';
7
7
  import { TypedEventEmitter } from '../types/events';
8
- export declare const TELEGRAM_ERROR_QUOTE_INVALID = "QUOTE_TEXT_INVALID";
9
8
  export declare class TelegramApiService {
10
9
  private readonly queue;
11
10
  private readonly telegram;
@@ -1 +1 @@
1
- {"version":3,"file":"telegramApi.d.ts","sourceRoot":"","sources":["../../src/services/telegramApi.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EACH,WAAW,EAGd,MAAM,mBAAmB,CAAC;AAE3B,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAGjD,OAAO,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AACzC,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,iBAAiB,EAAmB,MAAM,0BAA0B,CAAC;AAC9E,OAAO,EAAgB,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAGlE,eAAO,MAAM,4BAA4B,uBAAuB,CAAC;AAEjE,qBAAa,kBAAkB;IAC3B,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAiC;IACvD,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAoB;IAC7C,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAiB;IACzC,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAoB;IACjD,OAAO,CAAC,QAAQ,CAAC,2BAA2B,CAKlC;IAEV,OAAO,CAAC,QAAQ,CAAC,kCAAkC,CAAU;IAE7D,OAAO,CAAC,QAAQ,CAAC,SAAS,CAYxB;gBAGE,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,iBAAiB,EAC3B,OAAO,EAAE,cAAc,EACvB,YAAY,EAAE,iBAAiB,EAC/B,2BA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,OAAO,KACf,IAAI;IAcb,uBAAuB,CAAC,SAAS,EAAE,WAAW,EAAE;IAyDhD,cAAc;YAYA,WAAW;YA+BX,eAAe;YAiBf,cAAc;CA2G/B"}
@@ -1,7 +1,7 @@
1
1
  import { ResponseProcessingQueue } from './responseProcessingQueue';
2
2
  import { BotEventType } from '../types/events';
3
3
  import { createTrace } from '../helpers/traceFactory';
4
- export const TELEGRAM_ERROR_QUOTE_INVALID = 'QUOTE_TEXT_INVALID';
4
+ import { TELEGRAM_ERROR_QUOTE_INVALID } from '../helpers/constants';
5
5
  export class TelegramApiService {
6
6
  queue = new ResponseProcessingQueue();
7
7
  telegram;
@@ -101,11 +101,6 @@ export class TelegramApiService {
101
101
  }
102
102
  async processResponse(response, ignoreQuote = false) {
103
103
  const sentMessage = await this.sendApiRequest(response, ignoreQuote);
104
- this.eventEmitter.emit(BotEventType.apiRequestSent, {
105
- response,
106
- telegramMethod: this.methodMap[response.kind],
107
- traceId: response.traceId
108
- });
109
104
  if (sentMessage && 'content' in response) {
110
105
  await this.pinIfShould(response, sentMessage);
111
106
  for (const capture of response.captures) {
@@ -119,62 +114,71 @@ export class TelegramApiService {
119
114
  telegramMethod: this.methodMap[response.kind],
120
115
  traceId: response.traceId
121
116
  });
122
- switch (response.kind) {
123
- case 'text':
124
- return await this.telegram.sendMessage(response.chatInfo.id, response.content, {
125
- reply_parameters: response.replyInfo
117
+ try {
118
+ switch (response.kind) {
119
+ case 'text':
120
+ return await this.telegram.sendMessage(response.chatInfo.id, response.content, {
121
+ reply_parameters: response.replyInfo
122
+ ? {
123
+ message_id: response.replyInfo.id,
124
+ quote: ignoreQuote
125
+ ? undefined
126
+ : response.replyInfo.quote
127
+ }
128
+ : undefined,
129
+ parse_mode: 'MarkdownV2',
130
+ link_preview_options: {
131
+ is_disabled: response.disableWebPreview
132
+ },
133
+ reply_markup: response.keyboard
134
+ ? {
135
+ inline_keyboard: response.keyboard
136
+ }
137
+ : undefined
138
+ });
139
+ case 'image':
140
+ return await this.telegram.sendPhoto(response.chatInfo.id, response.content,
141
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
142
+ response.replyInfo?.id
126
143
  ? {
127
- message_id: response.replyInfo.id,
128
- quote: ignoreQuote
129
- ? undefined
130
- : response.replyInfo.quote
144
+ reply_to_message_id: response.replyInfo.id // eslint-disable-next-line @typescript-eslint/no-explicit-any
131
145
  }
132
- : undefined,
133
- parse_mode: 'MarkdownV2',
134
- link_preview_options: {
135
- is_disabled: response.disableWebPreview
136
- },
137
- reply_markup: response.keyboard
146
+ : undefined);
147
+ case 'video':
148
+ return await this.telegram.sendVideo(response.chatInfo.id, response.content,
149
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
150
+ response.replyInfo?.id
138
151
  ? {
139
- inline_keyboard: response.keyboard
152
+ reply_to_message_id: response.replyInfo.id // eslint-disable-next-line @typescript-eslint/no-explicit-any
140
153
  }
141
- : undefined
142
- });
143
- case 'image':
144
- return await this.telegram.sendPhoto(response.chatInfo.id, response.content,
145
- // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
146
- response.replyInfo?.id
147
- ? {
148
- reply_to_message_id: response.replyInfo.id // eslint-disable-next-line @typescript-eslint/no-explicit-any
149
- }
150
- : undefined);
151
- case 'video':
152
- return await this.telegram.sendVideo(response.chatInfo.id, response.content,
153
- // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
154
- response.replyInfo?.id
155
- ? {
156
- reply_to_message_id: response.replyInfo.id // eslint-disable-next-line @typescript-eslint/no-explicit-any
157
- }
158
- : undefined);
159
- case 'react':
160
- await this.telegram.setMessageReaction(response.chatInfo.id, response.messageId, [
161
- {
162
- type: 'emoji',
163
- emoji: response.emoji
164
- }
165
- ]);
166
- return null;
167
- case 'unpin':
168
- await this.telegram.unpinChatMessage(response.chatInfo.id, response.messageId);
169
- await this.storage.updateStateFor(response.action, response.chatInfo.id, (state) => {
170
- state.pinnedMessages = state.pinnedMessages.filter((x) => x != response.messageId);
171
- });
172
- return null;
173
- case 'inlineQuery':
174
- await this.telegram.answerInlineQuery(response.queryId, response.queryResults, { cache_time: 0 });
175
- return null;
176
- case 'delay':
177
- return null;
154
+ : undefined);
155
+ case 'react':
156
+ await this.telegram.setMessageReaction(response.chatInfo.id, response.messageId, [
157
+ {
158
+ type: 'emoji',
159
+ emoji: response.emoji
160
+ }
161
+ ]);
162
+ return null;
163
+ case 'unpin':
164
+ await this.telegram.unpinChatMessage(response.chatInfo.id, response.messageId);
165
+ await this.storage.updateStateFor(response.action, response.chatInfo.id, (state) => {
166
+ state.pinnedMessages = state.pinnedMessages.filter((x) => x != response.messageId);
167
+ });
168
+ return null;
169
+ case 'inlineQuery':
170
+ await this.telegram.answerInlineQuery(response.queryId, response.queryResults, { cache_time: 0 });
171
+ return null;
172
+ case 'delay':
173
+ return null;
174
+ }
175
+ }
176
+ finally {
177
+ this.eventEmitter.emit(BotEventType.apiRequestSent, {
178
+ response,
179
+ telegramMethod: this.methodMap[response.kind],
180
+ traceId: response.traceId
181
+ });
178
182
  }
179
183
  }
180
184
  }
package/package.json CHANGED
@@ -14,7 +14,7 @@
14
14
  "type": "git",
15
15
  "url": "https://github.com/AlexSolari/botFramework.git"
16
16
  },
17
- "version": "0.7.19",
17
+ "version": "0.7.21",
18
18
  "type": "module",
19
19
  "dependencies": {
20
20
  "async-sema": "^3.1.1",
@@ -28,12 +28,13 @@
28
28
  ],
29
29
  "devDependencies": {
30
30
  "@eslint/js": "^9.39.4",
31
- "@types/bun": "^1.3.11",
32
- "@types/node": "^22.19.15",
31
+ "@types/bun": "^1.3.12",
32
+ "@types/node": "^22.19.17",
33
+ "bun-types": "^1.3.12",
33
34
  "eslint": "^9.39.4",
34
35
  "jiti": "^2.6.1",
35
36
  "typescript": "^6.0.2",
36
- "typescript-eslint": "^8.58.0"
37
+ "typescript-eslint": "^8.58.2"
37
38
  },
38
39
  "scripts": {
39
40
  "build": "tsc -p tsconfig.build.json",