@joint-ops/hitlimit-bun 1.0.3 → 1.0.4

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/README.md CHANGED
@@ -73,14 +73,11 @@ import { createHitLimit } from '@joint-ops/hitlimit-bun'
73
73
  const limiter = createHitLimit({ limit: 100, window: '1m' })
74
74
 
75
75
  Bun.serve({
76
- async fetch(req) {
77
- const result = await limiter(req)
78
- if (!result.allowed) {
79
- return new Response(JSON.stringify(result.body), {
80
- status: 429,
81
- headers: result.headers
82
- })
83
- }
76
+ async fetch(req, server) {
77
+ // Returns a 429 Response if blocked, or null if allowed
78
+ const blocked = await limiter.check(req, server)
79
+ if (blocked) return blocked
80
+
84
81
  return new Response('Hello!')
85
82
  }
86
83
  })
@@ -106,17 +103,15 @@ Prevent brute force attacks on login endpoints.
106
103
  const authLimiter = createHitLimit({ limit: 5, window: '15m' })
107
104
 
108
105
  Bun.serve({
109
- async fetch(req) {
106
+ async fetch(req, server) {
110
107
  const url = new URL(req.url)
111
108
 
112
109
  if (url.pathname.startsWith('/auth')) {
113
- const result = await authLimiter(req)
114
- if (!result.allowed) {
115
- return new Response('Too many attempts', { status: 429 })
116
- }
110
+ const blocked = await authLimiter.check(req, server)
111
+ if (blocked) return blocked
117
112
  }
118
113
 
119
- return handler(req)
114
+ return handler(req, server)
120
115
  }
121
116
  })
122
117
  ```
@@ -153,19 +148,19 @@ Apply different limits to different route groups in Elysia.
153
148
  ```typescript
154
149
  new Elysia()
155
150
  // Global limit
156
- .use(hitlimit({ limit: 100, window: '1m' }))
151
+ .use(hitlimit({ limit: 100, window: '1m', name: 'global' }))
157
152
 
158
153
  // Stricter limit for auth
159
154
  .group('/auth', (app) =>
160
155
  app
161
- .use(hitlimit({ limit: 5, window: '15m' }))
156
+ .use(hitlimit({ limit: 5, window: '15m', name: 'auth' }))
162
157
  .post('/login', handler)
163
158
  )
164
159
 
165
160
  // Higher limit for API
166
161
  .group('/api', (app) =>
167
162
  app
168
- .use(hitlimit({ limit: 1000, window: '1m' }))
163
+ .use(hitlimit({ limit: 1000, window: '1m', name: 'api' }))
169
164
  .get('/data', handler)
170
165
  )
171
166
  .listen(3000)
@@ -311,13 +306,13 @@ Retry-After: 42
311
306
 
312
307
  hitlimit-bun is optimized for Bun's runtime with native performance:
313
308
 
314
- ### Store Benchmarks (Bun 1.0)
309
+ ### Store Benchmarks (Bun 1.3)
315
310
 
316
311
  | Store | Operations/sec | vs Node.js |
317
312
  |-------|----------------|------------|
318
- | **Memory** | 500,000+ | +25% faster |
319
- | **bun:sqlite** | 95,000+ | **+171% faster** 🔥 |
320
- | **Redis** | 15,000+ | +25% faster |
313
+ | **Memory** | 7,210,000+ | +130% faster |
314
+ | **bun:sqlite** | 520,000+ | **+10% faster** 🔥 |
315
+ | **Redis** | 6,900+ | +3% faster |
321
316
 
322
317
  ### HTTP Throughput
323
318
 
@@ -326,6 +321,8 @@ hitlimit-bun is optimized for Bun's runtime with native performance:
326
321
  | **Bun.serve** | 105,000 req/s | 12% |
327
322
  | **Elysia** | 115,000 req/s | 11% |
328
323
 
324
+ > **Note:** Benchmark results vary by hardware and environment. Run your own benchmarks to see results on your specific setup.
325
+
329
326
  ### Why bun:sqlite is So Fast
330
327
 
331
328
  ```
package/dist/elysia.d.ts CHANGED
@@ -4,6 +4,7 @@ export interface ElysiaHitLimitOptions extends HitLimitOptions<{
4
4
  request: Request;
5
5
  }> {
6
6
  sqlitePath?: string;
7
+ name?: string;
7
8
  }
8
9
  export declare function hitlimit(options?: ElysiaHitLimitOptions): Elysia<"", {
9
10
  decorator: {};
@@ -19,15 +20,15 @@ export declare function hitlimit(options?: ElysiaHitLimitOptions): Elysia<"", {
19
20
  macro: {};
20
21
  macroFn: {};
21
22
  parser: {};
22
- response: {
23
- 200: Response;
24
- };
23
+ response: {};
25
24
  }, {}, {
26
25
  derive: {};
27
26
  resolve: {};
28
27
  schema: {};
29
28
  standaloneSchema: {};
30
- response: {};
29
+ response: {
30
+ 200: Response;
31
+ };
31
32
  }, {
32
33
  derive: {};
33
34
  resolve: {};
@@ -1 +1 @@
1
- {"version":3,"file":"elysia.d.ts","sourceRoot":"","sources":["../src/elysia.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AAC/B,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAA;AAKhE,MAAM,WAAW,qBAAsB,SAAQ,eAAe,CAAC;IAAE,OAAO,EAAE,OAAO,CAAA;CAAE,CAAC;IAClF,UAAU,CAAC,EAAE,MAAM,CAAA;CACpB;AAMD,wBAAgB,QAAQ,CAAC,OAAO,GAAE,qBAA0B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2C3D"}
1
+ {"version":3,"file":"elysia.d.ts","sourceRoot":"","sources":["../src/elysia.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AAC/B,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAA;AAKhE,MAAM,WAAW,qBAAsB,SAAQ,eAAe,CAAC;IAAE,OAAO,EAAE,OAAO,CAAA;CAAE,CAAC;IAClF,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,IAAI,CAAC,EAAE,MAAM,CAAA;CACd;AAQD,wBAAgB,QAAQ,CAAC,OAAO,GAAE,qBAA0B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4C3D"}
package/dist/elysia.js CHANGED
@@ -165,13 +165,15 @@ async function checkLimit(config, req) {
165
165
  }
166
166
 
167
167
  // src/elysia.ts
168
+ var instanceCounter = 0;
168
169
  function getDefaultKey(_ctx) {
169
170
  return "unknown";
170
171
  }
171
172
  function hitlimit(options = {}) {
173
+ const pluginName = options.name ?? `hitlimit-${instanceCounter++}`;
172
174
  const store = options.store ?? sqliteStore({ path: options.sqlitePath });
173
175
  const config = resolveConfig(options, store, getDefaultKey);
174
- return new Elysia({ name: "hitlimit" }).onBeforeHandle({ as: "global" }, async ({ request, set }) => {
176
+ return new Elysia({ name: pluginName }).onBeforeHandle({ as: "scoped" }, async ({ request, set }) => {
175
177
  const ctx = { request };
176
178
  if (config.skip) {
177
179
  const shouldSkip = await config.skip(ctx);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@joint-ops/hitlimit-bun",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
4
4
  "description": "Fast Bun-native rate limiting for Bun.serve & Elysia - API throttling with bun:sqlite, high performance request limiting",
5
5
  "author": {
6
6
  "name": "Shayan M Hussain",
@@ -111,7 +111,7 @@
111
111
  "test:watch": "bun test --watch"
112
112
  },
113
113
  "dependencies": {
114
- "@joint-ops/hitlimit-types": "1.0.3"
114
+ "@joint-ops/hitlimit-types": "1.0.4"
115
115
  },
116
116
  "peerDependencies": {
117
117
  "elysia": ">=1.0.0",