@@ -29,6 +29,14 @@ export interface ConnectionResult {
2929 errorCodes ?: ConnectionErrorCode [ ]
3030}
3131
32+ export type SSHError = {
33+ level : 'client-socket' | 'client-ssh'
34+ message ?: string
35+ description ?: string
36+ }
37+
38+ export type DisconnectedCallback = ( conn : IBMi , error ?: SSHError ) => Promise < void > ;
39+
3240const remoteApps = [ // All names MUST also be defined as key in 'remoteFeatures' below!!
3341 {
3442 path : `/usr/bin/` ,
@@ -45,11 +53,9 @@ const remoteApps = [ // All names MUST also be defined as key in 'remoteFeatures
4553 }
4654] ;
4755
48- type DisconnectCallback = ( conn : IBMi ) => Promise < void > ;
49-
5056interface ConnectionCallbacks {
5157 onConnectedOperations ?: Function [ ] ,
52- timeoutCallback ?: ( conn : IBMi ) => Promise < void > ,
58+ onDisconnected ?: DisconnectedCallback ,
5359 uiErrorHandler : ( connection : IBMi , error : ConnectionErrorCode , data ?: any ) => Promise < boolean > ,
5460 progress : ( detail : { message : string } ) => void ,
5561 message : ( type : ConnectionMessageType , message : string ) => void ,
@@ -116,6 +122,8 @@ export default class IBMi {
116122 private currentAsp : string | undefined ;
117123 private libraryAsps = new Map < string , number > ( ) ;
118124
125+ connectionSuccessful = false ;
126+
119127 /**
120128 * @deprecated Will be replaced with {@link IBMi.getAllIAsps} in v3.0.0
121129 */
@@ -147,15 +155,6 @@ export default class IBMi {
147155 process . stdout . write ( text ) ;
148156 } ;
149157
150- private disconnectedCallback : ( DisconnectCallback ) | undefined ;
151-
152- /**
153- * Will only be called once per connection.
154- */
155- setDisconnectedCallback ( callback : DisconnectCallback ) {
156- this . disconnectedCallback = callback ;
157- }
158-
159158 /**
160159 * getConfigFile can return pre-defined configuration files,
161160 * but can lazy load new configuration files as well.
@@ -336,19 +335,17 @@ export default class IBMi {
336335 } ;
337336 }
338337
339- if ( callbacks . timeoutCallback ) {
340- const timeoutCallbackWrapper = ( ) => {
341- // Don't call the callback function if it was based on a user cancellation request.
342- if ( ! wasCancelled ) {
343- callbacks . timeoutCallback ! ( this ) ;
344- }
338+ // Trigger callbacks unless the connection is cancelled
339+ const callbackWrapper = ( error ?: SSHError ) => {
340+ if ( ! wasCancelled ) {
341+ callbacks . onDisconnected ?.( this , error ) ;
345342 }
343+ } ;
346344
347- // Register handlers after we might have to abort due to bad configuration.
348- this . client . connection ! . once ( `timeout` , timeoutCallbackWrapper ) ;
349- this . client . connection ! . once ( `end` , timeoutCallbackWrapper ) ;
350- this . client . connection ! . once ( `error` , timeoutCallbackWrapper ) ;
351- }
345+ //end: disconnected by user
346+ this . client . connection . once ( `end` , callbackWrapper ) ;
347+ //error: connection dropped for some reason (details given in the SSHError type)
348+ this . client . connection . once ( `error` , callbackWrapper ) ;
352349
353350 callbacks . progress ( {
354351 message : `Checking home directory.`
@@ -956,8 +953,7 @@ export default class IBMi {
956953 }
957954
958955 if ( ! options . reconnecting ) {
959- const delayedOperations : Function [ ] = callbacks . onConnectedOperations ? [ ...callbacks . onConnectedOperations ] : [ ] ;
960- for ( const operation of delayedOperations ) {
956+ for ( const operation of callbacks . onConnectedOperations || [ ] ) {
961957 await operation ( ) ;
962958 }
963959 }
@@ -977,6 +973,8 @@ export default class IBMi {
977973 maximumArgsLength : this . maximumArgsLength
978974 } ) ;
979975
976+ this . connectionSuccessful = true ;
977+
980978 return {
981979 success : true
982980 } ;
@@ -1172,11 +1170,8 @@ export default class IBMi {
11721170 }
11731171
11741172 if ( this . client ) {
1173+ this . client . dispose ( ) ;
11751174 this . client = undefined ;
1176-
1177- if ( failedToConnect === false && this . disconnectedCallback ) {
1178- this . disconnectedCallback ( this ) ;
1179- }
11801175 }
11811176 }
11821177
0 commit comments