@@ -162,37 +162,45 @@ export class SQLiteCloudConnection {
162162 }
163163
164164 this . operations . enqueue ( done => {
165+ // clear all listeners and call done in the operations queue
166+ const finish : ResultsCallback = ( error , result ) => {
167+ if ( this . socket ) {
168+ this . socket . removeAllListeners ( 'data' )
169+ this . socket . removeAllListeners ( 'error' )
170+ this . socket . removeAllListeners ( 'close' )
171+ }
172+ if ( callback ) {
173+ callback ?. call ( this , error )
174+ callback = undefined
175+ }
176+ if ( error ) {
177+ this . close ( )
178+ }
179+ // process next operation in the queue
180+ done ?. call ( this , error ? error : undefined )
181+ }
182+
165183 // connect to tls socket, initialize connection, setup event handlers
166184 const client : tls . TLSSocket = tls . connect ( this . config . port as number , this . config . host , this . config . tlsOptions , ( ) => {
167185 if ( ! client . authorized ) {
168186 const anonimizedError = anonimizeError ( client . authorizationError )
169187 this . log ( 'Connection was not authorized' , anonimizedError )
170188 this . close ( )
171- const error = new SQLiteCloudError ( 'Connection was not authorized' , { cause : anonimizedError } )
172- callback ?. call ( this , error )
173- done ?. call ( this , error )
189+ finish ( new SQLiteCloudError ( 'Connection was not authorized' , { cause : anonimizedError } ) )
174190 } else {
175191 this . socket = client
176- callback ?. call ( this , null )
177- done ?. call ( this )
192+ finish ( null )
178193 }
179194 } )
180195
181196 client . on ( 'close' , ( ) => {
182- if ( this . socket ) {
183- // no loggin if already disposed
184- this . log ( 'Connection closed' )
185- this . socket . destroy ( )
186- this . socket = undefined
187- }
197+ this . log ( 'Connection closed' )
198+ finish ( new SQLiteCloudError ( 'Connection was closed' ) )
188199 } )
189200
190201 client . once ( 'error' , ( error : any ) => {
191- error = new SQLiteCloudError ( 'Connection error' , { cause : error } )
192202 this . log ( 'Connection error' , error )
193- this . close ( )
194- callback ?. call ( this , error )
195- done ?. call ( this , error )
203+ finish ( new SQLiteCloudError ( 'Connection error' , { cause : error } ) )
196204 } )
197205 } )
198206
@@ -225,13 +233,19 @@ export class SQLiteCloudConnection {
225233 let socketTimeout : number
226234
227235 // clear all listeners and call done in the operations queue
228- const finish = ( error ?: Error ) => {
236+ const finish : ResultsCallback = ( error , result ) => {
229237 clearTimeout ( socketTimeout )
230238 if ( this . socket ) {
231239 this . socket . removeAllListeners ( 'data' )
232240 this . socket . removeAllListeners ( 'error' )
241+ this . socket . removeAllListeners ( 'close' )
233242 }
234- done ?. call ( this , error )
243+ if ( callback ) {
244+ callback ?. call ( this , error , result )
245+ callback = undefined
246+ }
247+ // process next operation in the queue
248+ done ?. call ( this , error ? error : undefined )
235249 }
236250
237251 // define the Promise that waits for the server response
@@ -263,15 +277,13 @@ export class SQLiteCloudConnection {
263277 if ( dataType !== CMD_ROWSET_CHUNK && compressedDataType !== CMD_ROWSET_CHUNK ) {
264278 this . socket ?. off ( 'data' , readData )
265279 const { data } = popData ( buffer )
266- callback ?. call ( this , null , data )
267- finish ?. call ( this )
280+ finish ( null , data )
268281 } else {
269282 // @ts -expect-error
270283 // check if rowset received the ending chunk
271284 if ( data . subarray ( data . indexOf ( ' ' ) + 1 , data . length ) . toString ( ) === '0 0 0 ' ) {
272285 const parsedData = parseRowsetChunks ( rowsetChunks )
273- callback ?. call ( this , null , parsedData )
274- finish ?. call ( this )
286+ finish ?. call ( this , null , parsedData )
275287 } else {
276288 // no ending string? ask server for another chunk
277289 rowsetChunks . push ( buffer )
@@ -285,28 +297,32 @@ export class SQLiteCloudConnection {
285297 const lastChar = buffer . subarray ( buffer . length - 1 , buffer . length ) . toString ( 'utf8' )
286298 if ( lastChar == ' ' ) {
287299 const { data } = popData ( buffer )
288- callback ?. call ( this , null , data )
289- finish ?. call ( this )
300+ finish ( null , data )
290301 }
291302 }
292303 } catch ( error ) {
293- this . close ( )
294- callback ?. call ( this , error as Error )
295- finish ?. call ( this , error as Error )
304+ console . assert ( error instanceof Error )
305+ if ( error instanceof Error ) {
306+ finish ( error )
307+ }
296308 }
297309 }
298310
311+ this . socket ?. once ( 'close' , ( ) => {
312+ finish ( new SQLiteCloudError ( 'Connection was closed' , { cause : anonimizeCommand ( commands ) } ) )
313+ } )
314+
299315 this . socket ?. write ( commands , 'utf8' , ( ) => {
300316 socketTimeout = setTimeout ( ( ) => {
301- finish ?. call ( this , new SQLiteCloudError ( 'Request timed out' , { cause : anonimizeCommand ( commands ) } ) )
317+ finish ( new SQLiteCloudError ( 'Request timed out' , { cause : anonimizeCommand ( commands ) } ) )
302318 } , this . config . timeout )
303319 this . socket ?. on ( 'data' , readData )
304320 } )
305321
306322 this . socket ?. once ( 'error' , ( error : any ) => {
307323 this . log ( 'Socket error' , error )
308324 this . close ( )
309- finish ?. call ( this , new SQLiteCloudError ( 'Socket error' , { cause : anonimizeError ( error ) } ) )
325+ finish ( new SQLiteCloudError ( 'Socket error' , { cause : anonimizeError ( error ) } ) )
310326 } )
311327 } )
312328
0 commit comments