Security News
NVD Backlog Tops 20,000 CVEs Awaiting Analysis as NIST Prepares System Updates
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
The 'temp' npm package is designed to handle temporary files and directories in a Node.js environment. It provides utilities to create, manage, and clean up temporary files and directories, ensuring that they are properly removed when no longer needed.
Creating a Temporary File
This feature allows you to create a temporary file with a specified prefix. The callback provides information about the file, including its path and file descriptor.
const temp = require('temp');
temp.open('myprefix', function(err, info) {
if (!err) {
console.log('File: ', info.path);
console.log('File descriptor: ', info.fd);
}
});
Creating a Temporary Directory
This feature allows you to create a temporary directory with a specified prefix. The callback provides the path to the created directory.
const temp = require('temp');
temp.mkdir('myprefix', function(err, dirPath) {
if (!err) {
console.log('Directory: ', dirPath);
}
});
Automatic Cleanup
This feature enables automatic cleanup of temporary files and directories when the process exits. By calling `temp.track()`, you ensure that all temporary files and directories are removed when they are no longer needed.
const temp = require('temp');
temp.track();
temp.open('myprefix', function(err, info) {
if (!err) {
console.log('File: ', info.path);
console.log('File descriptor: ', info.fd);
}
});
The 'tmp' package provides similar functionality for creating temporary files and directories. It also supports automatic cleanup and offers a more modern API with promises and async/await support. Compared to 'temp', 'tmp' is more actively maintained and has a larger user base.
The 'fs-extra' package extends the native 'fs' module with additional methods, including utilities for handling temporary files and directories. While it is not solely focused on temporary file management, it provides a comprehensive set of file system utilities that can be useful in various scenarios.
Handles generating a unique file/directory name under the appropriate system temporary directory, changing the file to an appropriate mode, and supports automatic removal.
temp
has a similar API to the fs
module.
You can create temporary files with open
and openSync
, temporary
directories with mkdir
and mkdirSync
, or you can get a unique name
in the system temporary directory with path
.
Working copies of the following examples can be found under the
examples
directory.
To create a temporary file use open
or openSync
, passing
them an optional prefix, suffix, or both (see below for details on
affixes). The object passed to the callback (or returned) has
path
and fd
keys:
{ path: "/path/to/file",
, fd: theFileDescriptor
}
In this example we write to a temporary file and call out to grep
and
wc -l
to determine the number of time foo
occurs in the text. The
temporary file is chmod'd 0600
and cleaned up automatically when the
process at exit:
var temp = require('temp'),
fs = require('fs'),
util = require('util'),
exec = require('child_process').exec;
// Fake data
var myData = "foo\nbar\nfoo\nbaz";
// Process the data (note: error handling omitted)
temp.open('myprefix', function(err, info) {
fs.write(info.fd, myData);
fs.close(info.fd, function(err) {
exec("grep foo '" + info.path + "' | wc -l", function(err, stdout) {
util.puts(stdout.trim());
});
});
});
To create a temporary directory, use mkdir
or mkdirSync
, passing
it an optional prefix, suffix, or both (see below for details on affixes).
In this example we create a temporary directory, write to a file within it, call out to an external program to create a PDF, and read the result. While the external process creates a lot of additional files, the temporary directory is removed automatically at exit:
var temp = require('../lib/temp'),
fs = require('fs'),
util = require('util'),
path = require('path'),
exec = require('child_process').exec;
// For use with ConTeXt, http://wiki.contextgarden.net
var myData = "\\starttext\nHello World\n\\stoptext";
temp.mkdir('pdfcreator', function(err, dirPath) {
var inputPath = path.join(dirPath, 'input.tex')
fs.writeFile(inputPath, myData, function(err) {
if (err) throw err;
process.chdir(dirPath);
exec("texexec '" + inputPath + "'", function(err) {
if (err) throw err;
fs.readFile(path.join(dirPath, 'input.pdf'), function(err, data) {
if (err) throw err;
sys.print(data);
});
});
});
});
To create a temporary WriteStream, use 'createWriteStream', which sits
on top of fs.createWriteStream
. The return value is a
fs.WriteStream
whose path
is registered for removal when
temp.cleanup
is called.
var temp = require('temp');
var stream = temp.createWriteStream();
stream.write("Some data");
// Maybe do some other things
stream.end();
You can provide custom prefixes and suffixes when creating temporary
files and directories. If you provide a string, it is used as the prefix
for the temporary name. If you provide an object with prefix
and
suffix
keys, they are used for the temporary name.
Here are some examples:
"aprefix"
: A simple prefix, prepended to the filename; this is
shorthand for:{prefix: "aprefix"}
: A simple prefix, prepended to the filename{suffix: ".asuffix"}
: A suffix, appended to the filename
(especially useful when the file needs to be named with specific
extension for use with an external program).{prefix: "myprefix", suffix: "mysuffix"}
: Customize both affixesnull
: Use the defaults for files and directories (prefixes "f-"
and "d-"
, respectively, no suffixes).In this simple example we read a pdf
, write it to a temporary file with
a .pdf
extension, and close it.
var fs = require('fs'),
temp = require('temp');
fs.readFile('/path/to/source.pdf', function(err, data) {
temp.open({suffix: '.pdf'}, function(err, info) {
if (err) throw err;
fs.write(info.fd, contents);
fs.close(info.fd, function(err) {
if (err) throw err;
// Do something with the file
});
});
});
If you just want a unique name in your temporary directory, use
path
:
var fs = require('fs');
var tempName = temp.path({suffix: '.pdf'});
// Do something with tempName
Note: The file isn't created for you, and the mode is not changed -- and it
will not be removed automatically at exit. If you use path
, it's
all up to you.
Works with Node.js v0.6.0 on OS X. Please let me know if you have problems running it on a later version of Node.js or have platform problems.
Install it using npm:
$ npm install temp
Or get it directly from: http://github.com/bruce/node-temp
For now, run test/test-temp.js
:
$ node test/test-temp.js
You can find the repository at: http://github.com/bruce/node-temp
Issues/Feature Requests can be submitted at: http://github.com/bruce/node-temp/issues
I'd really like to hear your feedback, and I'd love to receive your pull-requests!
Copyright (c) 2010-2012 Bruce Williams. See LICENSE for details.
FAQs
Temporary files and directories
The npm package temp receives a total of 4,619,598 weekly downloads. As such, temp popularity was classified as popular.
We found that temp demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers 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.
Security News
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
Security News
Research
A malicious npm package disguised as a WhatsApp client is exploiting authentication flows with a remote kill switch to exfiltrate data and destroy files.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.