11import { BindingValue } from "@ibm/mapepire-js" ;
22import * as node_ssh from "node-ssh" ;
33import path , { parse as parsePath } from 'path' ;
4+ import { ClientErrorExtensions } from "ssh2" ;
45import { EventEmitter } from 'stream' ;
56import { CompileTools } from "./CompileTools" ;
67import IBMiContent from "./IBMiContent" ;
@@ -30,6 +31,8 @@ export interface ConnectionResult {
3031 errorCodes ?: ConnectionErrorCode [ ]
3132}
3233
34+ export type DisconnectedCallback = ( conn : IBMi , error ?: Error & ClientErrorExtensions ) => Promise < void > ;
35+
3336const remoteApps = [ // All names MUST also be defined as key in 'remoteFeatures' below!!
3437 {
3538 path : `/usr/bin/` ,
@@ -46,11 +49,9 @@ const remoteApps = [ // All names MUST also be defined as key in 'remoteFeatures
4649 }
4750] ;
4851
49- type DisconnectCallback = ( conn : IBMi ) => Promise < void > ;
50-
5152interface ConnectionCallbacks {
5253 onConnectedOperations ?: Function [ ] ,
53- timeoutCallback ?: ( conn : IBMi ) => Promise < void > ,
54+ onDisconnected ?: DisconnectedCallback ,
5455 uiErrorHandler : ( connection : IBMi , error : ConnectionErrorCode , data ?: any ) => Promise < boolean > ,
5556 progress : ( detail : { message : string } ) => void ,
5657 message : ( type : ConnectionMessageType , message : string ) => void ,
@@ -117,6 +118,8 @@ export default class IBMi {
117118 private currentAsp : string | undefined ;
118119 private libraryAsps = new Map < string , number > ( ) ;
119120
121+ connectionSuccessful = false ;
122+
120123 /**
121124 * @deprecated Will be replaced with {@link IBMi.getAllIAsps} in v3.0.0
122125 */
@@ -148,15 +151,6 @@ export default class IBMi {
148151 process . stdout . write ( text ) ;
149152 } ;
150153
151- private disconnectedCallback : ( DisconnectCallback ) | undefined ;
152-
153- /**
154- * Will only be called once per connection.
155- */
156- setDisconnectedCallback ( callback : DisconnectCallback ) {
157- this . disconnectedCallback = callback ;
158- }
159-
160154 /**
161155 * getConfigFile can return pre-defined configuration files,
162156 * but can lazy load new configuration files as well.
@@ -268,7 +262,7 @@ export default class IBMi {
268262 if ( callbacks . cancelEmitter ) {
269263 callbacks . cancelEmitter . once ( 'cancel' , ( ) => {
270264 wasCancelled = true ;
271- this . dispose ( ) ;
265+ this . disconnect ( ) ;
272266 } ) ;
273267 }
274268
@@ -297,7 +291,8 @@ export default class IBMi {
297291 privateKeyPath : connectionObject . privateKeyPath ? Tools . resolvePath ( connectionObject . privateKeyPath ) : undefined ,
298292 passphrase : connectionObject . privateKeyPath ? connectionObject . passphrase : undefined ,
299293 debug : connectionObject . sshDebug ? ( message : string ) => this . appendOutput ( `\n[SSH debug] ${ message } ` ) : undefined
300- } as node_ssh . Config ) ;
294+ } ) ;
295+ this . connectionSuccessful = true ;
301296
302297 this . currentConnectionName = connectionObject . name ;
303298 this . currentHost = connectionObject . host ;
@@ -338,18 +333,18 @@ export default class IBMi {
338333 } ;
339334 }
340335
341- if ( callbacks . timeoutCallback ) {
342- const timeoutCallbackWrapper = ( ) => {
343- // Don't call the callback function if it was based on a user cancellation request.
344- if ( ! wasCancelled ) {
345- callbacks . timeoutCallback ! ( this ) ;
346- }
347- }
336+ // Trigger callbacks unless the connection is cancelled
337+ const onDisconnected = async ( error ?: Error & ClientErrorExtensions ) => {
338+ await this . dispose ( ) ;
339+ callbacks . onDisconnected ?.( this , error ) ;
340+ } ;
348341
349- // Register handlers after we might have to abort due to bad configuration.
350- this . client . connection ! . once ( `timeout` , timeoutCallbackWrapper ) ;
351- this . client . connection ! . once ( `end` , timeoutCallbackWrapper ) ;
352- this . client . connection ! . once ( `error` , timeoutCallbackWrapper ) ;
342+ if ( this . client . connection ) {
343+ //end: Disconnected by the user
344+ this . client . connection . once ( `end` , onDisconnected ) ;
345+ //error/tiemout: connection dropped for some reason (details given in the SSHError type)
346+ this . client . connection . once ( `error` , onDisconnected ) ;
347+ this . client . connection . once ( `timeout` , onDisconnected ) ;
353348 }
354349
355350 callbacks . progress ( {
@@ -958,8 +953,7 @@ export default class IBMi {
958953 }
959954
960955 if ( ! options . reconnecting ) {
961- const delayedOperations : Function [ ] = callbacks . onConnectedOperations ? [ ...callbacks . onConnectedOperations ] : [ ] ;
962- for ( const operation of delayedOperations ) {
956+ for ( const operation of callbacks . onConnectedOperations || [ ] ) {
963957 await operation ( ) ;
964958 }
965959 }
@@ -984,7 +978,7 @@ export default class IBMi {
984978 } ;
985979
986980 } catch ( e : any ) {
987- this . disconnect ( true ) ;
981+ this . disconnect ( ) ;
988982
989983 let error = e . message ;
990984 if ( wasCancelled ) {
@@ -1166,24 +1160,27 @@ export default class IBMi {
11661160 } ;
11671161 }
11681162
1169- private disconnect ( failedToConnect = false ) {
1170- if ( this . sqlJob ) {
1171- this . sqlJob . close ( ) ;
1172- this . sqlJob = undefined ;
1173- this . splfUserData = undefined ;
1163+ disconnect ( ) {
1164+ if ( this . client ?. connection ) {
1165+ //Close the connection and triggers its 'end' event
1166+ this . client . dispose ( ) ;
11741167 }
1175-
1176- if ( this . client ) {
1177- this . client = undefined ;
1178-
1179- if ( failedToConnect === false && this . disconnectedCallback ) {
1180- this . disconnectedCallback ( this ) ;
1181- }
1168+ else {
1169+ //There is no connection: dispose directly
1170+ this . dispose ( ) ;
11821171 }
11831172 }
11841173
1185- async dispose ( ) {
1186- this . disconnect ( ) ;
1174+ private async dispose ( ) {
1175+ CompileTools . reset ( ) ;
1176+
1177+ //Clear connected resources
1178+ if ( this . sqlJob ) {
1179+ delete this . sqlJob ;
1180+ delete this . splfUserData ;
1181+ }
1182+ await this . getComponent < Mapepire > ( Mapepire . ID ) ?. endJobs ( ) ;
1183+ delete this . client ;
11871184 }
11881185
11891186 /**
0 commit comments