diff --git a/web/database/queries/db-queries.js b/web/database/queries/db-queries.js new file mode 100644 index 000000000..a922c4b34 --- /dev/null +++ b/web/database/queries/db-queries.js @@ -0,0 +1,19 @@ +// @flow + +import type { SqliteDatabase } from 'sql.js'; + +function getSQLiteDBVersion(db: SqliteDatabase): number { + const versionData = db.exec('PRAGMA user_version;'); + if (!versionData.length || !versionData[0].values.length) { + throw new Error('Error while retrieving database version: empty result'); + } + const [dbVersion] = versionData[0].values[0]; + if (typeof dbVersion !== 'number') { + throw new Error( + 'Error while retrieving database version: invalid type returned', + ); + } + return dbVersion; +} + +export { getSQLiteDBVersion }; diff --git a/web/database/worker/db-worker.js b/web/database/worker/db-worker.js index f8c8427e7..9bb42cfa3 100644 --- a/web/database/worker/db-worker.js +++ b/web/database/worker/db-worker.js @@ -1,106 +1,99 @@ // @flow import localforage from 'localforage'; import initSqlJs, { type SqliteDatabase } from 'sql.js'; import { type SharedWorkerMessageEvent, type WorkerRequestMessage, type WorkerResponseMessage, workerRequestMessageTypes, workerResponseMessageTypes, type WorkerRequestProxyMessage, } from '../../types/worker-types.js'; +import { getSQLiteDBVersion } from '../queries/db-queries.js'; import { SQLITE_CONTENT } from '../utils/constants.js'; const localforageConfig: PartialConfig = { driver: localforage.INDEXEDDB, name: 'comm', storeName: 'commStorage', description: 'Comm encrypted database storage', version: '1.0', }; localforage.config(localforageConfig); let sqliteDb: ?SqliteDatabase = null; async function initDatabase(sqljsFilePath: string, sqljsFilename: ?string) { const content = await localforage.getItem(SQLITE_CONTENT); const locateFile = defaultFilename => { if (sqljsFilename) { return `${sqljsFilePath}/${sqljsFilename}`; } return `${sqljsFilePath}/${defaultFilename}`; }; const SQL = await initSqlJs({ locateFile, }); if (content) { sqliteDb = new SQL.Database(new Uint8Array(content)); } else { sqliteDb = new SQL.Database(); } - const versionData = sqliteDb.exec('PRAGMA user_version;'); - if (!versionData.length || !versionData[0].values.length) { - throw new Error('Error while retrieving database version'); - } - const [dbVersion] = versionData[0].values[0]; - if (typeof dbVersion === 'number') { - console.info(`Db version: ${dbVersion}`); - } else { - throw new Error('Error while retrieving database version'); - } + const dbVersion = getSQLiteDBVersion(sqliteDb); + console.info(`Db version: ${dbVersion}`); } async function processAppRequest( message: WorkerRequestMessage, ): Promise { if (message.type === workerRequestMessageTypes.PING) { return { type: workerResponseMessageTypes.PONG, text: 'PONG', }; } else if (message.type === workerRequestMessageTypes.INIT) { await initDatabase(message.sqljsFilePath, message.sqljsFilename); return; } throw new Error('Request type not supported'); } function connectHandler(event: SharedWorkerMessageEvent) { if (!event.ports.length) { return; } const port: MessagePort = event.ports[0]; console.log('Web database worker alive!'); port.onmessage = async function (messageEvent: MessageEvent) { const data: WorkerRequestProxyMessage = (messageEvent.data: any); const { id, message } = data; if (!id) { port.postMessage({ error: new Error('Request without identifier'), }); } try { const result = await processAppRequest(message); port.postMessage({ id, message: result, }); } catch (e) { port.postMessage({ id, error: e, }); } }; } self.addEventListener('connect', connectHandler);