blob: 85b2430c2e32de4fbeb3bd777cbbd9dc629a65c9 [file] [log] [blame]
Rahul Ravikumare8c45eb2023-11-13 16:44:53 -08001export function transformUrl(url: string): string {
2 if (isGitHub(url)) {
3 // Transform https://github.com URLs to https://raw.githubusercontent.com
4 // because GitHub applies DDos protection which prevents us from being
5 // able to pull the contents of the LICENSE file.
6 return rawGithubUrl(url);
7 }
8 return url;
9}
10
11function rawGithubUrl(url: string): string {
12 // Transform URL
13 const ignoreSet = new Set<string>(['https:', 'github.com', 'blob']);
14 const tokens = url.split('/');
15 const repo = [];
16 const path = [];
17 let pathStarted = false;
18 for (let i = 0; i < tokens.length; i += 1) {
19 if (tokens[i].length <= 0) {
20 continue;
21 }
22 if (tokens[i] === 'blob') {
23 pathStarted = true;
24 }
25 if (ignoreSet.has(tokens[i])) {
26 continue;
27 }
28 if (!pathStarted) {
29 repo.push(tokens[i]);
30 } else {
31 path.push(tokens[i]);
32 }
33 }
34 return `https://raw.githubusercontent.com/${repo.join('/')}/${path.join('/')}`;
35}
36
37function isGitHub(url: string): boolean {
38 return url.startsWith("https://github.com")
39}