-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): added redis store and minor corrections
- Loading branch information
1 parent
bed206a
commit 19ccb5f
Showing
17 changed files
with
724 additions
and
51 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,28 @@ | ||
import { serve } from "@hono/node-server"; | ||
import { Hono } from "hono"; | ||
import { type RateLimitInfo, rateLimiter } from "hono-rate-limiter"; | ||
import Page from "./Page"; | ||
|
||
// Init the app | ||
const app = new Hono<{ | ||
Variables: { | ||
rateLimit: RateLimitInfo; | ||
}; | ||
}>(); | ||
|
||
// Adding the rate limitter | ||
app.use( | ||
rateLimiter({ | ||
windowMs: 10_000, | ||
limit: 10, | ||
handler: (_, next) => next(), | ||
}), | ||
); | ||
|
||
// Routes | ||
app.get("/", (c) => c.html(<Page info={c.get("rateLimit")} />)); | ||
|
||
// Serving the app | ||
serve(app); | ||
import { serve } from "@hono/node-server"; | ||
import { Hono } from "hono"; | ||
import { type RateLimitInfo, rateLimiter } from "hono-rate-limiter"; | ||
import { logger } from "hono/logger"; | ||
import Page from "./Page"; | ||
|
||
// Init the app | ||
const app = new Hono<{ | ||
Variables: { | ||
rateLimit: RateLimitInfo; | ||
}; | ||
}>(); | ||
|
||
// Adding the rate limitter | ||
app.use( | ||
logger(), | ||
rateLimiter({ | ||
windowMs: 10_000, | ||
limit: 10, | ||
handler: (_, next) => next(), | ||
}), | ||
); | ||
|
||
// Routes | ||
app.get("/", (c) => c.html(<Page info={c.get("rateLimit")} />)); | ||
|
||
// Serving the app | ||
serve(app); |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,21 @@ | ||
{ | ||
"name": "hono-rate-limiter", | ||
"version": "0.0.1", | ||
"type": "commonjs", | ||
"dependencies": { | ||
"@swc/helpers": "~0.5.2" | ||
}, | ||
"peerDependencies": { | ||
"hono": "^4.1.1" | ||
}, | ||
"type": "commonjs", | ||
"main": "./src/index.js", | ||
"typings": "./src/index.d.ts" | ||
"exports": { | ||
".": { | ||
"default": "./src/index.js", | ||
"types": "./src/index.d.ts" | ||
}, | ||
"./redis": { | ||
"default": "./src/redis/index.js", | ||
"types": "./src/redis/index.d.ts" | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import type { Context, Env, Input } from "hono"; | ||
import { getRuntimeKey } from "hono/adapter"; | ||
|
||
export function defaultKeyGenerator< | ||
E extends Env, | ||
P extends string, | ||
I extends Input, | ||
>(c: Context<E, P, I>) { | ||
const runtime = getRuntimeKey(); | ||
|
||
let key: string | null = null; | ||
|
||
switch (runtime) { | ||
case "workerd": | ||
key = c.req.raw.headers.get("CF-Connecting-IP"); | ||
break; | ||
case "fastly": | ||
key = c.req.raw.headers.get("Fastly-Client-IP"); | ||
break; | ||
case "other": | ||
key = c.req.raw.headers.get("x-real-ip"); | ||
break; | ||
default: | ||
break; | ||
} | ||
|
||
return key ?? ""; | ||
} |
This file contains 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
File renamed without changes.
This file contains 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
Oops, something went wrong.