Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

header-order-rate-limit

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

header-order-rate-limit

Rate limits http requests based on header order

  • 1.3.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

header-order-rate-limit

Rate limits http requests based on header order

Installation

npm install header-order-rate-limit

Usage

const express = require("express");

const HeaderOrderRateLimiter = require("header-order-rate-limit");

const limiter = new HeaderOrderRateLimiter({
  perLastMilliseconds: 10000,
});

const port = 3000;
const app = express();

app.use(({ headers }, res, next) => {
  delete headers["if-none-match"];
  delete headers["cache-control"];
  delete headers["pragma"];

  if (limiter.check(headers)) return res.end("blocked!");

  limiter.track(headers);

  return next();
});

app.get("/", (_, res) => {
  res.send("passed!");
});

app.listen(port);

API

constructor(options)

Creates a new HeaderOrderRateLimiter instance with the specified options.

options: (optional) An object containing the rate limiting options: blockWhenAttemptsReach: Number of attempts allowed within the specified time window. Default: 3.

perLastMilliseconds: Time window in milliseconds to track attempts. Default: 3000.

useBackOffFactor: Expands the track window as new requests come so it becomes harder to make periodic fetches to avoid detection. Default: true.

calculateBackOffDeltaMilliseconds: (blockWhenAttemptsReach: number, rate: number[]) => void: Overrides back-off function. Default:

one millisecond * (count of order requests - 3)

First argument is max amount of requests before block.

Second argument is consecutive history of request unix times in milliseconds from old to new.

track(headers, { dateNow })

Tracks the timestamp of a request based on the provided headers.

headers: An object representing the headers of the request.

dateNow: (optional) A timestamp (EpochTimeStamp) representing the current time. If not provided, the current system time will be used. Returns the timestamp when the request was tracked.

check(headers, { dateNow })

Checks if a request has hit the rate limit based on the provided headers.

headers: An object representing the headers of the request.

dateNow: (optional) A timestamp (EpochTimeStamp) representing the current time. If not provided, the current system time will be used. Returns true if the request has hit the rate limit, otherwise false.

Test

npm test

Keywords

FAQs

Package last updated on 28 Nov 2023

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc