@lp-labs/token-bucket 0.0.0-stage → 0.1.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 +21 -0
- package/README.md +94 -2
- package/dist/cjs/index.d.ts +27 -0
- package/dist/cjs/index.js +89 -0
- package/dist/cjs/package.json +1 -0
- package/dist/esm/index.d.ts +27 -0
- package/dist/esm/index.js +85 -0
- package/package.json +61 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Libin Prasanth
|
|
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
CHANGED
|
@@ -1,3 +1,95 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @lp-labs/token-bucket
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Fractional local token accounting with cancellable burst reservations.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install @lp-labs/token-bucket
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Zero runtime, peer, or sibling dependencies. Node 18+; strict TypeScript declarations and independent ESM/CommonJS builds. Version 0.1.0, MIT.
|
|
12
|
+
|
|
13
|
+
## Quickstart
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
import { TokenBucket } from '@lp-labs/token-bucket';
|
|
17
|
+
let now = 0;
|
|
18
|
+
const bucket = new TokenBucket({ capacity: 4, refillPerSecond: 2, clock: () => now });
|
|
19
|
+
bucket.tryTake(4);
|
|
20
|
+
now = 250;
|
|
21
|
+
console.log(bucket.available); // 0.5
|
|
22
|
+
const reservation = bucket.reserve(0.5);
|
|
23
|
+
reservation?.cancel();
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## API and contracts
|
|
27
|
+
|
|
28
|
+
Construct TokenBucket with positive finite capacity, nonnegative finite refillPerSecond,
|
|
29
|
+
and clock. initialTokens defaults to capacity and must be in [0, capacity].
|
|
30
|
+
available refreshes accounting before returning spendable tokens. tryTake(amount = 1)
|
|
31
|
+
returns false without spending when insufficient. reserve(amount = 1) returns a
|
|
32
|
+
Reservation or undefined when insufficient; delayFor(amount = 1) predicts a wait.
|
|
33
|
+
Amounts must be positive finite values no greater than capacity.
|
|
34
|
+
|
|
35
|
+
Clock values are monotonic milliseconds, supplied by the caller (for example performance.now). Backward/nonfinite clocks and invalid numeric options throw RangeError. Refill is fractional and capped; held reservations occupy capacity until committed or cancelled. Cancellation refunds exactly once; commit spends tokens and opens refill capacity. delayFor returns milliseconds or Infinity, not a scheduling guarantee. No timers, waiting queue, global server rate guarantee, distributed coordination, or adaptive/circuit-breaker policy. O(1) operations, O(active reservations) caller storage. Do not retain unused reservations indefinitely.
|
|
36
|
+
|
|
37
|
+
Arithmetic uses JavaScript floating-point precision, not fixed-point credit accounting.
|
|
38
|
+
|
|
39
|
+
## Full declarations
|
|
40
|
+
|
|
41
|
+
The complete public declarations below are generated from the package's strict source. All runtime exports, interfaces, options, return types and readonly state are listed here.
|
|
42
|
+
|
|
43
|
+
<!-- declarations -->
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
export interface TokenBucketOptions {
|
|
47
|
+
capacity: number;
|
|
48
|
+
refillPerSecond: number;
|
|
49
|
+
clock: () => number;
|
|
50
|
+
initialTokens?: number;
|
|
51
|
+
}
|
|
52
|
+
export interface Reservation {
|
|
53
|
+
readonly amount: number;
|
|
54
|
+
readonly state: 'reserved' | 'committed' | 'cancelled';
|
|
55
|
+
commit(): boolean;
|
|
56
|
+
cancel(): boolean;
|
|
57
|
+
}
|
|
58
|
+
export declare class TokenBucket {
|
|
59
|
+
readonly capacity: number;
|
|
60
|
+
readonly refillPerSecond: number;
|
|
61
|
+
private tokens;
|
|
62
|
+
private last;
|
|
63
|
+
private held;
|
|
64
|
+
private readonly clock;
|
|
65
|
+
constructor(options: TokenBucketOptions);
|
|
66
|
+
private refresh;
|
|
67
|
+
private check;
|
|
68
|
+
get available(): number;
|
|
69
|
+
tryTake(amount?: number): boolean;
|
|
70
|
+
delayFor(amount?: number): number;
|
|
71
|
+
reserve(amount?: number): Reservation | undefined;
|
|
72
|
+
}
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
<!-- end declarations -->
|
|
76
|
+
|
|
77
|
+
## Development
|
|
78
|
+
|
|
79
|
+
```sh
|
|
80
|
+
npm ci --offline
|
|
81
|
+
npm run build
|
|
82
|
+
npm run typecheck
|
|
83
|
+
npm test
|
|
84
|
+
npm run format:check
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
All tests use deterministic clocks, immediate injected sleeps, or explicit promise gates rather than timing guesses. Package exports select ESM for import and CommonJS for require, with separate declaration trees. No runtime configuration files or global hooks.
|
|
88
|
+
|
|
89
|
+
## Support
|
|
90
|
+
|
|
91
|
+
If this library is useful to you, consider supporting its maintenance with a [donation via PayPal](https://www.paypal.me/LibinPrasanth).
|
|
92
|
+
|
|
93
|
+
## Author
|
|
94
|
+
|
|
95
|
+
Created by [Libin Prasanth](https://www.linkedin.com/in/libinprasanth/). Connect on LinkedIn for updates and discussion.
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export interface TokenBucketOptions {
|
|
2
|
+
capacity: number;
|
|
3
|
+
refillPerSecond: number;
|
|
4
|
+
clock: () => number;
|
|
5
|
+
initialTokens?: number;
|
|
6
|
+
}
|
|
7
|
+
export interface Reservation {
|
|
8
|
+
readonly amount: number;
|
|
9
|
+
readonly state: 'reserved' | 'committed' | 'cancelled';
|
|
10
|
+
commit(): boolean;
|
|
11
|
+
cancel(): boolean;
|
|
12
|
+
}
|
|
13
|
+
export declare class TokenBucket {
|
|
14
|
+
readonly capacity: number;
|
|
15
|
+
readonly refillPerSecond: number;
|
|
16
|
+
private tokens;
|
|
17
|
+
private last;
|
|
18
|
+
private held;
|
|
19
|
+
private readonly clock;
|
|
20
|
+
constructor(options: TokenBucketOptions);
|
|
21
|
+
private refresh;
|
|
22
|
+
private check;
|
|
23
|
+
get available(): number;
|
|
24
|
+
tryTake(amount?: number): boolean;
|
|
25
|
+
delayFor(amount?: number): number;
|
|
26
|
+
reserve(amount?: number): Reservation | undefined;
|
|
27
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TokenBucket = void 0;
|
|
4
|
+
class TokenBucket {
|
|
5
|
+
capacity;
|
|
6
|
+
refillPerSecond;
|
|
7
|
+
tokens;
|
|
8
|
+
last;
|
|
9
|
+
held = 0;
|
|
10
|
+
clock;
|
|
11
|
+
constructor(options) {
|
|
12
|
+
const { capacity, refillPerSecond, clock, initialTokens = capacity } = options;
|
|
13
|
+
if (!Number.isFinite(capacity) ||
|
|
14
|
+
capacity <= 0 ||
|
|
15
|
+
!Number.isFinite(refillPerSecond) ||
|
|
16
|
+
refillPerSecond < 0 ||
|
|
17
|
+
!Number.isFinite(initialTokens) ||
|
|
18
|
+
initialTokens < 0 ||
|
|
19
|
+
initialTokens > capacity)
|
|
20
|
+
throw new RangeError('Invalid bucket parameters');
|
|
21
|
+
this.capacity = capacity;
|
|
22
|
+
this.refillPerSecond = refillPerSecond;
|
|
23
|
+
this.clock = clock;
|
|
24
|
+
this.tokens = initialTokens;
|
|
25
|
+
this.last = clock();
|
|
26
|
+
if (!Number.isFinite(this.last))
|
|
27
|
+
throw new RangeError('Clock must be finite');
|
|
28
|
+
}
|
|
29
|
+
refresh() {
|
|
30
|
+
const now = this.clock();
|
|
31
|
+
if (!Number.isFinite(now) || now < this.last)
|
|
32
|
+
throw new RangeError('Clock must be monotonic milliseconds');
|
|
33
|
+
const elapsed = now - this.last;
|
|
34
|
+
const seconds = Number.isFinite(elapsed) ? elapsed / 1000 : now / 1000 - this.last / 1000;
|
|
35
|
+
const refill = this.refillPerSecond === 0 ? 0 : seconds * this.refillPerSecond;
|
|
36
|
+
this.tokens = Math.min(this.capacity - this.held, this.tokens + refill);
|
|
37
|
+
this.last = now;
|
|
38
|
+
}
|
|
39
|
+
check(amount) {
|
|
40
|
+
if (!Number.isFinite(amount) || amount <= 0 || amount > this.capacity)
|
|
41
|
+
throw new RangeError('Amount must be positive and within capacity');
|
|
42
|
+
}
|
|
43
|
+
get available() {
|
|
44
|
+
this.refresh();
|
|
45
|
+
return this.tokens;
|
|
46
|
+
}
|
|
47
|
+
tryTake(amount = 1) {
|
|
48
|
+
this.check(amount);
|
|
49
|
+
this.refresh();
|
|
50
|
+
if (this.tokens < amount)
|
|
51
|
+
return false;
|
|
52
|
+
this.tokens -= amount;
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
delayFor(amount = 1) {
|
|
56
|
+
this.check(amount);
|
|
57
|
+
this.refresh();
|
|
58
|
+
if (this.tokens >= amount)
|
|
59
|
+
return 0;
|
|
60
|
+
if (amount > this.capacity - this.held || this.refillPerSecond === 0)
|
|
61
|
+
return Infinity;
|
|
62
|
+
return ((amount - this.tokens) / this.refillPerSecond) * 1000;
|
|
63
|
+
}
|
|
64
|
+
reserve(amount = 1) {
|
|
65
|
+
if (!this.tryTake(amount))
|
|
66
|
+
return undefined;
|
|
67
|
+
this.held += amount;
|
|
68
|
+
let state = 'reserved';
|
|
69
|
+
const finish = (cancel) => {
|
|
70
|
+
if (state !== 'reserved')
|
|
71
|
+
return false;
|
|
72
|
+
this.refresh();
|
|
73
|
+
this.held -= amount;
|
|
74
|
+
if (cancel)
|
|
75
|
+
this.tokens += amount;
|
|
76
|
+
state = cancel ? 'cancelled' : 'committed';
|
|
77
|
+
return true;
|
|
78
|
+
};
|
|
79
|
+
return Object.freeze({
|
|
80
|
+
amount,
|
|
81
|
+
get state() {
|
|
82
|
+
return state;
|
|
83
|
+
},
|
|
84
|
+
commit: () => finish(false),
|
|
85
|
+
cancel: () => finish(true),
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
exports.TokenBucket = TokenBucket;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export interface TokenBucketOptions {
|
|
2
|
+
capacity: number;
|
|
3
|
+
refillPerSecond: number;
|
|
4
|
+
clock: () => number;
|
|
5
|
+
initialTokens?: number;
|
|
6
|
+
}
|
|
7
|
+
export interface Reservation {
|
|
8
|
+
readonly amount: number;
|
|
9
|
+
readonly state: 'reserved' | 'committed' | 'cancelled';
|
|
10
|
+
commit(): boolean;
|
|
11
|
+
cancel(): boolean;
|
|
12
|
+
}
|
|
13
|
+
export declare class TokenBucket {
|
|
14
|
+
readonly capacity: number;
|
|
15
|
+
readonly refillPerSecond: number;
|
|
16
|
+
private tokens;
|
|
17
|
+
private last;
|
|
18
|
+
private held;
|
|
19
|
+
private readonly clock;
|
|
20
|
+
constructor(options: TokenBucketOptions);
|
|
21
|
+
private refresh;
|
|
22
|
+
private check;
|
|
23
|
+
get available(): number;
|
|
24
|
+
tryTake(amount?: number): boolean;
|
|
25
|
+
delayFor(amount?: number): number;
|
|
26
|
+
reserve(amount?: number): Reservation | undefined;
|
|
27
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
export class TokenBucket {
|
|
2
|
+
capacity;
|
|
3
|
+
refillPerSecond;
|
|
4
|
+
tokens;
|
|
5
|
+
last;
|
|
6
|
+
held = 0;
|
|
7
|
+
clock;
|
|
8
|
+
constructor(options) {
|
|
9
|
+
const { capacity, refillPerSecond, clock, initialTokens = capacity } = options;
|
|
10
|
+
if (!Number.isFinite(capacity) ||
|
|
11
|
+
capacity <= 0 ||
|
|
12
|
+
!Number.isFinite(refillPerSecond) ||
|
|
13
|
+
refillPerSecond < 0 ||
|
|
14
|
+
!Number.isFinite(initialTokens) ||
|
|
15
|
+
initialTokens < 0 ||
|
|
16
|
+
initialTokens > capacity)
|
|
17
|
+
throw new RangeError('Invalid bucket parameters');
|
|
18
|
+
this.capacity = capacity;
|
|
19
|
+
this.refillPerSecond = refillPerSecond;
|
|
20
|
+
this.clock = clock;
|
|
21
|
+
this.tokens = initialTokens;
|
|
22
|
+
this.last = clock();
|
|
23
|
+
if (!Number.isFinite(this.last))
|
|
24
|
+
throw new RangeError('Clock must be finite');
|
|
25
|
+
}
|
|
26
|
+
refresh() {
|
|
27
|
+
const now = this.clock();
|
|
28
|
+
if (!Number.isFinite(now) || now < this.last)
|
|
29
|
+
throw new RangeError('Clock must be monotonic milliseconds');
|
|
30
|
+
const elapsed = now - this.last;
|
|
31
|
+
const seconds = Number.isFinite(elapsed) ? elapsed / 1000 : now / 1000 - this.last / 1000;
|
|
32
|
+
const refill = this.refillPerSecond === 0 ? 0 : seconds * this.refillPerSecond;
|
|
33
|
+
this.tokens = Math.min(this.capacity - this.held, this.tokens + refill);
|
|
34
|
+
this.last = now;
|
|
35
|
+
}
|
|
36
|
+
check(amount) {
|
|
37
|
+
if (!Number.isFinite(amount) || amount <= 0 || amount > this.capacity)
|
|
38
|
+
throw new RangeError('Amount must be positive and within capacity');
|
|
39
|
+
}
|
|
40
|
+
get available() {
|
|
41
|
+
this.refresh();
|
|
42
|
+
return this.tokens;
|
|
43
|
+
}
|
|
44
|
+
tryTake(amount = 1) {
|
|
45
|
+
this.check(amount);
|
|
46
|
+
this.refresh();
|
|
47
|
+
if (this.tokens < amount)
|
|
48
|
+
return false;
|
|
49
|
+
this.tokens -= amount;
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
delayFor(amount = 1) {
|
|
53
|
+
this.check(amount);
|
|
54
|
+
this.refresh();
|
|
55
|
+
if (this.tokens >= amount)
|
|
56
|
+
return 0;
|
|
57
|
+
if (amount > this.capacity - this.held || this.refillPerSecond === 0)
|
|
58
|
+
return Infinity;
|
|
59
|
+
return ((amount - this.tokens) / this.refillPerSecond) * 1000;
|
|
60
|
+
}
|
|
61
|
+
reserve(amount = 1) {
|
|
62
|
+
if (!this.tryTake(amount))
|
|
63
|
+
return undefined;
|
|
64
|
+
this.held += amount;
|
|
65
|
+
let state = 'reserved';
|
|
66
|
+
const finish = (cancel) => {
|
|
67
|
+
if (state !== 'reserved')
|
|
68
|
+
return false;
|
|
69
|
+
this.refresh();
|
|
70
|
+
this.held -= amount;
|
|
71
|
+
if (cancel)
|
|
72
|
+
this.tokens += amount;
|
|
73
|
+
state = cancel ? 'cancelled' : 'committed';
|
|
74
|
+
return true;
|
|
75
|
+
};
|
|
76
|
+
return Object.freeze({
|
|
77
|
+
amount,
|
|
78
|
+
get state() {
|
|
79
|
+
return state;
|
|
80
|
+
},
|
|
81
|
+
commit: () => finish(false),
|
|
82
|
+
cancel: () => finish(true),
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,63 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lp-labs/token-bucket",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Fractional local token accounting with cancellable burst reservations.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/cjs/index.js",
|
|
8
|
+
"module": "./dist/esm/index.js",
|
|
9
|
+
"types": "./dist/esm/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"import": {
|
|
13
|
+
"types": "./dist/esm/index.d.ts",
|
|
14
|
+
"default": "./dist/esm/index.js"
|
|
15
|
+
},
|
|
16
|
+
"require": {
|
|
17
|
+
"types": "./dist/cjs/index.d.ts",
|
|
18
|
+
"default": "./dist/cjs/index.js"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"./package.json": "./package.json"
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist",
|
|
25
|
+
"README.md",
|
|
26
|
+
"LICENSE"
|
|
27
|
+
],
|
|
28
|
+
"sideEffects": false,
|
|
29
|
+
"engines": {
|
|
30
|
+
"node": ">=18"
|
|
31
|
+
},
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build": "node scripts/build.mjs",
|
|
34
|
+
"typecheck": "npm run build && node scripts/check-types.mjs",
|
|
35
|
+
"test": "npm run build && node --test tests/*.test.mjs",
|
|
36
|
+
"format": "prettier --write src scripts tests *.json README.md",
|
|
37
|
+
"format:check": "prettier --check src scripts tests *.json README.md",
|
|
38
|
+
"prepack": "npm run build"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"typescript": "^5.9.3",
|
|
42
|
+
"prettier": "^3.6.2"
|
|
43
|
+
},
|
|
44
|
+
"prettier": {
|
|
45
|
+
"singleQuote": true,
|
|
46
|
+
"semi": true,
|
|
47
|
+
"trailingComma": "all",
|
|
48
|
+
"printWidth": 100,
|
|
49
|
+
"endOfLine": "lf"
|
|
50
|
+
},
|
|
51
|
+
"publishConfig": {
|
|
52
|
+
"access": "public",
|
|
53
|
+
"registry": "https://registry.npmjs.org/"
|
|
54
|
+
},
|
|
55
|
+
"author": {
|
|
56
|
+
"name": "Libin Prasanth",
|
|
57
|
+
"url": "https://www.linkedin.com/in/libinprasanth/"
|
|
58
|
+
},
|
|
59
|
+
"funding": {
|
|
60
|
+
"type": "paypal",
|
|
61
|
+
"url": "https://www.paypal.me/LibinPrasanth"
|
|
62
|
+
}
|
|
63
|
+
}
|