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