修复判断最新tags的漏洞,修复asyncFilter导致EMFILE: too many open files错误,修复game.promises.readFile的类型问题

This commit is contained in:
nonameShijian 2024-04-16 21:41:17 +08:00
parent 86a98eb3c8
commit 95fd48c3c7
3 changed files with 56 additions and 34 deletions

View File

@ -67,7 +67,7 @@ export class GamePromises {
} }
/** /**
* @param {string} filename * @param {string} filename
* @returns {Promise<Buffer>} * @returns {Promise<ArrayBuffer | Buffer>}
*/ */
readFile(filename) { readFile(filename) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {

View File

@ -1,4 +1,4 @@
import { ui, game } from "../../noname.js"; import { ui, game, lib } from "../../noname.js";
// https://github.com/libccy/noname/archive/refs/tags/v1.10.10.zip // https://github.com/libccy/noname/archive/refs/tags/v1.10.10.zip
@ -21,6 +21,9 @@ if (localStorage.getItem("noname_authorization")) {
defaultHeaders["Authorization"] = `token ${localStorage.getItem("noname_authorization")}`; defaultHeaders["Authorization"] = `token ${localStorage.getItem("noname_authorization")}`;
} }
/**
* 获取github授权的token
*/
export async function gainAuthorization() { export async function gainAuthorization() {
if (!localStorage.getItem("noname_authorization") && !sessionStorage.getItem("noname_authorization")) { if (!localStorage.getItem("noname_authorization") && !sessionStorage.getItem("noname_authorization")) {
const result = await game.promises.prompt("请输入您github的token以解除访问每小时60次的限制"); const result = await game.promises.prompt("请输入您github的token以解除访问每小时60次的限制");
@ -85,9 +88,10 @@ export function parseSize(limit) {
/** /**
* 对比版本号 * 对比版本号
* @param { string } ver1 * @param { string } ver1 版本号1
* @param { string } ver2 * @param { string } ver2 版本号2
* @returns { -1 | 0 | 1 } * @returns { -1 | 0 | 1 } -1为ver1 < ver2, 0为ver1 == ver2, 1为ver1 > ver2
* @throws {Error}
*/ */
export function checkVersion(ver1, ver2) { export function checkVersion(ver1, ver2) {
if (typeof ver1 !== "string") ver1 = String(ver1); if (typeof ver1 !== "string") ver1 = String(ver1);
@ -243,7 +247,7 @@ export async function getRepoTagDescription(tagName, options = { username: "libc
/** /**
* *
* 获取仓库指定分支和指定目录内的所有文件和目录 * 获取仓库指定分支和指定(单个)目录内的所有文件和目录
* @param { string } [path = ''] 路径名称(可放参数) * @param { string } [path = ''] 路径名称(可放参数)
* @param { string } [branch = ''] 仓库分支名称 * @param { string } [branch = ''] 仓库分支名称
* @param { Object } options * @param { Object } options
@ -301,6 +305,9 @@ export async function getRepoFilesList(
/** /**
* *
* 获取仓库指定分支和指定目录内的所有文件(包含子目录的文件) * 获取仓库指定分支和指定目录内的所有文件(包含子目录的文件)
*
* **注意 此api可能会大幅度消耗请求次数请谨慎使用**
*
* @param { string } [path = ''] 路径名称(可放参数) * @param { string } [path = ''] 路径名称(可放参数)
* @param { string } [branch = ''] 仓库分支名称 * @param { string } [branch = ''] 仓库分支名称
* @param { Object } options * @param { Object } options
@ -350,7 +357,7 @@ export async function flattenRepositoryFiles(
} }
/** /**
* 请求一个文件而不是直接储存为文件 * 请求一个文件而不是直接储存为文件这样可以省内存空间
* @param { string } url * @param { string } url
* @param { (receivedBytes: number, total?:number, filename?: string) => void } [onProgress] * @param { (receivedBytes: number, total?:number, filename?: string) => void } [onProgress]
* @param { RequestInit } [options={}] * @param { RequestInit } [options={}]
@ -520,14 +527,13 @@ export function createProgress(title, max, fileName, value) {
} }
/** /**
* Retrieves the latest version tag from a GitHub repository, excluding a specific tag. * 从GitHub存储库检索最新版本(tag)不包括特定tag
* This function fetches the list of tags from the GitHub repository specified by *
* the owner and repository name, then returns the latest tag name that is not v1998. * 此函数从GitHub存储库中获取由所有者和存储库名称指定的tags列表然后返回不是v1998的最新tag名称
* @param {string} owner - The username or organization name on GitHub that owns the repository. * @param {string} owner GitHub上拥有存储库的用户名或组织名称
* @param {string} repo - The name of the repository from which to fetch tags. * @param {string} repo 要从中提取tag的存储库的名称
* @returns {Promise<string>} A promise that resolves with the name of the latest version tag, * @returns {Promise<string>} 以最新版本tag的名称解析的promise或者如果操作失败则以错误拒绝
* or rejects with an error if the operation fails. * @throws {Error} 如果获取操作失败或找不到有效tag将抛出错误
* @throws {Error} Will throw an error if the fetch operation fails or if no valid tags are found.
*/ */
export async function getLatestVersionFromGitHub(owner = "libccy", repo = "noname") { export async function getLatestVersionFromGitHub(owner = "libccy", repo = "noname") {
if (!localStorage.getItem("noname_authorization")) await gainAuthorization(); if (!localStorage.getItem("noname_authorization")) await gainAuthorization();
@ -539,18 +545,24 @@ export async function getLatestVersionFromGitHub(owner = "libccy", repo = "nonam
for (const tag of tags) { for (const tag of tags) {
const tagName = tag.name; const tagName = tag.name;
if (tagName !== "v1998") return tagName; if (tagName === "v1998") continue;
try {
checkVersion(tagName, lib.version);
return tagName;
} catch {
// 非标准版本号
}
} }
throw new Error("No valid tags found in the repository"); throw new Error("No valid tags found in the repository");
} }
/** /**
* Fetches trees from a GitHub repository within specified directories. * 从指定目录中的GitHub存储库中获取树
* @param {string[]} directories - The list of directories to fetch the trees from. * @param {string[]} directories 要从中获取树的目录列表
* @param {string} version - The version or branch to fetch the trees from. * @param {string} version 从中获取树的版本或分支
* @param {string} owner - The owner of the GitHub repository. * @param {string} [owner = 'libccy'] GitHub上拥有存储库的用户名或组织名称
* @param {string} repo - The name of the GitHub repository. * @param {string} [repo = 'noname'] GitHub存储库的名称
* @returns {Promise<{ * @returns {Promise<{
* path: string; * path: string;
* mode: string; * mode: string;

View File

@ -328,17 +328,17 @@ export const otherMenu = function (/** @type { boolean | undefined } */ connectM
} }
if (!dev) { if (!dev) {
getRepoTags() getLatestVersionFromGitHub()
.then((tags) => tags.filter((tag) => tag.name != "v1998")[0]) .then(tagName => {
.then((tag) => { game.saveConfig("check_version", tagName.slice(1));
game.saveConfig("check_version", tag.name.slice(1)); if (typeof lib.config[`version_description_${tagName}`] == "object") {
if (typeof lib.config["version_description_" + tag.name] == "object") {
/** @type { ReturnType<import('../../../../library/update.js').getRepoTagDescription> } */ /** @type { ReturnType<import('../../../../library/update.js').getRepoTagDescription> } */
const description = lib.config["version_description_" + tag.name]; const description = lib.config[`version_description_${tagName}`];
return description; return description;
} else return getRepoTagDescription(tag.name); }
else return getRepoTagDescription(tagName);
}) })
.then((description) => { .then(description => {
// 保存版本信息 // 保存版本信息
if (typeof lib.config["version_description_" + description.name] != "object") { if (typeof lib.config["version_description_" + description.name] != "object") {
game.saveConfig("version_description_" + description.name, description); game.saveConfig("version_description_" + description.name, description);
@ -436,18 +436,28 @@ export const otherMenu = function (/** @type { boolean | undefined } */ connectM
* @param { (value: T) => Promise<boolean> } predicate * @param { (value: T) => Promise<boolean> } predicate
*/ */
const asyncFilter = async (arr, predicate) => { const asyncFilter = async (arr, predicate) => {
const results = await Promise.all(arr.map(predicate)); //将arr每10个分为一个数组分别使用Promise.all
/** @type { boolean[] } */
const results = [];
for (let i = 0; i < arr.length; i += 10) {
const pushArr = arr.slice(i, i + 10);
results.push(
...await Promise.all(pushArr.map(predicate))
);
}
return arr.filter((_v, index) => results[index]); return arr.filter((_v, index) => results[index]);
}; };
const result = await asyncFilter(files.flat(), async (v) => { const result = await asyncFilter(files.flat(), async v => {
return v.size != (await game.promises.readFile(v.path)).length; return game.promises.readFile(v.path).then(data => {
}).then((arr) => arr.map((v) => v.path)); return v.size != data.byteLength;
})
}).then(arr => arr.map((v) => v.path));
console.log("需要更新的文件有:", result); console.log("需要更新的文件有:", result);
game.print("需要更新的文件有:", result); game.print("需要更新的文件有:", result);
const finish = async () => { const finish = async () => {
await lib.init.promises.js("game", "asset.js"); await lib.init.promises.js("game", "asset");
if (Array.isArray(window.noname_asset_list)) { if (Array.isArray(window.noname_asset_list)) {
game.saveConfig("asset_version", window.noname_asset_list[0]); game.saveConfig("asset_version", window.noname_asset_list[0]);
delete window.noname_asset_list; delete window.noname_asset_list;