Skip to content

Commit

Permalink
perf: improve base64url encoding when native is available
Browse files Browse the repository at this point in the history
  • Loading branch information
panva committed Apr 6, 2021
1 parent 995b278 commit 385689e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 21 deletions.
42 changes: 42 additions & 0 deletions .versionrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"commit-all": true,
"scripts": {
"postchangelog": "sed -i '' -e 's/### \\[/## [/g' CHANGELOG.md"
},
"types": [
{
"type": "feat",
"section": "Features"
},
{
"type": "fix",
"section": "Bug Fixes"
},
{
"type": "chore",
"hidden": true
},
{
"type": "docs",
"hidden": true
},
{
"type": "style",
"hidden": true
},
{
"type": "refactor",
"section": "Refactor",
"hidden": true
},
{
"type": "perf",
"section": "Performance",
"hidden": false
},
{
"type": "test",
"hidden": true
}
]
}
22 changes: 6 additions & 16 deletions lib/help/base64url.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
const fromBase64 = (base64) => {
return base64.replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_')
if (Buffer.isEncoding('base64url')) {
module.exports.encode = (input) => Buffer.from(input).toString('base64url')
} else {
module.exports.encode = (input) =>
input.toString('base64').replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_')
}

const toBase64 = (base64url) => {
return base64url.replace(/-/g, '+').replace(/_/g, '/')
}

const encode = (buf) => {
return fromBase64(buf.toString('base64'))
}

const decode = (input) => {
return Buffer.from(toBase64(input), 'base64')
}

module.exports.decode = decode
module.exports.encode = encode
module.exports.decode = (input) => Buffer.from(input, 'base64')
5 changes: 0 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,5 @@
"files": [
"test/**/*.test.js"
]
},
"standard-version": {
"scripts": {
"postchangelog": "sed -i '' -e 's/### \\[/## [/g' CHANGELOG.md"
}
}
}

0 comments on commit 385689e

Please sign in to comment.