Skip to content

Commit

Permalink
fix: secret key transfer to nested config
Browse files Browse the repository at this point in the history
  • Loading branch information
npenin committed Dec 29, 2023
1 parent f550259 commit 77702ce
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions packages/config/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,8 @@ async function aesDecrypt(ciphertext: BufferSource, key: CryptoKey, iv: BufferSo

export default class Configuration<T extends object = SerializableObject>
{
private cryptKey?: CryptoKey;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
private constructor(private readonly path: string, private readonly config?: T, private readonly rootConfig?: any)
private constructor(private readonly path: string, private readonly config?: T, private readonly rootConfig?: any, private cryptKey?: CryptoKey)
{
if (typeof config == 'undefined')
config = {} as unknown as T;
Expand All @@ -70,9 +68,9 @@ export default class Configuration<T extends object = SerializableObject>
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
public static new<T extends object = SerializableObject>(path: string, config?: T, rootConfig?: any): ProxyConfigurationObjectNonArray<T>
public static new<T extends object = SerializableObject>(path: string, config?: T, rootConfig?: any, cryptKey?: CryptoKey): ProxyConfigurationObjectNonArray<T>
{
return new Proxy(new Configuration<T>(path, config, rootConfig), {
return new Proxy(new Configuration<T>(path, config, rootConfig, cryptKey), {
has(target, key)
{
if (typeof (key) == 'symbol')
Expand Down Expand Up @@ -201,8 +199,7 @@ export default class Configuration<T extends object = SerializableObject>

await fs.writeFile(file, content = '{}');
}
const config = Configuration.new(file, JSON.parse(content));
config.cryptKey = cryptKey;
const config = Configuration.new(file, JSON.parse(content), undefined, cryptKey);
if (needle)
{
const needleConfig = config.get<string, T>(needle);
Expand All @@ -228,7 +225,7 @@ export default class Configuration<T extends object = SerializableObject>
}, this.config);
if (typeof value == 'object' && !Array.isArray(value))
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return Configuration.new(this.path, value as SerializableObject, this.rootConfig) as any;
return Configuration.new(this.path, value as SerializableObject, this.rootConfig, this.cryptKey) as any;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return value as any
;
Expand Down

0 comments on commit 77702ce

Please sign in to comment.