Skip to content

Commit f22ba44

Browse files
committed
add: base64url support
Using string replaceAll since base64 is external base64-js. Dependency does not yet support base64url beatgammit/base64-js#53
1 parent 5857e29 commit f22ba44

File tree

2 files changed

+49
-7
lines changed

2 files changed

+49
-7
lines changed

index.js

+31-7
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,7 @@ Buffer.isEncoding = function isEncoding (encoding) {
396396
case 'ascii':
397397
case 'latin1':
398398
case 'binary':
399+
case 'base64url':
399400
case 'base64':
400401
case 'ucs2':
401402
case 'ucs-2':
@@ -544,8 +545,9 @@ function slowToString (encoding, start, end) {
544545
case 'binary':
545546
return latin1Slice(this, start, end)
546547

548+
case 'base64url':
547549
case 'base64':
548-
return base64Slice(this, start, end)
550+
return base64Slice(this, start, end, encoding)
549551

550552
case 'ucs2':
551553
case 'ucs-2':
@@ -871,8 +873,9 @@ function asciiWrite (buf, string, offset, length) {
871873
return blitBuffer(asciiToBytes(string), buf, offset, length)
872874
}
873875

874-
function base64Write (buf, string, offset, length) {
875-
return blitBuffer(base64ToBytes(string), buf, offset, length)
876+
function base64Write (buf, string, offset, length, encoding) {
877+
const b64 = encoding === 'base64url' ? base64urlToBase64(string) : string
878+
return blitBuffer(base64ToBytes(b64), buf, offset, length)
876879
}
877880

878881
function ucs2Write (buf, string, offset, length) {
@@ -930,9 +933,11 @@ Buffer.prototype.write = function write (string, offset, length, encoding) {
930933
case 'binary':
931934
return asciiWrite(this, string, offset, length)
932935

936+
case 'base64url':
933937
case 'base64':
938+
// console.log(encoding, '::', string)
934939
// Warning: maxLength not taken into account in base64Write
935-
return base64Write(this, string, offset, length)
940+
return base64Write(this, string, offset, length, encoding)
936941

937942
case 'ucs2':
938943
case 'ucs-2':
@@ -955,12 +960,14 @@ Buffer.prototype.toJSON = function toJSON () {
955960
}
956961
}
957962

958-
function base64Slice (buf, start, end) {
963+
function base64Slice (buf, start, end, encoding) {
964+
let b64
959965
if (start === 0 && end === buf.length) {
960-
return base64.fromByteArray(buf)
966+
b64 = base64.fromByteArray(buf)
961967
} else {
962-
return base64.fromByteArray(buf.slice(start, end))
968+
b64 = base64.fromByteArray(buf.slice(start, end))
963969
}
970+
return encoding === 'base64url' ? base64urlFromBase64(b64) : b64
964971
}
965972

966973
function utf8Slice (buf, start, end) {
@@ -1945,6 +1952,23 @@ function boundsError (value, length, type) {
19451952

19461953
const INVALID_BASE64_RE = /[^+/0-9A-Za-z-_]/g
19471954

1955+
const BASE64_CHAR_62 = '+'
1956+
const BASE64_CHAR_63 = '/'
1957+
const BASE64URL_CHAR_62 = '-'
1958+
const BASE64URL_CHAR_63 = '_'
1959+
1960+
function base64urlToBase64 (str) {
1961+
return str
1962+
.replaceAll(BASE64URL_CHAR_62, BASE64_CHAR_62)
1963+
.replaceAll(BASE64URL_CHAR_63, BASE64_CHAR_63)
1964+
}
1965+
1966+
function base64urlFromBase64 (str) {
1967+
return str
1968+
.replaceAll(BASE64_CHAR_62, BASE64URL_CHAR_62)
1969+
.replaceAll(BASE64_CHAR_63, BASE64URL_CHAR_63)
1970+
}
1971+
19481972
function base64clean (str) {
19491973
// Node takes equal signs as end of the Base64 encoding
19501974
str = str.split('=')[0]

test/base64.js

+18
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,21 @@ test('base64: high byte', function (t) {
5353
)
5454
t.end()
5555
})
56+
57+
test('base64url: convert to/from base64', function (t) {
58+
const base64url = '8J-Ps--4j_Cfj7PvuI8='
59+
const base64 = '8J+Ps++4j/Cfj7PvuI8='
60+
const text = '🏳️🏳️'
61+
62+
const base64urlBuf = new B(base64url, 'base64url')
63+
t.equal(base64urlBuf.toString('base64'), base64)
64+
t.equal(base64urlBuf.toString(), text)
65+
66+
const base64Buf = new B(base64, 'base64')
67+
t.equal(base64Buf.toString('base64url'), base64url)
68+
t.equal(base64Buf.toString(), text)
69+
70+
const buf = new B(text)
71+
t.equal(buf.toString('base64url'), base64url)
72+
t.end()
73+
})

0 commit comments

Comments
 (0)