From cb2bcb07c87b9fefcb861cb56f0548c0ed2289f7 Mon Sep 17 00:00:00 2001 From: Vinicius Maia Date: Thu, 18 Jan 2024 21:51:51 -0700 Subject: [PATCH 1/5] Tweaks on the reload interval logic and fixes on script-esm-ts example --- examples/script-esm-ts/index.ts | 10 ++++++++-- examples/script-esm-ts/tsconfig.json | 11 ++++++++++- packages/config-dug/src/config-dug.ts | 19 ++++++++++++++----- packages/config-dug/src/lib/options.ts | 4 ++++ 4 files changed, 36 insertions(+), 8 deletions(-) diff --git a/examples/script-esm-ts/index.ts b/examples/script-esm-ts/index.ts index 505c5c4..1b27aee 100644 --- a/examples/script-esm-ts/index.ts +++ b/examples/script-esm-ts/index.ts @@ -4,6 +4,12 @@ const schema = { logLevel: z.string().default('info'), }; -const config = await ConfigDug.getConfig(schema); +const configDug = new ConfigDug(schema, { reloadInterval: 20000 }); -console.log('logLevel', config.logLevel); +await configDug.load(); + +console.log('logLevel', configDug.getConfig().logLevel); + +configDug.on('config-reloaded', (config) => { + console.log('\n\nconfig-reloaded event received', config); +}); diff --git a/examples/script-esm-ts/tsconfig.json b/examples/script-esm-ts/tsconfig.json index 5fe44a1..cf5cac3 100644 --- a/examples/script-esm-ts/tsconfig.json +++ b/examples/script-esm-ts/tsconfig.json @@ -1,8 +1,17 @@ { - "extends": "@tsconfig/node18", "ts-node": { "swc": true, "esm": true }, + "compilerOptions": { + "lib": ["es2022"], + "module": "ES2022", + "target": "ES2022", + "strict": true, + "esModuleInterop": true, + "skipLibCheck": true, + "forceConsistentCasingInFileNames": true, + "moduleResolution": "node" + }, "include": ["./**/*"] } diff --git a/packages/config-dug/src/config-dug.ts b/packages/config-dug/src/config-dug.ts index b5dd1d3..ee750e7 100644 --- a/packages/config-dug/src/config-dug.ts +++ b/packages/config-dug/src/config-dug.ts @@ -63,7 +63,6 @@ class ConfigDug extends EventEmitter { private rawValues: UntypedConfig = {}; private validatedValues: ConfigDugConfig | undefined; private valueOrigins: ValueOrigins = {}; - private reloadTimeout?: NodeJS.Timeout; private pluginsInitialized = false; private loaded = false; @@ -248,6 +247,9 @@ class ConfigDug extends EventEmitter { private async loadPlugins(): Promise { let values: UntypedConfig = {}; + const { minReloadInterval, reloadInterval } = this.options; + let nextReloadIn: number = reloadInterval; + for (const plugin of this.options.plugins) { const pluginReturnValue: ConfigDugPluginOutput = await plugin.load(); @@ -255,13 +257,20 @@ class ConfigDug extends EventEmitter { this.valueOrigins = mergeOrigins(this.valueOrigins, pluginReturnValue.valueOrigins); - if (pluginReturnValue.nextReloadIn) { - this.reloadTimeout = setTimeout(async () => { - await this.reload(); - }, pluginReturnValue.nextReloadIn); + const pluginNextReloadIn = pluginReturnValue.nextReloadIn; + + if (pluginNextReloadIn && pluginNextReloadIn < nextReloadIn) { + nextReloadIn = pluginNextReloadIn; } } + setTimeout( + async () => { + await this.reload(); + }, + nextReloadIn >= minReloadInterval ? nextReloadIn : minReloadInterval + ); + debug('plugin values', values); return values; diff --git a/packages/config-dug/src/lib/options.ts b/packages/config-dug/src/lib/options.ts index c1640a3..f8f116f 100644 --- a/packages/config-dug/src/lib/options.ts +++ b/packages/config-dug/src/lib/options.ts @@ -14,6 +14,8 @@ const optionsSchema = z printConfig: z.boolean().optional(), strict: z.boolean().optional(), warnOnLocalConfigFile: z.boolean().optional(), + eloadInterval: z.number().optional(), + minReloadInterval: z.number().optional(), }) .strict(); @@ -26,6 +28,8 @@ const optionsWithDefaultsSchema = z.object({ printConfig: z.boolean().default(false), strict: z.boolean().default(false), warnOnLocalConfigFile: z.boolean().default(true), + minReloadInterval: z.number().default(10000), + reloadInterval: z.number().default(30000), }); const getOptions = (options: ConfigDugOptions): ConfigDugOptionsWithDefaults => { From c35852f68e484d90f57f274b8cb8feb97ebaca58 Mon Sep 17 00:00:00 2001 From: Vinicius Maia Date: Sat, 20 Jan 2024 12:50:58 -0700 Subject: [PATCH 2/5] fixed typo on config name --- packages/config-dug/src/config-dug.ts | 1 + packages/config-dug/src/lib/options.ts | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/config-dug/src/config-dug.ts b/packages/config-dug/src/config-dug.ts index ee750e7..3d10e26 100644 --- a/packages/config-dug/src/config-dug.ts +++ b/packages/config-dug/src/config-dug.ts @@ -248,6 +248,7 @@ class ConfigDug extends EventEmitter { let values: UntypedConfig = {}; const { minReloadInterval, reloadInterval } = this.options; + let nextReloadIn: number = reloadInterval; for (const plugin of this.options.plugins) { diff --git a/packages/config-dug/src/lib/options.ts b/packages/config-dug/src/lib/options.ts index f8f116f..f90acab 100644 --- a/packages/config-dug/src/lib/options.ts +++ b/packages/config-dug/src/lib/options.ts @@ -14,7 +14,7 @@ const optionsSchema = z printConfig: z.boolean().optional(), strict: z.boolean().optional(), warnOnLocalConfigFile: z.boolean().optional(), - eloadInterval: z.number().optional(), + reloadInterval: z.number().optional(), minReloadInterval: z.number().optional(), }) .strict(); @@ -28,8 +28,8 @@ const optionsWithDefaultsSchema = z.object({ printConfig: z.boolean().default(false), strict: z.boolean().default(false), warnOnLocalConfigFile: z.boolean().default(true), + reloadInterval: z.number().default(60000), minReloadInterval: z.number().default(10000), - reloadInterval: z.number().default(30000), }); const getOptions = (options: ConfigDugOptions): ConfigDugOptionsWithDefaults => { From 27a76396a8e707cf9553dcf018f205e01589f457 Mon Sep 17 00:00:00 2001 From: Vinicius Maia Date: Sat, 20 Jan 2024 20:48:59 -0700 Subject: [PATCH 3/5] minor tweaks to make sure service esm ts works with the aws plugin --- examples/service-esm-ts/tsconfig.json | 3 +++ packages/plugin-aws-secrets-manager/package.json | 1 + 2 files changed, 4 insertions(+) diff --git a/examples/service-esm-ts/tsconfig.json b/examples/service-esm-ts/tsconfig.json index 188d5ce..deb3e53 100644 --- a/examples/service-esm-ts/tsconfig.json +++ b/examples/service-esm-ts/tsconfig.json @@ -4,5 +4,8 @@ "swc": true, "esm": true }, + "compilerOptions": { + "module": "ES2022" + }, "include": ["./src/**/*"] } diff --git a/packages/plugin-aws-secrets-manager/package.json b/packages/plugin-aws-secrets-manager/package.json index 78d57c3..bdd4368 100644 --- a/packages/plugin-aws-secrets-manager/package.json +++ b/packages/plugin-aws-secrets-manager/package.json @@ -3,6 +3,7 @@ "version": "1.0.0-alpha.0", "author": "Neo Financial Engineering ", "license": "MIT", + "type": "module", "engines": { "node": ">=18.0.0" }, From 21fe54702d2c37486d5976ad0812a15ca8a554f6 Mon Sep 17 00:00:00 2001 From: Vinicius Maia Date: Sat, 20 Jan 2024 20:51:09 -0700 Subject: [PATCH 4/5] Undoing unnecessary changes --- examples/script-esm-ts/index.ts | 10 ++-------- examples/script-esm-ts/tsconfig.json | 11 +---------- 2 files changed, 3 insertions(+), 18 deletions(-) diff --git a/examples/script-esm-ts/index.ts b/examples/script-esm-ts/index.ts index 1b27aee..505c5c4 100644 --- a/examples/script-esm-ts/index.ts +++ b/examples/script-esm-ts/index.ts @@ -4,12 +4,6 @@ const schema = { logLevel: z.string().default('info'), }; -const configDug = new ConfigDug(schema, { reloadInterval: 20000 }); +const config = await ConfigDug.getConfig(schema); -await configDug.load(); - -console.log('logLevel', configDug.getConfig().logLevel); - -configDug.on('config-reloaded', (config) => { - console.log('\n\nconfig-reloaded event received', config); -}); +console.log('logLevel', config.logLevel); diff --git a/examples/script-esm-ts/tsconfig.json b/examples/script-esm-ts/tsconfig.json index cf5cac3..5fe44a1 100644 --- a/examples/script-esm-ts/tsconfig.json +++ b/examples/script-esm-ts/tsconfig.json @@ -1,17 +1,8 @@ { + "extends": "@tsconfig/node18", "ts-node": { "swc": true, "esm": true }, - "compilerOptions": { - "lib": ["es2022"], - "module": "ES2022", - "target": "ES2022", - "strict": true, - "esModuleInterop": true, - "skipLibCheck": true, - "forceConsistentCasingInFileNames": true, - "moduleResolution": "node" - }, "include": ["./**/*"] } From 94aed9540e641e55454851170ea880826d2b62f6 Mon Sep 17 00:00:00 2001 From: Vinicius Rocha Maia Date: Tue, 30 Jan 2024 11:35:19 -0300 Subject: [PATCH 5/5] Moved reload interval set up to main load config function --- packages/config-dug/src/config-dug.ts | 33 +++++++++++++++------------ 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/packages/config-dug/src/config-dug.ts b/packages/config-dug/src/config-dug.ts index 3d10e26..432c1a8 100644 --- a/packages/config-dug/src/config-dug.ts +++ b/packages/config-dug/src/config-dug.ts @@ -155,11 +155,13 @@ class ConfigDug extends EventEmitter { this.pluginsInitialized = true; } + const { values: pluginValues, nextReloadIn } = await this.loadPlugins(); + this.valueOrigins = {}; this.rawValues = { ...(await this.loadConfigFile('config.default')), ...(await this.loadConfigFile(`config.${environmentName}`)), - ...(await this.loadPlugins()), + ...pluginValues, ...(await this.loadLocalConfigFile(`config.${environmentName}.local`)), ...(await this.loadLocalConfigFile('config.local')), ...this.loadEnvironment(Object.keys(this.schema)), @@ -178,9 +180,22 @@ class ConfigDug extends EventEmitter { debug('load validated values', this.validatedValues); + this.setReloadInterval(nextReloadIn); + this.loaded = true; } + private setReloadInterval(nextReloadIn: number): void { + const { minReloadInterval } = this.options; + + setTimeout( + async () => { + await this.reload(); + }, + nextReloadIn >= minReloadInterval ? nextReloadIn : minReloadInterval + ); + } + private async loadConfigFile(filename: string): Promise { if (this.options.loadConfigFiles) { debug('load config file', filename); @@ -244,12 +259,9 @@ class ConfigDug extends EventEmitter { } } - private async loadPlugins(): Promise { + private async loadPlugins(): Promise<{ values: UntypedConfig; nextReloadIn: number }> { let values: UntypedConfig = {}; - - const { minReloadInterval, reloadInterval } = this.options; - - let nextReloadIn: number = reloadInterval; + let nextReloadIn: number = this.options.reloadInterval; for (const plugin of this.options.plugins) { const pluginReturnValue: ConfigDugPluginOutput = await plugin.load(); @@ -265,16 +277,9 @@ class ConfigDug extends EventEmitter { } } - setTimeout( - async () => { - await this.reload(); - }, - nextReloadIn >= minReloadInterval ? nextReloadIn : minReloadInterval - ); - debug('plugin values', values); - return values; + return { values, nextReloadIn }; } private notLoadedError(): Error {