]>
Commit | Line | Data |
---|---|---|
da332417 C |
1 | import { basename, dirname } from 'path' |
2 | ||
3 | class RedundancyUrlManager { | |
4 | ||
da332417 C |
5 | constructor (private baseUrls: string[] = []) { |
6 | // empty | |
7 | } | |
8 | ||
9 | removeBySegmentUrl (segmentUrl: string) { | |
10 | console.log('Removing redundancy of segment URL %s.', segmentUrl) | |
11 | ||
12 | const baseUrl = dirname(segmentUrl) | |
13 | ||
14 | this.baseUrls = this.baseUrls.filter(u => u !== baseUrl && u !== baseUrl + '/') | |
15 | } | |
16 | ||
da332417 | 17 | buildUrl (url: string) { |
da332417 C |
18 | const max = this.baseUrls.length + 1 |
19 | const i = this.getRandomInt(max) | |
20 | ||
21 | if (i === max - 1) return url | |
22 | ||
23 | const newBaseUrl = this.baseUrls[i] | |
24 | const slashPart = newBaseUrl.endsWith('/') ? '' : '/' | |
25 | ||
b82df0a3 | 26 | return newBaseUrl + slashPart + basename(url) |
da332417 C |
27 | } |
28 | ||
29 | countBaseUrls () { | |
30 | return this.baseUrls.length | |
31 | } | |
32 | ||
33 | private getRandomInt (max: number) { | |
34 | return Math.floor(Math.random() * Math.floor(max)) | |
35 | } | |
36 | } | |
37 | ||
38 | // --------------------------------------------------------------------------- | |
39 | ||
40 | export { | |
41 | RedundancyUrlManager | |
42 | } |