@@ -536,6 +536,7 @@ async function readFileHandle(filehandle, options) {
536
536
throw new ERR_FS_FILE_TOO_LARGE ( size ) ;
537
537
538
538
let totalRead = 0 ;
539
+ const noSize = size === 0 ;
539
540
let buffer = Buffer . allocUnsafeSlow ( length ) ;
540
541
let result = '' ;
541
542
let offset = 0 ;
@@ -558,7 +559,7 @@ async function readFileHandle(filehandle, options) {
558
559
559
560
if ( bytesRead === 0 ||
560
561
totalRead === size ||
561
- ( bytesRead !== buffer . length && ! chunkedRead ) ) {
562
+ ( bytesRead !== buffer . length && ! chunkedRead && ! noSize ) ) {
562
563
const singleRead = bytesRead === totalRead ;
563
564
564
565
const bytesToCheck = chunkedRead ? totalRead : bytesRead ;
@@ -568,7 +569,7 @@ async function readFileHandle(filehandle, options) {
568
569
}
569
570
570
571
if ( ! encoding ) {
571
- if ( size === 0 && ! singleRead ) {
572
+ if ( noSize && ! singleRead ) {
572
573
ArrayPrototypePush ( buffers , buffer ) ;
573
574
return Buffer . concat ( buffers , totalRead ) ;
574
575
}
@@ -581,15 +582,17 @@ async function readFileHandle(filehandle, options) {
581
582
result += decoder . end ( buffer ) ;
582
583
return result ;
583
584
}
584
-
585
+ const readBuffer = bytesRead !== buffer . length ?
586
+ buffer . subarray ( 0 , bytesRead ) :
587
+ buffer ;
585
588
if ( encoding ) {
586
- result += decoder . write ( buffer ) ;
589
+ result += decoder . write ( readBuffer ) ;
587
590
} else if ( size !== 0 ) {
588
591
offset = totalRead ;
589
592
} else {
590
593
buffers ??= [ ] ;
591
594
// Unknown file size requires chunks.
592
- ArrayPrototypePush ( buffers , buffer ) ;
595
+ ArrayPrototypePush ( buffers , readBuffer ) ;
593
596
buffer = Buffer . allocUnsafeSlow ( kReadFileUnknownBufferLength ) ;
594
597
}
595
598
}
0 commit comments