From 3380fef63ae3366be357202f550789e455415264 Mon Sep 17 00:00:00 2001 From: "Patrik J. Braun" Date: Fri, 12 Apr 2024 23:16:59 +0200 Subject: [PATCH] Fix default loading flakyness before first json load. --- package-lock.json | 14 +++++----- package.json | 2 +- .../model/extension/ExtensionConfigWrapper.ts | 14 ++++++++++ .../extension/ExtensionConfigWrapper.spec.ts | 26 +++++++++++++++++++ 4 files changed, 48 insertions(+), 8 deletions(-) create mode 100644 test/backend/unit/model/extension/ExtensionConfigWrapper.spec.ts diff --git a/package-lock.json b/package-lock.json index 06dc3a97..46ee58e8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -27,7 +27,7 @@ "reflect-metadata": "0.1.13", "sharp": "0.31.3", "ts-node-iptc": "1.0.11", - "typeconfig": "2.2.11", + "typeconfig": "2.2.13", "typeorm": "0.3.12", "xml2js": "0.6.2" }, @@ -20361,9 +20361,9 @@ } }, "node_modules/typeconfig": { - "version": "2.2.11", - "resolved": "https://registry.npmjs.org/typeconfig/-/typeconfig-2.2.11.tgz", - "integrity": "sha512-Knj+1kbIJ4zOZlUm2TPSWZUoiOW4txrmPyf6oyuBhaDQDlGxpSL5jobF3vVV9mZElK1V3ZQVeTgvGaiDyeT8mQ==", + "version": "2.2.13", + "resolved": "https://registry.npmjs.org/typeconfig/-/typeconfig-2.2.13.tgz", + "integrity": "sha512-eT9FqQVJTacuJELZ2XKN9s1phUnaceQd1NhzgTHZuULDWSOpcMTw8jRvg2Uyp14IRe9W9N8DOItz38QQJcwctQ==", "dependencies": { "minimist": "1.2.8" } @@ -35280,9 +35280,9 @@ } }, "typeconfig": { - "version": "2.2.11", - "resolved": "https://registry.npmjs.org/typeconfig/-/typeconfig-2.2.11.tgz", - "integrity": "sha512-Knj+1kbIJ4zOZlUm2TPSWZUoiOW4txrmPyf6oyuBhaDQDlGxpSL5jobF3vVV9mZElK1V3ZQVeTgvGaiDyeT8mQ==", + "version": "2.2.13", + "resolved": "https://registry.npmjs.org/typeconfig/-/typeconfig-2.2.13.tgz", + "integrity": "sha512-eT9FqQVJTacuJELZ2XKN9s1phUnaceQd1NhzgTHZuULDWSOpcMTw8jRvg2Uyp14IRe9W9N8DOItz38QQJcwctQ==", "requires": { "minimist": "1.2.8" } diff --git a/package.json b/package.json index 65e65868..c6281a54 100644 --- a/package.json +++ b/package.json @@ -54,7 +54,7 @@ "reflect-metadata": "0.1.13", "sharp": "0.31.3", "ts-node-iptc": "1.0.11", - "typeconfig": "2.2.11", + "typeconfig": "2.2.13", "typeorm": "0.3.12", "xml2js": "0.6.2" }, diff --git a/src/backend/model/extension/ExtensionConfigWrapper.ts b/src/backend/model/extension/ExtensionConfigWrapper.ts index eaa7270f..c4e0e827 100644 --- a/src/backend/model/extension/ExtensionConfigWrapper.ts +++ b/src/backend/model/extension/ExtensionConfigWrapper.ts @@ -3,6 +3,9 @@ import {PrivateConfigClass} from '../../../common/config/private/PrivateConfigCl import {ConfigClassBuilder} from 'typeconfig/node'; import {ExtensionConfigTemplateLoader} from './ExtensionConfigTemplateLoader'; import {NotificationManager} from '../NotifocationManager'; +import {ServerConfig} from '../../../common/config/private/PrivateConfig'; +import {ConfigClassOptions} from 'typeconfig/src/decorators/class/IConfigClass'; +import * as fs from 'fs'; const LOG_TAG = '[ExtensionConfigWrapper]'; @@ -16,6 +19,12 @@ export class ExtensionConfigWrapper { const pc = ConfigClassBuilder.attachPrivateInterface(new PrivateConfigClass()); ExtensionConfigTemplateLoader.Instance.loadExtensionTemplates(pc); try { + // make sure the config file exists by the time we load it. + // TODO: remove this once typeconfig is fixed and can properly load defaults in arrays + if (!fs.existsSync((pc.__options as ConfigClassOptions).configPath)) { + await pc.save(); + } + await pc.load(); // loading the basic configs, but we do not know the extension config hierarchy yet } catch (e) { @@ -34,6 +43,11 @@ export class ExtensionConfigWrapper { const pc = ConfigClassBuilder.attachPrivateInterface(new PrivateConfigClass()); ExtensionConfigTemplateLoader.Instance.loadExtensionTemplates(pc); try { + // make sure the config file exists by the time we load it. + // TODO: remove this once typeconfig is fixed and can properly load defaults in arrays + if (!fs.existsSync((pc.__options as ConfigClassOptions).configPath)) { + pc.saveSync(); + } pc.loadSync(); // loading the basic configs, but we do not know the extension config hierarchy yet } catch (e) { diff --git a/test/backend/unit/model/extension/ExtensionConfigWrapper.spec.ts b/test/backend/unit/model/extension/ExtensionConfigWrapper.spec.ts new file mode 100644 index 00000000..52b9facc --- /dev/null +++ b/test/backend/unit/model/extension/ExtensionConfigWrapper.spec.ts @@ -0,0 +1,26 @@ +import {expect} from 'chai'; +import {ExtensionConfigWrapper} from '../../../../../src/backend/model/extension/ExtensionConfigWrapper'; +import {TAGS} from '../../../../../src/common/config/public/ClientConfig'; + +// to help WebStorm to handle the test cases +declare let describe: any; +declare const after: any; +declare const before: any; +declare const it: any; + + +describe('ExtensionConfigWrapper', () => { + + it('should load original config multiple times with the same result', async () => { + const get = async () => JSON.parse(JSON.stringify((await ExtensionConfigWrapper.original()).toJSON({ + attachState: true, + attachVolatile: true, + skipTags: {secret: true} as TAGS + }))); + const a = await get(); + const b = await get(); + expect(b).to.deep.equal(a); + const c = await get(); + expect(c).to.deep.equal(a); + }); +});