1.0.1 • Published 5 years ago

dlimits v1.0.1

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

dlimits

Generic rate-limiter and Express rate-limiter middleware

How to use it

  const {default: Dlimits, defaultStore} = require('dlimits');
  // Constructir allow 1 requests each 2 seconds
  const key = 'my-func';
  const funcRateLimit = new Dlimits(1, 2e3, defaultStore, { minWait: 1000, maxWait: 8000 });

  const myFunc = async () => {
    try {
      await funcRateLimit.limit(key);
      console.log('protected code')
    } catch(e) {
      console.log(e);
      // Handle rate time limit
    }
  }

  (async () => {
    await myFunc(); // Will execute
    await myFunc(); // Will show an error
    await new Promise(res => setTimeout(res, 3e3));
    await myFunc(); // Will execute
  })();

express middleware

  import Dlimits from 'dlimits-express-middleware';
  

  app.use(Dlimits(200, 1000, () => 'my-func'), /*on rate limit */ (req, res, next) => res.send('rate limit'));
1.0.1

5 years ago

1.0.0

5 years ago