okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
5d9f47922
main
1// serve/web/chatstore.js - the saved chats, kept in this browser (IndexedDB, database "strata", store "chats", one record per 2// chat). Ported from PR #1787 by Scotty-Hudson. A chat can be several megabytes and localStorage holds about 5 MB for the whole 3// page, so the chats live here and only small keys (which chat is open, the settings) stay in localStorage. Nothing goes to the 4// server: the server has no endpoint for chats and never sees a title. 5// 6// Where the browser will not keep it (a private window, a blocked or full store) the list lives in memory for the visit, and 7// onProblem(kind) tells the page once: "memory" (no store), "quota" (full), "failed" (a write the browser refused). 8"use strict" ; 9( function ( root ) { 10function createChatStore ({ indexedDB :idb = root . indexedDB , onProblem= () => {}} = {}) { 11let db = null , told = false ; 12const problem = ( kind ) => { if ( ! told ) { told = true ; onProblem ( kind ); } }; 13 14function open () { 15return new Promise (( resolve ) => { 16let r ; 17try { r = idb . open ( "strata" , 1 ); } catch ( e ) { resolve ( null ); return ; } // private mode: memory only 18r . onupgradeneeded = () => { 19const d = r . result ; 20if ( ! d . objectStoreNames . contains ( "chats" )) d . createObjectStore ( "chats" , { keyPath :"id" }); 21}; 22// a database already at version 1 does not run onupgradeneeded, so the store may not be there 23r . onsuccess = () => resolve ( r . result . objectStoreNames . contains ( "chats" ) ?r . result :null ); 24r . onerror = () => resolve ( null ); 25r . onblocked = () => resolve ( null ); // another tab holds an older version of the database 26}); 27} 28// true when the store is there; false: memory only (and the page has been told) 29async function start () { 30db = await open (); 31if ( ! db ) problem ( "memory" ); 32return !! db ; 33} 34function all () { 35return new Promise (( resolve ) => { 36if ( ! db ) { resolve ([]); return ; } 37// the result is on the request, not on the store: reading it at oncomplete gives nothing 38const req = db . transaction ( "chats" , "readonly" ). objectStore ( "chats" ). getAll (); 39req . onsuccess = () => resolve ( req . result || []); 40req . onerror = () => resolve ([]); 41req . transaction . onabort = () => resolve ([]); // an aborted read fires abort, not error: do not hang 42}); 43} 44// a request that fails because the storage is full aborts the transaction, so it fires abort rather than error 45function watch ( t ) { 46const warn = () => problem ( t . error && t . error . name === "QuotaExceededError" ?"quota" :"failed" ); 47t . onerror = warn ; t . onabort = warn ; 48} 49function put ( chat ) { // one record per chat: not the whole list for one message 50if ( ! db ) return ; 51const t = db . transaction ( "chats" , "readwrite" ); 52t . objectStore ( "chats" ). put ( chat ); 53watch ( t ); 54} 55function remove ( id ) { 56if ( ! db ) return ; 57const t = db . transaction ( "chats" , "readwrite" ); 58t . objectStore ( "chats" ). delete ( id ); 59watch ( t ); 60} 61return { start, all, put, remove, get usable () { return !! db ; }}; 62} 63const api = { createChatStore}; 64root . StrataChatStore = api ; 65if ( typeof module !== "undefined" && module . exports ) module . exports = api ; 66})( typeof globalThis !== "undefined" ?globalThis :this );