import { SaveConflict, SaveFailure } from './save-errors.ts'; import type { ServerVersion } from './save-errors.ts'; const KEEPALIVE_LIMIT = 60_000; export type SaveInput = { body: unknown; text: string; /** The revision these words were built on. */ revision: number; /** The writer chose these words over a newer saved version. */ replace: boolean; /** * The writer's own local day, so a writer east or west of the server keeps their * own midnight for the daily count. */ day?: string; }; /** The writer's day and its total after that save. */ export type SaveToday = { day: string; words: number }; export type SaveAck = { revision: number; /** Present only when the save added words; cutting words does not take them back. */ today?: SaveToday; }; function readToday(value: unknown): SaveToday | undefined { if (!value || typeof value !== 'object') return undefined; const { day, words } = value as { day?: unknown; words?: unknown }; if (typeof day !== 'string' || typeof words !== 'number' || !Number.isFinite(words)) { return undefined; } return { day, words }; } /** * Scenes and notes both call this, so the save contract cannot drift between them. * `goneMessage` names what disappeared, because the server's 404 text is not written * for a writer. */ export async function putDocument( url: string, input: SaveInput, goneMessage: string ): Promise { let response: Response; const payload = JSON.stringify({ body: input.body, text: input.text, baseRevision: input.revision, replace: input.replace, ...(input.day ? { day: input.day } : {}) }); try { response = await fetch(url, { method: 'PUT', headers: { 'content-type': 'application/json' }, body: payload, // A save sent as the tab closes must still finish. The browser caps a // kept-alive request near 64 KB, so only a body under that asks for it. keepalive: new TextEncoder().encode(payload).byteLength < KEEPALIVE_LIMIT }); } catch { throw new SaveFailure( 'network', 'Could not reach the server. Your words are still on screen; the editor keeps trying.' ); } if (response.ok) { const answer = (await response.json().catch(() => null)) as { revision?: unknown; today?: unknown; } | null; if (typeof answer?.revision !== 'number') { throw new SaveFailure('server', 'The server did not take the save. Retrying.'); } return { revision: answer.revision, today: readToday(answer.today) }; } if (response.status === 409) { const answer = (await response.json().catch(() => null)) as (Partial & { code?: string }) | null; if (answer?.code === 'conflict' && typeof answer.revision === 'number') { throw new SaveConflict({ revision: answer.revision, body: answer.body ?? null, text: typeof answer.text === 'string' ? answer.text : '', words: typeof answer.words === 'number' ? answer.words : 0, savedAt: typeof answer.savedAt === 'string' ? answer.savedAt : null }); } } if (response.status === 401) { throw new SaveFailure( 'auth', 'You are signed out. Sign in again; your words are still on screen.' ); } if (response.status === 404) throw new SaveFailure('gone', goneMessage); throw new SaveFailure('server', 'The server did not take the save. Retrying.'); }