diff --git a/src/server.js b/src/server.js index fba0eb7..d15810c 100644 --- a/src/server.js +++ b/src/server.js @@ -2,7 +2,7 @@ import http from "http"; import express from "express"; import fs from "fs/promises"; import { initialize } from "@oas-tools/core"; -import constants from "./util/constants.js"; +import { constants } from "./util/constants.js"; // Function to read and parse JSON file async function readAndParse(filePath) { diff --git a/src/services/bwsService.js b/src/services/bwsService.js index 8b3afcd..064390a 100644 --- a/src/services/bwsService.js +++ b/src/services/bwsService.js @@ -1,4 +1,4 @@ -import constants from "../util/constants.js"; +import { constants } from "../util/constants.js"; import { runCommand } from "../util/cmdRunner.js"; import { cache, compress, decompress } from "../util/cache.js"; @@ -11,15 +11,14 @@ const groupSecretsByProject = async (secrets, fetchSecretsIfMissing = false) => } projectSecrets[projectId].push(secret); }); - + cache.projectSecrets = projectSecrets; if (fetchSecretsIfMissing) { const missingProjects = Object.keys(projectSecrets).filter(projectId => !cache.projectSecrets[projectId]); for (const projectId of missingProjects) { - console.log(`BWS API: Project-specific secrets not found in cache, fetching all secrets...`); + console.log(`${constants.LOG_TAG}: Project-specific secrets not found in cache, fetching all secrets...`); const allSecrets = await fetchAndCacheEntities('secret'); - console.log(`allSecrets in groupSecretsByProject: ${JSON.stringify(allSecrets)}`); await groupSecretsByProject(allSecrets); } } @@ -33,50 +32,47 @@ const fetchAndCacheEntities = async (entityType) => { }; const getCachedEntityByIdentifier = async (type, idValue, idType, projectId) => { - console.log(`BWS API: Search cache for ${type} with ${idType} ${idValue}...`); + console.log(`${constants.LOG_TAG}: Search cache for ${type} with ${idType} ${idValue}...`); if (!cache.project || !cache.secret) { - console.log(`BWS API: No ${type}s fetched yet, fetching all ${type}s...`); + console.log(`${constants.LOG_TAG}: No ${type}s fetched yet, fetching all ${type}s...`); await fetchAndCacheEntities(type); } let entity = null; if (type === 'secret') { if (projectId) { - console.log(`BWS API: Checking if ${type}s are fetched...`); let projectSecrets = cache.projectSecrets; if (projectSecrets[projectId]) { - console.log(`BWS API: Searching cache for secrets in project ${projectId}...`); + console.log(`${constants.LOG_TAG}: Searching cache for secrets in project ${projectId}...`); const projectSecrets = cache.projectSecrets[projectId]; entity = projectSecrets.find(e => e[idType] === idValue); - } else { - console.log(`BWS API: ${type}s with ${projectId} not found in cache.projectSecrets.`); } } else { - console.log(`BWS API: Searching entire cache for ${type}s with ${idType} ${idValue}...`); + console.log(`${constants.LOG_TAG}: Searching entire cache for ${type}s with ${idType} ${idValue}...`); const entities = decompress(cache[type]); entity = entities.find(e => e[idType] === idValue); } } else { - console.log(`BWS API: Searching entire cache for ${type} with ${idType} ${idValue}...`); + console.log(`${constants.LOG_TAG}: Searching entire cache for ${type} with ${idType} ${idValue}...`); const entities = decompress(cache[type]); entity = entities.find(e => e[idType] === idValue); } if (!entity) { - console.log(`BWS API: Entity ${type} with ${idType} ${idValue} not found in cache.`); + console.log(`${constants.LOG_TAG}: Entity ${type} with ${idType} ${idValue} not found in cache.`); } else { - console.log(`BWS API: Entity ${type} with ${idType} ${idValue} found in cache. Entity ID: ${entity.id}`); + console.log(`${constants.LOG_TAG}: Entity ${type} with ${idType} ${idValue} found in cache. Entity ID: ${entity.id}`); } return entity; }; const getCachedEntity = async (type, idValue, idType, projectId) => { - console.log(`BWS API: Get the value of ${type} with ${idType} ${idValue}`); + console.log(`${constants.LOG_TAG}: Get the value of ${type} with ${idType} ${idValue}`); let entity = await getCachedEntityByIdentifier(type, idValue, idType, projectId); if (!entity) { - console.log(`BWS API: Cache miss, fetching all ${type}s...`); + console.log(`${constants.LOG_TAG}: Cache miss, fetching all ${type}s...`); const resultJSON = await runCommand( `${constants.BWS_CLI_PATH} list ${type}s` ); @@ -97,7 +93,7 @@ export const getByIdOrName = async (req, res) => { if ((!_id && !_name) || (_id && _name)) { res.status(400).send({ - message: `BWS API: You must provide either id or name, but not both.`, + message: `${constants.LOG_TAG}: You must provide either id or name, but not both.`, }); return; } @@ -111,13 +107,13 @@ export const getByIdOrName = async (req, res) => { res.setHeader("Content-Type", "application/json"); res.send(entity); } else { - throw new Error(`BWS API: Entity ${_type} with ${idType} ${idValue} not found`); + throw new Error(`${constants.LOG_TAG}: Entity ${_type} with ${idType} ${idValue} not found`); } } catch (error) { console.error(`Error: ${error.message}`); res.setHeader("Content-Type", "application/json"); res.status(404).send({ - message: `BWS API: Error: ${error.message}`, + message: `${constants.LOG_TAG}: Error: ${error.message}`, }); } }; diff --git a/src/util/cache.js b/src/util/cache.js index 314e410..3d15593 100644 --- a/src/util/cache.js +++ b/src/util/cache.js @@ -1,13 +1,20 @@ import zlib from 'node:zlib'; +import { constants } from "../util/constants.js"; let cache = { - secret: null, - project: null, + secret: [], + project: [], projectSecrets: {} }; function compress(json) { - return zlib.gzipSync(JSON.stringify(json)).toString('base64'); + const jsonString = JSON.stringify(json); + const uncompressedSize = Buffer.byteLength(jsonString, 'utf8'); + const compressed = zlib.gzipSync(jsonString); + const compressedSize = compressed.length; + const compressionSavingsPercentage = ((uncompressedSize - compressedSize) / uncompressedSize) * 100; + console.log(`${constants.LOG_TAG}: Cache: Original size ${uncompressedSize} bytes, compressed size ${compressedSize} bytes, (${compressionSavingsPercentage.toFixed(2)}% saved)`); + return compressed.toString('base64'); } function decompress(compressed) { diff --git a/src/util/cmdRunner.js b/src/util/cmdRunner.js index a363c93..6838fcd 100644 --- a/src/util/cmdRunner.js +++ b/src/util/cmdRunner.js @@ -1,4 +1,5 @@ import { exec } from "child_process"; +import { constants } from "../util/constants.js"; /** * Run an arbitrary command line command and return the string output. @@ -6,19 +7,17 @@ import { exec } from "child_process"; * @returns {Promise} A Promise that resolves with the command output. */ export function runCommand(command) { - console.log(`BWS API: Run CLI: ${command}`); + console.log(`${constants.LOG_TAG}: Run CLI: ${command}`); return new Promise((resolve, reject) => { exec(command, (error, stdout, stderr) => { if (error) { reject(error); return; } - if (stderr) { reject(new Error(stderr)); return; - } - + } resolve(JSON.parse(stdout.trim())); }); }); diff --git a/src/util/constants.js b/src/util/constants.js index 9651334..b08b279 100644 --- a/src/util/constants.js +++ b/src/util/constants.js @@ -1,6 +1,7 @@ -const constants = { +export const constants = { BWS_ACCESS_TOKEN: process.env.BWS_ACCESS_TOKEN || undefined, BWS_CLI_PATH: process.env.BWS_CLI_PATH || "/usr/local/bin/bws", + LOG_TAG: "BWS API", defaults: { SERVER_PORT: 8080, OAS_FILE_PATH: "./api/spec.yaml", @@ -19,5 +20,3 @@ const constants = { } } } - -export default constants; \ No newline at end of file