Skip to content

Commit

Permalink
(automated) new prettier formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
nfriedly committed Apr 6, 2020
1 parent 6fea42e commit 5552275
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 118 deletions.
14 changes: 7 additions & 7 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@ module.exports = {
extends: ["eslint:recommended", "plugin:prettier/recommended"],
env: {
node: true,
es6: true
es6: true,
},
rules: {
"no-var": "error",
"prefer-const": "error",
strict: "error"
strict: "error",
},
overrides: [
{
files: ["*-test.js"],
env: {
mocha: true
mocha: true,
},
parserOptions: { ecmaVersion: 2018 },
rules: {
"prefer-arrow-callback": "error"
}
}
]
"prefer-arrow-callback": "error",
},
},
],
};
20 changes: 10 additions & 10 deletions lib/express-rate-limit.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ function RateLimit(options) {
skipFailedRequests: false, // Do not count failed requests (status >= 400)
skipSuccessfulRequests: false, // Do not count successful requests (status < 400)
// allows to create custom keys (by default user IP is used)
keyGenerator: function(req /*, res*/) {
keyGenerator: function (req /*, res*/) {
return req.ip;
},
skip: function(/*req, res*/) {
skip: function (/*req, res*/) {
return false;
},
handler: function(req, res /*, next*/) {
handler: function (req, res /*, next*/) {
res.status(options.statusCode).send(options.message);
},
onLimitReached: function(/*req, res, optionsUsed*/) {}
onLimitReached: function (/*req, res, optionsUsed*/) {},
},
options
);
Expand All @@ -40,7 +40,7 @@ function RateLimit(options) {
throw new Error("The store is not valid.");
}

["global", "delayMs", "delayAfter"].forEach(key => {
["global", "delayMs", "delayAfter"].forEach((key) => {
// note: this doesn't trigger if delayMs or delayAfter are set to 0, because that essentially disables them
if (options[key]) {
throw new Error(
Expand All @@ -56,7 +56,7 @@ function RateLimit(options) {

const key = options.keyGenerator(req, res);

options.store.incr(key, function(err, current, resetTime) {
options.store.incr(key, function (err, current, resetTime) {
if (err) {
return next(err);
}
Expand All @@ -66,12 +66,12 @@ function RateLimit(options) {

Promise.resolve(maxResult)
.catch(next)
.then(max => {
.then((max) => {
req.rateLimit = {
limit: max,
current: current,
remaining: Math.max(max - current, 0),
resetTime: resetTime
resetTime: resetTime,
};

if (options.headers && !res.headersSent) {
Expand Down Expand Up @@ -107,7 +107,7 @@ function RateLimit(options) {
};

if (options.skipFailedRequests) {
res.on("finish", function() {
res.on("finish", function () {
if (res.statusCode >= 400) {
decrementKey();
}
Expand All @@ -123,7 +123,7 @@ function RateLimit(options) {
}

if (options.skipSuccessfulRequests) {
res.on("finish", function() {
res.on("finish", function () {
if (res.statusCode < 400) {
options.store.decrement(key);
}
Expand Down
8 changes: 4 additions & 4 deletions lib/memory-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function MemoryStore(windowMs) {
let hits = {};
let resetTime = calculateNextResetTime(windowMs);

this.incr = function(key, cb) {
this.incr = function (key, cb) {
if (hits[key]) {
hits[key]++;
} else {
Expand All @@ -20,20 +20,20 @@ function MemoryStore(windowMs) {
cb(null, hits[key], resetTime);
};

this.decrement = function(key) {
this.decrement = function (key) {
if (hits[key]) {
hits[key]--;
}
};

// export an API to allow hits all IPs to be reset
this.resetAll = function() {
this.resetAll = function () {
hits = {};
resetTime = calculateNextResetTime(windowMs);
};

// export an API to allow hits from one IP to be reset
this.resetKey = function(key) {
this.resetKey = function (key) {
delete hits[key];
};

Expand Down
Loading

0 comments on commit 5552275

Please sign in to comment.