@@ -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
878881function 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
966973function utf8Slice ( buf , start , end ) {
@@ -1945,6 +1952,23 @@ function boundsError (value, length, type) {
19451952
19461953const INVALID_BASE64_RE = / [ ^ + / 0 - 9 A - Z a - 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+
19481972function base64clean ( str ) {
19491973 // Node takes equal signs as end of the Base64 encoding
19501974 str = str . split ( '=' ) [ 0 ]
0 commit comments