Skip to content

Commit 4b15afe

Browse files
committed
add document
1 parent 007e150 commit 4b15afe

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

packages/fresh/src/middlewares/ip_filter.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,30 @@ function findType(
4040
}
4141

4242
export interface ipFilterOptions {
43+
/**
44+
* Called when a request is blocked by the IP filter.
45+
*
46+
* The function receives the remote information and the current request
47+
* context and should return a Response (or a Promise resolving to one)
48+
* which will be sent back to the client. If not provided, a default
49+
* 403 Forbidden response will be used.
50+
*
51+
* Parameters:
52+
* - `remote.addr` - the remote IP address as a string.
53+
* - `remote.type` - the network family: "IPv4", "IPv6", or `undefined`.
54+
* - `ctx` - the request `Context` which can be used to inspect the
55+
* request or produce a custom response.
56+
*
57+
* @example
58+
* ```ts
59+
* const options: ipFilterOptions = {
60+
* onError: (remote, ctx) => {
61+
* console.log(`Blocked ${remote.addr} (${remote.type})`, ctx.url);
62+
* return new Response("Access denied", { status: 401 });
63+
* },
64+
* };
65+
* ```
66+
*/
4367
onError?: <State>(
4468
remote: {
4569
addr: string;
@@ -85,7 +109,8 @@ export function ipFilter<State>(
85109
rules: IpFilterRules,
86110
options?: ipFilterOptions,
87111
): Middleware<State> {
88-
const onBlock = options?.onError ?? (() => new Response("Forbidden", { status: 403 }));
112+
const onBlock = options?.onError ??
113+
(() => new Response("Forbidden", { status: 403 }));
89114
return function ipFilter<State>(ctx: Context<State>) {
90115
if (
91116
ctx.info.remoteAddr.transport !== "udp" &&

0 commit comments

Comments
 (0)