diff --git a/web/database/db-module.js b/web/database/db-module.js new file mode 100644 index 0000000000..568827a0cd --- /dev/null +++ b/web/database/db-module.js @@ -0,0 +1,25 @@ +// @flow + +import Module from './_generated/comm-query-executor.js'; +import type { EmscriptenModule } from './types/module.js'; +import { DEFAULT_COMM_QUERY_EXECUTOR_FILENAME } from './utils/constants.js'; + +function getDatabaseModule( + databaseModuleFilename?: ?string, + databaseModuleFilePath?: string, +): EmscriptenModule { + const fileName = databaseModuleFilename + ? databaseModuleFilename + : DEFAULT_COMM_QUERY_EXECUTOR_FILENAME; + + return Module({ + locateFile: (path: string, prefix?: string) => { + if (databaseModuleFilePath) { + return `${databaseModuleFilePath}/${fileName}`; + } + return `${prefix ?? ''}${fileName}`; + }, + }); +} + +export { getDatabaseModule }; diff --git a/web/database/utils/constants.js b/web/database/utils/constants.js index e93ce06483..a942b819f2 100644 --- a/web/database/utils/constants.js +++ b/web/database/utils/constants.js @@ -10,6 +10,8 @@ export const CURRENT_USER_ID_KEY = 'current_user_id'; export const DATABASE_WORKER_PATH = '/worker/database'; export const SQLJS_FILE_PATH = '/compiled/webworkers'; +export const DEFAULT_COMM_QUERY_EXECUTOR_FILENAME = 'comm_query_executor.wasm'; + export const DB_SUPPORTED_OS: $ReadOnlyArray = [ 'Windows 10', 'Linux', diff --git a/web/database/worker/db-worker.js b/web/database/worker/db-worker.js index be736d5678..75084793e7 100644 --- a/web/database/worker/db-worker.js +++ b/web/database/worker/db-worker.js @@ -19,7 +19,7 @@ import { type WorkerRequestProxyMessage, workerWriteRequests, } from '../../types/worker-types.js'; -import Module from '../_generated/comm-query-executor.js'; +import { getDatabaseModule } from '../db-module.js'; import { migrate, setupSQLiteDB } from '../queries/db-queries.js'; import { getAllDrafts, @@ -60,22 +60,13 @@ let encryptionKey: ?CryptoKey = null; let persistNeeded: boolean = false; let persistInProgress: boolean = false; -const defaultCommQueryExecutorFilename = 'comm_query_executor.wasm'; - async function initDatabase( sqljsFilePath: string, sqljsFilename: ?string, commQueryExecutorFilename: ?string, encryptionKeyJWK?: ?SubtleCrypto$JsonWebKey, ) { - const dbModule = Module({ - locateFile: function () { - if (commQueryExecutorFilename) { - return `${sqljsFilePath}/${commQueryExecutorFilename}`; - } - return `${sqljsFilePath}/${defaultCommQueryExecutorFilename}`; - }, - }); + const dbModule = getDatabaseModule(commQueryExecutorFilename, sqljsFilePath); try { const result = dbModule.CommQueryExecutor.testDBOperation();