1
0

feat: 🎸 update decodePayload

This commit is contained in:
kira924age
2023-12-21 21:28:29 +09:00
committed by Toby
parent b7ccf86f55
commit 5432f1997e

View File

@@ -199,11 +199,15 @@ async function importKey(key: string | JsonWebKey, algorithm: SubtleCryptoImport
function decodePayload<T = any>(raw: string): T | undefined {
try {
const binaryString = atob(raw)
const encodedString = encodeURIComponent(binaryString).replace(/%([0-9A-F]{2})/g, (_match, p1) => {
return String.fromCharCode(parseInt(p1, 16));
});
const bytes = new Uint8Array(binaryString.length);
for (let i = 0; i < binaryString.length; i++) {
bytes[i] = binaryString.charCodeAt(i);
}
return JSON.parse(decodeURIComponent(encodedString));
const decoder = new TextDecoder('utf-8');
const decodedString = decoder.decode(bytes);
return JSON.parse(decodedString);
} catch {
return
}