Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v18.x backport] fs: introduce dirent.parentPath #51021

Closed
Closed
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
28 changes: 28 additions & 0 deletions doc/api/deprecations.md
Original file line number Diff line number Diff line change
Expand Up @@ -3321,6 +3321,32 @@
[`message.headersDistinct`][], [`message.trailers`][], and
[`message.trailersDistinct`][] will be read-only.

<!-- md-lint skip-deprecation DEP0172 -->

<!-- md-lint skip-deprecation DEP0173 -->

<!-- md-lint skip-deprecation DEP0174 -->

<!-- md-lint skip-deprecation DEP0175 -->

<!-- md-lint skip-deprecation DEP0176 -->

<!-- md-lint skip-deprecation DEP0177 -->

### DEP0178: `dirent.path`

<!-- YAML
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/51020

Check warning on line 3341 in doc/api/deprecations.md

View workflow job for this annotation

GitHub Actions / lint-pr-url

pr-url doesn't match the URL of the current PR.
description: Documentation-only deprecation.
-->

Type: Documentation-only

The [`dirent.path`][] is deprecated due to its lack of consistency across
release lines. Please use [`dirent.parentPath`][] instead.

[NIST SP 800-38D]: https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf
[RFC 6066]: https://tools.ietf.org/html/rfc6066#section-3
[RFC 8247 Section 2.4]: https://www.rfc-editor.org/rfc/rfc8247#section-2.4
Expand Down Expand Up @@ -3366,6 +3392,8 @@
[`decipher.setAuthTag()`]: crypto.md#deciphersetauthtagbuffer-encoding
[`diagnostics_channel.subscribe(name, onMessage)`]: diagnostics_channel.md#diagnostics_channelsubscribename-onmessage
[`diagnostics_channel.unsubscribe(name, onMessage)`]: diagnostics_channel.md#diagnostics_channelunsubscribename-onmessage
[`dirent.parentPath`]: fs.md#direntparentpath
[`dirent.path`]: fs.md#direntpath
[`dns.lookup()`]: dns.md#dnslookuphostname-options-callback
[`dnsPromises.lookup()`]: dns.md#dnspromiseslookuphostname-options
[`domain`]: domain.md
Expand Down
17 changes: 17 additions & 0 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -6437,12 +6437,28 @@ The file name that this {fs.Dirent} object refers to. The type of this
value is determined by the `options.encoding` passed to [`fs.readdir()`][] or
[`fs.readdirSync()`][].

#### `dirent.parentPath`

<!-- YAML
added:
- REPLACEME
-->

> Stability: 1 – Experimental

* {string}

The path to the parent directory of the file this {fs.Dirent} object refers to.

#### `dirent.path`

<!-- YAML
added: v18.17.0
deprecated: REPLACEME
-->

> Stability: 0 - Deprecated: Use [`dirent.parentPath`][] instead.

* {string}

The base path that this {fs.Dirent} object refers to.
Expand Down Expand Up @@ -7982,6 +7998,7 @@ the file contents.
[`Number.MAX_SAFE_INTEGER`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER
[`ReadDirectoryChangesW`]: https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-readdirectorychangesw
[`UV_THREADPOOL_SIZE`]: cli.md#uv_threadpool_sizesize
[`dirent.parentPath`]: #direntparentpath
[`event ports`]: https://illumos.org/man/port_create
[`filehandle.createReadStream()`]: #filehandlecreatereadstreamoptions
[`filehandle.createWriteStream()`]: #filehandlecreatewritestreamoptions
Expand Down
3 changes: 2 additions & 1 deletion lib/internal/fs/dir.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,10 @@ class Dir {
ArrayPrototypePush(
this[kDirBufferedEntries],
getDirent(
pathModule.join(path, result[i]),
path,
result[i],
result[i + 1],
true, // Quirk to not introduce a breaking change.
),
);
}
Expand Down
23 changes: 14 additions & 9 deletions lib/internal/fs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,10 @@ function assertEncoding(encoding) {
}

class Dirent {
constructor(name, type, path) {
constructor(name, type, path, filepath = path && join(path, name)) {
this.name = name;
this.path = path;
this.parentPath = path;
this.path = filepath;
this[kType] = type;
}

Expand Down Expand Up @@ -197,8 +198,8 @@ class Dirent {
}

class DirentFromStats extends Dirent {
constructor(name, stats, path) {
super(name, null, path);
constructor(name, stats, path, filepath) {
super(name, null, path, filepath);
this[kStats] = stats;
}
}
Expand Down Expand Up @@ -233,7 +234,7 @@ function join(path, name) {
}

if (typeof path === 'string' && typeof name === 'string') {
return pathModule.basename(path) === name ? path : pathModule.join(path, name);
return pathModule.join(path, name);
}

if (isUint8Array(path) && isUint8Array(name)) {
Expand Down Expand Up @@ -268,7 +269,7 @@ function getDirents(path, { 0: names, 1: types }, callback) {
callback(err);
return;
}
names[idx] = new DirentFromStats(name, stats, path);
names[idx] = new DirentFromStats(name, stats, path, filepath);
if (--toFinish === 0) {
callback(null, names);
}
Expand Down Expand Up @@ -304,17 +305,21 @@ function getDirent(path, name, type, callback) {
callback(err);
return;
}
callback(null, new DirentFromStats(name, stats, filepath));
callback(null, new DirentFromStats(name, stats, path, filepath));
});
} else {
callback(null, new Dirent(name, type, path));
}
} else if (type === UV_DIRENT_UNKNOWN) {
const filepath = join(path, name);
const stats = lazyLoadFs().lstatSync(filepath);
return new DirentFromStats(name, stats, path);
} else {
// callback === true: Quirk to not introduce a breaking change.
return new DirentFromStats(name, stats, path, callback === true ? filepath : path);
} else if (callback === true) {
// callback === true: Quirk to not introduce a breaking change.
return new Dirent(name, type, path);
} else {
return new Dirent(name, type, path, path);
}
}

Expand Down
8 changes: 5 additions & 3 deletions test/parallel/test-fs-opendir.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ const invalidCallbackObj = {
const entries = files.map(() => {
const dirent = dir.readSync();
assertDirent(dirent);
return dirent.name;
});
assert.deepStrictEqual(files, entries.sort());
return { name: dirent.name, path: dirent.path, parentPath: dirent.parentPath, toString() { return dirent.name; } };
}).sort();
assert.deepStrictEqual(entries.map((d) => d.name), files);
assert.deepStrictEqual(entries.map((d) => d.path), files.map((name) => path.join(testDir, name)));
assert.deepStrictEqual(entries.map((d) => d.parentPath), Array(entries.length).fill(testDir));

// dir.read should return null when no more entries exist
assert.strictEqual(dir.readSync(), null);
Expand Down