Last active
May 6, 2023 04:49
-
-
Save Explosion-Scratch/abff985b1f5ba098a2a86c574ed1f492 to your computer and use it in GitHub Desktop.
Get CDN url
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function url(u) { | |
const GITHUB_API_URL = "https://api.github.com"; | |
const TEMPLATES = [ | |
[ | |
/^(https?):\/\/gitlab\.com\/([^\/]+.*\/[^\/]+)\/(?:raw|blob)\/(.+?)(?:\?.*)?$/i, | |
"$1://gl.githack.com/$2/raw/$3", | |
], | |
[ | |
/^(https?):\/\/bitbucket\.org\/([^\/]+\/[^\/]+)\/(?:raw|src)\/(.+?)(?:\?.*)?$/i, | |
"$1://bb.githack.com/$2/raw/$3", | |
], | |
// snippet file URL from web interface, with revision | |
[ | |
/^(https?):\/\/bitbucket\.org\/snippets\/([^\/]+\/[^\/]+)\/revisions\/([^\/\#\?]+)(?:\?[^#]*)?(?:\#file-(.+?))$/i, | |
"$1://bb.githack.com/!api/2.0/snippets/$2/$3/files/$4", | |
], | |
// snippet file URL from web interface, no revision | |
[ | |
/^(https?):\/\/bitbucket\.org\/snippets\/([^\/]+\/[^\/\#\?]+)(?:\?[^#]*)?(?:\#file-(.+?))$/i, | |
"$1://bb.githack.com/!api/2.0/snippets/$2/HEAD/files/$3", | |
], | |
// snippet file URLs from REST API | |
[ | |
/^(https?):\/\/bitbucket\.org\/\!api\/2.0\/snippets\/([^\/]+\/[^\/]+\/[^\/]+)\/files\/(.+?)(?:\?.*)?$/i, | |
"$1://bb.githack.com/!api/2.0/snippets/$2/files/$3", | |
], | |
[ | |
/^(https?):\/\/api\.bitbucket\.org\/2.0\/snippets\/([^\/]+\/[^\/]+\/[^\/]+)\/files\/(.+?)(?:\?.*)?$/i, | |
"$1://bb.githack.com/!api/2.0/snippets/$2/files/$3", | |
], | |
// welcome rawgit refugees | |
[ | |
/^(https?):\/\/(?:cdn\.)?rawgit\.com\/(.+?\/[0-9a-f]+\/raw\/(?:[0-9a-f]+\/)?.+)$/i, | |
"$1://gist.githack.com/$2", | |
], | |
[ | |
/^(https?):\/\/(?:cdn\.)?rawgit\.com\/([^\/]+\/[^\/]+\/[^\/]+|[0-9A-Za-z-]+\/[0-9a-f]+\/raw)\/(.+)/i, | |
"$1://raw.githack.com/$2/$3", | |
], | |
[ | |
/^(https?):\/\/raw\.github(?:usercontent)?\.com\/([^\/]+\/[^\/]+\/[^\/]+|[0-9A-Za-z-]+\/[0-9a-f]+\/raw)\/(.+)/i, | |
"$1://raw.githack.com/$2/$3", | |
], | |
[ | |
/^(https?):\/\/github\.com\/(.[^\/]+?)\/(.[^\/]+?)\/(?!releases\/)(?:(?:blob|raw)\/)?(.+?\/.+)/i, | |
"$1://raw.githack.com/$2/$3/$4", | |
], | |
[ | |
/^(https?):\/\/gist\.github(?:usercontent)?\.com\/(.+?\/[0-9a-f]+\/raw\/(?:[0-9a-f]+\/)?.+)$/i, | |
"$1://gist.githack.com/$2", | |
], | |
[ | |
/^(https?):\/\/git\.sr\.ht\/(~[^\/]+\/[^\/]+\/blob\/.+\/.+)/i, | |
"$1://srht.githack.com/$2", | |
], | |
[ | |
/^(https?):\/\/hg\.sr\.ht\/(~[^\/]+\/[^\/]+\/raw\/.+)/i, | |
"$1://srhgt.githack.com/$2", | |
], | |
]; | |
function mergeSlashes(url) { | |
try { | |
var url = new URL(url); | |
} catch (e) { | |
return url; | |
} | |
url.pathname = url.pathname.replace(/\/\/+/gi, "/"); | |
return url.toString(); | |
} | |
function maybeConvertUrl(url) { | |
for (var i in TEMPLATES) { | |
var [pattern, template] = TEMPLATES[i]; | |
if (pattern.test(url)) { | |
return url.replace(pattern, template); | |
} | |
} | |
} | |
function cdnize(url) { | |
return url.replace(/^(\w+):\/\/(\w+)/, "$1://$2cdn"); | |
} | |
return new Promise((resolve) => { | |
var ghUrl = maybeConvertUrl(u); | |
var dev, prod; | |
if (ghUrl) { | |
var matches = ghUrl.match( | |
/^(\w+:\/\/(raw).githack.com\/([^\/]+)\/([^\/]+))\/([^\/]+)\/(.*)/i | |
); | |
if (!matches) { | |
dev = ghUrl; | |
prod = cdnize(ghUrl); | |
setValid(); | |
} else if (matches[2] === "raw") { | |
dev = ghUrl; | |
let apiUrl = `${GITHUB_API_URL}/repos/${matches[3]}/${matches[4]}/git/refs/heads/${matches[5]}`; | |
fetch(apiUrl) | |
.then((res) => { | |
if (res.ok) return res.json(); | |
}) | |
.then((data) => { | |
let ref = | |
data && data.object && data.object.sha | |
? data.object.sha | |
: matches[5]; | |
prod = cdnize(`${matches[1]}/${ref}/${matches[6]}`); | |
resolve({ prod, dev }); | |
}); | |
} | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment