@joint-ops/hitlimit-bun 1.0.2 → 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 +29 -32
- package/dist/elysia.d.ts +5 -4
- package/dist/elysia.d.ts.map +1 -1
- package/dist/elysia.js +51 -17715
- package/dist/index.js +0 -29
- package/dist/stores/memory.js +0 -29
- package/dist/stores/redis.js +2 -9613
- package/dist/stores/sqlite.js +0 -29
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -17,23 +17,23 @@
|
|
|
17
17
|
**hitlimit-bun uses Bun's native SQLite** - no FFI overhead, no Node.js polyfills.
|
|
18
18
|
|
|
19
19
|
```
|
|
20
|
-
|
|
21
|
-
│
|
|
22
|
-
│ bun:sqlite ████████████████████████████
|
|
23
|
-
│ better-sqlite3
|
|
24
|
-
│
|
|
25
|
-
│ bun:sqlite is
|
|
26
|
-
│
|
|
27
|
-
|
|
20
|
+
┌─────────────────────────────────────────────────────────────────┐
|
|
21
|
+
│ │
|
|
22
|
+
│ bun:sqlite ████████████████████████████ 520,000 ops/s │
|
|
23
|
+
│ better-sqlite3 ██████████████████░░░░░░░░░░ 400,000 ops/s │
|
|
24
|
+
│ │
|
|
25
|
+
│ bun:sqlite is 30% faster with zero FFI overhead │
|
|
26
|
+
│ │
|
|
27
|
+
└─────────────────────────────────────────────────────────────────┘
|
|
28
28
|
```
|
|
29
29
|
|
|
30
30
|
- **Bun Native** - Built specifically for Bun's runtime, not a Node.js port
|
|
31
|
-
- **
|
|
32
|
-
- **
|
|
31
|
+
- **7.2M ops/sec** - Memory store performance
|
|
32
|
+
- **520K ops/sec** - With bun:sqlite persistence
|
|
33
33
|
- **Zero Config** - Works out of the box with sensible defaults
|
|
34
34
|
- **Elysia Plugin** - First-class Elysia framework integration
|
|
35
35
|
- **TypeScript First** - Full type safety and IntelliSense support
|
|
36
|
-
- **Tiny Footprint** -
|
|
36
|
+
- **Tiny Footprint** - ~23KB total, zero runtime dependencies
|
|
37
37
|
|
|
38
38
|
## Installation
|
|
39
39
|
|
|
@@ -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
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
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
|
|
114
|
-
if (
|
|
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.
|
|
309
|
+
### Store Benchmarks (Bun 1.3)
|
|
315
310
|
|
|
316
311
|
| Store | Operations/sec | vs Node.js |
|
|
317
312
|
|-------|----------------|------------|
|
|
318
|
-
| **Memory** |
|
|
319
|
-
| **bun:sqlite** |
|
|
320
|
-
| **Redis** |
|
|
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: {};
|
package/dist/elysia.d.ts.map
CHANGED
|
@@ -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;
|
|
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"}
|