@@ -228,7 +228,7 @@ export class SQLiteCloudConnection {
228228
229229 let buffer = Buffer . alloc ( 0 )
230230 const rowsetChunks : Buffer [ ] = [ ]
231- this . log ( `Sending : ${ commands } ` )
231+ this . log ( `Send : ${ commands } ` )
232232
233233 // define what to do if an answer does not arrive within the set timeout
234234 let socketTimeout : number
@@ -251,31 +251,30 @@ export class SQLiteCloudConnection {
251251
252252 // define the Promise that waits for the server response
253253 const readData = ( data : Uint8Array ) => {
254- this . log ( `Received: ${ data . length > 100 ? data . toString ( ) . substring ( 0 , 100 ) + '...' : data . toString ( ) } ` )
255254 try {
256255 // 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
257- const dataType = buffer . length === 0 ? data . subarray ( 0 , 1 ) . toString ( ) : buffer . subarray ( 0 , 1 ) . toString ( 'utf8' )
256+ let dataType = buffer . length === 0 ? data . subarray ( 0 , 1 ) . toString ( ) : buffer . subarray ( 0 , 1 ) . toString ( 'utf8' )
258257 buffer = Buffer . concat ( [ buffer , data ] )
259258 const commandLength = hasCommandLength ( dataType )
260259
261260 if ( commandLength ) {
262261 const commandLength = parseCommandLength ( buffer )
263-
264- // in case of compressed data, extract the dataType of compressed data
265- let compressedDataType = null
266- if ( dataType === CMD_COMPRESSED ) {
267- // remove LEN
268- let compressedBuffer = buffer . subarray ( buffer . indexOf ( ' ' ) + 1 , buffer . length )
269- // remove compressed size
270- compressedBuffer = compressedBuffer . subarray ( compressedBuffer . indexOf ( ' ' ) + 1 , compressedBuffer . length )
271- // remove decompressed size
272- compressedBuffer = compressedBuffer . subarray ( compressedBuffer . indexOf ( ' ' ) + 1 , compressedBuffer . length )
273- compressedDataType = compressedBuffer . subarray ( 0 , 1 ) . toString ( 'utf8' )
274- }
275-
276262 const hasReceivedEntireCommand = buffer . length - buffer . indexOf ( ' ' ) - 1 >= commandLength ? true : false
277263 if ( hasReceivedEntireCommand ) {
278- if ( dataType !== CMD_ROWSET_CHUNK && compressedDataType !== CMD_ROWSET_CHUNK ) {
264+ if ( this . config . verbose ) {
265+ let bufferString = buffer . toString ( 'utf8' )
266+ if ( bufferString . length > 1000 ) {
267+ bufferString = bufferString . substring ( 0 , 100 ) + '...' + bufferString . substring ( bufferString . length - 40 )
268+ }
269+ this . log ( `Receive: ${ bufferString } ` )
270+ }
271+
272+ // need to decompress this buffer before decoding?
273+ if ( dataType === CMD_COMPRESSED ) {
274+ ; ( { buffer, dataType } = decompressBuffer ( buffer ) )
275+ }
276+
277+ if ( dataType !== CMD_ROWSET_CHUNK ) {
279278 this . socket ?. off ( 'data' , readData )
280279 const { data } = popData ( buffer )
281280 finish ( null , data )
@@ -289,7 +288,9 @@ export class SQLiteCloudConnection {
289288 // no ending string? ask server for another chunk
290289 rowsetChunks . push ( buffer )
291290 buffer = Buffer . alloc ( 0 )
292- this . socket ?. write ( formatCommand ( 'OK' ) )
291+ const okCommand = formatCommand ( 'OK' )
292+ this . log ( `Send: ${ okCommand } ` )
293+ this . socket ?. write ( okCommand )
293294 }
294295 }
295296 }
@@ -403,20 +404,21 @@ function parseCommandLength(data: Buffer) {
403404/** Receive a compressed buffer, decompress with lz4, return buffer and datatype */
404405function decompressBuffer ( buffer : Buffer ) : { buffer : Buffer ; dataType : string } {
405406 const spaceIndex = buffer . indexOf ( ' ' )
406- buffer = buffer . subarray ( spaceIndex + 1 , buffer . length )
407+ buffer = buffer . subarray ( spaceIndex + 1 )
407408
408409 // extract compressed size
409410 const compressedSize = parseInt ( buffer . subarray ( 0 , buffer . indexOf ( ' ' ) + 1 ) . toString ( 'utf8' ) )
410- buffer = buffer . subarray ( buffer . indexOf ( ' ' ) + 1 , buffer . length )
411+ buffer = buffer . subarray ( buffer . indexOf ( ' ' ) + 1 )
411412
412413 // extract decompressed size
413414 const decompressedSize = parseInt ( buffer . subarray ( 0 , buffer . indexOf ( ' ' ) + 1 ) . toString ( 'utf8' ) )
414- buffer = buffer . subarray ( buffer . indexOf ( ' ' ) + 1 , buffer . length )
415+ buffer = buffer . subarray ( buffer . indexOf ( ' ' ) + 1 )
415416
416417 // extract compressed dataType
417418 const dataType = buffer . subarray ( 0 , 1 ) . toString ( 'utf8' )
418419 const decompressedBuffer = Buffer . alloc ( decompressedSize )
419- const decompressionResult = lz4 . decodeBlock ( buffer . subarray ( buffer . length - compressedSize , buffer . length ) , decompressedBuffer )
420+ const compressedBuffer = buffer . subarray ( buffer . length - compressedSize )
421+ const decompressionResult = lz4 . decompressBlock ( compressedBuffer , decompressedBuffer , 0 , compressedSize , 0 )
420422 buffer = Buffer . concat ( [ buffer . subarray ( 0 , buffer . length - compressedSize ) , decompressedBuffer ] )
421423 if ( decompressionResult <= 0 || decompressionResult !== decompressedSize ) {
422424 throw new Error ( `lz4 decompression error at offset ${ decompressionResult } ` )
@@ -548,7 +550,11 @@ function parseRowsetChunks(buffers: Buffer[]) {
548550 const data = [ ]
549551
550552 for ( let i = 0 ; i < buffers . length ; i ++ ) {
551- let buffer = buffers [ i ]
553+ let buffer : Buffer = buffers [ i ]
554+
555+ // validate and skip data type
556+ const dataType = buffer . subarray ( 0 , 1 ) . toString ( )
557+ console . assert ( dataType === CMD_ROWSET_CHUNK )
552558 buffer = buffer . subarray ( buffer . indexOf ( ' ' ) + 1 )
553559
554560 // chunk header, eg: 0:VERS NROWS NCOLS
@@ -595,11 +601,8 @@ function popData(buffer: Buffer): { data: SQLiteCloudDataTypes | SQLiteCloudRows
595601 // first character is the data type
596602 console . assert ( buffer && buffer instanceof Buffer )
597603 let dataType : string = buffer . subarray ( 0 , 1 ) . toString ( 'utf8' )
598-
599- // need to decompress this buffer before decoding?
600- if ( dataType === CMD_COMPRESSED ) {
601- ; ( { buffer, dataType } = decompressBuffer ( buffer ) )
602- }
604+ console . assert ( dataType !== CMD_COMPRESSED , "Compressed data shouldn't be decompressed before parsing" )
605+ console . assert ( dataType !== CMD_ROWSET_CHUNK , 'Chunked data should be parsed by parseRowsetChunks' )
603606
604607 let spaceIndex = buffer . indexOf ( ' ' )
605608 if ( spaceIndex === - 1 ) {
@@ -635,9 +638,6 @@ function popData(buffer: Buffer): { data: SQLiteCloudDataTypes | SQLiteCloudRows
635638 return popResults ( parseArray ( buffer , spaceIndex ) )
636639 case CMD_ROWSET :
637640 return popResults ( parseRowset ( buffer , spaceIndex ) )
638- case CMD_ROWSET_CHUNK :
639- console . assert ( false )
640- break
641641 case CMD_ERROR :
642642 parseError ( buffer , spaceIndex ) // throws custom error
643643 break
0 commit comments