11'use strict' ;
22
33const {
4+ ArrayPrototypePush,
45 Promise,
56 StringPrototypeStartsWith,
67} = primordials ;
@@ -18,6 +19,8 @@ const {
1819 createENOENT,
1920} = require ( 'internal/vfs/errors' ) ;
2021
22+ const kReadFileUnknownBufferLength = 8192 ;
23+
2124/**
2225 * A file handle that wraps a real file descriptor.
2326 */
@@ -35,10 +38,6 @@ class RealFileHandle extends VirtualFileHandle {
3538 }
3639 }
3740
38- #readFileBuffer( size ) {
39- return Buffer . allocUnsafe ( size || 8192 ) ;
40- }
41-
4241 #readFileResult( buffer , bytesRead , options ) {
4342 buffer = buffer . subarray ( 0 , bytesRead ) ;
4443 const encoding = typeof options === 'string' ? options : options ?. encoding ;
@@ -48,6 +47,11 @@ class RealFileHandle extends VirtualFileHandle {
4847 return buffer ;
4948 }
5049
50+ #readFileUnknownSizeResult( buffers , totalRead , options ) {
51+ return this . #readFileResult(
52+ Buffer . concat ( buffers , totalRead ) , totalRead , options ) ;
53+ }
54+
5155 /**
5256 * @param {string } path The VFS path
5357 * @param {string } flags The open flags
@@ -94,7 +98,23 @@ class RealFileHandle extends VirtualFileHandle {
9498 readFileSync ( options ) {
9599 this . #checkClosed( 'read' ) ;
96100 const size = fs . fstatSync ( this . #fd) . size ;
97- const buffer = this . #readFileBuffer( size ) ;
101+ if ( size === 0 ) {
102+ const buffers = [ ] ;
103+ let totalRead = 0 ;
104+
105+ while ( true ) {
106+ const buffer = Buffer . allocUnsafe ( kReadFileUnknownBufferLength ) ;
107+ const read = fs . readSync (
108+ this . #fd, buffer , 0 , buffer . byteLength , totalRead ) ;
109+ if ( read === 0 ) break ;
110+ ArrayPrototypePush ( buffers , buffer . subarray ( 0 , read ) ) ;
111+ totalRead += read ;
112+ }
113+
114+ return this . #readFileUnknownSizeResult( buffers , totalRead , options ) ;
115+ }
116+
117+ const buffer = Buffer . allocUnsafe ( size ) ;
98118 let bytesRead = 0 ;
99119 while ( bytesRead < buffer . byteLength ) {
100120 const read = fs . readSync (
@@ -114,7 +134,27 @@ class RealFileHandle extends VirtualFileHandle {
114134 async readFile ( options ) {
115135 this . #checkClosed( 'read' ) ;
116136 const size = ( await this . stat ( ) ) . size ;
117- const buffer = this . #readFileBuffer( size ) ;
137+ if ( size === 0 ) {
138+ const buffers = [ ] ;
139+ let totalRead = 0 ;
140+
141+ while ( true ) {
142+ const buffer = Buffer . allocUnsafe ( kReadFileUnknownBufferLength ) ;
143+ const { bytesRead : read } = await this . read (
144+ buffer ,
145+ 0 ,
146+ buffer . byteLength ,
147+ totalRead ,
148+ ) ;
149+ if ( read === 0 ) break ;
150+ ArrayPrototypePush ( buffers , buffer . subarray ( 0 , read ) ) ;
151+ totalRead += read ;
152+ }
153+
154+ return this . #readFileUnknownSizeResult( buffers , totalRead , options ) ;
155+ }
156+
157+ const buffer = Buffer . allocUnsafe ( size ) ;
118158 let bytesRead = 0 ;
119159 while ( bytesRead < buffer . byteLength ) {
120160 const { bytesRead : read } = await this . read (
0 commit comments