From b20f36d69461378f1a9d6b1b422977173d853e05 Mon Sep 17 00:00:00 2001 From: Daniele Fornaciari Date: Thu, 14 Nov 2024 10:40:11 +0100 Subject: [PATCH 1/2] Added a configurable handle of the cache to enable/disable by environment variable ENABLE_CACHE --- src/config.ts | 2 ++ src/defaults.ts | 3 +++ src/lib/configurations.ts | 11 +++++++++-- src/lib/onSendHandler.ts | 2 +- src/lib/public.ts | 13 ++++++++++--- src/lib/test/extract-acl-context.test.ts | 1 + src/lib/test/extract-language-context.test.ts | 1 + src/lib/test/serve-files.test.ts | 2 ++ src/lib/test/serve-public.test.ts | 1 + src/schemas/environmentVariablesSchema.ts | 4 ++++ src/test/config.test.ts | 1 + 11 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/config.ts b/src/config.ts index 4d52140..9fb2f04 100644 --- a/src/config.ts +++ b/src/config.ts @@ -126,6 +126,7 @@ const parseConfig = (config: EnvironmentVariables & Record): Run ACL_CONTEXT_BUILDER_PATH = defaults.ACL_CONTEXT_BUILDER_PATH, LANGUAGES_DIRECTORY_PATH = defaults.LANGUAGES_DIRECTORY_PATH, SERVICE_CONFIG_PATH = defaults.SERVICE_CONFIG_PATH, + ENABLE_CACHE = defaults.ENABLE_CACHE, } = config let serviceConfig: unknown = defaults.PUBLIC_HEADERS_MAP @@ -164,6 +165,7 @@ const parseConfig = (config: EnvironmentVariables & Record): Run RESOURCES_DIRECTORY_PATH: config.RESOURCES_DIRECTORY_PATH ?? defaults.RESOURCES_DIRECTORY_PATH, SERVICE_CONFIG_PATH, USER_PROPERTIES_HEADER_KEY: config.USER_PROPERTIES_HEADER_KEY, + ENABLE_CACHE, } } diff --git a/src/defaults.ts b/src/defaults.ts index b395e24..853e348 100644 --- a/src/defaults.ts +++ b/src/defaults.ts @@ -22,6 +22,8 @@ const CONTENT_TYPE_MAP: Record = { '.yml': 'text/yaml', } +const ENABLE_CACHE = 'true' + export { ACL_CONTEXT_BUILDER_PATH, CONTENT_TYPE_MAP, @@ -30,4 +32,5 @@ export { PUBLIC_DIRECTORY_PATH, PUBLIC_HEADERS_MAP, LANGUAGES_DIRECTORY_PATH, + ENABLE_CACHE, } diff --git a/src/lib/configurations.ts b/src/lib/configurations.ts index de3ae87..219771e 100644 --- a/src/lib/configurations.ts +++ b/src/lib/configurations.ts @@ -89,8 +89,15 @@ const shouldManipulate = (extension: ExtensionOutput): extension is Extension => ['.json', '.yaml', '.yml'].includes(extension) async function configurationsHandler(request: FastifyRequest, filename: string, config: RuntimeConfig): Promise { - const bufferPromise = fsCache.get(filename) ?? fileLoader(filename) - fsCache.set(filename, bufferPromise) + let bufferPromise: Promise + + if (config.ENABLE_CACHE === "true" ) { + bufferPromise = fsCache.get(filename) ?? fileLoader(filename) + fsCache.set(filename, bufferPromise) + } else { + bufferPromise = fileLoader(filename) + } + const buffer = await bufferPromise const fileExtension = path.extname(filename) as ExtensionOutput diff --git a/src/lib/onSendHandler.ts b/src/lib/onSendHandler.ts index fa1d478..c1e9687 100644 --- a/src/lib/onSendHandler.ts +++ b/src/lib/onSendHandler.ts @@ -85,7 +85,7 @@ const staticFileHandler = (context: FastifyContext) => async ( language && reply.header('content-language', language) buffer = fileBuffer } else if (isPublic(url)) { - const fileBuffer = await publicHandler(filename, injectNonce) + const fileBuffer = await publicHandler(filename, injectNonce, config) // eslint-disable-next-line @typescript-eslint/no-floating-promises reply.header('content-length', fileBuffer.length) buffer = fileBuffer diff --git a/src/lib/public.ts b/src/lib/public.ts index 0cb4296..56be73b 100644 --- a/src/lib/public.ts +++ b/src/lib/public.ts @@ -1,10 +1,17 @@ import { readFile } from 'fs/promises' +import { RuntimeConfig } from '../config' const fsCache = new Map>() -async function publicHandler(filename: string, injectNonce: (input: string) => string): Promise { - const bufferPromise: Promise = fsCache.get(filename) ?? readFile(filename) - fsCache.set(filename, bufferPromise) +async function publicHandler(filename: string, injectNonce: (input: string) => string, config: RuntimeConfig): Promise { + let bufferPromise: Promise + + if (config.ENABLE_CACHE === "true") { + bufferPromise = fsCache.get(filename) ?? readFile(filename) + fsCache.set(filename, bufferPromise) + } else { + bufferPromise = readFile(filename) + } // in case of html, parse variables if (filename.endsWith('.html')) { diff --git a/src/lib/test/extract-acl-context.test.ts b/src/lib/test/extract-acl-context.test.ts index f725bfd..76a9851 100644 --- a/src/lib/test/extract-acl-context.test.ts +++ b/src/lib/test/extract-acl-context.test.ts @@ -32,6 +32,7 @@ describe('Extract Acl Context', () => { RESOURCES_DIRECTORY_PATH: '', SERVICE_CONFIG_PATH: '', USER_PROPERTIES_HEADER_KEY: 'userproperties', + ENABLE_CACHE: '', } const request = { diff --git a/src/lib/test/extract-language-context.test.ts b/src/lib/test/extract-language-context.test.ts index 23c29ec..4e76193 100644 --- a/src/lib/test/extract-language-context.test.ts +++ b/src/lib/test/extract-language-context.test.ts @@ -45,6 +45,7 @@ describe('Extract Language Context', () => { RESOURCES_DIRECTORY_PATH: '', SERVICE_CONFIG_PATH: '', USER_PROPERTIES_HEADER_KEY: '', + ENABLE_CACHE: '', } interface Test { diff --git a/src/lib/test/serve-files.test.ts b/src/lib/test/serve-files.test.ts index 8010b5a..2ba94e2 100644 --- a/src/lib/test/serve-files.test.ts +++ b/src/lib/test/serve-files.test.ts @@ -28,6 +28,7 @@ import * as evaluateAcl from '../../sdk/evaluate-acl' import * as evaluateLanguage from '../../sdk/evaluate-language' import * as resolveReferences from '../../sdk/resolve-references' import { baseVariables, createConfigFile, createTmpDir, setupFastify } from '../../utils/test-utils' +import { ENABLE_CACHE } from '../../defaults' describe('Serve files', () => { const sandbox = createSandbox() @@ -65,6 +66,7 @@ describe('Serve files', () => { fastify = await setupFastify({ RESOURCES_DIRECTORY_PATH: configurations, SERVICE_CONFIG_PATH: configPath, + ENABLE_CACHE: '', }) }) diff --git a/src/lib/test/serve-public.test.ts b/src/lib/test/serve-public.test.ts index d1f6392..8911a1a 100644 --- a/src/lib/test/serve-public.test.ts +++ b/src/lib/test/serve-public.test.ts @@ -60,6 +60,7 @@ describe('Serve files', () => { PUBLIC_DIRECTORY_PATH: publicDir, RESOURCES_DIRECTORY_PATH: resourcesDir, SERVICE_CONFIG_PATH: configPath, + ENABLE_CACHE: '', }) }) diff --git a/src/schemas/environmentVariablesSchema.ts b/src/schemas/environmentVariablesSchema.ts index 15b5d9d..7f879d6 100644 --- a/src/schemas/environmentVariablesSchema.ts +++ b/src/schemas/environmentVariablesSchema.ts @@ -39,6 +39,10 @@ export const environmentVariablesSchema = { description: 'service configuration file absolute path', type: 'string', }, + ENABLE_CACHE: { + description: 'Enable use of cache (default: true)', + type: 'string', + } }, required: [], type: 'object', diff --git a/src/test/config.test.ts b/src/test/config.test.ts index 55ebbaa..5374ab9 100644 --- a/src/test/config.test.ts +++ b/src/test/config.test.ts @@ -38,6 +38,7 @@ const defaults = { PUBLIC_DIRECTORY_PATH: '/usr/static/public', RESOURCES_DIRECTORY_PATH: '/usr/static/configurations', USER_PROPERTIES_HEADER_KEY: 'miauserproperties', + ENABLE_CACHE: defaultConfigs.ENABLE_CACHE, } describe('config injection tests', () => { From 6cf2518f204b927314ced9f50bc0b7a5831857cb Mon Sep 17 00:00:00 2001 From: Daniele Fornaciari Date: Tue, 3 Dec 2024 17:11:23 +0100 Subject: [PATCH 2/2] fixing lint --- src/config.ts | 2 +- src/lib/configurations.ts | 4 ++-- src/lib/public.ts | 7 ++++--- src/lib/test/extract-acl-context.test.ts | 2 +- src/lib/test/extract-language-context.test.ts | 2 +- src/lib/test/serve-files.test.ts | 3 +-- src/lib/test/serve-public.test.ts | 2 +- src/schemas/environmentVariablesSchema.ts | 8 ++++---- src/test/config.test.ts | 2 +- 9 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/config.ts b/src/config.ts index 9fb2f04..5172fe3 100644 --- a/src/config.ts +++ b/src/config.ts @@ -158,6 +158,7 @@ const parseConfig = (config: EnvironmentVariables & Record): Run ACL_CONTEXT_BUILDER: getAclContextBuilder(ACL_CONTEXT_BUILDER_PATH), ACL_CONTEXT_BUILDER_PATH, CONTENT_TYPE_MAP: validateContentTypeMap(contentTypeMap), + ENABLE_CACHE, LANGUAGES_CONFIG: validateLanguages(LANGUAGES_DIRECTORY_PATH), LANGUAGES_DIRECTORY_PATH, PUBLIC_DIRECTORY_PATH: config.PUBLIC_DIRECTORY_PATH ?? defaults.PUBLIC_DIRECTORY_PATH, @@ -165,7 +166,6 @@ const parseConfig = (config: EnvironmentVariables & Record): Run RESOURCES_DIRECTORY_PATH: config.RESOURCES_DIRECTORY_PATH ?? defaults.RESOURCES_DIRECTORY_PATH, SERVICE_CONFIG_PATH, USER_PROPERTIES_HEADER_KEY: config.USER_PROPERTIES_HEADER_KEY, - ENABLE_CACHE, } } diff --git a/src/lib/configurations.ts b/src/lib/configurations.ts index 219771e..37488c9 100644 --- a/src/lib/configurations.ts +++ b/src/lib/configurations.ts @@ -90,8 +90,8 @@ const shouldManipulate = (extension: ExtensionOutput): extension is Extension => async function configurationsHandler(request: FastifyRequest, filename: string, config: RuntimeConfig): Promise { let bufferPromise: Promise - - if (config.ENABLE_CACHE === "true" ) { + + if (config.ENABLE_CACHE === 'true') { bufferPromise = fsCache.get(filename) ?? fileLoader(filename) fsCache.set(filename, bufferPromise) } else { diff --git a/src/lib/public.ts b/src/lib/public.ts index 56be73b..d4c69da 100644 --- a/src/lib/public.ts +++ b/src/lib/public.ts @@ -1,12 +1,13 @@ import { readFile } from 'fs/promises' -import { RuntimeConfig } from '../config' + +import type { RuntimeConfig } from '../config' const fsCache = new Map>() async function publicHandler(filename: string, injectNonce: (input: string) => string, config: RuntimeConfig): Promise { let bufferPromise: Promise - - if (config.ENABLE_CACHE === "true") { + + if (config.ENABLE_CACHE === 'true') { bufferPromise = fsCache.get(filename) ?? readFile(filename) fsCache.set(filename, bufferPromise) } else { diff --git a/src/lib/test/extract-acl-context.test.ts b/src/lib/test/extract-acl-context.test.ts index 76a9851..f127fac 100644 --- a/src/lib/test/extract-acl-context.test.ts +++ b/src/lib/test/extract-acl-context.test.ts @@ -25,6 +25,7 @@ describe('Extract Acl Context', () => { ACL_CONTEXT_BUILDER: undefined, ACL_CONTEXT_BUILDER_PATH: '', CONTENT_TYPE_MAP: {}, + ENABLE_CACHE: '', LANGUAGES_CONFIG: [], LANGUAGES_DIRECTORY_PATH: '', PUBLIC_DIRECTORY_PATH: '', @@ -32,7 +33,6 @@ describe('Extract Acl Context', () => { RESOURCES_DIRECTORY_PATH: '', SERVICE_CONFIG_PATH: '', USER_PROPERTIES_HEADER_KEY: 'userproperties', - ENABLE_CACHE: '', } const request = { diff --git a/src/lib/test/extract-language-context.test.ts b/src/lib/test/extract-language-context.test.ts index 4e76193..25a1c0b 100644 --- a/src/lib/test/extract-language-context.test.ts +++ b/src/lib/test/extract-language-context.test.ts @@ -25,6 +25,7 @@ describe('Extract Language Context', () => { ACL_CONTEXT_BUILDER: undefined, ACL_CONTEXT_BUILDER_PATH: '', CONTENT_TYPE_MAP: {}, + ENABLE_CACHE: '', LANGUAGES_CONFIG: [ { labelsMap: { hello: 'hello' }, @@ -45,7 +46,6 @@ describe('Extract Language Context', () => { RESOURCES_DIRECTORY_PATH: '', SERVICE_CONFIG_PATH: '', USER_PROPERTIES_HEADER_KEY: '', - ENABLE_CACHE: '', } interface Test { diff --git a/src/lib/test/serve-files.test.ts b/src/lib/test/serve-files.test.ts index 2ba94e2..f562ca8 100644 --- a/src/lib/test/serve-files.test.ts +++ b/src/lib/test/serve-files.test.ts @@ -28,7 +28,6 @@ import * as evaluateAcl from '../../sdk/evaluate-acl' import * as evaluateLanguage from '../../sdk/evaluate-language' import * as resolveReferences from '../../sdk/resolve-references' import { baseVariables, createConfigFile, createTmpDir, setupFastify } from '../../utils/test-utils' -import { ENABLE_CACHE } from '../../defaults' describe('Serve files', () => { const sandbox = createSandbox() @@ -64,9 +63,9 @@ describe('Serve files', () => { cleanup = () => Promise.all([confCleanup(), cpCleanup()]) fastify = await setupFastify({ + ENABLE_CACHE: '', RESOURCES_DIRECTORY_PATH: configurations, SERVICE_CONFIG_PATH: configPath, - ENABLE_CACHE: '', }) }) diff --git a/src/lib/test/serve-public.test.ts b/src/lib/test/serve-public.test.ts index 8911a1a..645bf87 100644 --- a/src/lib/test/serve-public.test.ts +++ b/src/lib/test/serve-public.test.ts @@ -57,10 +57,10 @@ describe('Serve files', () => { cleanup = () => Promise.all([pCleanup(), cpCleanup(), rCleanup()]) fastify = await setupFastify({ + ENABLE_CACHE: '', PUBLIC_DIRECTORY_PATH: publicDir, RESOURCES_DIRECTORY_PATH: resourcesDir, SERVICE_CONFIG_PATH: configPath, - ENABLE_CACHE: '', }) }) diff --git a/src/schemas/environmentVariablesSchema.ts b/src/schemas/environmentVariablesSchema.ts index 7f879d6..be510e0 100644 --- a/src/schemas/environmentVariablesSchema.ts +++ b/src/schemas/environmentVariablesSchema.ts @@ -23,6 +23,10 @@ export const environmentVariablesSchema = { description: 'Acl context builder file absolute path', type: 'string', }, + ENABLE_CACHE: { + description: 'Enable use of cache (default: true)', + type: 'string', + }, LANGUAGES_DIRECTORY_PATH: { description: 'Absolute path of the directory containing files to be used for translation', type: 'string', @@ -39,10 +43,6 @@ export const environmentVariablesSchema = { description: 'service configuration file absolute path', type: 'string', }, - ENABLE_CACHE: { - description: 'Enable use of cache (default: true)', - type: 'string', - } }, required: [], type: 'object', diff --git a/src/test/config.test.ts b/src/test/config.test.ts index 5374ab9..df4a91b 100644 --- a/src/test/config.test.ts +++ b/src/test/config.test.ts @@ -33,12 +33,12 @@ const defaults = { ACL_CONTEXT_BUILDER: undefined, ACL_CONTEXT_BUILDER_PATH: '/usr/src/app/config/acl-context-builder.js', CONTENT_TYPE_MAP: defaultConfigs.CONTENT_TYPE_MAP, + ENABLE_CACHE: defaultConfigs.ENABLE_CACHE, LANGUAGES_CONFIG: [], LANGUAGES_DIRECTORY_PATH: '/usr/static/languages', PUBLIC_DIRECTORY_PATH: '/usr/static/public', RESOURCES_DIRECTORY_PATH: '/usr/static/configurations', USER_PROPERTIES_HEADER_KEY: 'miauserproperties', - ENABLE_CACHE: defaultConfigs.ENABLE_CACHE, } describe('config injection tests', () => {