import { mkdirSync, readFileSync, writeFileSync, renameSync } from 'node:fs'; import path from 'node:path'; import { randomUUID } from 'node:crypto'; import { shortText, webUrl, SEARCH_PROVIDERS } from './policy.mjs'; import { DEFAULT_LISTS, normalizeLists } from './subscriptions.mjs'; export class Store { constructor(directory) { mkdirSync(directory, { recursive: true, mode: 0o700 }); this.file = path.join(directory, 'browser.json'); this.data = { version: 1, settings: { search: 'duckduckgo', adblock: true, linkDestination: 'internal', personalRules: '', homepage: '', filterLists: [...DEFAULT_LISTS] }, profiles: [{ id: 'user', name: 'User' }], bookmarks: [], history: [], exceptions: [], }; try { const saved = JSON.parse(readFileSync(this.file, 'utf8')); if (saved.version !== 1 || !Array.isArray(saved.profiles) || !saved.profiles.every(p => /^[a-z0-9-]{1,64}$/.test(p.id))) throw new Error('Unsupported profile store.'); this.data = { ...saved, settings: { ...this.data.settings, ...saved.settings } }; // Preserve existing data. Legacy profiles become ordinary, removable profiles. const personal = this.data.profiles.find(profile => profile.id === 'personal'); if (personal?.name === 'Personal') personal.name = 'User'; } catch (error) { if (error.code !== 'ENOENT') throw error; } for (const item of this.data.history) if (!item.id) item.id = randomUUID(); } save() { writeFileSync(this.file + '.tmp', JSON.stringify(this.data, null, 2), { mode: 0o600 }); renameSync(this.file + '.tmp', this.file); } profile(id) { const profile = this.data.profiles.find(p => p.id === id); if (!profile) throw new Error('Unknown profile.'); return profile; } createProfile(name) { if (this.data.profiles.length >= 20) throw new Error('The profile limit is 20.'); const profile = { id: randomUUID(), name: shortText(name, 60) }; this.data.profiles.push(profile); this.save(); return profile; } duplicateMetadata(sourceId, id, name) { this.profile(sourceId); if (!/^[a-f0-9-]{36}$/.test(id) || this.data.profiles.some(profile => profile.id === id)) throw new Error('Invalid destination profile.'); if (this.data.profiles.length >= 20) throw new Error('The profile limit is 20.'); const profile = { id, name: shortText(name, 60) }; this.data.profiles.push(profile); this.data.bookmarks.push(...this.data.bookmarks.filter(item => item.profileId === sourceId).map(item => ({ ...item, id: randomUUID(), profileId: id }))); this.data.history.push(...this.data.history.filter(item => item.profileId === sourceId).map(item => ({ ...item, id: randomUUID(), profileId: id }))); this.save(); return profile; } configure(input, { persist = true } = {}) { if (!Object.hasOwn(SEARCH_PROVIDERS, input.search) || typeof input.adblock !== 'boolean' || !['internal', 'external'].includes(input.linkDestination) || typeof input.personalRules !== 'string' || input.personalRules.length > 100_000) throw new Error('Invalid browser settings.'); const homepage = input.homepage ?? this.data.settings.homepage; if (typeof homepage !== 'string') throw new Error('Invalid homepage address.'); const filterLists = normalizeLists(input.filterLists ?? this.data.settings.filterLists); this.data.settings = { search: input.search, adblock: input.adblock, linkDestination: input.linkDestination, personalRules: input.personalRules, homepage: homepage.trim() ? webUrl(homepage.trim()) : '', filterLists }; if (persist) this.save(); } visit(profileId, url, title) { this.profile(profileId); if (!/^https?:/.test(url)) return; this.data.history.unshift({ id: randomUUID(), profileId, url: webUrl(url), title: String(title).slice(0, 200), at: Date.now() }); this.data.history = this.data.history.slice(0, 2000); this.save(); } bookmark(profileId, url, title) { this.profile(profileId); this.data.bookmarks.push({ id: randomUUID(), profileId, url: webUrl(url, { blank: false }), title: shortText(title || url, 500) }); this.save(); } removeHistory(profileId, id) { this.profile(profileId); if (typeof id !== 'string' || !this.data.history.some(item => item.profileId === profileId && item.id === id)) throw new Error('This history entry no longer exists. Refresh the list.'); this.data.history = this.data.history.filter(item => item.profileId !== profileId || item.id !== id); this.save(); } }