Skip to content

Commit 83f3b95

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 fe1c81f commit 83f3b95

File tree

4 files changed

+20
-12
lines changed

4 files changed

+20
-12
lines changed

Diff for: doc/api/fs.md

+11
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,10 @@ rejection only when `recursive` is false.
802802
### `fsPromises.mkdtemp(prefix[, options])`
803803
<!-- YAML
804804
added: v10.0.0
805+
changes:
806+
- version: REPLACEME
807+
pr-url: https://github.com/nodejs/node/pull/39028
808+
description: The `prefix` parameter now accepts an empty string.
805809
-->
806810
807811
* `prefix` {string}
@@ -2572,6 +2576,9 @@ See the POSIX mkdir(2) documentation for more details.
25722576
<!-- YAML
25732577
added: v5.10.0
25742578
changes:
2579+
- version: REPLACEME
2580+
pr-url: https://github.com/nodejs/node/pull/39028
2581+
description: The `prefix` parameter now accepts an empty string.
25752582
- version: v10.0.0
25762583
pr-url: https://github.com/nodejs/node/pull/12562
25772584
description: The `callback` parameter is no longer optional. Not passing
@@ -4501,6 +4508,10 @@ See the POSIX mkdir(2) documentation for more details.
45014508
### `fs.mkdtempSync(prefix[, options])`
45024509
<!-- YAML
45034510
added: v5.10.0
4511+
changes:
4512+
- version: REPLACEME
4513+
pr-url: https://github.com/nodejs/node/pull/39028
4514+
description: The `prefix` parameter now accepts an empty string.
45044515
-->
45054516
45064517
* `prefix` {string}

Diff for: lib/fs.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ const {
7474
codes: {
7575
ERR_FS_FILE_TOO_LARGE,
7676
ERR_INVALID_ARG_VALUE,
77-
ERR_INVALID_ARG_TYPE,
7877
ERR_FEATURE_UNAVAILABLE_ON_PLATFORM,
7978
},
8079
AbortError,
@@ -136,6 +135,7 @@ const {
136135
validateEncoding,
137136
validateFunction,
138137
validateInteger,
138+
validateString,
139139
} = require('internal/validators');
140140

141141
const watchers = require('internal/fs/watchers');
@@ -2712,9 +2712,8 @@ realpath.native = (path, options, callback) => {
27122712
function mkdtemp(prefix, options, callback) {
27132713
callback = makeCallback(typeof options === 'function' ? options : callback);
27142714
options = getOptions(options, {});
2715-
if (!prefix || typeof prefix !== 'string') {
2716-
throw new ERR_INVALID_ARG_TYPE('prefix', 'string', prefix);
2717-
}
2715+
2716+
validateString(prefix, 'prefix');
27182717
nullCheck(prefix, 'prefix');
27192718
warnOnNonPortableTemplate(prefix);
27202719
const req = new FSReqCallback();
@@ -2730,9 +2729,8 @@ function mkdtemp(prefix, options, callback) {
27302729
*/
27312730
function mkdtempSync(prefix, options) {
27322731
options = getOptions(options, {});
2733-
if (!prefix || typeof prefix !== 'string') {
2734-
throw new ERR_INVALID_ARG_TYPE('prefix', 'string', prefix);
2735-
}
2732+
2733+
validateString(prefix, 'prefix');
27362734
nullCheck(prefix, 'prefix');
27372735
warnOnNonPortableTemplate(prefix);
27382736
const path = `${prefix}XXXXXX`;

Diff for: lib/internal/fs/promises.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ const { Buffer } = require('buffer');
2828
const {
2929
codes: {
3030
ERR_FS_FILE_TOO_LARGE,
31-
ERR_INVALID_ARG_TYPE,
3231
ERR_INVALID_ARG_VALUE,
3332
ERR_METHOD_NOT_IMPLEMENTED,
3433
},
@@ -72,6 +71,7 @@ const {
7271
validateBuffer,
7372
validateEncoding,
7473
validateInteger,
74+
validateString,
7575
} = require('internal/validators');
7676
const pathModule = require('path');
7777
const { promisify } = require('internal/util');
@@ -693,9 +693,8 @@ async function realpath(path, options) {
693693

694694
async function mkdtemp(prefix, options) {
695695
options = getOptions(options, {});
696-
if (!prefix || typeof prefix !== 'string') {
697-
throw new ERR_INVALID_ARG_TYPE('prefix', 'string', prefix);
698-
}
696+
697+
validateString(prefix, 'prefix');
699698
nullCheck(prefix);
700699
warnOnNonPortableTemplate(prefix);
701700
return binding.mkdtemp(`${prefix}XXXXXX`, options.encoding, kUsePromises);

Diff for: 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)