22import { ObjLogger } from "@scramjet/obj-logger" ;
33import { CommunicationChannel as CC } from "@scramjet/symbols" ;
44import { IHostClient , IObjectLogger , UpstreamStreamsConfig , } from "@scramjet/types" ;
5+ import { defer } from "@scramjet/utility" ;
56import { Agent } from "http" ;
67import net , { Socket , createConnection } from "net" ;
8+ import { PassThrough } from "stream" ;
79
810type HostOpenConnections = [
911 net . Socket , net . Socket , net . Socket , net . Socket , net . Socket , net . Socket , net . Socket , net . Socket , net . Socket
@@ -42,20 +44,24 @@ class HostClient implements IHostClient {
4244 async init ( id : string ) : Promise < void > {
4345 const openConnections = await Promise . all (
4446 Array . from ( Array ( 9 ) )
45- . map ( ( ) => {
47+ . map ( ( _e : any , i : number ) => {
4648 // Error handling for each connection is process crash for now
4749 let connection : Socket ;
4850
4951 try {
5052 connection = net . createConnection ( this . instancesServerPort , this . instancesServerHost ) ;
51- connection . on ( "error" , ( ) => { } ) ;
53+ connection . on ( "error" , ( ) => {
54+ this . logger . warn ( `${ i } Stream error` ) ;
55+ } ) ;
5256 connection . setNoDelay ( true ) ;
5357 } catch ( e ) {
5458 return Promise . reject ( e ) ;
5559 }
5660
5761 return new Promise < net . Socket > ( res => {
58- connection . on ( "connect" , ( ) => res ( connection ) ) ;
62+ connection . on ( "connect" , ( ) => {
63+ res ( connection ) ;
64+ } ) ;
5965 } ) ;
6066 } )
6167 . map ( ( connPromised , index ) => {
@@ -74,6 +80,26 @@ class HostClient implements IHostClient {
7480
7581 this . _streams = openConnections as HostOpenConnections ;
7682
83+ const input = this . _streams [ CC . IN ] ;
84+
85+ const inputTarget = new PassThrough ( { emitClose : false } ) ;
86+
87+ input . on ( "end" , async ( ) => {
88+ await defer ( 500 ) ;
89+
90+ if ( ( this . _streams ! [ CC . CONTROL ] as net . Socket ) . readableEnded ) {
91+ this . logger . info ( "Input end. Control is also ended... We are disconnected." ) ;
92+ } else {
93+ this . logger . info ( "Input end. Control not ended. We are online. Desired input end." ) ;
94+ inputTarget . end ( ) ;
95+ }
96+ } ) ;
97+
98+ input . pipe ( inputTarget , { end : false } ) ;
99+
100+ this . _streams [ CC . IN ] = inputTarget ;
101+ //this._streams[CC.STDIN] = this._streams[CC.STDIN].pipe(new PassThrough({ emitClose: false }), { end: false });
102+
77103 try {
78104 this . bpmux = new BPMux ( this . _streams [ CC . PACKAGE ] ) ;
79105 } catch ( e ) {
@@ -118,6 +144,11 @@ class HostClient implements IHostClient {
118144 const streamsExitedPromised : Promise < void > [ ] = this . streams . map ( ( stream , i ) =>
119145 new Promise (
120146 ( res ) => {
147+ if ( [ CC . IN , CC . STDIN , CC . CONTROL ] . includes ( i ) ) {
148+ res ( ) ;
149+ return ;
150+ }
151+
121152 if ( ! hard && "writable" in stream ! ) {
122153 stream
123154 . on ( "error" , ( e ) => {
0 commit comments