fix: don't error if file required too early

Exception during run: TypeError: ttl must be positive integer or Infinity if set
    at new TTLCache (/home/runner/work/NodeBB/NodeBB/node_modules/@isaacs/ttlcache/index.js:35:13)
This commit is contained in:
Barış Soner Uşaklı
2024-06-11 12:09:24 -04:00
parent 60b4bc662f
commit 4430de8c4a

View File

@@ -5,12 +5,12 @@ const meta = require('../meta');
const helpers = require('./helpers'); const helpers = require('./helpers');
const user = require('../user'); const user = require('../user');
const cache = cacheCreate({ let cache;
ttl: meta.config.uploadRateLimitCooldown * 1000,
});
exports.clearCache = function () { exports.clearCache = function () {
cache.clear(); if (cache) {
cache.clear();
}
}; };
exports.ratelimit = helpers.try(async (req, res, next) => { exports.ratelimit = helpers.try(async (req, res, next) => {
@@ -18,7 +18,11 @@ exports.ratelimit = helpers.try(async (req, res, next) => {
if (!meta.config.uploadRateLimitThreshold || (uid && await user.isAdminOrGlobalMod(uid))) { if (!meta.config.uploadRateLimitThreshold || (uid && await user.isAdminOrGlobalMod(uid))) {
return next(); return next();
} }
if (!cache) {
cache = cacheCreate({
ttl: meta.config.uploadRateLimitCooldown * 1000,
});
}
const count = (cache.get(`${req.ip}:uploaded_file_count`) || 0) + req.files.files.length; const count = (cache.get(`${req.ip}:uploaded_file_count`) || 0) + req.files.files.length;
if (count > meta.config.uploadRateLimitThreshold) { if (count > meta.config.uploadRateLimitThreshold) {
return next(new Error(['[[error:upload-ratelimit-reached]]'])); return next(new Error(['[[error:upload-ratelimit-reached]]']));