11'use strict' ;
22
33const {
4+ ArrayPrototypePush,
45 Promise,
56 StringPrototypeStartsWith,
67} = primordials ;
78
9+ const { Buffer } = require ( 'buffer' ) ;
810const fs = require ( 'fs' ) ;
911const path = require ( 'path' ) ;
1012const { VirtualProvider } = require ( 'internal/vfs/provider' ) ;
@@ -17,6 +19,8 @@ const {
1719 createENOENT,
1820} = require ( 'internal/vfs/errors' ) ;
1921
22+ const kReadFileUnknownBufferLength = 8192 ;
23+
2024/**
2125 * A file handle that wraps a real file descriptor.
2226 */
@@ -34,6 +38,20 @@ class RealFileHandle extends VirtualFileHandle {
3438 }
3539 }
3640
41+ #readFileResult( buffer , bytesRead , options ) {
42+ buffer = buffer . subarray ( 0 , bytesRead ) ;
43+ const encoding = typeof options === 'string' ? options : options ?. encoding ;
44+ if ( encoding && encoding !== 'buffer' ) {
45+ buffer = buffer . toString ( encoding ) ;
46+ }
47+ return buffer ;
48+ }
49+
50+ #readFileUnknownSizeResult( buffers , totalRead , options ) {
51+ return this . #readFileResult(
52+ Buffer . concat ( buffers , totalRead ) , totalRead , options ) ;
53+ }
54+
3755 /**
3856 * @param {string } path The VFS path
3957 * @param {string } flags The open flags
@@ -79,12 +97,77 @@ class RealFileHandle extends VirtualFileHandle {
7997
8098 readFileSync ( options ) {
8199 this . #checkClosed( 'read' ) ;
82- return fs . readFileSync ( this . #realPath, options ) ;
100+ const size = fs . fstatSync ( this . #fd) . 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 ) ;
118+ let bytesRead = 0 ;
119+ while ( bytesRead < buffer . byteLength ) {
120+ const read = fs . readSync (
121+ this . #fd,
122+ buffer ,
123+ bytesRead ,
124+ buffer . byteLength - bytesRead ,
125+ bytesRead ,
126+ ) ;
127+ if ( read === 0 ) break ;
128+ bytesRead += read ;
129+ }
130+
131+ return this . #readFileResult( buffer , bytesRead , options ) ;
83132 }
84133
85134 async readFile ( options ) {
86135 this . #checkClosed( 'read' ) ;
87- return fs . promises . readFile ( this . #realPath, options ) ;
136+ const size = ( await this . stat ( ) ) . 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 ) ;
158+ let bytesRead = 0 ;
159+ while ( bytesRead < buffer . byteLength ) {
160+ const { bytesRead : read } = await this . read (
161+ buffer ,
162+ bytesRead ,
163+ buffer . byteLength - bytesRead ,
164+ bytesRead ,
165+ ) ;
166+ if ( read === 0 ) break ;
167+ bytesRead += read ;
168+ }
169+
170+ return this . #readFileResult( buffer , bytesRead , options ) ;
88171 }
89172
90173 writeFileSync ( data , options ) {
0 commit comments