mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-14 21:27:20 +00:00
chore(server): avoid config object been modified (#11452)
This commit is contained in:
@@ -174,3 +174,14 @@ test('should override correctly', t => {
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
test('should clone from original config without modifications', t => {
|
||||
const config = module.get(Config);
|
||||
const configFactory = module.get(ConfigFactory);
|
||||
|
||||
config.auth.allowSignup = !config.auth.allowSignup;
|
||||
|
||||
const newConfig = configFactory.clone();
|
||||
|
||||
t.not(newConfig.auth.allowSignup, config.auth.allowSignup);
|
||||
});
|
||||
|
||||
@@ -7,7 +7,11 @@ export const OVERRIDE_CONFIG_TOKEN = Symbol('OVERRIDE_CONFIG_TOKEN');
|
||||
|
||||
@Injectable()
|
||||
export class ConfigFactory {
|
||||
#original: AppConfig;
|
||||
readonly #config: AppConfig;
|
||||
get config() {
|
||||
return this.#config;
|
||||
}
|
||||
|
||||
constructor(
|
||||
@Inject(OVERRIDE_CONFIG_TOKEN)
|
||||
@@ -15,18 +19,17 @@ export class ConfigFactory {
|
||||
private readonly overrides: DeepPartial<AppConfig> = {}
|
||||
) {
|
||||
this.#config = this.loadDefault();
|
||||
}
|
||||
|
||||
get config() {
|
||||
return this.#config;
|
||||
this.#original = structuredClone(this.#config);
|
||||
}
|
||||
|
||||
clone() {
|
||||
return structuredClone(this.#config);
|
||||
// we did not freeze the #config object, it might be modified
|
||||
return structuredClone(this.#original);
|
||||
}
|
||||
|
||||
override(updates: DeepPartial<AppConfig>) {
|
||||
override(this.#config, updates);
|
||||
this.#original = structuredClone(this.#config);
|
||||
}
|
||||
|
||||
validate(updates: Array<{ module: string; key: string; value: any }>) {
|
||||
|
||||
Reference in New Issue
Block a user