@@ -13,7 +13,9 @@ import {
1313 decompressBuffer ,
1414 parseRowsetChunks ,
1515 CMD_COMPRESSED ,
16- CMD_ROWSET_CHUNK
16+ CMD_ROWSET_CHUNK ,
17+ bufferEndsWith ,
18+ ROWSET_CHUNKS_END
1719} from '../drivers/protocol'
1820import type { Socket } from 'bun'
1921
@@ -125,9 +127,8 @@ export class SQLiteCloudBunConnection extends SQLiteCloudConnection {
125127
126128 // reset buffer and rowset chunks, define response callback
127129 this . buffer = Buffer . alloc ( 0 )
128- this . rowsetChunks = [ ]
129- this . processCallback = callback
130130 this . startedOn = new Date ( )
131+ this . processCallback = callback
131132
132133 // compose commands following SCPC protocol
133134 const formattedCommands = formatCommand ( commands )
@@ -144,9 +145,8 @@ export class SQLiteCloudBunConnection extends SQLiteCloudConnection {
144145 // onData is called when data is received, it will process the data until all data is retrieved for a response
145146 // when response is complete or there's an error, finish is called to call the results callback set by processCommands...
146147
147- // buffer to store incoming data
148+ // buffer to accumulate incoming data until an whole command is received and can be parsed
148149 private buffer : Buffer = Buffer . alloc ( 0 )
149- private rowsetChunks : Buffer [ ] = [ ]
150150 private startedOn : Date = new Date ( )
151151
152152 // callback to be called when a command is finished processing
@@ -155,14 +155,16 @@ export class SQLiteCloudBunConnection extends SQLiteCloudConnection {
155155 /** Handles data received in response to an outbound command sent by processCommands */
156156 private processCommandsData ( socket : Socket < any > , data : Buffer ) {
157157 try {
158- // on first ondata event, dataType is read from data, on subsequent ondata event, is read from buffer that is the concatanations of data received on each ondata event
159- let dataType = this . buffer . length === 0 ? data . subarray ( 0 , 1 ) . toString ( ) : this . buffer . subarray ( 0 , 1 ) . toString ( 'utf8' )
160- this . buffer = Buffer . concat ( [ this . buffer , data ] )
161- const commandLength = hasCommandLength ( dataType )
158+ // append data to buffer as it arrives
159+ if ( data . length && data . length > 0 ) {
160+ this . buffer = Buffer . concat ( [ this . buffer , data ] )
161+ }
162162
163- if ( commandLength ) {
163+ let dataType = this . buffer ?. subarray ( 0 , 1 ) . toString ( )
164+ if ( hasCommandLength ( dataType ) ) {
164165 const commandLength = parseCommandLength ( this . buffer )
165166 const hasReceivedEntireCommand = this . buffer . length - this . buffer . indexOf ( ' ' ) - 1 >= commandLength ? true : false
167+ console . debug ( `dataType: ${ dataType } buffer.length: ${ this . buffer . length } , commandLenght: ${ commandLength } ` )
166168 if ( hasReceivedEntireCommand ) {
167169 if ( this . config ?. verbose ) {
168170 let bufferString = this . buffer . toString ( 'utf8' )
@@ -182,18 +184,10 @@ export class SQLiteCloudBunConnection extends SQLiteCloudConnection {
182184 const { data } = popData ( this . buffer )
183185 this . processCommandsFinish ?. call ( this , null , data )
184186 } else {
185- // check if rowset received the ending chunk
186- if ( data . subarray ( data . indexOf ( ' ' ) + 1 , data . length ) . toString ( ) === '0 0 0 ' ) {
187- const parsedData = parseRowsetChunks ( this . rowsetChunks )
187+ // check if rowset received the ending chunk in which case it can be unpacked
188+ if ( bufferEndsWith ( this . buffer , ROWSET_CHUNKS_END ) ) {
189+ const parsedData = parseRowsetChunks ( [ this . buffer ] )
188190 this . processCommandsFinish ?. call ( this , null , parsedData )
189- } else {
190- // no ending string? ask server for another chunk
191- this . rowsetChunks . push ( this . buffer )
192- this . buffer = Buffer . alloc ( 0 )
193-
194- // no longer need to ack the server
195- // const okCommand = formatCommand('OK')
196- // this.socket?.write(okCommand)
197191 }
198192 }
199193 }
@@ -221,6 +215,7 @@ export class SQLiteCloudBunConnection extends SQLiteCloudConnection {
221215 // console.debug('BunTransport.finish - result', result)
222216 }
223217 if ( this . processCallback ) {
218+ // console.error(`SQLiteCloudBunConnection.processCommandsFinish - error:${error}, result: ${result}`, error, result)
224219 this . processCallback ( error , result )
225220 }
226221 }
0 commit comments