Skip to content

Commit acc95cc

Browse files
committed
added some comments ofr easiser draft review
1 parent 81d645b commit acc95cc

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/middleware/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff 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,

src/middleware/rateLimiterSetup.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
import Redis from 'ioredis';
22
import 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+
*/
413
export 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
}

0 commit comments

Comments
 (0)