(value: unknown)
| 31 | const nullableString = (value: unknown): value is string | null => value === null || typeof value === 'string'; |
| 32 | |
| 33 | export function readConfigSnapshot(value: unknown): ConfigSnapshot { |
| 34 | if (!record(value) || typeof value.revision !== 'string' || !Array.isArray(value.settings)) throw new Error('The daemon returned an invalid configuration snapshot.'); |
| 35 | const keys = new Set<string>(); |
| 36 | for (const item of value.settings) { |
| 37 | if (!record(item) || typeof item.key !== 'string' || !item.key || ['__proto__', 'constructor', 'prototype'].includes(item.key) || keys.has(item.key) |
| 38 | || ['label', 'description', 'category', 'source', 'applyMode'].some(key => typeof item[key] !== 'string') |
| 39 | || !['boolean', 'integer', 'number', 'enum', 'string'].includes(String(item.type)) |
| 40 | || ['value', 'override', 'effective', 'defaultValue'].some(key => !nullableString(item[key])) |
| 41 | || typeof item.editable !== 'boolean' || typeof item.pendingRestart !== 'boolean' |
| 42 | || (item.effectiveKnown !== undefined && typeof item.effectiveKnown !== 'boolean') |
| 43 | || (item.options !== undefined && (!Array.isArray(item.options) || item.options.some(option => typeof option !== 'string'))) |
| 44 | || (item.type === 'enum' && (!Array.isArray(item.options) || !item.options.length)) |
| 45 | || ['minimum', 'maximum'].some(key => item[key] !== undefined && (typeof item[key] !== 'number' || !Number.isFinite(item[key])))) { |
| 46 | throw new Error('The daemon returned an invalid setting. Configuration was not changed.'); |
| 47 | } |
| 48 | keys.add(item.key); |
| 49 | } |
| 50 | return value as unknown as ConfigSnapshot; |
| 51 | } |
| 52 | |
| 53 | export function configValueError(setting: ConfigSetting, value: string | null): string | undefined { |
| 54 | if (value === null) return; |
no test coverage detected