1 import { basename, dirname } from 'path'
3 class RedundancyUrlManager {
5 constructor (private baseUrls: string[] = []) {
9 removeBySegmentUrl (segmentUrl: string) {
10 console.log('Removing redundancy of segment URL %s.', segmentUrl)
12 const baseUrl = dirname(segmentUrl)
14 this.baseUrls = this.baseUrls.filter(u => u !== baseUrl && u !== baseUrl + '/')
17 buildUrl (url: string) {
18 const max = this.baseUrls.length + 1
19 const i = this.getRandomInt(max)
21 if (i === max - 1) return url
23 const newBaseUrl = this.baseUrls[i]
24 const slashPart = newBaseUrl.endsWith('/') ? '' : '/'
26 return newBaseUrl + slashPart + basename(url)
30 return this.baseUrls.length
33 private getRandomInt (max: number) {
34 return Math.floor(Math.random() * Math.floor(max))
38 // ---------------------------------------------------------------------------