https://github.com/patelvivekdev/libsql-ratelimiter
libsql ratelimiter
https://github.com/patelvivekdev/libsql-ratelimiter
libsql rate-limiter
Last synced: 8 months ago
JSON representation
libsql ratelimiter
- Host: GitHub
- URL: https://github.com/patelvivekdev/libsql-ratelimiter
- Owner: patelvivekdev
- License: mit
- Created: 2025-01-15T03:49:18.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2025-01-15T03:53:49.000Z (12 months ago)
- Last Synced: 2025-04-27T16:43:38.119Z (8 months ago)
- Topics: libsql, rate-limiter
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/libsql-ratelimiter
- Size: 68.4 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# libsql-ratelimiter
A flexible rate-limiting library built on top of libSQL, providing multiple algorithms (fixed window, sliding window, and token bucket) to control how frequently actions can be performed.
## Installation
```bash
npm install libsql-ratelimiter
```
## Configuration
Environment variables can be used to set the configuration:
```env
LIBSQL_URL=
LIBSQL_AUTH_TOKEN=
```
## Basic Usage
```typescript
import { createRateLimiter } from 'libsql-ratelimiter';
async function example() {
const rateLimiter = await createRateLimiter({
url: 'file:./path/to/rate-limit.db', // process.env.LIBSQL_URL
// ...other config like authToken LIBSQL_AUTH_TOKEN can be used
});
// Check if it's initialized
console.log('Is initialized?', rateLimiter.isInitialized());
// Limit requests using the fixed window algorithm
const result = await rateLimiter.limit({
key: 'someUser',
limit: 5, // requests
window: 60, // seconds
algorithm: 'fixed', // 'fixed', 'sliding', or 'token'
});
console.log('Fixed window result:', result);
// Close the client when done
rateLimiter.close();
}
```
## API
### `createRateLimiter(config?)`
Creates and returns a new RateLimiter instance.
### `limit(options)`
Checks if a given request identified by `options.key` should be allowed under the specified algorithm. Returns a RateLimitResult object containing:
- `success`: whether the request is allowed
- `limit`: max capacity
- `remaining`: remaining limit
- `reset`: milliseconds until limit resets
### `isInitialized()`
Returns a boolean indicating if the underlying table is set up.
### `close()`
Closes the underlying client connection.