2023-08-24 08:39:47 +00:00
|
|
|
import * as plugins from './npmextra.plugins.js';
|
|
|
|
import * as paths from './npmextra.paths.js';
|
|
|
|
import { KeyValueStore } from './npmextra.classes.keyvaluestore.js';
|
|
|
|
|
2024-06-12 18:04:04 +00:00
|
|
|
export interface IAppDataOptions<T = any> {
|
2024-02-07 20:44:00 +00:00
|
|
|
dirPath?: string;
|
2024-06-12 18:04:04 +00:00
|
|
|
requiredKeys?: Array<keyof T>;
|
2024-02-09 14:57:32 +00:00
|
|
|
|
2024-06-19 13:07:49 +00:00
|
|
|
/**
|
|
|
|
* wether keys should be persisted on disk or not
|
|
|
|
*/
|
|
|
|
ephermal?: boolean;
|
|
|
|
|
2024-02-09 14:57:32 +00:00
|
|
|
/**
|
|
|
|
* kvStoreKey: 'MY_ENV_VAR'
|
|
|
|
*/
|
2024-06-19 17:03:26 +00:00
|
|
|
envMapping?: plugins.tsclass.typeFest.PartialDeep<T>;
|
2024-11-05 14:11:01 +00:00
|
|
|
overwriteObject?: plugins.tsclass.typeFest.PartialDeep<T>;
|
2024-02-07 20:44:00 +00:00
|
|
|
}
|
|
|
|
|
2024-02-12 23:48:44 +00:00
|
|
|
export class AppData<T = any> {
|
2024-01-25 12:57:55 +00:00
|
|
|
/**
|
|
|
|
* creates appdata. If no pathArg is given, data will be stored here:
|
|
|
|
* ${PWD}/.nogit/appdata
|
|
|
|
* @param pathArg
|
2024-02-09 14:57:32 +00:00
|
|
|
* @returns
|
2024-01-25 12:57:55 +00:00
|
|
|
*/
|
2024-06-19 17:03:26 +00:00
|
|
|
public static async createAndInit<T = any>(
|
|
|
|
optionsArg: IAppDataOptions<T> = {}
|
|
|
|
): Promise<AppData<T>> {
|
2024-02-12 23:48:44 +00:00
|
|
|
const appData = new AppData<T>(optionsArg);
|
2024-01-25 12:57:55 +00:00
|
|
|
await appData.readyDeferred.promise;
|
2023-08-26 07:56:20 +00:00
|
|
|
return appData;
|
|
|
|
}
|
|
|
|
|
|
|
|
// instance
|
2024-06-12 18:04:04 +00:00
|
|
|
public readyDeferred = plugins.smartpromise.defer<void>();
|
|
|
|
public options: IAppDataOptions<T>;
|
2024-02-12 23:50:50 +00:00
|
|
|
private kvStore: KeyValueStore<T>;
|
2024-06-12 18:04:04 +00:00
|
|
|
|
|
|
|
constructor(optionsArg: IAppDataOptions<T> = {}) {
|
2024-02-07 20:44:00 +00:00
|
|
|
this.options = optionsArg;
|
2024-01-27 18:03:06 +00:00
|
|
|
this.init();
|
2023-08-24 08:39:47 +00:00
|
|
|
}
|
|
|
|
|
2024-01-25 12:57:55 +00:00
|
|
|
/**
|
|
|
|
* inits app data
|
|
|
|
* @param pathArg
|
|
|
|
*/
|
2023-08-24 08:39:47 +00:00
|
|
|
private async init(pathArg?: string) {
|
2024-06-19 13:07:49 +00:00
|
|
|
if (this.options.dirPath || this.options.ephermal) {
|
2024-01-25 12:57:55 +00:00
|
|
|
// ok, nothing to do here;
|
2023-08-24 08:39:47 +00:00
|
|
|
} else {
|
|
|
|
const appDataDir = '/app/data';
|
|
|
|
const dataDir = '/data';
|
2023-08-24 20:59:43 +00:00
|
|
|
const nogitAppData = '.nogit/appdata';
|
2023-08-24 08:39:47 +00:00
|
|
|
const appDataExists = plugins.smartfile.fs.isDirectory(appDataDir);
|
|
|
|
const dataExists = plugins.smartfile.fs.isDirectory(dataDir);
|
|
|
|
if (appDataExists) {
|
2024-02-07 20:44:00 +00:00
|
|
|
this.options.dirPath = appDataDir;
|
2023-08-24 20:59:43 +00:00
|
|
|
} else if (dataExists) {
|
2024-02-07 20:44:00 +00:00
|
|
|
this.options.dirPath = dataDir;
|
2023-08-24 20:59:43 +00:00
|
|
|
} else {
|
|
|
|
await plugins.smartfile.fs.ensureDir(nogitAppData);
|
2024-02-07 20:44:00 +00:00
|
|
|
this.options.dirPath = nogitAppData;
|
2023-08-24 08:39:47 +00:00
|
|
|
}
|
|
|
|
}
|
2024-06-12 18:04:04 +00:00
|
|
|
|
|
|
|
this.kvStore = new KeyValueStore<T>({
|
2024-06-19 13:07:49 +00:00
|
|
|
typeArg: this.options.ephermal ? 'ephemeral' : 'custom',
|
2024-02-12 18:16:43 +00:00
|
|
|
identityArg: 'appkv',
|
|
|
|
customPath: this.options.dirPath,
|
2024-06-19 17:03:26 +00:00
|
|
|
mandatoryKeys: this.options.requiredKeys as Array<keyof T>,
|
2024-02-12 18:16:43 +00:00
|
|
|
});
|
2024-02-09 14:57:32 +00:00
|
|
|
|
|
|
|
if (this.options.envMapping) {
|
2024-06-19 17:03:26 +00:00
|
|
|
const qenvInstance = new plugins.qenv.Qenv(
|
|
|
|
process.cwd(),
|
|
|
|
plugins.path.join(process.cwd(), '.nogit')
|
|
|
|
);
|
2024-06-12 18:04:04 +00:00
|
|
|
|
2024-02-12 19:09:26 +00:00
|
|
|
// Recursive function to handle nested objects, now includes key parameter
|
2024-06-19 17:03:26 +00:00
|
|
|
const processEnvMapping = async (
|
|
|
|
key: keyof T,
|
|
|
|
mappingValue: any,
|
|
|
|
parentKey: keyof T | '' = ''
|
|
|
|
): Promise<any> => {
|
2024-02-12 19:09:26 +00:00
|
|
|
if (typeof mappingValue === 'string') {
|
2024-06-19 16:50:00 +00:00
|
|
|
let envValue: string | boolean | T[keyof T];
|
2024-06-19 17:03:26 +00:00
|
|
|
let convert: 'none' | 'json' | 'base64' | 'boolean' = 'none';
|
|
|
|
switch (true) {
|
|
|
|
case mappingValue.startsWith('hard:'):
|
|
|
|
envValue = mappingValue.replace('hard:', '') as T[keyof T];
|
|
|
|
break;
|
|
|
|
case mappingValue.startsWith('hard_boolean:'):
|
|
|
|
envValue = mappingValue.replace('hard_boolean:', '') === 'true';
|
|
|
|
convert = 'boolean';
|
|
|
|
break;
|
|
|
|
case mappingValue.startsWith('hard_json:'):
|
|
|
|
envValue = JSON.parse(mappingValue.replace('hard_json:', '')) as T[keyof T];
|
|
|
|
convert = 'json';
|
|
|
|
break;
|
|
|
|
case mappingValue.startsWith('hard_base64:'):
|
|
|
|
envValue = Buffer.from(
|
|
|
|
mappingValue.replace('hard_base64:', ''),
|
|
|
|
'base64'
|
|
|
|
).toString() as T[keyof T];
|
|
|
|
convert = 'base64';
|
|
|
|
break;
|
|
|
|
case mappingValue.startsWith('boolean:'):
|
|
|
|
envValue = (await qenvInstance.getEnvVarOnDemand(
|
|
|
|
mappingValue.replace('boolean:', '')
|
|
|
|
)) as T[keyof T];
|
|
|
|
convert = 'boolean';
|
|
|
|
break;
|
|
|
|
case mappingValue.startsWith('json:'):
|
|
|
|
envValue = (await qenvInstance.getEnvVarOnDemand(
|
|
|
|
mappingValue.replace('json:', '')
|
|
|
|
)) as T[keyof T];
|
|
|
|
convert = 'json';
|
|
|
|
break;
|
|
|
|
case mappingValue.startsWith('base64:'):
|
|
|
|
envValue = (await qenvInstance.getEnvVarOnDemand(
|
|
|
|
mappingValue.replace('base64:', '')
|
|
|
|
)) as T[keyof T];
|
|
|
|
convert = 'base64';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
envValue = (await qenvInstance.getEnvVarOnDemand(mappingValue)) as T[keyof T];
|
|
|
|
break;
|
2024-02-13 01:08:59 +00:00
|
|
|
}
|
2024-06-19 16:50:00 +00:00
|
|
|
|
|
|
|
// lets format the env value
|
2024-02-12 19:09:26 +00:00
|
|
|
if (envValue) {
|
2024-06-19 17:03:26 +00:00
|
|
|
if (typeof envValue === 'string' && convert === 'boolean') {
|
2024-06-19 16:50:00 +00:00
|
|
|
envValue = envValue === 'true';
|
|
|
|
}
|
2024-06-19 17:03:26 +00:00
|
|
|
if (
|
|
|
|
typeof envValue === 'string' &&
|
|
|
|
(mappingValue.endsWith('_JSON') || convert === 'json')
|
|
|
|
) {
|
2024-06-19 16:50:00 +00:00
|
|
|
envValue = JSON.parse(envValue as string) as T[keyof T];
|
|
|
|
}
|
2024-06-19 17:03:26 +00:00
|
|
|
if (
|
|
|
|
typeof envValue === 'string' &&
|
|
|
|
(mappingValue.endsWith('_BASE64') || convert === 'base64')
|
|
|
|
) {
|
2024-06-19 16:50:00 +00:00
|
|
|
envValue = Buffer.from(envValue as string, 'base64').toString();
|
2024-02-12 19:09:26 +00:00
|
|
|
}
|
2024-02-13 01:01:39 +00:00
|
|
|
if (!parentKey) {
|
2024-06-19 16:50:00 +00:00
|
|
|
await this.kvStore.writeKey(key, envValue as any);
|
2024-02-13 01:01:39 +00:00
|
|
|
} else {
|
2024-02-13 01:10:21 +00:00
|
|
|
return envValue;
|
2024-02-13 01:01:39 +00:00
|
|
|
}
|
2024-02-13 01:08:59 +00:00
|
|
|
} else {
|
2024-02-13 01:10:21 +00:00
|
|
|
return undefined;
|
2024-02-12 19:09:26 +00:00
|
|
|
}
|
|
|
|
} else if (typeof mappingValue === 'object' && mappingValue !== null) {
|
2024-06-12 18:04:04 +00:00
|
|
|
const resultObject: Partial<T> = {};
|
2024-02-12 19:09:26 +00:00
|
|
|
for (const innerKey in mappingValue) {
|
|
|
|
const nestedValue = mappingValue[innerKey];
|
|
|
|
// For nested objects, call recursively but do not immediately write to kvStore
|
2024-06-12 18:04:04 +00:00
|
|
|
const nestedResult = await processEnvMapping(innerKey as keyof T, nestedValue, key);
|
|
|
|
resultObject[innerKey as keyof T] = nestedResult;
|
2024-02-12 19:09:26 +00:00
|
|
|
}
|
|
|
|
if (parentKey === '') {
|
|
|
|
// Only write to kvStore if at the top level
|
2024-06-12 18:04:04 +00:00
|
|
|
await this.kvStore.writeKey(key, resultObject as T[keyof T]);
|
2024-02-12 19:09:26 +00:00
|
|
|
} else {
|
|
|
|
// For nested objects, return the constructed object instead of writing to kvStore
|
|
|
|
return resultObject;
|
2024-02-12 17:40:01 +00:00
|
|
|
}
|
2024-02-09 14:57:32 +00:00
|
|
|
}
|
2024-02-12 19:09:26 +00:00
|
|
|
};
|
2024-06-12 18:04:04 +00:00
|
|
|
|
2024-02-12 19:09:26 +00:00
|
|
|
for (const key in this.options.envMapping) {
|
2024-06-12 18:04:04 +00:00
|
|
|
await processEnvMapping(key as keyof T, this.options.envMapping[key]);
|
2024-02-09 14:57:32 +00:00
|
|
|
}
|
2024-11-05 14:11:01 +00:00
|
|
|
|
2024-11-05 20:29:26 +00:00
|
|
|
if (this.options.overwriteObject) {
|
2024-11-06 02:48:39 +00:00
|
|
|
for (const key of Object.keys(this.options.overwriteObject)) {
|
2024-11-05 20:29:26 +00:00
|
|
|
console.log(`-> heads up: overwriting key ${key} from options.overwriteObject`);
|
|
|
|
await this.kvStore.writeKey(key as keyof T, this.options.overwriteObject[key]);
|
|
|
|
}
|
2024-11-05 14:11:01 +00:00
|
|
|
}
|
2024-02-09 14:57:32 +00:00
|
|
|
}
|
|
|
|
|
2023-08-24 08:39:47 +00:00
|
|
|
this.readyDeferred.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-06-12 18:04:04 +00:00
|
|
|
* returns a kvstore that resides in appdata
|
2023-08-24 08:39:47 +00:00
|
|
|
*/
|
2024-06-12 18:04:04 +00:00
|
|
|
public async getKvStore(): Promise<KeyValueStore<T>> {
|
2023-08-24 08:39:47 +00:00
|
|
|
await this.readyDeferred.promise;
|
|
|
|
return this.kvStore;
|
|
|
|
}
|
2024-02-09 10:52:30 +00:00
|
|
|
|
2024-06-12 18:04:04 +00:00
|
|
|
public async logMissingKeys(): Promise<Array<keyof T>> {
|
2024-02-09 10:52:30 +00:00
|
|
|
const kvStore = await this.getKvStore();
|
2024-04-14 00:10:29 +00:00
|
|
|
const missingMandatoryKeys = await kvStore.getMissingMandatoryKeys();
|
2024-02-09 10:52:30 +00:00
|
|
|
if (missingMandatoryKeys.length > 0) {
|
2024-02-09 14:57:32 +00:00
|
|
|
console.log(
|
|
|
|
`The following mandatory keys are missing in the appdata:\n -> ${missingMandatoryKeys.join(
|
|
|
|
',\n -> '
|
|
|
|
)}`
|
|
|
|
);
|
2024-02-09 10:52:30 +00:00
|
|
|
} else {
|
|
|
|
console.log('All mandatory keys are present in the appdata');
|
|
|
|
}
|
2024-02-13 01:04:04 +00:00
|
|
|
return missingMandatoryKeys;
|
2024-02-09 10:52:30 +00:00
|
|
|
}
|
2024-02-10 03:54:00 +00:00
|
|
|
|
2024-06-12 18:04:04 +00:00
|
|
|
public async waitForAndGetKey<K extends keyof T>(keyArg: K): Promise<T[K] | undefined> {
|
2024-02-10 03:54:00 +00:00
|
|
|
await this.readyDeferred.promise;
|
2024-02-10 03:55:50 +00:00
|
|
|
await this.kvStore.waitForKeysPresent([keyArg]);
|
2024-06-12 18:04:04 +00:00
|
|
|
return this.kvStore.readKey(keyArg);
|
2024-02-10 03:54:00 +00:00
|
|
|
}
|
2024-06-19 17:03:26 +00:00
|
|
|
}
|