npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ddp-rate-limiter-mixin-fixed

v1.1.10

Published

A mixin for https://github.com/meteor/validated-method to add rate limitation support to Meteor's methods.

Downloads

39

Readme

ddp-rate-limiter-mixin

A mixin for mdg:validated-method to add rate limitation support to Meteor's methods.

Install

meteor add ddp-rate-limiter
npm install --save ddp-rate-limiter-mixin

Usage

import { ValidatedMethod } from 'meteor/mdg:validated-method';
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';

// limit the maximum 5 requests in 5 seconds to this method for every clients
const foo = new ValidatedMethod({
  name: 'foo',
  mixins: [RateLimiterMixin],
  rateLimit: {
    numRequests: 5,
    timeInterval: 5000,
  },
  validate: null,
  run() {
    // ...
  }
});

// limit the maximum 5 requests in 5 seconds to this method for user1 only
const boo = new ValidatedMethod({
  name: 'boo',
  mixins: [RateLimiterMixin],
  rateLimit: {
    matcher: {
      userId: 'user1',
    },
    numRequests: 5,
    timeInterval: 5000,
  },
  validate: null,
  run() {
    // ...
  }
});

// limit the maximum 5 requests in 5 seconds to this method for users who are not `Admin`
const bar = new ValidatedMethod({
  name: 'bar',
  mixins: [RateLimiterMixin],
  rateLimit: {
    // this use to match the request, optional
    matcher: {
      // optional, could be a string or a return-boolean function
      userId(userId) {
        return Meteor.users.findOne(userId).type !== 'Admin';
      },
      // optional, could be a string or a return-boolean function
      connectionId(connectionId) {
        return true;
      },
      // optional, could be a string or a return-boolean function
      clientAddress(clientAddress) {
        return true;
      },
    },
    numRequests: 5,
    timeInterval: 5000,
  },
  validate: null,
  run() {
    // ...
  }
});

Rate Limit

This package uses Meteor's DDPRateLimiter behind the scene to support rate limitation for Meteor methods. This comes with a nicer way to define rate limitation.

Using normal DDPRateLimiter way is fine, but it would introduce 'side-effect' to your system. Like sometimes you just do not know where the limitation is set, or what is the maximum requests could be called in a period of time. This package helps solving that by specifying limitation when defining a method.

rateLimit option

You have to specify the rateLimit option to define limitation.

rateLimit.matcher is optional. If specified it is similar to matcher 1st argument of DDPRateLimiter.addRule, except name is always the name of method and type is always method.

rateLimit: {
  // this use to match the request, optional
  matcher: {
    // optional, could be a string or a return-boolean function
    userId(userId) {
      // ...
      return true/false;
    },
    // optional, could be a string or a return-boolean function
    connectionId(connectionId) {
      return true/false;
    },
    // optional, could be a string or a return-boolean function
    clientAddress(clientAddress) {
      return true/false;
    },
  },
  // numRequests is the maximum number of  requests could be made in timeInterval (milliseconds)
  numRequests: 5,
  timeInterval: 5000, // 5000 milliseconds
  // callback is a function to be called after a rule is executed.
  callback: (check, ruleInput) => ()
},

rateLimit callback properties

Overview:

check: {
  allowed: boolean - indicates wether the call is allowed or not
  timeToReset: integer - milliseconds to reset the number for the current interval
  numInvocationsLeft: integer - number of invocations left for the current interval
}

ruleInput: {
  'type': string - either 'method' or 'subscription'
  'name': string - the name of the method or subscription being called
  'userId': string - the user ID attempting the method or subscription
  'connectionId': string - a string representing the user's DDP connection
  'clientAddress': string - the IP address of the user
}

For more info check the following links: