From c6c373f9fc021f30eba036089e3103cc2bd0231c Mon Sep 17 00:00:00 2001 From: Jixun Wu Date: Mon, 22 May 2023 00:00:35 +0100 Subject: [PATCH] refactor: make console log less verbose when not needed --- .env | 4 +++ src/decrypt-worker/util/DecryptError.ts | 1 + src/decrypt-worker/util/transformBlob.ts | 5 +++- src/decrypt-worker/worker/handler/decrypt.ts | 30 +++++++++----------- src/util/{timedLogger.ts => fnWrapper.ts} | 10 +++---- src/util/logUtils.ts | 25 ++++++++++++++++ 6 files changed, 52 insertions(+), 23 deletions(-) create mode 100644 .env create mode 100644 src/decrypt-worker/util/DecryptError.ts rename src/util/{timedLogger.ts => fnWrapper.ts} (58%) create mode 100644 src/util/logUtils.ts diff --git a/.env b/.env new file mode 100644 index 0000000..bcc85c2 --- /dev/null +++ b/.env @@ -0,0 +1,4 @@ +# Example environment file for vite to use. +# For more information, see: https://vitejs.dev/guide/env-and-mode.html + +ENABLE_PERF_LOG=0 diff --git a/src/decrypt-worker/util/DecryptError.ts b/src/decrypt-worker/util/DecryptError.ts new file mode 100644 index 0000000..205f1dd --- /dev/null +++ b/src/decrypt-worker/util/DecryptError.ts @@ -0,0 +1 @@ +export class UnsupportedSourceFile extends Error {} diff --git a/src/decrypt-worker/util/transformBlob.ts b/src/decrypt-worker/util/transformBlob.ts index e29110e..2fd1bb5 100644 --- a/src/decrypt-worker/util/transformBlob.ts +++ b/src/decrypt-worker/util/transformBlob.ts @@ -1,5 +1,6 @@ import { Transformer, Parakeet, TransformResult, fetchParakeet } from '@jixun/libparakeet'; import { toArrayBuffer } from './buffer'; +import { UnsupportedSourceFile } from './DecryptError'; export async function transformBlob( blob: Blob | ArrayBuffer, @@ -21,7 +22,9 @@ export async function transformBlob( cleanup.push(() => writer.delete()); const result = transformer.Transform(writer, reader); - if (result !== TransformResult.OK) { + if (result === TransformResult.ERROR_INVALID_FORMAT) { + throw new UnsupportedSourceFile(`transformer<${transformer.Name}> does not recognize this file`); + } else if (result !== TransformResult.OK) { throw new Error(`transformer<${transformer.Name}> failed with error: ${TransformResult[result]} (${result})`); } diff --git a/src/decrypt-worker/worker/handler/decrypt.ts b/src/decrypt-worker/worker/handler/decrypt.ts index 4d3ff55..851e978 100644 --- a/src/decrypt-worker/worker/handler/decrypt.ts +++ b/src/decrypt-worker/worker/handler/decrypt.ts @@ -1,8 +1,9 @@ import { Parakeet, fetchParakeet } from '@jixun/libparakeet'; -import { timedLogger } from '~/util/timedLogger'; +import { timedLogger, withGroupedLogs as withTimeGroupedLogs } from '~/util/logUtils'; import { allCryptoFactories } from '../../crypto/CryptoFactory'; import { toArrayBuffer, toBlob } from '~/decrypt-worker/util/buffer'; import { CryptoBase, CryptoFactory } from '~/decrypt-worker/crypto/CryptoBase'; +import { UnsupportedSourceFile } from '~/decrypt-worker/util/DecryptError'; // Use first 4MiB of the file to perform check. const TEST_FILE_HEADER_LEN = 4 * 1024 * 1024; @@ -29,8 +30,11 @@ class DecryptCommandHandler { } return result; } catch (error) { - console.error('decrypt failed: ', error); - continue; + if (error instanceof UnsupportedSourceFile) { + console.debug('WARN: decryptor does not recognize source file, wrong crypto?', error); + } else { + console.error('decrypt failed with unknown error: ', error); + } } } @@ -83,18 +87,12 @@ class DecryptCommandHandler { export const workerDecryptHandler = async ({ id, blobURI }: { id: string; blobURI: string }) => { const label = `decrypt( ${id} )`; - console.group(label); + return withTimeGroupedLogs(label, async () => { + const parakeet = await timedLogger(`${label}/init`, fetchParakeet); + const blob = await timedLogger(`${label}/fetch-src`, async () => fetch(blobURI).then((r) => r.blob())); + const buffer = await timedLogger(`${label}/read-src`, async () => blob.arrayBuffer()); - try { - return await timedLogger(`${label}/total`, async () => { - const parakeet = await timedLogger(`${label}/init`, fetchParakeet); - const blob = await timedLogger(`${label}/fetch-src`, async () => fetch(blobURI).then((r) => r.blob())); - const buffer = await timedLogger(`${label}/read-src`, async () => blob.arrayBuffer()); - - const handler = new DecryptCommandHandler(id, parakeet, blob, buffer); - return handler.decrypt(allCryptoFactories); - }); - } finally { - (console.groupEnd as (label: string) => void)(label); - } + const handler = new DecryptCommandHandler(id, parakeet, blob, buffer); + return handler.decrypt(allCryptoFactories); + }); }; diff --git a/src/util/timedLogger.ts b/src/util/fnWrapper.ts similarity index 58% rename from src/util/timedLogger.ts rename to src/util/fnWrapper.ts index 056d53b..8c3bdf4 100644 --- a/src/util/timedLogger.ts +++ b/src/util/fnWrapper.ts @@ -2,21 +2,19 @@ function isPromise(p: unknown): p is Promise { return !!p && typeof p === 'object' && 'then' in p && 'catch' in p && 'finally' in p; } -export function timedLogger(label: string, fn: () => R): R { - console.time(label); +export function wrapFunctionCall(pre: () => void, post: () => void, fn: () => R): R { + pre(); try { const result = fn(); if (isPromise(result)) { - result.finally(() => { - console.timeEnd(label); - }); + result.finally(post); } return result; } catch (e) { - console.timeEnd(label); + post(); throw e; } } diff --git a/src/util/logUtils.ts b/src/util/logUtils.ts new file mode 100644 index 0000000..e37609a --- /dev/null +++ b/src/util/logUtils.ts @@ -0,0 +1,25 @@ +import { wrapFunctionCall } from './fnWrapper'; + +export function timedLogger(label: string, fn: () => R): R { + if (import.meta.env.ENABLE_PERF_LOG !== '1') { + return fn(); + } else { + return wrapFunctionCall( + () => console.time(label), + () => console.timeEnd(label), + fn + ); + } +} + +export function withGroupedLogs(label: string, fn: () => R): R { + if (import.meta.env.ENABLE_PERF_LOG !== '1') { + return fn(); + } else { + return wrapFunctionCall( + () => console.group(label), + () => (console.groupEnd as (label: string) => void)(label), + () => timedLogger(`${label}/total`, fn) + ); + } +}