22
33const isUtf8RegExp = / ^ u t f - ? 8 $ / i;
44const isLatin1RegExp = / ^ (?: i s o - 8 8 5 9 - 1 | l a t i n 1 | u s - a s c i i ) $ / i;
5+ const bufferOps = require ( './node-buffer-ops' ) ;
56const rfc2047 = ( module . exports = { } ) ;
67
78let iconv ;
@@ -22,13 +23,13 @@ function stringify(obj) {
2223 return obj ;
2324 } else if ( obj === null || typeof obj === 'undefined' ) {
2425 return '' ;
26+ } else if ( obj instanceof Uint8Array ) {
27+ return bufferOps . fromUtf8 ( obj ) ;
2528 } else {
2629 return String ( obj ) ;
2730 }
2831}
2932
30- const replacementCharacterBuffer = Buffer . from ( '�' ) ;
31-
3233function decodeBuffer ( encodedText , encoding ) {
3334 if ( encoding === 'q' ) {
3435 encodedText = encodedText . replace ( / _ / g, ' ' ) ;
@@ -42,7 +43,7 @@ function decodeBuffer(encodedText, encoding) {
4243 numValidlyEncodedBytes += 1 ;
4344 }
4445 }
45- const buffer = Buffer . alloc (
46+ const buffer = bufferOps . allocByteBuffer (
4647 encodedText . length - numValidlyEncodedBytes * 2
4748 ) ;
4849 let j = 0 ;
@@ -62,7 +63,7 @@ function decodeBuffer(encodedText, encoding) {
6263 }
6364 return buffer ;
6465 } else {
65- return Buffer . from ( encodedText , 'base64' ) ;
66+ return bufferOps . fromBase64 ( encodedText ) ;
6667 }
6768}
6869
@@ -95,23 +96,23 @@ function decodeEncodedWord(encodedText, encoding, charset) {
9596 converter = new iconv . Iconv ( 'iso-8859-1' , 'utf-8//TRANSLIT' ) ;
9697 }
9798 try {
98- return converter . convert ( buffer ) . toString ( 'utf-8' ) ;
99+ return bufferOps . fromUtf8 ( converter . convert ( buffer ) ) ;
99100 } catch ( e2 ) { }
100101 } else if ( isUtf8RegExp . test ( charset ) ) {
101- const decoded = buffer . toString ( 'utf-8' ) ;
102+ const decoded = bufferOps . fromUtf8 ( buffer ) ;
102103 if (
103104 ! / \ufffd / . test ( decoded ) ||
104- buffer . includes ( replacementCharacterBuffer )
105+ bufferOps . includesReplacementCharacter ( buffer )
105106 ) {
106107 return decoded ;
107108 }
108109 } else if ( isLatin1RegExp . test ( charset ) ) {
109- return buffer . toString ( 'ascii' ) ;
110+ return bufferOps . fromAscii ( buffer ) ;
110111 } else if ( iconvLite && iconvLite . encodingExists ( charset ) ) {
111112 decoded = iconvLite . decode ( buffer , charset ) ;
112113 if (
113114 ! / \ufffd / . test ( decoded ) ||
114- buffer . includes ( replacementCharacterBuffer )
115+ bufferOps . includesReplacementCharacter ( buffer )
115116 ) {
116117 return decoded ;
117118 }
@@ -264,7 +265,7 @@ rfc2047.encode = (text) => {
264265 const charset = 'utf-8' ;
265266 // Around 25% faster than encodeURIComponent(token.replace(/ /g, "_")).replace(/%/g, "="):
266267 const encodedWordBody = bufferToQuotedPrintableString (
267- Buffer . from ( token , 'utf-8' )
268+ bufferOps . toUtf8 ( token )
268269 ) ;
269270 if ( previousTokenWasEncodedWord ) {
270271 result += ' ' ;
0 commit comments