FsaNodeCore.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.FsaNodeCore = void 0;
  4. const util_1 = require("../node/util");
  5. const util_2 = require("./util");
  6. const constants_1 = require("../node/constants");
  7. const FsaNodeFsOpenFile_1 = require("./FsaNodeFsOpenFile");
  8. const util = require("../node/util");
  9. class FsaNodeCore {
  10. constructor(root, syncAdapter) {
  11. this.root = root;
  12. this.syncAdapter = syncAdapter;
  13. this.fds = new Map();
  14. /**
  15. * A list of reusable (opened and closed) file descriptors, that should be
  16. * used first before creating a new file descriptor.
  17. */
  18. this.releasedFds = [];
  19. if (root instanceof Promise) {
  20. root
  21. .then(root => {
  22. this.root = root;
  23. })
  24. .catch(error => { });
  25. }
  26. }
  27. getSyncAdapter() {
  28. const adapter = this.syncAdapter;
  29. if (!adapter)
  30. throw new Error('No sync adapter');
  31. return adapter;
  32. }
  33. newFdNumber() {
  34. const releasedFd = this.releasedFds.pop();
  35. return typeof releasedFd === 'number' ? releasedFd : FsaNodeCore.fd--;
  36. }
  37. /**
  38. * @param path Path from root to the new folder.
  39. * @param create Whether to create the folders if they don't exist.
  40. */
  41. async getDir(path, create, funcName) {
  42. let curr = await this.root;
  43. const options = { create };
  44. try {
  45. for (const name of path) {
  46. curr = await curr.getDirectoryHandle(name, options);
  47. }
  48. }
  49. catch (error) {
  50. if (error && typeof error === 'object') {
  51. switch (error.name) {
  52. case 'TypeMismatchError':
  53. throw (0, util_1.createError)('ENOTDIR', funcName, path.join("/" /* FsaToNodeConstants.Separator */));
  54. case 'NotFoundError':
  55. throw (0, util_1.createError)('ENOENT', funcName, path.join("/" /* FsaToNodeConstants.Separator */));
  56. }
  57. }
  58. throw error;
  59. }
  60. return curr;
  61. }
  62. async getFile(path, name, funcName, create) {
  63. const dir = await this.getDir(path, false, funcName);
  64. const file = await dir.getFileHandle(name, { create });
  65. return file;
  66. }
  67. async getFileOrDir(path, name, funcName) {
  68. const dir = await this.getDir(path, false, funcName);
  69. if (!name)
  70. return dir;
  71. try {
  72. const file = await dir.getFileHandle(name);
  73. return file;
  74. }
  75. catch (error) {
  76. if (error && typeof error === 'object') {
  77. switch (error.name) {
  78. case 'TypeMismatchError':
  79. try {
  80. return await dir.getDirectoryHandle(name);
  81. }
  82. catch (error2) {
  83. if (error2 && typeof error2 === 'object') {
  84. switch (error2.name) {
  85. case 'TypeMismatchError':
  86. throw (0, util_1.createError)('ENOTDIR', funcName, path.join("/" /* FsaToNodeConstants.Separator */));
  87. case 'NotFoundError':
  88. throw (0, util_1.createError)('ENOENT', funcName, path.join("/" /* FsaToNodeConstants.Separator */));
  89. }
  90. }
  91. }
  92. case 'NotFoundError':
  93. throw (0, util_1.createError)('ENOENT', funcName, path.join("/" /* FsaToNodeConstants.Separator */));
  94. }
  95. }
  96. throw error;
  97. }
  98. }
  99. getFileByFd(fd, funcName) {
  100. if (!(0, util_1.isFd)(fd))
  101. throw TypeError(constants_1.ERRSTR.FD);
  102. const file = this.fds.get(fd);
  103. if (!file)
  104. throw (0, util_1.createError)('EBADF', funcName);
  105. return file;
  106. }
  107. async getFileByFdAsync(fd, funcName) {
  108. return this.getFileByFd(fd, funcName);
  109. }
  110. async __getFileById(id, funcName) {
  111. if (typeof id === 'number')
  112. return (await this.getFileByFd(id, funcName)).file;
  113. const filename = (0, util_1.pathToFilename)(id);
  114. const [folder, name] = (0, util_2.pathToLocation)(filename);
  115. return await this.getFile(folder, name, funcName);
  116. }
  117. async getFileByIdOrCreate(id, funcName) {
  118. if (typeof id === 'number')
  119. return (await this.getFileByFd(id, funcName)).file;
  120. const filename = (0, util_1.pathToFilename)(id);
  121. const [folder, name] = (0, util_2.pathToLocation)(filename);
  122. const dir = await this.getDir(folder, false, funcName);
  123. return await dir.getFileHandle(name, { create: true });
  124. }
  125. async __open(filename, flags, mode) {
  126. const [folder, name] = (0, util_2.pathToLocation)(filename);
  127. const throwIfExists = !!(flags & 128 /* FLAG.O_EXCL */);
  128. if (throwIfExists) {
  129. try {
  130. await this.getFile(folder, name, 'open', false);
  131. throw util.createError('EEXIST', 'writeFile');
  132. }
  133. catch (error) {
  134. const file404 = error && typeof error === 'object' && (error.code === 'ENOENT' || error.name === 'NotFoundError');
  135. if (!file404) {
  136. if (error && typeof error === 'object') {
  137. switch (error.name) {
  138. case 'TypeMismatchError':
  139. throw (0, util_1.createError)('ENOTDIR', 'open', filename);
  140. case 'NotFoundError':
  141. throw (0, util_1.createError)('ENOENT', 'open', filename);
  142. }
  143. }
  144. throw error;
  145. }
  146. }
  147. }
  148. try {
  149. const createIfMissing = !!(flags & 64 /* FLAG.O_CREAT */);
  150. const fsaFile = await this.getFile(folder, name, 'open', createIfMissing);
  151. return this.__open2(fsaFile, filename, flags, mode);
  152. }
  153. catch (error) {
  154. if (error && typeof error === 'object') {
  155. switch (error.name) {
  156. case 'TypeMismatchError':
  157. throw (0, util_1.createError)('ENOTDIR', 'open', filename);
  158. case 'NotFoundError':
  159. throw (0, util_1.createError)('ENOENT', 'open', filename);
  160. }
  161. }
  162. throw error;
  163. }
  164. }
  165. __open2(fsaFile, filename, flags, mode) {
  166. const fd = this.newFdNumber();
  167. const file = new FsaNodeFsOpenFile_1.FsaNodeFsOpenFile(fd, mode, flags, fsaFile, filename);
  168. this.fds.set(fd, file);
  169. return file;
  170. }
  171. async __close(fd) {
  172. const openFile = await this.getFileByFdAsync(fd, 'close');
  173. await openFile.close();
  174. const deleted = this.fds.delete(fd);
  175. if (deleted)
  176. this.releasedFds.push(fd);
  177. }
  178. getFileName(id) {
  179. if (typeof id === 'number') {
  180. const openFile = this.fds.get(id);
  181. if (!openFile)
  182. throw (0, util_1.createError)('EBADF', 'readFile');
  183. return openFile.filename;
  184. }
  185. return (0, util_1.pathToFilename)(id);
  186. }
  187. }
  188. exports.FsaNodeCore = FsaNodeCore;
  189. FsaNodeCore.fd = 0x7fffffff;
  190. //# sourceMappingURL=FsaNodeCore.js.map