mirror of
https://github.com/SukkaW/Surge.git
synced 2026-01-29 01:51:52 +08:00
Refactor/Perf: initial span tracer
This commit is contained in:
@@ -9,6 +9,7 @@ import { TTL } from './cache-filesystem';
|
||||
import { isCI } from 'ci-info';
|
||||
|
||||
import { add as SetAdd } from 'mnemonist/set';
|
||||
import type { Span } from '../trace';
|
||||
|
||||
const WHITELIST_DOMAIN = new Set([
|
||||
'w3s.link',
|
||||
@@ -86,11 +87,11 @@ const BLACK_TLD = new Set([
|
||||
'za.com'
|
||||
]);
|
||||
|
||||
export const getPhishingDomains = () => traceAsync('get phishing domains', async () => {
|
||||
export const getPhishingDomains = (parentSpan: Span) => parentSpan.traceChild('get phishing domains').traceAsyncFn(async (span) => {
|
||||
const [domainSet, domainSet2, gorhill] = await Promise.all([
|
||||
processDomainLists('https://curbengh.github.io/phishing-filter/phishing-filter-domains.txt', true, TTL.THREE_HOURS()),
|
||||
processDomainLists(span, 'https://curbengh.github.io/phishing-filter/phishing-filter-domains.txt', true, TTL.THREE_HOURS()),
|
||||
isCI
|
||||
? processDomainLists('https://phishing.army/download/phishing_army_blocklist.txt', true, TTL.THREE_HOURS())
|
||||
? processDomainLists(span, 'https://phishing.army/download/phishing_army_blocklist.txt', true, TTL.THREE_HOURS())
|
||||
: null,
|
||||
getGorhillPublicSuffixPromise()
|
||||
]);
|
||||
@@ -98,7 +99,7 @@ export const getPhishingDomains = () => traceAsync('get phishing domains', async
|
||||
SetAdd(domainSet, domainSet2);
|
||||
}
|
||||
|
||||
traceSync.skip('* whitelisting phishing domains', () => {
|
||||
span.traceChild('whitelisting phishing domains').traceSyncFn(() => {
|
||||
const trieForRemovingWhiteListed = createTrie(domainSet);
|
||||
for (const white of WHITELIST_DOMAIN) {
|
||||
const found = trieForRemovingWhiteListed.find(`.${white}`, false);
|
||||
@@ -112,7 +113,7 @@ export const getPhishingDomains = () => traceAsync('get phishing domains', async
|
||||
const domainCountMap: Record<string, number> = {};
|
||||
const getDomain = createCachedGorhillGetDomain(gorhill);
|
||||
|
||||
traceSync.skip('* process phishing domain set', () => {
|
||||
span.traceChild('process phishing domain set').traceSyncFn(() => {
|
||||
const domainArr = Array.from(domainSet);
|
||||
|
||||
for (let i = 0, len = domainArr.length; i < len; i++) {
|
||||
@@ -173,7 +174,7 @@ export const getPhishingDomains = () => traceAsync('get phishing domains', async
|
||||
}
|
||||
});
|
||||
|
||||
const results = traceSync.skip('* get final phishing results', () => Object.entries(domainCountMap)
|
||||
const results = span.traceChild('get final phishing results').traceSyncFn(() => Object.entries(domainCountMap)
|
||||
.filter(([, count]) => count >= 5)
|
||||
.map(([apexDomain]) => apexDomain));
|
||||
|
||||
|
||||
@@ -10,12 +10,13 @@ import picocolors from 'picocolors';
|
||||
import { normalizeDomain } from './normalize-domain';
|
||||
import { fetchAssets } from './fetch-assets';
|
||||
import { deserializeSet, fsCache, serializeSet } from './cache-filesystem';
|
||||
import type { Span } from '../trace';
|
||||
|
||||
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(span: Span, domainListsUrl: string, includeAllSubDomain = false, ttl: number | null = null) {
|
||||
return span.traceChild(`process domainlist: ${domainListsUrl}`).traceAsyncFn(() => fsCache.apply(
|
||||
domainListsUrl,
|
||||
async () => {
|
||||
const domainSets = new Set<string>();
|
||||
@@ -44,8 +45,8 @@ export function processDomainLists(domainListsUrl: string, includeAllSubDomain =
|
||||
}
|
||||
));
|
||||
}
|
||||
export function processHosts(hostsUrl: string, includeAllSubDomain = false, ttl: number | null = null) {
|
||||
return traceAsync(`- processHosts: ${hostsUrl}`, () => fsCache.apply(
|
||||
export function processHosts(span: Span, hostsUrl: string, includeAllSubDomain = false, ttl: number | null = null) {
|
||||
return span.traceChild(`processhosts: ${hostsUrl}`).traceAsyncFn(() => fsCache.apply(
|
||||
hostsUrl,
|
||||
async () => {
|
||||
const domainSets = new Set<string>();
|
||||
@@ -95,11 +96,12 @@ const enum ParseType {
|
||||
}
|
||||
|
||||
export async function processFilterRules(
|
||||
span: Span,
|
||||
filterRulesUrl: string,
|
||||
fallbackUrls?: readonly string[] | undefined | null,
|
||||
ttl: number | null = null
|
||||
): Promise<{ white: string[], black: string[], foundDebugDomain: boolean }> {
|
||||
const [white, black, warningMessages] = await traceAsync(`- processFilterRules: ${filterRulesUrl}`, () => fsCache.apply<Readonly<[
|
||||
const [white, black, warningMessages] = await span.traceChild('process filter rules: domainListsUrl').traceAsyncFn(() => fsCache.apply<Readonly<[
|
||||
white: string[],
|
||||
black: string[],
|
||||
warningMessages: string[]
|
||||
|
||||
@@ -25,16 +25,3 @@ export interface TaskResult {
|
||||
readonly end: number,
|
||||
readonly taskName: string
|
||||
}
|
||||
|
||||
export const task = <T>(importMetaPath: string, fn: () => Promise<T>, customname: string | null = null) => {
|
||||
const taskName = customname ?? path.basename(importMetaPath, path.extname(importMetaPath));
|
||||
return async () => {
|
||||
console.log(`🏃 [${taskName}] Start executing`);
|
||||
const start = Bun.nanoseconds();
|
||||
await fn();
|
||||
const end = Bun.nanoseconds();
|
||||
console.log(`✅ [${taskName}] ${picocolors.blue(`[${((end - start) / 1e6).toFixed(3)}ms]`)} Executed successfully`);
|
||||
|
||||
return { start, end, taskName } as TaskResult;
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user