Refactor updateList function to reset rules locally and update interval to 15 minutes
This commit is contained in:
parent
175cfe3da3
commit
f92b54e997
1 changed files with 7 additions and 13 deletions
20
index.ts
20
index.ts
|
@ -36,14 +36,12 @@ const cacheFetch = async (url: string): Promise<string> => {
|
|||
})
|
||||
}
|
||||
|
||||
let rules: string[] = [];
|
||||
let rulesText: Uint8Array | undefined;
|
||||
|
||||
|
||||
|
||||
const updateList = async () => {
|
||||
rules = [];
|
||||
rulesText = undefined;
|
||||
|
||||
let rules: string[] = [];
|
||||
|
||||
for (const filter of filterList) {
|
||||
let list = await cacheFetch(filter);
|
||||
|
||||
|
@ -57,31 +55,27 @@ const updateList = async () => {
|
|||
|
||||
rules = Array.from(new Set(rules));
|
||||
|
||||
rulesText = Bun.gzipSync(makeComment(rules.length) + rules.join('\n'))
|
||||
Bun.write(Bun.file("./cache/combined.txt.gz"), Bun.gzipSync(makeComment(rules.length) + rules.join('\n')));
|
||||
|
||||
console.log("Updated list @", new Date().toUTCString())
|
||||
}
|
||||
|
||||
const updateListInterval = setInterval(updateList, 1.8e+6) // Every 30 minutes
|
||||
const updateListInterval = setInterval(updateList, 90e4) // Every 15 minutes
|
||||
|
||||
Bun.serve({
|
||||
async fetch(request, server) {
|
||||
if (new URL(request.url).pathname !== "/") {
|
||||
return new Response(null, { status: 404 });
|
||||
}
|
||||
|
||||
if (!rulesText) {
|
||||
await updateList();
|
||||
}
|
||||
|
||||
return new Response(rulesText, {
|
||||
return new Response(Bun.file("./cache/combined.txt.gz"), {
|
||||
headers: {
|
||||
"Content-Type": "text/plain",
|
||||
"Content-Encoding": "gzip"
|
||||
}
|
||||
})
|
||||
},
|
||||
idleTimeout: 0
|
||||
idleTimeout: 1
|
||||
})
|
||||
|
||||
const makeComment = (ruleCount: number) => {
|
||||
|
|
Loading…
Add table
Reference in a new issue