import fs from 'fs' ; import { LogFileOperationResult , LogFileParsingState , ParsingMetadata } from 'root/app/log-parser/model' ; export async function getUserCredentials ( path , state ) { return new Promise < LogFileOperationResult < { DisplayName : string | undefined ; AccountID : string ; } > > ( ( resolve , reject ) => { const stream = fs . createReadStream ( path , { encoding : 'utf8' , autoClose : true , emitClose : true , start : 0 , } ) ; let DisplayName = '' ; let AccountID = '' ; let HasInjection = false ; let loginStateChangeIndex = 0 ; let chunkCursor = 0 ; stream . on ( 'data' , ( chunk ) => { if ( HasInjection || chunk . indexOf ( '[MTGA.Pro Logger]' ) !== - 1 ) { HasInjection = true ; const loginStateChange = chunk . lastIndexOf ( '**LoginStateChanged**' ) ; if ( loginStateChange !== - 1 ) { loginStateChangeIndex = chunkCursor + loginStateChange ; DisplayName = '' ; AccountID = '' ; } const accountPrefix = '**Userdata**' ; const userID = '"userId":"' ; const accountPrefixIndex = chunk . lastIndexOf ( accountPrefix ) ; if ( accountPrefixIndex !== - 1 && accountPrefixIndex + chunkCursor > loginStateChangeIndex ) { const accountIdLocator = accountPrefixIndex + accountPrefix . length ; const AccountIDStart = chunk . indexOf ( userID , accountIdLocator ) ; AccountID = chunk . slice ( AccountIDStart + userID . length ) . split ( '"' , 2 ) [ 0 ] ; const screenNameRealPrefix = '"screenName":"' ; const screenNameIndex = chunk . indexOf ( screenNameRealPrefix , accountPrefixIndex ) ; if ( screenNameIndex !== - 1 ) { const screenNameLocator = screenNameIndex + screenNameRealPrefix . length ; DisplayName = chunk . slice ( screenNameLocator ) . split ( '"' , 2 ) [ 0 ] ; } } } else { const accountPrefix = '"authenticateResponse": ' ; const userID = '"clientId": "' ; const accountPrefixIndex = chunk . indexOf ( accountPrefix ) ; if ( accountPrefixIndex !== - 1 ) { const accountIdLocator = accountPrefixIndex + accountPrefix . length ; const AccountIDStart = chunk . indexOf ( userID , accountIdLocator ) ; AccountID = chunk . slice ( AccountIDStart + userID . length ) . split ( '"' , 2 ) [ 0 ] ; const screenNameRealPrefix = '"screenName": "' ; const screenNameIndex = chunk . indexOf ( screenNameRealPrefix , accountPrefixIndex ) ; if ( screenNameIndex !== - 1 ) { const screenNameLocator = screenNameIndex + screenNameRealPrefix . length ; DisplayName = chunk . slice ( screenNameLocator ) . split ( '"' , 2 ) [ 0 ] ; } } } chunkCursor += chunk . length ; } ) ; stream . on ( 'close' , ( ) => { if ( DisplayName !== '' && AccountID !== '' ) { resolve ( [ { DisplayName , AccountID } , { ... state , bytesRead : 0 } , ] ) ; } else if ( DisplayName === '' && AccountID !== '' ) { resolve ( [ { DisplayName : undefined , AccountID } , { ... state , bytesRead : 0 } , ] ) ; } else { reject ( HasInjection ? 'Awaiting User ID. It will take up to 30s.' : 'Awaiting User ID. Switch into MTGA window and wait about 30s' ) ; } stream . close ( ) ; } ) ; stream . on ( 'error' , ( err ) => { reject ( err ) ; stream . close ( ) ; } ) ; } ) ; }