mirror of
https://github.com/danbulant/jose
synced 2026-05-23 06:18:58 +00:00
21 lines
667 B
TypeScript
21 lines
667 B
TypeScript
import { decoder } from '../../lib/buffer_utils.js'
|
|
|
|
let encodeImpl: (input: Uint8Array | string) => string
|
|
|
|
function normalize(input: string | Uint8Array) {
|
|
let encoded = input
|
|
if (encoded instanceof Uint8Array) {
|
|
encoded = decoder.decode(encoded)
|
|
}
|
|
return encoded
|
|
}
|
|
|
|
if (Buffer.isEncoding('base64url')) {
|
|
encodeImpl = (input) => Buffer.from(input).toString(<BufferEncoding>'base64url')
|
|
} else {
|
|
encodeImpl = (input) =>
|
|
Buffer.from(input).toString('base64').replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_')
|
|
}
|
|
|
|
export const encode = encodeImpl
|
|
export const decode = (input: Uint8Array | string) => Buffer.from(normalize(input), 'base64')
|