From 344b4f62a22fb8afe910f809b04485b10b51a79b Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Thu, 10 Nov 2022 13:54:39 +0100 Subject: quickjs preparations, clearer worker(-factory) names --- .../src/crypto/workers/synchronousWorkerFactory.ts | 36 -------- .../crypto/workers/synchronousWorkerFactoryNode.ts | 36 ++++++++ .../workers/synchronousWorkerFactoryPlain.ts | 36 ++++++++ .../src/crypto/workers/synchronousWorkerNode.ts | 2 +- .../src/crypto/workers/synchronousWorkerPlain.ts | 96 ++++++++++++++++++++++ .../src/crypto/workers/synchronousWorkerWeb.ts | 96 ---------------------- packages/taler-wallet-core/src/headless/helpers.ts | 6 +- packages/taler-wallet-core/src/index.browser.ts | 2 +- packages/taler-wallet-core/src/index.node.ts | 4 +- packages/taler-wallet-core/src/index.ts | 2 + 10 files changed, 177 insertions(+), 139 deletions(-) delete mode 100644 packages/taler-wallet-core/src/crypto/workers/synchronousWorkerFactory.ts create mode 100644 packages/taler-wallet-core/src/crypto/workers/synchronousWorkerFactoryNode.ts create mode 100644 packages/taler-wallet-core/src/crypto/workers/synchronousWorkerFactoryPlain.ts create mode 100644 packages/taler-wallet-core/src/crypto/workers/synchronousWorkerPlain.ts delete mode 100644 packages/taler-wallet-core/src/crypto/workers/synchronousWorkerWeb.ts (limited to 'packages/taler-wallet-core') diff --git a/packages/taler-wallet-core/src/crypto/workers/synchronousWorkerFactory.ts b/packages/taler-wallet-core/src/crypto/workers/synchronousWorkerFactory.ts deleted file mode 100644 index e9d67eec6..000000000 --- a/packages/taler-wallet-core/src/crypto/workers/synchronousWorkerFactory.ts +++ /dev/null @@ -1,36 +0,0 @@ -/* - This file is part of GNU Taler - (C) 2019 GNUnet e.V. - - GNU Taler is free software; you can redistribute it and/or modify it under the - terms of the GNU General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU General Public License for more details. - - You should have received a copy of the GNU General Public License along with - GNU Taler; see the file COPYING. If not, see - */ - -/** - * Imports. - */ -import { CryptoWorkerFactory } from "./cryptoDispatcher.js"; -import { CryptoWorker } from "./cryptoWorkerInterface.js"; -import { SynchronousCryptoWorker } from "./synchronousWorkerNode.js"; - -/** - * The synchronous crypto worker produced by this factory doesn't run in the - * background, but actually blocks the caller until the operation is done. - */ -export class SynchronousCryptoWorkerFactory implements CryptoWorkerFactory { - startWorker(): CryptoWorker { - return new SynchronousCryptoWorker(); - } - - getConcurrency(): number { - return 1; - } -} diff --git a/packages/taler-wallet-core/src/crypto/workers/synchronousWorkerFactoryNode.ts b/packages/taler-wallet-core/src/crypto/workers/synchronousWorkerFactoryNode.ts new file mode 100644 index 000000000..46cf12915 --- /dev/null +++ b/packages/taler-wallet-core/src/crypto/workers/synchronousWorkerFactoryNode.ts @@ -0,0 +1,36 @@ +/* + This file is part of GNU Taler + (C) 2019 GNUnet e.V. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see + */ + +/** + * Imports. + */ +import { CryptoWorkerFactory } from "./cryptoDispatcher.js"; +import { CryptoWorker } from "./cryptoWorkerInterface.js"; +import { SynchronousCryptoWorkerNode } from "./synchronousWorkerNode.js"; + +/** + * The synchronous crypto worker produced by this factory doesn't run in the + * background, but actually blocks the caller until the operation is done. + */ +export class SynchronousCryptoWorkerFactoryNode implements CryptoWorkerFactory { + startWorker(): CryptoWorker { + return new SynchronousCryptoWorkerNode(); + } + + getConcurrency(): number { + return 1; + } +} diff --git a/packages/taler-wallet-core/src/crypto/workers/synchronousWorkerFactoryPlain.ts b/packages/taler-wallet-core/src/crypto/workers/synchronousWorkerFactoryPlain.ts new file mode 100644 index 000000000..7662b41f5 --- /dev/null +++ b/packages/taler-wallet-core/src/crypto/workers/synchronousWorkerFactoryPlain.ts @@ -0,0 +1,36 @@ +/* + This file is part of GNU Taler + (C) 2019 GNUnet e.V. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see + */ + +/** + * Imports. + */ +import { CryptoWorkerFactory } from "./cryptoDispatcher.js"; +import { CryptoWorker } from "./cryptoWorkerInterface.js"; +import { SynchronousCryptoWorkerPlain } from "./synchronousWorkerPlain.js"; + +/** + * The synchronous crypto worker produced by this factory doesn't run in the + * background, but actually blocks the caller until the operation is done. + */ +export class SynchronousCryptoWorkerFactoryPlain implements CryptoWorkerFactory { + startWorker(): CryptoWorker { + return new SynchronousCryptoWorkerPlain(); + } + + getConcurrency(): number { + return 1; + } +} diff --git a/packages/taler-wallet-core/src/crypto/workers/synchronousWorkerNode.ts b/packages/taler-wallet-core/src/crypto/workers/synchronousWorkerNode.ts index 1f83dc519..b2653158c 100644 --- a/packages/taler-wallet-core/src/crypto/workers/synchronousWorkerNode.ts +++ b/packages/taler-wallet-core/src/crypto/workers/synchronousWorkerNode.ts @@ -31,7 +31,7 @@ const logger = new Logger("synchronousWorker.ts"); * The node crypto worker can also use IPC to offload cryptographic * operations to a helper process (usually written in C / part of taler-exchange). */ -export class SynchronousCryptoWorker implements CryptoWorker { +export class SynchronousCryptoWorkerNode implements CryptoWorker { /** * Function to be called when we receive a message from the worker thread. */ diff --git a/packages/taler-wallet-core/src/crypto/workers/synchronousWorkerPlain.ts b/packages/taler-wallet-core/src/crypto/workers/synchronousWorkerPlain.ts new file mode 100644 index 000000000..058896828 --- /dev/null +++ b/packages/taler-wallet-core/src/crypto/workers/synchronousWorkerPlain.ts @@ -0,0 +1,96 @@ +/* + This file is part of GNU Taler + (C) 2019 GNUnet e.V. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see + */ + +/** + * Imports. + */ +import { Logger } from "@gnu-taler/taler-util"; +import { + nativeCryptoR, + TalerCryptoInterfaceR, +} from "../cryptoImplementation.js"; +import { CryptoWorker } from "./cryptoWorkerInterface.js"; +import { processRequestWithImpl } from "./worker-common.js"; + +const logger = new Logger("synchronousWorker.ts"); + +/** + * Worker implementation that synchronously executes cryptographic + * operations. + */ +export class SynchronousCryptoWorkerPlain implements CryptoWorker { + /** + * Function to be called when we receive a message from the worker thread. + */ + onmessage: undefined | ((m: any) => void); + + /** + * Function to be called when we receive an error from the worker thread. + */ + onerror: undefined | ((m: any) => void); + + cryptoImplR: TalerCryptoInterfaceR; + + constructor() { + this.onerror = undefined; + this.onmessage = undefined; + this.cryptoImplR = { ...nativeCryptoR }; + } + + /** + * Add an event listener for either an "error" or "message" event. + */ + addEventListener(event: "message" | "error", fn: (x: any) => void): void { + switch (event) { + case "message": + this.onmessage = fn; + break; + case "error": + this.onerror = fn; + break; + } + } + + private dispatchMessage(msg: any): void { + if (this.onmessage) { + this.onmessage(msg); + } + } + + /** + * Send a message to the worker thread. + */ + postMessage(msg: any): void { + const handleRequest = async () => { + const responseMsg = await processRequestWithImpl(msg, this.cryptoImplR); + try { + setTimeout(() => this.dispatchMessage(responseMsg), 0); + } catch (e) { + logger.error("got error during dispatch", e); + } + }; + handleRequest().catch((e) => { + logger.error("Error while handling crypto request:", e); + }); + } + + /** + * Forcibly terminate the worker thread. + */ + terminate(): void { + // This is a no-op. + } +} diff --git a/packages/taler-wallet-core/src/crypto/workers/synchronousWorkerWeb.ts b/packages/taler-wallet-core/src/crypto/workers/synchronousWorkerWeb.ts deleted file mode 100644 index 0ba365fa1..000000000 --- a/packages/taler-wallet-core/src/crypto/workers/synchronousWorkerWeb.ts +++ /dev/null @@ -1,96 +0,0 @@ -/* - This file is part of GNU Taler - (C) 2019 GNUnet e.V. - - GNU Taler is free software; you can redistribute it and/or modify it under the - terms of the GNU General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU General Public License for more details. - - You should have received a copy of the GNU General Public License along with - GNU Taler; see the file COPYING. If not, see - */ - -/** - * Imports. - */ -import { Logger } from "@gnu-taler/taler-util"; -import { - nativeCryptoR, - TalerCryptoInterfaceR, -} from "../cryptoImplementation.js"; -import { CryptoWorker } from "./cryptoWorkerInterface.js"; -import { processRequestWithImpl } from "./worker-common.js"; - -const logger = new Logger("synchronousWorker.ts"); - -/** - * Worker implementation that synchronously executes cryptographic - * operations. - */ -export class SynchronousCryptoWorker implements CryptoWorker { - /** - * Function to be called when we receive a message from the worker thread. - */ - onmessage: undefined | ((m: any) => void); - - /** - * Function to be called when we receive an error from the worker thread. - */ - onerror: undefined | ((m: any) => void); - - cryptoImplR: TalerCryptoInterfaceR; - - constructor() { - this.onerror = undefined; - this.onmessage = undefined; - this.cryptoImplR = { ...nativeCryptoR }; - } - - /** - * Add an event listener for either an "error" or "message" event. - */ - addEventListener(event: "message" | "error", fn: (x: any) => void): void { - switch (event) { - case "message": - this.onmessage = fn; - break; - case "error": - this.onerror = fn; - break; - } - } - - private dispatchMessage(msg: any): void { - if (this.onmessage) { - this.onmessage(msg); - } - } - - /** - * Send a message to the worker thread. - */ - postMessage(msg: any): void { - const handleRequest = async () => { - const responseMsg = await processRequestWithImpl(msg, this.cryptoImplR); - try { - setTimeout(() => this.dispatchMessage(responseMsg), 0); - } catch (e) { - logger.error("got error during dispatch", e); - } - }; - handleRequest().catch((e) => { - logger.error("Error while handling crypto request:", e); - }); - } - - /** - * Forcibly terminate the worker thread. - */ - terminate(): void { - // This is a no-op. - } -} diff --git a/packages/taler-wallet-core/src/headless/helpers.ts b/packages/taler-wallet-core/src/headless/helpers.ts index cef7c94d9..64edf8fb0 100644 --- a/packages/taler-wallet-core/src/headless/helpers.ts +++ b/packages/taler-wallet-core/src/headless/helpers.ts @@ -33,7 +33,7 @@ import { AccessStats } from "@gnu-taler/idb-bridge"; import { Logger, WalletNotification } from "@gnu-taler/taler-util"; import * as fs from "fs"; import { NodeThreadCryptoWorkerFactory } from "../crypto/workers/nodeThreadWorker.js"; -import { SynchronousCryptoWorkerFactory } from "../crypto/workers/synchronousWorkerFactory.js"; +import { SynchronousCryptoWorkerFactoryNode } from "../crypto/workers/synchronousWorkerFactoryNode.js"; import { openTalerDatabase } from "../db-utils.js"; import { HttpRequestLibrary } from "../util/http.js"; import { SetTimeoutTimerAPI } from "../util/timer.js"; @@ -165,7 +165,7 @@ export async function getDefaultNodeWallet2( const cryptoWorkerType = args.cryptoWorkerType ?? "node-worker-thread"; if (cryptoWorkerType === "sync") { logger.info("using synchronous crypto worker"); - workerFactory = new SynchronousCryptoWorkerFactory(); + workerFactory = new SynchronousCryptoWorkerFactoryNode(); } else if (cryptoWorkerType === "node-worker-thread") { try { // Try if we have worker threads available, fails in older node versions. @@ -179,7 +179,7 @@ export async function getDefaultNodeWallet2( logger.warn( "worker threads not available, falling back to synchronous workers", ); - workerFactory = new SynchronousCryptoWorkerFactory(); + workerFactory = new SynchronousCryptoWorkerFactoryNode(); } } else { throw Error(`unsupported crypto worker type '${cryptoWorkerType}'`); diff --git a/packages/taler-wallet-core/src/index.browser.ts b/packages/taler-wallet-core/src/index.browser.ts index 029bc533f..02d3665c2 100644 --- a/packages/taler-wallet-core/src/index.browser.ts +++ b/packages/taler-wallet-core/src/index.browser.ts @@ -15,4 +15,4 @@ */ export * from "./index.js"; -export { SynchronousCryptoWorker } from "./crypto/workers/synchronousWorkerWeb.js"; +export { SynchronousCryptoWorkerPlain as SynchronousCryptoWorker } from "./crypto/workers/synchronousWorkerPlain.js"; diff --git a/packages/taler-wallet-core/src/index.node.ts b/packages/taler-wallet-core/src/index.node.ts index d7211b4dc..8567d13ac 100644 --- a/packages/taler-wallet-core/src/index.node.ts +++ b/packages/taler-wallet-core/src/index.node.ts @@ -24,7 +24,7 @@ export { DefaultNodeWalletArgs, } from "./headless/helpers.js"; export * from "./crypto/workers/nodeThreadWorker.js"; -export { SynchronousCryptoWorker } from "./crypto/workers/synchronousWorkerNode.js"; +export { SynchronousCryptoWorkerNode as SynchronousCryptoWorker } from "./crypto/workers/synchronousWorkerNode.js"; export type { AccessStats } from "@gnu-taler/idb-bridge"; -export * from "./crypto/workers/synchronousWorkerFactory.js"; +export * from "./crypto/workers/synchronousWorkerFactoryNode.js"; diff --git a/packages/taler-wallet-core/src/index.ts b/packages/taler-wallet-core/src/index.ts index afd86b45d..7cc23aa88 100644 --- a/packages/taler-wallet-core/src/index.ts +++ b/packages/taler-wallet-core/src/index.ts @@ -66,3 +66,5 @@ export { export * from "./util/timer.js"; export * from "./util/denominations.js"; + +export { SynchronousCryptoWorkerFactoryPlain } from "./crypto/workers/synchronousWorkerFactoryPlain.js"; -- cgit v1.2.3