All files / src/wrappers rateLimit.js

94.74% Statements 18/19
70% Branches 7/10
100% Functions 3/3
94.12% Lines 16/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 292x 2x   2x   2x 2x 2x   2x 2x   2x 6x 6x       6x 6x 2x 2x   4x          
import debug from 'debug';
import { RateLimiter } from 'limiter';
 
import JwksRateLimitError from '../errors/JwksRateLimitError';
 
export default function(client, { jwksRequestsPerMinute = 10 } = options) {
  const logger = debug('jwks');
  const getSigningKey = client.getSigningKey;
 
  const limiter = new RateLimiter(jwksRequestsPerMinute, 'minute', true);
  logger(`Configured rate limiting to JWKS endpoint at ${jwksRequestsPerMinute}/minute`);
 
  return (kid, cb) => {
    limiter.removeTokens(1, (err, remaining) => {
      Iif (err) {
        return cb(err);
      }
 
      logger('Requests to the JWKS endpoint available for the next minute:', remaining);
      if (remaining < 0) {
        logger('Too many requests to the JWKS endpoint');
        return cb(new JwksRateLimitError('Too many requests to the JWKS endpoint'));
      } else {
        return getSigningKey(kid, cb);
      }
    });
  };
}