@@ -30,34 +30,63 @@ function commandExistsSync(execName) {
3030 } ) ;
3131}
3232
33+ const encoder = new TextEncoder ( ) ;
34+
35+ function byteLength ( str ) {
36+ return encoder . encode ( str ) . byteLength ;
37+ }
38+
39+ // oxlint-disable-next-line max-lines-per-function
3340function waitForReady ( child , expectedResponse , signal ) {
3441 // oxlint-disable-next-line promise/avoid-new
3542 return new Promise ( ( resolve , reject ) => {
3643 const readlineStdout = readline . createInterface ( { input : child . stdout } ) ;
3744 const readlineStderr = readline . createInterface ( { input : child . stderr } ) ;
3845
3946 let recentOutput = "" ;
40- function recordOutput ( line ) {
41- recentOutput = `${ recentOutput } ${ line } \n` . slice ( - MAX_ERROR_BUFFER_BYTES ) ;
47+
48+ function recordOutput ( lineOutput ) {
49+ const safeLine =
50+ byteLength ( lineOutput ) > MAX_ERROR_BUFFER_BYTES / 2
51+ ? `${ lineOutput . slice ( 0 , MAX_ERROR_BUFFER_BYTES / 2 ) } …[truncated]`
52+ : lineOutput ;
53+
54+ recentOutput = `${ recentOutput } ${ safeLine } \n` ;
55+
56+ while ( byteLength ( recentOutput ) > MAX_ERROR_BUFFER_BYTES ) {
57+ const newline = recentOutput . indexOf ( "\n" ) ;
58+ if ( newline === - 1 ) {
59+ recentOutput = "" ;
60+ break ;
61+ }
62+ recentOutput = recentOutput . slice ( newline + 1 ) ;
63+ }
4264 }
4365
4466 function cleanup ( ) {
45- readlineStdout . removeAllListeners ( ) ;
46- readlineStdout . close ( ) ;
47- readlineStderr . removeAllListeners ( ) ;
48- readlineStderr . close ( ) ;
67+ readlineStdout . removeListener ( "line" , onLine ) ;
68+ readlineStderr . removeListener ( "line" , onErrLine ) ;
4969 child . removeListener ( "error" , onError ) ;
5070 child . removeListener ( "close" , onClose ) ;
5171 if ( signal ) {
5272 signal . removeEventListener ( "abort" , onAbort ) ;
5373 }
5474 }
5575
56- function onLine ( line ) {
57- console . log ( `[${ child . name } ] ${ line } ` ) ;
58- recordOutput ( line ) ;
59- if ( line . includes ( expectedResponse ) ) {
76+ function onLine ( lineOutput ) {
77+ console . log ( `[${ child . name } ] ${ lineOutput } ` ) ;
78+ recordOutput ( lineOutput ) ;
79+ if ( lineOutput . includes ( expectedResponse ) ) {
6080 cleanup ( ) ;
81+ readlineStdout . on ( "line" , ( line ) => {
82+ console . log ( `[${ child . name } ] ${ line } ` ) ;
83+ } ) ;
84+ readlineStderr . on ( "line" , ( line ) => {
85+ console . log ( `[${ child . name } ] ${ line } ` ) ;
86+ } ) ;
87+ child . once ( "close" , ( code ) => {
88+ console . log ( `[${ child . name } ] exited with code ${ code } ` ) ;
89+ } ) ;
6190 resolve ( child ) ;
6291 }
6392 }
0 commit comments