chore(server): avoid config object been modified (#11452)

This commit is contained in:
forehalo
2025-04-03 12:57:29 +00:00
parent 6939e80827
commit 2533a92873
5 changed files with 22 additions and 8 deletions

View File

@@ -866,7 +866,7 @@
}, },
"stripe": { "stripe": {
"type": "object", "type": "object",
"description": "Stripe API keys\n@default {}\n@link https://docs.stripe.com/api", "description": "Stripe sdk options\n@default {}\n@link https://docs.stripe.com/api",
"default": {} "default": {}
} }
} }

View File

@@ -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);
});

View File

@@ -7,7 +7,11 @@ export const OVERRIDE_CONFIG_TOKEN = Symbol('OVERRIDE_CONFIG_TOKEN');
@Injectable() @Injectable()
export class ConfigFactory { export class ConfigFactory {
#original: AppConfig;
readonly #config: AppConfig; readonly #config: AppConfig;
get config() {
return this.#config;
}
constructor( constructor(
@Inject(OVERRIDE_CONFIG_TOKEN) @Inject(OVERRIDE_CONFIG_TOKEN)
@@ -15,18 +19,17 @@ export class ConfigFactory {
private readonly overrides: DeepPartial<AppConfig> = {} private readonly overrides: DeepPartial<AppConfig> = {}
) { ) {
this.#config = this.loadDefault(); this.#config = this.loadDefault();
} this.#original = structuredClone(this.#config);
get config() {
return this.#config;
} }
clone() { 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(updates: DeepPartial<AppConfig>) {
override(this.#config, updates); override(this.#config, updates);
this.#original = structuredClone(this.#config);
} }
validate(updates: Array<{ module: string; key: string; value: any }>) { validate(updates: Array<{ module: string; key: string; value: any }>) {

View File

@@ -47,7 +47,7 @@ defineModuleConfig('payment', {
env: 'STRIPE_WEBHOOK_KEY', env: 'STRIPE_WEBHOOK_KEY',
}, },
stripe: { stripe: {
desc: 'Stripe API keys', desc: 'Stripe sdk options',
default: {}, default: {},
link: 'https://docs.stripe.com/api', link: 'https://docs.stripe.com/api',
}, },

View File

@@ -293,7 +293,7 @@
}, },
"stripe": { "stripe": {
"type": "Object", "type": "Object",
"desc": "Stripe API keys", "desc": "Stripe sdk options",
"link": "https://docs.stripe.com/api" "link": "https://docs.stripe.com/api"
} }
}, },