加入redirtype, 指定url不使用跟随重定向.

pull/48/head
fcying 2021-12-26 10:59:01 +08:00
parent d978d13efb
commit a2424010f1
1 changed files with 10 additions and 5 deletions

View File

@ -70,15 +70,19 @@ async function fetchHandler(e) {
const exp3 = /^(?:https?:\/\/)?github\.com\/.+?\/.+?\/(?:info|git-).*$/i const exp3 = /^(?:https?:\/\/)?github\.com\/.+?\/.+?\/(?:info|git-).*$/i
const exp4 = /^(?:https?:\/\/)?raw\.(?:githubusercontent|github)\.com\/.+?\/.+?\/.+?\/.+$/i const exp4 = /^(?:https?:\/\/)?raw\.(?:githubusercontent|github)\.com\/.+?\/.+?\/.+?\/.+$/i
const exp5 = /^(?:https?:\/\/)?gist\.(?:githubusercontent|github)\.com\/.+?\/.+?\/.+$/i const exp5 = /^(?:https?:\/\/)?gist\.(?:githubusercontent|github)\.com\/.+?\/.+?\/.+$/i
if (path.search(exp1) === 0 || path.search(exp5) === 0 || !Config.cnpmjs && (path.search(exp3) === 0 || path.search(exp4) === 0)) { const exp6 = /^(?:https?:\/\/)?github\.com\/.+?\/.+?\/releases\/latest\/?$/i
return httpHandler(req, path) if (path.search(exp6) === 0) {
return httpHandler(req, path, 'manual')
}
else if (path.search(exp1) === 0 || path.search(exp5) === 0 || !Config.cnpmjs && (path.search(exp3) === 0 || path.search(exp4) === 0)) {
return httpHandler(req, path, 'follow')
} else if (path.search(exp2) === 0) { } else if (path.search(exp2) === 0) {
if (Config.jsdelivr){ if (Config.jsdelivr){
const newUrl = path.replace('/blob/', '@').replace(/^(?:https?:\/\/)?github\.com/, 'https://cdn.jsdelivr.net/gh') const newUrl = path.replace('/blob/', '@').replace(/^(?:https?:\/\/)?github\.com/, 'https://cdn.jsdelivr.net/gh')
return Response.redirect(newUrl, 302) return Response.redirect(newUrl, 302)
}else{ }else{
path = path.replace('/blob/', '/raw/') path = path.replace('/blob/', '/raw/')
return httpHandler(req, path) return httpHandler(req, path, 'follow')
} }
} else if (path.search(exp3) === 0) { } else if (path.search(exp3) === 0) {
const newUrl = path.replace(/^(?:https?:\/\/)?github\.com/, 'https://github.com.cnpmjs.org') const newUrl = path.replace(/^(?:https?:\/\/)?github\.com/, 'https://github.com.cnpmjs.org')
@ -95,8 +99,9 @@ async function fetchHandler(e) {
/** /**
* @param {Request} req * @param {Request} req
* @param {string} pathname * @param {string} pathname
* @param {string} redirtype
*/ */
function httpHandler(req, pathname) { function httpHandler(req, pathname, redirtype) {
const reqHdrRaw = req.headers const reqHdrRaw = req.headers
// preflight // preflight
@ -120,7 +125,7 @@ function httpHandler(req, pathname) {
const reqInit = { const reqInit = {
method: req.method, method: req.method,
headers: reqHdrNew, headers: reqHdrNew,
redirect: 'follow', redirect: redirtype,
body: req.body body: req.body
} }
return proxy(urlObj, reqInit, rawLen, 0) return proxy(urlObj, reqInit, rawLen, 0)