import * as url from 'url' ; import * as http from 'http' ; function fatal ( err ) { console . error ( 'Missing or invalid credentials.' ) ; console . error ( err ) ; process . exit ( 1 ) ; } const expectedArgvCount = 5 ; function main ( argv ) { if ( argv . length !== expectedArgvCount ) { fatal ( `Wrong number of arguments. Expected ${ expectedArgvCount }. Got ${ argv . length } instead.` ) ; return ; } if ( ! process . env [ 'THEIA_GIT_ASKPASS_HANDLE' ] ) { fatal ( "Missing 'THEIA_GIT_ASKPASS_HANDLE' handle." ) ; return ; } const handle = process . env [ 'THEIA_GIT_ASKPASS_HANDLE' ] as string ; const { host , hostname , port , protocol } = url . parse ( handle ) ; const gitRequest = argv [ 2 ] ; const gitHost = argv [ 4 ] . substring ( 1 , argv [ 4 ] . length - 2 ) ; const opts = { host , hostname , port , protocol , path : '/' , method : 'POST' } ; const req = http . request ( opts , res => { if ( res . statusCode !== 200 ) { fatal ( `Bad status code: ${ res . statusCode }.` ) ; return ; } const chunks = [ ] ; res . setEncoding ( 'utf8' ) ; res . on ( 'data' , ( d ) => chunks . push ( d ) ) ; res . on ( 'end' , ( ) => { const raw = chunks . join ( '' ) ; try { const result = JSON . parse ( raw ) ; process . stdout . write ( result ) ; } catch ( err ) { fatal ( 'Error parsing the response.' ) ; return ; } setTimeout ( ( ) => process . exit ( 0 ) , 0 ) ; } ) ; } ) ; req . on ( 'error' , err => fatal ( err ) ) ; req . write ( JSON . stringify ( { gitRequest , gitHost } ) ) ; req . end ( ) ; } main ( process . argv ) ;