Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. /*!
  2. * Tmp
  3. *
  4. * Copyright (c) 2011-2017 KARASZI Istvan <github@spam.raszi.hu>
  5. *
  6. * MIT Licensed
  7. */
  8. /*
  9. * Module dependencies.
  10. */
  11. const fs = require('fs');
  12. const os = require('os');
  13. const path = require('path');
  14. const crypto = require('crypto');
  15. const _c = { fs: fs.constants, os: os.constants };
  16. const rimraf = require('rimraf');
  17. /*
  18. * The working inner variables.
  19. */
  20. const
  21. // the random characters to choose from
  22. RANDOM_CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',
  23. TEMPLATE_PATTERN = /XXXXXX/,
  24. DEFAULT_TRIES = 3,
  25. CREATE_FLAGS = (_c.O_CREAT || _c.fs.O_CREAT) | (_c.O_EXCL || _c.fs.O_EXCL) | (_c.O_RDWR || _c.fs.O_RDWR),
  26. // constants are off on the windows platform and will not match the actual errno codes
  27. IS_WIN32 = os.platform() === 'win32',
  28. EBADF = _c.EBADF || _c.os.errno.EBADF,
  29. ENOENT = _c.ENOENT || _c.os.errno.ENOENT,
  30. DIR_MODE = 0o700 /* 448 */,
  31. FILE_MODE = 0o600 /* 384 */,
  32. EXIT = 'exit',
  33. // this will hold the objects need to be removed on exit
  34. _removeObjects = [],
  35. // API change in fs.rmdirSync leads to error when passing in a second parameter, e.g. the callback
  36. FN_RMDIR_SYNC = fs.rmdirSync.bind(fs),
  37. FN_RIMRAF_SYNC = rimraf.sync;
  38. let
  39. _gracefulCleanup = false;
  40. /**
  41. * Gets a temporary file name.
  42. *
  43. * @param {(Options|tmpNameCallback)} options options or callback
  44. * @param {?tmpNameCallback} callback the callback function
  45. */
  46. function tmpName(options, callback) {
  47. const
  48. args = _parseArguments(options, callback),
  49. opts = args[0],
  50. cb = args[1];
  51. try {
  52. _assertAndSanitizeOptions(opts);
  53. } catch (err) {
  54. return cb(err);
  55. }
  56. let tries = opts.tries;
  57. (function _getUniqueName() {
  58. try {
  59. const name = _generateTmpName(opts);
  60. // check whether the path exists then retry if needed
  61. fs.stat(name, function (err) {
  62. /* istanbul ignore else */
  63. if (!err) {
  64. /* istanbul ignore else */
  65. if (tries-- > 0) return _getUniqueName();
  66. return cb(new Error('Could not get a unique tmp filename, max tries reached ' + name));
  67. }
  68. cb(null, name);
  69. });
  70. } catch (err) {
  71. cb(err);
  72. }
  73. }());
  74. }
  75. /**
  76. * Synchronous version of tmpName.
  77. *
  78. * @param {Object} options
  79. * @returns {string} the generated random name
  80. * @throws {Error} if the options are invalid or could not generate a filename
  81. */
  82. function tmpNameSync(options) {
  83. const
  84. args = _parseArguments(options),
  85. opts = args[0];
  86. _assertAndSanitizeOptions(opts);
  87. let tries = opts.tries;
  88. do {
  89. const name = _generateTmpName(opts);
  90. try {
  91. fs.statSync(name);
  92. } catch (e) {
  93. return name;
  94. }
  95. } while (tries-- > 0);
  96. throw new Error('Could not get a unique tmp filename, max tries reached');
  97. }
  98. /**
  99. * Creates and opens a temporary file.
  100. *
  101. * @param {(Options|null|undefined|fileCallback)} options the config options or the callback function or null or undefined
  102. * @param {?fileCallback} callback
  103. */
  104. function file(options, callback) {
  105. const
  106. args = _parseArguments(options, callback),
  107. opts = args[0],
  108. cb = args[1];
  109. // gets a temporary filename
  110. tmpName(opts, function _tmpNameCreated(err, name) {
  111. /* istanbul ignore else */
  112. if (err) return cb(err);
  113. // create and open the file
  114. fs.open(name, CREATE_FLAGS, opts.mode || FILE_MODE, function _fileCreated(err, fd) {
  115. /* istanbu ignore else */
  116. if (err) return cb(err);
  117. if (opts.discardDescriptor) {
  118. return fs.close(fd, function _discardCallback(possibleErr) {
  119. // the chance of getting an error on close here is rather low and might occur in the most edgiest cases only
  120. return cb(possibleErr, name, undefined, _prepareTmpFileRemoveCallback(name, -1, opts, false));
  121. });
  122. } else {
  123. // detachDescriptor passes the descriptor whereas discardDescriptor closes it, either way, we no longer care
  124. // about the descriptor
  125. const discardOrDetachDescriptor = opts.discardDescriptor || opts.detachDescriptor;
  126. cb(null, name, fd, _prepareTmpFileRemoveCallback(name, discardOrDetachDescriptor ? -1 : fd, opts, false));
  127. }
  128. });
  129. });
  130. }
  131. /**
  132. * Synchronous version of file.
  133. *
  134. * @param {Options} options
  135. * @returns {FileSyncObject} object consists of name, fd and removeCallback
  136. * @throws {Error} if cannot create a file
  137. */
  138. function fileSync(options) {
  139. const
  140. args = _parseArguments(options),
  141. opts = args[0];
  142. const discardOrDetachDescriptor = opts.discardDescriptor || opts.detachDescriptor;
  143. const name = tmpNameSync(opts);
  144. var fd = fs.openSync(name, CREATE_FLAGS, opts.mode || FILE_MODE);
  145. /* istanbul ignore else */
  146. if (opts.discardDescriptor) {
  147. fs.closeSync(fd);
  148. fd = undefined;
  149. }
  150. return {
  151. name: name,
  152. fd: fd,
  153. removeCallback: _prepareTmpFileRemoveCallback(name, discardOrDetachDescriptor ? -1 : fd, opts, true)
  154. };
  155. }
  156. /**
  157. * Creates a temporary directory.
  158. *
  159. * @param {(Options|dirCallback)} options the options or the callback function
  160. * @param {?dirCallback} callback
  161. */
  162. function dir(options, callback) {
  163. const
  164. args = _parseArguments(options, callback),
  165. opts = args[0],
  166. cb = args[1];
  167. // gets a temporary filename
  168. tmpName(opts, function _tmpNameCreated(err, name) {
  169. /* istanbul ignore else */
  170. if (err) return cb(err);
  171. // create the directory
  172. fs.mkdir(name, opts.mode || DIR_MODE, function _dirCreated(err) {
  173. /* istanbul ignore else */
  174. if (err) return cb(err);
  175. cb(null, name, _prepareTmpDirRemoveCallback(name, opts, false));
  176. });
  177. });
  178. }
  179. /**
  180. * Synchronous version of dir.
  181. *
  182. * @param {Options} options
  183. * @returns {DirSyncObject} object consists of name and removeCallback
  184. * @throws {Error} if it cannot create a directory
  185. */
  186. function dirSync(options) {
  187. const
  188. args = _parseArguments(options),
  189. opts = args[0];
  190. const name = tmpNameSync(opts);
  191. fs.mkdirSync(name, opts.mode || DIR_MODE);
  192. return {
  193. name: name,
  194. removeCallback: _prepareTmpDirRemoveCallback(name, opts, true)
  195. };
  196. }
  197. /**
  198. * Removes files asynchronously.
  199. *
  200. * @param {Object} fdPath
  201. * @param {Function} next
  202. * @private
  203. */
  204. function _removeFileAsync(fdPath, next) {
  205. const _handler = function (err) {
  206. if (err && !_isENOENT(err)) {
  207. // reraise any unanticipated error
  208. return next(err);
  209. }
  210. next();
  211. };
  212. if (0 <= fdPath[0])
  213. fs.close(fdPath[0], function () {
  214. fs.unlink(fdPath[1], _handler);
  215. });
  216. else fs.unlink(fdPath[1], _handler);
  217. }
  218. /**
  219. * Removes files synchronously.
  220. *
  221. * @param {Object} fdPath
  222. * @private
  223. */
  224. function _removeFileSync(fdPath) {
  225. let rethrownException = null;
  226. try {
  227. if (0 <= fdPath[0]) fs.closeSync(fdPath[0]);
  228. } catch (e) {
  229. // reraise any unanticipated error
  230. if (!_isEBADF(e) && !_isENOENT(e)) throw e;
  231. } finally {
  232. try {
  233. fs.unlinkSync(fdPath[1]);
  234. }
  235. catch (e) {
  236. // reraise any unanticipated error
  237. if (!_isENOENT(e)) rethrownException = e;
  238. }
  239. }
  240. if (rethrownException !== null) {
  241. throw rethrownException;
  242. }
  243. }
  244. /**
  245. * Prepares the callback for removal of the temporary file.
  246. *
  247. * Returns either a sync callback or a async callback depending on whether
  248. * fileSync or file was called, which is expressed by the sync parameter.
  249. *
  250. * @param {string} name the path of the file
  251. * @param {number} fd file descriptor
  252. * @param {Object} opts
  253. * @param {boolean} sync
  254. * @returns {fileCallback | fileCallbackSync}
  255. * @private
  256. */
  257. function _prepareTmpFileRemoveCallback(name, fd, opts, sync) {
  258. const removeCallbackSync = _prepareRemoveCallback(_removeFileSync, [fd, name], sync);
  259. const removeCallback = _prepareRemoveCallback(_removeFileAsync, [fd, name], sync, removeCallbackSync);
  260. if (!opts.keep) _removeObjects.unshift(removeCallbackSync);
  261. return sync ? removeCallbackSync : removeCallback;
  262. }
  263. /**
  264. * Prepares the callback for removal of the temporary directory.
  265. *
  266. * Returns either a sync callback or a async callback depending on whether
  267. * tmpFileSync or tmpFile was called, which is expressed by the sync parameter.
  268. *
  269. * @param {string} name
  270. * @param {Object} opts
  271. * @param {boolean} sync
  272. * @returns {Function} the callback
  273. * @private
  274. */
  275. function _prepareTmpDirRemoveCallback(name, opts, sync) {
  276. const removeFunction = opts.unsafeCleanup ? rimraf : fs.rmdir.bind(fs);
  277. const removeFunctionSync = opts.unsafeCleanup ? FN_RIMRAF_SYNC : FN_RMDIR_SYNC;
  278. const removeCallbackSync = _prepareRemoveCallback(removeFunctionSync, name, sync);
  279. const removeCallback = _prepareRemoveCallback(removeFunction, name, sync, removeCallbackSync);
  280. if (!opts.keep) _removeObjects.unshift(removeCallbackSync);
  281. return sync ? removeCallbackSync : removeCallback;
  282. }
  283. /**
  284. * Creates a guarded function wrapping the removeFunction call.
  285. *
  286. * The cleanup callback is save to be called multiple times.
  287. * Subsequent invocations will be ignored.
  288. *
  289. * @param {Function} removeFunction
  290. * @param {string} fileOrDirName
  291. * @param {boolean} sync
  292. * @param {cleanupCallbackSync?} cleanupCallbackSync
  293. * @returns {cleanupCallback | cleanupCallbackSync}
  294. * @private
  295. */
  296. function _prepareRemoveCallback(removeFunction, fileOrDirName, sync, cleanupCallbackSync) {
  297. let called = false;
  298. // if sync is true, the next parameter will be ignored
  299. return function _cleanupCallback(next) {
  300. /* istanbul ignore else */
  301. if (!called) {
  302. // remove cleanupCallback from cache
  303. const toRemove = cleanupCallbackSync || _cleanupCallback;
  304. const index = _removeObjects.indexOf(toRemove);
  305. /* istanbul ignore else */
  306. if (index >= 0) _removeObjects.splice(index, 1);
  307. called = true;
  308. if (sync || removeFunction === FN_RMDIR_SYNC || removeFunction === FN_RIMRAF_SYNC) {
  309. return removeFunction(fileOrDirName);
  310. } else {
  311. return removeFunction(fileOrDirName, next || function() {});
  312. }
  313. }
  314. };
  315. }
  316. /**
  317. * The garbage collector.
  318. *
  319. * @private
  320. */
  321. function _garbageCollector() {
  322. /* istanbul ignore else */
  323. if (!_gracefulCleanup) return;
  324. // the function being called removes itself from _removeObjects,
  325. // loop until _removeObjects is empty
  326. while (_removeObjects.length) {
  327. try {
  328. _removeObjects[0]();
  329. } catch (e) {
  330. // already removed?
  331. }
  332. }
  333. }
  334. /**
  335. * Random name generator based on crypto.
  336. * Adapted from http://blog.tompawlak.org/how-to-generate-random-values-nodejs-javascript
  337. *
  338. * @param {number} howMany
  339. * @returns {string} the generated random name
  340. * @private
  341. */
  342. function _randomChars(howMany) {
  343. let
  344. value = [],
  345. rnd = null;
  346. // make sure that we do not fail because we ran out of entropy
  347. try {
  348. rnd = crypto.randomBytes(howMany);
  349. } catch (e) {
  350. rnd = crypto.pseudoRandomBytes(howMany);
  351. }
  352. for (var i = 0; i < howMany; i++) {
  353. value.push(RANDOM_CHARS[rnd[i] % RANDOM_CHARS.length]);
  354. }
  355. return value.join('');
  356. }
  357. /**
  358. * Helper which determines whether a string s is blank, that is undefined, or empty or null.
  359. *
  360. * @private
  361. * @param {string} s
  362. * @returns {Boolean} true whether the string s is blank, false otherwise
  363. */
  364. function _isBlank(s) {
  365. return s === null || _isUndefined(s) || !s.trim();
  366. }
  367. /**
  368. * Checks whether the `obj` parameter is defined or not.
  369. *
  370. * @param {Object} obj
  371. * @returns {boolean} true if the object is undefined
  372. * @private
  373. */
  374. function _isUndefined(obj) {
  375. return typeof obj === 'undefined';
  376. }
  377. /**
  378. * Parses the function arguments.
  379. *
  380. * This function helps to have optional arguments.
  381. *
  382. * @param {(Options|null|undefined|Function)} options
  383. * @param {?Function} callback
  384. * @returns {Array} parsed arguments
  385. * @private
  386. */
  387. function _parseArguments(options, callback) {
  388. /* istanbul ignore else */
  389. if (typeof options === 'function') {
  390. return [{}, options];
  391. }
  392. /* istanbul ignore else */
  393. if (_isUndefined(options)) {
  394. return [{}, callback];
  395. }
  396. // copy options so we do not leak the changes we make internally
  397. const actualOptions = {};
  398. for (const key of Object.getOwnPropertyNames(options)) {
  399. actualOptions[key] = options[key];
  400. }
  401. return [actualOptions, callback];
  402. }
  403. /**
  404. * Generates a new temporary name.
  405. *
  406. * @param {Object} opts
  407. * @returns {string} the new random name according to opts
  408. * @private
  409. */
  410. function _generateTmpName(opts) {
  411. const tmpDir = opts.tmpdir;
  412. /* istanbul ignore else */
  413. if (!_isUndefined(opts.name))
  414. return path.join(tmpDir, opts.dir, opts.name);
  415. /* istanbul ignore else */
  416. if (!_isUndefined(opts.template))
  417. return path.join(tmpDir, opts.dir, opts.template).replace(TEMPLATE_PATTERN, _randomChars(6));
  418. // prefix and postfix
  419. const name = [
  420. opts.prefix ? opts.prefix : 'tmp',
  421. '-',
  422. process.pid,
  423. '-',
  424. _randomChars(12),
  425. opts.postfix ? '-' + opts.postfix : ''
  426. ].join('');
  427. return path.join(tmpDir, opts.dir, name);
  428. }
  429. /**
  430. * Asserts whether the specified options are valid, also sanitizes options and provides sane defaults for missing
  431. * options.
  432. *
  433. * @param {Options} options
  434. * @private
  435. */
  436. function _assertAndSanitizeOptions(options) {
  437. options.tmpdir = _getTmpDir(options);
  438. const tmpDir = options.tmpdir;
  439. /* istanbul ignore else */
  440. if (!_isUndefined(options.name))
  441. _assertIsRelative(options.name, 'name', tmpDir);
  442. /* istanbul ignore else */
  443. if (!_isUndefined(options.dir))
  444. _assertIsRelative(options.dir, 'dir', tmpDir);
  445. /* istanbul ignore else */
  446. if (!_isUndefined(options.template)) {
  447. _assertIsRelative(options.template, 'template', tmpDir);
  448. if (!options.template.match(TEMPLATE_PATTERN))
  449. throw new Error(`Invalid template, found "${options.template}".`);
  450. }
  451. /* istanbul ignore else */
  452. if (!_isUndefined(options.tries) && isNaN(options.tries) || options.tries < 0)
  453. throw new Error(`Invalid tries, found "${options.tries}".`);
  454. // if a name was specified we will try once
  455. options.tries = _isUndefined(options.name) ? options.tries || DEFAULT_TRIES : 1;
  456. options.keep = !!options.keep;
  457. options.detachDescriptor = !!options.detachDescriptor;
  458. options.discardDescriptor = !!options.discardDescriptor;
  459. options.unsafeCleanup = !!options.unsafeCleanup;
  460. // sanitize dir, also keep (multiple) blanks if the user, purportedly sane, requests us to
  461. options.dir = _isUndefined(options.dir) ? '' : path.relative(tmpDir, _resolvePath(options.dir, tmpDir));
  462. options.template = _isUndefined(options.template) ? undefined : path.relative(tmpDir, _resolvePath(options.template, tmpDir));
  463. // sanitize further if template is relative to options.dir
  464. options.template = _isBlank(options.template) ? undefined : path.relative(options.dir, options.template);
  465. // for completeness' sake only, also keep (multiple) blanks if the user, purportedly sane, requests us to
  466. options.name = _isUndefined(options.name) ? undefined : _sanitizeName(options.name);
  467. options.prefix = _isUndefined(options.prefix) ? '' : options.prefix;
  468. options.postfix = _isUndefined(options.postfix) ? '' : options.postfix;
  469. }
  470. /**
  471. * Resolve the specified path name in respect to tmpDir.
  472. *
  473. * The specified name might include relative path components, e.g. ../
  474. * so we need to resolve in order to be sure that is is located inside tmpDir
  475. *
  476. * @param name
  477. * @param tmpDir
  478. * @returns {string}
  479. * @private
  480. */
  481. function _resolvePath(name, tmpDir) {
  482. const sanitizedName = _sanitizeName(name);
  483. if (sanitizedName.startsWith(tmpDir)) {
  484. return path.resolve(sanitizedName);
  485. } else {
  486. return path.resolve(path.join(tmpDir, sanitizedName));
  487. }
  488. }
  489. /**
  490. * Sanitize the specified path name by removing all quote characters.
  491. *
  492. * @param name
  493. * @returns {string}
  494. * @private
  495. */
  496. function _sanitizeName(name) {
  497. if (_isBlank(name)) {
  498. return name;
  499. }
  500. return name.replace(/["']/g, '');
  501. }
  502. /**
  503. * Asserts whether specified name is relative to the specified tmpDir.
  504. *
  505. * @param {string} name
  506. * @param {string} option
  507. * @param {string} tmpDir
  508. * @throws {Error}
  509. * @private
  510. */
  511. function _assertIsRelative(name, option, tmpDir) {
  512. if (option === 'name') {
  513. // assert that name is not absolute and does not contain a path
  514. if (path.isAbsolute(name))
  515. throw new Error(`${option} option must not contain an absolute path, found "${name}".`);
  516. // must not fail on valid .<name> or ..<name> or similar such constructs
  517. let basename = path.basename(name);
  518. if (basename === '..' || basename === '.' || basename !== name)
  519. throw new Error(`${option} option must not contain a path, found "${name}".`);
  520. }
  521. else { // if (option === 'dir' || option === 'template') {
  522. // assert that dir or template are relative to tmpDir
  523. if (path.isAbsolute(name) && !name.startsWith(tmpDir)) {
  524. throw new Error(`${option} option must be relative to "${tmpDir}", found "${name}".`);
  525. }
  526. let resolvedPath = _resolvePath(name, tmpDir);
  527. if (!resolvedPath.startsWith(tmpDir))
  528. throw new Error(`${option} option must be relative to "${tmpDir}", found "${resolvedPath}".`);
  529. }
  530. }
  531. /**
  532. * Helper for testing against EBADF to compensate changes made to Node 7.x under Windows.
  533. *
  534. * @private
  535. */
  536. function _isEBADF(error) {
  537. return _isExpectedError(error, -EBADF, 'EBADF');
  538. }
  539. /**
  540. * Helper for testing against ENOENT to compensate changes made to Node 7.x under Windows.
  541. *
  542. * @private
  543. */
  544. function _isENOENT(error) {
  545. return _isExpectedError(error, -ENOENT, 'ENOENT');
  546. }
  547. /**
  548. * Helper to determine whether the expected error code matches the actual code and errno,
  549. * which will differ between the supported node versions.
  550. *
  551. * - Node >= 7.0:
  552. * error.code {string}
  553. * error.errno {number} any numerical value will be negated
  554. *
  555. * CAVEAT
  556. *
  557. * On windows, the errno for EBADF is -4083 but os.constants.errno.EBADF is different and we must assume that ENOENT
  558. * is no different here.
  559. *
  560. * @param {SystemError} error
  561. * @param {number} errno
  562. * @param {string} code
  563. * @private
  564. */
  565. function _isExpectedError(error, errno, code) {
  566. return IS_WIN32 ? error.code === code : error.code === code && error.errno === errno;
  567. }
  568. /**
  569. * Sets the graceful cleanup.
  570. *
  571. * If graceful cleanup is set, tmp will remove all controlled temporary objects on process exit, otherwise the
  572. * temporary objects will remain in place, waiting to be cleaned up on system restart or otherwise scheduled temporary
  573. * object removals.
  574. */
  575. function setGracefulCleanup() {
  576. _gracefulCleanup = true;
  577. }
  578. /**
  579. * Returns the currently configured tmp dir from os.tmpdir().
  580. *
  581. * @private
  582. * @param {?Options} options
  583. * @returns {string} the currently configured tmp dir
  584. */
  585. function _getTmpDir(options) {
  586. return path.resolve(_sanitizeName(options && options.tmpdir || os.tmpdir()));
  587. }
  588. // Install process exit listener
  589. process.addListener(EXIT, _garbageCollector);
  590. /**
  591. * Configuration options.
  592. *
  593. * @typedef {Object} Options
  594. * @property {?boolean} keep the temporary object (file or dir) will not be garbage collected
  595. * @property {?number} tries the number of tries before give up the name generation
  596. * @property (?int) mode the access mode, defaults are 0o700 for directories and 0o600 for files
  597. * @property {?string} template the "mkstemp" like filename template
  598. * @property {?string} name fixed name relative to tmpdir or the specified dir option
  599. * @property {?string} dir tmp directory relative to the root tmp directory in use
  600. * @property {?string} prefix prefix for the generated name
  601. * @property {?string} postfix postfix for the generated name
  602. * @property {?string} tmpdir the root tmp directory which overrides the os tmpdir
  603. * @property {?boolean} unsafeCleanup recursively removes the created temporary directory, even when it's not empty
  604. * @property {?boolean} detachDescriptor detaches the file descriptor, caller is responsible for closing the file, tmp will no longer try closing the file during garbage collection
  605. * @property {?boolean} discardDescriptor discards the file descriptor (closes file, fd is -1), tmp will no longer try closing the file during garbage collection
  606. */
  607. /**
  608. * @typedef {Object} FileSyncObject
  609. * @property {string} name the name of the file
  610. * @property {string} fd the file descriptor or -1 if the fd has been discarded
  611. * @property {fileCallback} removeCallback the callback function to remove the file
  612. */
  613. /**
  614. * @typedef {Object} DirSyncObject
  615. * @property {string} name the name of the directory
  616. * @property {fileCallback} removeCallback the callback function to remove the directory
  617. */
  618. /**
  619. * @callback tmpNameCallback
  620. * @param {?Error} err the error object if anything goes wrong
  621. * @param {string} name the temporary file name
  622. */
  623. /**
  624. * @callback fileCallback
  625. * @param {?Error} err the error object if anything goes wrong
  626. * @param {string} name the temporary file name
  627. * @param {number} fd the file descriptor or -1 if the fd had been discarded
  628. * @param {cleanupCallback} fn the cleanup callback function
  629. */
  630. /**
  631. * @callback fileCallbackSync
  632. * @param {?Error} err the error object if anything goes wrong
  633. * @param {string} name the temporary file name
  634. * @param {number} fd the file descriptor or -1 if the fd had been discarded
  635. * @param {cleanupCallbackSync} fn the cleanup callback function
  636. */
  637. /**
  638. * @callback dirCallback
  639. * @param {?Error} err the error object if anything goes wrong
  640. * @param {string} name the temporary file name
  641. * @param {cleanupCallback} fn the cleanup callback function
  642. */
  643. /**
  644. * @callback dirCallbackSync
  645. * @param {?Error} err the error object if anything goes wrong
  646. * @param {string} name the temporary file name
  647. * @param {cleanupCallbackSync} fn the cleanup callback function
  648. */
  649. /**
  650. * Removes the temporary created file or directory.
  651. *
  652. * @callback cleanupCallback
  653. * @param {simpleCallback} [next] function to call whenever the tmp object needs to be removed
  654. */
  655. /**
  656. * Removes the temporary created file or directory.
  657. *
  658. * @callback cleanupCallbackSync
  659. */
  660. /**
  661. * Callback function for function composition.
  662. * @see {@link https://github.com/raszi/node-tmp/issues/57|raszi/node-tmp#57}
  663. *
  664. * @callback simpleCallback
  665. */
  666. // exporting all the needed methods
  667. // evaluate _getTmpDir() lazily, mainly for simplifying testing but it also will
  668. // allow users to reconfigure the temporary directory
  669. Object.defineProperty(module.exports, 'tmpdir', {
  670. enumerable: true,
  671. configurable: false,
  672. get: function () {
  673. return _getTmpDir();
  674. }
  675. });
  676. module.exports.dir = dir;
  677. module.exports.dirSync = dirSync;
  678. module.exports.file = file;
  679. module.exports.fileSync = fileSync;
  680. module.exports.tmpName = tmpName;
  681. module.exports.tmpNameSync = tmpNameSync;
  682. module.exports.setGracefulCleanup = setGracefulCleanup;