express-slow-down
Advanced tools
Comparing version 1.5.0 to 1.6.0
"use strict"; | ||
const defaults = require("defaults"); | ||
const MemoryStore = require("./memory-store"); | ||
function SlowDown(options) { | ||
options = defaults(options, { | ||
function SlowDown(opts) { | ||
const options = { | ||
// window, delay, and max apply per-key unless global is set to true | ||
@@ -23,3 +22,4 @@ windowMs: 60 * 1000, // milliseconds - how long to keep records of requests in memory | ||
onLimitReached: function (/*req, res, optionsUsed*/) {}, | ||
}); | ||
...opts, | ||
}; | ||
@@ -29,5 +29,6 @@ // store to use for persisting rate limit data | ||
// ensure that the store has the incr method | ||
// ensure that the store has the increment method | ||
if ( | ||
typeof options.store.incr !== "function" || | ||
(typeof options.store.incr !== "function" && | ||
typeof options.store.increment !== "function") || | ||
typeof options.store.resetKey !== "function" || | ||
@@ -47,7 +48,3 @@ (options.skipFailedRequests && | ||
options.store.incr(key, function (err, current, resetTime) { | ||
if (err) { | ||
return next(err); | ||
} | ||
const handleIncrement = (current, resetTime) => { | ||
let delay = 0; | ||
@@ -141,3 +138,20 @@ | ||
next(); | ||
}); | ||
}; | ||
if (typeof options.store.increment !== "undefined") { | ||
return options.store | ||
.increment(key) | ||
.then(({ totalHits, resetTime }) => | ||
handleIncrement(totalHits, resetTime) | ||
) | ||
.catch((err) => next(err)); | ||
} else { | ||
options.store.incr(key, (err, current, resetTime) => { | ||
if (err) { | ||
next(err); | ||
} else { | ||
handleIncrement(current, resetTime); | ||
} | ||
}); | ||
} | ||
} | ||
@@ -144,0 +158,0 @@ |
{ | ||
"name": "express-slow-down", | ||
"version": "1.5.0", | ||
"version": "1.6.0", | ||
"description": "Basic IP rate-limiting middleware for Express that slows down responses rather than blocking the user.", | ||
@@ -34,5 +34,2 @@ "homepage": "https://github.com/nfriedly/express-slow-down", | ||
], | ||
"dependencies": { | ||
"defaults": "^1.0.3" | ||
}, | ||
"devDependencies": { | ||
@@ -39,0 +36,0 @@ "body-parser": "^1.20.0", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13106
0
177
- Removeddefaults@^1.0.3
- Removedclone@1.0.4(transitive)
- Removeddefaults@1.0.4(transitive)