xfs.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.xfs = void 0;
  4. const tslib_1 = require("tslib");
  5. const os_1 = tslib_1.__importDefault(require("os"));
  6. const NodeFS_1 = require("./NodeFS");
  7. const path_1 = require("./path");
  8. function getTempName(prefix) {
  9. const hash = Math.ceil(Math.random() * 0x100000000).toString(16).padStart(8, `0`);
  10. return `${prefix}${hash}`;
  11. }
  12. const tmpdirs = new Set();
  13. let tmpEnv = null;
  14. function initTmpEnv() {
  15. if (tmpEnv)
  16. return tmpEnv;
  17. const tmpdir = path_1.npath.toPortablePath(os_1.default.tmpdir());
  18. const realTmpdir = exports.xfs.realpathSync(tmpdir);
  19. process.once(`exit`, () => {
  20. exports.xfs.rmtempSync();
  21. });
  22. return tmpEnv = {
  23. tmpdir,
  24. realTmpdir,
  25. };
  26. }
  27. exports.xfs = Object.assign(new NodeFS_1.NodeFS(), {
  28. detachTemp(p) {
  29. tmpdirs.delete(p);
  30. },
  31. mktempSync(cb) {
  32. const { tmpdir, realTmpdir } = initTmpEnv();
  33. while (true) {
  34. const name = getTempName(`xfs-`);
  35. try {
  36. this.mkdirSync(path_1.ppath.join(tmpdir, name));
  37. }
  38. catch (error) {
  39. if (error.code === `EEXIST`) {
  40. continue;
  41. }
  42. else {
  43. throw error;
  44. }
  45. }
  46. const realP = path_1.ppath.join(realTmpdir, name);
  47. tmpdirs.add(realP);
  48. if (typeof cb === `undefined`)
  49. return realP;
  50. try {
  51. return cb(realP);
  52. }
  53. finally {
  54. if (tmpdirs.has(realP)) {
  55. tmpdirs.delete(realP);
  56. try {
  57. this.removeSync(realP);
  58. }
  59. catch {
  60. // Too bad if there's an error
  61. }
  62. }
  63. }
  64. }
  65. },
  66. async mktempPromise(cb) {
  67. const { tmpdir, realTmpdir } = initTmpEnv();
  68. while (true) {
  69. const name = getTempName(`xfs-`);
  70. try {
  71. await this.mkdirPromise(path_1.ppath.join(tmpdir, name));
  72. }
  73. catch (error) {
  74. if (error.code === `EEXIST`) {
  75. continue;
  76. }
  77. else {
  78. throw error;
  79. }
  80. }
  81. const realP = path_1.ppath.join(realTmpdir, name);
  82. tmpdirs.add(realP);
  83. if (typeof cb === `undefined`)
  84. return realP;
  85. try {
  86. return await cb(realP);
  87. }
  88. finally {
  89. if (tmpdirs.has(realP)) {
  90. tmpdirs.delete(realP);
  91. try {
  92. await this.removePromise(realP);
  93. }
  94. catch {
  95. // Too bad if there's an error
  96. }
  97. }
  98. }
  99. }
  100. },
  101. async rmtempPromise() {
  102. await Promise.all(Array.from(tmpdirs.values()).map(async (p) => {
  103. try {
  104. await exports.xfs.removePromise(p, { maxRetries: 0 });
  105. tmpdirs.delete(p);
  106. }
  107. catch {
  108. // Too bad if there's an error
  109. }
  110. }));
  111. },
  112. rmtempSync() {
  113. for (const p of tmpdirs) {
  114. try {
  115. exports.xfs.removeSync(p);
  116. tmpdirs.delete(p);
  117. }
  118. catch {
  119. // Too bad if there's an error
  120. }
  121. }
  122. },
  123. });