mirror of
https://github.com/jambonz/jambonz-api-server.git
synced 2026-01-25 02:08:24 +00:00
* add delay middleware to login and signin routes * Different delay for sendStatus and json --------- Co-authored-by: Markus Frindt <m.frindt@cognigy.com>
33 lines
932 B
JavaScript
33 lines
932 B
JavaScript
const logger = require('./logger');
|
|
|
|
function delayLoginMiddleware(req, res, next) {
|
|
if (req.path.includes('/login') || req.path.includes('/signin')) {
|
|
const min = 200;
|
|
const max = 1000;
|
|
/* Random delay between 200 - 1000ms */
|
|
const sendStatusDelay = Math.floor(Math.random() * (max - min + 1)) + min;
|
|
|
|
/* the res.json take longer, we decrease the max delay slightly to 0-800ms */
|
|
const jsonDelay = Math.floor(Math.random() * 800);
|
|
logger.debug(`delayLoginMiddleware: sendStatus ${sendStatusDelay} - json ${jsonDelay}`);
|
|
const sendStatus = res.sendStatus;
|
|
const json = res.json;
|
|
|
|
res.sendStatus = function(status) {
|
|
setTimeout(() => {
|
|
sendStatus.call(res, status);
|
|
}, sendStatusDelay);
|
|
};
|
|
res.json = function(body) {
|
|
setTimeout(() => {
|
|
json.call(res, body);
|
|
}, jsonDelay);
|
|
};
|
|
}
|
|
next();
|
|
}
|
|
|
|
module.exports = {
|
|
delayLoginMiddleware
|
|
};
|