import chardet from 'chardet' ; import iconv from 'iconv-lite' ; import { Transform , TransformCallback } from 'stream' ; export default class IconvTransform extends Transform { private temp : string = '' ; public _transform ( chunk , encoding , callback ) { this . temp += chunk ; callback ( ) ; } public _flush ( callback ) { const buffer = Buffer . from ( this . temp ) ; const charset = chardet . detect ( buffer ) ; let output = buffer . toString ( ) ; if ( charset ) { output = typeof charset === 'string' ? iconv . decode ( buffer , charset as string ) : iconv . decode ( buffer , ( charset as chardet . Confidence [ ] ) [ 0 ] . name ) ; } this . push ( output ) ; callback ( ) ; } }