feat: 同步配置前, 预处理订阅, 防止同时请求过多

This commit is contained in:
xream 2024-02-14 19:51:44 +08:00
parent 1b948cdf52
commit f85e360ea8
No known key found for this signature in database
GPG Key ID: 1D2C5225471789F9
4 changed files with 122 additions and 15 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "sub-store", "name": "sub-store",
"version": "2.14.217", "version": "2.14.218",
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.", "description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.",
"main": "src/main.js", "main": "src/main.js",
"scripts": { "scripts": {

View File

@ -1,8 +1,14 @@
import { version } from '../../package.json'; import { version } from '../../package.json';
import { SETTINGS_KEY, ARTIFACTS_KEY } from '@/constants'; import {
SETTINGS_KEY,
ARTIFACTS_KEY,
SUBS_KEY,
COLLECTIONS_KEY,
} from '@/constants';
import $ from '@/core/app'; import $ from '@/core/app';
import { produceArtifact } from '@/restful/sync'; import { produceArtifact } from '@/restful/sync';
import { syncToGist } from '@/restful/artifacts'; import { syncToGist } from '@/restful/artifacts';
import { findByName } from '@/utils/database';
!(async function () { !(async function () {
const settings = $.read(SETTINGS_KEY); const settings = $.read(SETTINGS_KEY);
@ -30,23 +36,83 @@ async function doSync() {
const files = {}; const files = {};
try { try {
const invalid = [];
const allSubs = $.read(SUBS_KEY);
const allCols = $.read(COLLECTIONS_KEY);
const subNames = [];
allArtifacts.map((artifact) => {
if (artifact.sync && artifact.source) {
if (artifact.type === 'subscription') {
const subName = artifact.source;
const sub = findByName(allSubs, subName);
if (sub && sub.url && !subNames.includes(subName)) {
subNames.push(subName);
}
} else if (artifact.type === 'collection') {
const collection = findByName(allCols, artifact.source);
if (collection && Array.isArray(collection.subscriptions)) {
collection.subscriptions.map((subName) => {
const sub = findByName(allSubs, subName);
if (sub && sub.url && !subNames.includes(subName)) {
subNames.push(subName);
}
});
}
}
}
});
if (subNames.length > 0) {
await Promise.all(
subNames.map(async (subName) => {
try {
await produceArtifact({
type: 'subscription',
name: subName,
});
} catch (e) {
// $.error(`${e.message ?? e}`);
}
}),
);
}
await Promise.all( await Promise.all(
allArtifacts.map(async (artifact) => { allArtifacts.map(async (artifact) => {
if (artifact.sync) { try {
$.info(`正在同步云配置:${artifact.name}...`); if (artifact.sync && artifact.source) {
const output = await produceArtifact({ $.info(`正在同步云配置:${artifact.name}...`);
type: artifact.type, const output = await produceArtifact({
name: artifact.source, type: artifact.type,
platform: artifact.platform, name: artifact.source,
}); platform: artifact.platform,
produceOpts: {
'include-unsupported-proxy':
artifact.includeUnsupportedProxy,
},
});
files[encodeURIComponent(artifact.name)] = { // if (!output || output.length === 0)
content: output, // throw new Error('该配置的结果为空 不进行上传');
};
files[encodeURIComponent(artifact.name)] = {
content: output,
};
}
} catch (e) {
$.error(
`同步配置 ${artifact.name} 发生错误: ${e.message ?? e}`,
);
invalid.push(artifact.name);
} }
}), }),
); );
if (invalid.length > 0) {
throw new Error(
`同步配置 ${invalid.join(', ')} 发生错误 详情请查看日志`,
);
}
const resp = await syncToGist(files); const resp = await syncToGist(files);
const body = JSON.parse(resp.body); const body = JSON.parse(resp.body);
@ -71,8 +137,8 @@ async function doSync() {
$.write(allArtifacts, ARTIFACTS_KEY); $.write(allArtifacts, ARTIFACTS_KEY);
$.notify('🌍 Sub-Store', '全部订阅同步成功!'); $.notify('🌍 Sub-Store', '全部订阅同步成功!');
} catch (err) { } catch (e) {
$.notify('🌍 Sub-Store', '同步订阅失败', `原因:${err}`); $.notify('🌍 Sub-Store', '同步订阅失败', `原因:${e.message ?? e}`);
$.error(`无法同步订阅配置到 Gist原因${err}`); $.error(`无法同步订阅配置到 Gist原因${e}`);
} }
} }

View File

@ -448,6 +448,46 @@ async function syncArtifacts() {
try { try {
const invalid = []; const invalid = [];
const allSubs = $.read(SUBS_KEY);
const allCols = $.read(COLLECTIONS_KEY);
const subNames = [];
allArtifacts.map((artifact) => {
if (artifact.sync && artifact.source) {
if (artifact.type === 'subscription') {
const subName = artifact.source;
const sub = findByName(allSubs, subName);
if (sub && sub.url && !subNames.includes(subName)) {
subNames.push(subName);
}
} else if (artifact.type === 'collection') {
const collection = findByName(allCols, artifact.source);
if (collection && Array.isArray(collection.subscriptions)) {
collection.subscriptions.map((subName) => {
const sub = findByName(allSubs, subName);
if (sub && sub.url && !subNames.includes(subName)) {
subNames.push(subName);
}
});
}
}
}
});
if (subNames.length > 0) {
await Promise.all(
subNames.map(async (subName) => {
try {
await produceArtifact({
type: 'subscription',
name: subName,
});
} catch (e) {
// $.error(`${e.message ?? e}`);
}
}),
);
}
await Promise.all( await Promise.all(
allArtifacts.map(async (artifact) => { allArtifacts.map(async (artifact) => {
try { try {

View File

@ -74,6 +74,7 @@ export default async function download(rawUrl, ua, timeout) {
// try to find in app cache // try to find in app cache
const cached = resourceCache.get(id); const cached = resourceCache.get(id);
if (!$arguments?.noCache && cached) { if (!$arguments?.noCache && cached) {
$.info(`使用缓存: ${url}`);
result = cached; result = cached;
} else { } else {
$.info( $.info(