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 {
|
function decodePayload<T = any>(raw: string): T | undefined {
|
||||||
try {
|
try {
|
||||||
const binaryString = atob(raw)
|
const binaryString = atob(raw)
|
||||||
const encodedString = encodeURIComponent(binaryString).replace(/%([0-9A-F]{2})/g, (_match, p1) => {
|
const bytes = new Uint8Array(binaryString.length);
|
||||||
return String.fromCharCode(parseInt(p1, 16));
|
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 {
|
} catch {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user