@@ -1726,6 +1726,56 @@ For detailed information, see the documentation of [`fsPromises.mkdtemp()`][].
17261726The optional ` options` argument can be a string specifying an encoding, or an
17271727object with an ` encoding` property specifying the character encoding to use.
17281728
1729+ ### ` fsPromises .mkstemp (prefix[, options])`
1730+
1731+ <!-- YAML
1732+ added: REPLACEME
1733+ -->
1734+
1735+ * ` prefix` {string|Buffer|URL}
1736+ * ` options` {string|Object}
1737+ * ` encoding` {string} **Default:** ` ' utf8' ` (or ` ' buffer' ` if ` prefix` is a ` Buffer` )
1738+ * Returns: {Promise} Fulfills with an {Object}:
1739+ * ` path` {string|Buffer} The path of the created file.
1740+ * ` handle` {FileHandle} The created file, opened for reading and writing.
1741+
1742+ Creates and opens a unique temporary file. A unique file name is generated by
1743+ appending six random characters to the end of the provided ` prefix` . Due to
1744+ platform inconsistencies, avoid trailing ` X ` characters in ` prefix` . Some
1745+ platforms, notably the BSDs, can return more than six random characters, and
1746+ replace trailing ` X ` characters in ` prefix` with random characters.
1747+
1748+ The file is created and opened in a single operation, so another process cannot
1749+ create a file with the same name in between. On POSIX systems, the file is only
1750+ readable and writable by its owner.
1751+
1752+ The optional ` options` argument can be a string specifying an encoding, or an
1753+ object with an ` encoding` property specifying the character encoding to use for
1754+ the returned ` path` .
1755+
1756+ ` ` ` mjs
1757+ import { mkstemp } from ' node:fs/promises' ;
1758+ import { join } from ' node:path' ;
1759+ import { tmpdir } from ' node:os' ;
1760+
1761+ const { path , handle } = await mkstemp (join (tmpdir (), ' foo-' ));
1762+ try {
1763+ await handle .writeFile (' some data' );
1764+ console .log (path);
1765+ // Prints: /tmp/foo-itXde2 or C:\Users\...\AppData\Local\Temp\foo-itXde2
1766+ } finally {
1767+ await handle .close ();
1768+ }
1769+ ` ` `
1770+
1771+ The file is not removed automatically. It is the caller's responsibility to
1772+ close the {FileHandle} and to remove the file when it is no longer needed.
1773+
1774+ As with [` fsPromises .mkdtemp ()` ][], the random characters are appended directly
1775+ to ` prefix` . To create a file _within_ a directory, ` prefix` must end with a
1776+ trailing platform-specific path separator (` require (' node:path' ).sep ` ) or
1777+ include the beginning of the file name.
1778+
17291779### ` fsPromises .open (path, flags[, mode])`
17301780
17311781<!-- YAML
@@ -4093,6 +4143,64 @@ mkdtemp(`${tmpDir}${sep}`, (err, directory) => {
40934143});
40944144```
40954145
4146+ ### `fs.mkstemp(prefix[, options], callback)`
4147+
4148+ <!-- YAML
4149+ added: REPLACEME
4150+ -->
4151+
4152+ * `prefix` {string|Buffer|URL}
4153+ * `options` {string|Object}
4154+ * `encoding` {string} **Default:** `'utf8'` (or `'buffer'` if `prefix` is a `Buffer`)
4155+ * `callback` {Function}
4156+ * `err` {Error}
4157+ * `file` {Object}
4158+ * `path` {string|Buffer} The path of the created file.
4159+ * `fd` {integer} A file descriptor for the created file, opened for
4160+ reading and writing.
4161+
4162+ Creates and opens a unique temporary file.
4163+
4164+ Generates six random characters to be appended behind a required `prefix` to
4165+ create a unique temporary file. Due to platform inconsistencies, avoid trailing
4166+ `X` characters in `prefix`. Some platforms, notably the BSDs, can return more
4167+ than six random characters, and replace trailing `X` characters in `prefix`
4168+ with random characters.
4169+
4170+ The file is created and opened in a single operation, so another process cannot
4171+ create a file with the same name in between. On POSIX systems, the file is only
4172+ readable and writable by its owner.
4173+
4174+ The optional `options` argument can be a string specifying an encoding, or an
4175+ object with an `encoding` property specifying the character encoding to use for
4176+ the `path` passed to the callback.
4177+
4178+ ```mjs
4179+ import { mkstemp, write, close } from 'node:fs';
4180+ import { join } from 'node:path';
4181+ import { tmpdir } from 'node:os';
4182+
4183+ mkstemp(join(tmpdir(), 'foo-'), (err, file) => {
4184+ if (err) throw err;
4185+ console.log(file.path);
4186+ // Prints: /tmp/foo-itXde2 or C:\U sers\. ..\A ppData\L ocal\T emp\f oo-itXde2
4187+ write(file.fd, 'some data', (err) => {
4188+ if (err) throw err;
4189+ close(file.fd, (err) => {
4190+ if (err) throw err;
4191+ });
4192+ });
4193+ });
4194+ ```
4195+
4196+ The file is not removed automatically. It is the caller's responsibility to
4197+ close the file descriptor and to remove the file when it is no longer needed.
4198+
4199+ As with [`fs.mkdtemp()`][], the random characters are appended directly to
4200+ `prefix`. To create a file _within_ a directory, `prefix` must end with a
4201+ trailing platform-specific path separator (`require('node:path').sep`) or
4202+ include the beginning of the file name.
4203+
40964204### `fs.open(path[, flags[, mode]], callback)`
40974205
40984206<!-- YAML
@@ -6577,6 +6685,29 @@ with the [`using`][] syntax.
65776685The optional ` options` argument can be a string specifying an encoding, or an
65786686object with an ` encoding` property specifying the character encoding to use.
65796687
6688+ ### ` fs .mkstempSync (prefix[, options])`
6689+
6690+ <!-- YAML
6691+ added: REPLACEME
6692+ -->
6693+
6694+ * ` prefix` {string|Buffer|URL}
6695+ * ` options` {string|Object}
6696+ * ` encoding` {string} **Default:** ` ' utf8' ` (or ` ' buffer' ` if ` prefix` is a ` Buffer` )
6697+ * Returns: {Object}
6698+ * ` path` {string|Buffer} The path of the created file.
6699+ * ` fd` {integer} A file descriptor for the created file, opened for reading
6700+ and writing.
6701+
6702+ Synchronously creates and opens a unique temporary file.
6703+
6704+ For detailed information, see the documentation of the asynchronous version of
6705+ this API: [` fs .mkstemp ()` ][].
6706+
6707+ The optional ` options` argument can be a string specifying an encoding, or an
6708+ object with an ` encoding` property specifying the character encoding to use for
6709+ the returned ` path` .
6710+
65806711### ` fs .openAsBlobSync (path[, options])`
65816712
65826713<!-- YAML
@@ -9597,6 +9728,7 @@ the file contents.
95979728[`fs.lutimes()`]: #fslutimespath-atime-mtime-callback
95989729[`fs.mkdir()`]: #fsmkdirpath-options-callback
95999730[`fs.mkdtemp()`]: #fsmkdtempprefix-options-callback
9731+ [`fs.mkstemp()`]: #fsmkstempprefix-options-callback
96009732[`fs.open()`]: #fsopenpath-flags-mode-callback
96019733[`fs.openAsBlob()`]: #fsopenasblobpath-options
96029734[`fs.opendir()`]: #fsopendirpath-options-callback
0 commit comments