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 3 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
16 changes: 16 additions & 0 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -2086,6 +2086,14 @@ fs.mkdir('/tmp/a/apple', { recursive: true }, (err) => {
});
```

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
fs.mkdir[util.features].recursive;
// true
```

See also: mkdir(2).

## fs.mkdirSync(path[, options])
Expand All @@ -2106,6 +2114,14 @@ changes:
Synchronously creates a directory. Returns `undefined`.
This is the synchronous version of [`fs.mkdir()`][].

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
fs.mkdirSync[util.features].recursive;
// true
```

See also: mkdir(2).

## fs.mkdtemp(prefix[, options], callback)
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
18 changes: 17 additions & 1 deletion 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,6 +745,14 @@ function mkdir(path, options, callback) {
validateMode(mode, 'mode', 0o777), recursive, req);
}

Object.defineProperty(mkdir, features, {
value: {
recursive: true
},
writable: false,
enumerable: false

This comment was marked as resolved.

});

function mkdirSync(path, options) {
if (typeof options === 'number' || typeof options === 'string') {
options = { mode: options };
Expand All @@ -766,6 +774,14 @@ function mkdirSync(path, options) {
handleErrorFromBinding(ctx);
}

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: true
},
writable: false,
enumerable: false
});

function readdir(path, options, callback) {
callback = makeCallback(typeof options === 'function' ? options : callback);
options = getOptions(options, {});
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
9 changes: 9 additions & 0 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 @@ -172,6 +173,14 @@ if (common.isMainThread && (common.isLinux || common.isOSX)) {
});
}

// mkdirp and mkdirSyncp feature detection
{
assert.strictEqual(fs.mkdir[features].recursive, true);
Copy link
Contributor

Choose a reason for hiding this comment

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

let's add a test for the undefined case?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

which undefined case? where features is undefined?

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah, I think it would be good to demonstrate that this will work fine for feature detection when util.features is undefined.

!!mkdir[require('util').propretyThatDoesntExist] === false

assert.strictEqual(fs.mkdir[features].fhwdgads, undefined);
assert.strictEqual(fs.mkdirSync[features].recursive, true);
assert.strictEqual(fs.mkdirSync[features].fhwdgads, undefined);
}

// Keep the event loop alive so the async mkdir() requests
// have a chance to run (since they don't ref the event loop).
process.nextTick(() => {});