88 lines
3.7 KiB
JavaScript
88 lines
3.7 KiB
JavaScript
'use strict';
|
|
|
|
const fs = require('node:fs');
|
|
const path = require('node:path');
|
|
|
|
/**
|
|
* UI preferences owned by main (identity/nickname live in the core data dir, never here).
|
|
* Every key has a validator; unknown keys and wrong types are rejected.
|
|
*/
|
|
const SCHEMA = {
|
|
lastAddress: { default: '', check: (v) => typeof v === 'string' && v.length <= 255 && !/[\u0000-\u001f]/.test(v) },
|
|
transmitMode: { default: 'ptt', check: (v) => v === 'ptt' || v === 'open' },
|
|
pttCode: { default: 'F8', check: (v) => typeof v === 'string' && /^[A-Za-z0-9]{1,32}$/.test(v) },
|
|
inputDeviceId: { default: '', check: (v) => typeof v === 'string' && v.length <= 512 },
|
|
outputDeviceId: { default: '', check: (v) => typeof v === 'string' && v.length <= 512 },
|
|
echoCancellation: { default: true, check: (v) => typeof v === 'boolean' },
|
|
noiseSuppression: { default: true, check: (v) => typeof v === 'boolean' },
|
|
autoGainControl: { default: true, check: (v) => typeof v === 'boolean' },
|
|
voiceVolume: { default: 100, check: (v) => Number.isInteger(v) && v >= 0 && v <= 200 },
|
|
hardwareVideoDecode: { default: true, check: (v) => typeof v === 'boolean' },
|
|
streamVolume: { default: 100, check: (v) => Number.isInteger(v) && v >= 0 && v <= 100 },
|
|
theme: { default: 'system', check: (v) => v === 'system' || v === 'dark' || v === 'light' },
|
|
// Quick-access servers: name + address only. Passwords are never stored.
|
|
savedServers: { default: [], check: validServers },
|
|
};
|
|
|
|
const CONTROL = /[\u0000-\u001f\u007f]/;
|
|
|
|
function validServers(list) {
|
|
if (!Array.isArray(list) || list.length > 50) return false;
|
|
const ids = new Set();
|
|
for (const s of list) {
|
|
if (!s || typeof s !== 'object' || Array.isArray(s)) return false;
|
|
const keys = Object.keys(s);
|
|
if (keys.length !== 3 || !keys.includes('id') || !keys.includes('name') || !keys.includes('address')) return false;
|
|
if (typeof s.id !== 'string' || !/^[a-z0-9]{6,32}$/.test(s.id) || ids.has(s.id)) return false;
|
|
if (typeof s.name !== 'string' || s.name.trim().length < 1 || s.name.length > 64 || CONTROL.test(s.name)) return false;
|
|
if (typeof s.address !== 'string' || s.address.trim().length < 1 || s.address.length > 255 || CONTROL.test(s.address)) return false;
|
|
ids.add(s.id);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
class Prefs {
|
|
constructor(file) {
|
|
this.file = file;
|
|
this.values = defaults();
|
|
try {
|
|
const raw = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
if (raw && typeof raw === 'object') {
|
|
for (const [key, spec] of Object.entries(SCHEMA)) {
|
|
if (Object.prototype.hasOwnProperty.call(raw, key) && spec.check(raw[key])) this.values[key] = raw[key];
|
|
}
|
|
}
|
|
} catch {
|
|
// first run or unreadable file: defaults
|
|
}
|
|
}
|
|
|
|
get() {
|
|
return { ...this.values };
|
|
}
|
|
|
|
/** Applies a validated partial update; returns the full prefs or throws. */
|
|
set(partial) {
|
|
if (!partial || typeof partial !== 'object' || Array.isArray(partial)) throw new Error('prefs: ожидался объект');
|
|
const next = { ...this.values };
|
|
for (const [key, value] of Object.entries(partial)) {
|
|
const spec = SCHEMA[key];
|
|
if (!spec) throw new Error(`prefs: неизвестный ключ ${key}`);
|
|
if (!spec.check(value)) throw new Error(`prefs: недопустимое значение ${key}`);
|
|
next[key] = value;
|
|
}
|
|
this.values = next;
|
|
fs.mkdirSync(path.dirname(this.file), { recursive: true });
|
|
const tmp = `${this.file}.tmp`;
|
|
fs.writeFileSync(tmp, JSON.stringify(next, null, 2));
|
|
fs.renameSync(tmp, this.file);
|
|
return this.get();
|
|
}
|
|
}
|
|
|
|
function defaults() {
|
|
return Object.fromEntries(Object.entries(SCHEMA).map(([key, spec]) => [key, spec.default]));
|
|
}
|
|
|
|
module.exports = { Prefs };
|