um-react/src/decrypt-worker/worker.ts

28 lines
855 B
TypeScript
Raw Normal View History

2023-05-09 00:22:00 +00:00
import { WorkerServerBus } from '~/util/WorkerEventBus';
import { DECRYPTION_WORKER_ACTION_NAME } from './constants';
import type { CryptoFactory } from './crypto/CryptoBase';
import { XiamiCrypto } from './crypto/xiami/xiami';
const bus = new WorkerServerBus();
onmessage = bus.onmessage;
const decryptorFactories: CryptoFactory[] = [
// Xiami (*.xm)
() => new XiamiCrypto(),
];
bus.addEventHandler(DECRYPTION_WORKER_ACTION_NAME.DECRYPT, async (blobURI) => {
const blob = await fetch(blobURI).then((r) => r.blob());
for (const factory of decryptorFactories) {
const decryptor = factory();
if (await decryptor.isSupported(blob)) {
const decrypted = await decryptor.decrypt(blob);
return { decrypted: URL.createObjectURL(decrypted) };
}
}
throw new Error('could not decrypt file: no working decryptor found');
});