Skip to content
This repository has been archived by the owner on Aug 16, 2024. It is now read-only.

Commit

Permalink
Small cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
bojanraic committed Jun 20, 2024
1 parent 376667a commit b02261e
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
34 changes: 15 additions & 19 deletions src/services/bwsService.js
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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);
}
}
Expand All @@ -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`
);
Expand All @@ -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;
}
Expand All @@ -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}`,
});
}
};
13 changes: 10 additions & 3 deletions src/util/cache.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
7 changes: 3 additions & 4 deletions src/util/cmdRunner.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
import { exec } from "child_process";
import { constants } from "../util/constants.js";

/**
* Run an arbitrary command line command and return the string output.
* @param {string} command - The command to be executed.
* @returns {Promise<string>} 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()));
});
});
Expand Down
5 changes: 2 additions & 3 deletions src/util/constants.js
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -19,5 +20,3 @@ const constants = {
}
}
}

export default constants;

0 comments on commit b02261e

Please sign in to comment.