NodeCrud.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.NodeCrud = 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("../fsa-to-crud/util");
  8. class NodeCrud {
  9. constructor(options) {
  10. var _a;
  11. this.options = options;
  12. this.put = async (collection, id, data, options) => {
  13. (0, util_2.assertType)(collection, 'put', 'crudfs');
  14. (0, util_1.assertName)(id, 'put', 'crudfs');
  15. const dir = this.dir + (collection.length ? collection.join(this.separator) + this.separator : '');
  16. const fs = this.fs;
  17. if (dir.length > 1)
  18. await fs.mkdir(dir, { recursive: true });
  19. const filename = dir + id;
  20. switch (options === null || options === void 0 ? void 0 : options.throwIf) {
  21. case 'exists': {
  22. try {
  23. await fs.writeFile(filename, data, { flag: 64 /* FLAG.O_CREAT */ | 128 /* FLAG.O_EXCL */ });
  24. }
  25. catch (error) {
  26. if (error && typeof error === 'object' && error.code === 'EEXIST')
  27. throw (0, util_3.newExistsError)();
  28. throw error;
  29. }
  30. break;
  31. }
  32. case 'missing': {
  33. try {
  34. await fs.writeFile(filename, data, { flag: 2 /* FLAG.O_RDWR */ });
  35. }
  36. catch (error) {
  37. if (error && typeof error === 'object' && error.code === 'ENOENT')
  38. throw (0, util_3.newMissingError)();
  39. throw error;
  40. }
  41. break;
  42. }
  43. default: {
  44. await fs.writeFile(filename, data);
  45. }
  46. }
  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 dir = await this.checkDir(collection);
  52. const filename = dir + id;
  53. const fs = this.fs;
  54. try {
  55. const buf = (await fs.readFile(filename));
  56. return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
  57. }
  58. catch (error) {
  59. if (error && typeof error === 'object') {
  60. switch (error.code) {
  61. case 'ENOENT':
  62. throw (0, util_3.newFile404Error)(collection, id);
  63. }
  64. }
  65. throw error;
  66. }
  67. };
  68. this.del = async (collection, id, silent) => {
  69. (0, util_2.assertType)(collection, 'del', 'crudfs');
  70. (0, util_1.assertName)(id, 'del', 'crudfs');
  71. try {
  72. const dir = await this.checkDir(collection);
  73. const filename = dir + id;
  74. await this.fs.unlink(filename);
  75. }
  76. catch (error) {
  77. if (!!silent)
  78. return;
  79. if (error && typeof error === 'object') {
  80. switch (error.code) {
  81. case 'ENOENT':
  82. throw (0, util_3.newFile404Error)(collection, id);
  83. }
  84. }
  85. throw error;
  86. }
  87. };
  88. this.info = async (collection, id) => {
  89. (0, util_2.assertType)(collection, 'info', 'crudfs');
  90. if (id) {
  91. (0, util_1.assertName)(id, 'info', 'crudfs');
  92. await this.checkDir(collection);
  93. try {
  94. const stats = await this.fs.stat(this.dir + collection.join(this.separator) + this.separator + id);
  95. if (!stats.isFile())
  96. throw (0, util_3.newFile404Error)(collection, id);
  97. return {
  98. type: 'resource',
  99. id,
  100. size: stats.size,
  101. modified: stats.mtimeMs,
  102. };
  103. }
  104. catch (error) {
  105. if (error && typeof error === 'object') {
  106. switch (error.code) {
  107. case 'ENOENT':
  108. throw (0, util_3.newFile404Error)(collection, id);
  109. }
  110. }
  111. throw error;
  112. }
  113. }
  114. else {
  115. await this.checkDir(collection);
  116. try {
  117. const stats = await this.fs.stat(this.dir + collection.join(this.separator));
  118. if (!stats.isDirectory())
  119. throw (0, util_3.newFolder404Error)(collection);
  120. return {
  121. type: 'collection',
  122. id: '',
  123. };
  124. }
  125. catch (error) {
  126. if (error && typeof error === 'object') {
  127. switch (error.code) {
  128. case 'ENOENT':
  129. case 'ENOTDIR':
  130. throw (0, util_3.newFolder404Error)(collection);
  131. }
  132. }
  133. throw error;
  134. }
  135. }
  136. };
  137. this.drop = async (collection, silent) => {
  138. (0, util_2.assertType)(collection, 'drop', 'crudfs');
  139. try {
  140. const dir = await this.checkDir(collection);
  141. const isRoot = dir === this.dir;
  142. if (isRoot) {
  143. const list = (await this.fs.readdir(dir));
  144. for (const entry of list)
  145. await this.fs.rmdir(dir + entry, { recursive: true });
  146. }
  147. else {
  148. await this.fs.rmdir(dir, { recursive: true });
  149. }
  150. }
  151. catch (error) {
  152. if (!silent)
  153. throw error;
  154. }
  155. };
  156. this.scan = function (collection) {
  157. return tslib_1.__asyncGenerator(this, arguments, function* () {
  158. var _a, e_1, _b, _c;
  159. (0, util_2.assertType)(collection, 'scan', 'crudfs');
  160. const dir = yield tslib_1.__await(this.checkDir(collection));
  161. const dirents = (yield tslib_1.__await(this.fs.readdir(dir, { withFileTypes: true })));
  162. try {
  163. for (var _d = true, dirents_1 = tslib_1.__asyncValues(dirents), dirents_1_1; dirents_1_1 = yield tslib_1.__await(dirents_1.next()), _a = dirents_1_1.done, !_a; _d = true) {
  164. _c = dirents_1_1.value;
  165. _d = false;
  166. const entry = _c;
  167. if (entry.isFile()) {
  168. yield yield tslib_1.__await({
  169. type: 'resource',
  170. id: '' + entry.name,
  171. });
  172. }
  173. else if (entry.isDirectory()) {
  174. yield yield tslib_1.__await({
  175. type: 'collection',
  176. id: '' + entry.name,
  177. });
  178. }
  179. }
  180. }
  181. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  182. finally {
  183. try {
  184. if (!_d && !_a && (_b = dirents_1.return)) yield tslib_1.__await(_b.call(dirents_1));
  185. }
  186. finally { if (e_1) throw e_1.error; }
  187. }
  188. });
  189. };
  190. this.list = async (collection) => {
  191. var _a, e_2, _b, _c;
  192. const entries = [];
  193. try {
  194. for (var _d = true, _e = tslib_1.__asyncValues(this.scan(collection)), _f; _f = await _e.next(), _a = _f.done, !_a; _d = true) {
  195. _c = _f.value;
  196. _d = false;
  197. const entry = _c;
  198. entries.push(entry);
  199. }
  200. }
  201. catch (e_2_1) { e_2 = { error: e_2_1 }; }
  202. finally {
  203. try {
  204. if (!_d && !_a && (_b = _e.return)) await _b.call(_e);
  205. }
  206. finally { if (e_2) throw e_2.error; }
  207. }
  208. return entries;
  209. };
  210. this.from = async (collection) => {
  211. (0, util_2.assertType)(collection, 'from', 'crudfs');
  212. const dir = this.dir + (collection.length ? collection.join(this.separator) + this.separator : '');
  213. const fs = this.fs;
  214. if (dir.length > 1)
  215. await fs.mkdir(dir, { recursive: true });
  216. await this.checkDir(collection);
  217. return new NodeCrud({
  218. dir,
  219. fs: this.fs,
  220. separator: this.separator,
  221. });
  222. };
  223. this.separator = (_a = options.separator) !== null && _a !== void 0 ? _a : '/';
  224. let dir = options.dir;
  225. const last = dir[dir.length - 1];
  226. if (last !== this.separator)
  227. dir = dir + this.separator;
  228. this.dir = dir;
  229. this.fs = options.fs;
  230. }
  231. async checkDir(collection) {
  232. const dir = this.dir + (collection.length ? collection.join(this.separator) + this.separator : '');
  233. const fs = this.fs;
  234. try {
  235. const stats = await fs.stat(dir);
  236. if (!stats.isDirectory())
  237. throw (0, util_3.newFolder404Error)(collection);
  238. return dir;
  239. }
  240. catch (error) {
  241. if (error && typeof error === 'object') {
  242. switch (error.code) {
  243. case 'ENOENT':
  244. case 'ENOTDIR':
  245. throw (0, util_3.newFolder404Error)(collection);
  246. }
  247. }
  248. throw error;
  249. }
  250. }
  251. }
  252. exports.NodeCrud = NodeCrud;
  253. //# sourceMappingURL=NodeCrud.js.map