import * as Stream from 'stream' ; import Logger from '../../../tools/env.logger' ; export type TMap = { [ key ] : string [ ] ; } ; export type TStats = { [ key ] : number ; } ; export default class Transform extends Stream . Transform { private _logger : Logger ; private _rest : string = '' ; private _lines : number [ ] = [ ] ; private _stopped : boolean = false ; constructor ( options ) { super ( options ) ; this . _logger = new Logger ( `Transform.Inspecting` ) ; } public getLines ( ) { return this . _lines ; } public _transform ( chunk , encoding , callback ) { let output = '' ; if ( typeof chunk === 'string' ) { output = this . _rest + chunk ; } else { output = this . _rest + chunk . toString ( 'utf8' ) ; } const rest = this . _getRest ( output ) ; this . _rest = rest . rest ; output = rest . cleared ; const matches = output . match ( /\u0002\d*\u0002/gi ) ; if ( matches !== null ) { matches . forEach ( ( match ) => { const num = parseInt ( match . replace ( /\u0002/gi , '' ) , 10 ) ; if ( isNaN ( num ) || ! isFinite ( num ) ) { this . _logger . warn ( `Fail to detect line number for ${ match }` ) ; return ; } this . _lines . push ( num ) ; } ) ; } if ( this . _stopped ) { return ; } if ( callback !== undefined ) { callback ( undefined , output ) ; } } public stop ( ) { this . _stopped = true ; } private _getRest ( str ) { const last = str . length - 1 ; for ( let i = last ; i >= 0 ; i -= 1 ) { if ( str [ i ] === '\n' && i > 0 ) { return { rest : str . substr ( i + 1 , last ) , cleared : str . substr ( 0 , i + 1 ) , } ; } } return { rest : '' , cleared : str } ; } }