feat: 🎸 update decodePayload
This commit is contained in:
12
src/index.ts
12
src/index.ts
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user