From 7a6f37fbccf9a419ccdb45fc09d0152cf0306574 Mon Sep 17 00:00:00 2001 From: xsanm Date: Thu, 13 Jul 2023 15:22:14 +0200 Subject: [PATCH] [web] add function to retrieve WASM module Summary: Making sure that module is now typed in Flow, and making it easier to retrieve the module (will be helpful in tests later). Depends on D8545 Test Plan: 1. `flow` 2. Check if result is still logged Reviewers: michal, tomek Reviewed By: michal Subscribers: ashoat Differential Revision: https://phab.comm.dev/D8547 --- web/database/db-module.js | 25 +++++++++++++++++++++++++ web/database/utils/constants.js | 2 ++ web/database/worker/db-worker.js | 13 ++----------- 3 files changed, 29 insertions(+), 11 deletions(-) create mode 100644 web/database/db-module.js 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();