import { BiStream } from '../stream' ; export interface StreamHook { param data onWrite ( data ) : void ; param buf param read onRead ( buf , read ) : void ; onClose ( ) : void ; } export class HookedStream implements BiStream { constructor ( private _stream , public hook = { } ) { } get ended ( ) : boolean { return this . _stream . ended ; } write ( data ) { if ( this . hook . onWrite ) this . hook . onWrite ( data ) ; return this . _stream . write ( data ) ; } async read ( buffer ) { const read = await this . _stream . read ( buffer ) ; if ( this . hook . onRead ) this . hook . onRead ( buffer , read ) ; return read ; } close ( ) { if ( this . hook . onClose ) this . hook . onClose ( ) ; this . _stream . close ( ) ; } }