Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 36.4k
fs: add mkstemp functions#33549
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
Closed
Uh oh!
There was an error while loading. Please reload this page.
Closed
fs: add mkstemp functions #33549
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8ea7bb6
fs: add mkstemp functions
targos 308b6f9
fixup! fs: add mkstemp functions
targos e047588
fixup! fixup! fs: add mkstemp functions
targos 744a476
fixup! fixup! fixup! fs: add mkstemp functions
targos bf9ce89
fixup! fixup! fixup! fixup! fs: add mkstemp functions
targos f57b726
Update doc/api/fs.md
targos 52da908
Update doc/api/fs.md
targos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -2677,6 +2677,70 @@ this API: [`fs.mkdtemp()`][]. | ||
| The optional `options` argument can be a string specifying an encoding, or an | ||
| object with an `encoding` property specifying the character encoding to use. | ||
| ## `fs.mkstemp(prefix[, options], callback)` | ||
| <!-- YAML | ||
| added: REPLACEME | ||
| --> | ||
| * `prefix` {string} | ||
| * `options` {string|Object} | ||
| * `encoding` {string} **Default:** `'utf8'` | ||
| * `callback` {Function} | ||
| * `err` {Error} | ||
| * `file` {Object} | ||
| * `path` {string} | ||
| * `fd` {number} | ||
| Creates and opens a unique temporary file. | ||
| Generates six random characters to be appended behind a required `prefix` to | ||
| create a unique temporary file. Due to platform 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 file path and file descriptor are passed as an object to the | ||
| callback's second parameter. | ||
| The optional `options` argument can be a string specifying an encoding, or an | ||
| object with an `encoding` property specifying the character encoding to use. | ||
| ```js | ||
| fs.mkstemp(path.join(os.tmpdir(), 'foo-'), (err, result) => { | ||
| if (err) throw err; | ||
| console.log(result.path); | ||
| // Prints: /tmp/foo-itXde2 or C:\Users\...\AppData\Local\Temp\foo-itXde2 | ||
| fs.close(result.fd, (err) => { | ||
| if (err) throw err; | ||
| }); | ||
| }); | ||
| ``` | ||
| The `fs.mkstemp()` method will append the six randomly selected characters | ||
| directly to the `prefix` string. For instance, given a directory `/tmp`, if the | ||
| intention is to create a temporary file *within* `/tmp`, the `prefix` must end | ||
| with a trailing platform-specific path separator (`require('path').sep`). | ||
| ## `fs.mkstempSync(prefix[, options])` | ||
| <!-- YAML | ||
| added: REPLACEME | ||
| --> | ||
| * `prefix` {string} | ||
| * `options` {string|Object} | ||
| * `encoding` {string} **Default:** `'utf8'` | ||
| * Returns: {Object} | ||
| * `path` {string} | ||
| * `fd` {number} | ||
| Returns an object with the created file path and file descriptor. | ||
| For detailed information, see the documentation of the asynchronous version of | ||
| this API: [`fs.mkstemp()`][]. | ||
| The optional `options` argument can be a string specifying an encoding, or an | ||
| object with an `encoding` property specifying the character encoding to use. | ||
| ## `fs.open(path[, flags[, mode]], callback)` | ||
| <!-- YAML | ||
| added: v0.0.2 | ||
| @@ -5089,6 +5153,44 @@ characters directly to the `prefix` string. For instance, given a directory | ||
| `prefix` must end with a trailing platform-specific path separator | ||
| (`require('path').sep`). | ||
| ### `fsPromises.mkstemp(prefix[, options])` | ||
| <!-- YAML | ||
| added: REPLACEME | ||
| --> | ||
| * `prefix` {string} | ||
| * `options` {string|Object} | ||
| * `encoding` {string} **Default:** `'utf8'` | ||
| * Returns: {Promise} | ||
| Creates and opens a unique empty temporary file, and fulfills the `Promise` with | ||
| an object that contains the created file path and a `FileHandle` object. A | ||
| unique file name is generated by appending six random characters to the end of | ||
| the provided `prefix`. Due to platform 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 optional `options` argument can be a string specifying an encoding, or an | ||
| object with an `encoding` property specifying the character encoding to use. | ||
| ```js | ||
| async function createRandomFile() { | ||
| const result = await fsPromises.mkstemp(path.join(os.tmpdir(), 'foo-')); | ||
| console.log(result.path); | ||
| // Prints: /tmp/foo-itXde2 or C:\Users\...\AppData\Local\Temp\foo-itXde2 | ||
| await result.handle.close(); | ||
targos marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| // Closes the file descriptor but does not remove the file. | ||
| } | ||
| createRandomFile().catch(console.error); | ||
| ``` | ||
| The `fsPromises.mkstemp()` method will append the six randomly selected | ||
| characters directly to the `prefix` string. For instance, given a directory | ||
| `/tmp`, if the intention is to create a temporary file *within* `/tmp`, the | ||
| `prefix` must end with a trailing platform-specific path separator | ||
| (`require('path').sep`). | ||
| ### `fsPromises.open(path, flags[, mode])` | ||
| <!-- YAML | ||
| added: v10.0.0 | ||
| @@ -5828,6 +5930,7 @@ the file contents. | ||
| [`fs.lstat()`]: #fs_fs_lstat_path_options_callback | ||
| [`fs.mkdir()`]: #fs_fs_mkdir_path_options_callback | ||
| [`fs.mkdtemp()`]: #fs_fs_mkdtemp_prefix_options_callback | ||
| [`fs.mkstemp()`]: #fs_fs_mkstemp_prefix_options_callback | ||
| [`fs.open()`]: #fs_fs_open_path_flags_mode_callback | ||
| [`fs.opendir()`]: #fs_fs_opendir_path_options_callback | ||
| [`fs.opendirSync()`]: #fs_fs_opendirsync_path_options | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.