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

express-attack

v0.5.3

Published

Express middleware for blocking and throttling inspired by rack-attack

Downloads

47

Readme

express-attack

Express middleware for blocking and throttling inspired by rack-attack and Node Redis GCRA Library

Installation

$ yarn install express-attack

Usage

const express = require('express')
const expressAttack = require('express-attack')
const requestIp = require('request-ip')
const app = express()

function localReq(req) {
  const clientIp = requestIp.getClientIp(req)
  return clientIp === '127.0.0.1' || clientIp === '::1'
}

const BAD_BOTS = ['bad-bot', 'evil-bot']
function badBot(req) {
  const userAgent = req.headers['user-agent'] || 'unknown'
  const found = BAD_BOTS.find(function(badBot) {
    return userAgent.indexOf(badBot) >= 0
  })
  return !!found
}

app.use(
  expressAttack({
    safelist: [localReq],
    blocklist: [badBot]
  })
)

app.get('/foo', function(req, res, next) {
  res.json({ msg: 'bar' })
})

app.listen(3000, function() {
  console.log('The server is running at http://localhost:3000')
})

Throttling

GCRA is used for throttling. Default store is memoryStore, you can create your own store.

// If I want to allow 2 request per second and at most 6 requests simultaneously, then:
// emissionInterval = period / rate => 1000 milliseconds / 2 reqs => 500
// burst => 6

function throttleByIp(req) {
  const clientIp = requestIp.getClientIp(req)

  return {
    key: clientIp
    burst 6,
    emissionInterval: 500
  }
}

app.use(
  expressAttack({
    throttles: [throttleByIp]
  })
)

Create your store

Create an object with below methods: get(key): retrun value according to key, if key doesn't exsits return undefined. set(key, timestamp, period): assign timestamp to key, and expire the key after period milliseconds.

function dummyStore() {
  const store = {}
  const get = async function(key) {
    return store[key]
  }
  const set = async function(key, timestamp, period) {
    store[key] = timestamp
  }

  return {
    get,
    set
  }
}


app.use(
  expressAttack({
    throttles: [throttleByIp],
    store: dummyStore()
  })
)

Customizing responses

// do what ever you want with response.
const blocklistedResponse = (req, res) => {
  return res.status(503).send('Service Unavailable')
}

const throttledResponse = (req, res) => {
  return res.status(503).send('Service Unavailable')
}

app.use(
  expressAttack({
    blocklistedResponse
    throttledResponse
  })
)

Options

  • safelist: array of safe request functions, if one of the fucntion return true, ths request is allowed to go.
  • blocklist: array of block request functions, if one of the function reutrn true, the request is blocked.
  • throttles: array of throttle functions, check Throttling for detail.
  • safelistedResponse(req, res, next): custom your response when request is mark as safelisted.
  • blocklistedResponse(req, res): custom your response when request is mark as blocklisted.
  • throttledResponse(req, res): custom your response when request is mark as throttled.
  • normalResponse(req, res, next): custom your response when request not in above situation.
  • errorResponse(req, res, next, error): custom your response when exception raise during check request phase.
  • store: check Custom your store for detail.

License

MIT