From 40fba964cdfd5e0aeee038304b514de183f61ef1 Mon Sep 17 00:00:00 2001 From: Hamid Reza Ghavami Date: Thu, 9 Jul 2026 23:43:04 +0300 Subject: [PATCH 1/5] fs: return Buffer from mkdtemp when prefix is a Buffer Signed-off-by: Hamid Reza Ghavami --- lib/fs.js | 12 +++++++++--- lib/internal/fs/promises.js | 9 ++++++--- test/parallel/test-fs-mkdtemp-buffer.js | 26 +++++++++++++++++++++++++ test/parallel/test-fs-mkdtemp.js | 13 ++----------- 4 files changed, 43 insertions(+), 17 deletions(-) create mode 100644 test/parallel/test-fs-mkdtemp-buffer.js diff --git a/lib/fs.js b/lib/fs.js index 1ea70ff192d6dd..fbc985a2678ddb 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -3571,7 +3571,9 @@ function mkdtemp(prefix, options, callback) { if (h !== null && vfsResult(h.mkdtemp(prefix, typeof options === 'function' ? undefined : options), callback)) return; options = getOptions(options); - + if (BufferIsBuffer(prefix)) { + options = { ...options, encoding: 'buffer' }; + } prefix = getValidatedPath(prefix, 'prefix'); warnOnNonPortableTemplate(prefix); @@ -3594,7 +3596,9 @@ function mkdtempSync(prefix, options) { } options = getOptions(options); - + if (BufferIsBuffer(prefix)) { + options = { ...options, encoding: 'buffer' }; + } prefix = getValidatedPath(prefix, 'prefix'); warnOnNonPortableTemplate(prefix); return binding.mkdtemp(prefix, options.encoding); @@ -3610,7 +3614,9 @@ function mkdtempSync(prefix, options) { */ function mkdtempDisposableSync(prefix, options) { options = getOptions(options); - + if (BufferIsBuffer(prefix)) { + options = { ...options, encoding: 'buffer' }; + } prefix = getValidatedPath(prefix, 'prefix'); warnOnNonPortableTemplate(prefix); diff --git a/lib/internal/fs/promises.js b/lib/internal/fs/promises.js index 3e336024a15a3c..2208864556c8aa 100644 --- a/lib/internal/fs/promises.js +++ b/lib/internal/fs/promises.js @@ -2024,9 +2024,10 @@ async function mkdtemp(prefix, options) { const promise = h.mkdtemp(prefix, options); if (promise !== undefined) return await promise; } - options = getOptions(options); - + if (BufferIsBuffer(prefix)) { + options = { ...options, encoding: 'buffer' }; + } prefix = getValidatedPath(prefix, 'prefix'); warnOnNonPortableTemplate(prefix); @@ -2039,7 +2040,9 @@ async function mkdtemp(prefix, options) { async function mkdtempDisposable(prefix, options) { options = getOptions(options); - + if (BufferIsBuffer(prefix)) { + options = { ...options, encoding: 'buffer' }; + } prefix = getValidatedPath(prefix, 'prefix'); warnOnNonPortableTemplate(prefix); diff --git a/test/parallel/test-fs-mkdtemp-buffer.js b/test/parallel/test-fs-mkdtemp-buffer.js new file mode 100644 index 00000000000000..4403db03570a9a --- /dev/null +++ b/test/parallel/test-fs-mkdtemp-buffer.js @@ -0,0 +1,26 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const fs = require('fs'); +const path = require('path'); + +const tmpdir = require('../common/tmpdir'); +tmpdir.refresh(); + +const prefixString = path.join(tmpdir.path, 'buffer-'); +const prefixBuffer = Buffer.from(prefixString); + +// 1. Test Sync API +const resultSync = fs.mkdtempSync(prefixBuffer); +assert.strictEqual(Buffer.isBuffer(resultSync), true); + +// 2. Test Callback API +fs.mkdtemp(prefixBuffer, common.mustSucceed((resultCb) => { + assert.strictEqual(Buffer.isBuffer(resultCb), true); +})); + +// 3. Test Promises API +fs.promises.mkdtemp(prefixBuffer) + .then(common.mustCall((resultPromise) => { + assert.strictEqual(Buffer.isBuffer(resultPromise), true); + })); diff --git a/test/parallel/test-fs-mkdtemp.js b/test/parallel/test-fs-mkdtemp.js index e93809d5b44546..a7d678d21ee02a 100644 --- a/test/parallel/test-fs-mkdtemp.js +++ b/test/parallel/test-fs-mkdtemp.js @@ -64,22 +64,13 @@ function handler(err, folder) { { const tmpFolder = fs.mkdtempSync(Buffer.from(tmpdir.resolve('foo.'))); - assert.strictEqual(path.basename(tmpFolder).length, 'foo.XXXXXX'.length); + assert.strictEqual(path.basename(tmpFolder.toString()).length, 'foo.XXXXXX'.length); assert(fs.existsSync(tmpFolder)); const utf8 = fs.mkdtempSync(Buffer.from(tmpdir.resolve('\u0222abc.'))); - assert.strictEqual(Buffer.byteLength(path.basename(utf8)), + assert.strictEqual(Buffer.byteLength(path.basename(utf8.toString())), Buffer.byteLength('\u0222abc.XXXXXX')); assert(fs.existsSync(utf8)); - - fs.mkdtemp(Buffer.from(tmpdir.resolve('bar.')), common.mustCall(handler)); - - // Same test as above, but making sure that passing an options object doesn't - // affect the way the callback function is handled. - fs.mkdtemp(Buffer.from(tmpdir.resolve('bar.')), {}, common.mustCall(handler)); - - // Warning fires only once - fs.mkdtemp(Buffer.from(tmpdir.resolve('bar.X')), common.mustCall(handler)); } // Test with Uint8Array From f30698f67a6344793b739818b06a7b801ad13f46 Mon Sep 17 00:00:00 2001 From: Hamid Reza Ghavami Date: Sat, 11 Jul 2026 22:10:38 +0300 Subject: [PATCH 2/5] test: restore missing mkdtemp async buffer tests --- test/parallel/test-fs-mkdtemp.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/parallel/test-fs-mkdtemp.js b/test/parallel/test-fs-mkdtemp.js index a7d678d21ee02a..3c2323440e0803 100644 --- a/test/parallel/test-fs-mkdtemp.js +++ b/test/parallel/test-fs-mkdtemp.js @@ -71,6 +71,14 @@ function handler(err, folder) { assert.strictEqual(Buffer.byteLength(path.basename(utf8.toString())), Buffer.byteLength('\u0222abc.XXXXXX')); assert(fs.existsSync(utf8)); + fs.mkdtemp(Buffer.from(tmpdir.resolve('bar.')), common.mustCall(handler)); + + // Same test as above, but making sure that passing an options object doesn't + // affect the way the callback function is handled. + fs.mkdtemp(Buffer.from(tmpdir.resolve('bar.')), {}, common.mustCall(handler)); + + // Warning fires only once + fs.mkdtemp(Buffer.from(tmpdir.resolve('bar.X')), common.mustCall(handler)); } // Test with Uint8Array From c137267bd485caaaed41e061546442c0c94c47c3 Mon Sep 17 00:00:00 2001 From: Hamid Reza Ghavami Date: Sun, 12 Jul 2026 10:27:52 +0300 Subject: [PATCH 3/5] doc: update fs.mkdtemp() return types and history --- doc/api/fs.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/doc/api/fs.md b/doc/api/fs.md index 51e1a6f40b985a..990a989a53469a 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -3882,6 +3882,9 @@ See the POSIX mkdir(2) documentation for more details.