Skip to content

Commit

Permalink
prefer base64urlSlice function, not mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
dcousens authored Oct 6, 2023
1 parent 340c098 commit 26a08e6
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,7 @@ function slowToString (encoding, start, end) {
return latin1Slice(this, start, end)

case 'base64url':
return base64urlSlice(this, start, end, encoding)
case 'base64':
return base64Slice(this, start, end, encoding)

Expand Down Expand Up @@ -947,14 +948,19 @@ Buffer.prototype.toJSON = function toJSON () {
}
}

function base64urlSlice (buf, start, end, encoding) {
if (start === 0 && end === buf.length) {
return base64urlFromBase64(base64.fromByteArray(buf))
}

return base64urlFromBase64(base64.fromByteArray(buf.slice(start, end)))
}

function base64Slice (buf, start, end, encoding) {
let b64
if (start === 0 && end === buf.length) {
b64 = base64.fromByteArray(buf)
} else {
b64 = base64.fromByteArray(buf.slice(start, end))
return base64.fromByteArray(buf)
}
return encoding === 'base64url' ? base64urlFromBase64(b64) : b64
return base64.fromByteArray(buf.slice(start, end))
}

function utf8Slice (buf, start, end) {
Expand Down

0 comments on commit 26a08e6

Please sign in to comment.