Skip to content

Commit 989c204

Browse files
VoltrexKeyvatargos
authored andcommitted
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: #39028 Reviewed-By: Darshan Sen <[email protected]> Reviewed-By: Zijian Liu <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Khaidi Chu <[email protected]>
1 parent f793380 commit 989c204

File tree

4 files changed

+21
-11
lines changed

4 files changed

+21
-11
lines changed

doc/api/fs.md

+11
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,10 @@ rejection only when `recursive` is false.
795795
### `fsPromises.mkdtemp(prefix[, options])`
796796
<!-- YAML
797797
added: v10.0.0
798+
changes:
799+
- version: REPLACEME
800+
pr-url: https://github.com/nodejs/node/pull/39028
801+
description: The `prefix` parameter now accepts an empty string.
798802
-->
799803
800804
* `prefix` {string}
@@ -2533,6 +2537,9 @@ See the POSIX mkdir(2) documentation for more details.
25332537
<!-- YAML
25342538
added: v5.10.0
25352539
changes:
2540+
- version: REPLACEME
2541+
pr-url: https://github.com/nodejs/node/pull/39028
2542+
description: The `prefix` parameter now accepts an empty string.
25362543
- version: v10.0.0
25372544
pr-url: https://github.com/nodejs/node/pull/12562
25382545
description: The `callback` parameter is no longer optional. Not passing
@@ -4434,6 +4441,10 @@ See the POSIX mkdir(2) documentation for more details.
44344441
### `fs.mkdtempSync(prefix[, options])`
44354442
<!-- YAML
44364443
added: v5.10.0
4444+
changes:
4445+
- version: REPLACEME
4446+
pr-url: https://github.com/nodejs/node/pull/39028
4447+
description: The `prefix` parameter now accepts an empty string.
44374448
-->
44384449
44394450
* `prefix` {string}

lib/fs.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ const {
119119
parseFileMode,
120120
validateBuffer,
121121
validateInteger,
122-
validateInt32
122+
validateInt32,
123+
validateString,
123124
} = require('internal/validators');
124125

125126
let truncateWarn = true;
@@ -1999,9 +2000,8 @@ realpath.native = (path, options, callback) => {
19992000
function mkdtemp(prefix, options, callback) {
20002001
callback = makeCallback(typeof options === 'function' ? options : callback);
20012002
options = getOptions(options, {});
2002-
if (!prefix || typeof prefix !== 'string') {
2003-
throw new ERR_INVALID_ARG_TYPE('prefix', 'string', prefix);
2004-
}
2003+
2004+
validateString(prefix, 'prefix');
20052005
nullCheck(prefix, 'prefix');
20062006
warnOnNonPortableTemplate(prefix);
20072007
const req = new FSReqCallback();
@@ -2012,9 +2012,8 @@ function mkdtemp(prefix, options, callback) {
20122012

20132013
function mkdtempSync(prefix, options) {
20142014
options = getOptions(options, {});
2015-
if (!prefix || typeof prefix !== 'string') {
2016-
throw new ERR_INVALID_ARG_TYPE('prefix', 'string', prefix);
2017-
}
2015+
2016+
validateString(prefix, 'prefix');
20182017
nullCheck(prefix, 'prefix');
20192018
warnOnNonPortableTemplate(prefix);
20202019
const path = `${prefix}XXXXXX`;

lib/internal/fs/promises.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ const {
6666
validateAbortSignal,
6767
validateBuffer,
6868
validateInteger,
69+
validateString,
6970
} = require('internal/validators');
7071
const pathModule = require('path');
7172
const { promisify } = require('internal/util');
@@ -671,9 +672,8 @@ async function realpath(path, options) {
671672

672673
async function mkdtemp(prefix, options) {
673674
options = getOptions(options, {});
674-
if (!prefix || typeof prefix !== 'string') {
675-
throw new ERR_INVALID_ARG_TYPE('prefix', 'string', prefix);
676-
}
675+
676+
validateString(prefix, 'prefix');
677677
nullCheck(prefix);
678678
warnOnNonPortableTemplate(prefix);
679679
return binding.mkdtemp(`${prefix}XXXXXX`, options.encoding, kUsePromises);

test/parallel/test-fs-mkdtemp-prefix-check.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const common = require('../common');
33
const assert = require('assert');
44
const fs = require('fs');
55

6-
const prefixValues = [undefined, null, 0, true, false, 1, ''];
6+
const prefixValues = [undefined, null, 0, true, false, 1];
77

88
function fail(value) {
99
assert.throws(

0 commit comments

Comments
 (0)