mirror of
https://github.com/sub-store-org/Sub-Store.git
synced 2026-11-28 15:33:47 +00:00
Improve the performance of cronSyncArtifact
This commit is contained in:
1 parent
4dc40e92ae
commit
7b68f9fa85
2 files changed
+9
-4
No files matched your search
@@ -660,7 +660,8 @@ function service() {
|
||||
const allArtifacts = $.read(ARTIFACTS_KEY);
|
||||
let success = [],
|
||||
failed = [];
|
||||
for (const artifact of Object.values(allArtifacts)) {
|
||||
|
||||
await Promise.all(Object.values(allArtifacts).map(async artifact => {
|
||||
if (artifact.sync) {
|
||||
$.info(`正在同步云配置:${artifact.name}...`);
|
||||
try {
|
||||
@@ -687,10 +688,13 @@ function service() {
|
||||
});
|
||||
artifact.updated = new Date().getTime();
|
||||
const body = JSON.parse(resp.body);
|
||||
|
||||
// extract real url from gist
|
||||
artifact.url = body.files[artifact.name].raw_url.replace(
|
||||
/\/raw\/[^\/]*\/(.*)/,
|
||||
"/raw/$1"
|
||||
);
|
||||
|
||||
$.write(allArtifacts, ARTIFACTS_KEY);
|
||||
$.info(`✅ 成功同步云配置:${artifact.name}`);
|
||||
success.push(artifact);
|
||||
@@ -704,7 +708,8 @@ function service() {
|
||||
failed.push(artifact);
|
||||
}
|
||||
}
|
||||
}
|
||||
}));
|
||||
|
||||
res.json({
|
||||
success,
|
||||
failed,
|
||||
|
||||
Vendored
+2
-2
@@ -1,2 +1,2 @@
|
||||
// UPDATED AT: 2022年 4月28日 星期四 17时01分35秒 CST
|
||||
const $=API("sub-store"),Base64=new Base64Code;function service(){console.log("\n┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅\n 𝑺𝒖𝒃-𝑺𝒕𝒐𝒓𝒆 © 𝑷𝒆𝒏𝒈-𝒀𝑴\n┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅\n");const e=express(),t="settings",n="subs",r="collections",s="rules",o="builtin",i="artifacts",a="Auto Generated Sub-Store Backup",c="Sub-Store",l="Sub-Store Artifacts Repository";async function p({filename:e,content:n}){const{gistToken:r}=$.read(t);return r?new Gist({token:r,key:l}).upload({filename:e,content:n}):Promise.reject("未设置Gist Token!")}async function u(e){const{headers:t}=await $.http.get({url:e,headers:{"User-Agent":"Quantumult/1.0.13 (iPhone10,3; iOS 14.0)"}});return t[Object.keys(t).filter(e=>/SUBSCRIPTION-USERINFO/i.test(e))[0]]}function f(e){const t=Object.keys(e);let n="";for(let r of t)if(/USER-AGENT/i.test(r)){n=e[r];break}return-1!==n.indexOf("Quantumult%20X")?"QX":-1!==n.indexOf("Surge")?"Surge":-1!==n.indexOf("Decar")||-1!==n.indexOf("Loon")?"Loon":-1!==n.indexOf("Stash")?"Clash":null}async function d(e,t=!0,n){let r=n;void 0!==n&&null!=n&&0!=n.trim().length||(r="Quantumult%20X");const s=HTTP({headers:{"User-Agent":r}}),o="#"+Base64.safeEncode(e),i=$.read(o),a=`#TIME-${Base64.safeEncode(e)}`,c=(new Date).getTime()-$.read(a)>36e5;if(t&&i&&!c)return $.log(`Use cached for resource: ${e}`),i;let l="";try{l=(await s.get(e)).body}catch(e){throw new Error(e)}finally{$.write(l,o),$.write((new Date).getTime(),a)}if(0===l.replace(/\s/g,"").length)throw new Error("订阅内容为空!");return l}async function h({type:e,item:t,platform:r,useCache:s,noProcessor:o}={platform:"JSON",useCache:!1,noProcessor:!1}){if("subscription"===e){const e=t,n=await d(e.url,s,e.ua);let i=ProxyUtils.parse(n);o||(i=await ProxyUtils.process(i,e.process||[],r));const a={};return i.forEach(e=>{a[e.name]&&$.notify("🌍 『 𝑺𝒖𝒃-𝑺𝒕𝒐𝒓𝒆 』","⚠️ 订阅包含重复节点!","请仔细检测配置!",{"media-url":"https://cdn3.iconfinder.com/data/icons/seo-outline-1/512/25_code_program_programming_develop_bug_search_developer-512.png"})}),ProxyUtils.produce(i,r)}if("collection"===e){const e=$.read(n),i=t,a=i.subscriptions;let c=[];for(let t=0;t<a.length;t++){const n=e[a[t]];$.info(`正在处理子订阅:${n.name},进度--${100*((t+1)/a.length).toFixed(1)}% `);try{const e=await d(n.url,s,n.ua);let t=ProxyUtils.parse(e);o||(t=await ProxyUtils.process(t,n.process||[],r)),c=c.concat(t)}catch(e){$.error(`处理组合订阅中的子订阅: ${n.name}时出现错误:${e}! 该订阅已被跳过。`)}}if(o||(c=await ProxyUtils.process(c,i.process||[],r)),0===c.length)throw new Error("组合订阅中不含有效节点!");const l={};return c.forEach(e=>{l[e.name]&&$.notify("🌍 『 𝑺𝒖𝒃-𝑺𝒕𝒐𝒓𝒆 』","⚠️ 订阅包含重复节点!","请仔细检测配置!",{"media-url":"https://cdn3.iconfinder.com/data/icons/seo-outline-1/512/25_code_program_programming_develop_bug_search_developer-512.png"})}),ProxyUtils.produce(c,r)}if("rule"===e){const e=t;let n=[];for(let t=0;t<e.urls.length;t++){const r=e.urls[t];$.info(`正在处理URL:${r},进度--${100*((t+1)/e.urls.length).toFixed(1)}% `);try{const{body:e}=await $.http.get(r),t=RuleUtils.parse(e);n=n.concat(t)}catch(e){$.error(`处理分流订阅中的URL: ${r}时出现错误:${e}! 该订阅已被跳过。`)}}return n=await RuleUtils.process(n,[{type:"Remove Duplicate Filter"}]),RuleUtils.produce(n,r)}}$.read(n)||$.write({},n),$.read(r)||$.write({},r),$.read(t)||$.write({},t),$.read(s)||$.write({},s),$.read(i)||$.write({},i),$.write({rules:getBuiltInRules()},o),e.get("/download/collection/:name",async function(e,t){const{name:s}=e.params,{cache:o}=e.query||"false",{raw:i}=e.query||"false",a=e.query.target||f(e.headers)||"JSON",c=void 0===o?"JSON"===a||"URI"===a:o,l=$.read(r)[s];$.info(`正在下载组合订阅:${s}`);const p=$.read(n),d=l.subscriptions;if(d.length>0){const e=p[d[0]],n=await u(e.url);n&&t.set("subscription-userinfo",n)}if(l)try{const e=await h({type:"collection",item:l,platform:a,useCache:c,noProcessor:i});"JSON"===a?t.set("Content-Type","application/json;charset=utf-8").send(e):t.send(e)}catch(e){$.notify("🌍 『 𝑺𝒖𝒃-𝑺𝒕𝒐𝒓𝒆 』 下载组合订阅失败",`❌ 下载组合订阅错误:${s}!`,`🤔 原因:${e}`),t.status(500).json({status:"failed",message:e})}else $.notify("🌍 『 𝑺𝒖𝒃-𝑺𝒕𝒐𝒓𝒆 』 下载组合订阅失败",`❌ 未找到组合订阅:${s}!`),t.status(404).json({status:"failed"})}),e.get("/download/:name",async function(e,t){const{name:r}=e.params,{cache:s}=e.query,{raw:o}=e.query||"false",i=e.query.target||f(e.headers)||"JSON",a=void 0===s?"JSON"===i||"URI"===i:s;$.info(`正在下载订阅:${r}`);const c=$.Line truncated
|
||||
// UPDATED AT: 2022年 4月30日 星期六 11时28分51秒 CST
|
||||
const $=API("sub-store"),Base64=new Base64Code;function service(){console.log("\n┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅\n 𝑺𝒖𝒃-𝑺𝒕𝒐𝒓𝒆 © 𝑷𝒆𝒏𝒈-𝒀𝑴\n┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅\n");const e=express(),t="settings",n="subs",r="collections",s="rules",o="builtin",i="artifacts",a="Auto Generated Sub-Store Backup",c="Sub-Store",l="Sub-Store Artifacts Repository";async function p({filename:e,content:n}){const{gistToken:r}=$.read(t);return r?new Gist({token:r,key:l}).upload({filename:e,content:n}):Promise.reject("未设置Gist Token!")}async function u(e){const{headers:t}=await $.http.get({url:e,headers:{"User-Agent":"Quantumult/1.0.13 (iPhone10,3; iOS 14.0)"}});return t[Object.keys(t).filter(e=>/SUBSCRIPTION-USERINFO/i.test(e))[0]]}function f(e){const t=Object.keys(e);let n="";for(let r of t)if(/USER-AGENT/i.test(r)){n=e[r];break}return-1!==n.indexOf("Quantumult%20X")?"QX":-1!==n.indexOf("Surge")?"Surge":-1!==n.indexOf("Decar")||-1!==n.indexOf("Loon")?"Loon":-1!==n.indexOf("Stash")?"Clash":null}async function d(e,t=!0,n){let r=n;void 0!==n&&null!=n&&0!=n.trim().length||(r="Quantumult%20X");const s=HTTP({headers:{"User-Agent":r}}),o="#"+Base64.safeEncode(e),i=$.read(o),a=`#TIME-${Base64.safeEncode(e)}`,c=(new Date).getTime()-$.read(a)>36e5;if(t&&i&&!c)return $.log(`Use cached for resource: ${e}`),i;let l="";try{l=(await s.get(e)).body}catch(e){throw new Error(e)}finally{$.write(l,o),$.write((new Date).getTime(),a)}if(0===l.replace(/\s/g,"").length)throw new Error("订阅内容为空!");return l}async function h({type:e,item:t,platform:r,useCache:s,noProcessor:o}={platform:"JSON",useCache:!1,noProcessor:!1}){if("subscription"===e){const e=t,n=await d(e.url,s,e.ua);let i=ProxyUtils.parse(n);o||(i=await ProxyUtils.process(i,e.process||[],r));const a={};return i.forEach(e=>{a[e.name]&&$.notify("🌍 『 𝑺𝒖𝒃-𝑺𝒕𝒐𝒓𝒆 』","⚠️ 订阅包含重复节点!","请仔细检测配置!",{"media-url":"https://cdn3.iconfinder.com/data/icons/seo-outline-1/512/25_code_program_programming_develop_bug_search_developer-512.png"})}),ProxyUtils.produce(i,r)}if("collection"===e){const e=$.read(n),i=t,a=i.subscriptions;let c=[];for(let t=0;t<a.length;t++){const n=e[a[t]];$.info(`正在处理子订阅:${n.name},进度--${100*((t+1)/a.length).toFixed(1)}% `);try{const e=await d(n.url,s,n.ua);let t=ProxyUtils.parse(e);o||(t=await ProxyUtils.process(t,n.process||[],r)),c=c.concat(t)}catch(e){$.error(`处理组合订阅中的子订阅: ${n.name}时出现错误:${e}! 该订阅已被跳过。`)}}if(o||(c=await ProxyUtils.process(c,i.process||[],r)),0===c.length)throw new Error("组合订阅中不含有效节点!");const l={};return c.forEach(e=>{l[e.name]&&$.notify("🌍 『 𝑺𝒖𝒃-𝑺𝒕𝒐𝒓𝒆 』","⚠️ 订阅包含重复节点!","请仔细检测配置!",{"media-url":"https://cdn3.iconfinder.com/data/icons/seo-outline-1/512/25_code_program_programming_develop_bug_search_developer-512.png"})}),ProxyUtils.produce(c,r)}if("rule"===e){const e=t;let n=[];for(let t=0;t<e.urls.length;t++){const r=e.urls[t];$.info(`正在处理URL:${r},进度--${100*((t+1)/e.urls.length).toFixed(1)}% `);try{const{body:e}=await $.http.get(r),t=RuleUtils.parse(e);n=n.concat(t)}catch(e){$.error(`处理分流订阅中的URL: ${r}时出现错误:${e}! 该订阅已被跳过。`)}}return n=await RuleUtils.process(n,[{type:"Remove Duplicate Filter"}]),RuleUtils.produce(n,r)}}$.read(n)||$.write({},n),$.read(r)||$.write({},r),$.read(t)||$.write({},t),$.read(s)||$.write({},s),$.read(i)||$.write({},i),$.write({rules:getBuiltInRules()},o),e.get("/download/collection/:name",async function(e,t){const{name:s}=e.params,{cache:o}=e.query||"false",{raw:i}=e.query||"false",a=e.query.target||f(e.headers)||"JSON",c=void 0===o?"JSON"===a||"URI"===a:o,l=$.read(r)[s];$.info(`正在下载组合订阅:${s}`);const p=$.read(n),d=l.subscriptions;if(d.length>0){const e=p[d[0]],n=await u(e.url);n&&t.set("subscription-userinfo",n)}if(l)try{const e=await h({type:"collection",item:l,platform:a,useCache:c,noProcessor:i});"JSON"===a?t.set("Content-Type","application/json;charset=utf-8").send(e):t.send(e)}catch(e){$.notify("🌍 『 𝑺𝒖𝒃-𝑺𝒕𝒐𝒓𝒆 』 下载组合订阅失败",`❌ 下载组合订阅错误:${s}!`,`🤔 原因:${e}`),t.status(500).json({status:"failed",message:e})}else $.notify("🌍 『 𝑺𝒖𝒃-𝑺𝒕𝒐𝒓𝒆 』 下载组合订阅失败",`❌ 未找到组合订阅:${s}!`),t.status(404).json({status:"failed"})}),e.get("/download/:name",async function(e,t){const{name:r}=e.params,{cache:s}=e.query,{raw:o}=e.query||"false",i=e.query.target||f(e.headers)||"JSON",a=void 0===s?"JSON"===i||"URI"===i:s;$.info(`正在下载订阅:${r}`);const c=$.Line truncated
|
||||
Reference in new issue
Block a user