FsaCrud.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.FsaCrud = void 0;
  4. const tslib_1 = require("tslib");
  5. const util_1 = require("../node-to-fsa/util");
  6. const util_2 = require("../crud/util");
  7. const util_3 = require("./util");
  8. class FsaCrud {
  9. constructor(root) {
  10. this.root = root;
  11. this.put = async (collection, id, data, options) => {
  12. (0, util_2.assertType)(collection, 'put', 'crudfs');
  13. (0, util_1.assertName)(id, 'put', 'crudfs');
  14. const [dir] = await this.getDir(collection, true);
  15. let file;
  16. switch (options === null || options === void 0 ? void 0 : options.throwIf) {
  17. case 'exists': {
  18. try {
  19. file = await dir.getFileHandle(id, { create: false });
  20. throw (0, util_3.newExistsError)();
  21. }
  22. catch (e) {
  23. if (e.name !== 'NotFoundError')
  24. throw e;
  25. file = await dir.getFileHandle(id, { create: true });
  26. }
  27. break;
  28. }
  29. case 'missing': {
  30. try {
  31. file = await dir.getFileHandle(id, { create: false });
  32. }
  33. catch (e) {
  34. if (e.name === 'NotFoundError')
  35. throw (0, util_3.newMissingError)();
  36. throw e;
  37. }
  38. break;
  39. }
  40. default: {
  41. file = await dir.getFileHandle(id, { create: true });
  42. }
  43. }
  44. const writable = await file.createWritable();
  45. await writable.write(data);
  46. await writable.close();
  47. };
  48. this.get = async (collection, id) => {
  49. (0, util_2.assertType)(collection, 'get', 'crudfs');
  50. (0, util_1.assertName)(id, 'get', 'crudfs');
  51. const [, file] = await this.getFile(collection, id);
  52. const blob = await file.getFile();
  53. const buffer = await blob.arrayBuffer();
  54. return new Uint8Array(buffer);
  55. };
  56. this.del = async (collection, id, silent) => {
  57. (0, util_2.assertType)(collection, 'del', 'crudfs');
  58. (0, util_1.assertName)(id, 'del', 'crudfs');
  59. try {
  60. const [dir] = await this.getFile(collection, id);
  61. await dir.removeEntry(id, { recursive: false });
  62. }
  63. catch (error) {
  64. if (!silent)
  65. throw error;
  66. }
  67. };
  68. this.info = async (collection, id) => {
  69. (0, util_2.assertType)(collection, 'info', 'crudfs');
  70. if (id) {
  71. (0, util_1.assertName)(id, 'info', 'crudfs');
  72. const [, file] = await this.getFile(collection, id);
  73. const blob = await file.getFile();
  74. return {
  75. type: 'resource',
  76. id,
  77. size: blob.size,
  78. modified: blob.lastModified,
  79. };
  80. }
  81. else {
  82. await this.getDir(collection, false);
  83. return {
  84. type: 'collection',
  85. id: '',
  86. };
  87. }
  88. };
  89. this.drop = async (collection, silent) => {
  90. var _a, e_1, _b, _c;
  91. (0, util_2.assertType)(collection, 'drop', 'crudfs');
  92. try {
  93. const [dir, parent] = await this.getDir(collection, false);
  94. if (parent) {
  95. await parent.removeEntry(dir.name, { recursive: true });
  96. }
  97. else {
  98. const root = await this.root;
  99. try {
  100. for (var _d = true, _e = tslib_1.__asyncValues(root.keys()), _f; _f = await _e.next(), _a = _f.done, !_a; _d = true) {
  101. _c = _f.value;
  102. _d = false;
  103. const name = _c;
  104. await root.removeEntry(name, { recursive: true });
  105. }
  106. }
  107. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  108. finally {
  109. try {
  110. if (!_d && !_a && (_b = _e.return)) await _b.call(_e);
  111. }
  112. finally { if (e_1) throw e_1.error; }
  113. }
  114. }
  115. }
  116. catch (error) {
  117. if (!silent)
  118. throw error;
  119. }
  120. };
  121. this.scan = function (collection) {
  122. return tslib_1.__asyncGenerator(this, arguments, function* () {
  123. var _a, e_2, _b, _c;
  124. (0, util_2.assertType)(collection, 'scan', 'crudfs');
  125. const [dir] = yield tslib_1.__await(this.getDir(collection, false));
  126. try {
  127. for (var _d = true, _e = tslib_1.__asyncValues(dir.entries()), _f; _f = yield tslib_1.__await(_e.next()), _a = _f.done, !_a; _d = true) {
  128. _c = _f.value;
  129. _d = false;
  130. const [id, handle] = _c;
  131. if (handle.kind === 'file') {
  132. yield yield tslib_1.__await({
  133. type: 'resource',
  134. id,
  135. });
  136. }
  137. else if (handle.kind === 'directory') {
  138. yield yield tslib_1.__await({
  139. type: 'collection',
  140. id,
  141. });
  142. }
  143. }
  144. }
  145. catch (e_2_1) { e_2 = { error: e_2_1 }; }
  146. finally {
  147. try {
  148. if (!_d && !_a && (_b = _e.return)) yield tslib_1.__await(_b.call(_e));
  149. }
  150. finally { if (e_2) throw e_2.error; }
  151. }
  152. });
  153. };
  154. this.list = async (collection) => {
  155. var _a, e_3, _b, _c;
  156. const entries = [];
  157. try {
  158. for (var _d = true, _e = tslib_1.__asyncValues(this.scan(collection)), _f; _f = await _e.next(), _a = _f.done, !_a; _d = true) {
  159. _c = _f.value;
  160. _d = false;
  161. const entry = _c;
  162. entries.push(entry);
  163. }
  164. }
  165. catch (e_3_1) { e_3 = { error: e_3_1 }; }
  166. finally {
  167. try {
  168. if (!_d && !_a && (_b = _e.return)) await _b.call(_e);
  169. }
  170. finally { if (e_3) throw e_3.error; }
  171. }
  172. return entries;
  173. };
  174. this.from = async (collection) => {
  175. (0, util_2.assertType)(collection, 'from', 'crudfs');
  176. const [dir] = await this.getDir(collection, true);
  177. return new FsaCrud(dir);
  178. };
  179. }
  180. async getDir(collection, create) {
  181. let parent = undefined;
  182. let dir = await this.root;
  183. try {
  184. for (const name of collection) {
  185. const child = await dir.getDirectoryHandle(name, { create });
  186. parent = dir;
  187. dir = child;
  188. }
  189. return [dir, parent];
  190. }
  191. catch (error) {
  192. if (error.name === 'NotFoundError')
  193. throw (0, util_3.newFolder404Error)(collection);
  194. throw error;
  195. }
  196. }
  197. async getFile(collection, id) {
  198. const [dir] = await this.getDir(collection, false);
  199. try {
  200. const file = await dir.getFileHandle(id, { create: false });
  201. return [dir, file];
  202. }
  203. catch (error) {
  204. if (error.name === 'NotFoundError')
  205. throw (0, util_3.newFile404Error)(collection, id);
  206. throw error;
  207. }
  208. }
  209. }
  210. exports.FsaCrud = FsaCrud;
  211. //# sourceMappingURL=FsaCrud.js.map