Product
Introducing Java Support in Socket
We're excited to announce that Socket now supports the Java programming language.
The 'temp-fs' npm package provides a simple and efficient way to create and manage temporary files and directories. It is particularly useful for applications that need to handle temporary data storage, such as during testing or when processing files that do not need to be permanently stored.
Create Temporary File
This feature allows you to create a temporary file. The file will be automatically deleted when the process exits.
const temp = require('temp-fs');
const tempFile = temp.createFile();
console.log('Temporary file created at:', tempFile.path);
Create Temporary Directory
This feature allows you to create a temporary directory. The directory will be automatically deleted when the process exits.
const temp = require('temp-fs');
const tempDir = temp.createDir();
console.log('Temporary directory created at:', tempDir.path);
Custom Cleanup
This feature allows you to create temporary files or directories with custom cleanup options. You can choose to keep the file or directory and manually clean it up later.
const temp = require('temp-fs');
const tempFile = temp.createFile({ keep: true });
console.log('Temporary file created at:', tempFile.path);
// Manually cleanup
tempFile.cleanupSync();
'tmp' is another npm package for creating temporary files and directories. It offers similar functionality to 'temp-fs' but with a slightly different API. 'tmp' also provides automatic cleanup of temporary files and directories.
'temp' is a package that provides temporary file and directory creation with automatic cleanup. It is similar to 'temp-fs' but has a more minimalistic API and fewer configuration options.
'fs-extra' extends the native Node.js 'fs' module with additional methods, including methods for creating temporary files and directories. While 'fs-extra' is more general-purpose, it can be used to achieve similar functionality to 'temp-fs'.
A temporary file and directory creator for io.js and Node.js™.
Just like raszi/node-tmp and bruce/node-temp, it can safely create temporary files and directories without worrying a lot of about race conditions as long as you don't do some tricky things. ;-) You can also let this module track the files or directories you created and delete them when the program exits.
npm install temp-fs
var tempfs = require('temp-fs');
// Create a tempfile in the system-provided tempdir.
tempfs.open(function (err, file) {
if (err) { throw err; }
console.log(file.path, file.fd);
// async
file.unlink(function () {
console.log('File delected');
});
// sync
// No problem even if unlink() is called twice.
file.unlink();
});
// Create a tempdir in current directory.
tempfs.mkdir({
dir: '.',
recursive: true, // It and its content will be remove recursively.
track: true // Track this directory.
}, function (err, dir) {
if (err) { throw err; }
console.log(dir.path, dir.recursive);
throw new Error('Since it is tracked, tempfs will remove it for you.');
dir.unlink();
});
limit: Number
The maximum number of chance to retry before throwing an error. It should be a finite number. Default: 5
recursive: Boolean
Whether unlink()
should remove a directory recursively. Default: false
track: Boolean
If set to true
, let temp-fs manage the the current file/directory for you
even if the global tracking is off. If set to false
, don't let temp-fs
manage it even if the global tracking is on. Otherwise, use the current
global setting.
mode: Number
File mode (default: 0600) or directory mode (default: 0700) to use.
name: String
If set, join the two paths options.dir || tempfs.dir()
and
options.name
together and use the result as the customized
filename/pathname.
dir: String
Where to put the generated tempfile or tempdir. Also see options.name
above. Default: tempfs.dir()
prefix: String
The prefix for the generated random name. Default: "tmp-"
suffix: String
The suffix for the generated random name. Default: ""
template: String
A string containing some capital letters Xs for substitution with random
characters. Then it is used as part of the filename/dirname. Just like what
you do with the mktemp (3)
function in the C library.
Use it to switch global files/directories tracking on or off. Turn it on if you don't want to manually delete everything. When it is turned off, all recorded files and directories will not be removed but still kept in case it is turned on again before the program exits.
This switch does not affect manually tracked files through options.track
.
They will be removed automatically on exit.
Note: When an uncaught exception occurs, all tracked temporary files and directories will be removed no matter it is on or off.
Return the path of a system-provided tempdir as require('os').tmpdir()
does.
You should not make any assumption about whether the path contains a trailing
path separator, or it is a real path. On most system it is not a fixed path,
and it can be changed by the user environment. When in doubt, check it first.
Return a customized/random filename/dirname. Options are documented at options.
Try to open a unique tempfile asynchronously. The callback function receives
two arguments error
and file
. If error
is null, file
has these
properties:
path
: The absolute path to the tempfile.fd
: An integer file descriptor.unlink
: A special function for you to delete the file. If you invoke it
with a callback function, it will become asynchronous. If the file is not
tracked, it may throw when an error occurs or the first argument of the
callback function will be an Error object.The synchronous version of tempfs.open
. It will throw when an error happens.
Try to create a new tempdir asynchronously. The callback function receives two
arguments error
and dir
. If error
is null, dir
has these properties:
path
: The absolute path to the tempdir.recursive
: Whether unlink() will remove the tempdir recursively.unlink
: A special function for you to remove the directory. If you
invoke it with a callback function, it will become asynchronous. If the
directory is not tracked, it may throw when an error occurs or the first
argument of the callback function will be an Error object.The synchronous version of tempfs.mkdir
. It will throw when an error happens.
Remove all tracked files and directories asynchronously.
Remove all tracked files and directories synchronously.
The MIT License (MIT)
FAQs
Temporary files, directories or names!
The npm package temp-fs receives a total of 203,401 weekly downloads. As such, temp-fs popularity was classified as popular.
We found that temp-fs demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
We're excited to announce that Socket now supports the Java programming language.
Security News
Socket detected a malicious Python package impersonating a popular browser cookie library to steal passwords, screenshots, webcam images, and Discord tokens.
Security News
Deno 2.0 is now available with enhanced package management, full Node.js and npm compatibility, improved performance, and support for major JavaScript frameworks.