Skip to content

Commit

Permalink
fix: no longer required server to be initialized
Browse files Browse the repository at this point in the history
  • Loading branch information
rayriffy committed Apr 7, 2024
1 parent 7e64f9e commit 9ffb155
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/twelve-avocados-give.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'elysia-rate-limit': patch
---

plugin no longer server to be initialized
12 changes: 12 additions & 0 deletions .idea/elysia-rate-limit.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions src/@types/Options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ export interface Options {
// message response when rate-limit reached (Default: rate-limit reached)
responseMessage: any

// should rate limit being counted when request result is failed (Default: false)
// should the rate limit be counted when a request result is failed (Default: false)
countFailedRequest: boolean

// key generator function to categorize client for rate-limiting
generator(request: Request, server: Server): MaybePromise<string>
generator(request: Request, server: Server | null): MaybePromise<string>

// context for storing requests count
context: Context

// exposed functions for writing custom script to skip counting i.e. not counting rate limit for some requests (Default: always return false)
// exposed functions for writing a custom script to skip counting i.e.,
// not counting rate limit for some requests
// (Default: always return false)
skip: (req: Request, key?: string) => boolean | Promise<boolean>
}
6 changes: 4 additions & 2 deletions src/services/defaultKeyGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ import type { Server } from 'bun'

export const defaultKeyGenerator = (
request: Request,
server: Server
server: Server | null
): string => {
const clientAddress = server.requestIP(request)?.address
const clientAddress = server?.requestIP(request)?.address

if (clientAddress === undefined) {
let reason: string

if (request === undefined)
reason = 'request is undefined'
else if (server === null)
reason = 'server is null'
else if (server.requestIP(request) === null)
reason = '.requestIP() returns null'
else if (server.requestIP(request)?.address === undefined)
Expand Down
6 changes: 0 additions & 6 deletions src/services/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ export const plugin = (userOptions?: Partial<Options>) => {
app.onBeforeHandle({ as: 'global' }, async ({ set, request }) => {
let clientKey: string | undefined

if (app.server === null)
throw new Error('Elysia is not initialized yet. Please call .listen() first.')

/**
* if a skip option has two parameters,
* then we will generate clientKey ahead of time.
Expand Down Expand Up @@ -72,9 +69,6 @@ export const plugin = (userOptions?: Partial<Options>) => {
})

app.onError(async ({ request }) => {
if (app.server === null)
throw new Error('Elysia is not initialized yet. Please call .listen() first.')

if (!options.countFailedRequest) {
const clientKey = await options.generator(request, app.server)
await options.context.decrement(clientKey)
Expand Down

0 comments on commit 9ffb155

Please sign in to comment.