@lucid-softworks/token-bucket 0.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Lucid Softworks
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
+ # `@lucid-softworks/token-bucket`
2
+
3
+ A continuously refilling token bucket with burst capacity, injected time, wait
4
+ estimation, and resetting.
5
+
6
+ ```ts
7
+ import { TokenBucket } from "@lucid-softworks/token-bucket";
8
+
9
+ const bucket = new TokenBucket({ capacity: 10, refillRate: 0.01 });
10
+ if (bucket.take()) await performRequest();
11
+ ```
12
+
13
+ `refillRate` is tokens per millisecond.
@@ -0,0 +1,20 @@
1
+ export interface TokenBucketOptions {
2
+ readonly capacity: number;
3
+ readonly refillRate: number;
4
+ readonly initialTokens?: number;
5
+ readonly now?: () => number;
6
+ }
7
+ /** A continuously refilling token bucket for burst-aware rate limiting. */
8
+ export declare class TokenBucket {
9
+ readonly options: TokenBucketOptions;
10
+ private tokens;
11
+ private lastRefill;
12
+ private readonly now;
13
+ constructor(options: TokenBucketOptions);
14
+ get available(): number;
15
+ take(count?: number): boolean;
16
+ timeUntilAvailable(count?: number): number;
17
+ reset(tokens?: number): void;
18
+ private refill;
19
+ }
20
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,kBAAkB;IACjC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,aAAa,CAAC,EAAE,MAAM,CAAC;IAChC,QAAQ,CAAC,GAAG,CAAC,EAAE,MAAM,MAAM,CAAC;CAC7B;AAED,2EAA2E;AAC3E,qBAAa,WAAW;IAKV,QAAQ,CAAC,OAAO,EAAE,kBAAkB;IAJhD,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,UAAU,CAAS;IAC3B,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAe;IAEnC,YAAqB,OAAO,EAAE,kBAAkB,EAkB/C;IAED,IAAI,SAAS,IAAI,MAAM,CAGtB;IAED,IAAI,CAAC,KAAK,GAAE,MAAU,GAAG,OAAO,CAQ/B;IAED,kBAAkB,CAAC,KAAK,GAAE,MAAU,GAAG,MAAM,CAe5C;IAED,KAAK,CAAC,MAAM,GAAE,MAA8B,GAAG,IAAI,CAUlD;IAED,OAAO,CAAC,MAAM;CASf"}
package/dist/index.js ADDED
@@ -0,0 +1,68 @@
1
+ /** A continuously refilling token bucket for burst-aware rate limiting. */
2
+ export class TokenBucket {
3
+ options;
4
+ tokens;
5
+ lastRefill;
6
+ now;
7
+ constructor(options) {
8
+ this.options = options;
9
+ if (!Number.isFinite(options.capacity) || options.capacity <= 0) {
10
+ throw new RangeError("capacity must be positive and finite");
11
+ }
12
+ if (!Number.isFinite(options.refillRate) || options.refillRate < 0) {
13
+ throw new RangeError("refillRate must be finite and non-negative");
14
+ }
15
+ const initial = options.initialTokens ?? options.capacity;
16
+ if (!Number.isFinite(initial) ||
17
+ initial < 0 ||
18
+ initial > options.capacity) {
19
+ throw new RangeError("initialTokens must be between zero and capacity");
20
+ }
21
+ this.tokens = initial;
22
+ this.now = options.now ?? Date.now;
23
+ this.lastRefill = this.now();
24
+ }
25
+ get available() {
26
+ this.refill();
27
+ return this.tokens;
28
+ }
29
+ take(count = 1) {
30
+ if (!Number.isFinite(count) || count <= 0) {
31
+ throw new RangeError("count must be positive and finite");
32
+ }
33
+ this.refill();
34
+ if (this.tokens < count)
35
+ return false;
36
+ this.tokens -= count;
37
+ return true;
38
+ }
39
+ timeUntilAvailable(count = 1) {
40
+ if (!Number.isFinite(count) ||
41
+ count <= 0 ||
42
+ count > this.options.capacity) {
43
+ throw new RangeError("count must be positive and no greater than capacity");
44
+ }
45
+ this.refill();
46
+ if (this.tokens >= count)
47
+ return 0;
48
+ return this.options.refillRate === 0
49
+ ? Number.POSITIVE_INFINITY
50
+ : (count - this.tokens) / this.options.refillRate;
51
+ }
52
+ reset(tokens = this.options.capacity) {
53
+ if (!Number.isFinite(tokens) ||
54
+ tokens < 0 ||
55
+ tokens > this.options.capacity) {
56
+ throw new RangeError("tokens must be between zero and capacity");
57
+ }
58
+ this.tokens = tokens;
59
+ this.lastRefill = this.now();
60
+ }
61
+ refill() {
62
+ const now = this.now();
63
+ const elapsed = Math.max(0, now - this.lastRefill);
64
+ this.tokens = Math.min(this.options.capacity, this.tokens + elapsed * this.options.refillRate);
65
+ this.lastRefill = now;
66
+ }
67
+ }
68
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAOA,2EAA2E;A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AAG,IAAI,CAAC,UAAU,CAAC,CAAC;QACnD,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,GAAG,CACpB,IAAI,CAAC,OAAO,CAAC,QAAQ,EACrB,IAAI,CAAC,MAAM,GAAG,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,UAAU,CAChD,CAAC;QACF,IAAI,CAAC,UAAU,GAAG,GAAG,CAAC;IACxB,CAAC;CACF"}
package/package.json ADDED
@@ -0,0 +1,65 @@
1
+ {
2
+ "name": "@lucid-softworks/token-bucket",
3
+ "version": "0.0.0",
4
+ "description": "A continuously refilling token bucket for burst-aware limiting.",
5
+ "keywords": [
6
+ "bucket",
7
+ "javascript",
8
+ "token",
9
+ "typescript",
10
+ "utility"
11
+ ],
12
+ "homepage": "https://github.com/lucid-softworks/token-bucket#readme",
13
+ "bugs": {
14
+ "url": "https://github.com/lucid-softworks/token-bucket/issues"
15
+ },
16
+ "license": "MIT",
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/lucid-softworks/token-bucket.git"
20
+ },
21
+ "files": [
22
+ "dist"
23
+ ],
24
+ "type": "module",
25
+ "sideEffects": false,
26
+ "exports": {
27
+ ".": {
28
+ "types": "./dist/index.d.ts",
29
+ "import": "./dist/index.js"
30
+ }
31
+ },
32
+ "publishConfig": {
33
+ "access": "public",
34
+ "provenance": true
35
+ },
36
+ "scripts": {
37
+ "bench": "vitest bench --run",
38
+ "bench:smoke": "VITEST_BENCHMARK_SMOKE=1 vitest bench --run",
39
+ "build": "tsc -p tsconfig.build.json",
40
+ "check": "oxfmt --check . && oxlint . && tsc -p tsconfig.json && tsc -p tsconfig.build.json && vitest run --coverage && publint && VITEST_BENCHMARK_SMOKE=1 vitest bench --run",
41
+ "format": "oxfmt --write .",
42
+ "format:check": "oxfmt --check .",
43
+ "lint": "oxlint .",
44
+ "pack:check": "publint",
45
+ "test": "vitest run --coverage",
46
+ "typecheck": "tsc -p tsconfig.json"
47
+ },
48
+ "devDependencies": {
49
+ "@lucid-softworks/oxfmt-config": "^0.1.1",
50
+ "@lucid-softworks/oxlint-config": "^0.1.0",
51
+ "@lucid-softworks/tsconfig": "^0.1.0",
52
+ "@lucid-softworks/vitest-config": "^0.1.1",
53
+ "@types/node": "26.1.1",
54
+ "@vitest/coverage-v8": "4.1.10",
55
+ "oxfmt": "0.60.0",
56
+ "oxlint": "1.75.0",
57
+ "publint": "0.3.22",
58
+ "typescript": "7.0.2",
59
+ "vitest": "4.1.10"
60
+ },
61
+ "engines": {
62
+ "node": ">=22"
63
+ },
64
+ "packageManager": "pnpm@11.16.0"
65
+ }