Skip to content

Commit 3e3da50

Browse files
fs: return Buffer from mkdtemp when prefix is a Buffer
Signed-off-by: Hamid Reza Ghavami <hamidr.ghavami@gmail.com>
1 parent 8fec65d commit 3e3da50

4 files changed

Lines changed: 43 additions & 17 deletions

File tree

lib/fs.js

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

35733573
options = getOptions(options);
3574-
3574+
if (BufferIsBuffer(prefix)) {
3575+
options = { ...options, encoding: 'buffer' };
3576+
}
35753577
prefix = getValidatedPath(prefix, 'prefix');
35763578
warnOnNonPortableTemplate(prefix);
35773579

@@ -3594,7 +3596,9 @@ function mkdtempSync(prefix, options) {
35943596
}
35953597

35963598
options = getOptions(options);
3597-
3599+
if (BufferIsBuffer(prefix)) {
3600+
options = { ...options, encoding: 'buffer' };
3601+
}
35983602
prefix = getValidatedPath(prefix, 'prefix');
35993603
warnOnNonPortableTemplate(prefix);
36003604
return binding.mkdtemp(prefix, options.encoding);
@@ -3610,7 +3614,9 @@ function mkdtempSync(prefix, options) {
36103614
*/
36113615
function mkdtempDisposableSync(prefix, options) {
36123616
options = getOptions(options);
3613-
3617+
if (BufferIsBuffer(prefix)) {
3618+
options = { ...options, encoding: 'buffer' };
3619+
}
36143620
prefix = getValidatedPath(prefix, 'prefix');
36153621
warnOnNonPortableTemplate(prefix);
36163622

lib/internal/fs/promises.js

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

@@ -2039,7 +2040,9 @@ async function mkdtemp(prefix, options) {
20392040

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

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((resultCb) => {
19+
assert.strictEqual(Buffer.isBuffer(resultCb), 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 & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,22 +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-
75-
fs.mkdtemp(Buffer.from(tmpdir.resolve('bar.')), common.mustCall(handler));
76-
77-
// Same test as above, but making sure that passing an options object doesn't
78-
// affect the way the callback function is handled.
79-
fs.mkdtemp(Buffer.from(tmpdir.resolve('bar.')), {}, common.mustCall(handler));
80-
81-
// Warning fires only once
82-
fs.mkdtemp(Buffer.from(tmpdir.resolve('bar.X')), common.mustCall(handler));
8374
}
8475

8576
// Test with Uint8Array

0 commit comments

Comments
 (0)