aboutsummaryrefslogblamecommitdiffhomepage
path: root/client/src/assets/player/p2p-media-loader/segment-url-builder.ts
blob: 32e7ce4f2b6e3db81847cd37ae537e9fd67bb998 (plain) (tree)



























                                                                              
import { basename } from 'path'
import { Segment } from 'p2p-media-loader-core'

function segmentUrlBuilderFactory (baseUrls: string[]) {
  return function segmentBuilder (segment: Segment) {
    const max = baseUrls.length + 1
    const i = getRandomInt(max)

    if (i === max - 1) return segment.url

    let newBaseUrl = baseUrls[i]
    let middlePart = newBaseUrl.endsWith('/') ? '' : '/'

    return newBaseUrl + middlePart + basename(segment.url)
  }
}

// ---------------------------------------------------------------------------

export {
  segmentUrlBuilderFactory
}

// ---------------------------------------------------------------------------

function getRandomInt (max: number) {
  return Math.floor(Math.random() * Math.floor(max))
}