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

limitdb

v3.0.0

Published

A database for limits on top of leveldb.

Downloads

58

Readme

Build Status

limitdb is a database for limits on top of leveldb.

Currently limitdb uses the Token Bucket Algorithm.

Installation

npm i limitdb

Configure

Create an instance of Limitdb as follows:

const Limitdb = require('limitdb');
const limitdb = new Limitdb({
  path: '/tmp/limitdb',
  types: {
    ip: {
      size: 10,
      per_second: 5
    }
  }
});

Options available:

  • path (string): a path for the leveldb database.
  • inMemory (boolean): set this to true to run the entire database in memory.
  • types (object): setup your bucket types.

The type defines the characteristics of a the bucket:

  • size is the maximun content of the bucket. This is the maximun burst you allow.
  • per_interval is the amount of tokens that the bucket receive on every interval.
  • interval defines the inverval in milliseconds.

You can also define your rates using per_second, per_minute, per_hour, per_day. So per_second: 1 is equivalent to per_interval: 1, interval: 1000.

If you omit size, limitdb assumes that size is the value of per_interval. So size: 10, per_second: 10 is the same than per_second: 10.

If you don't specify a filling rate with per_interval or any other per_x, the bucket is fixed and you have to manually reset it using PUT.

You can also define overrides inside your type definitions as follows:

const Limitdb = require('limitdb');
const limitdb = new Limitdb({
  path: '/tmp/limitdb',
  types: {
    ip: {
      size: 10,
      per_second: 5,
      overrides: {
        '127.0.0.1': {
          size: 100,
          per_second: 50
        }
      }
    }
  }
});

In this case the specific bucket for 127.0.0.1 of type ip will have a greater limit.

It is also possible to define overrides by regex:

overrides: {
  'local-ips': {
    match:      /192\.168\./
    size:       100,
    per_second: 50
  }
}

and also it is possible to configure expiration of overrides:

overrides: {
  '54.32.12.31': {
    size:       100,
    per_second: 50,
    until:      new Date(2016, 4, 1)
  }
}

TAKE

limitdb.take({ type: 'ip', key: '54.21.23.12' }, (err, result) => {
  console.dir(result);
});

limitdb.take takes as argument an object with:

  • type: the bucket type.
  • key: the identifier of the bucket.
  • count: the amount of tokens you need. This is optional and the default is 1.

The result object has:

  • conformant (boolean): true if the requested amount is conformant to the limit.
  • remaining (int): the amount of remaining tokens in the bucket.
  • reset (int / unix timestamp): unix timestamp of the date when the bucket will be full again.
  • limit (int): the size of the bucket.

PUT

You can manually reset a fill a bucket using PUT:

limitdb.put({ type: 'ip', key: '54.21.23.12' }, err => {});

limitdb.put takes as argument an object with:

  • type: the bucket type.
  • key: the identifier of the bucket.
  • count: the amount of tokens you want to put in the bucket. This is optional and the default is the size of the bucket.

STATUS

limitdb.status({ type: 'ip', prefix: '54' }, (err, result) => {
  console.dir(result.items);
});

limitdb.status takes as argument an object with

  • type: the bucket type.
  • prefix: a prefix of buckets to search.

The result object has:

  • items: an array of buckets with:
  • key: the key of the bucket.
  • size: the size of the bucket.
  • reset: the reset time.

Author

Auth0

License

This project is licensed under the MIT license. See the LICENSE file for more info.