Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Use Rust SQLite implementation for browser extension (#2159)

Leendert de Borst33b1238d782d
diff --git a/apps/browser-extension/package-lock.json b/apps/browser-extension/package-lock.json
index 7c8339936..fc5de84e2 100644
--- a/apps/browser-extension/package-lock.json
+++ b/apps/browser-extension/package-lock.json
@@ -68,11 +68,10 @@
"dependencies": {
"@aliasvault/models": "file:../models",
"@aliasvault/vault": "file:../vault",
- "otpauth": "^9.3.6",
- "sql.js": "^1.12.0"
+ "fflate": "^0.8.3",
+ "otpauth": "^9.3.6"
},
"devDependencies": {
- "@types/sql.js": "^1.4.9",
"@typescript-eslint/eslint-plugin": "^8.65.0",
"@typescript-eslint/parser": "^8.65.0",
"eslint": "^9.39.5",
@@ -102,12 +101,10 @@
"version": "1.0.0",
"license": "AGPL-3.0",
"devDependencies": {
- "@types/sql.js": "^1.4.11",
"@typescript-eslint/eslint-plugin": "^8.21.0",
"@typescript-eslint/parser": "^8.21.0",
"@vitest/coverage-v8": "^4.1.8",
"eslint": "^9.19.0",
- "sql.js": "^1.14.2",
"tsup": "^8.4.0",
"typescript": "^5.3.3",
"vitest": "^4.1.8"
diff --git a/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts b/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts
index 3d25c6a93..7b1702473 100644
--- a/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts
+++ b/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts
@@ -682,7 +682,7 @@ async function persistLocalVaultMutation(sqliteClient: SqliteClient, encryptionK
/*
* The stored blob is exactly this client's content, so re-adopt the pair as the cache (the store just cleared
- * it): the sync that follows can then read the vault without another decrypt + sql.js load.
+ * it): the sync that follows can then read the vault without another decrypt + database load.
*/
cachedSqliteClient = sqliteClient;
cachedVaultBlob = encryptedVault;
diff --git a/apps/browser-extension/src/platform/ExtensionPlatform.ts b/apps/browser-extension/src/platform/ExtensionPlatform.ts
index d393a1cec..f51d64133 100644
--- a/apps/browser-extension/src/platform/ExtensionPlatform.ts
+++ b/apps/browser-extension/src/platform/ExtensionPlatform.ts
@@ -2,7 +2,7 @@
* The browser extension's implementations of the client core's platform interfaces (e.g. storage, logger, etc.) which are different per platform.
*/
-import { createSqlJsEngine } from '@aliasvault/client/database/SqlJsEngine';
+import { createRustSqliteEngine } from '@aliasvault/client/database/RustSqliteEngine';
import { type IClientPlatform, type IKeyValueStore, type StorageKey, TranslatableMessage } from '@aliasvault/client/platform';
import { createWasmRustCore } from '@aliasvault/client/rust/WasmRustCore';
import { browser } from 'wxt/browser';
@@ -86,11 +86,12 @@ const extensionStorage: IKeyValueStore = {
};
/**
- * Get the path to the sql.js support files.
+ * The Rust core which also hosts the SQLite engine.
*/
-function locateSqlJsFile(file: string): string {
- return `src/${file}`;
-}
+const rustCore = createWasmRustCore(async (): Promise<BufferSource> => {
+ const wasmUrl = (browser.runtime.getURL as (path: string) => string)('src/aliasvault_core_bg.wasm');
+ return (await fetch(wasmUrl)).arrayBuffer();
+});
/**
* The platform the extension registers with the client core.
@@ -103,14 +104,8 @@ export const extensionPlatform: IClientPlatform = {
clientName: detectClientName(),
isDevelopment: import.meta.env.DEV,
},
- /**
- * Fetch the bundled Rust core binary.
- */
- rustCore: createWasmRustCore(async (): Promise<BufferSource> => {
- const wasmUrl = (browser.runtime.getURL as (path: string) => string)('src/aliasvault_core_bg.wasm');
- return (await fetch(wasmUrl)).arrayBuffer();
- }),
- sqlite: createSqlJsEngine(locateSqlJsFile),
+ rustCore,
+ sqlite: createRustSqliteEngine(rustCore),
/**
* Translate one of the core's own messages through the extension's i18n.
*/
diff --git a/apps/browser-extension/tests/helpers/client-platform.ts b/apps/browser-extension/tests/helpers/client-platform.ts
index ccb38963f..dcca0de28 100644
--- a/apps/browser-extension/tests/helpers/client-platform.ts
+++ b/apps/browser-extension/tests/helpers/client-platform.ts
@@ -5,21 +5,20 @@
*/
import { readFileSync } from 'node:fs';
-import { createRequire } from 'node:module';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
-import { createSqlJsEngine } from '@aliasvault/client/database/SqlJsEngine';
+import { createRustSqliteEngine } from '@aliasvault/client/database/RustSqliteEngine';
import { setPlatform } from '@aliasvault/client/platform';
import { createInMemoryPlatform } from '@aliasvault/client/platform/InMemoryPlatform';
import { createWasmRustCore } from '@aliasvault/client/rust/WasmRustCore';
const clientCoreDir = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../../../../core/client');
-const requireFromClientCore = createRequire(path.join(clientCoreDir, 'package.json'));
+
+// The Rust core which also hosts the SQLite engine.
+const rustCore = createWasmRustCore(async (): Promise<BufferSource> => readFileSync(path.join(clientCoreDir, 'wasm/aliasvault_core_bg.wasm')));
setPlatform(createInMemoryPlatform({
- // The Rust core WASM from the client core build output.
- rustCore: createWasmRustCore(async (): Promise<BufferSource> => readFileSync(path.join(clientCoreDir, 'wasm/aliasvault_core_bg.wasm'))),
- // sql.js support files from the client core's node_modules.
- sqlite: createSqlJsEngine((file: string): string => path.join(path.dirname(requireFromClientCore.resolve('sql.js')), file)),
+ rustCore,
+ sqlite: createRustSqliteEngine(rustCore),
}));
diff --git a/apps/browser-extension/wxt.config.ts b/apps/browser-extension/wxt.config.ts
index 7db8b5ac0..d0b202dc4 100644
--- a/apps/browser-extension/wxt.config.ts
+++ b/apps/browser-extension/wxt.config.ts
@@ -90,7 +90,6 @@ export default defineConfig({
web_accessible_resources: [{
resources: [
"webauthn.js",
- "src/sql-wasm-browser.wasm",
"src/aliasvault_core_bg.wasm"
],
matches: ["<all_urls>"]
@@ -122,10 +121,6 @@ export default defineConfig({
asciiOnlyJsPlugin(),
viteStaticCopy({
targets: [
- {
- src: path.resolve(CORE_DIR, 'client/node_modules/sql.js/dist/sql-wasm-browser.wasm'),
- dest: 'src'
- },
{
src: path.resolve(CORE_DIR, 'client/wasm/aliasvault_core_bg.wasm'),
dest: 'src'
diff --git a/core/client/package-lock.json b/core/client/package-lock.json
index b479aa592..7f8393203 100644
--- a/core/client/package-lock.json
+++ b/core/client/package-lock.json
@@ -12,11 +12,9 @@
"@aliasvault/models": "file:../models",
"@aliasvault/vault": "file:../vault",
"fflate": "^0.8.3",
- "otpauth": "^9.3.6",
- "sql.js": "^1.12.0"
+ "otpauth": "^9.3.6"
},
"devDependencies": {
- "@types/sql.js": "^1.4.9",
"@typescript-eslint/eslint-plugin": "^8.65.0",
"@typescript-eslint/parser": "^8.65.0",
"eslint": "^9.39.5",
@@ -46,12 +44,10 @@
"version": "1.0.0",
"license": "AGPL-3.0",
"devDependencies": {
- "@types/sql.js": "^1.4.11",
"@typescript-eslint/eslint-plugin": "^8.21.0",
"@typescript-eslint/parser": "^8.21.0",
"@vitest/coverage-v8": "^4.1.8",
"eslint": "^9.19.0",
- "sql.js": "^1.14.2",
"tsup": "^8.4.0",
"typescript": "^5.3.3",
"vitest": "^4.1.8"
@@ -772,13 +768,6 @@
"dev": true,
"license": "MIT"
},
- "node_modules/@types/emscripten": {
- "version": "1.41.6",
- "resolved": "https://registry.npmjs.org/@types/emscripten/-/emscripten-1.41.6.tgz",
- "integrity": "sha512-uN+9i8bFT5CUcZfyIEYDrSueACEyKGbUs5kC/72DGlZZoinh84sJfVV0i8UOJD1asdzkvLPBRrKs41kZ8MdEXg==",
- "dev": true,
- "license": "MIT"
- },
"node_modules/@types/estree": {
"version": "1.0.9",
"resolved": "https://registry.npmjs.org/@types/estree/-/estree-1.0.9.tgz",
@@ -800,27 +789,6 @@
"dev": true,
"license": "MIT"
},
- "node_modules/@types/node": {
- "version": "22.20.2",
- "resolved": "https://registry.npmjs.org/@types/node/-/node-22.20.2.tgz",
- "integrity": "sha512-xlvWf4Vs9n1PEVYwP1n4vvG07M6y8WgvJ2t0vbrWTmijsIHp1cS+uJ2kMIRdY3nHZK0nCYKrPeD171+SzF4/zw==",
- "dev": true,
- "license": "MIT",
- "dependencies": {
- "undici-types": "~6.21.0"
- }
- },
- "node_modules/@types/sql.js": {
- "version": "1.4.11",
- "resolved": "https://registry.npmjs.org/@types/sql.js/-/sql.js-1.4.11.tgz",
- "integrity": "sha512-QXIx38p2ZThJaK9vP5ZdqdlRe1FG9I8SmCZOS7FHfB/2qPAjZwkL7/vlfPg6N/oWHuuOaGg/P/IRwfP2W0kWVQ==",
- "dev": true,
- "license": "MIT",
- "dependencies": {
- "@types/emscripten": "*",
- "@types/node": "*"
- }
- },
"node_modules/@typescript-eslint/eslint-plugin": {
"version": "8.70.0",
"resolved": "https://registry.npmjs.org/@typescript-eslint/eslint-plugin/-/eslint-plugin-8.70.0.tgz",
@@ -4833,12 +4801,6 @@
"dev": true,
"license": "CC0-1.0"
},
- "node_modules/sql.js": {
- "version": "1.14.2",
- "resolved": "https://registry.npmjs.org/sql.js/-/sql.js-1.14.2.tgz",
- "integrity": "sha512-3ZGPovObMFrdw79zrUHbfdE/DLIsy8jdNdssmMSQuRAymedU6q84asPt0kgiqrdMYlPegDItiIMfmIXzZnYFcw==",
- "license": "MIT"
- },
"node_modules/stable-hash-x": {
"version": "0.2.0",
"resolved": "https://registry.npmjs.org/stable-hash-x/-/stable-hash-x-0.2.0.tgz",
@@ -5188,13 +5150,6 @@
"url": "https://github.com/sponsors/ljharb"
}
},
- "node_modules/undici-types": {
- "version": "6.21.0",
- "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.21.0.tgz",
- "integrity": "sha512-iwDZqg0QAGrg9Rav5H4n0M64c3mkR59cJ6wQp+7C4nI0gsmExaedaYLNO44eT4AtBBwjbTiGPMlt2Md0T9H9JQ==",
- "dev": true,
- "license": "MIT"
- },
"node_modules/unrs-resolver": {
"version": "1.12.2",
"resolved": "https://registry.npmjs.org/unrs-resolver/-/unrs-resolver-1.12.2.tgz",
diff --git a/core/client/package.json b/core/client/package.json
index cf2bb330d..35dec4e27 100644
--- a/core/client/package.json
+++ b/core/client/package.json
@@ -23,11 +23,9 @@
"@aliasvault/models": "file:../models",
"@aliasvault/vault": "file:../vault",
"fflate": "^0.8.3",
- "otpauth": "^9.3.6",
- "sql.js": "^1.12.0"
+ "otpauth": "^9.3.6"
},
"devDependencies": {
- "@types/sql.js": "^1.4.9",
"@typescript-eslint/eslint-plugin": "^8.65.0",
"@typescript-eslint/parser": "^8.65.0",
"eslint": "^9.39.5",
diff --git a/core/client/src/api/WebApiService.ts b/core/client/src/api/WebApiService.ts
index e675a7213..1cccb9fcf 100644
--- a/core/client/src/api/WebApiService.ts
+++ b/core/client/src/api/WebApiService.ts
@@ -83,12 +83,23 @@ export type EngineHttpResponse = {
* Service class for interacting with the web API.
*/
export class WebApiService {
+ /**
+ * The server API version this client speaks.
+ */
+ public static readonly API_VERSION = 2;
+
+ /**
+ * Build the versioned base URL for an API root URL, e.g. 'https://app.aliasvault.com/api' to 'https://app.aliasvault.com/api/v2/'.
+ */
+ public static versionedBaseUrl(apiUrl: string): string {
+ return apiUrl.replace(/\/$/, '') + `/v${WebApiService.API_VERSION}/`;
+ }
+
/**
* Get the base URL for the API from settings.
*/
private async getBaseUrl(): Promise<string> {
- const apiUrl = await this.getApiUrl();
- return apiUrl.replace(/\/$/, '') + '/v2/';
+ return WebApiService.versionedBaseUrl(await this.getApiUrl());
}
/**
diff --git a/core/client/src/auth/SrpAuthService.ts b/core/client/src/auth/SrpAuthService.ts
index 9be787c33..0c82f6a2f 100644
--- a/core/client/src/auth/SrpAuthService.ts
+++ b/core/client/src/auth/SrpAuthService.ts
@@ -1,3 +1,4 @@
+import { WebApiService } from '../api/WebApiService';
import { createAccountKeyHierarchy, type AccountKeyBlobs, type AccountKeyHierarchy } from '../crypto/AccountKeys';
import { EncryptionUtility } from '../crypto/EncryptionUtility';
import { rustCore } from '../rust/RustCore';
@@ -340,8 +341,7 @@ export class SrpAuthService {
// Prepare registration data
const prepared = await SrpAuthService.prepareRegistration(username, password);
- // Normalize the API URL
- const baseUrl = apiBaseUrl.replace(/\/$/, '') + '/v2/';
+ const baseUrl = WebApiService.versionedBaseUrl(apiBaseUrl);
// Send registration request to API
const response = await fetch(`${baseUrl}Auth/register`, {
@@ -404,7 +404,7 @@ export class SrpAuthService {
error?: string;
}> {
try {
- const baseUrl = apiBaseUrl.replace(/\/$/, '') + '/v2/';
+ const baseUrl = WebApiService.versionedBaseUrl(apiBaseUrl);
const normalizedUsername = SrpAuthService.normalizeUsername(username);
// Step 1: Initiate login
diff --git a/core/client/src/crypto/VaultBlob.ts b/core/client/src/crypto/VaultBlob.ts
index cf363c75c..d7184ef5c 100644
--- a/core/client/src/crypto/VaultBlob.ts
+++ b/core/client/src/crypto/VaultBlob.ts
@@ -14,7 +14,7 @@ const SQLITE_HEADER = new TextEncoder().encode('SQLite format 3\u0000');
/**
* Encrypt a plaintext SQLite database for local storage.
- * @param sqliteBytes - the database as exported by sql.js
+ * @param sqliteBytes - the database as exported by the SQLite engine
* @param key - the symmetric key the vault is stored under
* @returns Base64 of (IV | ciphertext | tag).
*/
diff --git a/core/client/src/database/BaseRepository.ts b/core/client/src/database/BaseRepository.ts
index d9dbee7bb..417eef20f 100644
--- a/core/client/src/database/BaseRepository.ts
+++ b/core/client/src/database/BaseRepository.ts
@@ -9,7 +9,7 @@ import type { ISqliteDatabase } from '../platform/SqliteEngine';
export type SqliteBindValue = string | number | null | Uint8Array;
/**
- * The database operations repositories need from their host, answered synchronously (sql.js) or asynchronously
+ * The database operations repositories need from their host, answered synchronously or asynchronously
* (the mobile native bridge).
*/
export interface IDatabaseClient {
@@ -26,7 +26,7 @@ export interface IDatabaseClient {
}
/**
- * A database client that answers every call synchronously, like sql.js.
+ * A database client that answers every call synchronously.
*/
export interface ISyncDatabaseClient extends IDatabaseClient {
getDb(): ISqliteDatabase | null;
diff --git a/core/client/src/database/DbOp.ts b/core/client/src/database/DbOp.ts
index ae0f29174..c1afea2fc 100644
--- a/core/client/src/database/DbOp.ts
+++ b/core/client/src/database/DbOp.ts
@@ -1,7 +1,7 @@
import type { IDatabaseClient, ISyncDatabaseClient, SqliteBindValue } from './BaseRepository';
/*
- * Repository methods are written once and can run on two kinds of host: synchronous (sql.js) and
+ * Repository methods are written once and can run on two kinds of host: synchronous and
* asynchronous (mobile native vault store).
*/
@@ -22,7 +22,7 @@ export type DbEffect =
| { kind: 'manifestScope' };
/**
- * A database operation that runs synchronously on sql.js and asynchronously over a native bridge.
+ * A database operation that can run both synchronously and asynchronously.
*/
export type DbOp<T> = Generator<DbEffect, T, unknown>;
diff --git a/core/client/src/database/RustSqliteEngine.ts b/core/client/src/database/RustSqliteEngine.ts
new file mode 100644
index 000000000..c1f6166a4
--- /dev/null
+++ b/core/client/src/database/RustSqliteEngine.ts
@@ -0,0 +1,73 @@
+/**
+ * The SQLite engine backed by the Rust core's bundled SQLite.
+ */
+import * as core from '../../wasm/aliasvault_core.js';
+
+import type { ISqliteDatabase, ISqliteEngine, ISqliteStatement, SqliteValue } from '../platform/SqliteEngine';
+import type { IRustCore } from '../rust/RustCoreBinding';
+
+/**
+ * A database held in the Rust core's memory, behind the engine interface.
+ */
+class RustSqliteDatabase implements ISqliteDatabase {
+ /**
+ * Wrap an open core database.
+ * @param db - the open database
+ */
+ public constructor(private readonly db: core.SqliteMemoryDatabase) {}
+
+ /** @inheritdoc */
+ public run(sql: string, params: SqliteValue[] = []): number {
+ return this.db.run(sql, params);
+ }
+
+ /** @inheritdoc */
+ public query<T>(sql: string, params: SqliteValue[] = []): T[] {
+ return this.db.query(sql, params) as T[];
+ }
+
+ /** @inheritdoc */
+ public exec(sql: string): void {
+ this.db.exec(sql);
+ }
+
+ /**
+ * The core caches prepared statements per connection, so binding the same SQL again is a lookup, not a parse.
+ * @param sql - the statement
+ */
+ public prepare(sql: string): ISqliteStatement {
+ return {
+ /** Bind and run. */
+ run: (params: SqliteValue[]): void => {
+ this.db.run(sql, params);
+ },
+ /** Nothing to release: the statement lives in the core's cache. */
+ finalize: (): void => {},
+ };
+ }
+
+ /** @inheritdoc */
+ public export(): Uint8Array {
+ return this.db.export();
+ }
+
+ /** @inheritdoc */
+ public close(): void {
+ this.db.close();
+ this.db.free();
+ }
+}
+
+/**
+ * Bind the engine interface to the Rust core's SQLite host.
+ * @param rustCore - the core binding, initialized before the first open
+ */
+export function createRustSqliteEngine(rustCore: IRustCore): ISqliteEngine {
+ return {
+ /** @inheritdoc */
+ open: async (bytes?: Uint8Array): Promise<ISqliteDatabase> => {
+ await rustCore.init();
+ return new RustSqliteDatabase(bytes ? core.SqliteMemoryDatabase.fromBytes(bytes) : core.SqliteMemoryDatabase.empty());
+ },
+ };
+}
diff --git a/core/client/src/database/SqlJsEngine.ts b/core/client/src/database/SqlJsEngine.ts
deleted file mode 100644
index 76f11405f..000000000
--- a/core/client/src/database/SqlJsEngine.ts
+++ /dev/null
@@ -1,103 +0,0 @@
-/**
- * The SQLite engine backed by sql.js, for hosts with a WebAssembly runtime (browser extension, web app, Node
- * tests).
- */
-import initSqlJs from 'sql.js';
-
-import type { ISqliteDatabase, ISqliteEngine, ISqliteStatement, SqliteValue } from '../platform/SqliteEngine';
-import type { Database, SqlJsStatic } from 'sql.js';
-
-/**
- * A sql.js database behind the engine interface.
- */
-class SqlJsDatabase implements ISqliteDatabase {
- /**
- * Wrap an open sql.js database.
- * @param db - the open sql.js database
- */
- public constructor(private readonly db: Database) {}
-
- /** @inheritdoc */
- public run(sql: string, params: SqliteValue[] = []): number {
- const statement = this.db.prepare(sql);
- try {
- statement.bind(params);
- statement.step();
- return this.db.getRowsModified();
- } finally {
- statement.free();
- }
- }
-
- /** @inheritdoc */
- public query<T>(sql: string, params: SqliteValue[] = []): T[] {
- const statement = this.db.prepare(sql);
- try {
- statement.bind(params);
- const rows: T[] = [];
- while (statement.step()) {
- rows.push(statement.getAsObject() as T);
- }
- return rows;
- } finally {
- statement.free();
- }
- }
-
- /** @inheritdoc */
- public exec(sql: string): void {
- this.db.run(sql);
- }
-
- /** @inheritdoc */
- public prepare(sql: string): ISqliteStatement {
- const statement = this.db.prepare(sql);
- return {
- /** Bind and run. */
- run: (params: SqliteValue[]): void => {
- statement.run(params);
- },
- /** Free the statement. */
- finalize: (): void => {
- statement.free();
- },
- };
- }
-
- /** @inheritdoc */
- public export(): Uint8Array {
- return this.db.export();
- }
-
- /** @inheritdoc */
- public close(): void {
- this.db.close();
- }
-}
-
-/**
- * Bind the engine interface to sql.js.
- * @param locateFile - resolves a sql.js support file (`sql-wasm.wasm`) to a URL the runtime can fetch
- */
-export function createSqlJsEngine(locateFile: (file: string) => string): ISqliteEngine {
- let sqlJs: Promise<SqlJsStatic> | null = null;
-
- /**
- * Load sql.js once; a failed load is forgotten so the next caller retries.
- */
- const load = (): Promise<SqlJsStatic> => {
- sqlJs ??= initSqlJs({ locateFile }).catch((error: unknown) => {
- sqlJs = null;
- throw error;
- });
- return sqlJs;
- };
-
- return {
- /** @inheritdoc */
- open: async (bytes?: Uint8Array): Promise<ISqliteDatabase> => {
- const SQL = await load();
- return new SqlJsDatabase(bytes ? new SQL.Database(bytes) : new SQL.Database());
- },
- };
-}
diff --git a/core/client/src/database/__tests__/LogoScoping.test.ts b/core/client/src/database/__tests__/LogoScoping.test.ts
index cd3a468fc..ac1c2d81a 100644
--- a/core/client/src/database/__tests__/LogoScoping.test.ts
+++ b/core/client/src/database/__tests__/LogoScoping.test.ts
@@ -1,10 +1,10 @@
-import initSqlJs from 'sql.js';
import { describe, expect, it } from 'vitest';
+import { getPlatform } from '../../platform/ClientPlatform';
import { BaseQueries } from '../queries/BaseQueries';
import { LogoQueries } from '../queries/LogoQueries';
-import type { Database } from 'sql.js';
+import type { ISqliteDatabase, SqliteValue } from '../../platform/SqliteEngine';
const PERSONAL = 'PERSONAL-MANIFEST';
const SHARED = 'SHARED-MANIFEST';
@@ -14,10 +14,9 @@ const SHARED = 'SHARED-MANIFEST';
* (composite (ManifestId, Id) keys, NOT NULL stamps).
* @returns The prepared database
*/
-async function makeDb(): Promise<Database> {
- const SQL = await initSqlJs();
- const db = new SQL.Database();
- db.run(`
+async function makeDb(): Promise<ISqliteDatabase> {
+ const db = await getPlatform().sqlite.open();
+ db.exec(`
CREATE TABLE Settings (ManifestId TEXT NOT NULL, "Key" TEXT NOT NULL, Value TEXT NULL, CreatedAt TEXT NOT NULL, UpdatedAt TEXT NOT NULL, IsDeleted INTEGER NOT NULL DEFAULT 0, PRIMARY KEY (ManifestId, "Key"));
CREATE TABLE Folders (ManifestId TEXT NOT NULL, Id TEXT NOT NULL, Name TEXT, ParentFolderId TEXT, PRIMARY KEY (ManifestId, Id));
CREATE TABLE Logos (ManifestId TEXT NOT NULL, Id TEXT NOT NULL, Kind TEXT NOT NULL DEFAULT 'favicon', Source TEXT NOT NULL, FileData BLOB, MimeType TEXT, Name TEXT, CreatedAt TEXT NOT NULL, UpdatedAt TEXT NOT NULL, IsDeleted INTEGER NOT NULL, PRIMARY KEY (ManifestId, Id));
@@ -36,15 +35,8 @@ async function makeDb(): Promise<Database> {
* @param params - Bound parameters
* @returns The rows
*/
-function rows(db: Database, sql: string, params: unknown[] = []): Record<string, unknown>[] {
- const stmt = db.prepare(sql);
- stmt.bind(params as never);
- const out: Record<string, unknown>[] = [];
- while (stmt.step()) {
- out.push(stmt.getAsObject());
- }
- stmt.free();
- return out;
+function rows(db: ISqliteDatabase, sql: string, params: SqliteValue[] = []): Record<string, unknown>[] {
+ return db.query<Record<string, unknown>>(sql, params);
}
describe('logo manifest scoping', () => {
diff --git a/core/client/src/platform/ClientPlatform.ts b/core/client/src/platform/ClientPlatform.ts
index 9f3b3de27..b557c07ed 100644
--- a/core/client/src/platform/ClientPlatform.ts
+++ b/core/client/src/platform/ClientPlatform.ts
@@ -22,7 +22,7 @@ export interface IClientPlatform {
/** The Rust core: WebAssembly on the web hosts (see WasmRustCore), the native uniffi bindings on mobile. */
rustCore: IRustCore;
- /** The SQLite engine the vault is opened with: sql.js on the web hosts (see SqlJsEngine), unavailable on mobile. */
+ /** The SQLite engine the vault is opened with. */
sqlite: ISqliteEngine;
/**
diff --git a/core/client/src/platform/SqliteEngine.ts b/core/client/src/platform/SqliteEngine.ts
index 02ed73310..2def81ef7 100644
--- a/core/client/src/platform/SqliteEngine.ts
+++ b/core/client/src/platform/SqliteEngine.ts
@@ -56,8 +56,7 @@ export interface ISqliteDatabase {
}
/**
- * The in-JS SQLite engine a host provides: sql.js on the browser and web hosts (see SqlJsEngine). Databases are opened
- * in memory only. The mobile host provides none, since its vault database is owned by the native vault store.
+ * The SQLite engine a host provides. The database is designed to be opened in memory only.
*/
export interface ISqliteEngine {
/**
diff --git a/core/client/src/sync/ManifestIdsInVault.test.ts b/core/client/src/sync/ManifestIdsInVault.test.ts
index bee5fc170..d4157e876 100644
--- a/core/client/src/sync/ManifestIdsInVault.test.ts
+++ b/core/client/src/sync/ManifestIdsInVault.test.ts
@@ -1,10 +1,11 @@
-import initSqlJs from 'sql.js';
import { describe, expect, it } from 'vitest';
+import { getPlatform } from '../platform/ClientPlatform';
+
import { VaultCodec } from './VaultCodec';
import type SqliteClient from '../database/SqliteClient';
-import type { Database } from 'sql.js';
+import type { ISqliteDatabase, SqliteValue } from '../platform/SqliteEngine';
const PERSONAL = 'PERSONAL-MANIFEST';
const SHARED = 'SHARED-MANIFEST';
@@ -14,7 +15,7 @@ const SHARED = 'SHARED-MANIFEST';
* @param db - the database to read
* @returns A client that can answer `executeQuery`
*/
-function asClient(db: Database): SqliteClient {
+function asClient(db: ISqliteDatabase): SqliteClient {
return {
/**
* Run a statement and return its rows as plain objects.
@@ -22,16 +23,7 @@ function asClient(db: Database): SqliteClient {
* @param params - bound parameters
* @returns The rows
*/
- executeQuery: <T>(query: string, params: unknown[] = []): T[] => {
- const stmt = db.prepare(query);
- stmt.bind(params as never);
- const out: T[] = [];
- while (stmt.step()) {
- out.push(stmt.getAsObject() as T);
- }
- stmt.free();
- return out;
- },
+ executeQuery: <T>(query: string, params: unknown[] = []): T[] => db.query<T>(query, params as SqliteValue[]),
} as unknown as SqliteClient;
}
@@ -39,10 +31,9 @@ function asClient(db: Database): SqliteClient {
* Build a vault holding a stamped table, an unstamped one, and a table this check must discover on its own.
* @returns The prepared database
*/
-async function makeDb(): Promise<Database> {
- const SQL = await initSqlJs();
- const db = new SQL.Database();
- db.run(`
+async function makeDb(): Promise<ISqliteDatabase> {
+ const db = await getPlatform().sqlite.open();
+ db.exec(`
CREATE TABLE Folders (ManifestId TEXT NOT NULL, Id TEXT NOT NULL, Name TEXT, PRIMARY KEY (ManifestId, Id));
CREATE TABLE Items (ManifestId TEXT NOT NULL, Id TEXT NOT NULL, PRIMARY KEY (ManifestId, Id));
CREATE TABLE __EFMigrationsHistory (MigrationId TEXT NOT NULL PRIMARY KEY, ProductVersion TEXT NOT NULL);
@@ -61,13 +52,13 @@ describe('VaultCodec.manifestIdsInVault', () => {
it('finds a table nothing lists, because it reads the stamp column off the schema', async () => {
const db = await makeDb();
- db.run(`CREATE TABLE SomethingNewer (ManifestId TEXT NOT NULL, Id TEXT NOT NULL); INSERT INTO SomethingNewer VALUES ('THIRD-MANIFEST', 'X');`);
+ db.exec(`CREATE TABLE SomethingNewer (ManifestId TEXT NOT NULL, Id TEXT NOT NULL); INSERT INTO SomethingNewer VALUES ('THIRD-MANIFEST', 'X');`);
expect(VaultCodec.manifestIdsInVault(asClient(db)).has('THIRD-MANIFEST')).toBe(true);
});
it('keeps the stamp exactly as written, since that is how the codec routes it', async () => {
const db = await makeDb();
- db.run(`INSERT INTO Items VALUES ('${SHARED.toLowerCase()}', 'ITEM-OTHERCASE');`);
+ db.exec(`INSERT INTO Items VALUES ('${SHARED.toLowerCase()}', 'ITEM-OTHERCASE');`);
const ids = VaultCodec.manifestIdsInVault(asClient(db));
expect(ids.has(SHARED)).toBe(true);
expect(ids.has(SHARED.toLowerCase())).toBe(true);
@@ -75,15 +66,14 @@ describe('VaultCodec.manifestIdsInVault', () => {
it('ignores unstamped rows and unstamped tables', async () => {
const db = await makeDb();
- db.run(`CREATE TABLE Unstamped (Id TEXT NOT NULL); INSERT INTO Unstamped VALUES ('X');`);
- db.run(`CREATE TABLE Loose (ManifestId TEXT, Id TEXT NOT NULL); INSERT INTO Loose VALUES (NULL, 'A'), ('', 'B');`);
+ db.exec(`CREATE TABLE Unstamped (Id TEXT NOT NULL); INSERT INTO Unstamped VALUES ('X');`);
+ db.exec(`CREATE TABLE Loose (ManifestId TEXT, Id TEXT NOT NULL); INSERT INTO Loose VALUES (NULL, 'A'), ('', 'B');`);
expect([...VaultCodec.manifestIdsInVault(asClient(db))].sort()).toEqual([PERSONAL, SHARED]);
});
it('is empty for a vault predating the stamps, so a legacy vault never looks unwritable', async () => {
- const SQL = await initSqlJs();
- const db = new SQL.Database();
- db.run(`CREATE TABLE Folders (Id TEXT NOT NULL PRIMARY KEY, Name TEXT); INSERT INTO Folders VALUES ('FOLDER-LEGACY', 'Mine');`);
+ const db = await getPlatform().sqlite.open();
+ db.exec(`CREATE TABLE Folders (Id TEXT NOT NULL PRIMARY KEY, Name TEXT); INSERT INTO Folders VALUES ('FOLDER-LEGACY', 'Mine');`);
expect(VaultCodec.manifestIdsInVault(asClient(db)).size).toBe(0);
});
});
diff --git a/core/client/src/sync/VaultCodec.ts b/core/client/src/sync/VaultCodec.ts
index 26fc0bc31..d65d4e12b 100644
--- a/core/client/src/sync/VaultCodec.ts
+++ b/core/client/src/sync/VaultCodec.ts
@@ -307,7 +307,7 @@ export class VaultCodec {
/**
* Normalize a SQLite-returned row into a JSON-safe shape: Uint8Array bytes become `{ __b64: ... }`.
- * @param row - raw row from sql.js
+ * @param row - raw row from the SQLite engine
*/
private static normalizeRowForJson(row: Record<string, unknown>): Record<string, unknown> {
/*
diff --git a/core/client/vitest.setup.ts b/core/client/vitest.setup.ts
index 7ce73335f..e4fb2d1b1 100644
--- a/core/client/vitest.setup.ts
+++ b/core/client/vitest.setup.ts
@@ -1,18 +1,16 @@
import { readFileSync } from 'node:fs';
-import { createRequire } from 'node:module';
import path from 'node:path';
-import { createSqlJsEngine } from './src/database/SqlJsEngine';
+import { createRustSqliteEngine } from './src/database/RustSqliteEngine';
import { setPlatform } from './src/platform/ClientPlatform';
import { createInMemoryPlatform } from './src/platform/InMemoryPlatform';
import { createWasmRustCore } from './src/rust/WasmRustCore';
-const require = createRequire(import.meta.url);
-
/*
- * Registers a Node-backed platform for the unit tests: WASM from the local build output, sql.js from node_modules.
+ * Registers a Node-backed platform for the unit tests.
*/
+const rustCore = createWasmRustCore(async (): Promise<BufferSource> => readFileSync(path.join(import.meta.dirname, 'wasm/aliasvault_core_bg.wasm')));
setPlatform(createInMemoryPlatform({
- rustCore: createWasmRustCore(async (): Promise<BufferSource> => readFileSync(path.join(import.meta.dirname, 'wasm/aliasvault_core_bg.wasm'))),
- sqlite: createSqlJsEngine((file: string): string => path.join(path.dirname(require.resolve('sql.js')), file)),
+ rustCore,
+ sqlite: createRustSqliteEngine(rustCore),
}));
diff --git a/core/rust/Cargo.lock b/core/rust/Cargo.lock
index 75a743c20..2b73f0330 100644
--- a/core/rust/Cargo.lock
+++ b/core/rust/Cargo.lock
@@ -43,18 +43,6 @@ dependencies = [
"subtle",
]
-[[package]]
-name = "ahash"
-version = "0.8.12"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "5a15f179cd60c4584b8a8c596927aadc462e27f2ca70c04e0071964a73ba7a75"
-dependencies = [
- "cfg-if",
- "once_cell",
- "version_check",
- "zerocopy",
-]
-
[[package]]
name = "aliasvault-core"
version = "0.31.0"
@@ -68,6 +56,7 @@ dependencies = [
"flate2",
"getrandom 0.2.17",
"getrandom 0.3.4",
+ "js-sys",
"mail-parser",
"num-bigint",
"rand 0.9.3",
@@ -80,7 +69,7 @@ dependencies = [
"sha2",
"srp",
"subtle",
- "thiserror",
+ "thiserror 1.0.69",
"uniffi",
"wasm-bindgen",
"zeroize",
@@ -177,7 +166,7 @@ dependencies = [
"proc-macro2",
"quote",
"serde",
- "syn",
+ "syn 2.0.111",
]
[[package]]
@@ -296,7 +285,7 @@ dependencies = [
"semver",
"serde",
"serde_json",
- "thiserror",
+ "thiserror 1.0.69",
]
[[package]]
@@ -368,7 +357,7 @@ dependencies = [
"heck",
"proc-macro2",
"quote",
- "syn",
+ "syn 2.0.111",
]
[[package]]
@@ -503,6 +492,12 @@ dependencies = [
"miniz_oxide",
]
+[[package]]
+name = "foldhash"
+version = "0.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "77ce24cb58228fbb8aa041425bb1050850ac19177686ea6e0f41a70416f56fdb"
+
[[package]]
name = "fs-err"
version = "2.11.0"
@@ -578,11 +573,11 @@ dependencies = [
[[package]]
name = "hashbrown"
-version = "0.14.5"
+version = "0.16.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1"
+checksum = "841d1cc9bed7f9236f321df977030373f4a4163ae1a7dbfe1a51a2c1a51d9100"
dependencies = [
- "ahash",
+ "foldhash",
]
[[package]]
@@ -590,6 +585,9 @@ name = "hashbrown"
version = "0.17.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a"
+dependencies = [
+ "foldhash",
+]
[[package]]
name = "hashify"
@@ -600,16 +598,16 @@ dependencies = [
"indexmap",
"proc-macro2",
"quote",
- "syn",
+ "syn 2.0.111",
]
[[package]]
name = "hashlink"
-version = "0.9.1"
+version = "0.12.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "6ba4ff7128dee98c7dc9794b6a411377e1404dba1c97deb8d1a55297bd25d8af"
+checksum = "a596f1b20ed2cc5ecac41a164aaebc7258057060f06c0cf7a2ba3991ee7990fb"
dependencies = [
- "hashbrown 0.14.5",
+ "hashbrown 0.17.1",
]
[[package]]
@@ -682,9 +680,9 @@ checksum = "b6d2cec3eae94f9f509c767b45932f1ada8350c4bdb85af2fcab4a3c14807981"
[[package]]
name = "libsqlite3-sys"
-version = "0.30.1"
+version = "0.38.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "2e99fb7a497b1e3339bc746195567ed8d3e24945ecd636e3619d20b9de9e9149"
+checksum = "f1d20bef17f513b9b3004532233187769cd072d790971f4e4da0e346eb6401e8"
dependencies = [
"cc",
"pkg-config",
@@ -1010,11 +1008,21 @@ dependencies = [
"zeroize",
]
+[[package]]
+name = "rsqlite-vfs"
+version = "0.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c51c9ae4df8a7fba42103df5c621fa3c37eccf3a3c650879e90fc48b11cc192c"
+dependencies = [
+ "hashbrown 0.16.1",
+ "thiserror 2.0.20",
+]
+
[[package]]
name = "rusqlite"
-version = "0.32.1"
+version = "0.40.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "7753b721174eb8ff87a9a0e799e2d7bc3749323e773db92e0984debb00019d6e"
+checksum = "23f2a97da3e3873c73cb2a2e71b35c40ff95e0b1eefa8d72d8499a6928c3b5b3"
dependencies = [
"bitflags",
"fallible-iterator",
@@ -1022,6 +1030,7 @@ dependencies = [
"hashlink",
"libsqlite3-sys",
"smallvec",
+ "sqlite-wasm-rs",
]
[[package]]
@@ -1053,7 +1062,7 @@ checksum = "1783eabc414609e28a5ba76aee5ddd52199f7107a0b24c2e9746a1ecc34a683d"
dependencies = [
"proc-macro2",
"quote",
- "syn",
+ "syn 2.0.111",
]
[[package]]
@@ -1104,7 +1113,7 @@ checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79"
dependencies = [
"proc-macro2",
"quote",
- "syn",
+ "syn 2.0.111",
]
[[package]]
@@ -1187,6 +1196,18 @@ dependencies = [
"der",
]
+[[package]]
+name = "sqlite-wasm-rs"
+version = "0.5.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "dc3efc0da82635d7e1ced0053bbbfa8c7ab9645d0bf36ceb4f7127bb85315d75"
+dependencies = [
+ "cc",
+ "js-sys",
+ "rsqlite-vfs",
+ "wasm-bindgen",
+]
+
[[package]]
name = "srp"
version = "0.6.0"
@@ -1229,6 +1250,17 @@ dependencies = [
"unicode-ident",
]
+[[package]]
+name = "syn"
+version = "3.0.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "12df2e0110f65b775f769bb17ef989067a1d931b2eb822bd4346631eeada89f9"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "unicode-ident",
+]
+
[[package]]
name = "textwrap"
version = "0.16.2"
@@ -1244,7 +1276,16 @@ version = "1.0.69"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52"
dependencies = [
- "thiserror-impl",
+ "thiserror-impl 1.0.69",
+]
+
+[[package]]
+name = "thiserror"
+version = "2.0.20"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ec86235f5fcc2a73650310756d2ac5b138a5780bbbdfae3eeccec992c435ba4f"
+dependencies = [
+ "thiserror-impl 2.0.20",
]
[[package]]
@@ -1255,7 +1296,18 @@ checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1"
dependencies = [
"proc-macro2",
"quote",
- "syn",
+ "syn 2.0.111",
+]
+
+[[package]]
+name = "thiserror-impl"
+version = "2.0.20"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bc04cd3e1236dd4a98afca4569f2deb3f120e5422a4023be2cb683f8486292af"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn 3.0.5",
]
[[package]]
@@ -1342,7 +1394,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "802d2051a700e3ec894c79f80d2705b69d85844dafbbe5d1a92776f8f48b563a"
dependencies = [
"quote",
- "syn",
+ "syn 2.0.111",
]
[[package]]
@@ -1372,7 +1424,7 @@ dependencies = [
"proc-macro2",
"quote",
"serde",
- "syn",
+ "syn 2.0.111",
"toml",
"uniffi_meta",
]
@@ -1490,7 +1542,7 @@ dependencies = [
"bumpalo",
"proc-macro2",
"quote",
- "syn",
+ "syn 2.0.111",
"wasm-bindgen-shared",
]
@@ -1550,7 +1602,7 @@ checksum = "2c7962b26b0a8685668b671ee4b54d007a67d4eaf05fda79ac0ecf41e32270f1"
dependencies = [
"proc-macro2",
"quote",
- "syn",
+ "syn 2.0.111",
]
[[package]]
@@ -1570,5 +1622,5 @@ checksum = "3c50655cbb0fe3fc43170059e702f1ce5e19b84cec58dc87b037a09935c2f328"
dependencies = [
"proc-macro2",
"quote",
- "syn",
+ "syn 2.0.111",
]
diff --git a/core/rust/Cargo.toml b/core/rust/Cargo.toml
index 8d167e68b..79aece945 100644
--- a/core/rust/Cargo.toml
+++ b/core/rust/Cargo.toml
@@ -21,9 +21,11 @@ uniffi = ["dep:uniffi"]
# Feature for UniFFI CLI bindgen tool (only for generating bindings, not for library)
uniffi-cli = ["uniffi/cli"]
# Feature for WASM builds (browser extension)
-wasm = ["dep:wasm-bindgen", "dep:serde-wasm-bindgen", "dep:console_error_panic_hook"]
+wasm = ["dep:wasm-bindgen", "dep:serde-wasm-bindgen", "dep:console_error_panic_hook", "dep:js-sys"]
# Feature for C FFI exports (.NET P/Invoke)
ffi = []
+# Feature for the in-memory SQLite host
+sqlite = ["dep:rusqlite"]
[dependencies]
# Serialization - core dependency for JSON handling
@@ -68,18 +70,22 @@ uniffi = { version = "0.28", optional = true }
# WASM bindings (optional)
wasm-bindgen = { version = "0.2", optional = true }
serde-wasm-bindgen = { version = "0.6", optional = true }
+js-sys = { version = "0.3", optional = true }
console_error_panic_hook = { version = "0.1", optional = true }
# Parse emails from source
mail-parser = { version = "0.11.6", features = ["full_encoding"] }
+# SQLite client which can be used by all host apps for uniform access
+rusqlite = { version = "0.40", features = ["bundled", "serialize"], optional = true }
+
# rsa 0.9 pulls rand 0.8 / getrandom 0.2, which only builds for wasm32-unknown-unknown with its "js" feature.
[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom02 = { package = "getrandom", version = "0.2", features = ["js"] }
[dev-dependencies]
# The sync engine tests drive the engine against a real SQLite host.
-rusqlite = { version = "0.32", features = ["bundled", "serialize", "backup"] }
+rusqlite = { version = "0.40", features = ["bundled", "serialize"] }
# Build dependencies for UniFFI
[build-dependencies]
@@ -98,10 +104,15 @@ opt-level = 0
debug = false
incremental = true
-# Release profile - optimized for size
+# Release profile - optimized for size (browser WASM and .NET)
[profile.release]
opt-level = "z"
lto = "fat"
codegen-units = 1
panic = "abort"
strip = "symbols"
+
+# Mobile profile - optimized for speed as increased bundle size is less relevant as its a one time download
+[profile.mobile]
+inherits = "release"
+opt-level = 3
diff --git a/core/rust/build.sh b/core/rust/build.sh
index 2e9d12974..da66c9af3 100755
--- a/core/rust/build.sh
+++ b/core/rust/build.sh
@@ -195,9 +195,9 @@ build_browser() {
# Build with wasm-pack
echo -e " Running wasm-pack build..."
if $FAST_MODE; then
- wasm-pack build --dev --target web --out-dir "$WASM_DIR" --features wasm
+ wasm-pack build --dev --target web --out-dir "$WASM_DIR" --features wasm,sqlite
else
- wasm-pack build --release --target web --out-dir "$WASM_DIR" --features wasm
+ wasm-pack build --release --target web --out-dir "$WASM_DIR" --features wasm,sqlite
fi
local end_time=$(date +%s)
@@ -602,21 +602,21 @@ build_android() {
CC="$toolchain/bin/aarch64-linux-android${api_level}-clang" \
CXX="$toolchain/bin/aarch64-linux-android${api_level}-clang++" \
CARGO_TARGET_AARCH64_LINUX_ANDROID_LINKER="$toolchain/bin/aarch64-linux-android${api_level}-clang" \
- cargo build $cargo_flags --target aarch64-linux-android --features uniffi
+ cargo build $cargo_flags --target aarch64-linux-android --features uniffi,sqlite
echo -e " Building for armeabi-v7a..."
AR="$toolchain/bin/llvm-ar" \
CC="$toolchain/bin/armv7a-linux-androideabi${api_level}-clang" \
CXX="$toolchain/bin/armv7a-linux-androideabi${api_level}-clang++" \
CARGO_TARGET_ARMV7_LINUX_ANDROIDEABI_LINKER="$toolchain/bin/armv7a-linux-androideabi${api_level}-clang" \
- cargo build $cargo_flags --target armv7-linux-androideabi --features uniffi
+ cargo build $cargo_flags --target armv7-linux-androideabi --features uniffi,sqlite
echo -e " Building for x86_64..."
AR="$toolchain/bin/llvm-ar" \
CC="$toolchain/bin/x86_64-linux-android${api_level}-clang" \
CXX="$toolchain/bin/x86_64-linux-android${api_level}-clang++" \
CARGO_TARGET_X86_64_LINUX_ANDROID_LINKER="$toolchain/bin/x86_64-linux-android${api_level}-clang" \
- cargo build $cargo_flags --target x86_64-linux-android --features uniffi
+ cargo build $cargo_flags --target x86_64-linux-android --features uniffi,sqlite
# Copy libraries
cp "target/aarch64-linux-android/$cargo_profile/libaliasvault_core.so" "$ANDROID_DIR/arm64-v8a/"
@@ -659,7 +659,7 @@ build_android() {
esac
echo -e " Building native library for bindgen..."
- cargo build --features uniffi --lib
+ cargo build --features uniffi,sqlite --lib
# Generate bindings from native library
cargo run --features uniffi-cli --bin uniffi-bindgen -- generate \
diff --git a/core/rust/src/lib.rs b/core/rust/src/lib.rs
index 84502934f..acad78f12 100644
--- a/core/rust/src/lib.rs
+++ b/core/rust/src/lib.rs
@@ -7,6 +7,7 @@
//! - **vault_sharing**: Sharing write logic for multi-manifest vaults
//! - **vault_pruner**: Prunes expired items from trash (30-day retention)
//! - **vault_sync**: the vault sync engine that every client uses for syncing with the server
+//! - **sqlite_host**: an in-memory SQLite database for hosts that cannot open one from bytes (`sqlite` feature)
//! - **credential_matcher**: Cross-platform credential filtering for autofill
//! - **email_parser**: RFC 822 email parsing into bodies and attachment metadata
//! - **favicon**: Favicon handling and source selection
@@ -36,6 +37,8 @@ pub mod password_generator;
pub mod identity_generator;
pub mod crypto;
pub mod vault_sync;
+#[cfg(any(feature = "sqlite", test))]
+pub mod sqlite_host;
pub use error::VaultError;
pub use vault_merge::{merge_canonical, CanonicalManifestMerge, CanonicalMergeInput, CanonicalMergeOutput, MergeStats, SqlStatement};
diff --git a/core/rust/src/sqlite_host.rs b/core/rust/src/sqlite_host.rs
new file mode 100644
index 000000000..2685474ad
--- /dev/null
+++ b/core/rust/src/sqlite_host.rs
@@ -0,0 +1,198 @@
+//! An in-memory SQLite database that can be used by host applications to be have uniform access to the database.
+//!
+//! The database lives in SQLite's own memory and are never persisted to the filesystem.
+
+use std::sync::Mutex;
+
+use base64::engine::general_purpose::STANDARD as BASE64;
+use base64::Engine;
+use rusqlite::types::{ToSql, ToSqlOutput, Value as RsValue, ValueRef};
+use rusqlite::{params_from_iter, Connection, MAIN_DB};
+use serde_json::{json, Map, Value};
+
+use crate::error::{VaultError, VaultResult};
+use crate::vault_sync::types::SqlStatement;
+
+/// A SQLite value as the bindings see it, typed and without JSON encoding.
+#[derive(Debug, Clone, PartialEq)]
+#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
+pub enum SqlValue {
+ Null,
+ Integer(i64),
+ Real(f64),
+ Text(String),
+ Blob(Vec<u8>),
+}
+
+/// The rows of one query, positional, with the column names once.
+#[derive(Debug, Clone, PartialEq)]
+#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
+pub struct SqlResult {
+ pub columns: Vec<String>,
+ pub rows: Vec<Vec<SqlValue>>,
+}
+
+/// One in-memory SQLite database.
+pub struct MemoryDatabase {
+ conn: Mutex<Connection>,
+}
+
+impl MemoryDatabase {
+ /// Open a database from its file bytes.
+ pub fn from_bytes(bytes: &[u8]) -> VaultResult<Self> {
+ let mut conn = Connection::open_in_memory().map_err(sql_error)?;
+ deserialize_into(&mut conn, bytes)?;
+ Ok(Self { conn: Mutex::new(conn) })
+ }
+
+ /// Open an empty database.
+ pub fn empty() -> VaultResult<Self> {
+ Ok(Self { conn: Mutex::new(Connection::open_in_memory().map_err(sql_error)?) })
+ }
+
+ /// Open an empty database and run a schema script on it.
+ pub fn with_schema(schema_sql: &str) -> VaultResult<Self> {
+ let conn = Connection::open_in_memory().map_err(sql_error)?;
+ conn.execute_batch(schema_sql).map_err(sql_error)?;
+ Ok(Self { conn: Mutex::new(conn) })
+ }
+
+ /// Run a SQL script (several statements, no parameters).
+ pub fn execute_batch(&self, sql: &str) -> VaultResult<()> {
+ self.lock().execute_batch(sql).map_err(sql_error)
+ }
+
+ /// Run one query and return its rows as JSON objects keyed by column name.
+ pub fn query(&self, sql: &str, params: &[Value]) -> VaultResult<Vec<Map<String, Value>>> {
+ query(&self.lock(), sql, params)
+ }
+
+ /// Run statements in one transaction; any failure rolls all of them back.
+ pub fn exec(&self, statements: &[SqlStatement]) -> VaultResult<()> {
+ exec(&self.lock(), statements)
+ }
+
+ /// Run one query with typed parameters and return its rows positionally.
+ pub fn query_values(&self, sql: &str, params: &[SqlValue]) -> VaultResult<SqlResult> {
+ let conn = self.lock();
+ let mut statement = conn.prepare_cached(sql).map_err(|e| VaultError::General(format!("{} ({})", e, sql)))?;
+ let columns: Vec<String> = statement.column_names().iter().map(|c| c.to_string()).collect();
+ let mut rows = statement.query(params_from_iter(params.iter())).map_err(|e| VaultError::General(format!("{} ({})", e, sql)))?;
+ let mut out = Vec::new();
+ while let Some(row) = rows.next().map_err(sql_error)? {
+ let mut values = Vec::with_capacity(columns.len());
+ for index in 0..columns.len() {
+ values.push(SqlValue::from(row.get_ref(index).map_err(sql_error)?));
+ }
+ out.push(values);
+ }
+ Ok(SqlResult { columns, rows: out })
+ }
+
+ /// Run one statement with typed parameters and return the rows it changed.
+ pub fn execute(&self, sql: &str, params: &[SqlValue]) -> VaultResult<u64> {
+ let conn = self.lock();
+ let mut statement = conn.prepare_cached(sql).map_err(|e| VaultError::General(format!("{} ({})", e, sql)))?;
+ let mut rows = statement.query(params_from_iter(params.iter())).map_err(|e| VaultError::General(format!("{} ({})", e, sql)))?;
+ while rows.next().map_err(sql_error)?.is_some() {}
+ Ok(conn.changes())
+ }
+
+ /// The database as SQLite file bytes.
+ pub fn export(&self) -> VaultResult<Vec<u8>> {
+ Ok(self.lock().serialize(MAIN_DB).map_err(sql_error)?.to_vec())
+ }
+
+ fn lock(&self) -> std::sync::MutexGuard<'_, Connection> {
+ self.conn.lock().unwrap_or_else(|poisoned| poisoned.into_inner())
+ }
+}
+
+/// Load SQLite file bytes into a connection's main database, held in SQLite-owned memory.
+pub fn deserialize_into(conn: &mut Connection, bytes: &[u8]) -> VaultResult<()> {
+ if bytes.is_empty() {
+ return Err(VaultError::General("Cannot open a SQLite database from zero bytes".to_string()));
+ }
+ conn.deserialize_read_exact(MAIN_DB, bytes, bytes.len(), false).map_err(sql_error)
+}
+
+fn sql_error(error: rusqlite::Error) -> VaultError {
+ VaultError::General(error.to_string())
+}
+
+impl ToSql for SqlValue {
+ fn to_sql(&self) -> rusqlite::Result<ToSqlOutput<'_>> {
+ Ok(match self {
+ SqlValue::Null => ToSqlOutput::Borrowed(ValueRef::Null),
+ SqlValue::Integer(i) => ToSqlOutput::Borrowed(ValueRef::Integer(*i)),
+ SqlValue::Real(f) => ToSqlOutput::Borrowed(ValueRef::Real(*f)),
+ SqlValue::Text(t) => ToSqlOutput::Borrowed(ValueRef::Text(t.as_bytes())),
+ SqlValue::Blob(b) => ToSqlOutput::Borrowed(ValueRef::Blob(b)),
+ })
+ }
+}
+
+impl From<ValueRef<'_>> for SqlValue {
+ fn from(value: ValueRef<'_>) -> Self {
+ match value {
+ ValueRef::Null => SqlValue::Null,
+ ValueRef::Integer(i) => SqlValue::Integer(i),
+ ValueRef::Real(f) => SqlValue::Real(f),
+ ValueRef::Text(t) => SqlValue::Text(String::from_utf8_lossy(t).to_string()),
+ ValueRef::Blob(b) => SqlValue::Blob(b.to_vec()),
+ }
+ }
+}
+
+/// A JSON parameter as a SQLite value.
+pub fn to_sql(value: &Value) -> RsValue {
+ match value {
+ Value::Null => RsValue::Null,
+ Value::Bool(b) => RsValue::Integer(*b as i64),
+ Value::Number(n) => n.as_i64().map(RsValue::Integer).unwrap_or_else(|| RsValue::Real(n.as_f64().unwrap_or(0.0))),
+ Value::String(s) => RsValue::Text(s.clone()),
+ Value::Object(o) if o.contains_key("__b64") => RsValue::Blob(BASE64.decode(o["__b64"].as_str().unwrap_or("")).unwrap_or_default()),
+ other => RsValue::Text(other.to_string()),
+ }
+}
+
+/// A SQLite value as JSON.
+pub fn from_sql(value: ValueRef<'_>) -> Value {
+ match value {
+ ValueRef::Null => Value::Null,
+ ValueRef::Integer(i) => json!(i),
+ ValueRef::Real(f) => json!(f),
+ ValueRef::Text(t) => Value::String(String::from_utf8_lossy(t).to_string()),
+ ValueRef::Blob(b) => json!({ "__b64": BASE64.encode(b) }),
+ }
+}
+
+/// Run one query on a connection and return its rows as JSON objects keyed by column name.
+pub fn query(conn: &Connection, sql: &str, params: &[Value]) -> VaultResult<Vec<Map<String, Value>>> {
+ let mut statement = conn.prepare(sql).map_err(|e| VaultError::General(format!("{} ({})", e, sql)))?;
+ let columns: Vec<String> = statement.column_names().iter().map(|c| c.to_string()).collect();
+ let values: Vec<RsValue> = params.iter().map(to_sql).collect();
+ let mut rows = statement.query(params_from_iter(values.iter())).map_err(|e| VaultError::General(format!("{} ({})", e, sql)))?;
+ let mut out = Vec::new();
+ while let Some(row) = rows.next().map_err(sql_error)? {
+ let mut object = Map::new();
+ for (index, column) in columns.iter().enumerate() {
+ object.insert(column.clone(), from_sql(row.get_ref(index).map_err(sql_error)?));
+ }
+ out.push(object);
+ }
+ Ok(out)
+}
+
+/// Run statements on a connection atomically.
+pub fn exec(conn: &Connection, statements: &[SqlStatement]) -> VaultResult<()> {
+ conn.execute_batch("SAVEPOINT exec_batch").map_err(sql_error)?;
+ for statement in statements {
+ let values: Vec<RsValue> = statement.params.iter().map(to_sql).collect();
+ if let Err(error) = conn.execute(&statement.sql, params_from_iter(values.iter())) {
+ let _ = conn.execute_batch("ROLLBACK TO exec_batch; RELEASE exec_batch");
+ return Err(VaultError::General(format!("{} ({})", error, statement.sql)));
+ }
+ }
+ conn.execute_batch("RELEASE exec_batch").map_err(sql_error)
+}
diff --git a/core/rust/src/uniffi_api.rs b/core/rust/src/uniffi_api.rs
index d5c7bdeb5..e4b0cb22e 100644
--- a/core/rust/src/uniffi_api.rs
+++ b/core/rust/src/uniffi_api.rs
@@ -633,3 +633,58 @@ impl VaultSyncSession {
self.inner.resume(&response_json)
}
}
+
+/// An in-memory SQLite database that can be used by host applications to be have uniform access to the database.
+#[cfg(feature = "sqlite")]
+#[derive(uniffi::Object)]
+pub struct SqliteMemoryDatabase {
+ inner: crate::sqlite_host::MemoryDatabase,
+}
+
+#[cfg(feature = "sqlite")]
+#[uniffi::export]
+impl SqliteMemoryDatabase {
+ /// Open a database from its SQLite file bytes.
+ #[uniffi::constructor]
+ pub fn from_bytes(bytes: Vec<u8>) -> Result<std::sync::Arc<Self>, VaultError> {
+ Ok(std::sync::Arc::new(Self { inner: crate::sqlite_host::MemoryDatabase::from_bytes(&bytes)? }))
+ }
+
+ /// Open an empty database and run a schema script on it.
+ #[uniffi::constructor]
+ pub fn with_schema(schema_sql: String) -> Result<std::sync::Arc<Self>, VaultError> {
+ Ok(std::sync::Arc::new(Self { inner: crate::sqlite_host::MemoryDatabase::with_schema(&schema_sql)? }))
+ }
+
+ /// Run a SQL script without parameters.
+ pub fn execute_batch(&self, sql: String) -> Result<(), VaultError> {
+ self.inner.execute_batch(&sql)
+ }
+
+ /// Run one query; `params_json` is a JSON array, the result a JSON array of row objects.
+ pub fn query(&self, sql: String, params_json: String) -> Result<String, VaultError> {
+ let params: Vec<serde_json::Value> = serde_json::from_str(&params_json)?;
+ Ok(serde_json::to_string(&self.inner.query(&sql, &params)?)?)
+ }
+
+ /// Run statements (a JSON array of `{"sql", "params"}`) in one transaction.
+ pub fn exec(&self, statements_json: String) -> Result<(), VaultError> {
+ let statements: Vec<crate::vault_sync::types::SqlStatement> = serde_json::from_str(&statements_json)?;
+ self.inner.exec(&statements)
+ }
+
+ /// Run one query with typed parameters; rows come back positionally under `columns`.
+ pub fn query_values(&self, sql: String, params: Vec<crate::sqlite_host::SqlValue>) -> Result<crate::sqlite_host::SqlResult, VaultError> {
+ self.inner.query_values(&sql, &params)
+ }
+
+ /// Run one statement with typed parameters and return the number of rows it changed.
+ pub fn execute(&self, sql: String, params: Vec<crate::sqlite_host::SqlValue>) -> Result<u64, VaultError> {
+ self.inner.execute(&sql, &params)
+ }
+
+ /// The database as SQLite file bytes.
+ pub fn export(&self) -> Result<Vec<u8>, VaultError> {
+ self.inner.export()
+ }
+}
diff --git a/core/rust/src/vault_sync/tests/mod.rs b/core/rust/src/vault_sync/tests/mod.rs
index 3cc665ef7..abf71e034 100644
--- a/core/rust/src/vault_sync/tests/mod.rs
+++ b/core/rust/src/vault_sync/tests/mod.rs
@@ -386,7 +386,7 @@ fn schema_rebuild_of_a_stale_vault_pushes_without_touching_the_key_hierarchy() {
/// A legacy sqlite-blob snapshot of the given database, as the server serves an account that has not migrated.
fn legacy_snapshot_of(conn: &rusqlite::Connection, kek: &str, revision: i64) -> Value {
- let bytes = conn.serialize(rusqlite::DatabaseName::Main).unwrap().to_vec();
+ let bytes = conn.serialize(rusqlite::MAIN_DB).unwrap().to_vec();
json!({
"status": 0,
"storageFormat": 0,
diff --git a/core/rust/src/vault_sync/tests/test_host.rs b/core/rust/src/vault_sync/tests/test_host.rs
index 2e05ac2be..5b05ff4fe 100644
--- a/core/rust/src/vault_sync/tests/test_host.rs
+++ b/core/rust/src/vault_sync/tests/test_host.rs
@@ -4,11 +4,11 @@ use std::collections::HashMap;
use base64::engine::general_purpose::STANDARD as BASE64;
use base64::Engine;
-use rusqlite::types::{Value as SqlValue, ValueRef};
-use rusqlite::{params_from_iter, Connection, DatabaseName};
+use rusqlite::{Connection, MAIN_DB};
use serde_json::{json, Map, Value};
use crate::crypto;
+use crate::sqlite_host;
use crate::vault_sync::types::{Command, Db, SqlStatement};
use crate::vault_sync::session::SyncSession;
@@ -48,20 +48,10 @@ pub fn complete_schema_sql() -> String {
source[start..end].replace('\u{feff}', "")
}
-/// Open a database from its file bytes (through a scratch file, since rusqlite's deserialize wants sqlite-owned memory).
+/// Open a database from its file bytes, in memory.
pub fn open_from_bytes(bytes: &[u8]) -> Connection {
- static COUNTER: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
- let unique = COUNTER.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
- let path = std::env::temp_dir().join(format!("aliasvault-sync-test-{}-{}.sqlite", std::process::id(), unique));
- std::fs::write(&path, bytes).unwrap();
- let file = Connection::open(&path).unwrap();
let mut conn = Connection::open_in_memory().unwrap();
- {
- let backup = rusqlite::backup::Backup::new(&file, &mut conn).unwrap();
- backup.run_to_completion(5, std::time::Duration::from_millis(1), None).unwrap();
- }
- drop(file);
- let _ = std::fs::remove_file(&path);
+ sqlite_host::deserialize_into(&mut conn, bytes).unwrap();
conn
}
@@ -105,7 +95,7 @@ impl TestHost {
/// Encrypt the local database as the at-rest blob, the way the app stores it.
pub fn store_local_as_blob(&mut self) {
- let bytes = self.local.serialize(DatabaseName::Main).unwrap().to_vec();
+ let bytes = self.local.serialize(MAIN_DB).unwrap().to_vec();
self.vault_blob = Some(crypto::symmetric_encrypt_bytes(&bytes, &self.vault_key).unwrap());
}
@@ -156,7 +146,7 @@ impl TestHost {
Ok(()) => json!({}),
Err(error) => json!({ "error": error }),
},
- Command::DbExport { db } => json!({ "bytes": BASE64.encode(self.db(db).serialize(DatabaseName::Main).unwrap().to_vec()) }),
+ Command::DbExport { db } => json!({ "bytes": BASE64.encode(self.db(db).serialize(MAIN_DB).unwrap().to_vec()) }),
Command::VaultStore { encrypted_blob, mark_dirty, encryption_key, expected_mutation_seq, revision } => {
self.store_calls.push(Command::VaultStore { encrypted_blob: String::new(), mark_dirty, encryption_key: encryption_key.clone(), expected_mutation_seq, revision });
if let Some(key) = encryption_key {
@@ -215,51 +205,10 @@ impl TestHost {
}
}
-fn to_sql(value: &Value) -> SqlValue {
- match value {
- Value::Null => SqlValue::Null,
- Value::Bool(b) => SqlValue::Integer(*b as i64),
- Value::Number(n) => n.as_i64().map(SqlValue::Integer).unwrap_or_else(|| SqlValue::Real(n.as_f64().unwrap_or(0.0))),
- Value::String(s) => SqlValue::Text(s.clone()),
- Value::Object(o) if o.contains_key("__b64") => SqlValue::Blob(BASE64.decode(o["__b64"].as_str().unwrap_or("")).unwrap_or_default()),
- other => SqlValue::Text(other.to_string()),
- }
-}
-
-fn from_sql(value: ValueRef<'_>) -> Value {
- match value {
- ValueRef::Null => Value::Null,
- ValueRef::Integer(i) => json!(i),
- ValueRef::Real(f) => json!(f),
- ValueRef::Text(t) => Value::String(String::from_utf8_lossy(t).to_string()),
- ValueRef::Blob(b) => json!({ "__b64": BASE64.encode(b) }),
- }
-}
-
pub fn query(conn: &Connection, sql: &str, params: &[Value]) -> Result<Vec<Map<String, Value>>, String> {
- let mut statement = conn.prepare(sql).map_err(|e| e.to_string())?;
- let columns: Vec<String> = statement.column_names().iter().map(|c| c.to_string()).collect();
- let values: Vec<SqlValue> = params.iter().map(to_sql).collect();
- let mut rows = statement.query(params_from_iter(values.iter())).map_err(|e| e.to_string())?;
- let mut out = Vec::new();
- while let Some(row) = rows.next().map_err(|e| e.to_string())? {
- let mut object = Map::new();
- for (index, column) in columns.iter().enumerate() {
- object.insert(column.clone(), from_sql(row.get_ref(index).map_err(|e| e.to_string())?));
- }
- out.push(object);
- }
- Ok(out)
+ sqlite_host::query(conn, sql, params).map_err(|e| e.to_string())
}
pub fn exec(conn: &Connection, statements: &[SqlStatement]) -> Result<(), String> {
- conn.execute_batch("BEGIN").map_err(|e| e.to_string())?;
- for statement in statements {
- let values: Vec<SqlValue> = statement.params.iter().map(to_sql).collect();
- if let Err(error) = conn.execute(&statement.sql, params_from_iter(values.iter())) {
- let _ = conn.execute_batch("ROLLBACK");
- return Err(format!("{} ({})", error, statement.sql));
- }
- }
- conn.execute_batch("COMMIT").map_err(|e| e.to_string())
+ sqlite_host::exec(conn, statements).map_err(|e| e.to_string())
}
diff --git a/core/rust/src/wasm.rs b/core/rust/src/wasm.rs
index 7f111e566..1450862c8 100644
--- a/core/rust/src/wasm.rs
+++ b/core/rust/src/wasm.rs
@@ -680,3 +680,121 @@ impl VaultSyncSessionJs {
self.inner.resume(response_json).map_err(|e| JsValue::from_str(&e.to_string()))
}
}
+
+// ============================================================================
+// SQLite host WASM Bindings
+// ============================================================================
+
+#[cfg(feature = "sqlite")]
+mod sqlite_js {
+ use js_sys::{Array, Object, Reflect, Uint8Array};
+ use wasm_bindgen::prelude::*;
+
+ use crate::sqlite_host::{MemoryDatabase, SqlResult, SqlValue};
+
+ /// An in-memory SQLite database that can be used by host applications to be have uniform access to the database.
+ #[wasm_bindgen(js_name = SqliteMemoryDatabase)]
+ pub struct SqliteMemoryDatabaseJs {
+ inner: Option<MemoryDatabase>,
+ }
+
+ #[wasm_bindgen(js_class = SqliteMemoryDatabase)]
+ impl SqliteMemoryDatabaseJs {
+ /// Open a database from its SQLite file bytes.
+ #[wasm_bindgen(js_name = fromBytes)]
+ pub fn from_bytes(bytes: &[u8]) -> Result<SqliteMemoryDatabaseJs, JsValue> {
+ Ok(Self { inner: Some(MemoryDatabase::from_bytes(bytes).map_err(error)?) })
+ }
+
+ /// Open an empty database.
+ pub fn empty() -> Result<SqliteMemoryDatabaseJs, JsValue> {
+ Ok(Self { inner: Some(MemoryDatabase::empty().map_err(error)?) })
+ }
+
+ /// Open an empty database and run a schema script on it.
+ #[wasm_bindgen(js_name = withSchema)]
+ pub fn with_schema(schema_sql: &str) -> Result<SqliteMemoryDatabaseJs, JsValue> {
+ Ok(Self { inner: Some(MemoryDatabase::with_schema(schema_sql).map_err(error)?) })
+ }
+
+ /// Run a statement that returns no rows and report how many rows it changed.
+ pub fn run(&self, sql: &str, params: JsValue) -> Result<f64, JsValue> {
+ Ok(self.db()?.execute(sql, &params_from_js(params)?).map_err(error)? as f64)
+ }
+
+ /// Run a statement and return its rows.
+ pub fn query(&self, sql: &str, params: JsValue) -> Result<JsValue, JsValue> {
+ Ok(rows_to_js(&self.db()?.query_values(sql, &params_from_js(params)?).map_err(error)?))
+ }
+
+ /// Run one or more statements separated by semicolons, without parameters.
+ pub fn exec(&self, sql: &str) -> Result<(), JsValue> {
+ self.db()?.execute_batch(sql).map_err(error)
+ }
+
+ /// The database as SQLite file bytes.
+ pub fn export(&self) -> Result<Vec<u8>, JsValue> {
+ self.db()?.export().map_err(error)
+ }
+
+ /// Close the database and free its memory.
+ pub fn close(&mut self) {
+ self.inner = None;
+ }
+
+ fn db(&self) -> Result<&MemoryDatabase, JsValue> {
+ self.inner.as_ref().ok_or_else(|| JsValue::from_str("The database is closed"))
+ }
+ }
+
+ fn error(error: crate::VaultError) -> JsValue {
+ JsValue::from_str(&error.to_string())
+ }
+
+ fn params_from_js(params: JsValue) -> Result<Vec<SqlValue>, JsValue> {
+ if params.is_undefined() || params.is_null() {
+ return Ok(Vec::new());
+ }
+ Array::from(&params).iter().map(|value| value_from_js(&value)).collect()
+ }
+
+ fn value_from_js(value: &JsValue) -> Result<SqlValue, JsValue> {
+ if value.is_null() || value.is_undefined() {
+ Ok(SqlValue::Null)
+ } else if let Some(text) = value.as_string() {
+ Ok(SqlValue::Text(text))
+ } else if let Some(flag) = value.as_bool() {
+ Ok(SqlValue::Integer(flag as i64))
+ } else if let Some(number) = value.as_f64() {
+ // Whole numbers within the safe integer range bind as INTEGER.
+ if number.fract() == 0.0 && number.abs() <= 9_007_199_254_740_992.0 { Ok(SqlValue::Integer(number as i64)) } else { Ok(SqlValue::Real(number)) }
+ } else if value.is_instance_of::<Uint8Array>() {
+ Ok(SqlValue::Blob(Uint8Array::new(value).to_vec()))
+ } else {
+ Err(JsValue::from_str("Unsupported SQLite parameter: expected string, number, boolean, null or Uint8Array"))
+ }
+ }
+
+ fn value_to_js(value: &SqlValue) -> JsValue {
+ match value {
+ SqlValue::Null => JsValue::NULL,
+ SqlValue::Integer(number) => JsValue::from_f64(*number as f64),
+ SqlValue::Real(number) => JsValue::from_f64(*number),
+ SqlValue::Text(text) => JsValue::from_str(text),
+ SqlValue::Blob(bytes) => Uint8Array::from(bytes.as_slice()).into(),
+ }
+ }
+
+ fn rows_to_js(result: &SqlResult) -> JsValue {
+ let columns: Vec<JsValue> = result.columns.iter().map(|column| JsValue::from_str(column)).collect();
+ let rows = Array::new();
+ for row in &result.rows {
+ let object = Object::new();
+ for (index, column) in columns.iter().enumerate() {
+ let _ = Reflect::set(&object, column, &value_to_js(&row[index]));
+ }
+ rows.push(&object);
+ }
+ rows.into()
+ }
+}
diff --git a/core/vault/package-lock.json b/core/vault/package-lock.json
index 97da2de3b..dfcdfed6f 100644
--- a/core/vault/package-lock.json
+++ b/core/vault/package-lock.json
@@ -9,12 +9,10 @@
"version": "1.0.0",
"license": "AGPL-3.0",
"devDependencies": {
- "@types/sql.js": "^1.4.11",
"@typescript-eslint/eslint-plugin": "^8.21.0",
"@typescript-eslint/parser": "^8.21.0",
"@vitest/coverage-v8": "^4.1.8",
"eslint": "^9.19.0",
- "sql.js": "^1.14.2",
"tsup": "^8.4.0",
"typescript": "^5.3.3",
"vitest": "^4.1.8"
@@ -1603,13 +1601,6 @@
"dev": true,
"license": "MIT"
},
- "node_modules/@types/emscripten": {
- "version": "1.41.5",
- "resolved": "https://registry.npmjs.org/@types/emscripten/-/emscripten-1.41.5.tgz",
- "integrity": "sha512-cMQm7pxu6BxtHyqJ7mQZ2kXWV5SLmugybFdHCBbJ5eHzOo6VhBckEgAT3//rP5FwPHNPeEiq4SmQ5ucBwsOo4Q==",
- "dev": true,
- "license": "MIT"
- },
"node_modules/@types/estree": {
"version": "1.0.8",
"resolved": "https://registry.npmjs.org/@types/estree/-/estree-1.0.8.tgz",
@@ -1624,27 +1615,6 @@
"dev": true,
"license": "MIT"
},
- "node_modules/@types/node": {
- "version": "26.4.0",
- "resolved": "https://registry.npmjs.org/@types/node/-/node-26.4.0.tgz",
- "integrity": "sha512-faiGnoIrLH/V8cibOMEAZ8pMw6oXqSukl29ra4mN8GdaB2ZewzeaLj+INpV5N+Z1eKWzY+IzaIZH2EIR6YZRNQ==",
- "dev": true,
- "license": "MIT",
- "dependencies": {
- "undici-types": "~8.3.0"
- }
- },
- "node_modules/@types/sql.js": {
- "version": "1.4.11",
- "resolved": "https://registry.npmjs.org/@types/sql.js/-/sql.js-1.4.11.tgz",
- "integrity": "sha512-QXIx38p2ZThJaK9vP5ZdqdlRe1FG9I8SmCZOS7FHfB/2qPAjZwkL7/vlfPg6N/oWHuuOaGg/P/IRwfP2W0kWVQ==",
- "dev": true,
- "license": "MIT",
- "dependencies": {
- "@types/emscripten": "*",
- "@types/node": "*"
- }
- },
"node_modules/@typescript-eslint/eslint-plugin": {
"version": "8.34.1",
"resolved": "https://registry.npmjs.org/@typescript-eslint/eslint-plugin/-/eslint-plugin-8.34.1.tgz",
@@ -4079,13 +4049,6 @@
"node": ">=0.10.0"
}
},
- "node_modules/sql.js": {
- "version": "1.14.2",
- "resolved": "https://registry.npmjs.org/sql.js/-/sql.js-1.14.2.tgz",
- "integrity": "sha512-3ZGPovObMFrdw79zrUHbfdE/DLIsy8jdNdssmMSQuRAymedU6q84asPt0kgiqrdMYlPegDItiIMfmIXzZnYFcw==",
- "dev": true,
- "license": "MIT"
- },
"node_modules/stackback": {
"version": "0.0.2",
"resolved": "https://registry.npmjs.org/stackback/-/stackback-0.0.2.tgz",
@@ -4506,13 +4469,6 @@
"dev": true,
"license": "MIT"
},
- "node_modules/undici-types": {
- "version": "8.3.0",
- "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-8.3.0.tgz",
- "integrity": "sha512-j375ScV60dom+YkPFIfTLcOiPxkN/buHz5GobjLhixFuANaNs3C9l4GmrWqejgXWJ7BbJcFYpTEUkS1Ge8bpZQ==",
- "dev": true,
- "license": "MIT"
- },
"node_modules/uri-js": {
"version": "4.4.1",
"resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.4.1.tgz",
diff --git a/core/vault/package.json b/core/vault/package.json
index b857ed225..20103afbd 100644
--- a/core/vault/package.json
+++ b/core/vault/package.json
@@ -22,12 +22,10 @@
],
"license": "AGPL-3.0",
"devDependencies": {
- "@types/sql.js": "^1.4.11",
"@typescript-eslint/eslint-plugin": "^8.21.0",
"@typescript-eslint/parser": "^8.21.0",
"@vitest/coverage-v8": "^4.1.8",
"eslint": "^9.19.0",
- "sql.js": "^1.14.2",
"tsup": "^8.4.0",
"typescript": "^5.3.3",
"vitest": "^4.1.8"
diff --git a/core/vault/src/__tests__/CompleteSchemaSql.test.ts b/core/vault/src/__tests__/CompleteSchemaSql.test.ts
deleted file mode 100644
index 2225091ec..000000000
--- a/core/vault/src/__tests__/CompleteSchemaSql.test.ts
+++ /dev/null
@@ -1,47 +0,0 @@
-import initSqlJs from 'sql.js';
-import { describe, it, expect } from 'vitest';
-
-import { COMPLETE_SCHEMA_SQL } from '../sql/SqlConstants';
-
-/*
- * The clients materialize every vault by applying COMPLETE_SCHEMA_SQL to an empty database, on SQLite builds
- * that are older than the one the EF tooling generates the script with (sql.js in the browser extension, the
- * system SQLite on iOS and Android). SQLite reparses every trigger in the schema while it rebuilds a table, so
- * a trigger whose body names a table that is momentarily dropped aborts the rebuild and the vault cannot be
- * opened at all. These tests run the script the way a client does.
- */
-describe('COMPLETE_SCHEMA_SQL', () => {
- it('applies to an empty database', async () => {
- const SQL = await initSqlJs();
- const db = new SQL.Database();
-
- try {
- expect(() => db.run(COMPLETE_SCHEMA_SQL)).not.toThrow();
- } finally {
- db.close();
- }
- });
-
- it('leaves every table a trigger writes to in place', async () => {
- const SQL = await initSqlJs();
- const db = new SQL.Database();
-
- try {
- db.run(COMPLETE_SCHEMA_SQL);
-
- const tables = new Set((db.exec("SELECT name FROM sqlite_master WHERE type = 'table'")[0]?.values ?? []).map(row => String(row[0])));
- const triggers = (db.exec("SELECT name, sql FROM sqlite_master WHERE type = 'trigger'")[0]?.values ?? []);
- expect(triggers.length).toBeGreaterThan(0);
-
- for (const [name, sql] of triggers) {
- const referenced = [...String(sql).matchAll(/(?:UPDATE|INSERT INTO|DELETE FROM)\s+"([^"]+)"/g)].map(match => match[1]);
- expect(referenced.length).toBeGreaterThan(0);
- for (const table of referenced) {
- expect(tables.has(table), `trigger ${String(name)} writes to missing table ${table}`).toBe(true);
- }
- }
- } finally {
- db.close();
- }
- });
-});