feat: rate-limit and metrics plugins #2

Merged
chrnx merged 10 commits from feature/rate-limit-metrics into develop 2026-09-19 16:58:43 +00:00
5 changed files with 101 additions and 1 deletions
Showing only changes of commit b15ce29315 - Show all commits

No files matched your search

+1 -1
View File
@@ -1,5 +1,5 @@
{
"workspace": ["./packages/jwt"],
"workspace": ["./packages/jwt", "./packages/rate-limit"],
"nodeModulesDir": "manual",
"compilerOptions": {
"experimentalDecorators": true,
+10
View File
@@ -0,0 +1,10 @@
# Changelog
## [Unreleased]
### Added
- `rateLimit({ rules, store?, keyOf?, provides? })`: publishes a `RateLimiter` under `provides`
(default `rateLimit`). Exceeding a rule throws `TooManyRequests`, a branded 429 carrying
`Retry-After`. The default `MemoryStore` sweeps expired windows on an interval and is released by
the plugin's `onShutdown`.
+53
View File
@@ -0,0 +1,53 @@
# @green-tea/rate-limit
Publishes a per-request rate limiter into the graph. A handler opts in by asking for it and naming a
rule, so the limit is visible in the handler's signature rather than hidden in a middleware chain —
and green-tea runs the step only for the routes that depend on it.
## Runtimes
| Runtime | Supported | Why not |
|---|---|---|
| Node | ✅ | |
| Deno | ✅ | |
| Bun | ✅ | |
| workerd (edge) | ❌ | The default store counts per isolate, and an edge deployment has many, so a shared limit is not one. Supported once a store that shares state ships. |
## Usage
```ts
import { createApp } from '@green-tea/core';
import { rateLimit } from '@green-tea/rate-limit';
const app = createApp({
modules: [AuthModule],
plugins: [
rateLimit({
rules: {
signin: { windowMs: 15 * 60 * 1000, max: 5, message: 'Too many attempts, try again later.' },
},
}),
],
});
```
```ts
@Post('/signin')
async signIn(@needs('rateLimit') limit: RateLimiter) {
await limit('signin');
// ...
}
```
Exceeding a rule throws a `429` carrying `Retry-After`. The error is recognised by green-tea through
a registered symbol rather than by extending its `HttpError`, so this package never imports core at
runtime.
## The store
The default `MemoryStore` keeps fixed windows in this process and sweeps expired ones on an interval.
**One process, one set of counters:** two replicas each enforce the limit separately, so the real
ceiling is `max × replicas`. Implement `RateLimitStore` against Redis when you scale past one.
The sweep timer keeps the process alive on purpose, and the plugin's `onShutdown` clears it. A custom
store owns its own lifecycle; `close()` is called on it if it has one.
+12
View File
@@ -0,0 +1,12 @@
{
"name": "@green-tea/rate-limit",
"version": "26.9.0-beta.0",
"license": "MIT",
"exports": "./src/index.ts",
"imports": {
"@green-tea/core": "npm:@green-tea/core@26.9.0-beta.2"
},
"publish": {
"include": ["src", "README.md", "CHANGELOG.md", "deno.json"]
}
}
+25
View File
@@ -0,0 +1,25 @@
{
"name": "@green-tea/rate-limit",
"version": "26.9.0-beta.0",
"private": true,
"description": "A per-request rate limiter published into the green-tea graph, with a branded 429 and a swept in-memory store.",
"license": "MIT",
"author": "green-tea contributors <projects@expressive-tea.io>",
"type": "module",
"repository": {
"type": "git",
"url": "git+https://github.com/Expressive-Tea/green-tea-plugins.git",
"directory": "packages/rate-limit"
},
"engines": {
"node": ">=22",
"deno": ">=2",
"bun": ">=1.3"
},
"exports": {
".": "./src/index.ts"
},
"devDependencies": {
"@green-tea/core": "26.9.0-beta.2"
}
}