NodeFileSystemDirectoryHandle.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.NodeFileSystemDirectoryHandle = void 0;
  4. const tslib_1 = require("tslib");
  5. const NodeFileSystemHandle_1 = require("./NodeFileSystemHandle");
  6. const util_1 = require("./util");
  7. const NodeFileSystemFileHandle_1 = require("./NodeFileSystemFileHandle");
  8. /**
  9. * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle
  10. */
  11. class NodeFileSystemDirectoryHandle extends NodeFileSystemHandle_1.NodeFileSystemHandle {
  12. constructor(fs, path, ctx = {}) {
  13. super('directory', (0, util_1.basename)(path, ctx.separator || '/'));
  14. this.fs = fs;
  15. this.ctx = (0, util_1.ctx)(ctx);
  16. this.__path = path[path.length - 1] === this.ctx.separator ? path : path + this.ctx.separator;
  17. }
  18. /**
  19. * Returns a new array iterator containing the keys for each item in
  20. * {@link NodeFileSystemDirectoryHandle} object.
  21. *
  22. * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle/keys
  23. */
  24. keys() {
  25. return tslib_1.__asyncGenerator(this, arguments, function* keys_1() {
  26. const list = yield tslib_1.__await(this.fs.promises.readdir(this.__path));
  27. for (const name of list)
  28. yield yield tslib_1.__await('' + name);
  29. });
  30. }
  31. /**
  32. * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle/entries
  33. */
  34. entries() {
  35. return tslib_1.__asyncGenerator(this, arguments, function* entries_1() {
  36. const { __path: path, fs, ctx } = this;
  37. const list = yield tslib_1.__await(fs.promises.readdir(path, { withFileTypes: true }));
  38. for (const d of list) {
  39. const dirent = d;
  40. const name = dirent.name + '';
  41. const newPath = path + name;
  42. if (dirent.isDirectory())
  43. yield yield tslib_1.__await([name, new NodeFileSystemDirectoryHandle(fs, newPath, ctx)]);
  44. else if (dirent.isFile())
  45. yield yield tslib_1.__await([name, new NodeFileSystemFileHandle_1.NodeFileSystemFileHandle(fs, newPath, ctx)]);
  46. }
  47. });
  48. }
  49. /**
  50. * Returns a new array iterator containing the values for each index in the
  51. * {@link FileSystemDirectoryHandle} object.
  52. *
  53. * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle/values
  54. */
  55. values() {
  56. return tslib_1.__asyncGenerator(this, arguments, function* values_1() {
  57. var _a, e_1, _b, _c;
  58. try {
  59. for (var _d = true, _e = tslib_1.__asyncValues(this.entries()), _f; _f = yield tslib_1.__await(_e.next()), _a = _f.done, !_a; _d = true) {
  60. _c = _f.value;
  61. _d = false;
  62. const [, value] = _c;
  63. yield yield tslib_1.__await(value);
  64. }
  65. }
  66. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  67. finally {
  68. try {
  69. if (!_d && !_a && (_b = _e.return)) yield tslib_1.__await(_b.call(_e));
  70. }
  71. finally { if (e_1) throw e_1.error; }
  72. }
  73. });
  74. }
  75. /**
  76. * Returns a {@link NodeFileSystemDirectoryHandle} for a subdirectory with the specified
  77. * name within the directory handle on which the method is called.
  78. *
  79. * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle/getDirectoryHandle
  80. * @param name A string representing the {@link NodeFileSystemHandle} name of
  81. * the subdirectory you wish to retrieve.
  82. * @param options An optional object containing options for the retrieved
  83. * subdirectory.
  84. */
  85. async getDirectoryHandle(name, options) {
  86. (0, util_1.assertName)(name, 'getDirectoryHandle', 'FileSystemDirectoryHandle');
  87. const filename = this.__path + name;
  88. try {
  89. const stats = await this.fs.promises.stat(filename);
  90. if (!stats.isDirectory())
  91. throw (0, util_1.newTypeMismatchError)();
  92. return new NodeFileSystemDirectoryHandle(this.fs, filename, this.ctx);
  93. }
  94. catch (error) {
  95. if (error instanceof DOMException)
  96. throw error;
  97. if (error && typeof error === 'object') {
  98. switch (error.code) {
  99. case 'ENOENT': {
  100. if (options === null || options === void 0 ? void 0 : options.create) {
  101. (0, util_1.assertCanWrite)(this.ctx.mode);
  102. await this.fs.promises.mkdir(filename);
  103. return new NodeFileSystemDirectoryHandle(this.fs, filename, this.ctx);
  104. }
  105. throw (0, util_1.newNotFoundError)();
  106. }
  107. case 'EPERM':
  108. case 'EACCES':
  109. throw (0, util_1.newNotAllowedError)();
  110. }
  111. }
  112. throw error;
  113. }
  114. }
  115. /**
  116. * Returns a {@link FileSystemFileHandle} for a file with the specified name,
  117. * within the directory the method is called.
  118. *
  119. * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle/getFileHandle
  120. * @param name A string representing the {@link NodeFileSystemHandle} name of
  121. * the file you wish to retrieve.
  122. * @param options An optional object containing options for the retrieved file.
  123. */
  124. async getFileHandle(name, options) {
  125. (0, util_1.assertName)(name, 'getFileHandle', 'FileSystemDirectoryHandle');
  126. const filename = this.__path + name;
  127. try {
  128. const stats = await this.fs.promises.stat(filename);
  129. if (!stats.isFile())
  130. throw (0, util_1.newTypeMismatchError)();
  131. return new NodeFileSystemFileHandle_1.NodeFileSystemFileHandle(this.fs, filename, this.ctx);
  132. }
  133. catch (error) {
  134. if (error instanceof DOMException)
  135. throw error;
  136. if (error && typeof error === 'object') {
  137. switch (error.code) {
  138. case 'ENOENT': {
  139. if (options === null || options === void 0 ? void 0 : options.create) {
  140. (0, util_1.assertCanWrite)(this.ctx.mode);
  141. await this.fs.promises.writeFile(filename, '');
  142. return new NodeFileSystemFileHandle_1.NodeFileSystemFileHandle(this.fs, filename, this.ctx);
  143. }
  144. throw (0, util_1.newNotFoundError)();
  145. }
  146. case 'EPERM':
  147. case 'EACCES':
  148. throw (0, util_1.newNotAllowedError)();
  149. }
  150. }
  151. throw error;
  152. }
  153. }
  154. /**
  155. * Attempts to remove an entry if the directory handle contains a file or
  156. * directory called the name specified.
  157. *
  158. * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle/removeEntry
  159. * @param name A string representing the {@link FileSystemHandle} name of the
  160. * entry you wish to remove.
  161. * @param options An optional object containing options.
  162. */
  163. async removeEntry(name, { recursive = false } = {}) {
  164. (0, util_1.assertCanWrite)(this.ctx.mode);
  165. (0, util_1.assertName)(name, 'removeEntry', 'FileSystemDirectoryHandle');
  166. const filename = this.__path + name;
  167. const promises = this.fs.promises;
  168. try {
  169. const stats = await promises.stat(filename);
  170. if (stats.isFile()) {
  171. await promises.unlink(filename);
  172. }
  173. else if (stats.isDirectory()) {
  174. await promises.rmdir(filename, { recursive });
  175. }
  176. else
  177. throw (0, util_1.newTypeMismatchError)();
  178. }
  179. catch (error) {
  180. if (error instanceof DOMException)
  181. throw error;
  182. if (error && typeof error === 'object') {
  183. switch (error.code) {
  184. case 'ENOENT': {
  185. throw (0, util_1.newNotFoundError)();
  186. }
  187. case 'EPERM':
  188. case 'EACCES':
  189. throw (0, util_1.newNotAllowedError)();
  190. case 'ENOTEMPTY':
  191. throw new DOMException('The object can not be modified in this way.', 'InvalidModificationError');
  192. }
  193. }
  194. throw error;
  195. }
  196. }
  197. /**
  198. * The `resolve()` method of the {@link FileSystemDirectoryHandle} interface
  199. * returns an {@link Array} of directory names from the parent handle to the specified
  200. * child entry, with the name of the child entry as the last array item.
  201. *
  202. * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle/resolve
  203. * @param possibleDescendant The {@link NodeFileSystemFileHandle} from which
  204. * to return the relative path.
  205. */
  206. async resolve(possibleDescendant) {
  207. if (possibleDescendant instanceof NodeFileSystemDirectoryHandle ||
  208. possibleDescendant instanceof NodeFileSystemFileHandle_1.NodeFileSystemFileHandle) {
  209. const path = this.__path;
  210. const childPath = possibleDescendant.__path;
  211. if (!childPath.startsWith(path))
  212. return null;
  213. let relative = childPath.slice(path.length);
  214. if (relative === '')
  215. return [];
  216. const separator = this.ctx.separator;
  217. if (relative[0] === separator)
  218. relative = relative.slice(1);
  219. return relative.split(separator);
  220. }
  221. return null;
  222. }
  223. }
  224. exports.NodeFileSystemDirectoryHandle = NodeFileSystemDirectoryHandle;
  225. //# sourceMappingURL=NodeFileSystemDirectoryHandle.js.map