Skip to content

Commit

Permalink
feat: translate rate limiting with dayjs
Browse files Browse the repository at this point in the history
  • Loading branch information
titanism committed Jul 2, 2022
1 parent 7e93ebc commit ac1250a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 44 deletions.
49 changes: 10 additions & 39 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ const etag = require('koa-etag');
const json = require('koa-json');
const koa404Handler = require('koa-404-handler');
const koaConnect = require('koa-connect');
const ms = require('ms');
const multimatch = require('multimatch');
const ratelimit = require('@ladjs/koa-simple-ratelimit');
const removeTrailingSlashes = require('koa-no-trailing-slash');
const requestId = require('express-request-id');
Expand All @@ -30,14 +28,11 @@ const responseTime = require('response-time');
const sharedConfig = require('@ladjs/shared-config');
const { boolean } = require('boolean');

const RATE_LIMIT_EXCEEDED = 'Rate limit exceeded, retry in %s.';

class API {
// eslint-disable-next-line complexity
constructor(config, Users) {
this.config = {
...sharedConfig('API'),
rateLimitIgnoredGlobs: [],
...config
};

Expand Down Expand Up @@ -106,42 +101,21 @@ class API {
app.use(this.logger.middleware);

// Allow before hooks to get setup
if (_.isFunction(this.config.hookBeforeSetup)) {
if (_.isFunction(this.config.hookBeforeSetup))
this.config.hookBeforeSetup(app);
}

// Basic auth
if (this.config.auth) {
app.use(auth(this.config.auth));
}
if (this.config.auth) app.use(auth(this.config.auth));

// Rate limiting
if (this.client && this.config.rateLimit) {
app.use((ctx, next) => {
// Check against ignored/whitelisted paths
if (
Array.isArray(this.config.rateLimitIgnoredGlobs) &&
this.config.rateLimitIgnoredGlobs.length > 0
) {
const match = multimatch(ctx.path, this.config.rateLimitIgnoredGlobs);
if (Array.isArray(match) && match.length > 0) {
return next();
}
}

return ratelimit({
if (this.client && this.config.rateLimit)
app.use(
ratelimit({
...this.config.rateLimit,
db: this.client,
logger: this.logger,
errorMessage(exp) {
const fn =
typeof ctx.request.t === 'function' ? ctx.request.t : util.format;
// NOTE: ms does not support i18n localization
return fn(RATE_LIMIT_EXCEEDED, ms(exp, { long: true }));
}
})(ctx, next);
});
}
logger: this.logger
})
);

// Remove trailing slashes
app.use(removeTrailingSlashes());
Expand Down Expand Up @@ -172,9 +146,7 @@ class API {
app.use(json());

// Passport
if (this.passport) {
app.use(this.passport.initialize());
}
if (this.passport) app.use(this.passport.initialize());

// Store the user's last ip address in the background
if (this.config.storeIPAddress) {
Expand All @@ -189,9 +161,8 @@ class API {
app.use(koa404Handler);

// Allow before hooks to get setup
if (_.isFunction(this.config.hookBeforeRoutes)) {
if (_.isFunction(this.config.hookBeforeRoutes))
this.config.hookBeforeRoutes(app);
}

// Mount the app's defined and nested routes
if (this.config.routes) {
Expand Down
8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
"dependencies": {
"@koa/router": "^10.1.1",
"@ladjs/i18n": "^7.2.6",
"@ladjs/koa-simple-ratelimit": "^3.0.0",
"@ladjs/koa-simple-ratelimit": "^4.0.1",
"@ladjs/passport": "^5.0.2",
"@ladjs/redis": "^1.0.7",
"@ladjs/shared-config": "^7.0.3",
"@ladjs/shared-config": "^8.0.0",
"@ladjs/store-ip-address": "^0.0.7",
"boolean": "^3.2.0",
"cabin": "^9.1.2",
Expand All @@ -35,8 +35,6 @@
"koa-json": "^2.0.2",
"koa-no-trailing-slash": "^2.1.0",
"lodash": "^4.17.21",
"ms": "^2.1.3",
"multimatch": "5",
"request-received": "^0.0.3",
"response-time": "^2.3.2"
},
Expand All @@ -45,7 +43,7 @@
"@commitlint/config-conventional": "^17.0.3",
"ava": "^4.3.0",
"cross-env": "^7.0.3",
"eslint": "^8.18.0",
"eslint": "^8.19.0",
"eslint-config-xo-lass": "^2.0.1",
"fixpack": "^4.0.0",
"husky": "^8.0.1",
Expand Down

0 comments on commit ac1250a

Please sign in to comment.