-
Notifications
You must be signed in to change notification settings - Fork 95
/
gmpsettings.js
183 lines (155 loc) · 4.66 KB
/
gmpsettings.js
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* SPDX-FileCopyrightText: 2024 Greenbone AG
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import {isDefined} from './utils/identity';
export const DEFAULT_RELOAD_INTERVAL = 15 * 1000; // fifteen seconds
export const DEFAULT_RELOAD_INTERVAL_ACTIVE = 3 * 1000; // three seconds
export const DEFAULT_RELOAD_INTERVAL_INACTIVE = 60 * 1000; // one minute
export const DEFAULT_MANUAL_URL = `https://docs.greenbone.net/GSM-Manual/gos-22.04/`;
export const DEFAULT_PROTOCOLDOC_URL = `https://docs.greenbone.net/API/GMP/gmp-22.5.html`;
export const DEFAULT_REPORT_RESULTS_THRESHOLD = 25000;
export const DEFAULT_LOG_LEVEL = 'warn';
export const DEFAULT_TIMEOUT = 300000; // 5 minutes
const set = (storage, name, value) => {
if (isDefined(value)) {
storage.setItem(name, value);
} else {
storage.removeItem(name);
}
};
const setAndFreeze = (obj, name, value) => {
Object.defineProperty(obj, name, {
value: value,
writable: false,
});
};
const warnDeprecatedSetting = (oldName, newName) => {
// eslint-disable-next-line no-console
console.warn(
'A deprecated setting',
oldName,
'is used. Please use',
newName,
'instead.',
);
};
class GmpSettings {
constructor(storage = global.localStorage, options = {}) {
const {
enableEPSS = false,
enableGreenboneSensor = false,
disableLoginForm = false,
enableStoreDebugLog,
guestUsername,
guestPassword,
loglevel,
manualUrl = DEFAULT_MANUAL_URL,
manualLanguageMapping,
protocol,
protocolDocUrl = DEFAULT_PROTOCOLDOC_URL,
reloadInterval = DEFAULT_RELOAD_INTERVAL,
reloadIntervalActive = DEFAULT_RELOAD_INTERVAL_ACTIVE,
reloadIntervalInactive = DEFAULT_RELOAD_INTERVAL_INACTIVE,
reportResultsThreshold = DEFAULT_REPORT_RESULTS_THRESHOLD,
server,
timeout = DEFAULT_TIMEOUT,
vendorVersion,
vendorLabel,
} = options;
let {
apiProtocol = protocol,
apiServer = server,
logLevel = loglevel,
} = options;
this.storage = storage;
if (isDefined(loglevel)) {
warnDeprecatedSetting('loglevel', 'logLevel');
}
if (isDefined(server)) {
warnDeprecatedSetting('server', 'apiServer');
}
if (isDefined(protocol)) {
warnDeprecatedSetting('protocol', 'apiProtocol');
}
if (isDefined(enableStoreDebugLog)) {
this.enableStoreDebugLog = enableStoreDebugLog;
}
if (!isDefined(logLevel)) {
logLevel = storage.logLevel;
}
if (!isDefined(logLevel)) {
logLevel = DEFAULT_LOG_LEVEL;
}
if (!isDefined(apiProtocol)) {
apiProtocol = global.location.protocol;
}
if (!isDefined(apiServer)) {
apiServer = global.location.host;
}
this.logLevel = logLevel;
this.reloadInterval = reloadInterval;
this.reloadIntervalActive = reloadIntervalActive;
this.reloadIntervalInactive = reloadIntervalInactive;
this.reportResultsThreshold = reportResultsThreshold;
this.timeout = timeout;
setAndFreeze(this, 'apiProtocol', apiProtocol);
setAndFreeze(this, 'apiServer', apiServer);
setAndFreeze(this, 'disableLoginForm', disableLoginForm);
setAndFreeze(this, 'enableEPSS', enableEPSS);
setAndFreeze(this, 'enableGreenboneSensor', enableGreenboneSensor);
setAndFreeze(this, 'guestUsername', guestUsername);
setAndFreeze(this, 'guestPassword', guestPassword);
setAndFreeze(this, 'manualUrl', manualUrl);
setAndFreeze(this, 'manualLanguageMapping', manualLanguageMapping);
setAndFreeze(this, 'protocolDocUrl', protocolDocUrl);
setAndFreeze(this, 'vendorVersion', vendorVersion);
setAndFreeze(this, 'vendorLabel', vendorLabel);
}
set token(value) {
set(this.storage, 'token', value);
}
get token() {
return this.storage.token;
}
set timezone(value) {
set(this.storage, 'timezone', value);
}
get timezone() {
return this.storage.timezone;
}
set username(value) {
set(this.storage, 'username', value);
}
get username() {
return this.storage.username;
}
set locale(value) {
set(this.storage, 'locale', value);
}
get locale() {
return this.storage.locale;
}
get logLevel() {
return this.storage.logLevel;
}
set logLevel(value) {
set(this.storage, 'logLevel', value);
}
get enableStoreDebugLog() {
const enabled = this.storage.enableStoreDebugLog;
if (isDefined(enabled)) {
return enabled === '1';
}
return enabled;
}
set enableStoreDebugLog(value) {
let storeValue;
if (isDefined(value)) {
storeValue = value ? '1' : '0';
}
set(this.storage, 'enableStoreDebugLog', storeValue);
}
}
export default GmpSettings;
// vim: set ts=2 sw=2 tw=80: