1616 */
1717import * as childProcess from 'child_process' ;
1818import { StdioOptions } from 'child_process' ;
19- import { Readable , Writable } from 'stream' ;
2019import * as readline from 'readline' ;
2120import * as Path from 'path' ;
2221import Log from '@secret-agent/commons/Logger' ;
2322import ILaunchedProcess from '@secret-agent/puppet-interfaces/ILaunchedProcess' ;
24- import { PipeTransport } from './PipeTransport' ;
23+ import Resolvable from '@secret-agent/commons/Resolvable' ;
24+ import { WebSocketTransport } from './WebSocketTransport' ;
2525
2626const { log } = Log ( module ) ;
2727
2828const logProcessExit = process . env . NODE_ENV !== 'test' ;
2929
30- export default function launchProcess (
30+ export default async function launchProcess (
3131 executablePath : string ,
3232 processArguments : string [ ] ,
3333 env : NodeJS . ProcessEnv ,
3434) : Promise < ILaunchedProcess > {
35- const stdio : StdioOptions = [ 'ignore' , 'pipe' , 'pipe' , 'pipe' , 'pipe' ] ;
35+ const stdio : StdioOptions = [ 'ignore' , 'pipe' , 'pipe' ] ;
3636
3737 log . info ( `Puppet.LaunchProcess` , { sessionId : null , executablePath, processArguments } ) ;
3838 const launchedProcess = childProcess . spawn ( executablePath , processArguments , {
@@ -60,11 +60,16 @@ export default function launchProcess(
6060
6161 const stdout = readline . createInterface ( { input : launchedProcess . stdout } ) ;
6262 stdout . on ( 'line' , line => {
63- log . stats ( `${ exe } .stdout` , { message : line , sessionId : null } ) ;
63+ if ( line ) log . stats ( `${ exe } .stdout` , { message : line , sessionId : null } ) ;
6464 } ) ;
6565
66+ const websocketEndpointResolvable = new Resolvable < string > ( ) ;
6667 const stderr = readline . createInterface ( { input : launchedProcess . stderr } ) ;
6768 stderr . on ( 'line' , line => {
69+ if ( ! line ) return ;
70+ const match = line . match ( / D e v T o o l s l i s t e n i n g o n ( .* ) / ) ;
71+ if ( match ) websocketEndpointResolvable . resolve ( match [ 1 ] . trim ( ) ) ;
72+
6873 log . warn ( `${ exe } .stderr` , { message : line , sessionId : null } ) ;
6974 } ) ;
7075
@@ -76,10 +81,9 @@ export default function launchProcess(
7681 }
7782 } ) ;
7883
79- const transport = new PipeTransport (
80- launchedProcess . stdio [ 3 ] as Writable ,
81- launchedProcess . stdio [ 4 ] as Readable ,
82- ) ;
84+ const wsEndpoint = await websocketEndpointResolvable . promise ;
85+ const transport = new WebSocketTransport ( wsEndpoint ) ;
86+ await transport . waitForOpen ;
8387
8488 return Promise . resolve ( < ILaunchedProcess > {
8589 transport,
@@ -89,6 +93,11 @@ export default function launchProcess(
8993 function close ( ) : Promise < void > {
9094 if ( launchedProcess . killed || processKilled ) return ;
9195
96+ try {
97+ transport . close ( ) ;
98+ } catch ( error ) {
99+ // drown
100+ }
92101 try {
93102 const closed = new Promise < void > ( resolve => launchedProcess . once ( 'exit' , resolve ) ) ;
94103 if ( process . platform === 'win32' ) {
0 commit comments