import { createChunkReader } from 'root/app/log-parser/chunk_reader' ; import { LogFileOperationResult , LogFileParsingState , ParsingMetadata } from 'root/app/log-parser/model' ; import { extractEventData , extractValue } from 'root/app/log-parser/parsing' ; import { asString } from 'root/lib/type_utils' ; export async function getFileId ( path , state , options ) { return new Promise < LogFileOperationResult < string > > ( ( resolve , reject ) => { const chunkStream = createChunkReader ( path , state . bytesRead ) ; chunkStream . onData ( ( chunk , bytesRead ) => { let chunkCursor = 0 ; while ( true ) { const nextEventIndex = chunk . indexOf ( options . eventPrefix , chunkCursor ) ; if ( nextEventIndex === - 1 ) { return ; } const nextEventStart = nextEventIndex + options . eventPrefix . length ; const whitespacesPadding = 10 ; const eventStart = chunk . slice ( nextEventStart , nextEventStart + options . fileId . eventName . length + whitespacesPadding ) . trimLeft ( ) . toUpperCase ( ) ; if ( eventStart . indexOf ( options . fileId . eventName . toUpperCase ( ) ) !== 0 ) { chunkCursor = nextEventStart ; continue ; } const jsonStart = chunk . indexOf ( '{' , nextEventStart ) ; const partialJSON = chunk . slice ( jsonStart ) . split ( /\}\r?\n/ , 2 ) [ 0 ] ; const json = `${ partialJSON }}` ; try { const parsedEvent = JSON . parse ( json ) ; const data = extractEventData ( options . fileId . eventName , parsedEvent ) ; if ( data === undefined ) { reject ( new Error ( 'Invalid file id event. No data associated.' ) ) ; chunkStream . close ( ) ; return ; } const rawId = extractValue ( data , options . fileId . attributesPathToId ) ; const id = asString ( rawId ) ; if ( id === undefined ) { reject ( new Error ( `Could not find a valid id for the log file in the event data: ${ data }` ) ) ; } else { resolve ( [ id , { ... state , bytesRead : bytesRead + jsonStart + json . length + 1 } ] ) ; } } catch ( err ) { reject ( new Error ( 'Enable detailed logs in MTGA account and restart game...' ) ) ; } chunkStream . close ( ) ; return ; } } ) ; chunkStream . onEnd ( ( ) => { reject ( new Error ( 'Enable detailed logs in MTGA account and restart game...' ) ) ; chunkStream . close ( ) ; } ) ; chunkStream . onError ( ( err ) => { reject ( err ) ; chunkStream . close ( ) ; } ) ; } ) ; }