fs: return Buffer from mkdtemp when prefix is a Buffer · nodejs/node@1f53d7e · GitHub
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

‎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

0 commit comments

Comments
 (0)