File tree 2 files changed +32
-8
lines changed
2 files changed +32
-8
lines changed Original file line number Diff line number Diff line change 6
6
ArrayPrototypeSplice,
7
7
FunctionPrototypeBind,
8
8
ObjectDefineProperty,
9
+ PromiseReject,
9
10
Symbol,
10
11
SymbolAsyncIterator,
11
12
} = primordials ;
@@ -164,16 +165,24 @@ class Dir {
164
165
}
165
166
166
167
close ( callback ) {
167
- if ( this [ kDirClosed ] === true ) {
168
- throw new ERR_DIR_CLOSED ( ) ;
169
- }
170
-
168
+ // Promise
171
169
if ( callback === undefined ) {
170
+ if ( this [ kDirClosed ] === true ) {
171
+ return PromiseReject ( new ERR_DIR_CLOSED ( ) ) ;
172
+ }
172
173
return this [ kDirClosePromisified ] ( ) ;
173
- } else if ( typeof callback !== 'function' ) {
174
+ }
175
+
176
+ // callback
177
+ if ( typeof callback !== 'function' ) {
174
178
throw new ERR_INVALID_CALLBACK ( callback ) ;
175
179
}
176
180
181
+ if ( this [ kDirClosed ] === true ) {
182
+ process . nextTick ( callback , new ERR_DIR_CLOSED ( ) ) ;
183
+ return ;
184
+ }
185
+
177
186
if ( this [ kDirOperationQueue ] !== null ) {
178
187
this [ kDirOperationQueue ] . push ( ( ) => {
179
188
this . close ( callback ) ;
Original file line number Diff line number Diff line change @@ -223,12 +223,11 @@ async function doAsyncIterInvalidCallbackTest() {
223
223
}
224
224
doAsyncIterInvalidCallbackTest ( ) . then ( common . mustCall ( ) ) ;
225
225
226
- // Check if directory already closed - throw an exception
226
+ // Check first call to close() - should not report an error.
227
227
async function doAsyncIterDirClosedTest ( ) {
228
228
const dir = await fs . promises . opendir ( testDir ) ;
229
229
await dir . close ( ) ;
230
-
231
- assert . throws ( ( ) => dir . close ( ) , dirclosedError ) ;
230
+ await assert . rejects ( ( ) => dir . close ( ) , dirclosedError ) ;
232
231
}
233
232
doAsyncIterDirClosedTest ( ) . then ( common . mustCall ( ) ) ;
234
233
@@ -267,3 +266,19 @@ async function doConcurrentAsyncMixedOps() {
267
266
await promise2 ;
268
267
}
269
268
doConcurrentAsyncMixedOps ( ) . then ( common . mustCall ( ) ) ;
269
+
270
+ // Check if directory already closed - the callback should pass an error.
271
+ {
272
+ const dir = fs . opendirSync ( testDir ) ;
273
+ dir . closeSync ( ) ;
274
+ dir . close ( common . mustCall ( ( error ) => {
275
+ assert . strictEqual ( error . code , dirclosedError . code ) ;
276
+ } ) ) ;
277
+ }
278
+
279
+ // Check if directory already closed - throw an promise exception.
280
+ {
281
+ const dir = fs . opendirSync ( testDir ) ;
282
+ dir . closeSync ( ) ;
283
+ assert . rejects ( dir . close ( ) , dirclosedError ) . then ( common . mustCall ( ) ) ;
284
+ }
You can’t perform that action at this time.
0 commit comments