Chore: fix leak thread

This commit is contained in:
SukkaW 2023-12-24 00:58:04 +08:00
parent a109091b3e
commit d7c8bf8d11
4 changed files with 26 additions and 27 deletions

View File

@ -1,10 +1,5 @@
import { fsCache } from './lib/cache-filesystem';
import { defaultRequestInit, fetchWithRetry } from './lib/fetch-retry';
import { createMemoizedPromise } from './lib/memo-promise';
import { traceAsync } from './lib/trace-runner';
export const getPublicSuffixListTextPromise = createMemoizedPromise(() => traceAsync('obtain public_suffix_list', () => fsCache.apply(
'public_suffix_list.dat',
() => fetchWithRetry('https://publicsuffix.org/list/public_suffix_list.dat', defaultRequestInit).then(r => r.text()),
{ ttl: 24 * 60 * 60 * 1000 }
)));
export const getPublicSuffixListTextPromise = createMemoizedPromise(() => traceAsync('obtain public_suffix_list', () => fetchWithRetry('https://publicsuffix.org/list/public_suffix_list.dat', defaultRequestInit).then(r => r.text())));

View File

@ -121,9 +121,16 @@ export class Cache {
}
return value;
}
destroy() {
this.db.close();
}
}
export const fsCache = new Cache({ cachePath: path.resolve(import.meta.dir, '../../.cache') });
// export const fsCache = new Cache({ cachePath: path.resolve(import.meta.dir, '../../.cache') });
// process.on('exit', () => {
// fsCache.destroy();
// });
const separator = String.fromCharCode(0);

View File

@ -1,7 +1,5 @@
import type { BunFile } from 'bun';
import { fetchWithRetry, defaultRequestInit } from './fetch-retry';
import { fsCache } from './cache-filesystem';
import picocolors from 'picocolors';
// import { TextLineStream } from './text-line-transform-stream';
// import { PolyfillTextDecoderStream } from './text-decoder-stream';

View File

@ -9,15 +9,14 @@ import { traceAsync } from './trace-runner';
import picocolors from 'picocolors';
import { normalizeDomain } from './normalize-domain';
import { fetchAssets } from './fetch-assets';
import { deserializeSet, fsCache, serializeSet } from './cache-filesystem';
const DEBUG_DOMAIN_TO_FIND: string | null = null; // example.com | null
let foundDebugDomain = false;
export function processDomainLists(domainListsUrl: string, includeAllSubDomain = false, ttl: number | null = null) {
return traceAsync(`- processDomainLists: ${domainListsUrl}`, () => fsCache.apply(
export function processDomainLists(domainListsUrl: string, includeAllSubDomain = false, _ttl: number | null = null) {
return traceAsync(`- processDomainLists: ${domainListsUrl}`, /* () => fsCache.apply(
domainListsUrl,
async () => {
*/async () => {
const domainSets = new Set<string>();
for await (const line of await fetchRemoteTextByLine(domainListsUrl)) {
@ -33,19 +32,19 @@ export function processDomainLists(domainListsUrl: string, includeAllSubDomain =
}
return domainSets;
},
});/* ,
{
ttl,
temporaryBypass: DEBUG_DOMAIN_TO_FIND !== null,
serializer: serializeSet,
deserializer: deserializeSet
}
));
)); */
}
export function processHosts(hostsUrl: string, includeAllSubDomain = false, skipDomainCheck = false, ttl: number | null = null) {
return traceAsync(`- processHosts: ${hostsUrl}`, () => fsCache.apply(
export function processHosts(hostsUrl: string, includeAllSubDomain = false, skipDomainCheck = false, _ttl: number | null = null) {
return traceAsync(`- processHosts: ${hostsUrl}`, /* () => fsCache.apply(
hostsUrl,
async () => {
*/async () => {
const domainSets = new Set<string>();
for await (const l of await fetchRemoteTextByLine(hostsUrl)) {
@ -74,14 +73,14 @@ export function processHosts(hostsUrl: string, includeAllSubDomain = false, skip
console.log(picocolors.gray('[process hosts]'), picocolors.gray(hostsUrl), picocolors.gray(domainSets.size));
return domainSets;
},
{
});
/* {
ttl,
temporaryBypass: DEBUG_DOMAIN_TO_FIND !== null,
serializer: serializeSet,
deserializer: deserializeSet
}
));
) */
}
// eslint-disable-next-line sukka-ts/no-const-enum -- bun bundler is smart, maybe?
@ -96,15 +95,15 @@ const enum ParseType {
export async function processFilterRules(
filterRulesUrl: string,
fallbackUrls?: readonly string[] | undefined | null,
ttl: number | null = null
_ttl: number | null = null
): Promise<{ white: string[], black: string[], foundDebugDomain: boolean }> {
const [white, black, warningMessages] = await traceAsync(`- processFilterRules: ${filterRulesUrl}`, () => fsCache.apply<[
const [white, black, warningMessages] = await traceAsync(`- processFilterRules: ${filterRulesUrl}`, /* () => fsCache.apply<[
white: string[],
black: string[],
warningMessages: string[]
]>(
filterRulesUrl,
async () => {
*/async () => {
const whitelistDomainSets = new Set<string>();
const blacklistDomainSets = new Set<string>();
@ -192,14 +191,14 @@ export async function processFilterRules(
Array.from(blacklistDomainSets),
warningMessages
];
},
{
});
/* {
ttl,
temporaryBypass: DEBUG_DOMAIN_TO_FIND !== null,
serializer: JSON.stringify,
deserializer: JSON.parse
}
));
) */
warningMessages.forEach(msg => {
console.warn(