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

smart-throttle

v1.0.5

Published

Module to perform functional load balancing and server side throttling

Downloads

4

Readme

smart-throttle

Functional load balancing and server side throttling for N

Usage

Multiple Server Side Throttle in single setup

app.js

import express from 'express'
import { MultiThrottle } from 'smart-throttle'

const app = express();


const ThrottleMiddleWare = new MultiThrottle('/absolutePath/multiThrottleConfig.json')

// console.log('Tokens')
// console.log(ThrottleMiddleWare.tokens.getTokens())

app.use(ThrottleMiddleWare.throttle);

app.get('/api/test/intercept', (req, res) => {
    res.json({ message: 'Welcome to Test Intercept' });
})

app.get('/api/v2/test/intercept', (req, res) => {
    res.json({ message: 'Welcome to Test Intercept' });
})

app.get('/api/v3/test/intercept', (req, res) => {
    res.json({ message: 'Welcome to Test Intercept' });
})

app.get('/api/test/welcome',(req,res)=>{
    res.json({message: 'Welcome to App'})
})

const PORT = 3000;

app.listen(PORT, () => {
    console.log(`Server is running on port ${PORT}`);
});

multiThrottleConfig.json

[
    {
        "features": [
            {
                "name": "NEXT",
                "weight": 50
            },
            {
                "name": "RETRY",
                "weight": 50,
                "response": {
                    "status": 200,
                    "body": {
                        "message": "Try Again after some time"
                    }
                }
            }
        ],
        "pathList": [
            "/api/test/intercept"
        ]
    },

    {
        "features": [
            {
                "name": "NEXT",
                "weight": 0
            },
            {
                "name": "RETRY",
                "weight": 100,
                "response": {
                    "status": 200,
                    "body": {
                        "message": "Try Again V2"
                    }
                }
            }
        ],
        "pathList": [
            "/api/v2/test/intercept"
        ]
    },

    {
        "features": [
            {
                "name": "NEXT",
                "weight": 0
            },
            {
                "name": "RETRY",
                "weight": 100,
                "response": {
                    "status": 200,
                    "body": {
                        "message": "Try Again V3"
                    }
                }
            }
        ],
        "pathList": [
            "/api/v3/test/intercept"
        ]
    }
]

Single Server Side Throttle

app.js

import express from 'express'

// Import ServerSideThrottle
import { ServerSideThrottle } from 'smart-throttle'

const app = express();

// Setup Throttle Middleware and pass to express
// NOTE : Pre-requisite to setup configuration file in path : /absolutePath/throttleConfig.json
const ThrottleMiddleWare = new ServerSideThrottle('/absolutePath/throttleConfig.json')
app.use(ThrottleMiddleWare.throttle)

app.get('/api/test/intercept', (req, res) => {
    res.json({ message: 'Welcome to Test Intercept' });
})

const PORT = 3001;

app.listen(PORT, () => {
    console.log(`Server is running on port ${PORT}`);
});

throttleConfig.json

{
    "features":[
        {
            "name":"NEXT",
            "weight":50
        },
        {
            "name":"RETRY",
            "weight":50,
            "response":{
                "status":200,
                "body":{
                    "message":"Try Again after some time"
                }
            }
        }
    ],
    "pathList":[
        "/api/test/intercept"
    ]
}

Guide

| Name | Level | Location | Desc | Sample Usage | | ----------- | ----------- | ----------- | ----------- | ----------- | | MultiThrottle | Class | Module Export | Class Object with throttle method to be passed to express Multiple Throttle Configurations in single file | const ThrottleMiddleWare = new MultiThrottle('configPath.json') | | ServerSideThrottle | Class | Module Export | Class Object with throttle method to be passed to express Single throttle configuration per file. Need to create a new instance for each throttle configuration | const ThrottleMiddleWare = new MultiThrottle('configPath.json') | | throttle | method | MultiThrottle/ServerSideThrottle | Method consumed by express to perform throttling | app.use(ThrottleMiddleWare.throttle); | | features | field | throttleConfig.json | Array of features | Refer Config | | name | field | features | Name of Feature | Refer Config | | weight | field | features | Weightage of feature Keep total to 100 to reduce complexity | Refer Config | | response | field | features | Response Details | Refer Config | | status | field | response | HTTP status code | Refer Config | | body | field | response | Response JSON/body | Refer Config | | pathList | field | throttleConfig.json | Array of paths where throttle is enabled | Refer Config |

Installation

This is a Node.js module available through the npm registry.

Before installing, download and install Node.js. Node.js 0.10 or higher is required.

If this is a brand new project, make sure to create a package.json first with the npm init command.

Installation is done using the npm install command:

$ npm install smart-throttle

Follow our installing guide for more information.

Features

  • Server Side Throttling
  • Functional Load Balancing