FsPromises.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.FsPromises = void 0;
  4. const util_1 = require("./util");
  5. const fs_node_utils_1 = require("@jsonjoy.com/fs-node-utils");
  6. // AsyncIterator implementation for promises.watch
  7. class FSWatchAsyncIterator {
  8. constructor(fs, path, options = {}) {
  9. this.fs = fs;
  10. this.path = path;
  11. this.options = options;
  12. this.eventQueue = [];
  13. this.resolveQueue = [];
  14. this.finished = false;
  15. this.maxQueue = options.maxQueue || 2048;
  16. this.overflow = options.overflow || 'ignore';
  17. this.startWatching();
  18. // Handle AbortSignal
  19. if (options.signal) {
  20. if (options.signal.aborted) {
  21. this.finish();
  22. return;
  23. }
  24. options.signal.addEventListener('abort', () => {
  25. this.finish();
  26. });
  27. }
  28. }
  29. startWatching() {
  30. try {
  31. this.watcher = this.fs.watch(this.path, this.options, (eventType, filename) => {
  32. this.enqueueEvent({ eventType, filename });
  33. });
  34. }
  35. catch (error) {
  36. // If we can't start watching, finish immediately
  37. this.finish();
  38. throw error;
  39. }
  40. }
  41. enqueueEvent(event) {
  42. if (this.finished)
  43. return;
  44. // Handle queue overflow
  45. if (this.eventQueue.length >= this.maxQueue) {
  46. if (this.overflow === 'throw') {
  47. const error = new Error(`Watch queue overflow: more than ${this.maxQueue} events queued`);
  48. this.finish(error);
  49. return;
  50. }
  51. else {
  52. // 'ignore' - drop the oldest event
  53. this.eventQueue.shift();
  54. }
  55. }
  56. this.eventQueue.push(event);
  57. // If there's a waiting promise, resolve it
  58. if (this.resolveQueue.length > 0) {
  59. const { resolve } = this.resolveQueue.shift();
  60. const nextEvent = this.eventQueue.shift();
  61. resolve({ value: nextEvent, done: false });
  62. }
  63. }
  64. finish(error) {
  65. if (this.finished)
  66. return;
  67. this.finished = true;
  68. if (this.watcher) {
  69. this.watcher.close();
  70. this.watcher = null;
  71. }
  72. // Resolve or reject all pending promises
  73. while (this.resolveQueue.length > 0) {
  74. const { resolve, reject } = this.resolveQueue.shift();
  75. if (error) {
  76. reject(error);
  77. }
  78. else {
  79. resolve({ value: undefined, done: true });
  80. }
  81. }
  82. }
  83. async next() {
  84. if (this.finished) {
  85. return { value: undefined, done: true };
  86. }
  87. // If we have queued events, return one
  88. if (this.eventQueue.length > 0) {
  89. const event = this.eventQueue.shift();
  90. return { value: event, done: false };
  91. }
  92. // Otherwise, wait for the next event
  93. return new Promise((resolve, reject) => {
  94. this.resolveQueue.push({ resolve, reject });
  95. });
  96. }
  97. async return() {
  98. this.finish();
  99. return { value: undefined, done: true };
  100. }
  101. async throw(error) {
  102. this.finish(error);
  103. throw error;
  104. }
  105. [Symbol.asyncIterator]() {
  106. return this;
  107. }
  108. }
  109. class FsPromises {
  110. constructor(fs, FileHandle) {
  111. this.fs = fs;
  112. this.FileHandle = FileHandle;
  113. this.constants = fs_node_utils_1.constants;
  114. this.cp = (0, util_1.promisify)(this.fs, 'cp');
  115. this.opendir = (0, util_1.promisify)(this.fs, 'opendir');
  116. this.statfs = (0, util_1.promisify)(this.fs, 'statfs');
  117. this.lutimes = (0, util_1.promisify)(this.fs, 'lutimes');
  118. this.glob = (0, util_1.promisify)(this.fs, 'glob');
  119. this.access = (0, util_1.promisify)(this.fs, 'access');
  120. this.chmod = (0, util_1.promisify)(this.fs, 'chmod');
  121. this.chown = (0, util_1.promisify)(this.fs, 'chown');
  122. this.copyFile = (0, util_1.promisify)(this.fs, 'copyFile');
  123. this.lchmod = (0, util_1.promisify)(this.fs, 'lchmod');
  124. this.lchown = (0, util_1.promisify)(this.fs, 'lchown');
  125. this.link = (0, util_1.promisify)(this.fs, 'link');
  126. this.lstat = (0, util_1.promisify)(this.fs, 'lstat');
  127. this.mkdir = (0, util_1.promisify)(this.fs, 'mkdir');
  128. this.mkdtemp = (0, util_1.promisify)(this.fs, 'mkdtemp');
  129. this.readdir = (0, util_1.promisify)(this.fs, 'readdir');
  130. this.readlink = (0, util_1.promisify)(this.fs, 'readlink');
  131. this.realpath = (0, util_1.promisify)(this.fs, 'realpath');
  132. this.rename = (0, util_1.promisify)(this.fs, 'rename');
  133. this.rmdir = (0, util_1.promisify)(this.fs, 'rmdir');
  134. this.rm = (0, util_1.promisify)(this.fs, 'rm');
  135. this.stat = (0, util_1.promisify)(this.fs, 'stat');
  136. this.symlink = (0, util_1.promisify)(this.fs, 'symlink');
  137. this.truncate = (0, util_1.promisify)(this.fs, 'truncate');
  138. this.unlink = (0, util_1.promisify)(this.fs, 'unlink');
  139. this.utimes = (0, util_1.promisify)(this.fs, 'utimes');
  140. this.readFile = (id, options) => {
  141. return (0, util_1.promisify)(this.fs, 'readFile')(id instanceof this.FileHandle ? id.fd : id, options);
  142. };
  143. this.appendFile = (path, data, options) => {
  144. return (0, util_1.promisify)(this.fs, 'appendFile')(path instanceof this.FileHandle ? path.fd : path, data, options);
  145. };
  146. this.open = (path, flags = 'r', mode) => {
  147. return (0, util_1.promisify)(this.fs, 'open', fd => new this.FileHandle(this.fs, fd))(path, flags, mode);
  148. };
  149. this.writeFile = (id, data, options) => {
  150. const dataPromise = (0, util_1.isReadableStream)(data) ? (0, util_1.streamToBuffer)(data) : Promise.resolve(data);
  151. return dataPromise.then(data => (0, util_1.promisify)(this.fs, 'writeFile')(id instanceof this.FileHandle ? id.fd : id, data, options));
  152. };
  153. this.watch = (filename, options) => {
  154. const watchOptions = typeof options === 'string' ? { encoding: options } : options || {};
  155. return new FSWatchAsyncIterator(this.fs, filename, watchOptions);
  156. };
  157. }
  158. }
  159. exports.FsPromises = FsPromises;
  160. //# sourceMappingURL=FsPromises.js.map