Skip to content

Commit

Permalink
Option to control RateLimit-* headers (#46)
Browse files Browse the repository at this point in the history
* feat: headers option

* refactor: types

* docs: readme
  • Loading branch information
rayriffy authored May 23, 2024
1 parent 47b0d43 commit 3d0c0ae
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/mean-cougars-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'elysia-rate-limit': minor
---

added ability to let pligin not to send RateLimit-\* headers
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,15 @@ new Elysia().use(
)
```
### headers
`boolean`
Default `true`
Should this plugin automatically set `RateLimit-*` headers to the response?
If you want to disable this feature, you can set this option to `false`.
### skip
`(request: Request, key: string): boolean | Promise<boolean>`
Expand Down
4 changes: 4 additions & 0 deletions src/@types/Options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@ export interface Options {
// since this function will slightly reduce server performance
// (Default: not defined)
injectServer?: () => Server | null

// let the plugin in control of RateLimit-* headers
// (Default: true)
headers: boolean
}
1 change: 1 addition & 0 deletions src/constants/defaultOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ export const defaultOptions: Omit<Options, 'context'> = {
scoping: 'global',
countFailedRequest: false,
generator: defaultKeyGenerator,
headers: true,
skip: () => false,
}
15 changes: 9 additions & 6 deletions src/services/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,17 @@ export const plugin = (userOptions?: Partial<Options>) => {
const clonedResponse = options.errorResponse.clone()

// append headers
for (const [key, value] of Object.entries(builtHeaders))
clonedResponse.headers.set(key, value)
if (options.headers)
for (const [key, value] of Object.entries(builtHeaders))
clonedResponse.headers.set(key, value)

return clonedResponse
}
else {
// append headers
for (const [key, value] of Object.entries(builtHeaders))
set.headers[key] = value
if (options.headers)
for (const [key, value] of Object.entries(builtHeaders))
set.headers[key] = value

// set default status code
set.status = 429
Expand All @@ -96,8 +98,9 @@ export const plugin = (userOptions?: Partial<Options>) => {
}

// append headers
for (const [key, value] of Object.entries(builtHeaders))
set.headers[key] = value
if (options.headers)
for (const [key, value] of Object.entries(builtHeaders))
set.headers[key] = value

logger('plugin', 'clientKey %s passed through with %d/%d request used (resetting in %d seconds)', clientKey, options.max - payload.remaining, options.max, reset)
}
Expand Down

0 comments on commit 3d0c0ae

Please sign in to comment.