blob: 32e7ce4f2b6e3db81847cd37ae537e9fd67bb998 (
plain) (
blame)
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
|
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))
}
|