mirror of
https://github.com/danbulant/jose
synced 2026-05-24 12:35:36 +00:00
perf: use 'base64url' encoding when available in Node.js runtime
This commit is contained in:
parent
02941146c7
commit
808f06cd08
1 changed files with 13 additions and 4 deletions
|
|
@ -1,12 +1,21 @@
|
||||||
import { decoder } from '../../lib/buffer_utils.js'
|
import { decoder } from '../../lib/buffer_utils.js'
|
||||||
|
|
||||||
export const encode = (input: Uint8Array | string) =>
|
let encodeImpl: (input: Uint8Array | string) => string
|
||||||
Buffer.from(input).toString('base64').replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_')
|
|
||||||
|
|
||||||
export const decode = (input: Uint8Array | string) => {
|
function normalize(input: string | Uint8Array) {
|
||||||
let encoded = input
|
let encoded = input
|
||||||
if (encoded instanceof Uint8Array) {
|
if (encoded instanceof Uint8Array) {
|
||||||
encoded = decoder.decode(encoded)
|
encoded = decoder.decode(encoded)
|
||||||
}
|
}
|
||||||
return new Uint8Array(Buffer.from(encoded, 'base64'))
|
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')
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue