Skip to content

Commit

Permalink
SYSTEST-9456-Change supportedSDKs config and compare in-use/sample co… (
Browse files Browse the repository at this point in the history
#134)

* SYSTEST-9456-Change supportedSDKs config and compare in-use/sample configs

* SYSTEST-9456-minor edit

* SYSTEST-9456-addressed comments

* SYSTEST-9456-addressed comment to move functions to util file
  • Loading branch information
Nandana-NNR authored Jul 3, 2023
1 parent 114a989 commit 1f4c456
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 24 deletions.
2 changes: 1 addition & 1 deletion server/src/.mf.config.SAMPLE.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

"multiUserConnections": "warn",

"supportedSdks": [
"supportedOpenRPCs": [

{
"name": "core",
Expand Down
6 changes: 3 additions & 3 deletions server/src/commandLine.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const knownOpts = {
'proxy' : String,
'multiUserConnections': String
};
for ( const [sdk, oSdk] of Object.entries(config.dotConfig.supportedSdks) ) {
for ( const [sdk, oSdk] of Object.entries(config.dotConfig.supportedOpenRPCs) ) {
if ( oSdk.cliFlag ) {
if ( ! knownOpts.hasOwnProperty(oSdk.cliFlag) ) {
knownOpts[oSdk.cliFlag] = Boolean;
Expand All @@ -65,7 +65,7 @@ const shortHands = {
't' : [ '--triggers' ],
'noval' : [ '--novalidate' ]
};
for ( const [sdk, oSdk] of Object.entries(config.dotConfig.supportedSdks) ) {
for ( const [sdk, oSdk] of Object.entries(config.dotConfig.supportedOpenRPCs) ) {
if ( oSdk.cliShortFlag ) {
if ( ! shortHands.hasOwnProperty(oSdk.cliShortFlag) ) {
shortHands[oSdk.cliShortFlag] = [ `--${oSdk.cliFlag}` ];
Expand Down Expand Up @@ -103,7 +103,7 @@ config.validate = mergeArrayOfStrings(config.validate, config.dotConfig.validate

// Convert boolean flags for any SDKs into a simple map/dict/obj
const sdks = {};
for ( const [sdk, oSdk] of Object.entries(config.dotConfig.supportedSdks) ) {
for ( const [sdk, oSdk] of Object.entries(config.dotConfig.supportedOpenRPCs) ) {
if ( parsed[oSdk.name] || oSdk.enabled ) {
sdks[oSdk.name] = true;
}
Expand Down
21 changes: 15 additions & 6 deletions server/src/dotConfig.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
'use strict';

import path from 'path';
import { fileURLToPath } from 'url';
import fs from 'fs';
import { logger } from './logger.mjs';
import { createAbsoluteFilePath, getCreationDate, getModificationDate } from './util.mjs';


function handleError(fileName, __dirname) {
logger.error(
Expand All @@ -39,16 +40,17 @@ function handleError(fileName, __dirname) {
}

function loadDotConfig() {
let __filename, __dirname, fileName, dotConfig;
let fileName, dotConfig;
try {
__filename = fileURLToPath(import.meta.url);
__dirname = path.dirname(__filename);
fileName = path.resolve(__dirname, '.mf.config.json');
fileName = createAbsoluteFilePath ('.mf.config.json');

dotConfig = JSON.parse(
fs.readFileSync(fileName, 'UTF-8')
);

// For offering backward compatibility to support "supportedSDKs"
if (dotConfig.hasOwnProperty("supportedSDKs")) {
dotConfig.supportedOpenRPCs = dotConfig.supportedSDKs
}
logger.info(`Read Mock Firebolt configuration from ${fileName}`);
} catch ( ex ) {
handleError(fileName, __dirname);
Expand All @@ -58,6 +60,13 @@ function loadDotConfig() {
return dotConfig;
}

const creationTimeSec = getCreationDate('.mf.config.SAMPLE.json')
const modificationTimeSec = getModificationDate('.mf.config.json')

if (creationTimeSec >= modificationTimeSec) {
logger.importantWarning("The .mf.config.SAMPLE.json file in your repo is newer than your .mf.config.json file. Changes to the config file format may have occurred since you created or edited your file, and you may need to merge your changes within the changes to the sample file. Refer to the release notes for more information.")
}

const dotConfig = loadDotConfig();

// --- Exports ---
Expand Down
18 changes: 9 additions & 9 deletions server/src/fireboltOpenRpc.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ function getMeta() {
}

function getMethod(methodName) {
for ( let ii = 0; ii < config.dotConfig.supportedSdks.length; ii += 1 ) {
const sdkName = config.dotConfig.supportedSdks[ii].name;
for ( let ii = 0; ii < config.dotConfig.supportedOpenRPCs.length; ii += 1 ) {
const sdkName = config.dotConfig.supportedOpenRPCs[ii].name;
if (config.app.allowMixedCase){
methodName = toLowerCase(methodName);
}
Expand All @@ -90,8 +90,8 @@ function isMethodKnown(methodName) {
}

function getSchema(schemaName) {
for ( const ii = 0; ii < config.dotConfig.supportedSdks.length; ii += 1 ) {
const sdkName = config.dotConfig.supportedSdks[ii].name;
for ( const ii = 0; ii < config.dotConfig.supportedOpenRPCs.length; ii += 1 ) {
const sdkName = config.dotConfig.supportedOpenRPCs[ii].name;
if ( schemaName in meta[sdkName].components.schemas ) { return meta[sdkName].components.schemas[schemaName]; }
}
return undefined;
Expand Down Expand Up @@ -263,7 +263,7 @@ async function readSdkJsonFileIfEnabled(sdkName) {
let url, fileUrl;
if ( isSdkEnabled(sdkName) ) {
try {
const oSdk = config.dotConfig.supportedSdks.find((oSdk) => { return ( oSdk.name === sdkName ); });
const oSdk = config.dotConfig.supportedOpenRPCs.find((oSdk) => { return ( oSdk.name === sdkName ); });
if ( oSdk.fileName ) {
const openRpcFileName = oSdk.fileName;
if ( path.isAbsolute(openRpcFileName) || openRpcFileName.startsWith('~') ) {
Expand Down Expand Up @@ -305,7 +305,7 @@ async function readAllEnabledSdkJsonFiles() {
await readSdkJsonFileIfEnabled('mock');
}
else {
await Promise.all(config.dotConfig.supportedSdks.map(async (oSdk) => {
await Promise.all(config.dotConfig.supportedOpenRPCs.map(async (oSdk) => {
const sdkName = oSdk.name;
await readSdkJsonFileIfEnabled(sdkName);
}));
Expand All @@ -314,7 +314,7 @@ async function readAllEnabledSdkJsonFiles() {

function buildMethodMapsForAllEnabledSdks() {
// Build faster-performing maps for methods (vs. openrpc.methods array)
config.dotConfig.supportedSdks.forEach(function(oSdk) {
config.dotConfig.supportedOpenRPCs.forEach(function(oSdk) {
const sdkName = oSdk.name;
if ( isSdkEnabled(sdkName) ) {
methodMaps[sdkName] = buildMethodMap(meta[sdkName]);
Expand All @@ -324,14 +324,14 @@ function buildMethodMapsForAllEnabledSdks() {

// --- Module-level Code ---

// Will contain one key for each API enabled (See .mf.config.json::supportedSdks)
// Will contain one key for each API enabled (See .mf.config.json::supportedOpenRPCs)
// The value for each key will be an object containing keys: openrpc, info, methods, and components (contents of firebolt-xxx-sdk.json file)
const rawMeta = {};

// Same as above, but $refs have been dereferenced; this is the main datastructure used here
let meta = {};

// Will contain one key for each API enabled (See .mf.config.json::supportedSdks)
// Will contain one key for each API enabled (See .mf.config.json::supportedOpenRPCs)
// The value for each key will be an object with keys for each method
const methodMaps = {};

Expand Down
2 changes: 1 addition & 1 deletion server/src/fireboltOpenRpcDereferencing.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ function dereferenceSchemas(metaForSdk, methodName) {

function dereferenceMeta(_meta) {
const meta = JSON.parse(JSON.stringify(_meta)); // Deep copy
config.dotConfig.supportedSdks.forEach(function(oSdk) {
config.dotConfig.supportedOpenRPCs.forEach(function(oSdk) {
const sdkName = oSdk.name;
if ( sdkName in meta ) {
const metaForSdk = meta[sdkName];
Expand Down
2 changes: 1 addition & 1 deletion server/src/routes/api/health.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function healthcheck(req, res) {
meta = fireboltOpenRpc.getMeta();
}

config.dotConfig.supportedSdks.forEach(function(oSdk) {
config.dotConfig.supportedOpenRPCs.forEach(function(oSdk) {
const sdkName = oSdk.name;
if ( sdkManagement.isSdkEnabled(sdkName) ) {
if ( meta[sdkName] && meta[sdkName].info ) {
Expand Down
2 changes: 1 addition & 1 deletion server/src/sdkManagement.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import * as commandLine from './commandLine.mjs';

function isSdkEnabled(sdkName) {
// Check if sdk with given name is enabled in the .mf.config.json file
const oSdk = config.dotConfig.supportedSdks.find((oSdk) => { return ( oSdk.name === sdkName ); });
const oSdk = config.dotConfig.supportedOpenRPCs.find((oSdk) => { return ( oSdk.name === sdkName ); });
if ( oSdk && oSdk.enabled ) {
return true;
}
Expand Down
48 changes: 47 additions & 1 deletion server/src/util.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ import * as tmp from 'tmp';

import { config } from './config.mjs';

import { fileURLToPath } from 'url';

import path from 'path';

import fs from 'fs';

// Use: await delay(2000);
function delay(ms) {
return new Promise(res => setTimeout(res, ms));
Expand Down Expand Up @@ -60,6 +66,46 @@ function mergeArrayOfStrings(originalFlags, overrideFlags, denyFlags) {
return newFlags
}

/*
* @function:createAbsoluteFilePath
* @Description: Create absolute filepath from given file name
* @param {String} fileName - file name ex: .mf.config.SAMPLE.jsons
* @Return: Absolute file path ex: D:\mock-firebolt\server\src\.mf.config.SAMPLE.json
*/
function createAbsoluteFilePath(fileName) {
let filePath, __dirname, __filename
__filename = fileURLToPath(import.meta.url).replace("build", "src");
__dirname = path.dirname(__filename);
filePath = path.resolve(__dirname, fileName);
return filePath
}

/*
* @function:getCreationDate
* @Description: To get creation date of file in seconds
* @param {String} fileName - Name of file whose creation time needs to be retrieved in seconds
* @Return: creation time in seconds ex: 1687860231
*/

function getCreationDate(fileName) {
let cFile = createAbsoluteFilePath (fileName)
const creationTimeSec = Math.floor(fs.statSync(cFile).birthtimeMs / 1000);
return creationTimeSec
}

/*
* @function:getModificationDate
* @Description: To get modification date of file in seconds
* @param {String} fileName - Name of file whose modification time needs to be retrieved in seconds
* @Return: modification time in seconds ex: 1687860232
*/
function getModificationDate(fileName) {
let mFile = createAbsoluteFilePath (fileName)
const modificationTimeSec = Math.floor(fs.statSync(mFile).mtimeMs / 1000);
return modificationTimeSec
}


// --- Exports ---

export { delay, randomIntFromInterval, getUserIdFromReq, createTmpFile, mergeArrayOfStrings };
export { delay, randomIntFromInterval, getUserIdFromReq, createTmpFile, mergeArrayOfStrings, createAbsoluteFilePath, getCreationDate, getModificationDate };
2 changes: 1 addition & 1 deletion server/test/suite/config.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ test(`config works properly`, () => {
dotConfig: {
validate: ["method", "params", "response", "events"],
multiUserConnections: 'warn',
supportedSdks: [
supportedOpenRPCs: [
{
cliFlag: null,
cliShortFlag: null,
Expand Down

0 comments on commit 1f4c456

Please sign in to comment.