All files / src/feed retrievable.ts

95.23% Statements 20/21
66.66% Branches 2/3
100% Functions 5/5
94.73% Lines 18/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 6833x     33x 33x                               11x 11x   8x   3x   3x 3x                             9x 9x   9x 11x     9x     33x               9x 11x     11x    
import { Objects } from 'cafe-utility'
import { Bee } from '../bee'
import { BeeRequestOptions, DownloadOptions } from '../types'
import { EthAddress, FeedIndex, Reference, Topic } from '../utils/typed-bytes'
import { getFeedUpdateChunkReference } from './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: Bee,
  reference: Reference,
  options: DownloadOptions | undefined,
  requestOptions: BeeRequestOptions,
): Promise<boolean> {
  try {
    await bee.downloadChunk(reference, options, requestOptions)
 
    return true
  } catch (e: unknown) {
    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: EthAddress, topic: Topic, index: FeedIndex): Reference[] {
  const count = index.toBigInt()
  const updateReferences: Reference[] = []
 
  for (let i = 0n; i <= count; i++) {
    updateReferences.push(getFeedUpdateChunkReference(owner, topic, FeedIndex.fromBigInt(i)))
  }
 
  return updateReferences
}
 
export async function areAllSequentialFeedsUpdateRetrievable(
  bee: Bee,
  owner: EthAddress,
  topic: Topic,
  index: FeedIndex,
  options: DownloadOptions | undefined,
  requestOptions: BeeRequestOptions,
): Promise<boolean> {
  const chunkRetrievablePromises = getAllSequenceUpdateReferences(owner, topic, index).map(async reference =>
    isChunkRetrievable(bee, reference, options, requestOptions),
  )
 
  return (await Promise.all(chunkRetrievablePromises)).every(result => result)
}