FsaNodeCore.js 7.4 KB

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