Files
npmextra/ts/npmextra.classes.appdata.ts

220 lines
7.5 KiB
TypeScript
Raw Permalink Normal View History

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