Skip to content

Commit

Permalink
feat: Rename whitelist / blacklist, require node 10 (#197)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: whitelist / blacklist are now allowlist and blocklist
BREAKING CHANGE: drops support for node 8
  • Loading branch information
scttcper authored Jul 20, 2020
1 parent 6f68cdb commit b0f006a
Show file tree
Hide file tree
Showing 10 changed files with 1,757 additions and 9,171 deletions.
9 changes: 4 additions & 5 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: 2.1
jobs:
test:
docker:
- image: circleci/node:12
- image: circleci/node:14
- image: circleci/redis
steps:
- checkout
Expand All @@ -24,12 +24,11 @@ jobs:
path: jest
release:
docker:
- image: circleci/node:12
- image: circleci/node:14
steps:
- checkout
- run: npm install
- run: npm run build
- run: npm run semantic-release
- run: npm ci
- run: npx semantic-release

workflows:
version: 2
Expand Down
8 changes: 8 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"root": true,
"env": {
"node": true
},
"extends": ["@ctrl/eslint-config"],
"rules": {}
}
21 changes: 0 additions & 21 deletions .eslintrc.js

This file was deleted.

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ console.log('listening on port http://localhost:4000');
- `max` max requests within `duration` [2500]
- `duration` of limit in milliseconds [3600000]
- `id` id to compare requests [ip]
- `whitelist` array of ids to whitelist
- `blacklist` array of ids to blacklist
- `allowlist` array of ids to allowlist
- `blocklist` array of ids to blocklist
- `prefix` redis key prefix ["limit"]

## Responses
Expand Down
10,162 changes: 1,591 additions & 8,571 deletions package-lock.json

Large diffs are not rendered by default.

34 changes: 15 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,35 +24,31 @@
"build": "tsc -p tsconfig.build.json",
"test": "jest --runInBand",
"test:watch": "jest --watch --runInBand",
"test:ci": "jest --ci --runInBand --reporters=default --reporters=jest-junit --coverage",
"semantic-release": "semantic-release"
"test:ci": "jest --ci --runInBand --reporters=default --reporters=jest-junit --coverage"
},
"dependencies": {
"debug": "^4.1.1",
"ms": "^2.1.2"
},
"devDependencies": {
"@ctrl/eslint-config": "1.0.3",
"@jest/globals": "26.1.0",
"@types/debug": "4.1.5",
"@types/jest": "25.2.3",
"@types/ioredis": "4.17.2",
"@types/koa": "2.11.3",
"@types/node": "13.13.12",
"@types/redis": "2.8.22",
"@types/ms": "0.7.31",
"@types/node": "14.0.23",
"@types/redis": "2.8.25",
"@types/supertest": "2.0.10",
"@typescript-eslint/eslint-plugin": "2.30.0",
"@typescript-eslint/parser": "2.30.0",
"eslint": "6.8.0",
"eslint-config-xo-space": "0.25.0",
"eslint-config-xo-typescript": "0.28.0",
"eslint-plugin-import": "2.22.0",
"ioredis": "4.16.3",
"jest": "25.5.3",
"jest-junit": "10.0.0",
"koa": "2.11.0",
"delay": "4.3.0",
"ioredis": "4.17.3",
"jest": "26.1.0",
"jest-junit": "11.0.1",
"koa": "2.13.0",
"redis": "3.0.2",
"semantic-release": "17.0.7",
"supertest": "4.0.2",
"ts-jest": "25.4.0",
"typescript": "3.8.3"
"ts-jest": "26.1.3",
"typescript": "3.9.7"
},
"jest": {
"preset": "ts-jest",
Expand All @@ -62,6 +58,6 @@
"branch": "master"
},
"engines": {
"node": ">=8"
"node": ">=10"
}
}
8 changes: 1 addition & 7 deletions renovate.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
{
"extends": [
"config:js-lib",
"schedule:monthly"
],
"automerge": "minor",
"semanticCommits": true,
"pinVersions": false
"extends": ["github>scttcper/renovate-config"]
}
27 changes: 13 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { RedisClient } from 'redis';

const debug = logger('koa-simple-ratelimit');

async function find(db: RedisClient, p: string): Promise<string> {
async function find(db: RedisClient, p: string): Promise<string | null> {
return new Promise((resolve, reject) => {
db.get(p, (err, reply) => {
if (err) {
Expand Down Expand Up @@ -46,19 +46,19 @@ export interface RatelimitOptions {
/**
* id to compare requests default: ip
*/
id?: (ctx: any) => any;
id?: (ctx: any) => string | false;
/**
* redis key prefix default: "limit"
*/
prefix?: string;
/**
* array of ids to whitelist
* array of ids to always allow
*/
whitelist?: string[];
allowlist?: string[];
/**
* array of ids to blacklist
* array of ids to always deny
*/
blacklist?: string[];
blocklist?: string[];
/**
* throw on rate limit exceeded default: false
*/
Expand Down Expand Up @@ -97,9 +97,9 @@ export function ratelimit(
duration: 3600000,
throw: false,
prefix: 'limit',
id: (ctx: any) => ctx.ip,
whitelist: [],
blacklist: [],
id: (ctx: any): string => ctx.ip,
allowlist: [],
blocklist: [],
headers: {
remaining: 'X-RateLimit-Remaining',
reset: 'X-RateLimit-Reset',
Expand All @@ -115,20 +115,19 @@ export function ratelimit(
total = 'X-RateLimit-Limit',
} = opts.headers || {};

return async function (ctx, next) {
// eslint-disable-next-line func-names
return async function rateLimitMiddleware(ctx, next) {
const id = opts.id(ctx);

if (id === false) {
return next();
}

// Whitelist
if (opts.whitelist && opts.whitelist.includes(id)) {
if (opts.allowlist?.includes(id)) {
return next();
}

// Blacklist
if (opts.blacklist && opts.blacklist.includes(id)) {
if (opts.blocklist?.includes(id)) {
return ctx.throw(403);
}

Expand Down
Loading

0 comments on commit b0f006a

Please sign in to comment.