wallet-core/packages/taler-wallet-webextension/src/browserWorkerEntry.ts

87 lines
2.2 KiB
TypeScript
Raw Normal View History

2019-08-16 15:03:52 +02:00
/*
2022-06-06 17:05:26 +02:00
This file is part of GNU Taler
(C) 2022 Taler Systems S.A.
2019-08-16 15:03:52 +02:00
2022-06-06 17:05:26 +02:00
GNU Taler is free software; you can redistribute it and/or modify it under the
2019-08-16 15:03:52 +02:00
terms of the GNU General Public License as published by the Free Software
Foundation; either version 3, or (at your option) any later version.
2022-06-06 17:05:26 +02:00
GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
2019-08-16 15:03:52 +02:00
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
2022-06-06 17:05:26 +02:00
GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
2019-08-16 15:03:52 +02:00
/**
* Web worker for crypto operations.
*/
/**
* Imports.
*/
2022-09-30 13:11:17 +02:00
import {
2023-02-15 23:32:42 +01:00
j2s,
Logger,
2022-09-30 13:11:17 +02:00
getErrorDetailFromException,
2023-02-15 23:32:42 +01:00
} from "@gnu-taler/taler-util";
import { nativeCrypto } from "@gnu-taler/taler-wallet-core";
const logger = new Logger("browserWorkerEntry.ts");
2019-08-16 15:03:52 +02:00
2022-03-23 21:24:23 +01:00
const worker: Worker = self as any as Worker;
2019-08-16 15:03:52 +02:00
2020-04-07 10:28:55 +02:00
async function handleRequest(
operation: string,
id: number,
2022-03-25 20:57:27 +01:00
req: unknown,
2020-04-07 10:28:55 +02:00
): Promise<void> {
2022-03-23 21:24:23 +01:00
const impl = nativeCrypto;
2019-08-16 15:03:52 +02:00
if (!(operation in impl)) {
console.error(`crypto operation '${operation}' not found`);
return;
}
2022-09-30 13:11:17 +02:00
logger.info(`browser worker crypto request: ${j2s(req)}`);
let responseMsg: any;
2019-08-16 15:03:52 +02:00
try {
2022-09-30 13:11:17 +02:00
const result = await (impl as any)[operation](impl, req);
responseMsg = { type: "success", result, id };
} catch (e: any) {
logger.error(`error during operation: ${e.stack ?? e.toString()}`);
responseMsg = {
type: "error",
id,
error: getErrorDetailFromException(e),
};
2019-08-16 15:03:52 +02:00
}
2022-09-30 13:11:17 +02:00
worker.postMessage(responseMsg);
2019-08-16 15:03:52 +02:00
}
worker.onmessage = (msg: MessageEvent) => {
2022-03-25 20:57:27 +01:00
const req = msg.data.req;
if (typeof req !== "object") {
console.error("request must be an object");
2019-08-16 15:03:52 +02:00
return;
}
const id = msg.data.id;
if (typeof id !== "number") {
console.error("RPC id must be number");
return;
}
const operation = msg.data.operation;
if (typeof operation !== "string") {
console.error("RPC operation must be string");
return;
}
2022-03-25 20:57:27 +01:00
handleRequest(operation, id, req).catch((e) => {
2021-08-13 23:04:05 +02:00
console.error("error in browser worker", e);
2019-08-16 15:03:52 +02:00
});
};