Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -6740,6 +6740,26 @@ provided by the operating system's underlying directory mechanisms.
Entries added or removed while iterating over the directory might not be
included in the iteration results.

#### `dir[Symbol.asyncDispose]()`

<!-- YAML
added: REPLACEME
-->

> Stability: 1 - Experimental

An alias for `dir.close()`.

#### `dir[Symbol.Dispose]()`

<!-- YAML
added: REPLACEME
-->

> Stability: 1 - Experimental

An alias for `dir.closeSync()`.

### Class: `fs.Dirent`

<!-- YAML
Expand Down
25 changes: 21 additions & 4 deletions lib/internal/fs/dir.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ const {
ArrayPrototypePush,
ArrayPrototypeShift,
FunctionPrototypeBind,
ObjectDefineProperty,
ObjectDefineProperties,
PromiseReject,
SymbolAsyncDispose,
SymbolAsyncIterator,
SymbolDispose,
} = primordials;

const pathModule = require('path');
Expand Down Expand Up @@ -293,12 +295,27 @@ class Dir {
}
}

ObjectDefineProperty(Dir.prototype, SymbolAsyncIterator, {
__proto__: null,
value: Dir.prototype.entries,
const nonEnumerableDescriptor = {
enumerable: false,
writable: true,
configurable: true,
};
ObjectDefineProperties(Dir.prototype, {
[SymbolDispose]: {
__proto__: null,
...nonEnumerableDescriptor,
value: Dir.prototype.closeSync,
},
[SymbolAsyncDispose]: {
__proto__: null,
...nonEnumerableDescriptor,
value: Dir.prototype.close,
},
[SymbolAsyncIterator]: {
__proto__: null,
...nonEnumerableDescriptor,
value: Dir.prototype.entries,
},
});

function opendir(path, options, callback) {
Expand Down
16 changes: 13 additions & 3 deletions test/parallel/test-fs-promises-file-handle-dispose.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
'use strict';

const common = require('../common');
const { promises: fs } = require('fs');
const assert = require('assert');
const { opendirSync, promises: fs } = require('fs');

async function doOpen() {
async function explicitCall() {
const fh = await fs.open(__filename);
fh.on('close', common.mustCall());
await fh[Symbol.asyncDispose]();

const dh = await fs.opendir(__dirname);
await dh[Symbol.asyncDispose]();
await assert.rejects(dh.read(), { code: 'ERR_DIR_CLOSED' });

const dhSync = opendirSync(__dirname);
dhSync[Symbol.dispose]();
assert.throws(() => dhSync.readSync(), { code: 'ERR_DIR_CLOSED' });
}

doOpen().then(common.mustCall());
explicitCall().then(common.mustCall());
// TODO(aduh95): add test for implicit calls, with `await using` syntax.
Loading