Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -3882,6 +3882,10 @@ See the POSIX mkdir(2) documentation for more details.
<!-- YAML
added: v5.10.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/64397
description: The resulting path is now passed as a Buffer if the
provided prefix is a Buffer.
- version:
- v20.6.0
- v18.19.0
Expand Down Expand Up @@ -3915,7 +3919,7 @@ changes:
* `encoding` {string} **Default:** `'utf8'`
* `callback` {Function}
* `err` {Error}
* `directory` {string}
* `directory` {string|Buffer}

Creates a unique temporary directory.

Expand All @@ -3925,8 +3929,10 @@ inconsistencies, avoid trailing `X` characters in `prefix`. Some platforms,
notably the BSDs, can return more than six random characters, and replace
trailing `X` characters in `prefix` with random characters.

The created directory path is passed as a string to the callback's second
parameter.
The created directory path is passed to the callback's second parameter. If
`prefix` is a `Buffer`, then the resulting directory path is passed as a
`Buffer`. Otherwise, the path is passed as a string using the specified
encoding.

The optional `options` argument can be a string specifying an encoding, or an
object with an `encoding` property specifying the character encoding to use.
Expand Down
12 changes: 9 additions & 3 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
Expand All @@ -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);

Expand Down
9 changes: 6 additions & 3 deletions lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);

Expand Down
26 changes: 26 additions & 0 deletions test/parallel/test-fs-mkdtemp-buffer.js
Original file line number Diff line number Diff line change
@@ -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);
}));
5 changes: 2 additions & 3 deletions test/parallel/test-fs-mkdtemp.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading