From 1b116a8db3576c868238d611153faa7f9805cff4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=B2=81=E6=A0=91=E4=BA=BA?= Date: Mon, 16 Sep 2024 22:27:45 +0100 Subject: [PATCH] refactor: simplify logging and catch errors --- src/decrypt-worker/worker/handler/decrypt.ts | 7 +++--- .../worker/handler/qmcv2_parser.ts | 24 ++++++------------- src/util/fnWrapper.ts | 21 ++++++---------- src/util/logUtils.ts | 6 ++--- 4 files changed, 21 insertions(+), 37 deletions(-) diff --git a/src/decrypt-worker/worker/handler/decrypt.ts b/src/decrypt-worker/worker/handler/decrypt.ts index 7dc2a91..18d7727 100644 --- a/src/decrypt-worker/worker/handler/decrypt.ts +++ b/src/decrypt-worker/worker/handler/decrypt.ts @@ -19,7 +19,7 @@ class DecryptCommandHandler { this.label = `DecryptCommandHandler(${label})`; } - log(label: string, fn: () => R): R { + log(label: string, fn: () => Promise): Promise { return timedLogger(`${this.label}: ${label}`, fn); } @@ -52,7 +52,7 @@ class DecryptCommandHandler { } 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), ); 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; + const id = payloadId.replace('://', ':'); const label = `decrypt(${id})`; return withTimeGroupedLogs(label, async () => { const buffer = await fetch(blobURI).then((r) => r.arrayBuffer()); diff --git a/src/decrypt-worker/worker/handler/qmcv2_parser.ts b/src/decrypt-worker/worker/handler/qmcv2_parser.ts index f232133..89db7ae 100644 --- a/src/decrypt-worker/worker/handler/qmcv2_parser.ts +++ b/src/decrypt-worker/worker/handler/qmcv2_parser.ts @@ -1,26 +1,16 @@ import { fetchParakeet, FooterParserState } from '@um/libparakeet'; import type { FetchMusicExNamePayload } from '~/decrypt-worker/types'; 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) => { - const label = `decrypt(${id})`; - return withTimeGroupedLogs(label, async () => { - const parakeet = await timedLogger(`${label}/init`, fetchParakeet); - const blob = await timedLogger(`${label}/fetch-src`, async () => - fetch(blobURI, { headers: { Range: 'bytes=-1024' } }).then((r) => r.blob()), - ); + const label = `qmcMusixEx(${id.replace('://', ':')})`; + return timedLogger(label, async () => { + const parakeet = await fetchParakeet(); + const blob = await fetch(blobURI, { headers: { Range: 'bytes=-1024' } }).then((r) => r.blob()); + const buffer = await blob.arrayBuffer(); - const buffer = await timedLogger(`${label}/read-src`, async () => { - // 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); + const parsed = makeQMCv2FooterParser(parakeet).parse(buffer.slice(-1024)); if (parsed.state === FooterParserState.OK) { return parsed.mediaName; } diff --git a/src/util/fnWrapper.ts b/src/util/fnWrapper.ts index 8c3bdf4..b830a6c 100644 --- a/src/util/fnWrapper.ts +++ b/src/util/fnWrapper.ts @@ -1,20 +1,13 @@ -function isPromise(p: unknown): p is Promise { - return !!p && typeof p === 'object' && 'then' in p && 'catch' in p && 'finally' in p; -} - -export function wrapFunctionCall(pre: () => void, post: () => void, fn: () => R): R { +export async function wrapFunctionCall( + pre: () => void, + post: () => void, + fn: () => Promise, +): Promise { pre(); try { - const result = fn(); - - if (isPromise(result)) { - result.finally(post); - } - - return result; - } catch (e) { + return await fn(); + } finally { post(); - throw e; } } diff --git a/src/util/logUtils.ts b/src/util/logUtils.ts index d691faa..68d533d 100644 --- a/src/util/logUtils.ts +++ b/src/util/logUtils.ts @@ -1,6 +1,6 @@ import { wrapFunctionCall } from './fnWrapper'; -export function timedLogger(label: string, fn: () => R): R { +export async function timedLogger(label: string, fn: () => Promise): Promise { if (import.meta.env.VITE_ENABLE_PERF_LOG !== '1') { return fn(); } else { @@ -12,13 +12,13 @@ export function timedLogger(label: string, fn: () => R): R { } } -export function withGroupedLogs(label: string, fn: () => R): R { +export async function withGroupedLogs(label: string, fn: () => Promise): Promise { if (import.meta.env.VITE_ENABLE_PERF_LOG !== '1') { return fn(); } else { return wrapFunctionCall( () => console.group(label), - () => (console.groupEnd as (label: string) => void)(label), + () => console.groupEnd(), () => timedLogger(`${label}/total`, fn), ); }