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

@iroomit/rate-limit-mongodb

v1.0.1

Published

MongoDB store for the Node.js express-rate-limit package.

Downloads

10

Readme

Rate Limit MongoDB Store

Npm version

MongoDB store for the express-rate-limit Node.js middleware package, used at iROOMit.

This package was heavily inspired by 2do2go/rate-limit-mongo, but is not a direct fork. Development on 2do2go/rate-limit-mongo has been stalled for several years, and also relies on some dependencies that are unnecessary in modern Node.js.

This implementation is also written in TypeScript for improved IntelliSense in VSCode and improved compile-time error detection.

Compatibility

This package is tested as compatible for MongoDB driver version >= 5, and express-rate-limit version >= 6.

Although untested, it may work with older versions of the MongoDB driver.

Version 6 and up of the express-rate-limit package is a hard requirement.

Install

npm install @iroomit/rate-limit-mongodb

or

yarn add @iroomit/rate-limit-mongodb

Usage

import RateLimit from 'express-rate-limit';
import MongoDBStore from '@iroomit/rate-limit-mongodb';

const limiter = new RateLimit({
  store: new MongoDBStore({
    uri: 'mongodb://127.0.0.1:27017/test_db',
    user: 'mongouser',
    password: 'mongopassword'
  }),
  max: 100,
  windowMs: 15 * 60 * 1000
});

//  apply to all requests
app.use(limiter);

Configuration

The MongoDBStore class can be configured using the following options, depending on whether you are passing a MongoDB URI to establish the connection with this library or directly passing a MongoDB collection.

Common Options

These options apply regardless of whether you are passing a MongoDB URI or a MongoDB collection directly:

  • prefix (optional):

    • A string that will be prefixed to all keys stored in the MongoDB collection.
    • Default: "mongodb_rl_".
  • resetExpireDateOnChange (optional):

    • A boolean that, if set to true, will reset the expiration date of a key each time it is incremented or decremented.
    • Default: false.
  • createTtlIndex (optional):

    • A boolean that, if set to true, will automatically create a TTL index on the expirationDate field in the MongoDB collection.
    • This is useful to automatically remove expired rate-limit records.
    • Default: true.

MongoDB Connection Options

These options are used if you are passing a MongoDB URI to connect to the database yourself. The collection field should not be provided in this case.

  • uri (required):

    • A string representing the MongoDB connection URI.
    • Example: "mongodb://localhost:27017/rateLimitDB".
  • collectionName (optional):

    • The name of the MongoDB collection where rate-limit records will be stored.
    • Default: "expressRateRecords".
  • connectionOptions (optional):

    • An object containing additional options to be passed to the MongoClient constructor.
    • This allows for advanced configuration of the MongoDB connection, such as SSL settings, connection pool size, etc.
  • user (optional):

    • A string representing the MongoDB username for authentication.
  • password (optional):

    • A string representing the MongoDB password for authentication.
  • authSource (optional):

    • A string representing the database to authenticate against.
    • Default: The database name extracted from the URI.

MongoDB Collection Option

These options are used if you are directly passing a MongoDB collection object to the MongoDBStore constructor. The uri field should not be provided in this case.

  • collection (required):
    • A MongoDB Collection instance where rate-limit records will be stored.

Testing

The test suite can be run with Docker, by running:

docker-compose -f docker-compose.test.yml up

You may be able to run the test suite outside of Docker, however the mongodb-memory-server package used in the test suite only runs on select operating systems. This is probably fine if you are using Windows or macOS, but may cause problems on Linux if your distribution is not supported.

By running the test suite in Docker, we ensure that all required dependencies are installed for the mongodb-memory-server package.