From d7c8bf8d11e44dab15b9495d5437ea5047fc5652 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Sun, 24 Dec 2023 00:58:04 +0800 Subject: [PATCH] Chore: fix leak thread --- Build/download-publicsuffixlist.ts | 7 +----- Build/lib/cache-filesystem.ts | 9 +++++++- Build/lib/fetch-text-by-line.ts | 2 -- Build/lib/parse-filter.ts | 35 +++++++++++++++--------------- 4 files changed, 26 insertions(+), 27 deletions(-) diff --git a/Build/download-publicsuffixlist.ts b/Build/download-publicsuffixlist.ts index 6258a560..8ce61fd4 100644 --- a/Build/download-publicsuffixlist.ts +++ b/Build/download-publicsuffixlist.ts @@ -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()))); diff --git a/Build/lib/cache-filesystem.ts b/Build/lib/cache-filesystem.ts index e104cb5d..6a1fecfc 100644 --- a/Build/lib/cache-filesystem.ts +++ b/Build/lib/cache-filesystem.ts @@ -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); diff --git a/Build/lib/fetch-text-by-line.ts b/Build/lib/fetch-text-by-line.ts index 352e1f08..4f372481 100644 --- a/Build/lib/fetch-text-by-line.ts +++ b/Build/lib/fetch-text-by-line.ts @@ -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'; diff --git a/Build/lib/parse-filter.ts b/Build/lib/parse-filter.ts index fe1ae99b..fba447ab 100644 --- a/Build/lib/parse-filter.ts +++ b/Build/lib/parse-filter.ts @@ -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(); 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(); 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(); const blacklistDomainSets = new Set(); @@ -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(