diff --git a/web/database/database-module-provider.js b/web/database/database-module-provider.js --- a/web/database/database-module-provider.js +++ b/web/database/database-module-provider.js @@ -5,7 +5,7 @@ import { DATABASE_WORKER_PATH, - DATABASE_MODULE_FILE_PATH, + WORKERS_MODULES_FILE_PATH, SQLITE_ENCRYPTION_KEY, } from './utils/constants.js'; import { isDesktopSafari, isSQLiteSupported } from './utils/db-utils.js'; @@ -87,7 +87,7 @@ invariant(this.workerProxy, 'Worker proxy should exist'); await this.workerProxy.scheduleOnWorker({ type: workerRequestMessageTypes.INIT, - databaseModuleFilePath: `${origin}${DATABASE_MODULE_FILE_PATH}`, + databaseModuleFilePath: `${origin}${WORKERS_MODULES_FILE_PATH}`, encryptionKey, commQueryExecutorFilename, }); diff --git a/web/database/utils/constants.js b/web/database/utils/constants.js --- a/web/database/utils/constants.js +++ b/web/database/utils/constants.js @@ -8,10 +8,12 @@ export const CURRENT_USER_ID_KEY = 'current_user_id'; export const DATABASE_WORKER_PATH = '/worker/database'; -export const DATABASE_MODULE_FILE_PATH = '/compiled/webworkers'; +export const WORKERS_MODULES_FILE_PATH = '/compiled/webworkers'; export const DEFAULT_COMM_QUERY_EXECUTOR_FILENAME = 'comm_query_executor.wasm'; +export const DEFAULT_OLM_FILENAME = 'olm.wasm'; + export const COMM_SQLITE_DATABASE_PATH = 'comm.sqlite'; export const NOTIFICATIONS_OLM_SESSION_CONTENT = diff --git a/web/push-notif/push-notifs-handler.js b/web/push-notif/push-notifs-handler.js --- a/web/push-notif/push-notifs-handler.js +++ b/web/push-notif/push-notifs-handler.js @@ -20,6 +20,10 @@ } from 'lib/utils/push-alerts.js'; import { ashoatKeyserverID } from 'lib/utils/validation-utils.js'; +import { + WORKERS_MODULES_FILE_PATH, + DEFAULT_OLM_FILENAME, +} from '../database/utils/constants.js'; import electron from '../electron.js'; import PushNotifModal from '../modals/push-notif-modal.react.js'; import { updateNavInfoActionType } from '../redux/action-types.js'; @@ -78,6 +82,10 @@ return; } + const olmDirPath = `${window.location.origin}${WORKERS_MODULES_FILE_PATH}`; + const olmFilePath = `${olmDirPath}/${DEFAULT_OLM_FILENAME}`; + workerRegistration.active?.postMessage({ olmFilePath }); + const subscription = await workerRegistration.pushManager.subscribe({ userVisibleOnly: true, applicationServerKey: publicKey, diff --git a/web/push-notif/service-worker.js b/web/push-notif/service-worker.js --- a/web/push-notif/service-worker.js +++ b/web/push-notif/service-worker.js @@ -1,5 +1,7 @@ // @flow +import olm from '@commapp/olm'; + import type { PlainTextWebNotification } from 'lib/types/notif-types.js'; import { convertNonPendingIDToNewSchema } from 'lib/utils/migration-utils.js'; import { ashoatKeyserverID } from 'lib/utils/validation-utils.js'; @@ -11,6 +13,10 @@ +data: PushMessageData; } +declare class CommAppMessage extends ExtendableEvent { + +data: { +olmFilePath?: string }; +} + declare var clients: Clients; declare function skipWaiting(): Promise; @@ -22,6 +28,23 @@ event.waitUntil(clients.claim()); }); +self.addEventListener('message', (event: CommAppMessage) => { + event.waitUntil( + (async () => { + if (!event.data.olmFilePath) { + return; + } + try { + await olm.init({ + locateFile: () => event.data.olmFilePath, + }); + } catch (e) { + console.log('Failed to initialize olm', e); + } + })(), + ); +}); + self.addEventListener('push', (event: PushEvent) => { const data: PlainTextWebNotification = event.data.json(); diff --git a/web/webpack.config.cjs b/web/webpack.config.cjs --- a/web/webpack.config.cjs +++ b/web/webpack.config.cjs @@ -145,6 +145,14 @@ }, ], }), + new CopyPlugin({ + patterns: [ + { + from: 'node_modules/@commapp/olm/olm.wasm', + to: path.join(__dirname, 'dist', 'webworkers'), + }, + ], + }), ]; const prodWebWorkersPlugins = [ @@ -161,6 +169,19 @@ }, ], }), + new CopyPlugin({ + patterns: [ + { + from: 'node_modules/@commapp/olm/olm.wasm', + to: path.join( + __dirname, + 'dist', + 'webworkers', + 'olm.[contenthash:12].wasm', + ), + }, + ], + }), new WebpackManifestPlugin({ publicPath: '', }),