@@ -124,13 +124,11 @@ class FileTransferClient {
124124 } else if ( this . _command == MOVE_STATUS ) {
125125 this . _command = await this . processMoveStatus ( new DataView ( this . _buffer . buffer , 0 , this . _offset ) ) ;
126126 } else {
127- console . log ( "Unknown Command : " + this . _command ) ;
127+ console . log ( "Unknown Command: " + this . _command ) ;
128128 }
129129 if ( this . _command != THIS_COMMAND ) {
130130 //reset buffer
131131 this . _offset = 0 ;
132- } else {
133- //wait for more
134132 }
135133 }
136134
@@ -198,13 +196,12 @@ class FileTransferClient {
198196 let freeSpace = payload . getUint32 ( 16 , true ) ;
199197 if ( status != STATUS_OK ) {
200198 if ( status == STATUS_ERROR_USB_MOUNTED ) {
201- console . log ( "Unable to write while USB connected" ) ;
199+ this . _reject ( "Unable to write while USB connected" ) ;
202200 } else if ( status == STATUS_ERROR ) {
203- console . log ( "Invalid Path" ) ;
201+ this . _reject ( "Invalid Path" ) ;
204202 } else {
205- console . log ( "Unknown Status" , status ) ;
203+ this . _reject ( "Unknown Status: " + status ) ;
206204 }
207- this . _reject ( status ) ;
208205 this . _reject = null ;
209206 this . _resolve = null ;
210207 return ANY_COMMAND ;
@@ -237,13 +234,12 @@ class FileTransferClient {
237234 let chunkLength = payload . getUint32 ( 12 , true ) ;
238235 if ( status != STATUS_OK ) {
239236 if ( status == STATUS_ERROR_USB_MOUNTED ) {
240- console . log ( "Unable to read while USB connected" ) ;
237+ this . _reject ( "Unable to read while USB connected" ) ;
241238 } else if ( status == STATUS_ERROR ) {
242- console . log ( "Invalid Path" ) ;
239+ this . _reject ( "Invalid Path" ) ;
243240 } else {
244- console . log ( "Unknown Status" , status ) ;
241+ this . _reject ( "Unknown Status: " + status ) ;
245242 }
246- this . _reject ( status ) ;
247243 this . _resolve = null ;
248244 this . _reject = null ;
249245 this . _incomingFile = null ;
@@ -286,23 +282,19 @@ class FileTransferClient {
286282 let b = this . _buffer . buffer ;
287283 const headerSize = 28 ;
288284 let cmd , path ;
285+ let flags , modificationTime , fileSize ;
289286 let status = payload . getUint8 ( 1 ) ;
290287 let pathLength = payload . getUint16 ( 2 , true ) ;
291288 let i = payload . getUint32 ( 4 , true ) ;
292289 let totalItems = payload . getUint32 ( 8 , true ) ;
293- let flags = payload . getUint32 ( 12 , true ) ;
294- let modificationTime = payload . getBigUint64 ( 16 , true ) ;
295- let fileSize = payload . getUint32 ( 24 , true ) ;
296-
297290 if ( status != STATUS_OK ) {
298291 if ( status == STATUS_ERROR_USB_MOUNTED ) {
299- console . log ( "Unable to read while USB connected" ) ;
292+ this . _reject ( "Unable to read while USB connected" ) ;
300293 } else if ( status == STATUS_ERROR ) {
301- console . log ( "Invalid Path" ) ;
294+ this . _reject ( "Invalid Path" ) ;
302295 } else {
303- console . log ( "Unknown Status" , status ) ;
296+ this . _reject ( "Unknown Status: " + status ) ;
304297 }
305- this . _reject ( status ) ;
306298 this . _resolve = null ;
307299 this . _reject = null ;
308300 return ANY_COMMAND ;
@@ -314,28 +306,21 @@ class FileTransferClient {
314306 if ( offset + headerSize + pathLength > payload . byteLength ) {
315307 break ;
316308 }
317- cmd = payload . getUint8 ( offset + 0 ) ;
318- status = payload . getUint8 ( offset + 1 ) ;
319309 pathLength = payload . getUint16 ( offset + 2 , true ) ;
320310 i = payload . getUint32 ( offset + 4 , true ) ;
321311 totalItems = payload . getUint32 ( offset + 8 , true ) ;
322- flags = payload . getUint32 ( offset + 12 , true ) ;
323- modificationTime = payload . getBigUint64 ( offset + 16 , true ) ;
324- fileSize = payload . getUint32 ( offset + 24 , true ) ;
325312 offset += headerSize + pathLength ;
326313 }
327314
328- if ( i < totalItems - 1 ) {
315+ // Check if we have all items and all expected data for last item
316+ if ( i < totalItems - 1 || payload . byteLength < offset + headerSize ) {
329317 // need more
330318 return THIS_COMMAND ;
331319 }
332- // full payload
333320
321+ // full payload, now process it
334322 offset = 0 ;
335323 while ( offset < payload . byteLength ) {
336- if ( offset + headerSize > payload . byteLength ) {
337- break ;
338- }
339324 cmd = payload . getUint8 ( offset + 0 ) ;
340325 status = payload . getUint8 ( offset + 1 ) ;
341326 pathLength = payload . getUint16 ( offset + 2 , true ) ;
@@ -344,7 +329,6 @@ class FileTransferClient {
344329 flags = payload . getUint32 ( offset + 12 , true ) ;
345330 modificationTime = payload . getBigUint64 ( offset + 16 , true ) ;
346331 fileSize = payload . getUint32 ( offset + 24 , true ) ;
347-
348332 if ( cmd != LISTDIR_ENTRY ) {
349333 throw new ProtocolError ( ) ;
350334 }
@@ -383,13 +367,12 @@ class FileTransferClient {
383367
384368 if ( status != STATUS_OK ) {
385369 if ( status == STATUS_ERROR_USB_MOUNTED ) {
386- console . log ( "Unable to write while USB connected" ) ;
370+ this . _reject ( "Unable to write while USB connected" ) ;
387371 } else if ( status == STATUS_ERROR ) {
388- console . log ( "Invalid Path" ) ;
372+ this . _reject ( "Invalid Path" ) ;
389373 } else {
390- console . log ( "Unknown Status" , status ) ;
374+ this . _reject ( "Unknown Status: " + status ) ;
391375 }
392- this . _reject ( status ) ;
393376 } else {
394377 this . _resolve ( true ) ;
395378 }
@@ -409,13 +392,12 @@ class FileTransferClient {
409392 let status = payload . getUint8 ( 1 ) ;
410393 if ( status != STATUS_OK ) {
411394 if ( status == STATUS_ERROR_USB_MOUNTED ) {
412- console . log ( "Unable to write while USB connected" ) ;
395+ this . _reject ( "Unable to write while USB connected" ) ;
413396 } else if ( status == STATUS_ERROR ) {
414- console . log ( "File or Folder not found" ) ;
397+ this . _reject ( "File or Folder not found" ) ;
415398 } else {
416- console . log ( "Unknown Status" , status ) ;
399+ this . _reject ( "Unknown Status: " + status ) ;
417400 }
418- this . _reject ( status ) ;
419401 } else {
420402 this . _resolve ( true ) ;
421403 }
@@ -435,13 +417,12 @@ class FileTransferClient {
435417 let status = payload . getUint8 ( 1 ) ;
436418 if ( status != STATUS_OK ) {
437419 if ( status == STATUS_ERROR_USB_MOUNTED ) {
438- console . log ( "Unable to write while USB connected" ) ;
420+ this . _reject ( "Unable to write while USB connected" ) ;
439421 } else if ( status == STATUS_ERROR ) {
440- console . log ( "Unable to move file" ) ;
422+ this . _reject ( "Unable to move file" ) ;
441423 } else {
442- console . log ( "Unknown Status" , status ) ;
424+ this . _reject ( "Unknown Status: " + status ) ;
443425 }
444- this . _reject ( status ) ;
445426 } else {
446427 this . _resolve ( true ) ;
447428 }
@@ -454,7 +435,7 @@ class FileTransferClient {
454435 async makeDir ( path , modificationTime ) {
455436 await this . checkConnection ( ) ;
456437 if ( modificationTime === undefined ) {
457- modificationTime = Date . now ( )
438+ modificationTime = Date . now ( ) ;
458439 }
459440 let encoded = new TextEncoder ( ) . encode ( path ) ;
460441 var header = new ArrayBuffer ( 16 ) ;
@@ -477,9 +458,6 @@ class FileTransferClient {
477458 // Returns a list of tuples, one tuple for each file or directory in the given path
478459 async listDir ( path ) {
479460 await this . checkConnection ( ) ;
480-
481- this . _incomingOffset = 0 ;
482-
483461 let encoded = new TextEncoder ( ) . encode ( path ) ;
484462 var header = new ArrayBuffer ( 4 ) ;
485463 var view = new DataView ( header ) ;
0 commit comments