gramio 0.1.5 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -1
- package/dist/index.cjs +69 -10
- package/dist/index.d.cts +63 -4
- package/dist/index.d.ts +63 -4
- package/dist/index.js +69 -10
- package/package.json +5 -8
package/README.md
CHANGED
|
@@ -2,8 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
<div align="center">
|
|
4
4
|
|
|
5
|
-
[](https://core.telegram.org/bots/api)
|
|
6
6
|
[](https://www.npmjs.org/package/gramio)
|
|
7
|
+
[](https://www.npmjs.org/package/gramio)
|
|
7
8
|
[](https://jsr.io/@gramio/core)
|
|
8
9
|
[](https://jsr.io/@gramio/core)
|
|
9
10
|
|
package/dist/index.cjs
CHANGED
|
@@ -95,6 +95,9 @@ class Composer {
|
|
|
95
95
|
compose(context, next = middlewareIo.noopNext) {
|
|
96
96
|
this.composed(context, next);
|
|
97
97
|
}
|
|
98
|
+
composeWait(context, next = middlewareIo.noopNext) {
|
|
99
|
+
return this.composed(context, next);
|
|
100
|
+
}
|
|
98
101
|
}
|
|
99
102
|
|
|
100
103
|
var __create$1 = Object.create;
|
|
@@ -175,6 +178,7 @@ class Plugin {
|
|
|
175
178
|
errorsDefinitions: {},
|
|
176
179
|
decorators: {}
|
|
177
180
|
};
|
|
181
|
+
"~" = this._;
|
|
178
182
|
/** Create new Plugin. Please provide `name` */
|
|
179
183
|
constructor(name, { dependencies } = {}) {
|
|
180
184
|
this._.name = name;
|
|
@@ -302,24 +306,72 @@ class Plugin {
|
|
|
302
306
|
}
|
|
303
307
|
return this;
|
|
304
308
|
}
|
|
309
|
+
/**
|
|
310
|
+
* ! ** At the moment, it can only pick up types** */
|
|
311
|
+
extend(plugin) {
|
|
312
|
+
return this;
|
|
313
|
+
}
|
|
305
314
|
}
|
|
306
315
|
_init$1 = __decoratorStart$1();
|
|
307
316
|
Plugin = __decorateElement$1(_init$1, 0, "Plugin", _Plugin_decorators, Plugin);
|
|
308
317
|
__runInitializers$1(_init$1, 1, Plugin);
|
|
309
318
|
|
|
310
319
|
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
320
|
+
class UpdateQueue {
|
|
321
|
+
updateQueue = [];
|
|
322
|
+
pendingUpdates = /* @__PURE__ */ new Set();
|
|
323
|
+
handler;
|
|
324
|
+
onIdleResolver;
|
|
325
|
+
onIdlePromise;
|
|
326
|
+
isActive = false;
|
|
327
|
+
constructor(handler) {
|
|
328
|
+
this.handler = handler;
|
|
329
|
+
}
|
|
330
|
+
add(update) {
|
|
331
|
+
this.updateQueue.push(update);
|
|
332
|
+
this.start();
|
|
333
|
+
}
|
|
334
|
+
start() {
|
|
335
|
+
this.isActive = true;
|
|
336
|
+
while (this.updateQueue.length && this.isActive) {
|
|
337
|
+
const update = this.updateQueue.shift();
|
|
338
|
+
if (!update) continue;
|
|
339
|
+
const promise = this.handler(update);
|
|
340
|
+
this.pendingUpdates.add(promise);
|
|
341
|
+
promise.finally(async () => {
|
|
342
|
+
this.pendingUpdates.delete(promise);
|
|
343
|
+
if (this.pendingUpdates.size === 0 && this.updateQueue.length === 0 && this.onIdleResolver) {
|
|
344
|
+
this.onIdleResolver();
|
|
345
|
+
this.onIdleResolver = void 0;
|
|
346
|
+
}
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
async stop(timeout = 3e3) {
|
|
351
|
+
if (this.updateQueue.length === 0 && this.pendingUpdates.size === 0) {
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
this.onIdlePromise = new Promise((resolve) => {
|
|
355
|
+
this.onIdleResolver = resolve;
|
|
356
|
+
});
|
|
357
|
+
await Promise.race([this.onIdlePromise, sleep(timeout)]);
|
|
358
|
+
this.isActive = false;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
|
|
311
362
|
class Updates {
|
|
312
363
|
bot;
|
|
313
364
|
isStarted = false;
|
|
314
365
|
offset = 0;
|
|
315
366
|
composer;
|
|
367
|
+
queue;
|
|
316
368
|
constructor(bot, onError) {
|
|
317
369
|
this.bot = bot;
|
|
318
370
|
this.composer = new Composer(onError);
|
|
371
|
+
this.queue = new UpdateQueue(this.handleUpdate.bind(this));
|
|
319
372
|
}
|
|
320
373
|
async handleUpdate(data) {
|
|
321
374
|
const updateType = Object.keys(data).at(1);
|
|
322
|
-
this.offset = data.update_id + 1;
|
|
323
375
|
const UpdateContext = contexts.contextsMappings[updateType];
|
|
324
376
|
if (!UpdateContext) throw new Error(updateType);
|
|
325
377
|
const updatePayload = data[updateType];
|
|
@@ -345,7 +397,7 @@ class Updates {
|
|
|
345
397
|
updateId: data.update_id
|
|
346
398
|
});
|
|
347
399
|
}
|
|
348
|
-
this.composer.
|
|
400
|
+
return this.composer.composeWait(context);
|
|
349
401
|
} catch (error) {
|
|
350
402
|
throw new Error(`[UPDATES] Update type ${updateType} not supported.`);
|
|
351
403
|
}
|
|
@@ -362,10 +414,13 @@ class Updates {
|
|
|
362
414
|
try {
|
|
363
415
|
const updates = await this.bot.api.getUpdates({
|
|
364
416
|
...params,
|
|
365
|
-
offset: this.offset
|
|
417
|
+
offset: this.offset,
|
|
418
|
+
timeout: 30
|
|
366
419
|
});
|
|
420
|
+
const updateId = updates.at(-1)?.update_id;
|
|
421
|
+
this.offset = updateId ? updateId + 1 : this.offset;
|
|
367
422
|
for await (const update of updates) {
|
|
368
|
-
|
|
423
|
+
this.queue.add(update);
|
|
369
424
|
}
|
|
370
425
|
} catch (error) {
|
|
371
426
|
console.error("Error received when fetching updates", error);
|
|
@@ -418,12 +473,14 @@ _Bot_decorators = [inspectable.Inspectable({
|
|
|
418
473
|
serialize: () => ({})
|
|
419
474
|
})];
|
|
420
475
|
class Bot {
|
|
476
|
+
/** @deprecated use `~` instead*/
|
|
421
477
|
_ = {
|
|
422
|
-
/** @internal. Remap generic */
|
|
478
|
+
/** @deprecated @internal. Remap generic */
|
|
423
479
|
derives: {}
|
|
424
480
|
};
|
|
425
|
-
/** @internal. Remap generic */
|
|
481
|
+
/** @deprecated use `~.derives` instead @internal. Remap generic */
|
|
426
482
|
__Derives;
|
|
483
|
+
"~" = this._;
|
|
427
484
|
filters = {
|
|
428
485
|
context: (name2) => (context) => context.is(name2)
|
|
429
486
|
};
|
|
@@ -515,7 +572,7 @@ class Bot {
|
|
|
515
572
|
}
|
|
516
573
|
async _callApi(method, params = {}) {
|
|
517
574
|
const debug$api$method = debug$api.extend(method);
|
|
518
|
-
const url = `${this.options.api.baseURL}${this.options.token}/${method}`;
|
|
575
|
+
const url = `${this.options.api.baseURL}${this.options.token}/${this.options.api.useTest ? "test/" : ""}${method}`;
|
|
519
576
|
const reqOptions = {
|
|
520
577
|
method: "POST",
|
|
521
578
|
...this.options.api.fetchOptions,
|
|
@@ -1076,9 +1133,11 @@ class Bot {
|
|
|
1076
1133
|
* Stops receiving events via long-polling or webhook
|
|
1077
1134
|
* Currently does not implement graceful shutdown
|
|
1078
1135
|
* */
|
|
1079
|
-
async stop() {
|
|
1080
|
-
if (this.updates.isStarted)
|
|
1081
|
-
|
|
1136
|
+
async stop(timeout = 3e3) {
|
|
1137
|
+
if (this.updates.isStarted) {
|
|
1138
|
+
this.updates.stopPolling();
|
|
1139
|
+
}
|
|
1140
|
+
await this.updates.queue.stop(timeout);
|
|
1082
1141
|
await this.runImmutableHooks("onStop", {
|
|
1083
1142
|
plugins: this.dependencies,
|
|
1084
1143
|
// biome-ignore lint/style/noNonNullAssertion: bot.init() guarantees this.info
|
package/dist/index.d.cts
CHANGED
|
@@ -56,6 +56,7 @@ declare class Composer {
|
|
|
56
56
|
derive<Update extends UpdateName, Handler extends Hooks.Derive<ContextType<AnyBot, Update>>>(updateNameOrHandler: MaybeArray<Update> | Handler, handler?: Handler): this;
|
|
57
57
|
protected recompose(): this;
|
|
58
58
|
compose(context: Context<AnyBot>, next?: middleware_io.NextMiddleware): void;
|
|
59
|
+
composeWait(context: Context<AnyBot>, next?: middleware_io.NextMiddleware): unknown;
|
|
59
60
|
}
|
|
60
61
|
|
|
61
62
|
/**
|
|
@@ -132,6 +133,42 @@ declare class Plugin<Errors extends ErrorDefinitions = {}, Derives extends Deriv
|
|
|
132
133
|
}>;
|
|
133
134
|
decorators: Record<string, unknown>;
|
|
134
135
|
};
|
|
136
|
+
"~": {
|
|
137
|
+
/** Name of plugin */
|
|
138
|
+
name: string;
|
|
139
|
+
/** List of plugin dependencies. If user does't extend from listed there dependencies it throw a error */
|
|
140
|
+
dependencies: string[];
|
|
141
|
+
/** remap generic type. {} in runtime */
|
|
142
|
+
Errors: Errors;
|
|
143
|
+
/** remap generic type. {} in runtime */
|
|
144
|
+
Derives: Derives;
|
|
145
|
+
/** Composer */
|
|
146
|
+
composer: Composer;
|
|
147
|
+
/** Store plugin preRequests hooks */
|
|
148
|
+
preRequests: [Hooks.PreRequest<any>, MaybeArray<keyof APIMethods> | undefined][];
|
|
149
|
+
/** Store plugin onResponses hooks */
|
|
150
|
+
onResponses: [Hooks.OnResponse<any>, MaybeArray<keyof APIMethods> | undefined][];
|
|
151
|
+
/** Store plugin onResponseErrors hooks */
|
|
152
|
+
onResponseErrors: [Hooks.OnResponseError<any>, MaybeArray<keyof APIMethods> | undefined][];
|
|
153
|
+
/**
|
|
154
|
+
* Store plugin groups
|
|
155
|
+
*
|
|
156
|
+
* If you use `on` or `use` in group and on plugin-level groups handlers are registered after plugin-level handlers
|
|
157
|
+
* */
|
|
158
|
+
groups: ((bot: AnyBot) => AnyBot)[];
|
|
159
|
+
/** Store plugin onStarts hooks */
|
|
160
|
+
onStarts: Hooks.OnStart[];
|
|
161
|
+
/** Store plugin onStops hooks */
|
|
162
|
+
onStops: Hooks.OnStop[];
|
|
163
|
+
/** Store plugin onErrors hooks */
|
|
164
|
+
onErrors: Hooks.OnError<any, any>[];
|
|
165
|
+
/** Map of plugin errors */
|
|
166
|
+
errorsDefinitions: Record<string, {
|
|
167
|
+
new (...args: any): any;
|
|
168
|
+
prototype: Error;
|
|
169
|
+
}>;
|
|
170
|
+
decorators: Record<string, unknown>;
|
|
171
|
+
};
|
|
135
172
|
/** Create new Plugin. Please provide `name` */
|
|
136
173
|
constructor(name: string, { dependencies }?: {
|
|
137
174
|
dependencies?: string[];
|
|
@@ -270,6 +307,9 @@ declare class Plugin<Errors extends ErrorDefinitions = {}, Derives extends Deriv
|
|
|
270
307
|
*/
|
|
271
308
|
onError<T extends UpdateName>(updateName: MaybeArray<T>, handler: Hooks.OnError<Errors, ContextType<Bot, T> & Derives["global"] & Derives[T]>): this;
|
|
272
309
|
onError(handler: Hooks.OnError<Errors, Context<AnyBot> & Derives["global"]>): this;
|
|
310
|
+
/**
|
|
311
|
+
* ! ** At the moment, it can only pick up types** */
|
|
312
|
+
extend<NewPlugin extends AnyPlugin>(plugin: MaybePromise<NewPlugin>): Plugin<Errors & NewPlugin["_"]["Errors"], Derives & NewPlugin["_"]["Derives"]>;
|
|
273
313
|
}
|
|
274
314
|
|
|
275
315
|
/** Bot options that you can provide to {@link Bot} constructor */
|
|
@@ -517,13 +557,27 @@ type AnyBot = Bot<any, any>;
|
|
|
517
557
|
/** Type of Bot that accepts any generics */
|
|
518
558
|
type AnyPlugin = Plugin<any, any>;
|
|
519
559
|
|
|
560
|
+
declare class UpdateQueue<Data = TelegramUpdate> {
|
|
561
|
+
private updateQueue;
|
|
562
|
+
private pendingUpdates;
|
|
563
|
+
private handler;
|
|
564
|
+
private onIdleResolver;
|
|
565
|
+
private onIdlePromise;
|
|
566
|
+
private isActive;
|
|
567
|
+
constructor(handler: (update: Data) => Promise<unknown>);
|
|
568
|
+
add(update: Data): void;
|
|
569
|
+
private start;
|
|
570
|
+
stop(timeout?: number): Promise<void>;
|
|
571
|
+
}
|
|
572
|
+
|
|
520
573
|
declare class Updates {
|
|
521
574
|
private readonly bot;
|
|
522
575
|
isStarted: boolean;
|
|
523
576
|
private offset;
|
|
524
577
|
composer: Composer;
|
|
578
|
+
queue: UpdateQueue<TelegramUpdate>;
|
|
525
579
|
constructor(bot: AnyBot, onError: CaughtMiddlewareHandler<Context<any>>);
|
|
526
|
-
handleUpdate(data: TelegramUpdate): Promise<
|
|
580
|
+
handleUpdate(data: TelegramUpdate): Promise<unknown>;
|
|
527
581
|
/** @deprecated use bot.start instead @internal */
|
|
528
582
|
startPolling(params?: APIMethodParams<"getUpdates">): Promise<void>;
|
|
529
583
|
startFetchLoop(params?: APIMethodParams<"getUpdates">): Promise<void>;
|
|
@@ -544,12 +598,17 @@ declare class Updates {
|
|
|
544
598
|
* ```
|
|
545
599
|
*/
|
|
546
600
|
declare class Bot<Errors extends ErrorDefinitions = {}, Derives extends DeriveDefinitions = DeriveDefinitions> {
|
|
601
|
+
/** @deprecated use `~` instead*/
|
|
547
602
|
_: {
|
|
548
|
-
/** @internal. Remap generic */
|
|
603
|
+
/** @deprecated @internal. Remap generic */
|
|
549
604
|
derives: Derives;
|
|
550
605
|
};
|
|
551
|
-
/** @internal. Remap generic */
|
|
606
|
+
/** @deprecated use `~.derives` instead @internal. Remap generic */
|
|
552
607
|
__Derives: Derives;
|
|
608
|
+
"~": {
|
|
609
|
+
/** @deprecated @internal. Remap generic */
|
|
610
|
+
derives: Derives;
|
|
611
|
+
};
|
|
553
612
|
private filters;
|
|
554
613
|
/** Options provided to instance */
|
|
555
614
|
readonly options: BotOptions;
|
|
@@ -927,7 +986,7 @@ declare class Bot<Errors extends ErrorDefinitions = {}, Derives extends DeriveDe
|
|
|
927
986
|
* Stops receiving events via long-polling or webhook
|
|
928
987
|
* Currently does not implement graceful shutdown
|
|
929
988
|
* */
|
|
930
|
-
stop(): Promise<void>;
|
|
989
|
+
stop(timeout?: number): Promise<void>;
|
|
931
990
|
}
|
|
932
991
|
|
|
933
992
|
declare const frameworks: {
|
package/dist/index.d.ts
CHANGED
|
@@ -56,6 +56,7 @@ declare class Composer {
|
|
|
56
56
|
derive<Update extends UpdateName, Handler extends Hooks.Derive<ContextType<AnyBot, Update>>>(updateNameOrHandler: MaybeArray<Update> | Handler, handler?: Handler): this;
|
|
57
57
|
protected recompose(): this;
|
|
58
58
|
compose(context: Context<AnyBot>, next?: middleware_io.NextMiddleware): void;
|
|
59
|
+
composeWait(context: Context<AnyBot>, next?: middleware_io.NextMiddleware): unknown;
|
|
59
60
|
}
|
|
60
61
|
|
|
61
62
|
/**
|
|
@@ -132,6 +133,42 @@ declare class Plugin<Errors extends ErrorDefinitions = {}, Derives extends Deriv
|
|
|
132
133
|
}>;
|
|
133
134
|
decorators: Record<string, unknown>;
|
|
134
135
|
};
|
|
136
|
+
"~": {
|
|
137
|
+
/** Name of plugin */
|
|
138
|
+
name: string;
|
|
139
|
+
/** List of plugin dependencies. If user does't extend from listed there dependencies it throw a error */
|
|
140
|
+
dependencies: string[];
|
|
141
|
+
/** remap generic type. {} in runtime */
|
|
142
|
+
Errors: Errors;
|
|
143
|
+
/** remap generic type. {} in runtime */
|
|
144
|
+
Derives: Derives;
|
|
145
|
+
/** Composer */
|
|
146
|
+
composer: Composer;
|
|
147
|
+
/** Store plugin preRequests hooks */
|
|
148
|
+
preRequests: [Hooks.PreRequest<any>, MaybeArray<keyof APIMethods> | undefined][];
|
|
149
|
+
/** Store plugin onResponses hooks */
|
|
150
|
+
onResponses: [Hooks.OnResponse<any>, MaybeArray<keyof APIMethods> | undefined][];
|
|
151
|
+
/** Store plugin onResponseErrors hooks */
|
|
152
|
+
onResponseErrors: [Hooks.OnResponseError<any>, MaybeArray<keyof APIMethods> | undefined][];
|
|
153
|
+
/**
|
|
154
|
+
* Store plugin groups
|
|
155
|
+
*
|
|
156
|
+
* If you use `on` or `use` in group and on plugin-level groups handlers are registered after plugin-level handlers
|
|
157
|
+
* */
|
|
158
|
+
groups: ((bot: AnyBot) => AnyBot)[];
|
|
159
|
+
/** Store plugin onStarts hooks */
|
|
160
|
+
onStarts: Hooks.OnStart[];
|
|
161
|
+
/** Store plugin onStops hooks */
|
|
162
|
+
onStops: Hooks.OnStop[];
|
|
163
|
+
/** Store plugin onErrors hooks */
|
|
164
|
+
onErrors: Hooks.OnError<any, any>[];
|
|
165
|
+
/** Map of plugin errors */
|
|
166
|
+
errorsDefinitions: Record<string, {
|
|
167
|
+
new (...args: any): any;
|
|
168
|
+
prototype: Error;
|
|
169
|
+
}>;
|
|
170
|
+
decorators: Record<string, unknown>;
|
|
171
|
+
};
|
|
135
172
|
/** Create new Plugin. Please provide `name` */
|
|
136
173
|
constructor(name: string, { dependencies }?: {
|
|
137
174
|
dependencies?: string[];
|
|
@@ -270,6 +307,9 @@ declare class Plugin<Errors extends ErrorDefinitions = {}, Derives extends Deriv
|
|
|
270
307
|
*/
|
|
271
308
|
onError<T extends UpdateName>(updateName: MaybeArray<T>, handler: Hooks.OnError<Errors, ContextType<Bot, T> & Derives["global"] & Derives[T]>): this;
|
|
272
309
|
onError(handler: Hooks.OnError<Errors, Context<AnyBot> & Derives["global"]>): this;
|
|
310
|
+
/**
|
|
311
|
+
* ! ** At the moment, it can only pick up types** */
|
|
312
|
+
extend<NewPlugin extends AnyPlugin>(plugin: MaybePromise<NewPlugin>): Plugin<Errors & NewPlugin["_"]["Errors"], Derives & NewPlugin["_"]["Derives"]>;
|
|
273
313
|
}
|
|
274
314
|
|
|
275
315
|
/** Bot options that you can provide to {@link Bot} constructor */
|
|
@@ -517,13 +557,27 @@ type AnyBot = Bot<any, any>;
|
|
|
517
557
|
/** Type of Bot that accepts any generics */
|
|
518
558
|
type AnyPlugin = Plugin<any, any>;
|
|
519
559
|
|
|
560
|
+
declare class UpdateQueue<Data = TelegramUpdate> {
|
|
561
|
+
private updateQueue;
|
|
562
|
+
private pendingUpdates;
|
|
563
|
+
private handler;
|
|
564
|
+
private onIdleResolver;
|
|
565
|
+
private onIdlePromise;
|
|
566
|
+
private isActive;
|
|
567
|
+
constructor(handler: (update: Data) => Promise<unknown>);
|
|
568
|
+
add(update: Data): void;
|
|
569
|
+
private start;
|
|
570
|
+
stop(timeout?: number): Promise<void>;
|
|
571
|
+
}
|
|
572
|
+
|
|
520
573
|
declare class Updates {
|
|
521
574
|
private readonly bot;
|
|
522
575
|
isStarted: boolean;
|
|
523
576
|
private offset;
|
|
524
577
|
composer: Composer;
|
|
578
|
+
queue: UpdateQueue<TelegramUpdate>;
|
|
525
579
|
constructor(bot: AnyBot, onError: CaughtMiddlewareHandler<Context<any>>);
|
|
526
|
-
handleUpdate(data: TelegramUpdate): Promise<
|
|
580
|
+
handleUpdate(data: TelegramUpdate): Promise<unknown>;
|
|
527
581
|
/** @deprecated use bot.start instead @internal */
|
|
528
582
|
startPolling(params?: APIMethodParams<"getUpdates">): Promise<void>;
|
|
529
583
|
startFetchLoop(params?: APIMethodParams<"getUpdates">): Promise<void>;
|
|
@@ -544,12 +598,17 @@ declare class Updates {
|
|
|
544
598
|
* ```
|
|
545
599
|
*/
|
|
546
600
|
declare class Bot<Errors extends ErrorDefinitions = {}, Derives extends DeriveDefinitions = DeriveDefinitions> {
|
|
601
|
+
/** @deprecated use `~` instead*/
|
|
547
602
|
_: {
|
|
548
|
-
/** @internal. Remap generic */
|
|
603
|
+
/** @deprecated @internal. Remap generic */
|
|
549
604
|
derives: Derives;
|
|
550
605
|
};
|
|
551
|
-
/** @internal. Remap generic */
|
|
606
|
+
/** @deprecated use `~.derives` instead @internal. Remap generic */
|
|
552
607
|
__Derives: Derives;
|
|
608
|
+
"~": {
|
|
609
|
+
/** @deprecated @internal. Remap generic */
|
|
610
|
+
derives: Derives;
|
|
611
|
+
};
|
|
553
612
|
private filters;
|
|
554
613
|
/** Options provided to instance */
|
|
555
614
|
readonly options: BotOptions;
|
|
@@ -927,7 +986,7 @@ declare class Bot<Errors extends ErrorDefinitions = {}, Derives extends DeriveDe
|
|
|
927
986
|
* Stops receiving events via long-polling or webhook
|
|
928
987
|
* Currently does not implement graceful shutdown
|
|
929
988
|
* */
|
|
930
|
-
stop(): Promise<void>;
|
|
989
|
+
stop(timeout?: number): Promise<void>;
|
|
931
990
|
}
|
|
932
991
|
|
|
933
992
|
declare const frameworks: {
|
package/dist/index.js
CHANGED
|
@@ -97,6 +97,9 @@ class Composer {
|
|
|
97
97
|
compose(context, next = noopNext) {
|
|
98
98
|
this.composed(context, next);
|
|
99
99
|
}
|
|
100
|
+
composeWait(context, next = noopNext) {
|
|
101
|
+
return this.composed(context, next);
|
|
102
|
+
}
|
|
100
103
|
}
|
|
101
104
|
|
|
102
105
|
var __create$1 = Object.create;
|
|
@@ -177,6 +180,7 @@ class Plugin {
|
|
|
177
180
|
errorsDefinitions: {},
|
|
178
181
|
decorators: {}
|
|
179
182
|
};
|
|
183
|
+
"~" = this._;
|
|
180
184
|
/** Create new Plugin. Please provide `name` */
|
|
181
185
|
constructor(name, { dependencies } = {}) {
|
|
182
186
|
this._.name = name;
|
|
@@ -304,24 +308,72 @@ class Plugin {
|
|
|
304
308
|
}
|
|
305
309
|
return this;
|
|
306
310
|
}
|
|
311
|
+
/**
|
|
312
|
+
* ! ** At the moment, it can only pick up types** */
|
|
313
|
+
extend(plugin) {
|
|
314
|
+
return this;
|
|
315
|
+
}
|
|
307
316
|
}
|
|
308
317
|
_init$1 = __decoratorStart$1();
|
|
309
318
|
Plugin = __decorateElement$1(_init$1, 0, "Plugin", _Plugin_decorators, Plugin);
|
|
310
319
|
__runInitializers$1(_init$1, 1, Plugin);
|
|
311
320
|
|
|
312
321
|
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
322
|
+
class UpdateQueue {
|
|
323
|
+
updateQueue = [];
|
|
324
|
+
pendingUpdates = /* @__PURE__ */ new Set();
|
|
325
|
+
handler;
|
|
326
|
+
onIdleResolver;
|
|
327
|
+
onIdlePromise;
|
|
328
|
+
isActive = false;
|
|
329
|
+
constructor(handler) {
|
|
330
|
+
this.handler = handler;
|
|
331
|
+
}
|
|
332
|
+
add(update) {
|
|
333
|
+
this.updateQueue.push(update);
|
|
334
|
+
this.start();
|
|
335
|
+
}
|
|
336
|
+
start() {
|
|
337
|
+
this.isActive = true;
|
|
338
|
+
while (this.updateQueue.length && this.isActive) {
|
|
339
|
+
const update = this.updateQueue.shift();
|
|
340
|
+
if (!update) continue;
|
|
341
|
+
const promise = this.handler(update);
|
|
342
|
+
this.pendingUpdates.add(promise);
|
|
343
|
+
promise.finally(async () => {
|
|
344
|
+
this.pendingUpdates.delete(promise);
|
|
345
|
+
if (this.pendingUpdates.size === 0 && this.updateQueue.length === 0 && this.onIdleResolver) {
|
|
346
|
+
this.onIdleResolver();
|
|
347
|
+
this.onIdleResolver = void 0;
|
|
348
|
+
}
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
async stop(timeout = 3e3) {
|
|
353
|
+
if (this.updateQueue.length === 0 && this.pendingUpdates.size === 0) {
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
|
+
this.onIdlePromise = new Promise((resolve) => {
|
|
357
|
+
this.onIdleResolver = resolve;
|
|
358
|
+
});
|
|
359
|
+
await Promise.race([this.onIdlePromise, sleep(timeout)]);
|
|
360
|
+
this.isActive = false;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
|
|
313
364
|
class Updates {
|
|
314
365
|
bot;
|
|
315
366
|
isStarted = false;
|
|
316
367
|
offset = 0;
|
|
317
368
|
composer;
|
|
369
|
+
queue;
|
|
318
370
|
constructor(bot, onError) {
|
|
319
371
|
this.bot = bot;
|
|
320
372
|
this.composer = new Composer(onError);
|
|
373
|
+
this.queue = new UpdateQueue(this.handleUpdate.bind(this));
|
|
321
374
|
}
|
|
322
375
|
async handleUpdate(data) {
|
|
323
376
|
const updateType = Object.keys(data).at(1);
|
|
324
|
-
this.offset = data.update_id + 1;
|
|
325
377
|
const UpdateContext = contextsMappings[updateType];
|
|
326
378
|
if (!UpdateContext) throw new Error(updateType);
|
|
327
379
|
const updatePayload = data[updateType];
|
|
@@ -347,7 +399,7 @@ class Updates {
|
|
|
347
399
|
updateId: data.update_id
|
|
348
400
|
});
|
|
349
401
|
}
|
|
350
|
-
this.composer.
|
|
402
|
+
return this.composer.composeWait(context);
|
|
351
403
|
} catch (error) {
|
|
352
404
|
throw new Error(`[UPDATES] Update type ${updateType} not supported.`);
|
|
353
405
|
}
|
|
@@ -364,10 +416,13 @@ class Updates {
|
|
|
364
416
|
try {
|
|
365
417
|
const updates = await this.bot.api.getUpdates({
|
|
366
418
|
...params,
|
|
367
|
-
offset: this.offset
|
|
419
|
+
offset: this.offset,
|
|
420
|
+
timeout: 30
|
|
368
421
|
});
|
|
422
|
+
const updateId = updates.at(-1)?.update_id;
|
|
423
|
+
this.offset = updateId ? updateId + 1 : this.offset;
|
|
369
424
|
for await (const update of updates) {
|
|
370
|
-
|
|
425
|
+
this.queue.add(update);
|
|
371
426
|
}
|
|
372
427
|
} catch (error) {
|
|
373
428
|
console.error("Error received when fetching updates", error);
|
|
@@ -420,12 +475,14 @@ _Bot_decorators = [Inspectable({
|
|
|
420
475
|
serialize: () => ({})
|
|
421
476
|
})];
|
|
422
477
|
class Bot {
|
|
478
|
+
/** @deprecated use `~` instead*/
|
|
423
479
|
_ = {
|
|
424
|
-
/** @internal. Remap generic */
|
|
480
|
+
/** @deprecated @internal. Remap generic */
|
|
425
481
|
derives: {}
|
|
426
482
|
};
|
|
427
|
-
/** @internal. Remap generic */
|
|
483
|
+
/** @deprecated use `~.derives` instead @internal. Remap generic */
|
|
428
484
|
__Derives;
|
|
485
|
+
"~" = this._;
|
|
429
486
|
filters = {
|
|
430
487
|
context: (name2) => (context) => context.is(name2)
|
|
431
488
|
};
|
|
@@ -517,7 +574,7 @@ class Bot {
|
|
|
517
574
|
}
|
|
518
575
|
async _callApi(method, params = {}) {
|
|
519
576
|
const debug$api$method = debug$api.extend(method);
|
|
520
|
-
const url = `${this.options.api.baseURL}${this.options.token}/${method}`;
|
|
577
|
+
const url = `${this.options.api.baseURL}${this.options.token}/${this.options.api.useTest ? "test/" : ""}${method}`;
|
|
521
578
|
const reqOptions = {
|
|
522
579
|
method: "POST",
|
|
523
580
|
...this.options.api.fetchOptions,
|
|
@@ -1078,9 +1135,11 @@ class Bot {
|
|
|
1078
1135
|
* Stops receiving events via long-polling or webhook
|
|
1079
1136
|
* Currently does not implement graceful shutdown
|
|
1080
1137
|
* */
|
|
1081
|
-
async stop() {
|
|
1082
|
-
if (this.updates.isStarted)
|
|
1083
|
-
|
|
1138
|
+
async stop(timeout = 3e3) {
|
|
1139
|
+
if (this.updates.isStarted) {
|
|
1140
|
+
this.updates.stopPolling();
|
|
1141
|
+
}
|
|
1142
|
+
await this.updates.queue.stop(timeout);
|
|
1084
1143
|
await this.runImmutableHooks("onStop", {
|
|
1085
1144
|
plugins: this.dependencies,
|
|
1086
1145
|
// biome-ignore lint/style/noNonNullAssertion: bot.init() guarantees this.info
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "gramio",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.1
|
|
4
|
+
"version": "0.2.1",
|
|
5
5
|
"description": "Powerful, extensible and really type-safe Telegram Bot API framework",
|
|
6
6
|
"main": "dist/index.cjs",
|
|
7
7
|
"module": "dist/index.js",
|
|
@@ -48,17 +48,14 @@
|
|
|
48
48
|
},
|
|
49
49
|
"dependencies": {
|
|
50
50
|
"@gramio/callback-data": "^0.0.3",
|
|
51
|
-
"@gramio/contexts": "^0.
|
|
52
|
-
"@gramio/files": "^0.1.
|
|
51
|
+
"@gramio/contexts": "^0.1.2",
|
|
52
|
+
"@gramio/files": "^0.1.2",
|
|
53
53
|
"@gramio/format": "^0.1.5",
|
|
54
|
-
"@gramio/keyboards": "^1.0.
|
|
54
|
+
"@gramio/keyboards": "^1.0.2",
|
|
55
55
|
"@gramio/types": "^8.1.0",
|
|
56
|
-
"debug": "^4.
|
|
56
|
+
"debug": "^4.4.0",
|
|
57
57
|
"inspectable": "^3.0.2",
|
|
58
58
|
"middleware-io": "^2.8.1"
|
|
59
59
|
},
|
|
60
|
-
"overrides": {
|
|
61
|
-
"@gramio/types": "^8.1.0"
|
|
62
|
-
},
|
|
63
60
|
"files": ["dist"]
|
|
64
61
|
}
|