@mtcute/dispatcher 0.17.2 → 0.18.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/callback-data-builder.cjs +126 -0
- package/callback-data-builder.d.cts +49 -0
- package/callback-data-builder.js +121 -0
- package/callback-data-builder.test.d.cts +1 -0
- package/context/base.d.cts +9 -0
- package/context/business-message.cjs +143 -0
- package/context/business-message.d.cts +60 -0
- package/context/business-message.js +138 -0
- package/context/callback-query.cjs +92 -0
- package/context/callback-query.d.cts +62 -0
- package/context/callback-query.js +87 -0
- package/context/chat-join-request.cjs +32 -0
- package/context/chat-join-request.d.cts +17 -0
- package/context/chat-join-request.d.ts +1 -1
- package/context/chat-join-request.js +27 -0
- package/context/chosen-inline-result.cjs +30 -0
- package/context/chosen-inline-result.d.cts +22 -0
- package/context/chosen-inline-result.d.ts +1 -1
- package/context/chosen-inline-result.js +25 -0
- package/context/index.d.cts +9 -0
- package/context/inline-query.cjs +20 -0
- package/context/inline-query.d.cts +15 -0
- package/context/inline-query.js +15 -0
- package/context/message.cjs +182 -0
- package/context/message.d.cts +82 -0
- package/context/message.d.ts +2 -2
- package/context/message.js +177 -0
- package/context/parse.cjs +45 -0
- package/context/parse.d.cts +13 -0
- package/context/parse.js +40 -0
- package/context/pre-checkout-query.cjs +24 -0
- package/context/pre-checkout-query.d.cts +17 -0
- package/context/pre-checkout-query.d.ts +1 -1
- package/context/pre-checkout-query.js +19 -0
- package/context/scene-transition.cjs +52 -0
- package/context/scene-transition.d.cts +24 -0
- package/context/scene-transition.d.ts +1 -1
- package/context/scene-transition.js +47 -0
- package/dispatcher.cjs +860 -0
- package/dispatcher.d.cts +880 -0
- package/dispatcher.d.ts +4 -4
- package/dispatcher.js +855 -0
- package/filters/bots.cjs +94 -0
- package/filters/bots.d.cts +62 -0
- package/filters/bots.d.ts +2 -4
- package/filters/bots.js +89 -0
- package/filters/bots.test.d.cts +1 -0
- package/filters/bundle.cjs +79 -0
- package/filters/bundle.d.cts +10 -0
- package/filters/bundle.js +74 -0
- package/filters/chat.cjs +43 -0
- package/filters/chat.d.cts +29 -0
- package/filters/chat.d.ts +8 -6
- package/filters/chat.js +38 -0
- package/filters/group.cjs +49 -0
- package/filters/group.d.cts +26 -0
- package/filters/group.js +44 -0
- package/filters/index.d.cts +4 -0
- package/filters/logic.cjs +57 -0
- package/filters/logic.d.cts +29 -0
- package/filters/logic.js +52 -0
- package/filters/logic.test.d.cts +1 -0
- package/filters/message.cjs +130 -0
- package/filters/message.d.cts +223 -0
- package/filters/message.d.ts +5 -1
- package/filters/message.js +125 -0
- package/filters/state.cjs +21 -0
- package/filters/state.d.cts +15 -0
- package/filters/state.js +16 -0
- package/filters/text.cjs +87 -0
- package/filters/text.d.cts +64 -0
- package/filters/text.d.ts +2 -2
- package/filters/text.js +82 -0
- package/filters/types.d.cts +91 -0
- package/filters/updates.cjs +27 -0
- package/filters/updates.d.cts +39 -0
- package/filters/updates.js +22 -0
- package/filters/user.cjs +57 -0
- package/filters/user.d.cts +24 -0
- package/filters/user.js +52 -0
- package/handler.d.cts +41 -0
- package/handler.d.ts +1 -1
- package/index.cjs +37 -2528
- package/index.js +16 -2507
- package/package.json +10 -9
- package/propagation.cjs +15 -0
- package/propagation.d.cts +22 -0
- package/propagation.js +10 -0
- package/state/index.d.cts +5 -0
- package/state/key.cjs +32 -0
- package/state/key.d.cts +24 -0
- package/state/key.js +27 -0
- package/state/provider.d.cts +5 -0
- package/state/providers/index.d.cts +2 -0
- package/state/providers/memory.cjs +79 -0
- package/state/providers/memory.d.cts +29 -0
- package/state/providers/memory.js +74 -0
- package/state/providers/sqlite.cjs +99 -0
- package/state/providers/sqlite.d.cts +28 -0
- package/state/providers/sqlite.js +94 -0
- package/state/repository.d.cts +62 -0
- package/state/service.cjs +69 -0
- package/state/service.d.cts +20 -0
- package/state/service.js +64 -0
- package/state/update-state.cjs +206 -0
- package/state/update-state.d.cts +151 -0
- package/state/update-state.d.ts +1 -1
- package/state/update-state.js +201 -0
- package/wizard.cjs +90 -0
- package/wizard.d.cts +64 -0
- package/wizard.js +85 -0
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
if (typeof globalThis !== "undefined" && !globalThis._MTCUTE_CJS_DEPRECATION_WARNED) {
|
|
2
|
+
globalThis._MTCUTE_CJS_DEPRECATION_WARNED = true;
|
|
3
|
+
console.warn("[mtcute-workspace] CommonJS support is deprecated and will be removed in 0.20.0. Please consider switching to ESM, it's " + (/* @__PURE__ */ new Date()).getFullYear() + " already.");
|
|
4
|
+
console.warn("[mtcute-workspace] Learn more about switching to ESM: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c");
|
|
5
|
+
}
|
|
6
|
+
"use strict";
|
|
7
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
8
|
+
const utils = require("@fuman/utils");
|
|
9
|
+
const utils_js = require("@mtcute/core/utils.js");
|
|
10
|
+
const makeCurrentSceneKey = (key) => `$current_scene_${key}`;
|
|
11
|
+
class StateService {
|
|
12
|
+
constructor(provider) {
|
|
13
|
+
this.provider = provider;
|
|
14
|
+
}
|
|
15
|
+
_cache = new utils.LruMap(100);
|
|
16
|
+
_vacuumTimer;
|
|
17
|
+
_loaded = false;
|
|
18
|
+
_load = utils_js.asyncResettable(async () => {
|
|
19
|
+
await this.provider.driver.load?.();
|
|
20
|
+
this._loaded = true;
|
|
21
|
+
});
|
|
22
|
+
async load() {
|
|
23
|
+
await this._load.run();
|
|
24
|
+
this._vacuumTimer = utils.timers.setInterval(() => {
|
|
25
|
+
Promise.resolve(this.provider.state.vacuum(Date.now())).catch(() => {
|
|
26
|
+
});
|
|
27
|
+
}, 3e5);
|
|
28
|
+
}
|
|
29
|
+
async destroy() {
|
|
30
|
+
await this.provider.driver.save?.();
|
|
31
|
+
await this.provider.driver.destroy?.();
|
|
32
|
+
utils.timers.clearInterval(this._vacuumTimer);
|
|
33
|
+
this._loaded = false;
|
|
34
|
+
}
|
|
35
|
+
async getState(key) {
|
|
36
|
+
if (!this._loaded) await this.load();
|
|
37
|
+
const cached = this._cache.get(key);
|
|
38
|
+
if (cached) return cached;
|
|
39
|
+
const state = await this.provider.state.getState(key, Date.now());
|
|
40
|
+
if (!state) return null;
|
|
41
|
+
return JSON.parse(state);
|
|
42
|
+
}
|
|
43
|
+
async setState(key, state, ttl) {
|
|
44
|
+
if (!this._loaded) await this.load();
|
|
45
|
+
this._cache.set(key, state);
|
|
46
|
+
await this.provider.state.setState(key, JSON.stringify(state), ttl);
|
|
47
|
+
}
|
|
48
|
+
async deleteState(key) {
|
|
49
|
+
if (!this._loaded) await this.load();
|
|
50
|
+
this._cache.delete(key);
|
|
51
|
+
await this.provider.state.deleteState(key);
|
|
52
|
+
}
|
|
53
|
+
getCurrentScene(key) {
|
|
54
|
+
return this.getState(makeCurrentSceneKey(key));
|
|
55
|
+
}
|
|
56
|
+
setCurrentScene(key, scene, ttl) {
|
|
57
|
+
return this.setState(makeCurrentSceneKey(key), scene, ttl);
|
|
58
|
+
}
|
|
59
|
+
deleteCurrentScene(key) {
|
|
60
|
+
return this.deleteState(makeCurrentSceneKey(key));
|
|
61
|
+
}
|
|
62
|
+
getRateLimit(key, limit, window) {
|
|
63
|
+
return this.provider.state.getRateLimit(key, Date.now(), limit, window);
|
|
64
|
+
}
|
|
65
|
+
resetRateLimit(key) {
|
|
66
|
+
return this.provider.state.resetRateLimit(key);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
exports.StateService = StateService;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { MaybePromise } from '@mtcute/core';
|
|
2
|
+
import { IStateStorageProvider } from './provider.js';
|
|
3
|
+
export declare class StateService {
|
|
4
|
+
readonly provider: IStateStorageProvider;
|
|
5
|
+
constructor(provider: IStateStorageProvider);
|
|
6
|
+
private _cache;
|
|
7
|
+
private _vacuumTimer?;
|
|
8
|
+
private _loaded;
|
|
9
|
+
private _load;
|
|
10
|
+
load(): Promise<void>;
|
|
11
|
+
destroy(): Promise<void>;
|
|
12
|
+
getState<T>(key: string): Promise<T | null>;
|
|
13
|
+
setState<T>(key: string, state: T, ttl?: number): Promise<void>;
|
|
14
|
+
deleteState(key: string): Promise<void>;
|
|
15
|
+
getCurrentScene(key: string): Promise<string | null>;
|
|
16
|
+
setCurrentScene(key: string, scene: string, ttl?: number): Promise<void>;
|
|
17
|
+
deleteCurrentScene(key: string): Promise<void>;
|
|
18
|
+
getRateLimit(key: string, limit: number, window: number): MaybePromise<[number, number]>;
|
|
19
|
+
resetRateLimit(key: string): MaybePromise<void>;
|
|
20
|
+
}
|
package/state/service.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { LruMap, timers } from "@fuman/utils";
|
|
2
|
+
import { asyncResettable } from "@mtcute/core/utils.js";
|
|
3
|
+
const makeCurrentSceneKey = (key) => `$current_scene_${key}`;
|
|
4
|
+
class StateService {
|
|
5
|
+
constructor(provider) {
|
|
6
|
+
this.provider = provider;
|
|
7
|
+
}
|
|
8
|
+
_cache = new LruMap(100);
|
|
9
|
+
_vacuumTimer;
|
|
10
|
+
_loaded = false;
|
|
11
|
+
_load = asyncResettable(async () => {
|
|
12
|
+
await this.provider.driver.load?.();
|
|
13
|
+
this._loaded = true;
|
|
14
|
+
});
|
|
15
|
+
async load() {
|
|
16
|
+
await this._load.run();
|
|
17
|
+
this._vacuumTimer = timers.setInterval(() => {
|
|
18
|
+
Promise.resolve(this.provider.state.vacuum(Date.now())).catch(() => {
|
|
19
|
+
});
|
|
20
|
+
}, 3e5);
|
|
21
|
+
}
|
|
22
|
+
async destroy() {
|
|
23
|
+
await this.provider.driver.save?.();
|
|
24
|
+
await this.provider.driver.destroy?.();
|
|
25
|
+
timers.clearInterval(this._vacuumTimer);
|
|
26
|
+
this._loaded = false;
|
|
27
|
+
}
|
|
28
|
+
async getState(key) {
|
|
29
|
+
if (!this._loaded) await this.load();
|
|
30
|
+
const cached = this._cache.get(key);
|
|
31
|
+
if (cached) return cached;
|
|
32
|
+
const state = await this.provider.state.getState(key, Date.now());
|
|
33
|
+
if (!state) return null;
|
|
34
|
+
return JSON.parse(state);
|
|
35
|
+
}
|
|
36
|
+
async setState(key, state, ttl) {
|
|
37
|
+
if (!this._loaded) await this.load();
|
|
38
|
+
this._cache.set(key, state);
|
|
39
|
+
await this.provider.state.setState(key, JSON.stringify(state), ttl);
|
|
40
|
+
}
|
|
41
|
+
async deleteState(key) {
|
|
42
|
+
if (!this._loaded) await this.load();
|
|
43
|
+
this._cache.delete(key);
|
|
44
|
+
await this.provider.state.deleteState(key);
|
|
45
|
+
}
|
|
46
|
+
getCurrentScene(key) {
|
|
47
|
+
return this.getState(makeCurrentSceneKey(key));
|
|
48
|
+
}
|
|
49
|
+
setCurrentScene(key, scene, ttl) {
|
|
50
|
+
return this.setState(makeCurrentSceneKey(key), scene, ttl);
|
|
51
|
+
}
|
|
52
|
+
deleteCurrentScene(key) {
|
|
53
|
+
return this.deleteState(makeCurrentSceneKey(key));
|
|
54
|
+
}
|
|
55
|
+
getRateLimit(key, limit, window) {
|
|
56
|
+
return this.provider.state.getRateLimit(key, Date.now(), limit, window);
|
|
57
|
+
}
|
|
58
|
+
resetRateLimit(key) {
|
|
59
|
+
return this.provider.state.resetRateLimit(key);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
export {
|
|
63
|
+
StateService
|
|
64
|
+
};
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
if (typeof globalThis !== "undefined" && !globalThis._MTCUTE_CJS_DEPRECATION_WARNED) {
|
|
2
|
+
globalThis._MTCUTE_CJS_DEPRECATION_WARNED = true;
|
|
3
|
+
console.warn("[mtcute-workspace] CommonJS support is deprecated and will be removed in 0.20.0. Please consider switching to ESM, it's " + (/* @__PURE__ */ new Date()).getFullYear() + " already.");
|
|
4
|
+
console.warn("[mtcute-workspace] Learn more about switching to ESM: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c");
|
|
5
|
+
}
|
|
6
|
+
"use strict";
|
|
7
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
8
|
+
const utils = require("@fuman/utils");
|
|
9
|
+
const core = require("@mtcute/core");
|
|
10
|
+
class RateLimitError extends core.MtcuteError {
|
|
11
|
+
constructor(reset) {
|
|
12
|
+
super("You are being rate limited.");
|
|
13
|
+
this.reset = reset;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
class UpdateState {
|
|
17
|
+
_key;
|
|
18
|
+
_localKey;
|
|
19
|
+
_storage;
|
|
20
|
+
_scene;
|
|
21
|
+
_scoped;
|
|
22
|
+
_cached;
|
|
23
|
+
_localStorage;
|
|
24
|
+
_localKeyBase;
|
|
25
|
+
constructor(storage, key, scene, scoped, customStorage, customKey) {
|
|
26
|
+
this._storage = storage;
|
|
27
|
+
this._key = key;
|
|
28
|
+
this._scene = scene;
|
|
29
|
+
this._scoped = scoped;
|
|
30
|
+
this._localStorage = customStorage ?? storage;
|
|
31
|
+
this._localKeyBase = customKey ?? key;
|
|
32
|
+
this._updateLocalKey();
|
|
33
|
+
}
|
|
34
|
+
/** Name of the current scene */
|
|
35
|
+
get scene() {
|
|
36
|
+
return this._scene;
|
|
37
|
+
}
|
|
38
|
+
_updateLocalKey() {
|
|
39
|
+
if (!this._scoped) {
|
|
40
|
+
this._localKey = this._localKeyBase;
|
|
41
|
+
} else {
|
|
42
|
+
this._localKey = this._scene ? `${this._scene}_${this._localKeyBase}` : this._localKeyBase;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
async get(fallback, force) {
|
|
46
|
+
if (typeof fallback === "boolean") {
|
|
47
|
+
force = fallback;
|
|
48
|
+
fallback = void 0;
|
|
49
|
+
}
|
|
50
|
+
if (!force && this._cached !== void 0) {
|
|
51
|
+
if (!this._cached && fallback) {
|
|
52
|
+
return typeof fallback === "function" ? fallback() : fallback;
|
|
53
|
+
}
|
|
54
|
+
return this._cached;
|
|
55
|
+
}
|
|
56
|
+
let res = await this._localStorage.getState(this._localKey);
|
|
57
|
+
if (!res && fallback) {
|
|
58
|
+
res = typeof fallback === "function" ? fallback() : fallback;
|
|
59
|
+
}
|
|
60
|
+
this._cached = res;
|
|
61
|
+
return res;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Set new state to the storage
|
|
65
|
+
*
|
|
66
|
+
* @param state New state
|
|
67
|
+
* @param ttl TTL for the new state (in seconds)
|
|
68
|
+
*/
|
|
69
|
+
async set(state, ttl) {
|
|
70
|
+
this._cached = state;
|
|
71
|
+
await this._localStorage.setState(this._localKey, state, ttl);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Merge the given object to the current state.
|
|
75
|
+
*
|
|
76
|
+
* > **Note**: If the storage currently has no state,
|
|
77
|
+
* > then `fallback` must be provided.
|
|
78
|
+
*
|
|
79
|
+
* Basically a shorthand to calling `.get()`,
|
|
80
|
+
* modifying and then calling `.set()`
|
|
81
|
+
*
|
|
82
|
+
* @param state State to be merged
|
|
83
|
+
* @param fallback Default state
|
|
84
|
+
* @param ttl TTL for the new state (in seconds)
|
|
85
|
+
* @param forceLoad Whether to force load the old state from storage
|
|
86
|
+
*/
|
|
87
|
+
async merge(state, params = {}) {
|
|
88
|
+
const { fallback, ttl, forceLoad } = params;
|
|
89
|
+
const old = await this.get(forceLoad);
|
|
90
|
+
if (!old) {
|
|
91
|
+
if (!fallback) {
|
|
92
|
+
throw new core.MtArgumentError("Cannot use merge on empty state without fallback.");
|
|
93
|
+
}
|
|
94
|
+
const fallback_ = typeof fallback === "function" ? fallback() : fallback;
|
|
95
|
+
await this.set({ ...fallback_, ...state }, ttl);
|
|
96
|
+
} else {
|
|
97
|
+
await this.set({ ...old, ...state }, ttl);
|
|
98
|
+
}
|
|
99
|
+
return this._cached;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Delete the state from the storage
|
|
103
|
+
*/
|
|
104
|
+
async delete() {
|
|
105
|
+
this._cached = null;
|
|
106
|
+
await this._localStorage.deleteState(this._localKey);
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Enter some scene
|
|
110
|
+
*/
|
|
111
|
+
async enter(scene, params) {
|
|
112
|
+
const { with: with_, ttl, reset = true } = params ?? {};
|
|
113
|
+
if (reset && this._scoped) await this.delete();
|
|
114
|
+
if (!scene["_scene"]) {
|
|
115
|
+
throw new core.MtArgumentError("Cannot enter a non-scene Dispatcher");
|
|
116
|
+
}
|
|
117
|
+
if (!scene["_parent"]) {
|
|
118
|
+
throw new core.MtArgumentError("This scene has not been registered");
|
|
119
|
+
}
|
|
120
|
+
this._scene = scene["_scene"];
|
|
121
|
+
this._scoped = scene["_sceneScoped"];
|
|
122
|
+
this._updateLocalKey();
|
|
123
|
+
await this._storage.setCurrentScene(this._key, this._scene, ttl);
|
|
124
|
+
if (with_) {
|
|
125
|
+
if (scene["_customStateKeyDelegate"]) {
|
|
126
|
+
throw new core.MtArgumentError("Cannot use `with` parameter when the scene uses a custom state key delegate");
|
|
127
|
+
}
|
|
128
|
+
await scene.getState(this._key).set(with_, ttl);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Exit from current scene to the root
|
|
133
|
+
*
|
|
134
|
+
* @param reset
|
|
135
|
+
* Whether to reset scene state (only applicable in case this is a scoped scene)
|
|
136
|
+
*/
|
|
137
|
+
async exit(reset = true) {
|
|
138
|
+
if (reset && this._scoped) await this.delete();
|
|
139
|
+
this._scene = null;
|
|
140
|
+
this._updateLocalKey();
|
|
141
|
+
await this._storage.deleteCurrentScene(this._key);
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Rate limit some handler.
|
|
145
|
+
*
|
|
146
|
+
* When the rate limit exceeds, {@link RateLimitError} is thrown.
|
|
147
|
+
*
|
|
148
|
+
* This is a simple rate-limiting solution that uses
|
|
149
|
+
* the same key as the state. If you need something more
|
|
150
|
+
* sophisticated and/or customizable, you'll have to implement
|
|
151
|
+
* your own rate-limiter.
|
|
152
|
+
*
|
|
153
|
+
* > **Note**: `key` is used to prefix the local key
|
|
154
|
+
* > derived using the given key delegate.
|
|
155
|
+
*
|
|
156
|
+
* @param key Key of the rate limit
|
|
157
|
+
* @param limit Maximum number of requests in `window`
|
|
158
|
+
* @param window Window size in seconds
|
|
159
|
+
* @returns Tuple containing the number of remaining and
|
|
160
|
+
* unix time in ms when the user can try again
|
|
161
|
+
*/
|
|
162
|
+
async rateLimit(key, limit, window) {
|
|
163
|
+
const [remaining, reset] = await this._localStorage.getRateLimit(`${key}:${this._localKey}`, limit, window);
|
|
164
|
+
if (!remaining) {
|
|
165
|
+
throw new RateLimitError(reset);
|
|
166
|
+
}
|
|
167
|
+
return [remaining - 1, reset];
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Throttle some handler.
|
|
171
|
+
*
|
|
172
|
+
* When the rate limit exceeds, this function waits for it to reset.
|
|
173
|
+
*
|
|
174
|
+
* This is a simple wrapper over {@link rateLimit}, and follows the same logic.
|
|
175
|
+
*
|
|
176
|
+
* > **Note**: `key` is used to prefix the local key
|
|
177
|
+
* > derived using the given key delegate.
|
|
178
|
+
*
|
|
179
|
+
* @param key Key of the rate limit
|
|
180
|
+
* @param limit Maximum number of requests in `window`
|
|
181
|
+
* @param window Window size in seconds
|
|
182
|
+
* @returns Tuple containing the number of remaining and
|
|
183
|
+
* unix time in ms when the user can try again
|
|
184
|
+
*/
|
|
185
|
+
async throttle(key, limit, window) {
|
|
186
|
+
try {
|
|
187
|
+
return await this.rateLimit(key, limit, window);
|
|
188
|
+
} catch (e) {
|
|
189
|
+
if (e instanceof RateLimitError) {
|
|
190
|
+
await utils.sleep(e.reset - Date.now());
|
|
191
|
+
return this.throttle(key, limit, window);
|
|
192
|
+
}
|
|
193
|
+
throw e;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Reset the rate limit
|
|
198
|
+
*
|
|
199
|
+
* @param key Key of the rate limit
|
|
200
|
+
*/
|
|
201
|
+
async resetRateLimit(key) {
|
|
202
|
+
await this._localStorage.resetRateLimit(`${key}:${this._localKey}`);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
exports.RateLimitError = RateLimitError;
|
|
206
|
+
exports.UpdateState = UpdateState;
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { Dispatcher } from '../dispatcher.js';
|
|
2
|
+
import { StateService } from './service.js';
|
|
3
|
+
import { MtcuteError } from '@mtcute/core';
|
|
4
|
+
/**
|
|
5
|
+
* Error thrown by `.rateLimit()`
|
|
6
|
+
*/
|
|
7
|
+
export declare class RateLimitError extends MtcuteError {
|
|
8
|
+
readonly reset: number;
|
|
9
|
+
constructor(reset: number);
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* State of the current update.
|
|
13
|
+
*
|
|
14
|
+
* @template State Type that represents the state
|
|
15
|
+
* @template SceneName Possible scene names
|
|
16
|
+
*/
|
|
17
|
+
export declare class UpdateState<State extends object> {
|
|
18
|
+
private _key;
|
|
19
|
+
private _localKey;
|
|
20
|
+
private _storage;
|
|
21
|
+
private _scene;
|
|
22
|
+
private _scoped?;
|
|
23
|
+
private _cached?;
|
|
24
|
+
private _localStorage;
|
|
25
|
+
private _localKeyBase;
|
|
26
|
+
constructor(storage: StateService, key: string, scene: string | null, scoped?: boolean, customStorage?: StateService, customKey?: string);
|
|
27
|
+
/** Name of the current scene */
|
|
28
|
+
get scene(): string | null;
|
|
29
|
+
private _updateLocalKey;
|
|
30
|
+
/**
|
|
31
|
+
* Retrieve the state from the storage, falling back to default
|
|
32
|
+
* if not found
|
|
33
|
+
*
|
|
34
|
+
* @param fallback Default state value
|
|
35
|
+
* @param force Whether to ignore cached state (def. `false`)
|
|
36
|
+
*/
|
|
37
|
+
get(fallback: State | (() => State), force?: boolean): Promise<State>;
|
|
38
|
+
/**
|
|
39
|
+
* Retrieve the state from the storage, falling back to default
|
|
40
|
+
* if not found
|
|
41
|
+
*
|
|
42
|
+
* @param fallback Default state value
|
|
43
|
+
* @param force Whether to ignore cached state (def. `false`)
|
|
44
|
+
*/
|
|
45
|
+
get(fallback?: State | (() => State), force?: boolean): Promise<State | null>;
|
|
46
|
+
/**
|
|
47
|
+
* Retrieve the state from the storage
|
|
48
|
+
*
|
|
49
|
+
* @param force Whether to ignore cached state (def. `false`)
|
|
50
|
+
*/
|
|
51
|
+
get(force?: boolean): Promise<State | null>;
|
|
52
|
+
/**
|
|
53
|
+
* Set new state to the storage
|
|
54
|
+
*
|
|
55
|
+
* @param state New state
|
|
56
|
+
* @param ttl TTL for the new state (in seconds)
|
|
57
|
+
*/
|
|
58
|
+
set(state: State, ttl?: number): Promise<void>;
|
|
59
|
+
/**
|
|
60
|
+
* Merge the given object to the current state.
|
|
61
|
+
*
|
|
62
|
+
* > **Note**: If the storage currently has no state,
|
|
63
|
+
* > then `fallback` must be provided.
|
|
64
|
+
*
|
|
65
|
+
* Basically a shorthand to calling `.get()`,
|
|
66
|
+
* modifying and then calling `.set()`
|
|
67
|
+
*
|
|
68
|
+
* @param state State to be merged
|
|
69
|
+
* @param fallback Default state
|
|
70
|
+
* @param ttl TTL for the new state (in seconds)
|
|
71
|
+
* @param forceLoad Whether to force load the old state from storage
|
|
72
|
+
*/
|
|
73
|
+
merge(state: Partial<State>, params?: {
|
|
74
|
+
fallback?: State | (() => State);
|
|
75
|
+
ttl?: number;
|
|
76
|
+
forceLoad?: boolean;
|
|
77
|
+
}): Promise<State>;
|
|
78
|
+
/**
|
|
79
|
+
* Delete the state from the storage
|
|
80
|
+
*/
|
|
81
|
+
delete(): Promise<void>;
|
|
82
|
+
/**
|
|
83
|
+
* Enter some scene
|
|
84
|
+
*/
|
|
85
|
+
enter<SceneState extends object, Scene extends Dispatcher<SceneState>>(scene: Scene, params?: {
|
|
86
|
+
/**
|
|
87
|
+
* Initial state for the scene
|
|
88
|
+
*
|
|
89
|
+
* Note that this will only work if the scene uses the same key delegate as this state.
|
|
90
|
+
*/
|
|
91
|
+
with?: SceneState;
|
|
92
|
+
/** TTL for the scene (in seconds) */
|
|
93
|
+
ttl?: number;
|
|
94
|
+
/**
|
|
95
|
+
* If currently in a scoped scene, whether to reset the state
|
|
96
|
+
*
|
|
97
|
+
* @default true
|
|
98
|
+
*/
|
|
99
|
+
reset?: boolean;
|
|
100
|
+
}): Promise<void>;
|
|
101
|
+
/**
|
|
102
|
+
* Exit from current scene to the root
|
|
103
|
+
*
|
|
104
|
+
* @param reset
|
|
105
|
+
* Whether to reset scene state (only applicable in case this is a scoped scene)
|
|
106
|
+
*/
|
|
107
|
+
exit(reset?: boolean): Promise<void>;
|
|
108
|
+
/**
|
|
109
|
+
* Rate limit some handler.
|
|
110
|
+
*
|
|
111
|
+
* When the rate limit exceeds, {@link RateLimitError} is thrown.
|
|
112
|
+
*
|
|
113
|
+
* This is a simple rate-limiting solution that uses
|
|
114
|
+
* the same key as the state. If you need something more
|
|
115
|
+
* sophisticated and/or customizable, you'll have to implement
|
|
116
|
+
* your own rate-limiter.
|
|
117
|
+
*
|
|
118
|
+
* > **Note**: `key` is used to prefix the local key
|
|
119
|
+
* > derived using the given key delegate.
|
|
120
|
+
*
|
|
121
|
+
* @param key Key of the rate limit
|
|
122
|
+
* @param limit Maximum number of requests in `window`
|
|
123
|
+
* @param window Window size in seconds
|
|
124
|
+
* @returns Tuple containing the number of remaining and
|
|
125
|
+
* unix time in ms when the user can try again
|
|
126
|
+
*/
|
|
127
|
+
rateLimit(key: string, limit: number, window: number): Promise<[number, number]>;
|
|
128
|
+
/**
|
|
129
|
+
* Throttle some handler.
|
|
130
|
+
*
|
|
131
|
+
* When the rate limit exceeds, this function waits for it to reset.
|
|
132
|
+
*
|
|
133
|
+
* This is a simple wrapper over {@link rateLimit}, and follows the same logic.
|
|
134
|
+
*
|
|
135
|
+
* > **Note**: `key` is used to prefix the local key
|
|
136
|
+
* > derived using the given key delegate.
|
|
137
|
+
*
|
|
138
|
+
* @param key Key of the rate limit
|
|
139
|
+
* @param limit Maximum number of requests in `window`
|
|
140
|
+
* @param window Window size in seconds
|
|
141
|
+
* @returns Tuple containing the number of remaining and
|
|
142
|
+
* unix time in ms when the user can try again
|
|
143
|
+
*/
|
|
144
|
+
throttle(key: string, limit: number, window: number): Promise<[number, number]>;
|
|
145
|
+
/**
|
|
146
|
+
* Reset the rate limit
|
|
147
|
+
*
|
|
148
|
+
* @param key Key of the rate limit
|
|
149
|
+
*/
|
|
150
|
+
resetRateLimit(key: string): Promise<void>;
|
|
151
|
+
}
|
package/state/update-state.d.ts
CHANGED