@@ -4,6 +4,7 @@ const jschardet = require('jschardet')
44const isBuffer = require ( 'is-buffer' )
55const iconv = require ( 'iconv-lite' )
66const charset = require ( 'charset' )
7+ const he = require ( 'he' )
78
89const REGEX_CHARSET = / c h a r s e t = [ " ] * ( [ ^ > " \s ] + ) / i
910
@@ -13,7 +14,9 @@ const inferredEncoding = content => {
1314}
1415
1516module . exports = targetEncoding => {
16- if ( ! targetEncoding ) throw new TypeError ( 'Need to provide a target encoding.' )
17+ if ( ! iconv . encodingExists ( targetEncoding ) ) {
18+ throw new TypeError ( `Target encoding '${ targetEncoding } ' not supported.` )
19+ }
1720
1821 const getEncoding = ( content , contentType ) =>
1922 charset ( { 'content-type' : contentType } , content ) ||
@@ -23,6 +26,13 @@ module.exports = targetEncoding => {
2326 return ( buffer , contentType ) => {
2427 if ( ! isBuffer ( buffer ) ) throw new TypeError ( 'content should be a buffer.' )
2528 const encoding = getEncoding ( buffer , contentType )
26- return iconv . decode ( buffer , encoding ) . replace ( REGEX_CHARSET , targetEncoding )
29+
30+ const str = iconv
31+ . decode ( buffer , encoding )
32+ . replace ( REGEX_CHARSET , targetEncoding )
33+ . toString ( )
34+
35+ // Ensure to resolve Base64 entities
36+ return he . decode ( str )
2737 }
2838}
0 commit comments