Skip to content

Commit 394e94c

Browse files
authored
feat(middleware): add rateLimit feature. (#12)
* feat(middleware): add rateLimit feature. * "test": "NODE_ENV='test' TZ='UTC' jest --coverage --runInBand --forceExit",
1 parent eeb9f9d commit 394e94c

5 files changed

Lines changed: 12 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
> Unreleased
55
66
* refactor: drop axios & introduce dataloader
7+
* feat(middleware): add rateLimit feature.
78

89
## [v2.0.1]
910
> Aug 18, 2017

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
- Use GraphQL.
2121
- Dataloader for per-request memory cache.
22+
- Rate-limiting 1 requests per sec.
2223

2324
## How To Use
2425

@@ -109,6 +110,7 @@ Options:
109110

110111
- [Micro](https://github.com/zeit/micro): Asynchronous HTTP microservices.
111112
- [Micro-router](https://github.com/pedronauck/micro-router): A tiny and functional router for Zeit's Micro.
113+
- [Micro-ratelimit](https://github.com/dotcypress/micro-ratelimit): Rate-limiting middleware for micro.
112114
- [Graphql-tools](https://github.com/apollographql/graphql-tools): 🔧 Build and mock your GraphQL.js schema using the schema language.
113115
- [Graphql-server-micro](https://github.com/apollographql/apollo-server/tree/master/packages/graphql-server-micro): 🌍 GraphQL server
114116
- [Dataloader](https://github.com/facebook/dataloader): DataLoader is a generic utility to be used as part of your application's data fetching layer to provide a consistent API over various backends and reduce requests to those backends via batching and caching.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"pkg": "rm -rf pkg && pkg . --out-path pkg",
2626
"eslint": "eslint ./",
2727
"format": "prettier --trailing-comma all --single-quote --write '{src,bin}/**/*.js'",
28-
"test": "NODE_ENV='test' TZ='UTC' jest --coverage --runInBand",
28+
"test": "NODE_ENV='test' TZ='UTC' jest --coverage --runInBand --forceExit",
2929
"test:watch": "npm run test -- --watch"
3030
},
3131
"now": {
@@ -55,6 +55,7 @@
5555
"micro": "^8.0.2",
5656
"micro-compress": "^1.0.0",
5757
"micro-cors": "^0.0.4",
58+
"micro-ratelimit": "^0.2.0",
5859
"microrouter": "^2.2.2",
5960
"query-string": "^5.0.0",
6061
"ramda": "^0.24.1",

src/utils/middleware.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const R = require('ramda');
22
const compress = require('micro-compress');
33
const cors = require('micro-cors');
4+
const rateLimit = require('micro-ratelimit');
45

56
const ORIGIN = process.env.ORIGIN;
67

@@ -11,6 +12,8 @@ const middleware = R.compose(
1112
origin: ORIGIN || '*',
1213
}),
1314
compress,
15+
// TODO: jest problem --forceExit
16+
R.curry(rateLimit)({ window: 1000, limit: 1 }),
1417
);
1518

1619
module.exports = middleware;

yarn.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2536,6 +2536,10 @@ micro-dev@^1.2.1:
25362536
pretty-error "2.1.1"
25372537
string-length "2.0.0"
25382538

2539+
micro-ratelimit@^0.2.0:
2540+
version "0.2.0"
2541+
resolved "https://registry.yarnpkg.com/micro-ratelimit/-/micro-ratelimit-0.2.0.tgz#68585fa8baed3b7dc696196889a0a7dfa6254bdf"
2542+
25392543
micro@8.0.2:
25402544
version "8.0.2"
25412545
resolved "https://registry.yarnpkg.com/micro/-/micro-8.0.2.tgz#e49127185c8d1a61146f0555603bef2a7fc47b03"

0 commit comments

Comments
 (0)