# koa-rate-limits

> koa rate limit middleware base on @aftership/rate-limiter

Latest version **1.0.1** (published 2020-10-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install koa-rate-limits
pnpm add koa-rate-limits
yarn add koa-rate-limits
bun add koa-rate-limits
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2020-10-09 |
| First published | 2020-10-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 23.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | dingyuanwu |
| Maintainers | dingyuanwu |
| Keywords | koa, middleware, rate-limit |

## Links

- npm: https://www.npmjs.com/package/koa-rate-limits
- Repository: https://github.com/love477/koa-rate-limit
- Homepage: https://github.com/love477/koa-rate-limit#readme
- Issues: https://github.com/love477/koa-rate-limit/issues
- npm.io page: https://npm.io/package/koa-rate-limits

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 1.0.1 (latest) — 2020-10-09

## README

# 限流中间件
koa-rate-limit是koa的限流中间件。核心是基于Redis实现，所以在使用的时候确保已经安装Redis。  
提供的能力： 
1. 基本的限流实现，基于固定窗口的限流算法
2. 支持API级的限流，遵循路由匹配原则的限流规则匹配

开发中的能力：
1. 令牌桶限流算法的实现
2. 基于用户IP的限流
3. 限流配置的动态配置

## 使用说明
### 安装
```sh
# npm
npm i koa-rate-limit

# yarn 
yarn add koa-rate-limit
```
### 使用示例
```ts
import * as Koa from 'koa';
import * as Redis from 'ioredis';
import * as Router from 'koa-router';
import { WindowRateLimiter IConfig } from 'koa-rate-limit';

const app = new Koa();

const limitConfig: IConfig = {
    default: {
        limit: 10,
        duration: 10,
    },
    _hello: {
        limit: 3,
        duration: 10,
    },
};
WindowRateLimiter.init(new Redis(), limiterConfig, 'prefix');

app.use(WindowRateLimiter.limiter);

const router = new Router();
router.get('/hello', (ctx, next) => {
  ctx.body = 'hello world';
});

app.use(router.routes());

app.use((ctx, next) => {
  ctx.body = ctx.path;
});

app.listen(3000);
```

### 使用示例说明
关于Redis和ioredis的使用，请参考下面的文档:  
[Redis使用指南](http://www.redis.cn/)  
[ioredis使用指南](https://github.com/luin/ioredis#readme)  

关于koa中间件的使用，请参考下面的文档：  
[koa中间件机制详解](https://cnodejs.org/topic/58fd8ec7523b9d0956dad945)

**关于配置文件的说明**
中间件使用的配置文件通常是下面的格式：
```ts
const limitConfig: IConfig = {
    default: {
        limit: 10,
        duration: 10,
    },
    _hello: {
        limit: 3,
        duration: 10,
    },
};
```
本中间件是对接口进行限流，使用接口的path作为限流的key，若提供了prefix，则key为：${prefix}${key}：
```
this.key = prefix ? `${prefix}${key}` : key;
```
这里的key的生成方式如下：
```
// 替换path中的'/'为'_'
const key = path.replace(/\//g, '_');
```
限流的规则如下：
1. 根据接口的path计算出key
2. 查找与key最匹配的限流配置，遵循最佳匹配原则
3. 若没有与key最匹配的限流配置，则检查默认配置

## 支持功能
功能：
1. 支持接口级别的限流
2. 支持固定窗口限流算法和令牌桶限流算法

## 版本记录
### 1.0.0  
1. 基于固定窗口限流算法实现KOA中间件  
2. 支持接口级别的限流

---
_Source: https://npm.io/package/koa-rate-limits · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
