chore: remove duplicate if branches (#155)

master
Natsuu 2025-10-09 12:56:34 +08:00 committed by GitHub
parent 9b24915abe
commit b5cbda6a0b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 11 additions and 6 deletions

View File

@ -38,7 +38,7 @@ const exp6 = /^(?:https?:\/\/)?github\.com\/.+?\/.+?\/tags.*$/i
*/ */
function makeRes(body, status = 200, headers = {}) { function makeRes(body, status = 200, headers = {}) {
headers['access-control-allow-origin'] = '*' headers['access-control-allow-origin'] = '*'
return new Response(body, {status, headers}) return new Response(body, { status, headers })
} }
@ -82,8 +82,8 @@ async function fetchHandler(e) {
return Response.redirect('https://' + urlObj.host + PREFIX + path, 301) return Response.redirect('https://' + urlObj.host + PREFIX + path, 301)
} }
// cfworker 会把路径中的 `//` 合并成 `/` // cfworker 会把路径中的 `//` 合并成 `/`
path = urlObj.href.substr(urlObj.origin.length + PREFIX.length).replace(/^https?:\/+/, 'https://') path = urlObj.href.slice(urlObj.origin.length + PREFIX.length).replace(/^https?:\/+/, 'https://')
if (path.search(exp1) === 0 || path.search(exp5) === 0 || path.search(exp6) === 0 || path.search(exp3) === 0 || path.search(exp4) === 0) { if (path.search(exp1) === 0 || path.search(exp5) === 0 || path.search(exp6) === 0 || path.search(exp3) === 0) {
return httpHandler(req, path) return httpHandler(req, path)
} else if (path.search(exp2) === 0) { } else if (path.search(exp2) === 0) {
if (Config.jsdelivr) { if (Config.jsdelivr) {
@ -94,8 +94,13 @@ async function fetchHandler(e) {
return httpHandler(req, path) return httpHandler(req, path)
} }
} else if (path.search(exp4) === 0) { } else if (path.search(exp4) === 0) {
const newUrl = path.replace(/(?<=com\/.+?\/.+?)\/(.+?\/)/, '@$1').replace(/^(?:https?:\/\/)?raw\.(?:githubusercontent|github)\.com/, 'https://cdn.jsdelivr.net/gh') if (Config.jsdelivr) {
return Response.redirect(newUrl, 302) const newUrl = path.replace(/(?<=com\/.+?\/.+?)\/(.+?\/)/, '@$1').replace(/^(?:https?:\/\/)?raw\.(?:githubusercontent|github)\.com/, 'https://cdn.jsdelivr.net/gh')
return Response.redirect(newUrl, 302)
}
else {
return httpHandler(req, path)
}
} else { } else {
return fetch(ASSET_URL + path) return fetch(ASSET_URL + path)
} }
@ -127,7 +132,7 @@ function httpHandler(req, pathname) {
} }
} }
if (!flag) { if (!flag) {
return new Response("blocked", {status: 403}) return new Response("blocked", { status: 403 })
} }
if (urlStr.search(/^https?:\/\//) !== 0) { if (urlStr.search(/^https?:\/\//) !== 0) {
urlStr = 'https://' + urlStr urlStr = 'https://' + urlStr