Repository navigation
feat(rateLimit): rate limit headers and retryAfter - #82
Open
fratzinger wants to merge 1 commit into
Open
fratzinger wants to merge 1 commit into
fratzinger wants to merge 1 commit into
Conversation
- `headers`: report the limit to REST clients on `context.http.headers`, as `'legacy'` (`X-RateLimit-*`), `'draft-6'`, `'draft-7'` or `'draft-8'` of the IETF RateLimit header fields draft (the formats of express-rate-limit), or headers of your own from a function. Off by default. - A rejection adds `Retry-After` and puts the headers on `error.data.headers`: Feathers' REST error handlers send only the error's status and body. The docs show the middleware that sends them. - `error.data.retryAfter` (seconds) on every rejection, for socket clients too. - A store failure that isn't a `RateLimiterRes` still throws `TooManyRequests`, without headers. - Docs: send headers only for a caller's own bucket, not on a shared one or a brute-force target. TODO for the next major: drop `rateLimitRes` from the error's data, which goes to the client. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Deploying feathers-utils with
|
| Latest commit: |
f5cbd95
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://2e71b3a7.feathers-utils.pages.dev |
| Branch Preview URL: | https://feat-rate-limit-headers.feathers-utils.pages.dev |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
rateLimitrejects withTooManyRequests, but a client can't tell when to try again or how close it is to the limit. Providers like GitHub, OpenAI or IONOS answer withRetry-Afterand rate limit headers; this adds the same, opt-in.headersoption (defaultfalse): sets the limit oncontext.http.headersfor REST clients.'legacy':X-RateLimit-Limit,-Remaining,-Reset(epoch seconds).'draft-6','draft-7','draft-8': that version of the IETF RateLimit header fields draft, in the formats ofexpress-rate-limit'sstandardHeaders.(info, context) => headersfor a scheme of your own, e.g.X-RateLimit-Remaining-Requests.infois{ limit, remaining, reset, window, limited }.Retry-Afteris added and the headers also go onerror.data.headers. Feathers' REST error handlers (Express and Koa, checked against v5.0.53) send only the error's status and body, not the headers hooks set, so the docs show the middleware that sends them.error.data.retryAfter(seconds) on every rejection, with or withoutheaders, so socket clients get it too. Socket.io has no standard for this;retryAfterkeeps the name and unit of the HTTP header.RateLimiterResstill throwsTooManyRequests, without headers.No breaking change: without
headers, the only difference is the addeddata.retryAfter.Security note
The docs say to send headers only where the bucket is the caller's own (per user or IP). On a shared bucket (a static
keyor the defaultcontext.path),remainingtells every caller how much everyone else is calling. On a login or another brute-force target, the headers help an attacker pace just under the limit.error.data.rateLimitResalready exposes the same points to clients today, sincedatais serialized with the error. ATODO(next major)marks dropping it from the error; it stays oncontext.params.rateLimitfor the server.Other Information
Retry-Afteron a rejection,retryAfterwithout headers, and no headers by default. The full suite passes; the hook is at 97% coverage.rate-limit.hook.mdand the option's JSDoc (rendered by the docs site); no separate docs PR.🤖 Generated with Claude Code