Skip to content

Commit

Permalink
Added a configurable handle of the cache to enable/disable by environ…
Browse files Browse the repository at this point in the history
…ment variable ENABLE_CACHE
  • Loading branch information
dfornaciari committed Nov 29, 2024
1 parent f8c376f commit b20f36d
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 6 deletions.
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 @@ -164,6 +165,7 @@ const parseConfig = (config: EnvironmentVariables & Record<string, string>): 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,

Check failure on line 168 in src/config.ts

View workflow job for this annotation

GitHub Actions / lint / checks

Expected object keys to be in ascending order. 'ENABLE_CACHE' should be before 'USER_PROPERTIES_HEADER_KEY'
}
}

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>

Check failure on line 93 in src/lib/configurations.ts

View workflow job for this annotation

GitHub Actions / lint / checks

Trailing spaces not allowed
if (config.ENABLE_CACHE === "true" ) {

Check failure on line 94 in src/lib/configurations.ts

View workflow job for this annotation

GitHub Actions / lint / checks

Strings must use singlequote

Check failure on line 94 in src/lib/configurations.ts

View workflow job for this annotation

GitHub Actions / lint / checks

There should be no space before this paren
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
13 changes: 10 additions & 3 deletions src/lib/public.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { readFile } from 'fs/promises'

Check failure on line 1 in src/lib/public.ts

View workflow job for this annotation

GitHub Actions / lint / checks

There should be at least one empty line between import groups
import { RuntimeConfig } from '../config'

Check failure on line 2 in src/lib/public.ts

View workflow job for this annotation

GitHub Actions / lint / checks

All imports in the declaration are only used as types. Use `import type`

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>

Check failure on line 8 in src/lib/public.ts

View workflow job for this annotation

GitHub Actions / lint / checks

Trailing spaces not allowed
if (config.ENABLE_CACHE === "true") {

Check failure on line 9 in src/lib/public.ts

View workflow job for this annotation

GitHub Actions / lint / checks

Strings must use singlequote
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 @@ -32,6 +32,7 @@ describe('Extract Acl Context', () => {
RESOURCES_DIRECTORY_PATH: '',
SERVICE_CONFIG_PATH: '',
USER_PROPERTIES_HEADER_KEY: 'userproperties',
ENABLE_CACHE: '',

Check failure on line 35 in src/lib/test/extract-acl-context.test.ts

View workflow job for this annotation

GitHub Actions / lint / checks

Expected object keys to be in ascending order. 'ENABLE_CACHE' should be before 'USER_PROPERTIES_HEADER_KEY'
}

const request = {
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 @@ -45,6 +45,7 @@ describe('Extract Language Context', () => {
RESOURCES_DIRECTORY_PATH: '',
SERVICE_CONFIG_PATH: '',
USER_PROPERTIES_HEADER_KEY: '',
ENABLE_CACHE: '',

Check failure on line 48 in src/lib/test/extract-language-context.test.ts

View workflow job for this annotation

GitHub Actions / lint / checks

Expected object keys to be in ascending order. 'ENABLE_CACHE' should be before 'USER_PROPERTIES_HEADER_KEY'
}

interface Test {
Expand Down
2 changes: 2 additions & 0 deletions src/lib/test/serve-files.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -65,6 +66,7 @@ describe('Serve files', () => {
fastify = await setupFastify({
RESOURCES_DIRECTORY_PATH: configurations,
SERVICE_CONFIG_PATH: configPath,
ENABLE_CACHE: '',
})
})

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 @@ -60,6 +60,7 @@ describe('Serve files', () => {
PUBLIC_DIRECTORY_PATH: publicDir,
RESOURCES_DIRECTORY_PATH: resourcesDir,
SERVICE_CONFIG_PATH: configPath,
ENABLE_CACHE: '',
})
})

Expand Down
4 changes: 4 additions & 0 deletions src/schemas/environmentVariablesSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
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 @@ -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', () => {
Expand Down

0 comments on commit b20f36d

Please sign in to comment.