From 4a3fe663f4c23dd1779e7b646b00bdf79ce9fd12 Mon Sep 17 00:00:00 2001 From: hunshnet <337490703@qq.com> Date: Fri, 10 Apr 2020 18:32:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0raw.githubusercontent.com?= =?UTF-8?q?=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + index.js | 15 +++++++++------ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index ec05980..40a92f0 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,7 @@ python版本的机器如果无法正常访问github.io会启动报错,请自 ## Changelog +* 2020.04.10 增加对`raw.githubusercontent.com`文件的支持 * 2020.04.09 增加Python版本(使用Flask) * 2020.03.23 新增了clone的支持 * 2020.03.22 初始版本 diff --git a/index.js b/index.js index 7d3464f..bbf8659 100644 --- a/index.js +++ b/index.js @@ -58,23 +58,26 @@ async function fetchHandler(e) { const urlStr = req.url const urlObj = new URL(urlStr) let path = urlObj.searchParams.get('q') - if(path) - { + if (path) { return Response.redirect('https://' + urlObj.host + '/' + path, 301) } // cfworker 会把路径中的 `//` 合并成 `/` path = urlObj.href.substr(urlObj.origin.length + 1).replace(/^https?:\/+/, 'https://') - const exp = /^(?:https?:\/\/)?github\.com\/.+?\/.+?\/(?:releases|archive)\/.*$/ + const exp1 = /^(?:https?:\/\/)?github\.com\/.+?\/.+?\/(?:releases|archive)\/.*$/ const exp2 = /^(?:https?:\/\/)?github\.com\/.+?\/.+?\/(?:blob)\/.*$/ const exp3 = /^(?:https?:\/\/)?github\.com\/.+?\/.+?\/(?:info|git-upload-pack).*$/ - if (path.search(exp)===0 || !Config.jsdelivr && path.search(exp2)===0 || !Config.cnpmjs && path.search(exp3)===0) { + const exp4 = /^(?:https?:\/\/)?raw\.githubusercontent\.com\/.+?\/.+?\/.+?\/.+$/ + if (path.search(exp1) === 0 || !Config.jsdelivr && path.search(exp2) === 0 || !Config.cnpmjs && (path.search(exp3) === 0 || path.search(exp4))) { return httpHandler(req, path) - }else if(path.search(exp2)===0) { + } else if (path.search(exp2) === 0) { const newUrl = path.replace('/blob/', '@').replace(/^(?:https?:\/\/)?github\.com/, 'https://cdn.jsdelivr.net/gh') return Response.redirect(newUrl, 302) - }else if (path.search(exp3)===0){ + } else if (path.search(exp3) === 0) { const newUrl = path.replace(/^(?:https?:\/\/)?github\.com/, 'https://github.com.cnpmjs.org') return Response.redirect(newUrl, 302) + } else if (path.search(exp4) === 0) { + const newUrl = path.replace(/(?<=com\/.+?\/.+?)\/(.+?\/)/, '@$1').replace(/^(?:https?:\/\/)?raw\.githubusercontent\.com/, 'https://cdn.jsdelivr.net/gh') + return Response.redirect(newUrl, 302) } else { return fetch(ASSET_URL + path) }