Compare commits

..

4 Commits

Author SHA1 Message Date
NobyDa
999271fa9d Improve resource cache key. (#190) 2023-02-08 12:30:26 +08:00
QuentinHsu
5de35c7720 🐞 fix(subscriptions): negative usage flow (#175) 2022-10-25 00:07:23 +08:00
Jasonzza
06d0c14abc fix: sync artifacts issue (#164) 2022-09-11 23:52:51 +08:00
Peng-YM
029900085c fix: cron-sync-artifacts.js path 2022-09-10 11:47:34 +08:00
7 changed files with 42 additions and 32 deletions

View File

@@ -51,4 +51,4 @@ jobs:
./backend/dist/sub-store-0.min.js
./backend/dist/sub-store-1.min.js
./backend/dist/sub-store-parser.loon.min.js
./backend/dist/cron-sync-artifacts.js
./backend/dist/cron-sync-artifacts.min.js

View File

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

View File

@@ -51,12 +51,14 @@ async function doSync() {
const body = JSON.parse(resp.body);
for (const artifact of allArtifacts) {
artifact.updated = new Date().getTime();
// extract real url from gist
artifact.url = body.files[artifact.name].raw_url.replace(
/\/raw\/[^/]*\/(.*)/,
'/raw/$1',
);
if (artifact.sync) {
artifact.updated = new Date().getTime();
// extract real url from gist
artifact.url = body.files[artifact.name].raw_url.replace(
/\/raw\/[^/]*\/(.*)/,
'/raw/$1',
);
}
}
$.write(allArtifacts, ARTIFACTS_KEY);

View File

@@ -66,8 +66,12 @@ async function getFlowInfo(req, res) {
}
// unit is KB
const upload = Number(flowHeaders.match(/upload=(\d+)/)[1]);
const download = Number(flowHeaders.match(/download=(\d+)/)[1]);
const uploadMatch = flowHeaders.match(/upload=(-?)(\d+)/)
const upload = Number(uploadMatch[1] + uploadMatch[2]);
const downloadMatch = flowHeaders.match(/download=(-?)(\d+)/)
const download = Number(downloadMatch[1] + downloadMatch[2]);
const total = Number(flowHeaders.match(/total=(\d+)/)[1]);
// optional expire timestamp

View File

@@ -201,12 +201,14 @@ async function syncAllArtifacts(_, res) {
const body = JSON.parse(resp.body);
for (const artifact of allArtifacts) {
artifact.updated = new Date().getTime();
// extract real url from gist
artifact.url = body.files[artifact.name].raw_url.replace(
/\/raw\/[^/]*\/(.*)/,
'/raw/$1',
);
if (artifact.sync) {
artifact.updated = new Date().getTime();
// extract real url from gist
artifact.url = body.files[artifact.name].raw_url.replace(
/\/raw\/[^/]*\/(.*)/,
'/raw/$1',
);
}
}
$.write(allArtifacts, ARTIFACTS_KEY);

View File

@@ -16,8 +16,13 @@ class ResourceCache {
let clear = false;
Object.entries(this.resourceCache).forEach((entry) => {
const [id, updated] = entry;
if (new Date().getTime() - updated > this.expires) {
if (!updated.time) {
// clear old version cache
delete this.resourceCache[id];
$.delete(`#${id}`);
clear = true;
}
if (new Date().getTime() - updated.time > this.expires) {
delete this.resourceCache[id];
clear = true;
}
@@ -26,9 +31,6 @@ class ResourceCache {
}
revokeAll() {
Object.keys(this.resourceCache).forEach((id) => {
$.delete(`#${id}`);
});
this.resourceCache = {};
this._persist();
}
@@ -38,17 +40,16 @@ class ResourceCache {
}
get(id) {
const updated = this.resourceCache[id];
const updated = this.resourceCache[id] && this.resourceCache[id].time;
if (updated && new Date().getTime() - updated <= this.expires) {
return $.read(`#${id}`);
return this.resourceCache[id].data;
}
return null;
}
set(id, value) {
this.resourceCache[id] = new Date().getTime();
this.resourceCache[id] = { time: new Date().getTime(), data: value }
this._persist();
$.write(value, `#${id}`);
}
}

View File

@@ -17,8 +17,13 @@ class ResourceCache {
let clear = false;
Object.entries(this.resourceCache).forEach((entry) => {
const [id, updated] = entry;
if (new Date().getTime() - updated > this.expires) {
if (!updated.time) {
// clear old version cache
delete this.resourceCache[id];
$.delete(`#${id}`);
clear = true;
}
if (new Date().getTime() - updated.time > this.expires) {
delete this.resourceCache[id];
clear = true;
}
@@ -27,9 +32,6 @@ class ResourceCache {
}
revokeAll() {
Object.keys(this.resourceCache).forEach((id) => {
$.delete(`#${id}`);
});
this.resourceCache = {};
this._persist();
}
@@ -39,17 +41,16 @@ class ResourceCache {
}
get(id) {
const updated = this.resourceCache[id];
const updated = this.resourceCache[id] && this.resourceCache[id].time;
if (updated && new Date().getTime() - updated <= this.expires) {
return $.read(`#${id}`);
return this.resourceCache[id].data;
}
return null;
}
set(id, value) {
this.resourceCache[id] = new Date().getTime();
this.resourceCache[id] = { time: new Date().getTime(), data: value }
this._persist();
$.write(value, `#${id}`);
}
}