diff --git a/web/database/utils/constants.js b/web/database/utils/constants.js index 6298a6e46..b0b31b5c5 100644 --- a/web/database/utils/constants.js +++ b/web/database/utils/constants.js @@ -1,8 +1,23 @@ // @flow export const SQLITE_CONTENT = 'sqliteFileContent'; export const SQLITE_ENCRYPTION_KEY = 'encryptionKey'; export const CURRENT_USER_ID_KEY = 'current_user_id'; export const DB_PERSIST_THROTTLE_WAIT_MS = 300; + +export const DB_SUPPORTED_OS: $ReadOnlyArray = [ + 'Windows 10', + 'Linux', + 'Mac OS', +]; + +export const DB_SUPPORTED_BROWSERS: $ReadOnlyArray = [ + 'edge', + 'edge-chromium', + 'chrome', + 'safari', + 'firefox', + 'opera', +]; diff --git a/web/database/utils/db-utils.js b/web/database/utils/db-utils.js index 8b688e34e..634ce0817 100644 --- a/web/database/utils/db-utils.js +++ b/web/database/utils/db-utils.js @@ -1,27 +1,48 @@ // @flow +import { detect as detectBrowser } from 'detect-browser'; import type { QueryExecResult } from 'sql.js'; +import { isStaff } from 'lib/shared/user-utils.js'; +import { isDev } from 'lib/utils/dev-utils.js'; + +import { DB_SUPPORTED_BROWSERS, DB_SUPPORTED_OS } from './constants.js'; + function parseSQLiteQueryResult(result: QueryExecResult): T[] { const { columns, values } = result; return values.map(rowResult => { const row: any = Object.fromEntries( columns.map((key, index) => [key, rowResult[index]]), ); return row; }); } // NOTE: sql.js has behavior that when there are multiple statements in query // e.g. "statement1; statement2; statement3;" // and statement2 will not return anything, the result will be: // [result1, result3], not [result1, undefined, result3] function parseMultiStatementSQLiteResult( rawResult: $ReadOnlyArray, ): T[][] { return rawResult.map((queryResult: QueryExecResult) => parseSQLiteQueryResult(queryResult), ); } -export { parseMultiStatementSQLiteResult }; +function isSQLiteSupported(currentLoggedInUserID: ?string): boolean { + if (!currentLoggedInUserID) { + return false; + } + if (!isDev && (!currentLoggedInUserID || !isStaff(currentLoggedInUserID))) { + return false; + } + + const browser = detectBrowser(); + return ( + DB_SUPPORTED_OS.includes(browser.os) && + DB_SUPPORTED_BROWSERS.includes(browser.name) + ); +} + +export { parseMultiStatementSQLiteResult, isSQLiteSupported };