Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support sub configuration to be immutable to #19

Merged
merged 2 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
"pretty-quick": "^3.1.0",
"ts-jest": "^29.1.5",
"typescript": "^5.4.5",
"prom-client": "^15.0.0"
"prom-client": "^15.0.0",
"jest-extended": "^4.0.2"
}
}
3 changes: 2 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { createDebug } from './utils/debug';
import { LOCAL_SCHEMAS_PACKAGE_VERSION } from './constants';
import { createConfigError } from './errors';
import { initializeMetrics as initializeMetricsInternal } from './metrics';
import { deepFreeze } from './utils/helpers';

const debug = createDebug('config');

Expand Down Expand Up @@ -101,7 +102,7 @@ export async function config<T extends { [typeSymbol]: unknown; $id: string }>(

debug('freezing validated config');
// freeze the merged config so it can't be modified by the package user
Object.freeze(validatedConfig);
deepFreeze(validatedConfig);

function get<TPath extends string>(path: TPath): GetFieldType<T[typeof typeSymbol], TPath> {
debug('get called with path: %s', path);
Expand Down
10 changes: 10 additions & 0 deletions src/utils/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function deepFreeze<T>(obj: T): void {
const propNames = Object.getOwnPropertyNames(obj);
for (const name of propNames) {
const value = obj[name as keyof T];
if (typeof value === 'object') {
deepFreeze(value);
}
}
Object.freeze(obj);
}
53 changes: 52 additions & 1 deletion tests/config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ describe('config', () => {
});

const conf = configInstance.getAll();

expect(conf).toEqual({
host: 'avi',
port: 5432,
Expand Down Expand Up @@ -303,5 +302,57 @@ describe('config', () => {

await expect(promise).rejects.toThrow();
});

it('should not allow to modify the root object', async () => {
const configInstance = await config({
configName: 'name',
version: 1,
schema: commonDbPartialV1,
configServerUrl: URL,
localConfigPath: './tests/config',
offlineMode: true,
});

const conf = configInstance.getAll();
const action = () => {
conf.host = 'dummmy.host.check';
};
expect(action).toThrow(/Cannot assign to read only property/);
});

it('should not allow to modify the child object', async () => {
const configInstance = await config({
configName: 'name',
version: 1,
schema: commonDbPartialV1,
configServerUrl: URL,
localConfigPath: './tests/config',
offlineMode: true,
});

const conf = configInstance.get('ssl');
const action = () => {
conf.enabled = true; // try to change ssl configuration
};
expect(action).toThrow(/Cannot assign to read only property/);
});

it('should not allow to modify the extracted child object from root', async () => {
const configInstance = await config({
configName: 'name',
version: 1,
schema: commonDbPartialV1,
configServerUrl: URL,
localConfigPath: './tests/config',
offlineMode: true,
});

const conf = configInstance.getAll();
const sslConf = conf.ssl;
const action = () => {
sslConf.enabled = true;
};
expect(action).toThrow(/Cannot assign to read only property/);
});
});
});
1 change: 1 addition & 0 deletions tests/configurations/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = {
coverageDirectory: '<rootDir>/coverage',
rootDir: '../../.',
testMatch: ['<rootDir>/tests/**/*.spec.ts'],
setupFilesAfterEnv: ['jest-extended/all'],
// setupFiles: ['<rootDir>/tests/configurations/jest.setup.ts'],
// setupFiles: ['<rootDir>/tests/configurations/afterEnv.setup.ts'],
// reporters: [
Expand Down