{"id":30713072,"url":"https://github.com/tdanks2000/elysia-gatekeeper","last_synced_at":"2025-09-03T03:11:08.192Z","repository":{"id":311810777,"uuid":"1045165655","full_name":"TDanks2000/elysia-gatekeeper","owner":"TDanks2000","description":"Rate limiting plugin for Elysia (Bun) with pluggable strategies and stores. Simple defaults, flexible configuration, and helpful headers out of the box.","archived":false,"fork":false,"pushed_at":"2025-08-27T00:38:36.000Z","size":37,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-08-27T01:58:27.107Z","etag":null,"topics":["elysiajs","limit","rate-limiting","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/TDanks2000.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-08-26T18:46:00.000Z","updated_at":"2025-08-27T00:38:20.000Z","dependencies_parsed_at":"2025-08-27T01:58:35.859Z","dependency_job_id":"fa8c3bf2-8764-460f-b793-b03bbf5456f7","html_url":"https://github.com/TDanks2000/elysia-gatekeeper","commit_stats":null,"previous_names":["tdanks2000/elysia-gatekeeper"],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/TDanks2000/elysia-gatekeeper","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TDanks2000%2Felysia-gatekeeper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TDanks2000%2Felysia-gatekeeper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TDanks2000%2Felysia-gatekeeper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TDanks2000%2Felysia-gatekeeper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TDanks2000","download_url":"https://codeload.github.com/TDanks2000/elysia-gatekeeper/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TDanks2000%2Felysia-gatekeeper/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273382096,"owners_count":25095386,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-03T02:00:09.631Z","response_time":76,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["elysiajs","limit","rate-limiting","typescript"],"created_at":"2025-09-03T03:11:00.170Z","updated_at":"2025-09-03T03:11:08.183Z","avatar_url":"https://github.com/TDanks2000.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Elysia Gatekeeper\n\nRate limiting plugin for Elysia (Bun) with pluggable strategies and stores. Simple defaults, flexible configuration, and helpful headers out of the box.\n\n### Features\n- **Strategies**: fixed window (default) and sliding window; bring your own custom strategy\n- **Stores**: in-memory store included; bring your own store for distributed setups\n- **Headers**: standard `X-RateLimit-*` and optional `Retry-After` headers\n- **Flexible keys**: per-IP, per-user, per-tenant, or any custom key\n- **Zero-config**: sensible defaults (`windowMs=60_000`, `max=100`)\n\n## Installation\n```bash\nbun add elysia-gatekeeper\n```\n\n## Quick start\n```ts\nimport { Elysia } from \"elysia\";\nimport { rateLimiter } from \"elysia-gatekeeper\";\n\nnew Elysia()\n  .use(rateLimiter())\n  .get(\"/\", () =\u003e \"ok\")\n  .listen(3000);\n```\n\n## Options\n```ts\nimport type { Context } from \"elysia\";\n\ninterface RateLimiterOptions {\n  windowMs: number; // length of the rate limit window in ms\n  max: number | ((ctx: Context) =\u003e number | Promise\u003cnumber\u003e);\n  headers?:\n    | boolean\n    | {\n        limit?: boolean;\n        remaining?: boolean;\n        reset?: boolean;\n        retryAfter?: boolean;\n      };\n  keyGenerator?: (ctx: Context) =\u003e string | Promise\u003cstring\u003e;\n  skip?: (ctx: Context) =\u003e boolean | Promise\u003cboolean\u003e;\n  store?: \"memory\" | RateLimitStore; // default: memory\n  strategy?: \"fixed\" | \"sliding\" | RateLimitStrategy; // default: fixed\n  statusCode?: number; // default: 429\n  message?: string | ((ctx: Context) =\u003e Response); // default: \"Too many requests\"\n  draftSpecHeaders?: boolean; // default: true (lowercase header aliases)\n}\n```\n\n### Store interface\n```ts\ninterface RateLimitStore {\n  incr(key: string, windowMs: number): Promise\u003c{ totalHits: number; resetMs: number }\u003e;\n  resetKey(key: string): Promise\u003cvoid\u003e;\n  shutdown?(): Promise\u003cvoid\u003e;\n}\n```\n\n### Strategy interface\n```ts\ninterface RateLimitStrategy {\n  incr(\n    store: RateLimitStore,\n    key: string,\n    windowMs: number,\n  ): Promise\u003c{ totalHits: number; resetMs: number }\u003e;\n}\n```\n\n## Usage examples\n\n### Sliding window strategy\n```ts\nimport { rateLimiter } from \"elysia-gatekeeper\";\n\napp.use(\n  rateLimiter({\n    windowMs: 60_000,\n    max: 100,\n    strategy: \"sliding\",\n  }),\n);\n```\n\n### Custom key generator (per-user or per-tenant)\n```ts\napp.use(\n  rateLimiter({\n    keyGenerator: (ctx) =\u003e ctx.request.headers.get(\"x-user-id\") || \"anonymous\",\n  }),\n);\n```\n\n### Disable/enable specific headers\n```ts\napp.use(\n  rateLimiter({\n    headers: { limit: true, remaining: true, reset: true, retryAfter: true },\n  }),\n);\n```\n\n### Custom strategy\n```ts\nconst tokenBucketStrategy: RateLimitStrategy = {\n  async incr(store, key, windowMs) {\n    // implement token bucket using store\n    return store.incr(key, windowMs);\n  },\n};\n\napp.use(\n  rateLimiter({\n    strategy: tokenBucketStrategy,\n  }),\n);\n```\n\n## Headers\nWhen enabled (default), responses include:\n- `X-RateLimit-Limit`: configured `max`\n- `X-RateLimit-Remaining`: remaining requests in the current window\n- `X-RateLimit-Reset`: absolute UNIX time in seconds when the window resets\n\nIf a request is blocked, `Retry-After` is set using the computed reset time. When `draftSpecHeaders` is `true`, lowercase header aliases are also set.\n\n## Stores and distribution\n- The included `MemoryStore` is fast and simple for single-instance apps.\n- For multi-instance/distributed deployments, implement a custom `RateLimitStore` using a central backend (e.g., Redis). For sliding windows, a structure like a sorted set is recommended.\n\n## Helpers\nThis package exports helper key generators under `helpers` (IP-based, header-based, etc.). Example:\n```ts\nimport { helpers, rateLimiter } from \"elysia-gatekeeper\";\n\napp.use(rateLimiter({ keyGenerator: helpers.ipKey }));\n```\n\n## Development\n- Build: `bun run build`\n- Test: `bun test`\n\n## Notes\n- Default behavior is a fixed window strategy with an in-memory store.\n- Sliding window strategy in-memory keeps a timestamp queue per key. For high-cardinality or distributed systems, prefer a centralized store.\n\n## Acknowledgements\n- Built for the Elysia framework on the [Bun](https://bun.sh) runtime\n- Inspired by common rate-limiting primitives across web frameworks\n\n## License\n\nThis project is licensed under the MIT License. See [LICENSE](./LICENSE) for details.\n\n---\n\n## ❤️ Mental Health Reminder\n\n\u003cp align=\"start\"\u003e\n  \u003ca target=\"_blank\" href=\"https://tdanks.com/mental-health/quote\"\u003e\n    ❤️ You are great, you are enough, and your presence is valued. If you’re struggling with your mental health, please reach out to someone you love and consult a professional. You are not alone. ❤️\n  \u003c/a\u003e\n\u003c/p\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftdanks2000%2Felysia-gatekeeper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftdanks2000%2Felysia-gatekeeper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftdanks2000%2Felysia-gatekeeper/lists"}