fs: allow empty string for temp directory prefix

The `fs` lib module's `mkdtemp()` and `mkdtempSync()` methods were
missing a validator, and weren't allowing the empty string as a valid
prefix.

PR-URL: https://github.com/nodejs/node/pull/39028
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Zijian Liu <lxxyxzj@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Khaidi Chu <i@2333.moe>
This commit is contained in:
Voltrex 2021-06-14 03:37:54 +04:30 committed by Michaël Zasso
parent fe1c81f247
commit 83f3b959f9
No known key found for this signature in database
GPG Key ID: 770F7A9A5AE15600
4 changed files with 20 additions and 12 deletions

View File

@ -802,6 +802,10 @@ rejection only when `recursive` is false.
### `fsPromises.mkdtemp(prefix[, options])`
<!-- YAML
added: v10.0.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/39028
description: The `prefix` parameter now accepts an empty string.
-->
* `prefix` {string}
@ -2572,6 +2576,9 @@ See the POSIX mkdir(2) documentation for more details.
<!-- YAML
added: v5.10.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/39028
description: The `prefix` parameter now accepts an empty string.
- version: v10.0.0
pr-url: https://github.com/nodejs/node/pull/12562
description: The `callback` parameter is no longer optional. Not passing
@ -4501,6 +4508,10 @@ See the POSIX mkdir(2) documentation for more details.
### `fs.mkdtempSync(prefix[, options])`
<!-- YAML
added: v5.10.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/39028
description: The `prefix` parameter now accepts an empty string.
-->
* `prefix` {string}

View File

@ -74,7 +74,6 @@ const {
codes: {
ERR_FS_FILE_TOO_LARGE,
ERR_INVALID_ARG_VALUE,
ERR_INVALID_ARG_TYPE,
ERR_FEATURE_UNAVAILABLE_ON_PLATFORM,
},
AbortError,
@ -136,6 +135,7 @@ const {
validateEncoding,
validateFunction,
validateInteger,
validateString,
} = require('internal/validators');
const watchers = require('internal/fs/watchers');
@ -2712,9 +2712,8 @@ realpath.native = (path, options, callback) => {
function mkdtemp(prefix, options, callback) {
callback = makeCallback(typeof options === 'function' ? options : callback);
options = getOptions(options, {});
if (!prefix || typeof prefix !== 'string') {
throw new ERR_INVALID_ARG_TYPE('prefix', 'string', prefix);
}
validateString(prefix, 'prefix');
nullCheck(prefix, 'prefix');
warnOnNonPortableTemplate(prefix);
const req = new FSReqCallback();
@ -2730,9 +2729,8 @@ function mkdtemp(prefix, options, callback) {
*/
function mkdtempSync(prefix, options) {
options = getOptions(options, {});
if (!prefix || typeof prefix !== 'string') {
throw new ERR_INVALID_ARG_TYPE('prefix', 'string', prefix);
}
validateString(prefix, 'prefix');
nullCheck(prefix, 'prefix');
warnOnNonPortableTemplate(prefix);
const path = `${prefix}XXXXXX`;

View File

@ -28,7 +28,6 @@ const { Buffer } = require('buffer');
const {
codes: {
ERR_FS_FILE_TOO_LARGE,
ERR_INVALID_ARG_TYPE,
ERR_INVALID_ARG_VALUE,
ERR_METHOD_NOT_IMPLEMENTED,
},
@ -72,6 +71,7 @@ const {
validateBuffer,
validateEncoding,
validateInteger,
validateString,
} = require('internal/validators');
const pathModule = require('path');
const { promisify } = require('internal/util');
@ -693,9 +693,8 @@ async function realpath(path, options) {
async function mkdtemp(prefix, options) {
options = getOptions(options, {});
if (!prefix || typeof prefix !== 'string') {
throw new ERR_INVALID_ARG_TYPE('prefix', 'string', prefix);
}
validateString(prefix, 'prefix');
nullCheck(prefix);
warnOnNonPortableTemplate(prefix);
return binding.mkdtemp(`${prefix}XXXXXX`, options.encoding, kUsePromises);

View File

@ -3,7 +3,7 @@ const common = require('../common');
const assert = require('assert');
const fs = require('fs');
const prefixValues = [undefined, null, 0, true, false, 1, ''];
const prefixValues = [undefined, null, 0, true, false, 1];
function fail(value) {
assert.throws(