refactor: make console log less verbose when not needed
This commit is contained in:
parent
c3809b48f7
commit
c6c373f9fc
4
.env
Normal file
4
.env
Normal file
@ -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
|
1
src/decrypt-worker/util/DecryptError.ts
Normal file
1
src/decrypt-worker/util/DecryptError.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export class UnsupportedSourceFile extends Error {}
|
@ -1,5 +1,6 @@
|
|||||||
import { Transformer, Parakeet, TransformResult, fetchParakeet } from '@jixun/libparakeet';
|
import { Transformer, Parakeet, TransformResult, fetchParakeet } from '@jixun/libparakeet';
|
||||||
import { toArrayBuffer } from './buffer';
|
import { toArrayBuffer } from './buffer';
|
||||||
|
import { UnsupportedSourceFile } from './DecryptError';
|
||||||
|
|
||||||
export async function transformBlob(
|
export async function transformBlob(
|
||||||
blob: Blob | ArrayBuffer,
|
blob: Blob | ArrayBuffer,
|
||||||
@ -21,7 +22,9 @@ export async function transformBlob(
|
|||||||
cleanup.push(() => writer.delete());
|
cleanup.push(() => writer.delete());
|
||||||
|
|
||||||
const result = transformer.Transform(writer, reader);
|
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})`);
|
throw new Error(`transformer<${transformer.Name}> failed with error: ${TransformResult[result]} (${result})`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
import { Parakeet, fetchParakeet } from '@jixun/libparakeet';
|
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 { allCryptoFactories } from '../../crypto/CryptoFactory';
|
||||||
import { toArrayBuffer, toBlob } from '~/decrypt-worker/util/buffer';
|
import { toArrayBuffer, toBlob } from '~/decrypt-worker/util/buffer';
|
||||||
import { CryptoBase, CryptoFactory } from '~/decrypt-worker/crypto/CryptoBase';
|
import { CryptoBase, CryptoFactory } from '~/decrypt-worker/crypto/CryptoBase';
|
||||||
|
import { UnsupportedSourceFile } from '~/decrypt-worker/util/DecryptError';
|
||||||
|
|
||||||
// Use first 4MiB of the file to perform check.
|
// Use first 4MiB of the file to perform check.
|
||||||
const TEST_FILE_HEADER_LEN = 4 * 1024 * 1024;
|
const TEST_FILE_HEADER_LEN = 4 * 1024 * 1024;
|
||||||
@ -29,8 +30,11 @@ class DecryptCommandHandler {
|
|||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('decrypt failed: ', error);
|
if (error instanceof UnsupportedSourceFile) {
|
||||||
continue;
|
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 }) => {
|
export const workerDecryptHandler = async ({ id, blobURI }: { id: string; blobURI: string }) => {
|
||||||
const label = `decrypt( ${id} )`;
|
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 {
|
const handler = new DecryptCommandHandler(id, parakeet, blob, buffer);
|
||||||
return await timedLogger(`${label}/total`, async () => {
|
return handler.decrypt(allCryptoFactories);
|
||||||
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);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
@ -2,21 +2,19 @@ function isPromise<T = unknown>(p: unknown): p is Promise<T> {
|
|||||||
return !!p && typeof p === 'object' && 'then' in p && 'catch' in p && 'finally' in p;
|
return !!p && typeof p === 'object' && 'then' in p && 'catch' in p && 'finally' in p;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function timedLogger<R = unknown>(label: string, fn: () => R): R {
|
export function wrapFunctionCall<R = unknown>(pre: () => void, post: () => void, fn: () => R): R {
|
||||||
console.time(label);
|
pre();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const result = fn();
|
const result = fn();
|
||||||
|
|
||||||
if (isPromise(result)) {
|
if (isPromise(result)) {
|
||||||
result.finally(() => {
|
result.finally(post);
|
||||||
console.timeEnd(label);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.timeEnd(label);
|
post();
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
25
src/util/logUtils.ts
Normal file
25
src/util/logUtils.ts
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
import { wrapFunctionCall } from './fnWrapper';
|
||||||
|
|
||||||
|
export function timedLogger<R = unknown>(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<R = unknown>(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)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user