File tree Expand file tree Collapse file tree 2 files changed +15
-0
lines changed Expand file tree Collapse file tree 2 files changed +15
-0
lines changed Original file line number Diff line number Diff line change @@ -39,6 +39,7 @@ export function expressRateLimiter(
3939 const redisClient = new Redis ( redisClientOptions ) ; // Default port is 6379 automatically
4040 const rateLimiter = setupRateLimiter ( rateLimiterAlgo , rateLimiterOptions , redisClient ) ;
4141
42+ // return the rate limiting middleware
4243 return async ( req : Request , res : Response , next : NextFunction ) : Promise < void > => {
4344 const requestTimestamp = new Date ( ) . valueOf ( ) ;
4445 const { query } : { query : string } = req . body ;
@@ -57,11 +58,15 @@ export function expressRateLimiter(
5758 *
5859 * req.ip and req.ips will worx in express but not with other frameworks
5960 */
61+ // check for a proxied ip address before using the ip address on request
6062 const ip : string = req . ips [ 0 ] || req . ip ;
63+
6164 // FIXME: this will only work with type complexity
6265 const queryComplexity = getQueryTypeComplexity ( query , typeWeightObject ) ;
6366
6467 try {
68+ // process the request and conditinoally respond to client with status code 429 o
69+ // r pass the request onto the next middleware function
6570 const rateLimiterResponse = await rateLimiter . processRequest (
6671 ip ,
6772 requestTimestamp ,
Original file line number Diff line number Diff line change 11import Redis from 'ioredis' ;
22import TokenBucket from '../rateLimiters/tokenBucket' ;
33
4+ /**
5+ * Instatieate the rateLimiting algorithm class based on the developer selection and options
6+ *
7+ * @export
8+ * @param {RateLimiterSelection } selection
9+ * @param {RateLimiterOptions } options
10+ * @param {Redis } client
11+ * @return {* }
12+ */
413export default function setupRateLimiter (
514 selection : RateLimiterSelection ,
615 options : RateLimiterOptions ,
@@ -24,6 +33,7 @@ export default function setupRateLimiter(
2433 throw new Error ( 'Sliding Window Counter algonithm has not be implemented.' ) ;
2534 break ;
2635 default :
36+ // typescript should never let us invoke this function with anything other than the options above
2737 throw new Error ( 'Selected rate limiting algorithm is not suppported' ) ;
2838 break ;
2939 }
You can’t perform that action at this time.
0 commit comments