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