clean up
This commit is contained in:
33
src/index.ts
33
src/index.ts
@@ -139,28 +139,20 @@ function byteStringToBytes(byteStr: string): Uint8Array {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function arrayBufferToBase64String(arrayBuffer: ArrayBuffer): string {
|
function arrayBufferToBase64String(arrayBuffer: ArrayBuffer): string {
|
||||||
const byteArray = new Uint8Array(arrayBuffer)
|
return btoa(bytesToByteString(new Uint8Array(arrayBuffer)))
|
||||||
const byteStr = bytesToByteString(byteArray)
|
|
||||||
return btoa(byteStr)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function base64StringToArrayBuffer(b64str: string): ArrayBuffer {
|
function base64StringToArrayBuffer(b64str: string): ArrayBuffer {
|
||||||
const byteStr = atob(b64str)
|
return byteStringToBytes(atob(b64str)).buffer
|
||||||
const bytes = byteStringToBytes(byteStr)
|
|
||||||
return bytes.buffer
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function textToArrayBuffer(str: string): ArrayBuffer {
|
function textToArrayBuffer(str: string): ArrayBuffer {
|
||||||
const buf = decodeURI(encodeURIComponent(str)) // 2 bytes for each char
|
return byteStringToBytes(decodeURI(encodeURIComponent(str)))
|
||||||
const bytes = byteStringToBytes(buf)
|
|
||||||
return bytes
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
function arrayBufferToText(arrayBuffer: ArrayBuffer): string {
|
function arrayBufferToText(arrayBuffer: ArrayBuffer): string {
|
||||||
const byteArray = new Uint8Array(arrayBuffer)
|
return bytesToByteString(new Uint8Array(arrayBuffer))
|
||||||
const byteStr = bytesToByteString(byteArray)
|
|
||||||
return byteStr
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function arrayBufferToBase64Url(arrayBuffer: ArrayBuffer): string {
|
function arrayBufferToBase64Url(arrayBuffer: ArrayBuffer): string {
|
||||||
@@ -212,23 +204,8 @@ async function importKey(key: string | JsonWebKey, algorithm: SubtleCryptoImport
|
|||||||
}
|
}
|
||||||
|
|
||||||
function decodePayload(raw: string): JwtHeader | JwtPayload | null {
|
function decodePayload(raw: string): JwtHeader | JwtPayload | null {
|
||||||
switch (raw.length % 4) {
|
|
||||||
case 0:
|
|
||||||
break
|
|
||||||
case 1:
|
|
||||||
raw += '==='
|
|
||||||
break
|
|
||||||
case 2:
|
|
||||||
raw += '=='
|
|
||||||
break
|
|
||||||
case 3:
|
|
||||||
raw += '='
|
|
||||||
break
|
|
||||||
default:
|
|
||||||
throw new Error('Invalid base64url string!')
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
raw += '='.repeat(4-(raw.length % 4))
|
||||||
return JSON.parse(atob(raw))
|
return JSON.parse(atob(raw))
|
||||||
} catch {
|
} catch {
|
||||||
return null
|
return null
|
||||||
|
|||||||
Reference in New Issue
Block a user