refactor: simplify logging and catch errors
Some checks failed
continuous-integration/drone/push Build is failing
continuous-integration/drone/pr Build is failing

This commit is contained in:
鲁树人 2024-09-16 22:27:45 +01:00
parent 7fef8da083
commit 1b116a8db3
4 changed files with 21 additions and 37 deletions

View File

@ -19,7 +19,7 @@ class DecryptCommandHandler {
this.label = `DecryptCommandHandler(${label})`; this.label = `DecryptCommandHandler(${label})`;
} }
log<R>(label: string, fn: () => R): R { log<R>(label: string, fn: () => Promise<R>): Promise<R> {
return timedLogger(`${this.label}: ${label}`, fn); return timedLogger(`${this.label}: ${label}`, fn);
} }
@ -52,7 +52,7 @@ class DecryptCommandHandler {
} }
async tryDecryptWith(decipher: DecipherInstance) { async tryDecryptWith(decipher: DecipherInstance) {
const result = await this.log(`decrypt ${decipher.cipherName}`, async () => const result = await this.log(`try decrypt with ${decipher.cipherName}`, async () =>
decipher.decrypt(this.buffer, this.options), decipher.decrypt(this.buffer, this.options),
); );
switch (result.status) { switch (result.status) {
@ -79,8 +79,9 @@ class DecryptCommandHandler {
} }
} }
export const workerDecryptHandler = async ({ id, blobURI, options }: DecryptCommandPayload) => { export const workerDecryptHandler = async ({ id: payloadId, blobURI, options }: DecryptCommandPayload) => {
await umCryptoReady; await umCryptoReady;
const id = payloadId.replace('://', ':');
const label = `decrypt(${id})`; const label = `decrypt(${id})`;
return withTimeGroupedLogs(label, async () => { return withTimeGroupedLogs(label, async () => {
const buffer = await fetch(blobURI).then((r) => r.arrayBuffer()); const buffer = await fetch(blobURI).then((r) => r.arrayBuffer());

View File

@ -1,26 +1,16 @@
import { fetchParakeet, FooterParserState } from '@um/libparakeet'; import { fetchParakeet, FooterParserState } from '@um/libparakeet';
import type { FetchMusicExNamePayload } from '~/decrypt-worker/types'; import type { FetchMusicExNamePayload } from '~/decrypt-worker/types';
import { makeQMCv2FooterParser } from '~/decrypt-worker/util/qmc2KeyCrypto'; import { makeQMCv2FooterParser } from '~/decrypt-worker/util/qmc2KeyCrypto';
import { timedLogger, withGroupedLogs as withTimeGroupedLogs } from '~/util/logUtils'; import { timedLogger } from '~/util/logUtils.ts';
export const workerParseMusicExMediaName = async ({ id, blobURI }: FetchMusicExNamePayload) => { export const workerParseMusicExMediaName = async ({ id, blobURI }: FetchMusicExNamePayload) => {
const label = `decrypt(${id})`; const label = `qmcMusixEx(${id.replace('://', ':')})`;
return withTimeGroupedLogs(label, async () => { return timedLogger(label, async () => {
const parakeet = await timedLogger(`${label}/init`, fetchParakeet); const parakeet = await fetchParakeet();
const blob = await timedLogger(`${label}/fetch-src`, async () => const blob = await fetch(blobURI, { headers: { Range: 'bytes=-1024' } }).then((r) => r.blob());
fetch(blobURI, { headers: { Range: 'bytes=-1024' } }).then((r) => r.blob()), const buffer = await blob.arrayBuffer();
);
const buffer = await timedLogger(`${label}/read-src`, async () => { const parsed = makeQMCv2FooterParser(parakeet).parse(buffer.slice(-1024));
// Firefox: the range header does not work...?
const blobBuffer = await blob.arrayBuffer();
if (blobBuffer.byteLength > 1024) {
return blobBuffer.slice(-1024);
}
return blobBuffer;
});
const parsed = makeQMCv2FooterParser(parakeet).parse(buffer);
if (parsed.state === FooterParserState.OK) { if (parsed.state === FooterParserState.OK) {
return parsed.mediaName; return parsed.mediaName;
} }

View File

@ -1,20 +1,13 @@
function isPromise<T = unknown>(p: unknown): p is Promise<T> { export async function wrapFunctionCall<R = unknown>(
return !!p && typeof p === 'object' && 'then' in p && 'catch' in p && 'finally' in p; pre: () => void,
} post: () => void,
fn: () => Promise<R>,
export function wrapFunctionCall<R = unknown>(pre: () => void, post: () => void, fn: () => R): R { ): Promise<R> {
pre(); pre();
try { try {
const result = fn(); return await fn();
} finally {
if (isPromise(result)) {
result.finally(post);
}
return result;
} catch (e) {
post(); post();
throw e;
} }
} }

View File

@ -1,6 +1,6 @@
import { wrapFunctionCall } from './fnWrapper'; import { wrapFunctionCall } from './fnWrapper';
export function timedLogger<R = unknown>(label: string, fn: () => R): R { export async function timedLogger<R = unknown>(label: string, fn: () => Promise<R>): Promise<R> {
if (import.meta.env.VITE_ENABLE_PERF_LOG !== '1') { if (import.meta.env.VITE_ENABLE_PERF_LOG !== '1') {
return fn(); return fn();
} else { } else {
@ -12,13 +12,13 @@ export function timedLogger<R = unknown>(label: string, fn: () => R): R {
} }
} }
export function withGroupedLogs<R = unknown>(label: string, fn: () => R): R { export async function withGroupedLogs<R = unknown>(label: string, fn: () => Promise<R>): Promise<R> {
if (import.meta.env.VITE_ENABLE_PERF_LOG !== '1') { if (import.meta.env.VITE_ENABLE_PERF_LOG !== '1') {
return fn(); return fn();
} else { } else {
return wrapFunctionCall( return wrapFunctionCall(
() => console.group(label), () => console.group(label),
() => (console.groupEnd as (label: string) => void)(label), () => console.groupEnd(),
() => timedLogger(`${label}/total`, fn), () => timedLogger(`${label}/total`, fn),
); );
} }