import EventEmitter from 'events' ; import { Writable , WritableOptions } from 'stream' ; import { handleData } from './internals' ; import { defaultOptions , FastImageError , ImageInfo , Options } from './models' ; export class FastImageStream extends Writable { buffer : Buffer ; threshold : number ; start : bigint ; finished : boolean ; constructor ( options ) { super ( options as WritableOptions ) ; this . threshold = options . threshold ?? defaultOptions . threshold ; this . buffer = Buffer . alloc ( 0 ) ; this . start = process . hrtime . bigint ( ) ; this . finished = false ; } analyze ( chunk ) { this . buffer = Buffer . concat ( [ this . buffer , chunk ] ) ; this . finished = handleData ( this . buffer , undefined , this . threshold , this . start , new EventEmitter ( ) , ( error , data ? ) => { if ( error ) { this . emit ( 'error' , error ) ; } else { this . emit ( 'info' , data ) ; } this . destroy ( ) ; } ) ; } _write ( chunk , _e , cb ) { this . analyze ( chunk ) ; cb ( ) ; } _writev ( chunks , cb ) { for ( const { chunk } of chunks ) { this . analyze ( chunk ) ; } cb ( ) ; } _final ( cb ) { if ( this . finished ) { cb ( ) ; return ; } cb ( new FastImageError ( 'Unsupported data.' , 'UNSUPPORTED' ) ) ; } }