index.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. "use strict";
  2. /* IMPORT */
  3. Object.defineProperty(exports, "__esModule", { value: true });
  4. exports.writeFileSync = exports.writeFile = exports.readFileSync = exports.readFile = void 0;
  5. const path = require("path");
  6. const consts_1 = require("./consts");
  7. const fs_1 = require("./utils/fs");
  8. const lang_1 = require("./utils/lang");
  9. const scheduler_1 = require("./utils/scheduler");
  10. const temp_1 = require("./utils/temp");
  11. function readFile(filePath, options = consts_1.DEFAULT_READ_OPTIONS) {
  12. var _a;
  13. if (lang_1.default.isString(options))
  14. return readFile(filePath, { encoding: options });
  15. const timeout = Date.now() + ((_a = options.timeout) !== null && _a !== void 0 ? _a : consts_1.DEFAULT_TIMEOUT_ASYNC);
  16. return fs_1.default.readFileRetry(timeout)(filePath, options);
  17. }
  18. exports.readFile = readFile;
  19. ;
  20. function readFileSync(filePath, options = consts_1.DEFAULT_READ_OPTIONS) {
  21. var _a;
  22. if (lang_1.default.isString(options))
  23. return readFileSync(filePath, { encoding: options });
  24. const timeout = Date.now() + ((_a = options.timeout) !== null && _a !== void 0 ? _a : consts_1.DEFAULT_TIMEOUT_SYNC);
  25. return fs_1.default.readFileSyncRetry(timeout)(filePath, options);
  26. }
  27. exports.readFileSync = readFileSync;
  28. ;
  29. const writeFile = (filePath, data, options, callback) => {
  30. if (lang_1.default.isFunction(options))
  31. return writeFile(filePath, data, consts_1.DEFAULT_WRITE_OPTIONS, options);
  32. const promise = writeFileAsync(filePath, data, options);
  33. if (callback)
  34. promise.then(callback, callback);
  35. return promise;
  36. };
  37. exports.writeFile = writeFile;
  38. const writeFileAsync = async (filePath, data, options = consts_1.DEFAULT_WRITE_OPTIONS) => {
  39. var _a;
  40. if (lang_1.default.isString(options))
  41. return writeFileAsync(filePath, data, { encoding: options });
  42. const timeout = Date.now() + ((_a = options.timeout) !== null && _a !== void 0 ? _a : consts_1.DEFAULT_TIMEOUT_ASYNC);
  43. let schedulerCustomDisposer = null, schedulerDisposer = null, tempDisposer = null, tempPath = null, fd = null;
  44. try {
  45. if (options.schedule)
  46. schedulerCustomDisposer = await options.schedule(filePath);
  47. schedulerDisposer = await scheduler_1.default.schedule(filePath);
  48. filePath = await fs_1.default.realpathAttempt(filePath) || filePath;
  49. [tempPath, tempDisposer] = temp_1.default.get(filePath, options.tmpCreate || temp_1.default.create, !(options.tmpPurge === false));
  50. const useStatChown = consts_1.IS_POSIX && lang_1.default.isUndefined(options.chown), useStatMode = lang_1.default.isUndefined(options.mode);
  51. if (useStatChown || useStatMode) {
  52. const stat = await fs_1.default.statAttempt(filePath);
  53. if (stat) {
  54. options = { ...options };
  55. if (useStatChown)
  56. options.chown = { uid: stat.uid, gid: stat.gid };
  57. if (useStatMode)
  58. options.mode = stat.mode;
  59. }
  60. }
  61. const parentPath = path.dirname(filePath);
  62. await fs_1.default.mkdirAttempt(parentPath, {
  63. mode: consts_1.DEFAULT_FOLDER_MODE,
  64. recursive: true
  65. });
  66. fd = await fs_1.default.openRetry(timeout)(tempPath, 'w', options.mode || consts_1.DEFAULT_FILE_MODE);
  67. if (options.tmpCreated)
  68. options.tmpCreated(tempPath);
  69. if (lang_1.default.isString(data)) {
  70. await fs_1.default.writeRetry(timeout)(fd, data, 0, options.encoding || consts_1.DEFAULT_ENCODING);
  71. }
  72. else if (!lang_1.default.isUndefined(data)) {
  73. await fs_1.default.writeRetry(timeout)(fd, data, 0, data.length, 0);
  74. }
  75. if (options.fsync !== false) {
  76. if (options.fsyncWait !== false) {
  77. await fs_1.default.fsyncRetry(timeout)(fd);
  78. }
  79. else {
  80. fs_1.default.fsyncAttempt(fd);
  81. }
  82. }
  83. await fs_1.default.closeRetry(timeout)(fd);
  84. fd = null;
  85. if (options.chown)
  86. await fs_1.default.chownAttempt(tempPath, options.chown.uid, options.chown.gid);
  87. if (options.mode)
  88. await fs_1.default.chmodAttempt(tempPath, options.mode);
  89. try {
  90. await fs_1.default.renameRetry(timeout)(tempPath, filePath);
  91. }
  92. catch (error) {
  93. if (error.code !== 'ENAMETOOLONG')
  94. throw error;
  95. await fs_1.default.renameRetry(timeout)(tempPath, temp_1.default.truncate(filePath));
  96. }
  97. tempDisposer();
  98. tempPath = null;
  99. }
  100. finally {
  101. if (fd)
  102. await fs_1.default.closeAttempt(fd);
  103. if (tempPath)
  104. temp_1.default.purge(tempPath);
  105. if (schedulerCustomDisposer)
  106. schedulerCustomDisposer();
  107. if (schedulerDisposer)
  108. schedulerDisposer();
  109. }
  110. };
  111. const writeFileSync = (filePath, data, options = consts_1.DEFAULT_WRITE_OPTIONS) => {
  112. var _a;
  113. if (lang_1.default.isString(options))
  114. return writeFileSync(filePath, data, { encoding: options });
  115. const timeout = Date.now() + ((_a = options.timeout) !== null && _a !== void 0 ? _a : consts_1.DEFAULT_TIMEOUT_SYNC);
  116. let tempDisposer = null, tempPath = null, fd = null;
  117. try {
  118. filePath = fs_1.default.realpathSyncAttempt(filePath) || filePath;
  119. [tempPath, tempDisposer] = temp_1.default.get(filePath, options.tmpCreate || temp_1.default.create, !(options.tmpPurge === false));
  120. const useStatChown = consts_1.IS_POSIX && lang_1.default.isUndefined(options.chown), useStatMode = lang_1.default.isUndefined(options.mode);
  121. if (useStatChown || useStatMode) {
  122. const stat = fs_1.default.statSyncAttempt(filePath);
  123. if (stat) {
  124. options = { ...options };
  125. if (useStatChown)
  126. options.chown = { uid: stat.uid, gid: stat.gid };
  127. if (useStatMode)
  128. options.mode = stat.mode;
  129. }
  130. }
  131. const parentPath = path.dirname(filePath);
  132. fs_1.default.mkdirSyncAttempt(parentPath, {
  133. mode: consts_1.DEFAULT_FOLDER_MODE,
  134. recursive: true
  135. });
  136. fd = fs_1.default.openSyncRetry(timeout)(tempPath, 'w', options.mode || consts_1.DEFAULT_FILE_MODE);
  137. if (options.tmpCreated)
  138. options.tmpCreated(tempPath);
  139. if (lang_1.default.isString(data)) {
  140. fs_1.default.writeSyncRetry(timeout)(fd, data, 0, options.encoding || consts_1.DEFAULT_ENCODING);
  141. }
  142. else if (!lang_1.default.isUndefined(data)) {
  143. fs_1.default.writeSyncRetry(timeout)(fd, data, 0, data.length, 0);
  144. }
  145. if (options.fsync !== false) {
  146. if (options.fsyncWait !== false) {
  147. fs_1.default.fsyncSyncRetry(timeout)(fd);
  148. }
  149. else {
  150. fs_1.default.fsyncAttempt(fd);
  151. }
  152. }
  153. fs_1.default.closeSyncRetry(timeout)(fd);
  154. fd = null;
  155. if (options.chown)
  156. fs_1.default.chownSyncAttempt(tempPath, options.chown.uid, options.chown.gid);
  157. if (options.mode)
  158. fs_1.default.chmodSyncAttempt(tempPath, options.mode);
  159. try {
  160. fs_1.default.renameSyncRetry(timeout)(tempPath, filePath);
  161. }
  162. catch (error) {
  163. if (error.code !== 'ENAMETOOLONG')
  164. throw error;
  165. fs_1.default.renameSyncRetry(timeout)(tempPath, temp_1.default.truncate(filePath));
  166. }
  167. tempDisposer();
  168. tempPath = null;
  169. }
  170. finally {
  171. if (fd)
  172. fs_1.default.closeSyncAttempt(fd);
  173. if (tempPath)
  174. temp_1.default.purge(tempPath);
  175. }
  176. };
  177. exports.writeFileSync = writeFileSync;