Add optional cache max value size limit configuration (#13871)

Resolves #13708
This commit is contained in:
Rijk van Zanten
2022-06-13 13:03:41 -04:00
committed by GitHub
parent e0730af281
commit 8cee483a1d
7 changed files with 43 additions and 13 deletions

View File

@@ -74,6 +74,7 @@ const allowedEnvironmentVars = [
'CACHE_REDIS_PORT',
'CACHE_REDIS_PASSWORD',
'CACHE_MEMCACHE',
'CACHE_VALUE_MAX_SIZE',
// storage
'STORAGE_LOCATIONS',
'STORAGE_.+_DRIVER',
@@ -206,6 +207,7 @@ const defaults: Record<string, any> = {
CACHE_CONTROL_S_MAXAGE: '0',
CACHE_SCHEMA: true,
CACHE_PERMISSIONS: true,
CACHE_VALUE_MAX_SIZE: false,
AUTH_PROVIDERS: '',
AUTH_DISABLE_DEFAULT: false,

View File

@@ -8,16 +8,27 @@ import { getCacheControlHeader } from '../utils/get-cache-headers';
import logger from '../logger';
import { ExportService } from '../services';
import { getDateFormatted } from '../utils/get-date-formatted';
import { stringByteSize } from '../utils/get-string-byte-size';
import { parse as parseBytesConfiguration } from 'bytes';
export const respond: RequestHandler = asyncHandler(async (req, res) => {
const { cache } = getCache();
let exceedsMaxSize = false;
if (env.CACHE_VALUE_MAX_SIZE !== false) {
const valueSize = stringByteSize(JSON.stringify(res.locals.payload));
const maxSize = parseBytesConfiguration(env.CACHE_VALUE_MAX_SIZE);
exceedsMaxSize = valueSize > maxSize;
}
if (
req.method.toLowerCase() === 'get' &&
env.CACHE_ENABLED === true &&
cache &&
!req.sanitizedQuery.export &&
res.locals.cache !== false
res.locals.cache !== false &&
exceedsMaxSize === false
) {
const key = getCacheKey(req);

View File

@@ -0,0 +1,6 @@
/**
* Returns the byte size for a given input string
*/
export function stringByteSize(string: string): number {
return Buffer.byteLength(string, 'utf-8');
}