Исправление утечки памяти, оптимизация кода, лучшая читаемость кода
This commit is contained in:
@@ -6,53 +6,35 @@ import * as secp256k1 from '@noble/secp256k1';
|
||||
|
||||
|
||||
self.onmessage = async (event: MessageEvent) => {
|
||||
const { action, data } = event.data;
|
||||
const { id, type, payload } = event.data;
|
||||
|
||||
switch (action) {
|
||||
case 'encodeWithPassword': {
|
||||
const { password, payload, task } = data;
|
||||
const result = await encodeWithPassword(password, payload);
|
||||
self.postMessage({ action: 'encodeWithPasswordResult', result, task });
|
||||
break;
|
||||
try {
|
||||
let result;
|
||||
switch (type) {
|
||||
case 'encodeWithPassword':
|
||||
result = await encodeWithPassword(payload.password, payload.data);
|
||||
break;
|
||||
case 'decodeWithPassword':
|
||||
result = await decodeWithPassword(payload.password, payload.data);
|
||||
break;
|
||||
case 'encrypt':
|
||||
result = await encrypt(payload.publicKey, payload.data);
|
||||
break;
|
||||
case 'decrypt':
|
||||
result = await decrypt(payload.privateKey, payload.data);
|
||||
break;
|
||||
case 'chacha20Encrypt':
|
||||
result = await chacha20Encrypt(payload.data);
|
||||
break;
|
||||
case 'chacha20Decrypt':
|
||||
result = await chacha20Decrypt(payload.ciphertext, payload.nonce, payload.key);
|
||||
break;
|
||||
default:
|
||||
throw new Error(`Unknown action: ${type}`);
|
||||
}
|
||||
case 'chacha20Encrypt': {
|
||||
const { payload, task } = data;
|
||||
const result = await chacha20Encrypt(payload);
|
||||
self.postMessage({ action: 'chacha20EncryptResult', result, task });
|
||||
break;
|
||||
}
|
||||
case 'chacha20Decrypt': {
|
||||
const { ciphertext, nonce, key, task } = data;
|
||||
const result = await chacha20Decrypt(ciphertext, nonce, key);
|
||||
self.postMessage({ action: 'chacha20DecryptResult', result, task });
|
||||
break;
|
||||
}
|
||||
case 'decodeWithPassword': {
|
||||
const { password, payload, task } = data;
|
||||
try{
|
||||
const result = await decodeWithPassword(password, payload);
|
||||
self.postMessage({ action: 'decodeWithPasswordResult', result, task });
|
||||
return;
|
||||
}catch(e){
|
||||
const result = null;
|
||||
self.postMessage({ action: 'decodeWithPasswordResult', result, task });
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'decrypt': {
|
||||
const { payload: encryptedData, privateKey, task } = data;
|
||||
const result = await decrypt(encryptedData, privateKey);
|
||||
self.postMessage({ action: 'decryptResult', result, task });
|
||||
break;
|
||||
}
|
||||
case 'encrypt': {
|
||||
const { payload: plainData, publicKey, task } = data;
|
||||
const result = await encrypt(plainData, publicKey);
|
||||
self.postMessage({ action: 'encryptResult', result, task });
|
||||
break;
|
||||
}
|
||||
default:
|
||||
console.error(`Unknown action: ${action}`);
|
||||
self.postMessage({ id, ok: true, data: result });
|
||||
} catch (error) {
|
||||
self.postMessage({ id, ok: false, error: String(error) });
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user