@@ -47,6 +47,7 @@ class Dir {
4747 #closePromisified;
4848 #operationQueue = null ;
4949 #handlerQueue = [ ] ;
50+ #pendingRecursiveOpens = 0 ;
5051
5152 constructor ( handle , path , options ) {
5253 if ( handle == null ) throw new ERR_MISSING_ARGS ( 'handle' ) ;
@@ -133,7 +134,11 @@ class Dir {
133134 const dirent = ArrayPrototypeShift ( this . #bufferedEntries) ;
134135
135136 if ( this . #options. recursive && dirent . isDirectory ( ) ) {
136- this . #readSyncRecursive( dirent ) ;
137+ if ( maybeSync ) {
138+ this . #readSyncRecursive( dirent ) ;
139+ } else {
140+ this . #readRecursive( dirent ) ;
141+ }
137142 }
138143
139144 if ( maybeSync )
@@ -146,6 +151,13 @@ class Dir {
146151 }
147152 }
148153
154+ if ( ! maybeSync && this . #pendingRecursiveOpens > 0 ) {
155+ ArrayPrototypePush ( this . #operationQueue ??= [ ] , ( ) => {
156+ this . #readImpl( maybeSync , callback ) ;
157+ } ) ;
158+ return ;
159+ }
160+
149161 const req = new FSReqCallback ( ) ;
150162 req . oncomplete = ( err , result ) => {
151163 process . nextTick ( ( ) => {
@@ -205,6 +217,24 @@ class Dir {
205217 ArrayPrototypePush ( this . #handlerQueue, { handle, path } ) ;
206218 }
207219
220+ #readRecursive( dirent ) {
221+ const path = pathModule . join ( dirent . parentPath , dirent . name ) ;
222+ const req = new FSReqCallback ( ) ;
223+ req . oncomplete = ( err , handle ) => {
224+ if ( ! err && handle ) {
225+ ArrayPrototypePush ( this . #handlerQueue, { handle, path } ) ;
226+ }
227+ this . #pendingRecursiveOpens-- ;
228+ if ( this . #pendingRecursiveOpens === 0 && this . #operationQueue !== null ) {
229+ const queue = this . #operationQueue;
230+ this . #operationQueue = null ;
231+ for ( const op of queue ) op ( ) ;
232+ }
233+ } ;
234+ this . #pendingRecursiveOpens++ ;
235+ dirBinding . opendir ( path , this . #options. encoding , req ) ;
236+ }
237+
208238 readSync ( ) {
209239 if ( this . #closed === true ) {
210240 throw new ERR_DIR_CLOSED ( ) ;
0 commit comments