forked from exceptionless/Exceptionless.JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettingsManager.ts
More file actions
92 lines (77 loc) · 3.02 KB
/
SettingsManager.ts
File metadata and controls
92 lines (77 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { ILog } from "../logging/ILog.js";
import { merge } from "../Utils.js";
import { Configuration } from "./Configuration.js";
export class ClientSettings {
constructor(
public settings: { [key: string]: string },
public version: number
) { }
}
export class SettingsManager {
private static readonly SETTINGS_FILE_NAME: string = "settings.json";
private static _isUpdatingSettings: boolean = false;
public static async applySavedServerSettings(config: Configuration): Promise<void> {
if (!config?.isValid) {
return;
}
const savedSettings = await this.getSavedServerSettings(config);
if (savedSettings) {
config.services.log.trace(`Applying saved settings: v${savedSettings.version}`);
config.settings = merge(config.settings, savedSettings.settings);
config.settingsVersion = savedSettings.version;
}
}
public static async updateSettings(config: Configuration): Promise<void> {
if (!config?.enabled || this._isUpdatingSettings) {
return;
}
this._isUpdatingSettings = true;
const { log } = config.services;
try {
const unableToUpdateMessage = "Unable to update settings";
if (!config.isValid) {
log.error(`${unableToUpdateMessage}: ApiKey is not set`);
return;
}
const version = config.settingsVersion;
log.trace(`Checking for updated settings from: v${version}`);
const response = await config.services.submissionClient.getSettings(version);
if (response.status === 304) {
log.trace("Settings are up-to-date");
return;
}
if (!response?.success || !response.data) {
log.warn(`${unableToUpdateMessage}: ${response.message}`);
return;
}
const data: ClientSettings = JSON.parse(response.data);
log.info(`Updating settings from v${version} to v${data.version}`);
const settings = merge(config.settings, data.settings);
// TODO: Store snapshot of settings after reading from config and attributes and use that to revert to defaults.
// Remove any existing server settings that are not in the new server settings.
const savedServerSettings = await SettingsManager.getSavedServerSettings(config);
for (const key in savedServerSettings || {}) {
if (data.settings[key]) {
continue;
}
delete settings[key];
}
config.settings = settings;
config.settingsVersion = data.version;
await config.services.storage.setItem(SettingsManager.SETTINGS_FILE_NAME, response.data);
log.trace(`Updated settings: v${data.version}`);
} catch (ex) {
log.error(`Error updating settings: ${ex.message}`);
} finally {
this._isUpdatingSettings = false;
}
}
private static async getSavedServerSettings(config: Configuration): Promise<ClientSettings> {
try {
const settings = await config.services.storage.getItem(SettingsManager.SETTINGS_FILE_NAME);
return settings && JSON.parse(settings) as ClientSettings;
} catch {
return null;
}
}
}