Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. # Tmp
  2. A simple temporary file and directory creator for [node.js.][1]
  3. [![Build Status](https://travis-ci.org/raszi/node-tmp.svg?branch=master)](https://travis-ci.org/raszi/node-tmp)
  4. [![Dependencies](https://david-dm.org/raszi/node-tmp.svg)](https://david-dm.org/raszi/node-tmp)
  5. [![npm version](https://badge.fury.io/js/tmp.svg)](https://badge.fury.io/js/tmp)
  6. [![API documented](https://img.shields.io/badge/API-documented-brightgreen.svg)](https://raszi.github.io/node-tmp/)
  7. [![Known Vulnerabilities](https://snyk.io/test/npm/tmp/badge.svg)](https://snyk.io/test/npm/tmp)
  8. ## About
  9. This is a [widely used library][2] to create temporary files and directories
  10. in a [node.js][1] environment.
  11. Tmp offers both an asynchronous and a synchronous API. For all API calls, all
  12. the parameters are optional. There also exists a promisified version of the
  13. API, see (5) under references below.
  14. Tmp uses crypto for determining random file names, or, when using templates,
  15. a six letter random identifier. And just in case that you do not have that much
  16. entropy left on your system, Tmp will fall back to pseudo random numbers.
  17. You can set whether you want to remove the temporary file on process exit or
  18. not, and the destination directory can also be set.
  19. ## How to install
  20. ```bash
  21. npm install tmp
  22. ```
  23. ## Usage
  24. Please also check [API docs][4].
  25. ### Asynchronous file creation
  26. Simple temporary file creation, the file will be closed and unlinked on process exit.
  27. ```javascript
  28. var tmp = require('tmp');
  29. tmp.file(function _tempFileCreated(err, path, fd, cleanupCallback) {
  30. if (err) throw err;
  31. console.log('File: ', path);
  32. console.log('Filedescriptor: ', fd);
  33. // If we don't need the file anymore we could manually call the cleanupCallback
  34. // But that is not necessary if we didn't pass the keep option because the library
  35. // will clean after itself.
  36. cleanupCallback();
  37. });
  38. ```
  39. ### Synchronous file creation
  40. A synchronous version of the above.
  41. ```javascript
  42. var tmp = require('tmp');
  43. var tmpobj = tmp.fileSync();
  44. console.log('File: ', tmpobj.name);
  45. console.log('Filedescriptor: ', tmpobj.fd);
  46. // If we don't need the file anymore we could manually call the removeCallback
  47. // But that is not necessary if we didn't pass the keep option because the library
  48. // will clean after itself.
  49. tmpobj.removeCallback();
  50. ```
  51. Note that this might throw an exception if either the maximum limit of retries
  52. for creating a temporary name fails, or, in case that you do not have the permission
  53. to write to the directory where the temporary file should be created in.
  54. ### Asynchronous directory creation
  55. Simple temporary directory creation, it will be removed on process exit.
  56. If the directory still contains items on process exit, then it won't be removed.
  57. ```javascript
  58. var tmp = require('tmp');
  59. tmp.dir(function _tempDirCreated(err, path, cleanupCallback) {
  60. if (err) throw err;
  61. console.log('Dir: ', path);
  62. // Manual cleanup
  63. cleanupCallback();
  64. });
  65. ```
  66. If you want to cleanup the directory even when there are entries in it, then
  67. you can pass the `unsafeCleanup` option when creating it.
  68. ### Synchronous directory creation
  69. A synchronous version of the above.
  70. ```javascript
  71. var tmp = require('tmp');
  72. var tmpobj = tmp.dirSync();
  73. console.log('Dir: ', tmpobj.name);
  74. // Manual cleanup
  75. tmpobj.removeCallback();
  76. ```
  77. Note that this might throw an exception if either the maximum limit of retries
  78. for creating a temporary name fails, or, in case that you do not have the permission
  79. to write to the directory where the temporary directory should be created in.
  80. ### Asynchronous filename generation
  81. It is possible with this library to generate a unique filename in the specified
  82. directory.
  83. ```javascript
  84. var tmp = require('tmp');
  85. tmp.tmpName(function _tempNameGenerated(err, path) {
  86. if (err) throw err;
  87. console.log('Created temporary filename: ', path);
  88. });
  89. ```
  90. ### Synchronous filename generation
  91. A synchronous version of the above.
  92. ```javascript
  93. var tmp = require('tmp');
  94. var name = tmp.tmpNameSync();
  95. console.log('Created temporary filename: ', name);
  96. ```
  97. ## Advanced usage
  98. ### Asynchronous file creation
  99. Creates a file with mode `0644`, prefix will be `prefix-` and postfix will be `.txt`.
  100. ```javascript
  101. var tmp = require('tmp');
  102. tmp.file({ mode: 0644, prefix: 'prefix-', postfix: '.txt' }, function _tempFileCreated(err, path, fd) {
  103. if (err) throw err;
  104. console.log('File: ', path);
  105. console.log('Filedescriptor: ', fd);
  106. });
  107. ```
  108. ### Synchronous file creation
  109. A synchronous version of the above.
  110. ```javascript
  111. var tmp = require('tmp');
  112. var tmpobj = tmp.fileSync({ mode: 0644, prefix: 'prefix-', postfix: '.txt' });
  113. console.log('File: ', tmpobj.name);
  114. console.log('Filedescriptor: ', tmpobj.fd);
  115. ```
  116. ### Controlling the Descriptor
  117. As a side effect of creating a unique file `tmp` gets a file descriptor that is
  118. returned to the user as the `fd` parameter. The descriptor may be used by the
  119. application and is closed when the `removeCallback` is invoked.
  120. In some use cases the application does not need the descriptor, needs to close it
  121. without removing the file, or needs to remove the file without closing the
  122. descriptor. Two options control how the descriptor is managed:
  123. * `discardDescriptor` - if `true` causes `tmp` to close the descriptor after the file
  124. is created. In this case the `fd` parameter is undefined.
  125. * `detachDescriptor` - if `true` causes `tmp` to return the descriptor in the `fd`
  126. parameter, but it is the application's responsibility to close it when it is no
  127. longer needed.
  128. ```javascript
  129. var tmp = require('tmp');
  130. tmp.file({ discardDescriptor: true }, function _tempFileCreated(err, path, fd, cleanupCallback) {
  131. if (err) throw err;
  132. // fd will be undefined, allowing application to use fs.createReadStream(path)
  133. // without holding an unused descriptor open.
  134. });
  135. ```
  136. ```javascript
  137. var tmp = require('tmp');
  138. tmp.file({ detachDescriptor: true }, function _tempFileCreated(err, path, fd, cleanupCallback) {
  139. if (err) throw err;
  140. cleanupCallback();
  141. // Application can store data through fd here; the space used will automatically
  142. // be reclaimed by the operating system when the descriptor is closed or program
  143. // terminates.
  144. });
  145. ```
  146. ### Asynchronous directory creation
  147. Creates a directory with mode `0755`, prefix will be `myTmpDir_`.
  148. ```javascript
  149. var tmp = require('tmp');
  150. tmp.dir({ mode: 0750, prefix: 'myTmpDir_' }, function _tempDirCreated(err, path) {
  151. if (err) throw err;
  152. console.log('Dir: ', path);
  153. });
  154. ```
  155. ### Synchronous directory creation
  156. Again, a synchronous version of the above.
  157. ```javascript
  158. var tmp = require('tmp');
  159. var tmpobj = tmp.dirSync({ mode: 0750, prefix: 'myTmpDir_' });
  160. console.log('Dir: ', tmpobj.name);
  161. ```
  162. ### mkstemp like, asynchronously
  163. Creates a new temporary directory with mode `0700` and filename like `/tmp/tmp-nk2J1u`.
  164. ```javascript
  165. var tmp = require('tmp');
  166. tmp.dir({ template: '/tmp/tmp-XXXXXX' }, function _tempDirCreated(err, path) {
  167. if (err) throw err;
  168. console.log('Dir: ', path);
  169. });
  170. ```
  171. ### mkstemp like, synchronously
  172. This will behave similarly to the asynchronous version.
  173. ```javascript
  174. var tmp = require('tmp');
  175. var tmpobj = tmp.dirSync({ template: '/tmp/tmp-XXXXXX' });
  176. console.log('Dir: ', tmpobj.name);
  177. ```
  178. ### Asynchronous filename generation
  179. The `tmpName()` function accepts the `prefix`, `postfix`, `dir`, etc. parameters also:
  180. ```javascript
  181. var tmp = require('tmp');
  182. tmp.tmpName({ template: '/tmp/tmp-XXXXXX' }, function _tempNameGenerated(err, path) {
  183. if (err) throw err;
  184. console.log('Created temporary filename: ', path);
  185. });
  186. ```
  187. ### Synchronous filename generation
  188. The `tmpNameSync()` function works similarly to `tmpName()`.
  189. ```javascript
  190. var tmp = require('tmp');
  191. var tmpname = tmp.tmpNameSync({ template: '/tmp/tmp-XXXXXX' });
  192. console.log('Created temporary filename: ', tmpname);
  193. ```
  194. ## Graceful cleanup
  195. One may want to cleanup the temporary files even when an uncaught exception
  196. occurs. To enforce this, you can call the `setGracefulCleanup()` method:
  197. ```javascript
  198. var tmp = require('tmp');
  199. tmp.setGracefulCleanup();
  200. ```
  201. ## Options
  202. All options are optional :)
  203. * `mode`: the file mode to create with, it fallbacks to `0600` on file creation and `0700` on directory creation
  204. * `prefix`: the optional prefix, fallbacks to `tmp-` if not provided
  205. * `postfix`: the optional postfix, fallbacks to `.tmp` on file creation
  206. * `template`: [`mkstemp`][3] like filename template, no default
  207. * `dir`: the optional temporary directory, fallbacks to system default (guesses from environment)
  208. * `tries`: how many times should the function try to get a unique filename before giving up, default `3`
  209. * `keep`: signals that the temporary file or directory should not be deleted on exit, default is `false`, means delete
  210. * Please keep in mind that it is recommended in this case to call the provided `cleanupCallback` function manually.
  211. * `unsafeCleanup`: recursively removes the created temporary directory, even when it's not empty. default is `false`
  212. [1]: http://nodejs.org/
  213. [2]: https://www.npmjs.com/browse/depended/tmp
  214. [3]: http://www.kernel.org/doc/man-pages/online/pages/man3/mkstemp.3.html
  215. [4]: https://raszi.github.io/node-tmp/
  216. [5]: https://github.com/benjamingr/tmp-promise