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 ---------------------- 6 files changed, 169 insertions(+), 133 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/src/crypto') 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. - } -} -- cgit v1.2.3