taler-typescript-core

Wallet core logic and WebUIs for various components
Log | Files | Refs | Submodules | README | LICENSE

commit 9486afd0e2f1822f473691a28fcde7e4878fbd87
parent 21bec5320c4da682bfe6cfa776a1801421fcd135
Author: Florian Dold <florian@dold.me>
Date:   Tue,  5 Nov 2024 13:10:19 +0100

wallet-core: fix/refactor logic to get sqlite3 db file path

Diffstat:
Mpackages/taler-wallet-core/src/host-common.ts | 28++++++++++++++++++++++++++++
Mpackages/taler-wallet-core/src/host-impl.node.ts | 10++++++++--
Mpackages/taler-wallet-core/src/host-impl.qtart.ts | 10++++++++--
Mpackages/taler-wallet-embedded/src/wallet-qjs.ts | 28----------------------------
4 files changed, 44 insertions(+), 32 deletions(-)

diff --git a/packages/taler-wallet-core/src/host-common.ts b/packages/taler-wallet-core/src/host-common.ts @@ -58,3 +58,31 @@ export function makeTempfileId(length: number): string { } return result; } + +/** + * Get the underlying sqlite3 DB filename + * from the storage path specified by the wallet-core client. + */ +export function getSqlite3FilenameFromStoragePath( + p: string | undefined, +): string { + // Allow specifying a directory as the storage path. + // In that case, we pick the filename and use the sqlite3 + // backend. + // + // We still allow specifying a filename for backwards + // compatibility and control over the exact file. + // + // Specifying a directory allows us to automatically + // migrate to a new DB file in the future. + if (!p) { + return ":memory:"; + } + if (p.endsWith("/")) { + // Current sqlite3 DB filename. + // Should rarely if ever change. + return `${p}talerwalletdb-v30.sqlite3`; + } else { + return p; + } +} diff --git a/packages/taler-wallet-core/src/host-impl.node.ts b/packages/taler-wallet-core/src/host-impl.node.ts @@ -41,7 +41,11 @@ import { createPlatformHttpLib } from "@gnu-taler/taler-util/http"; import * as fs from "fs"; import { NodeThreadCryptoWorkerFactory } from "./crypto/workers/nodeThreadWorker.js"; import { SynchronousCryptoWorkerFactoryPlain } from "./crypto/workers/synchronousWorkerFactoryPlain.js"; -import { DefaultNodeWalletArgs, makeTempfileId } from "./host-common.js"; +import { + DefaultNodeWalletArgs, + getSqlite3FilenameFromStoragePath, + makeTempfileId, +} from "./host-common.js"; import { Wallet } from "./wallet.js"; const logger = new Logger("host-impl.node.ts"); @@ -114,7 +118,9 @@ async function makeSqliteDb( BridgeIDBFactory.enableTracing = false; } const imp = await createNodeSqlite3Impl(); - const dbFilename = args.persistentStoragePath ?? ":memory:"; + const dbFilename = getSqlite3FilenameFromStoragePath( + args.persistentStoragePath, + ); logger.info(`using database ${dbFilename}`); const myBackend = await createSqliteBackend(imp, { filename: dbFilename, diff --git a/packages/taler-wallet-core/src/host-impl.qtart.ts b/packages/taler-wallet-core/src/host-impl.qtart.ts @@ -45,7 +45,11 @@ import { import { createPlatformHttpLib } from "@gnu-taler/taler-util/http"; import { qjsOs, qjsStd } from "@gnu-taler/taler-util/qtart"; import { SynchronousCryptoWorkerFactoryPlain } from "./crypto/workers/synchronousWorkerFactoryPlain.js"; -import { DefaultNodeWalletArgs, makeTempfileId } from "./host-common.js"; +import { + DefaultNodeWalletArgs, + getSqlite3FilenameFromStoragePath, + makeTempfileId, +} from "./host-common.js"; import { Wallet } from "./wallet.js"; const logger = new Logger("host-impl.qtart.ts"); @@ -101,7 +105,9 @@ async function makeSqliteDb( args: DefaultNodeWalletArgs, ): Promise<MakeDbResult> { BridgeIDBFactory.enableTracing = false; - let filename = args.persistentStoragePath ?? ":memory:"; + const filename = getSqlite3FilenameFromStoragePath( + args.persistentStoragePath, + ); logger.info(`opening sqlite3 database ${j2s(filename)}`); const imp = await createQtartSqlite3Impl(); const myBackend = await createSqliteBackend(imp, { diff --git a/packages/taler-wallet-embedded/src/wallet-qjs.ts b/packages/taler-wallet-embedded/src/wallet-qjs.ts @@ -37,7 +37,6 @@ import { WalletNotification, enableNativeLogging, getErrorDetailFromException, - j2s, openPromise, performanceNow, setGlobalLogLevelFromString, @@ -89,37 +88,10 @@ class NativeWalletMessageHandler { switch (operation) { case "init": { - // Allow specifying a directory as the storage path. - // In that case, we pick the filename and use the sqlite3 - // backend. - // - // We still allow specifying a filename for backwards - // compatibility and control over the exact file. - // - // Specifying a directory allows us to automatically - // migrate to a new DB file in the future. - const p = args.persistentStoragePath; - let persistentStoragePath: string | undefined; - if (p != null) { - if (typeof p !== "string") { - throw Error("persistentStoragePath must be a string"); - } - if (p.endsWith("/")) { - logger.info( - `Specified directory ${j2s(p)} as persistentStoragePath`, - ); - persistentStoragePath = `${p}talerwalletdb-v30.sqlite3`; - } else { - logger.info(`Specified file ${j2s(p)} as as persistentStoragePath`); - persistentStoragePath = p; - } - } - const wR = await createNativeWalletHost2({ notifyHandler: async (notification: WalletNotification) => { sendNativeMessage({ type: "notification", payload: notification }); }, - persistentStoragePath, httpLib: this.httpLib, cryptoWorkerType: args.cryptoWorkerType, ...args,