Skip to content

Commit

Permalink
利cnpmjs简化代码和设置
Browse files Browse the repository at this point in the history
  • Loading branch information
Purple-CSGO committed Jan 6, 2022
1 parent 1d691c7 commit 153bfb7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 203 deletions.
76 changes: 40 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
## 路线

- [x] repo
- [x] get
- [X] repo
- [X] get
- [ ] bucket
- [ ] submit
- [ ] list
Expand All @@ -43,19 +43,20 @@
如果有配置好的API服务可以直接使用,API[参考这里](##API设计),这里给出可用的域名,请尽量自己搭建减少这里的压力,毕竟是免费的,也可[提交你的域名](https://github.com/One-Studio/better-github-api/issues)

- https://api.upup.cool
- ...
- ...

下面是使用举例:

| 含义 | URL |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 获取[advancedfx](https://github.com/advancedfx/advancedfx)仓库的最新版本号 | https://api.upup.cool/repo/advancedfx/advancedfx/version |
| 获取[advancedfx](https://github.com/advancedfx/advancedfx)仓库的最新版本包含hlae字符串的zip压缩包 | https://api.upup.cool/repo/advancedfx/advancedfx/&hlae&&&.zip |

| 含义 | URL |
| ---------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------- |
| 获取[advancedfx](https://github.com/advancedfx/advancedfx)仓库的最新版本号 | https://api.upup.cool/repo/advancedfx/advancedfx/version |
| 获取[advancedfx](https://github.com/advancedfx/advancedfx)仓库的最新版本包含hlae字符串的zip压缩包 | https://api.upup.cool/repo/advancedfx/advancedfx/&hlae&&&.zip |
| 获取[advancedfx](https://github.com/advancedfx/advancedfx)仓库的最新版本号v2.115.0的HLAE_Setup.exe安装器 | https://api.upup.cool/repo/advancedfx/advancedfx/v2.115.0/&HLAE_Setup.exe |
| 获取本仓库最新源代码 | https://api.upup.cool/repo/One-Studio/better-github-api/source |
| 获取本仓库最新简化信息 | https://api.upup.cool/repo/One-Studio/better-github-api/info |
| 获取...上述zip压缩包,使用KV键值对[预先存好的信息](##/get) | https://api.upup.cool/get/hlae |
| 获取[advancedfx](https://github.com/advancedfx/advancedfx)仓库的最新版本号 | https://api.upup.cool/get/hlae/version |
| 获取本仓库最新源代码 | https://api.upup.cool/repo/One-Studio/better-github-api/source |
| 获取本仓库最新简化信息 | https://api.upup.cool/repo/One-Studio/better-github-api/info |
| 获取...上述zip压缩包,使用KV键值对[预先存好的信息](##/get) | https://api.upup.cool/get/hlae |
| 获取[advancedfx](https://github.com/advancedfx/advancedfx)仓库的最新版本号 | https://api.upup.cool/get/hlae/version |

## 部署

Expand All @@ -71,29 +72,23 @@
### API服务

1. 复制项目文件中`api.js`的内容到左侧代码框,`保存并部署`
2. 修改 **ASSET_URL**, **Config**, **PREFI;**
3. 修改 **HOME_PAGE**,请求不加参数时跳转到主页;
4. 保存并部署;
5. 返回Workers界面,点击KV设置命名空间,添加两个命名空间:`KV``BUCKET`(可修改),回到Worker的设置页绑定命名空间,注意变量名称必须是`KV``BUCKET`
6. 提交KV键值对:给刚才绑定KV变量名的命名空间添加键值对(仓库简称-值)以使用get方法,[参照这里](##/get)
7. (可选)修改Worker的域名/路由。
2. 修改 **HOME_PAGE**,请求不加参数时跳转到主页;
3. 保存并部署;
4. 返回Workers界面,点击KV设置命名空间,添加两个命名空间:`KV``BUCKET`(可修改),回到Worker的设置页绑定命名空间,注意变量名称必须是`KV``BUCKET`
5. 提交KV键值对:给刚才绑定KV变量名的命名空间添加键值对(仓库简称-值)以使用get方法,[参照这里](##/get)
6. (可选)修改Worker的域名/路由。

#### 配置参数说明:

**ASSET_URL** 修改为Worker的域名,如用自定义域名也要改成对应的。

> ↓一般不用改
**Config** clone是否使用cnpm,项目文件是否使用jsDeliver的开关,1开,0关。

**PREFIX** 前缀,,默认(根路径情况为"/"),如果自定义路由为example.com/gh/*,请将PREFIX改为 '/gh/',注意,少一个杠都会错!
****HOME_PAGE**,请求不加参数时跳转到主页ASSET_URL** 修改为Worker的域名,如用自定义域名也要改成对应的。

#### 域名修改说明:

**如果有自己的域名**,可以给CDN和API服务分配分配到两个子域名中。使用CloudFlare做域名的DNS服务器之后,按下表设置DNS:


| 类型 | 名称 | 内容 |
| ---- | ---- | ------- |
| ------ | ------ | --------- |
| A | api | 8.8.8.8 |
| A | cdn | 8.8.8.8 |

Expand All @@ -110,8 +105,9 @@ KV(Key-Value)键值对:CloudFlare提供了免费1GB的键值对存储功

### 一级API


| 参数 | 含义 |
| ------- | ------------------------------------------------------- |
| --------- | --------------------------------------------------------- |
| /repo | 获取GitHub仓库信息 |
| /get | 利用KV中已有的键值对快速获取GitHub仓库信息 |
| /bucket | 与get类似,使用KV缓存的键值信息简化参数复杂度,加快响应 |
Expand All @@ -122,8 +118,9 @@ KV(Key-Value)键值对:CloudFlare提供了免费1GB的键值对存储功

### /repo


| API | 含义 |
| ---------------------------------- | ------------------------------------------------------------ |
| ------------------------------------ | -------------------------------------------------------------- |
| /`仓库主`/`仓库名` | 获取该仓库的最新版本的唯一附件 |
| /`仓库主`/`仓库名`/version | 获取该仓库的最新版本的版本号 |
| /`仓库主`/`仓库名`/source | 获取该仓库的最新版本的源代码下载地址 |
Expand All @@ -135,25 +132,29 @@ KV(Key-Value)键值对:CloudFlare提供了免费1GB的键值对存储功
| /`仓库主`/`仓库名`/`版本`/info | 获取该仓库的给定`版本`的信息,如版本号和附件 |
| /`仓库主`/`仓库名`/`版本`/`过滤器` | 获取该仓库的给定`版本`的附件,同时**过滤附件名**得到唯一附件 |


| 仓库信息 | 类型 | 示例 |
| -------- | ------ | ----------------- |
| ---------- | -------- | ------------------- |
| 仓库主 | string | One-Studio |
| 仓库名 | string | better-github-api |


| 版本 | 含义 | 示例 |
| ------ | -------------- | -------- |
| -------- | ---------------- | ---------- |
| latest | 最新版本 | latest |
| 其他 | 指定的其他版本 | v2.116.0 |


| info成员 | 类型 | 含义 |
| -------- | ----------- | --------- |
| ---------- | ------------- | ----------- |
| version | string | 版本号 |
| source | string | 源代码URL |
| assets | asset array | 附件 |
| log | string | 更新日志 |


| asset成员 | 类型 | 含义 |
| -------------------- | ------ | -------------------------------------- |
| ---------------------- | -------- | ---------------------------------------- |
| name | string | 附件名 |
| size | int | 附件大小 |
| browser_download_url | string | 附件下载链接 (TODO 原始 or 加速后?) |
Expand All @@ -168,8 +169,9 @@ filter用`&`分隔的过滤器各个部分,**必须&开头**,格式为 &`inc

> 右侧连通的空白部分&可省去。

| 过滤器部分 | 含义 ||
| ---------- | ---------- | ---- |
| ------------ | ------------ | ------ |
| include | 包含字符串 | hlae |
| exclude | 排除字符串 | |
| start | 开头字符串 | |
Expand All @@ -179,8 +181,9 @@ filter用`&`分隔的过滤器各个部分,**必须&开头**,格式为 &`inc

> 仓库简称后的API与repo一致,区别在使用KV存储的信息(仓库主、仓库名、filter等)简化API。

| API | 含义 |
| ------------------------------- | ------------------------------------------------------------ |
| --------------------------------- | -------------------------------------------------------------- |
| /get/`仓库简称` | 获取该仓库的最新版本的唯一附件 |
| /get/`仓库简称`/version | 获取该仓库的最新版本的版本号 |
| /get/`仓库简称`/source | 获取该仓库的最新版本的源代码下载地址 |
Expand All @@ -194,8 +197,9 @@ filter用`&`分隔的过滤器各个部分,**必须&开头**,格式为 &`inc

至于KV里如何存`简称-全称`的对应关系:


|| 类型 | 含义 ||
| ------ | ------ | ---------------------- | -------------------------------------------------------- |
| -------- | -------- | ------------------------ | ---------------------------------------------------------- |
| repo | string | 仓库主/仓库名 | advancedfx/advancedfx |
| filter | string | 附件名过滤器 | &hlae&&&.zip |
| info | object | KV键值对信息(多语言) | {"zh_CN": "hlae的zip安装包", "zh_TW": "hlae的zip安裝器"} |
Expand All @@ -217,8 +221,9 @@ filter用`&`分隔的过滤器各个部分,**必须&开头**,格式为 &`inc

### /bucket (未完成)


| API示例 | 含义 |
| -------------------- | ------------------ |
| ---------------------- | -------------------- |
| /bucket | 获取所有bucket信息 |
| /bucket/hlae | 获取hlae最新安装包 |
| /bucket/hlae/version | |
Expand All @@ -227,4 +232,3 @@ filter用`&`分隔的过滤器各个部分,**必须&开头**,格式为 &`inc
| /get/hlae | |

### /submit(未完成)

172 changes: 5 additions & 167 deletions api.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,12 @@
// 以下是better-github-api的核心设置 //
//////////////////////////////////////////

/////常用
//Workers部署的地址、链接
const ASSET_URL = "api.upup.cool";

//主页 请求无参数时跳转主页 要求有https://
const HOME_PAGE = "https://upup.cool";

//认证?待定
const AUTH = "";
/////

// 前缀,如果自定义路由为example.com/gh/*,将PREFIX改为 '/gh/',注意,少一个杠都会错!
const PREFIX = "/";

// git使用cnpmjs镜像、分支文件使用jsDelivr镜像的开关,0为关闭,默认开启
const Config = {
jsdelivr: 1,
cnpmjs: 1,
};

//////////////////////////////////////////
// 以下是gh-proxy的核心代码,稍作修改适配API //
/////////////////////////////////////////
// 以下是better-github-api的核心代码 //
//////////////////////////////////////////

// 获取handleRequest对请求处理的结果
addEventListener("fetch", (event) => {
Expand All @@ -41,161 +24,16 @@ addEventListener("fetch", (event) => {
);
});

/** @type {RequestInit} */
const PREFLIGHT_INIT = {
status: 204,
headers: new Headers({
"access-control-allow-origin": "*",
"access-control-allow-methods":
"GET,POST,PUT,PATCH,TRACE,DELETE,HEAD,OPTIONS",
"access-control-max-age": "1728000",
}),
};

/**
* @param {any} body
* @param {number} status
* @param {Object<string, string>} headers
*/
function makeRes(body, status = 200, headers = {}) {
headers["access-control-allow-origin"] = "*";
return new Response(body, { status, headers });
}

/**
* @param {string} urlStr
*/
function newUrl(urlStr) {
try {
return new URL(urlStr);
} catch (err) {
return null;
}
}

/**
*
* @param {URL} urlObj
* @param {RequestInit} reqInit
*/
async function proxy(urlObj, reqInit, rawLen) {
const res = await fetch(urlObj.href, reqInit);
const resHdrOld = res.headers;
const resHdrNew = new Headers(resHdrOld);

// verify
if (rawLen) {
const newLen = resHdrOld.get("content-length") || "";
const badLen = rawLen !== newLen;

if (badLen) {
return makeRes(res.body, 400, {
"--error": `bad len: ${newLen}, except: ${rawLen}`,
"access-control-expose-headers": "--error",
});
}
}
const status = res.status;
resHdrNew.set("access-control-expose-headers", "*");
resHdrNew.set("access-control-allow-origin", "*");

resHdrNew.delete("content-security-policy");
resHdrNew.delete("content-security-policy-report-only");
resHdrNew.delete("clear-site-data");

return new Response(res.body, {
status,
headers: resHdrNew,
});
}

/**
* @param {Request} req
* @param {string} pathname
*/
function httpHandler(req, pathname) {
const reqHdrRaw = req.headers;

// preflight
if (
req.method === "OPTIONS" &&
reqHdrRaw.has("access-control-request-headers")
) {
return new Response(null, PREFLIGHT_INIT);
}

let rawLen = "";

const reqHdrNew = new Headers(reqHdrRaw);

let urlStr = pathname;
if (urlStr.startsWith("github")) {
urlStr = "https://" + urlStr;
}
const urlObj = newUrl(urlStr);

/** @type {RequestInit} */
const reqInit = {
method: req.method,
headers: reqHdrNew,
redirect: "follow",
body: req.body,
};
return proxy(urlObj, reqInit, rawLen);
}

/**
* @param {Request} req
* @param {string} path
* @note 方法名修改为cdnHandler,参数增加req以返回请求,开头的几行删除
*/
async function cdnHandler(req, path) {
const exp1 =
/^(?:https?:\/\/)?github\.com\/.+?\/.+?\/(?:releases|archive)\/.*$/i;
const exp2 = /^(?:https?:\/\/)?github\.com\/.+?\/.+?\/(?:blob)\/.*$/i;
const exp3 = /^(?:https?:\/\/)?github\.com\/.+?\/.+?\/(?:info|git-).*$/i;
const exp4 =
/^(?:https?:\/\/)?raw\.githubusercontent\.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))
) {
return httpHandler(req, path);
} else if (path.search(exp2) === 0) {
if (Config.jsdelivr) {
const newUrl = path
.replace("/blob/", "@")
.replace(/^(?:https?:\/\/)?github\.com/, "https://cdn.jsdelivr.net/gh");
return Response.redirect(newUrl, 302);
} else {
path = path.replace("/blob/", "/raw/");
return httpHandler(req, path);
}
} 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);
}
}
path = path.replace("github.com", "github.com.cnpmjs.org");

//////////////////////////////////////////
// 以下是better-github-api的核心代码 //
//////////////////////////////////////////
return Response.redirect(path, 302);
}

function getJsonLength(jsonData){
var jsonLength = 0;
Expand Down

0 comments on commit 153bfb7

Please sign in to comment.