@ethersphere/bee-js
Version:
Javascript client for Bee
48 lines (47 loc) • 1.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.areAllSequentialFeedsUpdateRetrievable = void 0;
const cafe_utility_1 = require("cafe-utility");
const typed_bytes_1 = require("../utils/typed-bytes");
const index_1 = require("./index");
/**
* Function that checks if a chunk is retrievable by actually downloading it.
* The /stewardship/{reference} endpoint does not support verification of chunks, but only manifest's references.
*
* @param bee
* @param ref
* @param options
*/
async function isChunkRetrievable(bee, reference, options, requestOptions) {
try {
await bee.downloadChunk(reference, options, requestOptions);
return true;
}
catch (e) {
const status = cafe_utility_1.Objects.getDeep(e, 'status');
if (status === 404 || status === 500) {
return false;
}
throw e;
}
}
/**
* Creates array of references for all sequence updates chunk up to the given index.
*
* @param owner
* @param topic
* @param index
*/
function getAllSequenceUpdateReferences(owner, topic, index) {
const count = index.toBigInt();
const updateReferences = [];
for (let i = 0n; i <= count; i++) {
updateReferences.push((0, index_1.getFeedUpdateChunkReference)(owner, topic, typed_bytes_1.FeedIndex.fromBigInt(i)));
}
return updateReferences;
}
async function areAllSequentialFeedsUpdateRetrievable(bee, owner, topic, index, options, requestOptions) {
const chunkRetrievablePromises = getAllSequenceUpdateReferences(owner, topic, index).map(async (reference) => isChunkRetrievable(bee, reference, options, requestOptions));
return (await Promise.all(chunkRetrievablePromises)).every(result => result);
}
exports.areAllSequentialFeedsUpdateRetrievable = areAllSequentialFeedsUpdateRetrievable;