mirror of
https://git.unlock-music.dev/um/um-react.git
synced 2024-11-23 21:22:16 +00:00
refactor: extend transformBlob
to take cleanup function
This commit is contained in:
parent
0f9748ec14
commit
3597e7789b
@ -9,11 +9,11 @@ export class QMC2Crypto implements CryptoBase {
|
||||
checkByDecryptHeader = false;
|
||||
|
||||
async decrypt(buffer: ArrayBuffer): Promise<Blob> {
|
||||
// FIXME: Move the cleanup to transformBlob
|
||||
const mod = await fetchParakeet();
|
||||
const footerParser = mod.make.QMCv2FooterParser(SEED, ENC_V2_KEY_1, ENC_V2_KEY_2);
|
||||
return transformBlob(buffer, (p) => p.make.QMCv2(footerParser)).finally(() => {
|
||||
footerParser.delete();
|
||||
const parakeet = await fetchParakeet();
|
||||
const footerParser = parakeet.make.QMCv2FooterParser(SEED, ENC_V2_KEY_1, ENC_V2_KEY_2);
|
||||
return transformBlob(buffer, (p) => p.make.QMCv2(footerParser), {
|
||||
parakeet,
|
||||
cleanup: () => footerParser.delete(),
|
||||
});
|
||||
}
|
||||
|
||||
@ -35,12 +35,14 @@ export class QMC2CryptoWithKey implements CryptoBase {
|
||||
throw new Error('key was not provided');
|
||||
}
|
||||
|
||||
// FIXME: Move the cleanup to transformBlob
|
||||
const mod = await fetchParakeet();
|
||||
const parakeet = await fetchParakeet();
|
||||
const textEncoder = new TextEncoder();
|
||||
const key = textEncoder.encode(options.qmc2Key);
|
||||
const keyCrypto = mod.make.QMCv2KeyCrypto(SEED, ENC_V2_KEY_1, ENC_V2_KEY_2);
|
||||
return transformBlob(buffer, (p) => p.make.QMCv2EKey(key, keyCrypto));
|
||||
const keyCrypto = parakeet.make.QMCv2KeyCrypto(SEED, ENC_V2_KEY_1, ENC_V2_KEY_2);
|
||||
return transformBlob(buffer, (p) => p.make.QMCv2EKey(key, keyCrypto), {
|
||||
parakeet,
|
||||
cleanup: () => keyCrypto.delete(),
|
||||
});
|
||||
}
|
||||
|
||||
public static make() {
|
||||
|
@ -5,21 +5,24 @@ import { UnsupportedSourceFile } from './DecryptError';
|
||||
export async function transformBlob(
|
||||
blob: Blob | ArrayBuffer,
|
||||
transformerFactory: (p: Parakeet) => Transformer | Promise<Transformer>,
|
||||
parakeet?: Parakeet
|
||||
{ cleanup, parakeet }: { cleanup?: () => void; parakeet?: Parakeet } = {}
|
||||
) {
|
||||
const cleanup: (() => void)[] = [];
|
||||
const registeredCleanupFns: (() => void)[] = [];
|
||||
if (cleanup) {
|
||||
registeredCleanupFns.push(cleanup);
|
||||
}
|
||||
|
||||
try {
|
||||
const mod = parakeet ?? (await fetchParakeet());
|
||||
const transformer = await transformerFactory(mod);
|
||||
cleanup.push(() => transformer.delete());
|
||||
registeredCleanupFns.push(() => transformer.delete());
|
||||
|
||||
const reader = mod.make.Reader(await toArrayBuffer(blob));
|
||||
cleanup.push(() => reader.delete());
|
||||
registeredCleanupFns.push(() => reader.delete());
|
||||
|
||||
const sink = mod.make.WriterSink();
|
||||
const writer = sink.getWriter();
|
||||
cleanup.push(() => writer.delete());
|
||||
registeredCleanupFns.push(() => writer.delete());
|
||||
|
||||
const result = transformer.Transform(writer, reader);
|
||||
if (result === TransformResult.ERROR_INVALID_FORMAT) {
|
||||
@ -30,6 +33,6 @@ export async function transformBlob(
|
||||
|
||||
return sink.collectBlob();
|
||||
} finally {
|
||||
cleanup.forEach((clean) => clean());
|
||||
registeredCleanupFns.forEach((cleanup) => cleanup());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user