rjweb-server-ratelimit
TypeScript icon, indicating that this package has built-in type declarations

2.4.0 • Public • Published

Welcome to rjweb-server-ratelimit 👋

A rjweb-server Middleware to add rate limits

Install

npm i rjweb-server-ratelimit

or

yarn add rjweb-server-ratelimit

or

pnpm add rjweb-server-ratelimit

Usage

Setting a RateLimit on a Path

const { Server } = require('rjweb-server')
const { rateLimit } = require('rjweb-server-ratelimit')

const server = new Server({
  port: 8000
}, [
  rateLimit.config({
    http: {
      message: 'Over the limit!!',
      rules: [
        {
          path: '/api',
          ignore: '/api/version',
          timeWindow: 30000,
          maxHits: 5
        }
      ]
    }
  })
])

server.path('/api', (path) => path
  .http('GET', '/', (http) => http
    .onRequest((ctr) => {
      const limit = ctr.getRateLimits()[0]

      ctr.print(`You have used ${limit.hits} / ${limit.max} of your limits, they will be reset in ${limit.resetIn}ms`)
    })
  )
)

server.start().then((res) => {
  console.log(`Server started on port ${res.port}`)
})

Using Regex

const { Server } = require('rjweb-server')
const { rateLimit } = require('rjweb-server-ratelimit')

const server = new Server({
  port: 8000
}, [
  rateLimit.config({
    http: {
      message: 'Over the limit!!',
      rules: [
        {
          path: /api|auth/g,
          timeWindow: 30000,
          maxHits: 5
        }
      ]
    }
  })
])

server.path('/api', (path) => path
  .http('GET', '/', (http) => http
    .onRequest((ctr) => {
      const limit = ctr.getRateLimits()[0]

      ctr.print(`You have used ${limit.hits} / ${limit.max} of your limits, they will be reset in ${limit.resetIn}ms`)
    })
  )
)

server.start().then((res) => {
  console.log(`Server started on port ${res.port}`)
})

Match with Arrays

Now if any of the paths in that array match it will apply the ratelimit

const { Server } = require('rjweb-server')
const { rateLimit } = require('rjweb-server-ratelimit')

const server = new Server({
  port: 8000
}, [
  rateLimit.config({
    http: {
      message: 'Over the limit!!',
      rules: [
        {
          path: [/api|auth/g, '/stats'],
          timeWindow: 30000,
          maxHits: 5
        }
      ]
    }
  })
])

server.path('/api', (path) => path
  .http('GET', '/', (http) => http
    .onRequest((ctr) => {
      const limit = ctr.getRateLimits()[0]

      ctr.print(`You have used ${limit.hits} / ${limit.max} of your limits, they will be reset in ${limit.resetIn}ms`)
    })
  )
)

server.start().then((res) => {
  console.log(`Server started on port ${res.port}`)
})

Author

👤 0x4096

Show your support

Give a Star if this project helped you!

📝 License

Copyright © 2023 0x4096.
This project is MIT licensed.

Package Sidebar

Install

npm i rjweb-server-ratelimit

Weekly Downloads

1

Version

2.4.0

License

MIT

Unpacked Size

34.3 kB

Total Files

19

Last publish

Collaborators

  • 0x4096