[storage] Profile: Keep the sunset place in IndexedDB, written as a plain checked copy

This commit is contained in:
2026-11-02 00:01:50 +01:00
parent 1e8d1cda5a
commit 3ed91eea7b
4 changed files with 220 additions and 0 deletions
+82
View File
@@ -0,0 +1,82 @@
/**
* The ONLY module that touches IndexedDB (CLAUDE.md §2). The database `rosea` and its
* stores are listed in CLAUDE.md §4; each store arrives with the feature that writes
* it. So far: `profile`, written by the sunset-place question
* (.agents/features/sunset-place.md).
*
* The database name changes together with the product name (CLAUDE.md preamble).
*/
import Dexie, { type DexieOptions, type Table } from 'dexie';
import type { NamedPlace } from '@rosea/engine';
export const DATABASE_NAME = 'rosea';
/** Where the player wants their sunset: a town they chose, or the guess from the device's zone. */
export type SunsetPlace = { readonly kind: 'town'; readonly town: NamedPlace } | { readonly kind: 'device-zone' };
interface ProfileRow {
readonly key: string;
readonly value: unknown;
}
const SUNSET_PLACE = 'sunsetPlace';
export interface Profile {
/** The saved sunset place, or null if the player has not answered yet. */
loadSunsetPlace(): Promise<SunsetPlace | null>;
saveSunsetPlace(place: SunsetPlace): Promise<void>;
close(): void;
}
const isNumberIn = (x: unknown, limit: number): x is number => typeof x === 'number' && Number.isFinite(x) && Math.abs(x) <= limit;
const isString = (x: unknown): x is string => typeof x === 'string';
/** The shape check a stored value must pass. A value from an older version or a broken write fails it. */
export function asSunsetPlace(value: unknown): SunsetPlace | null {
if (typeof value !== 'object' || value === null) return null;
const v = value as Record<string, unknown>;
if (v['kind'] === 'device-zone') return { kind: 'device-zone' };
if (v['kind'] !== 'town' || typeof v['town'] !== 'object' || v['town'] === null) return null;
const t = v['town'] as Record<string, unknown>;
if (!isString(t['name']) || t['name'] === '' || !isString(t['region']) || !isString(t['country']) || !isString(t['timeZone'])) return null;
if (!isNumberIn(t['lat'], 90) || !isNumberIn(t['lon'], 180)) return null;
return { kind: 'town', town: { name: t['name'], region: t['region'], country: t['country'], lat: t['lat'], lon: t['lon'], timeZone: t['timeZone'] } };
}
class RoseaDatabase extends Dexie {
profile!: Table<ProfileRow, string>;
constructor(options?: DexieOptions) {
super(DATABASE_NAME, options);
this.version(1).stores({ profile: 'key' });
}
}
/**
* Opens the player's database. `options` lets a test pass fake-indexeddb; the app
* passes nothing and gets the browser's IndexedDB.
*/
export function openProfile(options?: DexieOptions): Profile {
const db = new RoseaDatabase(options);
return {
async loadSunsetPlace() {
const row = await db.profile.get(SUNSET_PLACE);
if (row === undefined) return null;
const place = asSunsetPlace(row.value);
// Never swallowed: a broken record is said out loud, then the question is asked again.
if (place === null) console.warn('storage: the saved sunset place has an unknown shape; asking again', row.value);
return place;
},
async saveSunsetPlace(place) {
// Write a fresh plain copy, never the caller's object: a Svelte 5 state Proxy
// cannot be structured-cloned, and IndexedDB refuses it (DataCloneError). The
// same shape check as on reading, so nothing malformed is ever written.
const plain = asSunsetPlace(place);
if (plain === null) throw new TypeError('storage: refusing to save a sunset place of an unknown shape');
await db.profile.put({ key: SUNSET_PLACE, value: plain });
},
close() {
db.close();
}
};
}