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 { defaultRequestInit, fetchWithRetry } from './lib/fetch-retry';
import { createMemoizedPromise } from './lib/memo-promise'; import { createMemoizedPromise } from './lib/memo-promise';
import { traceAsync } from './lib/trace-runner'; import { traceAsync } from './lib/trace-runner';
export const getPublicSuffixListTextPromise = createMemoizedPromise(() => traceAsync('obtain public_suffix_list', () => fsCache.apply( export const getPublicSuffixListTextPromise = createMemoizedPromise(() => traceAsync('obtain public_suffix_list', () => fetchWithRetry('https://publicsuffix.org/list/public_suffix_list.dat', defaultRequestInit).then(r => r.text())));
'public_suffix_list.dat',
() => fetchWithRetry('https://publicsuffix.org/list/public_suffix_list.dat', defaultRequestInit).then(r => r.text()),
{ ttl: 24 * 60 * 60 * 1000 }
)));

View File

@ -121,9 +121,16 @@ export class Cache {
} }
return value; 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); const separator = String.fromCharCode(0);

View File

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

View File

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