1.1.2 • Published 6 years ago

node-ratelimits v1.1.2

Weekly downloads
-
License
ISC
Repository
gitlab
Last release
6 years ago

Rate Limits

A NodeJS library to rate limit anything. Documentation can be found at https://docs.sdfx.ga/ratelimits

Basic Usage

const { Application, Ratelimit } = require('node-ratelimits'); // Import the needed classes

const application = new Application(); // Create a new application
const limit = new Ratelimit({ amount: 5, time: 15000 }); // Create a new ratelimit, amount is how many will be allowed in time ms

application.register('limit', limit); // Register the ratelimit to the application as "limit"

await application.limit('limit', 'id'); // Returns either false or a time in ms, false for no ratelimit

Advanced Usage

Editing Ratelimits

const ratelimit = application.get('limit'); // You can either get the ratelimit from the application, or have the ratelimit class from before

ratelimit.edit({ amount: 5, time: 10000 }); // Update the amount and time, either one can be left out

Deregistering Limits

application.deregister('limit'); // Removes the limit from the application

Clearing Ratelimits

application.clear('id'); // Clears all ratelimits for the id "id"

application.clear('id', 'limit'); // Clears the "limit" ratelimit for the id "id"

Custom Stores

By default, an in-memory object store will be used, but by following this, you'll be able to implement another custom store, such as a database.

const { Application, Ratelimit, BaseStore } = require('node-ratelimits'); // Import the needed classes - notice the extra BaseStore class

class Store extends BaseStore { // Create a new class for the store, it is important that you extend the BaseStore class
    async get(id) { // Define the get method, the id will be a string
        const data = await getDataById(id); // Get data by id from any source
        if(!data) return null; // Return null if there is no entry

        return data; // Return the data if it exists
    }

    async set(id, data) { // Define the set method, id and data will both be strings
        try {
            await setDataById(id, data); // Set the data by id in any destination

            return true; // Return true to indicate that the data has been set successfully
        }
        catch(e) {
            return false; // Return false to indicate that something went wrong while setting the data
        }
    }

    async remove(id) { // Define the remove method, id will be a string
        try {
            await removeDataById(id); // Remove the data from any source

            return true; // Return true to indicate success in deletion
        }
        catch(e) {
            return false; // Return false to indicate failure in deletion
        }
    }
}

const application = new Application(new Store()); // Create a new application - note that this time the custom store was passed into the constructor

// Continue with the rest of the library as usual
1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago