← Files Flows by plugin.spaceARCHIVED FILE
lib/capture-preferences.mjs
1.65 KB · Nov 30, 2026 · 00:17 UTC
import { createHash } from 'node:crypto';
import { readFile } from 'node:fs/promises';
import { join } from 'node:path';
import { DEFAULT_CAPTURE_SETTINGS, validateCaptureSettings } from './capture-settings.mjs';
import { withLibraryLock, writeState } from './local-state.mjs';
// Shared by the native panel, browser viewer and capture CLI on this computer.
// Flow documents keep the settings used for their existing captures.
export function createCapturePreferences(root) {
const path = join(root, 'capture-preferences.json');
async function read() {
let settings;
try { settings = JSON.parse(await readFile(path, 'utf8')); }
catch (error) { if (error.code !== 'ENOENT') throw error; }
if (settings && validateCaptureSettings(settings).length) throw new Error('Invalid global screenshot settings.');
const value = settings ?? structuredClone(DEFAULT_CAPTURE_SETTINGS);
return { settings: value, configured: Boolean(settings), revision: createHash('sha256').update(JSON.stringify(value)).digest('hex') };
}
return {
read,
async effective(fallback) { const saved = await read(); return saved.configured ? saved.settings : fallback ?? saved.settings; },
async save(settings, revision) {
const errors = validateCaptureSettings(settings);
if (errors.length) throw Object.assign(new Error(errors.join('\n')), { status: 422 });
return withLibraryLock(root, async () => {
if ((await read()).revision !== revision) throw Object.assign(new Error('Screenshot settings changed elsewhere. Reopen settings and try again.'), { status: 409 });
await writeState(path, settings);
return read();
});
},
};
}
SHA-256: 27d8ae24250aecfb0bafb32c02a9a3a0e9a87284e0b2553cee9d620b9d254af4