You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix: improve rate limiter IP detection with socket.remoteAddress fallback and type declarations
- Add req.socket?.remoteAddress as third fallback in defaultKeyGenerator
(after req.ip and req.remoteAddress) to bridge the gap between test
patterns and the actual default implementation
- Add ip?, remoteAddress?, socket?, and rateLimit? to ZeroRequest type
in common.d.ts so TypeScript users can work with connection-level
properties without type errors
- Add missing current/reset properties to ctx.rateLimit type to match
the runtime shape set by the rate-limit middleware
- Add concrete Bun.serve example in README showing how to populate
req.ip via server.requestIP() before rate limiting
- Add 3 new unit tests validating the socket.remoteAddress fallback
and priority ordering in defaultKeyGenerator
- Update all documentation references to reflect the expanded fallback
chain: req.ip || req.remoteAddress || req.socket?.remoteAddress
Copy file name to clipboardExpand all lines: README.md
+29-2Lines changed: 29 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -73,6 +73,32 @@ Bun.serve({
73
73
})
74
74
```
75
75
76
+
### Enabling Client IP for Rate Limiting
77
+
78
+
Bun's standard `Request` object does not expose the client IP address. To enable the default rate limiter key generator (and any middleware that reads `req.ip`), use `server.requestIP()` in the `Bun.serve` fetch handler:
// Populate req.ip from Bun's server.requestIP before passing to the router
91
+
Bun.serve({
92
+
port: 3000,
93
+
fetch(req, server) {
94
+
req.ip=server.requestIP(req)?.address
95
+
returnrouter.fetch(req)
96
+
},
97
+
})
98
+
```
99
+
100
+
> **Why is this needed?** The Fetch API `Request` type does not include connection-level properties like `ip`. Bun provides client IP via `server.requestIP(req)` in the fetch handler's second argument. Setting `req.ip` before calling `router.fetch` ensures the rate limiter (and other middleware) can identify clients correctly. Without this, the default key generator falls back to unique per-request keys, which effectively disables rate limiting.
0 commit comments