Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a configurable handle of the cache to enable/disable by environ… #441

Merged
merged 2 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ const parseConfig = (config: EnvironmentVariables & Record<string, string>): 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

Expand Down Expand Up @@ -157,6 +158,7 @@ const parseConfig = (config: EnvironmentVariables & Record<string, string>): 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,
Expand Down
3 changes: 3 additions & 0 deletions src/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const CONTENT_TYPE_MAP: Record<Extension | string, string> = {
'.yml': 'text/yaml',
}

const ENABLE_CACHE = 'true'

export {
ACL_CONTEXT_BUILDER_PATH,
CONTENT_TYPE_MAP,
Expand All @@ -30,4 +32,5 @@ export {
PUBLIC_DIRECTORY_PATH,
PUBLIC_HEADERS_MAP,
LANGUAGES_DIRECTORY_PATH,
ENABLE_CACHE,
}
11 changes: 9 additions & 2 deletions src/lib/configurations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ConfigurationResponse> {
const bufferPromise = fsCache.get(filename) ?? fileLoader(filename)
fsCache.set(filename, bufferPromise)
let bufferPromise: Promise<Buffer>

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
Expand Down
2 changes: 1 addition & 1 deletion src/lib/onSendHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 11 additions & 3 deletions src/lib/public.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { readFile } from 'fs/promises'

import type { RuntimeConfig } from '../config'

const fsCache = new Map<string, Promise<Buffer>>()

async function publicHandler(filename: string, injectNonce: (input: string) => string): Promise<Buffer> {
const bufferPromise: Promise<Buffer> = fsCache.get(filename) ?? readFile(filename)
fsCache.set(filename, bufferPromise)
async function publicHandler(filename: string, injectNonce: (input: string) => string, config: RuntimeConfig): Promise<Buffer> {
let bufferPromise: Promise<Buffer>

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')) {
Expand Down
1 change: 1 addition & 0 deletions src/lib/test/extract-acl-context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: '',
Expand Down
1 change: 1 addition & 0 deletions src/lib/test/extract-language-context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand Down
1 change: 1 addition & 0 deletions src/lib/test/serve-files.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ describe('Serve files', () => {
cleanup = () => Promise.all([confCleanup(), cpCleanup()])

fastify = await setupFastify({
ENABLE_CACHE: '',
RESOURCES_DIRECTORY_PATH: configurations,
SERVICE_CONFIG_PATH: configPath,
})
Expand Down
1 change: 1 addition & 0 deletions src/lib/test/serve-public.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ 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,
Expand Down
4 changes: 4 additions & 0 deletions src/schemas/environmentVariablesSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
1 change: 1 addition & 0 deletions src/test/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ 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',
Expand Down
Loading