@yaebal/throttle 0.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 neverlane
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,13 @@
1
+ # @yaebal/throttle
2
+
3
+ minimum ms between outgoing calls. defaults to 34 (~30/sec, telegram's global cap).
4
+
5
+ ## install
6
+
7
+ ```sh
8
+ pnpm add @yaebal/throttle
9
+ ```
10
+
11
+ ---
12
+
13
+ part of [**yaebal**](https://github.com/neverlane/yaebal) — a type-safe, runtime-agnostic Telegram Bot API framework. MIT.
package/lib/index.d.ts ADDED
@@ -0,0 +1,20 @@
1
+ import type { Api } from "@yaebal/core";
2
+ export interface ThrottleOptions {
3
+ /** minimum ms between outgoing calls. defaults to 34 (~30/sec, telegram's global cap). */
4
+ minIntervalMs?: number;
5
+ }
6
+ /**
7
+ * reserve the next call slot: no earlier than `now`, and never closer than
8
+ * `interval` to the previous slot. pure — exported for testing.
9
+ */
10
+ export declare function reserve(now: number, next: number, interval: number): {
11
+ at: number;
12
+ next: number;
13
+ };
14
+ /**
15
+ * space out outgoing api calls by at least `minIntervalMs` (hooks `api.before`).
16
+ * calls past the cap are delayed, not dropped, so nothing is lost — they just
17
+ * queue up behind the rate limit.
18
+ */
19
+ export declare function throttle(api: Api, options?: ThrottleOptions): void;
20
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,cAAc,CAAC;AAExC,MAAM,WAAW,eAAe;IAC/B,0FAA0F;IAC1F,aAAa,CAAC,EAAE,MAAM,CAAC;CACvB;AAED;;;GAGG;AACH,wBAAgB,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CAIjG;AAED;;;;GAIG;AACH,wBAAgB,QAAQ,CAAC,GAAG,EAAE,GAAG,EAAE,OAAO,GAAE,eAAoB,GAAG,IAAI,CAgBtE"}
package/lib/index.js ADDED
@@ -0,0 +1,27 @@
1
+ /**
2
+ * reserve the next call slot: no earlier than `now`, and never closer than
3
+ * `interval` to the previous slot. pure — exported for testing.
4
+ */
5
+ export function reserve(now, next, interval) {
6
+ const at = Math.max(now, next);
7
+ return { at, next: at + interval };
8
+ }
9
+ /**
10
+ * space out outgoing api calls by at least `minIntervalMs` (hooks `api.before`).
11
+ * calls past the cap are delayed, not dropped, so nothing is lost — they just
12
+ * queue up behind the rate limit.
13
+ */
14
+ export function throttle(api, options = {}) {
15
+ const interval = options.minIntervalMs ?? 34;
16
+ let next = 0;
17
+ api.before(async () => {
18
+ const now = Date.now();
19
+ const slot = reserve(now, next, interval);
20
+ next = slot.next;
21
+ const wait = slot.at - now;
22
+ if (wait > 0)
23
+ await new Promise((r) => setTimeout(r, wait));
24
+ return undefined; // keep params unchanged
25
+ });
26
+ }
27
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAOA;;;GAGG;AACH,MAAM,UAAU,OAAO,CAAC,GAAW,EAAE,IAAY,EAAE,QAAgB;IAClE,MAAM,EAAE,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;IAE/B,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,EAAE,GAAG,QAAQ,EAAE,CAAC;AACpC,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,QAAQ,CAAC,GAAQ,EAAE,UAA2B,EAAE;IAC/D,MAAM,QAAQ,GAAG,OAAO,CAAC,aAAa,IAAI,EAAE,CAAC;IAE7C,IAAI,IAAI,GAAG,CAAC,CAAC;IAEb,GAAG,CAAC,MAAM,CAAC,KAAK,IAAwB,EAAE;QACzC,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QACvB,MAAM,IAAI,GAAG,OAAO,CAAC,GAAG,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAC;QAE1C,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC;QAEjB,MAAM,IAAI,GAAG,IAAI,CAAC,EAAE,GAAG,GAAG,CAAC;QAC3B,IAAI,IAAI,GAAG,CAAC;YAAE,MAAM,IAAI,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,UAAU,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC;QAE5D,OAAO,SAAS,CAAC,CAAC,wBAAwB;IAC3C,CAAC,CAAC,CAAC;AACJ,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=index.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.test.d.ts","sourceRoot":"","sources":["../src/index.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,25 @@
1
+ import assert from "node:assert/strict";
2
+ import test from "node:test";
3
+ import { reserve, throttle } from "./index.js";
4
+ test("reserve runs the first call immediately", () => {
5
+ assert.deepEqual(reserve(1000, 0, 34), { at: 1000, next: 1034 });
6
+ });
7
+ test("reserve spaces rapid calls by the interval", () => {
8
+ assert.deepEqual(reserve(1000, 1034, 34), { at: 1034, next: 1068 });
9
+ assert.deepEqual(reserve(1034, 1068, 34), { at: 1068, next: 1102 });
10
+ });
11
+ test("reserve respects real elapsed time (no artificial wait)", () => {
12
+ assert.deepEqual(reserve(2000, 1068, 34), { at: 2000, next: 2034 });
13
+ });
14
+ test("throttle registers a before hook that resolves", async () => {
15
+ let hook;
16
+ const api = {
17
+ before: (h) => {
18
+ hook = h;
19
+ },
20
+ };
21
+ throttle(api, { minIntervalMs: 0 });
22
+ assert.equal(typeof hook, "function");
23
+ await hook?.("sendMessage", undefined); // interval 0 → no delay
24
+ });
25
+ //# sourceMappingURL=index.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.test.js","sourceRoot":"","sources":["../src/index.test.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,oBAAoB,CAAC;AACxC,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAE/C,IAAI,CAAC,yCAAyC,EAAE,GAAG,EAAE;IACpD,MAAM,CAAC,SAAS,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC,EAAE,EAAE,CAAC,EAAE,EAAE,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;AAClE,CAAC,CAAC,CAAC;AAEH,IAAI,CAAC,4CAA4C,EAAE,GAAG,EAAE;IACvD,MAAM,CAAC,SAAS,CAAC,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,EAAE,CAAC,EAAE,EAAE,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;IACpE,MAAM,CAAC,SAAS,CAAC,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,EAAE,CAAC,EAAE,EAAE,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;AACrE,CAAC,CAAC,CAAC;AAEH,IAAI,CAAC,yDAAyD,EAAE,GAAG,EAAE;IACpE,MAAM,CAAC,SAAS,CAAC,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,EAAE,CAAC,EAAE,EAAE,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;AACrE,CAAC,CAAC,CAAC;AAEH,IAAI,CAAC,gDAAgD,EAAE,KAAK,IAAI,EAAE;IACjE,IAAI,IAAyE,CAAC;IAE9E,MAAM,GAAG,GAAG;QACX,MAAM,EAAE,CAAC,CAAc,EAAE,EAAE;YAC1B,IAAI,GAAG,CAAC,CAAC;QACV,CAAC;KACQ,CAAC;IAEX,QAAQ,CAAC,GAAG,EAAE,EAAE,aAAa,EAAE,CAAC,EAAE,CAAC,CAAC;IACpC,MAAM,CAAC,KAAK,CAAC,OAAO,IAAI,EAAE,UAAU,CAAC,CAAC;IAEtC,MAAM,IAAI,EAAE,CAAC,aAAa,EAAE,SAAS,CAAC,CAAC,CAAC,wBAAwB;AACjE,CAAC,CAAC,CAAC"}
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "@yaebal/throttle",
3
+ "version": "0.0.1",
4
+ "description": "yaebal throttle plugin — space out outgoing API calls to avoid 429s.",
5
+ "type": "module",
6
+ "main": "./lib/index.js",
7
+ "types": "./lib/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./lib/index.d.ts",
11
+ "import": "./lib/index.js"
12
+ }
13
+ },
14
+ "files": [
15
+ "lib",
16
+ "src"
17
+ ],
18
+ "dependencies": {
19
+ "@yaebal/core": "0.0.1"
20
+ },
21
+ "devDependencies": {
22
+ "@types/node": "latest"
23
+ },
24
+ "engines": {
25
+ "node": ">=20"
26
+ },
27
+ "keywords": [
28
+ "telegram",
29
+ "telegram-bot",
30
+ "yaebal",
31
+ "throttle",
32
+ "rate-limit"
33
+ ],
34
+ "license": "MIT",
35
+ "repository": {
36
+ "type": "git",
37
+ "url": "https://github.com/neverlane/yaebal",
38
+ "directory": "packages/throttle"
39
+ },
40
+ "publishConfig": {
41
+ "access": "public"
42
+ },
43
+ "scripts": {
44
+ "build": "tsc -p tsconfig.json",
45
+ "typecheck": "tsc -p tsconfig.json --noEmit",
46
+ "test": "node --test lib"
47
+ }
48
+ }
@@ -0,0 +1,31 @@
1
+ import assert from "node:assert/strict";
2
+ import test from "node:test";
3
+ import { reserve, throttle } from "./index.js";
4
+
5
+ test("reserve runs the first call immediately", () => {
6
+ assert.deepEqual(reserve(1000, 0, 34), { at: 1000, next: 1034 });
7
+ });
8
+
9
+ test("reserve spaces rapid calls by the interval", () => {
10
+ assert.deepEqual(reserve(1000, 1034, 34), { at: 1034, next: 1068 });
11
+ assert.deepEqual(reserve(1034, 1068, 34), { at: 1068, next: 1102 });
12
+ });
13
+
14
+ test("reserve respects real elapsed time (no artificial wait)", () => {
15
+ assert.deepEqual(reserve(2000, 1068, 34), { at: 2000, next: 2034 });
16
+ });
17
+
18
+ test("throttle registers a before hook that resolves", async () => {
19
+ let hook: ((method: string, params: unknown) => Promise<unknown>) | undefined;
20
+
21
+ const api = {
22
+ before: (h: typeof hook) => {
23
+ hook = h;
24
+ },
25
+ } as never;
26
+
27
+ throttle(api, { minIntervalMs: 0 });
28
+ assert.equal(typeof hook, "function");
29
+
30
+ await hook?.("sendMessage", undefined); // interval 0 → no delay
31
+ });
package/src/index.ts ADDED
@@ -0,0 +1,39 @@
1
+ import type { Api } from "@yaebal/core";
2
+
3
+ export interface ThrottleOptions {
4
+ /** minimum ms between outgoing calls. defaults to 34 (~30/sec, telegram's global cap). */
5
+ minIntervalMs?: number;
6
+ }
7
+
8
+ /**
9
+ * reserve the next call slot: no earlier than `now`, and never closer than
10
+ * `interval` to the previous slot. pure — exported for testing.
11
+ */
12
+ export function reserve(now: number, next: number, interval: number): { at: number; next: number } {
13
+ const at = Math.max(now, next);
14
+
15
+ return { at, next: at + interval };
16
+ }
17
+
18
+ /**
19
+ * space out outgoing api calls by at least `minIntervalMs` (hooks `api.before`).
20
+ * calls past the cap are delayed, not dropped, so nothing is lost — they just
21
+ * queue up behind the rate limit.
22
+ */
23
+ export function throttle(api: Api, options: ThrottleOptions = {}): void {
24
+ const interval = options.minIntervalMs ?? 34;
25
+
26
+ let next = 0;
27
+
28
+ api.before(async (): Promise<undefined> => {
29
+ const now = Date.now();
30
+ const slot = reserve(now, next, interval);
31
+
32
+ next = slot.next;
33
+
34
+ const wait = slot.at - now;
35
+ if (wait > 0) await new Promise((r) => setTimeout(r, wait));
36
+
37
+ return undefined; // keep params unchanged
38
+ });
39
+ }