This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
2021-06-25 15:51:43 -07:00
|
|
|
const USER_AGENT_BOTS = /bot|crawler|baiduspider|80legs|ia_archiver|voyager|curl|wget|yahoo! slurp|mediapartners-google|facebookexternalhit|twitterbot|whatsapp|php|python/;
|
|
|
|
const USER_AGENT_BROWSERS = /mozilla|msie|gecko|firefox|edge|opera|safari|netscape|konqueror|android/;
|
|
|
|
|
|
|
|
const isBrowser = (userAgent) => {
|
|
|
|
if (!userAgent) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const isProbablyBot = !!userAgent.toLowerCase().match(USER_AGENT_BOTS);
|
|
|
|
const isProbablyBrowser = !!userAgent.toLowerCase().match(USER_AGENT_BROWSERS);
|
|
|
|
|
|
|
|
return isProbablyBrowser || !isProbablyBot;
|
|
|
|
}
|
|
|
|
|
2021-07-09 11:42:39 -07:00
|
|
|
const overload = {
|
|
|
|
en: [[17, 23]],
|
|
|
|
};
|
|
|
|
|
|
|
|
const isHighLoad = (timestamp, overloadPeriods) => {
|
|
|
|
if (overloadPeriods === undefined) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let [periodStart, periodEnd] of overloadPeriods) {
|
|
|
|
if (timestamp.getUTCHours() >= periodStart && timestamp.getUTCHours() < periodEnd) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-06-25 15:51:43 -07:00
|
|
|
export default function(req, res, next) {
|
2021-07-09 11:42:39 -07:00
|
|
|
if (process.env.NODE_ENV === 'production') {
|
|
|
|
res.spa = isBrowser(req.headers['user-agent']) || isHighLoad(new Date, overload[process.env.LOCALE]);
|
|
|
|
}
|
|
|
|
next();
|
2021-06-25 15:51:43 -07:00
|
|
|
}
|