@@ -1732,6 +1732,58 @@ For detailed information, see the documentation of [`fsPromises.mkdtemp()`][].
17321732The optional ` options` argument can be a string specifying an encoding, or an
17331733object with an ` encoding` property specifying the character encoding to use.
17341734
1735+ ### ` fsPromises .mkstemp (prefix[, options])`
1736+
1737+ <!-- YAML
1738+ added: REPLACEME
1739+ -->
1740+
1741+ > Stability: 1 - Experimental
1742+
1743+ * ` prefix` {string|Buffer|URL}
1744+ * ` options` {string|Object}
1745+ * ` encoding` {string} **Default:** ` ' utf8' ` (or ` ' buffer' ` if ` prefix` is a ` Buffer` )
1746+ * Returns: {Promise} Fulfills with an {Object}:
1747+ * ` path` {string|Buffer} The path of the created file.
1748+ * ` handle` {FileHandle} The created file, opened for reading and writing.
1749+
1750+ Creates and opens a unique temporary file. A unique file name is generated by
1751+ appending six random characters to the end of the provided ` prefix` . Due to
1752+ platform inconsistencies, avoid trailing ` X ` characters in ` prefix` . Some
1753+ platforms, notably the BSDs, can return more than six random characters, and
1754+ replace trailing ` X ` characters in ` prefix` with random characters.
1755+
1756+ The file is created and opened in a single operation, so another process cannot
1757+ create a file with the same name in between. On POSIX systems, the file is only
1758+ readable and writable by its owner.
1759+
1760+ The optional ` options` argument can be a string specifying an encoding, or an
1761+ object with an ` encoding` property specifying the character encoding to use for
1762+ the returned ` path` .
1763+
1764+ ` ` ` mjs
1765+ import { mkstemp } from ' node:fs/promises' ;
1766+ import { join } from ' node:path' ;
1767+ import { tmpdir } from ' node:os' ;
1768+
1769+ const { path , handle } = await mkstemp (join (tmpdir (), ' foo-' ));
1770+ try {
1771+ await handle .writeFile (' some data' );
1772+ console .log (path);
1773+ // Prints: /tmp/foo-itXde2 or C:\Users\...\AppData\Local\Temp\foo-itXde2
1774+ } finally {
1775+ await handle .close ();
1776+ }
1777+ ` ` `
1778+
1779+ The file is not removed automatically. It is the caller's responsibility to
1780+ close the {FileHandle} and to remove the file when it is no longer needed.
1781+
1782+ As with [` fsPromises .mkdtemp ()` ][], the random characters are appended directly
1783+ to ` prefix` . To create a file _within_ a directory, ` prefix` must end with a
1784+ trailing platform-specific path separator (` require (' node:path' ).sep ` ) or
1785+ include the beginning of the file name.
1786+
17351787### ` fsPromises .open (path, flags[, mode])`
17361788
17371789<!-- YAML
@@ -4099,6 +4151,66 @@ mkdtemp(`${tmpDir}${sep}`, (err, directory) => {
40994151});
41004152```
41014153
4154+ ### `fs.mkstemp(prefix[, options], callback)`
4155+
4156+ <!-- YAML
4157+ added: REPLACEME
4158+ -->
4159+
4160+ > Stability: 1 - Experimental
4161+
4162+ * `prefix` {string|Buffer|URL}
4163+ * `options` {string|Object}
4164+ * `encoding` {string} **Default:** `'utf8'` (or `'buffer'` if `prefix` is a `Buffer`)
4165+ * `callback` {Function}
4166+ * `err` {Error}
4167+ * `file` {Object}
4168+ * `path` {string|Buffer} The path of the created file.
4169+ * `fd` {integer} A file descriptor for the created file, opened for
4170+ reading and writing.
4171+
4172+ Creates and opens a unique temporary file.
4173+
4174+ Generates six random characters to be appended behind a required `prefix` to
4175+ create a unique temporary file. Due to platform inconsistencies, avoid trailing
4176+ `X` characters in `prefix`. Some platforms, notably the BSDs, can return more
4177+ than six random characters, and replace trailing `X` characters in `prefix`
4178+ with random characters.
4179+
4180+ The file is created and opened in a single operation, so another process cannot
4181+ create a file with the same name in between. On POSIX systems, the file is only
4182+ readable and writable by its owner.
4183+
4184+ The optional `options` argument can be a string specifying an encoding, or an
4185+ object with an `encoding` property specifying the character encoding to use for
4186+ the `path` passed to the callback.
4187+
4188+ ```mjs
4189+ import { mkstemp, write, close } from 'node:fs';
4190+ import { join } from 'node:path';
4191+ import { tmpdir } from 'node:os';
4192+
4193+ mkstemp(join(tmpdir(), 'foo-'), (err, file) => {
4194+ if (err) throw err;
4195+ console.log(file.path);
4196+ // Prints: /tmp/foo-itXde2 or C:\U sers\. ..\A ppData\L ocal\T emp\f oo-itXde2
4197+ write(file.fd, 'some data', (err) => {
4198+ if (err) throw err;
4199+ close(file.fd, (err) => {
4200+ if (err) throw err;
4201+ });
4202+ });
4203+ });
4204+ ```
4205+
4206+ The file is not removed automatically. It is the caller's responsibility to
4207+ close the file descriptor and to remove the file when it is no longer needed.
4208+
4209+ As with [`fs.mkdtemp()`][], the random characters are appended directly to
4210+ `prefix`. To create a file _within_ a directory, `prefix` must end with a
4211+ trailing platform-specific path separator (`require('node:path').sep`) or
4212+ include the beginning of the file name.
4213+
41024214### `fs.open(path[, flags[, mode]], callback)`
41034215
41044216<!-- YAML
@@ -6583,6 +6695,31 @@ with the [`using`][] syntax.
65836695The optional ` options` argument can be a string specifying an encoding, or an
65846696object with an ` encoding` property specifying the character encoding to use.
65856697
6698+ ### ` fs .mkstempSync (prefix[, options])`
6699+
6700+ <!-- YAML
6701+ added: REPLACEME
6702+ -->
6703+
6704+ > Stability: 1 - Experimental
6705+
6706+ * ` prefix` {string|Buffer|URL}
6707+ * ` options` {string|Object}
6708+ * ` encoding` {string} **Default:** ` ' utf8' ` (or ` ' buffer' ` if ` prefix` is a ` Buffer` )
6709+ * Returns: {Object}
6710+ * ` path` {string|Buffer} The path of the created file.
6711+ * ` fd` {integer} A file descriptor for the created file, opened for reading
6712+ and writing.
6713+
6714+ Synchronously creates and opens a unique temporary file.
6715+
6716+ For detailed information, see the documentation of the asynchronous version of
6717+ this API: [` fs .mkstemp ()` ][].
6718+
6719+ The optional ` options` argument can be a string specifying an encoding, or an
6720+ object with an ` encoding` property specifying the character encoding to use for
6721+ the returned ` path` .
6722+
65866723### ` fs .openAsBlobSync (path[, options])`
65876724
65886725<!-- YAML
@@ -9610,6 +9747,7 @@ the file contents.
96109747[`fs.lutimes()`]: #fslutimespath-atime-mtime-callback
96119748[`fs.mkdir()`]: #fsmkdirpath-options-callback
96129749[`fs.mkdtemp()`]: #fsmkdtempprefix-options-callback
9750+ [`fs.mkstemp()`]: #fsmkstempprefix-options-callback
96139751[`fs.open()`]: #fsopenpath-flags-mode-callback
96149752[`fs.openAsBlob()`]: #fsopenasblobpath-options
96159753[`fs.opendir()`]: #fsopendirpath-options-callback
0 commit comments