Skip to content

Commit

Permalink
fs: allow empty string for temp directory prefix
Browse files Browse the repository at this point in the history
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: #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>
  • Loading branch information
VoltrexKeyva authored and targos committed Sep 4, 2021
1 parent f793380 commit 989c204
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
11 changes: 11 additions & 0 deletions doc/api/fs.md
Expand Up @@ -795,6 +795,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}
Expand Down Expand Up @@ -2533,6 +2537,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
Expand Down Expand Up @@ -4434,6 +4441,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}
Expand Down
13 changes: 6 additions & 7 deletions lib/fs.js
Expand Up @@ -119,7 +119,8 @@ const {
parseFileMode,
validateBuffer,
validateInteger,
validateInt32
validateInt32,
validateString,
} = require('internal/validators');

let truncateWarn = true;
Expand Down Expand Up @@ -1999,9 +2000,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();
Expand All @@ -2012,9 +2012,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`;
Expand Down
6 changes: 3 additions & 3 deletions lib/internal/fs/promises.js
Expand Up @@ -66,6 +66,7 @@ const {
validateAbortSignal,
validateBuffer,
validateInteger,
validateString,
} = require('internal/validators');
const pathModule = require('path');
const { promisify } = require('internal/util');
Expand Down Expand Up @@ -671,9 +672,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);
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-fs-mkdtemp-prefix-check.js
Expand Up @@ -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(
Expand Down

0 comments on commit 989c204

Please sign in to comment.