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
17 changes: 13 additions & 4 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -1637,6 +1637,9 @@ makeDirectory().catch(console.error);
<!-- YAML
added: v10.0.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/64397
description: The `encoding` option now defaults to `'buffer'` if `prefix` is a `Buffer`.
- version:
- v20.6.0
- v18.19.0
Expand All @@ -1651,7 +1654,7 @@ changes:

* `prefix` {string|Buffer|URL}
* `options` {string|Object}
* `encoding` {string} **Default:** `'utf8'`
* `encoding` {string} **Default:** `'utf8'` (or `'buffer'` if `prefix` is a `Buffer`)
Comment thread
aduh95 marked this conversation as resolved.
* Returns: {Promise} Fulfills with the created directory path.
If `encoding` is `'buffer'`, then the resulting directory
path is returned as a {Buffer}. Otherwise, the path is returned as a
Expand Down Expand Up @@ -1692,7 +1695,7 @@ added: v24.4.0

* `prefix` {string|Buffer|URL}
* `options` {string|Object}
* `encoding` {string} **Default:** `'utf8'`
* `encoding` {string} **Default:** `'utf8'` (or `'buffer'` if `prefix` is a `Buffer`)
Comment thread
hamidrezaghavami marked this conversation as resolved.
* 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.
Expand Down Expand Up @@ -3978,6 +3981,9 @@ See the POSIX mkdir(2) documentation for more details.
<!-- YAML
added: v5.10.0
changes:
- version: v24.4.0
pr-url: https://github.com/nodejs/node/pull/64397
description: The `encoding` option now defaults to `'buffer'` if `prefix` is a `Buffer`.
- version:
- v20.6.0
- v18.19.0
Expand Down Expand Up @@ -4008,7 +4014,7 @@ changes:

* `prefix` {string|Buffer|URL}
* `options` {string|Object}
* `encoding` {string} **Default:** `'utf8'`
* `encoding` {string} **Default:** `'utf8'` (or `'buffer'` if `prefix` is a `Buffer`)
* `callback` {Function}
* `err` {Error}
* `directory` {string|Buffer}
Expand Down Expand Up @@ -6490,6 +6496,9 @@ See the POSIX mkdir(2) documentation for more details.
<!-- YAML
added: v5.10.0
changes:
- version: v24.4.0
pr-url: https://github.com/nodejs/node/pull/64397
description: The `encoding` option now defaults to `'buffer'` if `prefix` is a `Buffer`.
- version:
- v20.6.0
- v18.19.0
Expand All @@ -6504,7 +6513,7 @@ changes:

* `prefix` {string|Buffer|URL}
* `options` {string|Object}
* `encoding` {string} **Default:** `'utf8'`
* `encoding` {string} **Default:** `'utf8'` (or `'buffer'` if `prefix` is a `Buffer`)
* Returns: {string|Buffer}

Returns the created directory path. If `encoding` is `'buffer'`, then the
Expand Down
12 changes: 9 additions & 3 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

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

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

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

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((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);
}));
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