diff --git a/doc/api/fs.md b/doc/api/fs.md index 9be67ded1f50..5955cfe0bf01 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -1637,6 +1637,9 @@ makeDirectory().catch(console.error); * `prefix` {string|Buffer|URL} * `options` {string|Object} - * `encoding` {string} **Default:** `'utf8'` + * `encoding` {string} **Default:** `'utf8'` (or `'buffer'` if `prefix` is a `Buffer`) * Returns: {Promise} Fulfills with a Promise for an async-disposable Object: * `path` {string|Buffer} The path of the created directory. * `remove` {AsyncFunction} A function which removes the created directory. @@ -3978,6 +3985,9 @@ See the POSIX mkdir(2) documentation for more details. * `prefix` {string|Buffer|URL} * `options` {string|Object} - * `encoding` {string} **Default:** `'utf8'` + * `encoding` {string} **Default:** `'utf8'` (or `'buffer'` if `prefix` is a `Buffer`) * Returns: {Object} A disposable object: * `path` {string|Buffer} The path of the created directory. * `remove` {Function} A function which removes the created directory. diff --git a/lib/fs.js b/lib/fs.js index 63312ba19507..d207caaefe1c 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -3679,7 +3679,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); @@ -3702,7 +3704,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); @@ -3718,7 +3722,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 ec163ccf9068..09654169cac3 100644 --- a/lib/internal/fs/promises.js +++ b/lib/internal/fs/promises.js @@ -2039,9 +2039,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); @@ -2054,7 +2055,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 000000000000..c9c04d57eaca --- /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((result) => { + assert.strictEqual(Buffer.isBuffer(result), 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 e93809d5b445..3c2323440e08 100644 --- a/test/parallel/test-fs-mkdtemp.js +++ b/test/parallel/test-fs-mkdtemp.js @@ -64,14 +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