File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -107,6 +107,24 @@ export class RateLimiter {
107107 async reset ( ip : string ) : Promise < void > {
108108 await this . cache . delete ( ip ) ;
109109 }
110+ /**
111+ * Returns the number of remaining requests allowed for a given IP
112+ * in the current window.
113+ *
114+ * Does not consume a request — use `check()` for that.
115+ *
116+ * @param ip - The IP address to check.
117+ * @returns Promise resolving to the number of remaining requests,
118+ * or the full limit if the IP has no recorded requests.
119+ *
120+ * @example
121+ * const left = await rateLimiter.remaining("192.168.1.1");
122+ * console.log(`You have ${left} requests left.`);
123+ */
124+ async remaining ( ip : string ) : Promise < number > {
125+ const current = ( await this . cache . get ( ip ) ) ?? 0 ;
126+ return Math . max ( 0 , this . limit - current ) ;
127+ }
110128
111129 allow ( ip : string ) : void {
112130 this . allowlist . add ( ip ) ;
You can’t perform that action at this time.
0 commit comments