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

[WIP] fs: feature detection for recursive mkdir[Sync] #22302

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 6 additions & 10 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -2086,13 +2086,11 @@ fs.mkdir('/tmp/a/apple', { recursive: true }, (err) => {
});
```

`fs.mkdir` has non-enumerable non-writable property `Symbol('recursive')` that
can be used to detect if the recursive feature is available
The `util.features` symbol can be used to feature detect if
recusion is available.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: recursion

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to recuse myself 😅


```js
Object.getOwnPropertySymbols(fs.mkdir).map((sym) => {
return sym.toString();
}).includes('Symbol(recursive)');
fs.mkdir[util.features].includes('recursive');
// true
```

Expand All @@ -2116,13 +2114,11 @@ changes:
Synchronously creates a directory. Returns `undefined`.
This is the synchronous version of [`fs.mkdir()`][].

`fs.mkdirSync` has non-enumerable non-writable property `Symbol('recursive')`
that can be used to detect if the recursive feature is available
The `util.features` symbol can be used to feature detect if
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Link to util.features?

recusion is available.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto


```js
Object.getOwnPropertySymbols(fs.mkdirSync).map((sym) => {
return sym.toString();
}).includes('Symbol(recursive)');
fs.mkdirSync[util.features].includes('recursive');
// true
```

Expand Down
7 changes: 7 additions & 0 deletions doc/api/util.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,13 @@ The `--throw-deprecation` command line flag and `process.throwDeprecation`
property take precedence over `--trace-deprecation` and
`process.traceDeprecation`.

### util.features
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: ### -> ##

<!-- YAML
added: REPlACEME
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

l -> L

-->

* {symbol} that can be used to do feature detection.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😍😻😍

## util.format(format[, ...args])
<!-- YAML
added: v0.5.3
Expand Down
22 changes: 9 additions & 13 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const {
O_SYMLINK
} = constants;

const { _extend } = require('util');
const { _extend, features } = require('util');
const pathModule = require('path');
const { isUint8Array } = require('internal/util/types');
const binding = process.binding('fs');
Expand Down Expand Up @@ -745,12 +745,10 @@ function mkdir(path, options, callback) {
validateMode(mode, 'mode', 0o777), recursive, req);
}

Object.defineProperties(mkdir, {
[Symbol('recursive')]: {
value: true,
writable: false,
enumerable: false
}
Object.defineProperty(mkdir, features, {
value: ['recursive'],
writable: false,
enumerable: false

This comment was marked as resolved.

});

function mkdirSync(path, options) {
Expand All @@ -774,12 +772,10 @@ function mkdirSync(path, options) {
handleErrorFromBinding(ctx);
}

Object.defineProperties(mkdirSync, {
[Symbol('recursive')]: {
value: true,
writable: false,
enumerable: false
}
Object.defineProperty(mkdirSync, features, {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we also add this property on the fs.promises implementation?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that once we have consensus on this PR we should make a tracking issue and potentially get a bunch of different APIs properly setup with this

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, I was more specifically thinking we should have the symbol on the fs.promises.mkdir function.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we get consensus on this approach, let's pull the Object.defineProperty bit into its own helper too, and hide the writeable: false , enumerable: false ritual?

common.addFeatures(mkdirSync)

value: ['recursive'],
writable: false,
enumerable: false
});

function readdir(path, options, callback) {
Expand Down
1 change: 1 addition & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -1472,6 +1472,7 @@ module.exports = exports = {
callbackify,
debuglog,
deprecate,
features: Symbol('features'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can the description be node.util.features

This comment was marked as resolved.

format,
formatWithOptions,
getSystemErrorName,
Expand Down
17 changes: 5 additions & 12 deletions test/parallel/test-fs-mkdir.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const common = require('../common');
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const { features } = require('util');

const tmpdir = require('../common/tmpdir');
tmpdir.refresh();
Expand Down Expand Up @@ -174,18 +175,10 @@ if (common.isMainThread && (common.isLinux || common.isOSX)) {

// mkdirp and mkdirSyncp feature detection
{
assert.strictEqual(Object.getOwnPropertySymbols(fs.mkdir).map((sym) => {
return sym.toString();
}).includes('Symbol(recursive)'), true);
assert.strictEqual(Object.getOwnPropertySymbols(fs.mkdirSync).map((sym) => {
return sym.toString();
}).includes('Symbol(recursive)'), true);
assert.strictEqual(Object.getOwnPropertySymbols(fs.mkdir).map((sym) => {
return sym.toString();
}).includes('Symbol(fhwdgads)'), false);
assert.strictEqual(Object.getOwnPropertySymbols(fs.mkdirSync).map((sym) => {
return sym.toString();
}).includes('Symbol(fhwdgads)'), false);
assert.strictEqual(fs.mkdir[features].includes('recursive'), true);
assert.strictEqual(fs.mkdir[features].includes('fhwdgads'), false);
assert.strictEqual(fs.mkdirSync[features].includes('recursive'), true);
assert.strictEqual(fs.mkdirSync[features].includes('fhwdgads'), false);
}

// Keep the event loop alive so the async mkdir() requests
Expand Down