Skip to content

Commit 1f53d7e

Browse files
fs: return Buffer from mkdtemp when prefix is a Buffer
Signed-off-by: Hamid Reza Ghavami <hamidr.ghavami@gmail.com> PR-URL: #64397 Fixes: #58795 Reviewed-By: René <contact.9a5d6388@renegade334.me.uk> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 10720f7 commit 1f53d7e

5 files changed

Lines changed: 65 additions & 14 deletions

File tree

‎doc/api/fs.md‎

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1637,6 +1637,9 @@ makeDirectory().catch(console.error);
16371637
<!-- YAML
16381638
added: v10.0.0
16391639
changes:
1640+
- version: REPLACEME
1641+
pr-url: https://github.com/nodejs/node/pull/64397
1642+
description: The `encoding` option now defaults to `'buffer'` if `prefix` is a `Buffer`.
16401643
- version:
16411644
- v20.6.0
16421645
- v18.19.0
@@ -1651,7 +1654,7 @@ changes:
16511654
16521655
* `prefix` {string|Buffer|URL}
16531656
* `options` {string|Object}
1654-
* `encoding` {string} **Default:** `'utf8'`
1657+
* `encoding` {string} **Default:** `'utf8'` (or `'buffer'` if `prefix` is a `Buffer`)
16551658
* Returns: {Promise} Fulfills with the created directory path.
16561659
If `encoding` is `'buffer'`, then the resulting directory
16571660
path is returned as a {Buffer}. Otherwise, the path is returned as a
@@ -1688,11 +1691,15 @@ characters directly to the `prefix` string. For instance, given a directory
16881691
16891692
<!-- YAML
16901693
added: v24.4.0
1694+
changes:
1695+
- version: REPLACEME
1696+
pr-url: https://github.com/nodejs/node/pull/64397
1697+
description: The `encoding` option now defaults to `'buffer'` if `prefix` is a `Buffer`.
16911698
-->
16921699
16931700
* `prefix` {string|Buffer|URL}
16941701
* `options` {string|Object}
1695-
* `encoding` {string} **Default:** `'utf8'`
1702+
* `encoding` {string} **Default:** `'utf8'` (or `'buffer'` if `prefix` is a `Buffer`)
16961703
* Returns: {Promise} Fulfills with a Promise for an async-disposable Object:
16971704
* `path` {string|Buffer} The path of the created directory.
16981705
* `remove` {AsyncFunction} A function which removes the created directory.
@@ -3978,6 +3985,9 @@ See the POSIX mkdir(2) documentation for more details.
39783985
<!-- YAML
39793986
added: v5.10.0
39803987
changes:
3988+
- version: REPLACEME
3989+
pr-url: https://github.com/nodejs/node/pull/64397
3990+
description: The `encoding` option now defaults to `'buffer'` if `prefix` is a `Buffer`.
39813991
- version:
39823992
- v20.6.0
39833993
- v18.19.0
@@ -4008,7 +4018,7 @@ changes:
40084018
40094019
* `prefix` {string|Buffer|URL}
40104020
* `options` {string|Object}
4011-
* `encoding` {string} **Default:** `'utf8'`
4021+
* `encoding` {string} **Default:** `'utf8'` (or `'buffer'` if `prefix` is a `Buffer`)
40124022
* `callback` {Function}
40134023
* `err` {Error}
40144024
* `directory` {string|Buffer}
@@ -6490,6 +6500,9 @@ See the POSIX mkdir(2) documentation for more details.
64906500
<!-- YAML
64916501
added: v5.10.0
64926502
changes:
6503+
- version: REPLACEME
6504+
pr-url: https://github.com/nodejs/node/pull/64397
6505+
description: The `encoding` option now defaults to `'buffer'` if `prefix` is a `Buffer`.
64936506
- version:
64946507
- v20.6.0
64956508
- v18.19.0
@@ -6504,7 +6517,7 @@ changes:
65046517
65056518
* `prefix` {string|Buffer|URL}
65066519
* `options` {string|Object}
6507-
* `encoding` {string} **Default:** `'utf8'`
6520+
* `encoding` {string} **Default:** `'utf8'` (or `'buffer'` if `prefix` is a `Buffer`)
65086521
* Returns: {string|Buffer}
65096522
65106523
Returns the created directory path. If `encoding` is `'buffer'`, then the
@@ -6521,11 +6534,15 @@ object with an `encoding` property specifying the character encoding to use.
65216534
65226535
<!-- YAML
65236536
added: v24.4.0
6537+
changes:
6538+
- version: REPLACEME
6539+
pr-url: https://github.com/nodejs/node/pull/64397
6540+
description: The `encoding` option now defaults to `'buffer'` if `prefix` is a `Buffer`.
65246541
-->
65256542
65266543
* `prefix` {string|Buffer|URL}
65276544
* `options` {string|Object}
6528-
* `encoding` {string} **Default:** `'utf8'`
6545+
* `encoding` {string} **Default:** `'utf8'` (or `'buffer'` if `prefix` is a `Buffer`)
65296546
* Returns: {Object} A disposable object:
65306547
* `path` {string|Buffer} The path of the created directory.
65316548
* `remove` {Function} A function which removes the created directory.

‎lib/fs.js‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3683,7 +3683,9 @@ function mkdtemp(prefix, options, callback) {
36833683
if (h !== null && vfsResult(h.mkdtemp(prefix, typeof options === 'function' ? undefined : options), callback)) return;
36843684

36853685
options = getOptions(options);
3686-
3686+
if (BufferIsBuffer(prefix)) {
3687+
options = { ...options, encoding: 'buffer' };
3688+
}
36873689
prefix = getValidatedPath(prefix, 'prefix');
36883690
warnOnNonPortableTemplate(prefix);
36893691

@@ -3706,7 +3708,9 @@ function mkdtempSync(prefix, options) {
37063708
}
37073709

37083710
options = getOptions(options);
3709-
3711+
if (BufferIsBuffer(prefix)) {
3712+
options = { ...options, encoding: 'buffer' };
3713+
}
37103714
prefix = getValidatedPath(prefix, 'prefix');
37113715
warnOnNonPortableTemplate(prefix);
37123716
return binding.mkdtemp(prefix, options.encoding);
@@ -3722,7 +3726,9 @@ function mkdtempSync(prefix, options) {
37223726
*/
37233727
function mkdtempDisposableSync(prefix, options) {
37243728
options = getOptions(options);
3725-
3729+
if (BufferIsBuffer(prefix)) {
3730+
options = { ...options, encoding: 'buffer' };
3731+
}
37263732
prefix = getValidatedPath(prefix, 'prefix');
37273733
warnOnNonPortableTemplate(prefix);
37283734

‎lib/internal/fs/promises.js‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2025,9 +2025,10 @@ async function mkdtemp(prefix, options) {
20252025
const promise = h.mkdtemp(prefix, options);
20262026
if (promise !== undefined) return await promise;
20272027
}
2028-
20292028
options = getOptions(options);
2030-
2029+
if (BufferIsBuffer(prefix)) {
2030+
options = { ...options, encoding: 'buffer' };
2031+
}
20312032
prefix = getValidatedPath(prefix, 'prefix');
20322033
warnOnNonPortableTemplate(prefix);
20332034

@@ -2040,7 +2041,9 @@ async function mkdtemp(prefix, options) {
20402041

20412042
async function mkdtempDisposable(prefix, options) {
20422043
options = getOptions(options);
2043-
2044+
if (BufferIsBuffer(prefix)) {
2045+
options = { ...options, encoding: 'buffer' };
2046+
}
20442047
prefix = getValidatedPath(prefix, 'prefix');
20452048
warnOnNonPortableTemplate(prefix);
20462049

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const fs = require('fs');
5+
const path = require('path');
6+
7+
const tmpdir = require('../common/tmpdir');
8+
tmpdir.refresh();
9+
10+
const prefixString = path.join(tmpdir.path, 'buffer-');
11+
const prefixBuffer = Buffer.from(prefixString);
12+
13+
// 1. Test Sync API
14+
const resultSync = fs.mkdtempSync(prefixBuffer);
15+
assert.strictEqual(Buffer.isBuffer(resultSync), true);
16+
17+
// 2. Test Callback API
18+
fs.mkdtemp(prefixBuffer, common.mustSucceed((result) => {
19+
assert.strictEqual(Buffer.isBuffer(result), true);
20+
}));
21+
22+
// 3. Test Promises API
23+
fs.promises.mkdtemp(prefixBuffer)
24+
.then(common.mustCall((resultPromise) => {
25+
assert.strictEqual(Buffer.isBuffer(resultPromise), true);
26+
}));

‎test/parallel/test-fs-mkdtemp.js‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,13 @@ function handler(err, folder) {
6464
{
6565
const tmpFolder = fs.mkdtempSync(Buffer.from(tmpdir.resolve('foo.')));
6666

67-
assert.strictEqual(path.basename(tmpFolder).length, 'foo.XXXXXX'.length);
67+
assert.strictEqual(path.basename(tmpFolder.toString()).length, 'foo.XXXXXX'.length);
6868
assert(fs.existsSync(tmpFolder));
6969

7070
const utf8 = fs.mkdtempSync(Buffer.from(tmpdir.resolve('\u0222abc.')));
71-
assert.strictEqual(Buffer.byteLength(path.basename(utf8)),
71+
assert.strictEqual(Buffer.byteLength(path.basename(utf8.toString())),
7272
Buffer.byteLength('\u0222abc.XXXXXX'));
7373
assert(fs.existsSync(utf8));
74-
7574
fs.mkdtemp(Buffer.from(tmpdir.resolve('bar.')), common.mustCall(handler));
7675

7776
// Same test as above, but making sure that passing an options object doesn't

0 commit comments

Comments
 (0)