Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

web: allow multi tab access when using idb vfs

Abdullah Atta727ac6d9079f
diff --git a/apps/web/src/common/db.ts b/apps/web/src/common/db.ts
index c66a676fe..73829caa1 100644
--- a/apps/web/src/common/db.ts
+++ b/apps/web/src/common/db.ts
@@ -56,22 +56,29 @@ async function initializeDatabase(persistence: DatabasePersistence) {
);
await storage.migrate();
+ const multiTab = !!globalThis.SharedWorker && isFeatureSupported("opfs");
database.setup({
sqliteOptions: {
dialect: (name, init) =>
- createDialect(persistence === "memory" ? ":memory:" : name, true, init),
+ createDialect({
+ name: persistence === "memory" ? ":memory:" : name,
+ encrypted: true,
+ async: !isFeatureSupported("opfs"),
+ init,
+ multiTab
+ }),
...(IS_DESKTOP_APP || isFeatureSupported("opfs")
? { journalMode: "WAL", lockingMode: "exclusive" }
: {
journalMode: "MEMORY",
- lockingMode: "exclusive"
+ lockingMode: "normal"
}),
tempStore: "memory",
synchronous: "normal",
pageSize: 8192,
cacheSize: -32000,
password: Buffer.from(databaseKey).toString("hex"),
- skipInitialization: !IS_DESKTOP_APP && !!globalThis.SharedWorker
+ skipInitialization: !IS_DESKTOP_APP && multiTab
},
storage: storage,
eventsource: EventSource,
diff --git a/apps/web/src/common/sqlite/IDBBatchAtomicVFS.js b/apps/web/src/common/sqlite/IDBBatchAtomicVFS.js
index 4832f7320..766215a7b 100644
--- a/apps/web/src/common/sqlite/IDBBatchAtomicVFS.js
+++ b/apps/web/src/common/sqlite/IDBBatchAtomicVFS.js
@@ -1,7 +1,7 @@
/* eslint-disable header/header */
// Copyright 2022 Roy T. Hashimoto. All Rights Reserved.
import * as VFS from "./VFS.js";
-import { WebLocksExclusive as WebLocks } from "./WebLocks.js";
+import { WebLocksShared as WebLocks } from "./WebLocks.js";
import { IDBContext } from "./IDBContext.js";
const SECTOR_SIZE = 512;
diff --git a/apps/web/src/common/sqlite/index.desktop.ts b/apps/web/src/common/sqlite/index.desktop.ts
index 355a68491..22d226192 100644
--- a/apps/web/src/common/sqlite/index.desktop.ts
+++ b/apps/web/src/common/sqlite/index.desktop.ts
@@ -32,6 +32,7 @@ import Worker from "./sqlite.worker.desktop.ts?worker";
import type { SQLiteWorker } from "./sqlite.worker.desktop";
import { wrap, Remote } from "comlink";
import { Mutex } from "async-mutex";
+import { DialectOptions } from ".";
class SqliteDriver implements Driver {
connection?: DatabaseConnection;
@@ -100,11 +101,11 @@ class SqliteWorkerConnection implements DatabaseConnection {
}
}
-export const createDialect = (name: string, _encrypted: boolean): Dialect => {
+export const createDialect = (options: DialectOptions): Dialect => {
return {
createDriver: () =>
new SqliteDriver({
- name
+ name: options.name
}),
createAdapter: () => new SqliteAdapter(),
createIntrospector: (db) => new SqliteIntrospector(db),
diff --git a/apps/web/src/common/sqlite/index.ts b/apps/web/src/common/sqlite/index.ts
index 56dc905dd..43c81ad63 100644
--- a/apps/web/src/common/sqlite/index.ts
+++ b/apps/web/src/common/sqlite/index.ts
@@ -27,7 +27,6 @@ import {
WaSqliteWorkerMultipleTabDriver,
WaSqliteWorkerSingleTabDriver
} from "./wa-sqlite-kysely-driver";
-import { isFeatureSupported } from "../../utils/feature-check";
declare module "kysely" {
interface Driver {
@@ -35,22 +34,26 @@ declare module "kysely" {
}
}
-export const createDialect = (
- name: string,
- encrypted: boolean,
- init?: () => Promise<void>
-): Dialect => {
+export type DialectOptions = {
+ name: string;
+ encrypted: boolean;
+ async: boolean;
+ multiTab: boolean;
+ init?: () => Promise<void>;
+};
+export const createDialect = (options: DialectOptions): Dialect => {
+ const { async, encrypted, multiTab, name, init } = options;
return {
createDriver: () =>
- globalThis.SharedWorker
+ multiTab
? new WaSqliteWorkerMultipleTabDriver({
- async: !isFeatureSupported("opfs"),
+ async,
dbName: name,
encrypted,
init
})
: new WaSqliteWorkerSingleTabDriver({
- async: !isFeatureSupported("opfs"),
+ async,
dbName: name,
encrypted
}),
diff --git a/apps/web/src/utils/logger.ts b/apps/web/src/utils/logger.ts
index fb93d9738..4c4ab37f9 100644
--- a/apps/web/src/utils/logger.ts
+++ b/apps/web/src/utils/logger.ts
@@ -31,9 +31,17 @@ import { isFeatureSupported } from "./feature-check";
let logger: typeof _logger = new NoopLogger();
async function initializeLogger() {
+ const multiTab = !!globalThis.SharedWorker && isFeatureSupported("opfs");
await initialize(
{
- dialect: (name, init) => createDialect(name, false, init),
+ dialect: (name, init) =>
+ createDialect({
+ name,
+ init,
+ async: !isFeatureSupported("opfs"),
+ multiTab,
+ encrypted: false
+ }),
...(IS_DESKTOP_APP || isFeatureSupported("opfs")
? { journalMode: "WAL", lockingMode: "exclusive" }
: {
@@ -44,7 +52,7 @@ async function initializeLogger() {
synchronous: "normal",
pageSize: 8192,
cacheSize: -32000,
- skipInitialization: !IS_DESKTOP_APP && !!globalThis.SharedWorker
+ skipInitialization: !IS_DESKTOP_APP && multiTab
},
false
);