Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs: make mkdtemp accept buffers and URL #48828

Merged
merged 2 commits into from
Jul 26, 2023
Merged
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
15 changes: 12 additions & 3 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -1163,14 +1163,17 @@ makeDirectory().catch(console.error);
<!-- YAML
added: v10.0.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/48828
description: The `prefix` parameter now accepts buffers and URL.
- version:
- v16.5.0
- v14.18.0
pr-url: https://github.com/nodejs/node/pull/39028
description: The `prefix` parameter now accepts an empty string.
-->

* `prefix` {string}
* `prefix` {string|Buffer|URL}
* `options` {string|Object}
* `encoding` {string} **Default:** `'utf8'`
* Returns: {Promise} Fulfills with a string containing the file system path
Expand Down Expand Up @@ -3254,6 +3257,9 @@ 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/48828
description: The `prefix` parameter now accepts buffers and URL.
- version: v18.0.0
pr-url: https://github.com/nodejs/node/pull/41678
description: Passing an invalid callback to the `callback` argument
Expand All @@ -3277,7 +3283,7 @@ changes:
description: The `callback` parameter is optional now.
-->

* `prefix` {string}
* `prefix` {string|Buffer|URL}
* `options` {string|Object}
* `encoding` {string} **Default:** `'utf8'`
* `callback` {Function}
Expand Down Expand Up @@ -5566,14 +5572,17 @@ 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/48828
description: The `prefix` parameter now accepts buffers and URL.
- version:
- v16.5.0
- v14.18.0
pr-url: https://github.com/nodejs/node/pull/39028
description: The `prefix` parameter now accepts an empty string.
-->

* `prefix` {string}
* `prefix` {string|Buffer|URL}
* `options` {string|Object}
* `encoding` {string} **Default:** `'utf8'`
* Returns: {string}
Expand Down
30 changes: 21 additions & 9 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ const {
getValidatedPath,
getValidMode,
handleErrorFromBinding,
nullCheck,
possiblyTransformPath,
preprocessSymlinkDestination,
Stats,
Expand Down Expand Up @@ -2900,7 +2899,7 @@ realpath.native = (path, options, callback) => {

/**
* Creates a unique temporary directory.
* @param {string} prefix
* @param {string | Buffer | URL} prefix
* @param {string | { encoding?: string; }} [options]
* @param {(
* err?: Error,
Expand All @@ -2912,27 +2911,40 @@ function mkdtemp(prefix, options, callback) {
callback = makeCallback(typeof options === 'function' ? options : callback);
options = getOptions(options);

validateString(prefix, 'prefix');
nullCheck(prefix, 'prefix');
prefix = getValidatedPath(prefix, 'prefix');
warnOnNonPortableTemplate(prefix);

let path;
if (typeof prefix === 'string') {
path = `${prefix}XXXXXX`;
} else {
path = Buffer.concat([prefix, Buffer.from('XXXXXX')]);
}

const req = new FSReqCallback();
req.oncomplete = callback;
binding.mkdtemp(`${prefix}XXXXXX`, options.encoding, req);
binding.mkdtemp(path, options.encoding, req);
}

/**
* Synchronously creates a unique temporary directory.
* @param {string} prefix
* @param {string | Buffer | URL} prefix
* @param {string | { encoding?: string; }} [options]
* @returns {string}
*/
function mkdtempSync(prefix, options) {
options = getOptions(options);

validateString(prefix, 'prefix');
nullCheck(prefix, 'prefix');
prefix = getValidatedPath(prefix, 'prefix');
warnOnNonPortableTemplate(prefix);
const path = `${prefix}XXXXXX`;

let path;
if (typeof prefix === 'string') {
path = `${prefix}XXXXXX`;
} else {
path = Buffer.concat([prefix, Buffer.from('XXXXXX')]);
}

const ctx = { path };
const result = binding.mkdtemp(path, options.encoding,
undefined, ctx);
Expand Down
14 changes: 10 additions & 4 deletions lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ const {
getStatsFromBinding,
getValidatedPath,
getValidMode,
nullCheck,
preprocessSymlinkDestination,
stringToFlags,
stringToSymlinkType,
Expand Down Expand Up @@ -997,10 +996,17 @@ async function realpath(path, options) {
async function mkdtemp(prefix, options) {
options = getOptions(options);

validateString(prefix, 'prefix');
nullCheck(prefix);
prefix = getValidatedPath(prefix, 'prefix');
warnOnNonPortableTemplate(prefix);
return binding.mkdtemp(`${prefix}XXXXXX`, options.encoding, kUsePromises);

let path;
if (typeof prefix === 'string') {
path = `${prefix}XXXXXX`;
} else {
path = Buffer.concat([prefix, Buffer.from('XXXXXX')]);
}

return binding.mkdtemp(path, options.encoding, kUsePromises);
}

async function writeFile(path, data, options) {
Expand Down
7 changes: 5 additions & 2 deletions lib/internal/fs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const {
StringPrototypeEndsWith,
StringPrototypeIncludes,
Symbol,
TypedArrayPrototypeAt,
TypedArrayPrototypeIncludes,
} = primordials;

Expand Down Expand Up @@ -375,7 +376,7 @@ const nullCheck = hideStackFrames((path, propName, throwError = true) => {
const err = new ERR_INVALID_ARG_VALUE(
propName,
path,
'must be a string or Uint8Array without null bytes',
'must be a string, Uint8Array, or URL without null bytes',
);
if (throwError) {
throw err;
Expand Down Expand Up @@ -749,7 +750,9 @@ let nonPortableTemplateWarn = true;
function warnOnNonPortableTemplate(template) {
// Template strings passed to the mkdtemp() family of functions should not
// end with 'X' because they are handled inconsistently across platforms.
if (nonPortableTemplateWarn && StringPrototypeEndsWith(template, 'X')) {
if (nonPortableTemplateWarn &&
((typeof template === 'string' && StringPrototypeEndsWith(template, 'X')) ||
(typeof template !== 'string' && TypedArrayPrototypeAt(template, -1) === 0x58))) {
process.emitWarning('mkdtemp() templates ending with X are not portable. ' +
'For details see: https://nodejs.org/api/fs.html');
nonPortableTemplateWarn = false;
Expand Down
Loading