Upgrade to use @unlock-music/crypto #78

Merged
lsr merged 18 commits from upgrade/um-crypto-part1 into main 2024-09-24 22:19:32 +00:00
4 changed files with 21 additions and 37 deletions
Showing only changes of commit 1b116a8db3 - Show all commits

View File

@ -19,7 +19,7 @@ class DecryptCommandHandler {
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);
}
@ -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());

View File

@ -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;
}

View File

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

View File

@ -1,6 +1,6 @@
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') {
return fn();
} 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') {
return fn();
} else {
return wrapFunctionCall(
() => console.group(label),
() => (console.groupEnd as (label: string) => void)(label),
() => console.groupEnd(),
() => timedLogger(`${label}/total`, fn),
);
}