A simple temporary file and directory creator for \href{http://nodejs.org/}{\texttt{ node.\+js.}}

\href{https://travis-ci.org/raszi/node-tmp}{\texttt{ }} \href{https://david-dm.org/raszi/node-tmp}{\texttt{ }} \href{https://badge.fury.io/js/tmp}{\texttt{ }} \href{https://raszi.github.io/node-tmp/}{\texttt{ }} \href{https://snyk.io/test/npm/tmp}{\texttt{ }}\hypertarget{md_sfcode_node_modules_tmp_README_autotoc_md30456}{}\doxysection{About}\label{md_sfcode_node_modules_tmp_README_autotoc_md30456}
This is a \href{https://www.npmjs.com/browse/depended/tmp}{\texttt{ widely used library}} to create temporary files and directories in a \href{http://nodejs.org/}{\texttt{ node.\+js}} environment.

Tmp offers both an asynchronous and a synchronous API. For all API calls, all the parameters are optional. There also exists a promisified version of the API, see (5) under references below.

Tmp uses crypto for determining random file names, or, when using templates, a six letter random identifier. And just in case that you do not have that much entropy left on your system, Tmp will fall back to pseudo random numbers.

You can set whether you want to remove the temporary file on process exit or not, and the destination directory can also be set.\hypertarget{md_sfcode_node_modules_tmp_README_autotoc_md30457}{}\doxysection{How to install}\label{md_sfcode_node_modules_tmp_README_autotoc_md30457}

\begin{DoxyCode}{0}
\DoxyCodeLine{npm install tmp }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_tmp_README_autotoc_md30458}{}\doxysection{Usage}\label{md_sfcode_node_modules_tmp_README_autotoc_md30458}
Please also check \href{https://raszi.github.io/node-tmp/}{\texttt{ API docs}}.\hypertarget{md_sfcode_node_modules_tmp_README_autotoc_md30459}{}\doxysubsection{Asynchronous file creation}\label{md_sfcode_node_modules_tmp_README_autotoc_md30459}
Simple temporary file creation, the file will be closed and unlinked on process exit.


\begin{DoxyCode}{0}
\DoxyCodeLine{var tmp = require('tmp');}
\DoxyCodeLine{}
\DoxyCodeLine{tmp.file(function \_tempFileCreated(err, path, fd, cleanupCallback) \{}
\DoxyCodeLine{  if (err) throw err;}
\DoxyCodeLine{}
\DoxyCodeLine{  console.log('File: ', path);}
\DoxyCodeLine{  console.log('Filedescriptor: ', fd);}
\DoxyCodeLine{  }
\DoxyCodeLine{  // If we don't need the file anymore we could manually call the cleanupCallback}
\DoxyCodeLine{  // But that is not necessary if we didn't pass the keep option because the library}
\DoxyCodeLine{  // will clean after itself.}
\DoxyCodeLine{  cleanupCallback();}
\DoxyCodeLine{\}); }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_tmp_README_autotoc_md30460}{}\doxysubsection{Synchronous file creation}\label{md_sfcode_node_modules_tmp_README_autotoc_md30460}
A synchronous version of the above.


\begin{DoxyCode}{0}
\DoxyCodeLine{var tmp = require('tmp');}
\DoxyCodeLine{}
\DoxyCodeLine{var tmpobj = tmp.fileSync();}
\DoxyCodeLine{console.log('File: ', tmpobj.name);}
\DoxyCodeLine{console.log('Filedescriptor: ', tmpobj.fd);}
\DoxyCodeLine{  }
\DoxyCodeLine{// If we don't need the file anymore we could manually call the removeCallback}
\DoxyCodeLine{// But that is not necessary if we didn't pass the keep option because the library}
\DoxyCodeLine{// will clean after itself.}
\DoxyCodeLine{tmpobj.removeCallback(); }

\end{DoxyCode}


Note that this might throw an exception if either the maximum limit of retries for creating a temporary name fails, or, in case that you do not have the permission to write to the directory where the temporary file should be created in.\hypertarget{md_sfcode_node_modules_tmp_README_autotoc_md30461}{}\doxysubsection{Asynchronous directory creation}\label{md_sfcode_node_modules_tmp_README_autotoc_md30461}
Simple temporary directory creation, it will be removed on process exit.

If the directory still contains items on process exit, then it won\textquotesingle{}t be removed.


\begin{DoxyCode}{0}
\DoxyCodeLine{var tmp = require('tmp');}
\DoxyCodeLine{}
\DoxyCodeLine{tmp.dir(function \_tempDirCreated(err, path, cleanupCallback) \{}
\DoxyCodeLine{  if (err) throw err;}
\DoxyCodeLine{}
\DoxyCodeLine{  console.log('Dir: ', path);}
\DoxyCodeLine{  }
\DoxyCodeLine{  // Manual cleanup}
\DoxyCodeLine{  cleanupCallback();}
\DoxyCodeLine{\}); }

\end{DoxyCode}


If you want to cleanup the directory even when there are entries in it, then you can pass the {\ttfamily unsafe\+Cleanup} option when creating it.\hypertarget{md_sfcode_node_modules_tmp_README_autotoc_md30462}{}\doxysubsection{Synchronous directory creation}\label{md_sfcode_node_modules_tmp_README_autotoc_md30462}
A synchronous version of the above.


\begin{DoxyCode}{0}
\DoxyCodeLine{var tmp = require('tmp');}
\DoxyCodeLine{}
\DoxyCodeLine{var tmpobj = tmp.dirSync();}
\DoxyCodeLine{console.log('Dir: ', tmpobj.name);}
\DoxyCodeLine{// Manual cleanup}
\DoxyCodeLine{tmpobj.removeCallback(); }

\end{DoxyCode}


Note that this might throw an exception if either the maximum limit of retries for creating a temporary name fails, or, in case that you do not have the permission to write to the directory where the temporary directory should be created in.\hypertarget{md_sfcode_node_modules_tmp_README_autotoc_md30463}{}\doxysubsection{Asynchronous filename generation}\label{md_sfcode_node_modules_tmp_README_autotoc_md30463}
It is possible with this library to generate a unique filename in the specified directory.


\begin{DoxyCode}{0}
\DoxyCodeLine{var tmp = require('tmp');}
\DoxyCodeLine{}
\DoxyCodeLine{tmp.tmpName(function \_tempNameGenerated(err, path) \{}
\DoxyCodeLine{    if (err) throw err;}
\DoxyCodeLine{}
\DoxyCodeLine{    console.log('Created temporary filename: ', path);}
\DoxyCodeLine{\}); }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_tmp_README_autotoc_md30464}{}\doxysubsection{Synchronous filename generation}\label{md_sfcode_node_modules_tmp_README_autotoc_md30464}
A synchronous version of the above.


\begin{DoxyCode}{0}
\DoxyCodeLine{var tmp = require('tmp');}
\DoxyCodeLine{}
\DoxyCodeLine{var name = tmp.tmpNameSync();}
\DoxyCodeLine{console.log('Created temporary filename: ', name); }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_tmp_README_autotoc_md30465}{}\doxysection{Advanced usage}\label{md_sfcode_node_modules_tmp_README_autotoc_md30465}
\hypertarget{md_sfcode_node_modules_tmp_README_autotoc_md30466}{}\doxysubsection{Asynchronous file creation}\label{md_sfcode_node_modules_tmp_README_autotoc_md30466}
Creates a file with mode {\ttfamily 0644}, prefix will be {\ttfamily prefix-\/} and postfix will be {\ttfamily .txt}.


\begin{DoxyCode}{0}
\DoxyCodeLine{var tmp = require('tmp');}
\DoxyCodeLine{}
\DoxyCodeLine{tmp.file(\{ mode: 0644, prefix: 'prefix-\/', postfix: '.txt' \}, function \_tempFileCreated(err, path, fd) \{}
\DoxyCodeLine{  if (err) throw err;}
\DoxyCodeLine{}
\DoxyCodeLine{  console.log('File: ', path);}
\DoxyCodeLine{  console.log('Filedescriptor: ', fd);}
\DoxyCodeLine{\}); }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_tmp_README_autotoc_md30467}{}\doxysubsection{Synchronous file creation}\label{md_sfcode_node_modules_tmp_README_autotoc_md30467}
A synchronous version of the above.


\begin{DoxyCode}{0}
\DoxyCodeLine{var tmp = require('tmp');}
\DoxyCodeLine{}
\DoxyCodeLine{var tmpobj = tmp.fileSync(\{ mode: 0644, prefix: 'prefix-\/', postfix: '.txt' \});}
\DoxyCodeLine{console.log('File: ', tmpobj.name);}
\DoxyCodeLine{console.log('Filedescriptor: ', tmpobj.fd); }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_tmp_README_autotoc_md30468}{}\doxysubsection{Controlling the Descriptor}\label{md_sfcode_node_modules_tmp_README_autotoc_md30468}
As a side effect of creating a unique file {\ttfamily tmp} gets a file descriptor that is returned to the user as the {\ttfamily fd} parameter. The descriptor may be used by the application and is closed when the {\ttfamily remove\+Callback} is invoked.

In some use cases the application does not need the descriptor, needs to close it without removing the file, or needs to remove the file without closing the descriptor. Two options control how the descriptor is managed\+:


\begin{DoxyItemize}
\item {\ttfamily discard\+Descriptor} -\/ if {\ttfamily true} causes {\ttfamily tmp} to close the descriptor after the file is created. In this case the {\ttfamily fd} parameter is undefined.
\item {\ttfamily detach\+Descriptor} -\/ if {\ttfamily true} causes {\ttfamily tmp} to return the descriptor in the {\ttfamily fd} parameter, but it is the application\textquotesingle{}s responsibility to close it when it is no longer needed.
\end{DoxyItemize}


\begin{DoxyCode}{0}
\DoxyCodeLine{var tmp = require('tmp');}
\DoxyCodeLine{}
\DoxyCodeLine{tmp.file(\{ discardDescriptor: true \}, function \_tempFileCreated(err, path, fd, cleanupCallback) \{}
\DoxyCodeLine{  if (err) throw err;}
\DoxyCodeLine{  // fd will be undefined, allowing application to use fs.createReadStream(path)}
\DoxyCodeLine{  // without holding an unused descriptor open.}
\DoxyCodeLine{\}); }

\end{DoxyCode}



\begin{DoxyCode}{0}
\DoxyCodeLine{var tmp = require('tmp');}
\DoxyCodeLine{}
\DoxyCodeLine{tmp.file(\{ detachDescriptor: true \}, function \_tempFileCreated(err, path, fd, cleanupCallback) \{}
\DoxyCodeLine{  if (err) throw err;}
\DoxyCodeLine{}
\DoxyCodeLine{  cleanupCallback();}
\DoxyCodeLine{  // Application can store data through fd here; the space used will automatically}
\DoxyCodeLine{  // be reclaimed by the operating system when the descriptor is closed or program}
\DoxyCodeLine{  // terminates.}
\DoxyCodeLine{\}); }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_tmp_README_autotoc_md30469}{}\doxysubsection{Asynchronous directory creation}\label{md_sfcode_node_modules_tmp_README_autotoc_md30469}
Creates a directory with mode {\ttfamily 0755}, prefix will be {\ttfamily my\+Tmp\+Dir\+\_\+}.


\begin{DoxyCode}{0}
\DoxyCodeLine{var tmp = require('tmp');}
\DoxyCodeLine{}
\DoxyCodeLine{tmp.dir(\{ mode: 0750, prefix: 'myTmpDir\_' \}, function \_tempDirCreated(err, path) \{}
\DoxyCodeLine{  if (err) throw err;}
\DoxyCodeLine{}
\DoxyCodeLine{  console.log('Dir: ', path);}
\DoxyCodeLine{\}); }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_tmp_README_autotoc_md30470}{}\doxysubsection{Synchronous directory creation}\label{md_sfcode_node_modules_tmp_README_autotoc_md30470}
Again, a synchronous version of the above.


\begin{DoxyCode}{0}
\DoxyCodeLine{var tmp = require('tmp');}
\DoxyCodeLine{}
\DoxyCodeLine{var tmpobj = tmp.dirSync(\{ mode: 0750, prefix: 'myTmpDir\_' \});}
\DoxyCodeLine{console.log('Dir: ', tmpobj.name); }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_tmp_README_autotoc_md30471}{}\doxysubsection{mkstemp like, asynchronously}\label{md_sfcode_node_modules_tmp_README_autotoc_md30471}
Creates a new temporary directory with mode {\ttfamily 0700} and filename like {\ttfamily /tmp/tmp-\/nk2\+J1u}.


\begin{DoxyCode}{0}
\DoxyCodeLine{var tmp = require('tmp');}
\DoxyCodeLine{}
\DoxyCodeLine{tmp.dir(\{ template: '/tmp/tmp-\/XXXXXX' \}, function \_tempDirCreated(err, path) \{}
\DoxyCodeLine{  if (err) throw err;}
\DoxyCodeLine{}
\DoxyCodeLine{  console.log('Dir: ', path);}
\DoxyCodeLine{\}); }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_tmp_README_autotoc_md30472}{}\doxysubsection{mkstemp like, synchronously}\label{md_sfcode_node_modules_tmp_README_autotoc_md30472}
This will behave similarly to the asynchronous version.


\begin{DoxyCode}{0}
\DoxyCodeLine{var tmp = require('tmp');}
\DoxyCodeLine{}
\DoxyCodeLine{var tmpobj = tmp.dirSync(\{ template: '/tmp/tmp-\/XXXXXX' \});}
\DoxyCodeLine{console.log('Dir: ', tmpobj.name); }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_tmp_README_autotoc_md30473}{}\doxysubsection{Asynchronous filename generation}\label{md_sfcode_node_modules_tmp_README_autotoc_md30473}
The {\ttfamily tmp\+Name()} function accepts the {\ttfamily prefix}, {\ttfamily postfix}, {\ttfamily dir}, etc. parameters also\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{var tmp = require('tmp');}
\DoxyCodeLine{}
\DoxyCodeLine{tmp.tmpName(\{ template: '/tmp/tmp-\/XXXXXX' \}, function \_tempNameGenerated(err, path) \{}
\DoxyCodeLine{    if (err) throw err;}
\DoxyCodeLine{}
\DoxyCodeLine{    console.log('Created temporary filename: ', path);}
\DoxyCodeLine{\}); }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_tmp_README_autotoc_md30474}{}\doxysubsection{Synchronous filename generation}\label{md_sfcode_node_modules_tmp_README_autotoc_md30474}
The {\ttfamily tmp\+Name\+Sync()} function works similarly to {\ttfamily tmp\+Name()}.


\begin{DoxyCode}{0}
\DoxyCodeLine{var tmp = require('tmp');}
\DoxyCodeLine{var tmpname = tmp.tmpNameSync(\{ template: '/tmp/tmp-\/XXXXXX' \});}
\DoxyCodeLine{console.log('Created temporary filename: ', tmpname); }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_tmp_README_autotoc_md30475}{}\doxysection{Graceful cleanup}\label{md_sfcode_node_modules_tmp_README_autotoc_md30475}
One may want to cleanup the temporary files even when an uncaught exception occurs. To enforce this, you can call the {\ttfamily set\+Graceful\+Cleanup()} method\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{var tmp = require('tmp');}
\DoxyCodeLine{}
\DoxyCodeLine{tmp.setGracefulCleanup(); }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_tmp_README_autotoc_md30476}{}\doxysection{Options}\label{md_sfcode_node_modules_tmp_README_autotoc_md30476}
All options are optional \+:)


\begin{DoxyItemize}
\item {\ttfamily mode}\+: the file mode to create with, it fallbacks to {\ttfamily 0600} on file creation and {\ttfamily 0700} on directory creation
\item {\ttfamily prefix}\+: the optional prefix, fallbacks to {\ttfamily tmp-\/} if not provided
\item {\ttfamily postfix}\+: the optional postfix, fallbacks to {\ttfamily .tmp} on file creation
\item {\ttfamily template}\+: \href{http://www.kernel.org/doc/man-pages/online/pages/man3/mkstemp.3.html}{\texttt{ {\ttfamily mkstemp}}} like filename template, no default
\item {\ttfamily dir}\+: the optional temporary directory, fallbacks to system default (guesses from environment)
\item {\ttfamily tries}\+: how many times should the function try to get a unique filename before giving up, default {\ttfamily 3}
\item {\ttfamily keep}\+: signals that the temporary file or directory should not be deleted on exit, default is {\ttfamily false}, means delete
\begin{DoxyItemize}
\item Please keep in mind that it is recommended in this case to call the provided {\ttfamily cleanup\+Callback} function manually.
\end{DoxyItemize}
\item {\ttfamily unsafe\+Cleanup}\+: recursively removes the created temporary directory, even when it\textquotesingle{}s not empty. default is {\ttfamily false} 
\end{DoxyItemize}