CrudCasBase.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.CrudCasBase = void 0;
  4. const normalizeErrors = async (code) => {
  5. try {
  6. return await code();
  7. }
  8. catch (error) {
  9. if (error && typeof error === 'object') {
  10. switch (error.name) {
  11. case 'ResourceNotFound':
  12. case 'CollectionNotFound':
  13. throw new DOMException(error.message, 'BlobNotFound');
  14. }
  15. }
  16. throw error;
  17. }
  18. };
  19. class CrudCasBase {
  20. constructor(crud, hash, hash2Loc, hashEqual) {
  21. this.crud = crud;
  22. this.hash = hash;
  23. this.hash2Loc = hash2Loc;
  24. this.hashEqual = hashEqual;
  25. this.put = async (blob) => {
  26. const digest = await this.hash(blob);
  27. const [collection, resource] = this.hash2Loc(digest);
  28. await this.crud.put(collection, resource, blob);
  29. return digest;
  30. };
  31. this.get = async (hash, options) => {
  32. const [collection, resource] = this.hash2Loc(hash);
  33. return await normalizeErrors(async () => {
  34. const blob = await this.crud.get(collection, resource);
  35. if (!(options === null || options === void 0 ? void 0 : options.skipVerification)) {
  36. const digest = await this.hash(blob);
  37. if (!this.hashEqual(digest, hash))
  38. throw new DOMException('Blob contents does not match hash', 'Integrity');
  39. }
  40. return blob;
  41. });
  42. };
  43. this.del = async (hash, silent) => {
  44. const [collection, resource] = this.hash2Loc(hash);
  45. await normalizeErrors(async () => {
  46. return await this.crud.del(collection, resource, silent);
  47. });
  48. };
  49. this.info = async (hash) => {
  50. const [collection, resource] = this.hash2Loc(hash);
  51. return await normalizeErrors(async () => {
  52. return await this.crud.info(collection, resource);
  53. });
  54. };
  55. }
  56. }
  57. exports.CrudCasBase = CrudCasBase;
  58. //# sourceMappingURL=CrudCasBase.js.map