joyent/node — Issue #2459: temporary files / directories support
Author: dmmalam
Created: 2012-01-04 00:04:42+00:00
State: open

The fs module should have support for getting OS dependant temporary files / directories.

While certain NPM modules exist, and hand rolling a function is trivial, it is also error prone. None of NPM modules support Windows. It is very easy to make errors in the hashing function, and create subtle hard to trace bugs. This feature should be supported by the core system in a bullet proof way, so all apps have an easy way to get access to a temp file/dir. It should make portability much easier.

eg
```javascript
fs.tempFile(function(err, path, fd){
...

fs.tempDir(function(err, path){
...

```

Required:
* The paths supplied should respect the temp folder policies on their respective OS's ( ie C:\windows\temp, /var/ etc)
* Sync versions should be available to be consistent with the other fs methods
* Options to specify the file extension (eg .jpg), file mode, and file/dir prefix

Possible:
* Plugin a hashing functions to provide custom names?
* Option to automatically delete directory/file after use. node-tmp adds a handler to process.exit, however this isn't ideal for long living server apps. Could offer an event to emit, or method call, or just be simple and let the user clean up?

Existing npm implementations (though none are ideal)

* https://github.com/raszi/node-tmp ( which we are currently using, and seems like a sane API)
* https://github.com/bruce/node-temp

Also many other languages provide temp file creation in the core library.

* Ruby: http://www.ruby-doc.org/stdlib-1.9.3/libdoc/tempfile/rdoc/Tempfile.html
* Python: http://docs.python.org/library/tempfile.html
* .Net: http://msdn.microsoft.com/en-us/library/system.io.path.gettempfilename(v=vs.80).aspx
* Java: http://docs.oracle.com/javase/1.4.2/docs/api/java/io/File.html#createTempFile(java.lang.String, java.lang.String)