@@ -396,6 +396,7 @@ Buffer.isEncoding = function isEncoding (encoding) {
396
396
case 'ascii' :
397
397
case 'latin1' :
398
398
case 'binary' :
399
+ case 'base64url' :
399
400
case 'base64' :
400
401
case 'ucs2' :
401
402
case 'ucs-2' :
@@ -544,8 +545,9 @@ function slowToString (encoding, start, end) {
544
545
case 'binary' :
545
546
return latin1Slice ( this , start , end )
546
547
548
+ case 'base64url' :
547
549
case 'base64' :
548
- return base64Slice ( this , start , end )
550
+ return base64Slice ( this , start , end , encoding )
549
551
550
552
case 'ucs2' :
551
553
case 'ucs-2' :
@@ -871,8 +873,9 @@ function asciiWrite (buf, string, offset, length) {
871
873
return blitBuffer ( asciiToBytes ( string ) , buf , offset , length )
872
874
}
873
875
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 )
876
879
}
877
880
878
881
function ucs2Write ( buf , string , offset , length ) {
@@ -930,9 +933,11 @@ Buffer.prototype.write = function write (string, offset, length, encoding) {
930
933
case 'binary' :
931
934
return asciiWrite ( this , string , offset , length )
932
935
936
+ case 'base64url' :
933
937
case 'base64' :
938
+ // console.log(encoding, '::', string)
934
939
// Warning: maxLength not taken into account in base64Write
935
- return base64Write ( this , string , offset , length )
940
+ return base64Write ( this , string , offset , length , encoding )
936
941
937
942
case 'ucs2' :
938
943
case 'ucs-2' :
@@ -955,12 +960,14 @@ Buffer.prototype.toJSON = function toJSON () {
955
960
}
956
961
}
957
962
958
- function base64Slice ( buf , start , end ) {
963
+ function base64Slice ( buf , start , end , encoding ) {
964
+ let b64
959
965
if ( start === 0 && end === buf . length ) {
960
- return base64 . fromByteArray ( buf )
966
+ b64 = base64 . fromByteArray ( buf )
961
967
} else {
962
- return base64 . fromByteArray ( buf . slice ( start , end ) )
968
+ b64 = base64 . fromByteArray ( buf . slice ( start , end ) )
963
969
}
970
+ return encoding === 'base64url' ? base64urlFromBase64 ( b64 ) : b64
964
971
}
965
972
966
973
function utf8Slice ( buf , start , end ) {
@@ -1945,6 +1952,23 @@ function boundsError (value, length, type) {
1945
1952
1946
1953
const INVALID_BASE64_RE = / [ ^ + / 0 - 9 A - Z a - z - _ ] / g
1947
1954
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
+
1948
1972
function base64clean ( str ) {
1949
1973
// Node takes equal signs as end of the Base64 encoding
1950
1974
str = str . split ( '=' ) [ 0 ]
0 commit comments