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