UNPKG

@ethersphere/bee-js

Version:
42 lines 1.53 kB
import { Objects } from 'cafe-utility'; import { FeedIndex } from "../utils/typed-bytes.js"; import { getFeedUpdateChunkReference } from "./index.js"; /** * 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 = 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(getFeedUpdateChunkReference(owner, topic, FeedIndex.fromBigInt(i))); } return updateReferences; } export 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); }