Upgrade to use @unlock-music/crypto #78
@ -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());
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
|
@ -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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user