Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

@npm/spife-ratelimit

Package Overview
Dependencies
Maintainers
30
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@npm/spife-ratelimit

Middleware that adds [Token Bucket]-style ratelimiting to [Spife].

latest
Source
npmnpm
Version
2.0.1
Version published
Weekly downloads
7
Maintainers
30
Weekly downloads
 
Created
Source

spife-ratelimiter

Middleware that adds Token Bucket-style ratelimiting to Spife.

// settings.js:
module.exports.MIDDLEWARE = {
  ['@npm/spife-ratelimiting', {
    redisUrl: 'redis://localhost:6379', // let it connect to redis
    redisClient,                        // (or give it a client)

    costHeader: 'npm-cost',             // the "cost" header to use
                                        // for determining how many
                                        // tokens a request took

    clock: Date.now,                    // if you would like time to
                                        // move to the beat of a
                                        // different drum

    getBucket (spifeRequest) {          // given a request, return
                                        // an object describing its
                                        // token bucket
      return {
        capacity: 100,                  // how many tokens does
                                        // the bucket hold?

        interval: 60,                   // in minutes, how long
                                        // does the bucket take to
                                        // fill up?

        key: String                     // what should the bucket be
                                        // named?
      }
    }
  }]
}

// views.js:
const reply = require('@npm/spife/reply')
const cost = require('@npm/spife-ratelimiter/decorator')

module.exports = {
  // a view can declare how much it costs up-front:
  index: cost(index, 5),
  other
}

function index (req, context) {
  return 'I cost five tokens!'
}

function other (req, context) {
  return reply.header(
    'I cost a hundred tokens sometimes',
    'npm-cost',
    Math.random() > 0.5 ? 100 : 0
  )
}

This is implemented in redis by keeping two keys per bucket: one that tracks the current number of tokens, and one that tracks the last update time. Both keys are expired at interval, and updated atomically using a Lua script.

FAQs

Package last updated on 12 Mar 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts