Newer
Older
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
Object.defineProperty(exports, "__esModule", { value: true });
const axios_1 = require("axios");
const StatusesService_1 = require("./StatusesService");
const common_1 = require("../utils/common");
constructor(cdnUrl, defaultBucket) {
this._cdnUrl = "";
this._defaultBucket = "";
if (!cdnUrl) {
throw new Error("CDN URL Must not be empty");
}
if (!defaultBucket) {
throw new Error("Default bucket must not be empty");
}
this._cdnUrl = cdnUrl;
this._defaultBucket = defaultBucket;
this._statusesService = new StatusesService_1.default();
fetchFile(fileName, bucket = this._defaultBucket) {
return __awaiter(this, void 0, void 0, function* () {
// v?= is a dirty fix to prevent caching of the CORS error response in case file is missing.
// Because of browser caching CORS error in case of missing file, it's not possible to retrieve it when it's being uploaded.
// Dig into https://www.backblaze.com/b2/docs/cors_rules.html and fix
const { data } = yield axios_1.default(`${this._cdnUrl}/file/${bucket}/${fileName}?v=${Math.random()}`);
fetchStatusData(statusFileName) {
return __awaiter(this, void 0, void 0, function* () {
const rawStatusData = (yield this.fetchFile(statusFileName));
let decodedResult;
try {
decodedResult = this._statusesService.decodeStatusObject(new Uint8Array(common_1.base64ToArrayBuffer(rawStatusData.Status)));
}
catch (e) {
throw new Error("Error decoding status object");
merkleTreeId: rawStatusData.MerkleTreeID,
transactionHash: rawStatusData.TransactionID,
statusRaw: rawStatusData.Status,
statusClassName: decodedResult.className,
}
readStatuses(statusId) {
return __awaiter(this, void 0, void 0, function* () {
let finished = false;
let currentIndex = 1;
const statuses = [];
while (!finished) {
try {
const statusData = yield this.fetchStatusData(`${STATUS_FILE_PREFIX}_${statusId}_${currentIndex}`);
statuses.push(statusData);
currentIndex++;
}
catch (error) {
// Axios error. Finishing fetching statuses.
finished = true;
}
else {
throw error;
}
}
}
return statuses;
});
}
getMerkleTree(fileName) {
return __awaiter(this, void 0, void 0, function* () {
const merkleTreeFileContents = (yield this.fetchFile(fileName));
const statusesData = yield Promise.all(merkleTreeFileContents.map((statusFileName) => {
return this.fetchStatusData(statusFileName);
}));
const statusesMap = statusesData.reduce((acc, statusData, index) => {
Object.assign(acc, {
[merkleTreeFileContents[index].toLowerCase()]: statusData,
});
return acc;
}, {});
return Object.keys(statusesMap)
.sort()
.map((key) => statusesMap[key].statusRaw);