Chore: Housekeeping & Make ESLint Happy

This commit is contained in:
SukkaW
2024-09-29 11:01:16 +08:00
parent 8c655582ee
commit 14bcc10ce7
14 changed files with 294 additions and 333 deletions

View File

@@ -6,17 +6,15 @@ import { RulesetOutput } from './lib/create-file';
export const buildCloudMounterRules = task(require.main === module, __filename)(async (span) => {
// AND,((SRC-IP,192.168.1.110), (DOMAIN, example.com))
const results = DOMAINS.flatMap(domain => {
return PROCESS_NAMES.flatMap(process => [
`AND,((${domain}),(PROCESS-NAME,${process}))`,
...[
'10.0.0.0/8',
// '127.0.0.0/8',
'172.16.0.0/12',
'192.168.0.0/16'
].map(cidr => `AND,((${domain}),(SRC-IP,${cidr}))`)
]);
});
const results = DOMAINS.flatMap(domain => PROCESS_NAMES.flatMap(process => [
`AND,((${domain}),(PROCESS-NAME,${process}))`,
...[
'10.0.0.0/8',
// '127.0.0.0/8',
'172.16.0.0/12',
'192.168.0.0/16'
].map(cidr => `AND,((${domain}),(SRC-IP,${cidr}))`)
]));
const description = SHARED_DESCRIPTION;

View File

@@ -34,11 +34,8 @@ export const buildCommon = task(require.main === module, __filename)(async (span
if (isDirectory) return true;
const extname = path.extname(filepath);
if (extname === '.js' || extname === '.ts') {
return false;
}
return true;
return !(extname === '.js' || extname === '.ts');
})
.crawl(SOURCE_DIR)
.withPromise();
@@ -67,49 +64,46 @@ export const buildCommon = task(require.main === module, __filename)(async (span
const $skip = Symbol('skip');
const processFile = (span: Span, sourcePath: string) => {
// console.log('Processing', sourcePath);
return span.traceChildAsync(`process file: ${sourcePath}`, async () => {
const lines: string[] = [];
const processFile = (span: Span, sourcePath: string) => span.traceChildAsync(`process file: ${sourcePath}`, async () => {
const lines: string[] = [];
let title = '';
const descriptions: string[] = [];
let sgmodulePathname: string | null = null;
let title = '';
const descriptions: string[] = [];
let sgmodulePathname: string | null = null;
try {
for await (const line of readFileByLine(sourcePath)) {
if (line.startsWith(MAGIC_COMMAND_SKIP)) {
return $skip;
}
if (line.startsWith(MAGIC_COMMAND_TITLE)) {
title = line.slice(MAGIC_COMMAND_TITLE.length).trim();
continue;
}
if (line.startsWith(MAGIC_COMMAND_DESCRIPTION)) {
descriptions.push(line.slice(MAGIC_COMMAND_DESCRIPTION.length).trim());
continue;
}
if (line.startsWith(MAGIC_COMMAND_SGMODULE_MITM_HOSTNAMES)) {
sgmodulePathname = line.slice(MAGIC_COMMAND_SGMODULE_MITM_HOSTNAMES.length).trim();
continue;
}
const l = processLine(line);
if (l) {
lines.push(l);
}
try {
for await (const line of readFileByLine(sourcePath)) {
if (line.startsWith(MAGIC_COMMAND_SKIP)) {
return $skip;
}
} catch (e) {
console.error('Error processing', sourcePath);
console.trace(e);
}
return [title, descriptions, lines, sgmodulePathname] as const;
});
};
if (line.startsWith(MAGIC_COMMAND_TITLE)) {
title = line.slice(MAGIC_COMMAND_TITLE.length).trim();
continue;
}
if (line.startsWith(MAGIC_COMMAND_DESCRIPTION)) {
descriptions.push(line.slice(MAGIC_COMMAND_DESCRIPTION.length).trim());
continue;
}
if (line.startsWith(MAGIC_COMMAND_SGMODULE_MITM_HOSTNAMES)) {
sgmodulePathname = line.slice(MAGIC_COMMAND_SGMODULE_MITM_HOSTNAMES.length).trim();
continue;
}
const l = processLine(line);
if (l) {
lines.push(l);
}
}
} catch (e) {
console.error('Error processing', sourcePath);
console.trace(e);
}
return [title, descriptions, lines, sgmodulePathname] as const;
});
function transformDomainset(parentSpan: Span, sourcePath: string, relativePath: string) {
return parentSpan

View File

@@ -79,9 +79,7 @@ const priorityOrder: Record<'default' | string & {}, number> = {
LICENSE: 70,
default: Number.MAX_VALUE
};
const prioritySorter = (a: TreeType, b: TreeType) => {
return ((priorityOrder[a.name] || priorityOrder.default) - (priorityOrder[b.name] || priorityOrder.default)) || a.name.localeCompare(b.name);
};
const prioritySorter = (a: TreeType, b: TreeType) => ((priorityOrder[a.name] || priorityOrder.default) - (priorityOrder[b.name] || priorityOrder.default)) || a.name.localeCompare(b.name);
const html = (string: TemplateStringsArray, ...values: any[]) => string.reduce((acc, str, i) => acc + str + (values[i] ?? ''), '');

View File

@@ -205,18 +205,16 @@ function generateAppProfile(
appendArrayInPlace(
redults,
POLICY_GROUPS.flatMap(([name, insertProxy, insertDirect]) => {
return [
' [',
` 'name' => '${name}',`,
' \'type\' => \'select\',',
' \'proxies\' => [',
insertProxy && name !== 'Default Proxy' && ' \'Default Proxy\',',
insertDirect && ' \'DIRECT\',',
' ],',
' ],'
].filter(isTruthy);
})
POLICY_GROUPS.flatMap(([name, insertProxy, insertDirect]) => [
' [',
` 'name' => '${name}',`,
' \'type\' => \'select\',',
' \'proxies\' => [',
insertProxy && name !== 'Default Proxy' && ' \'Default Proxy\',',
insertDirect && ' \'DIRECT\',',
' ],',
' ],'
].filter(isTruthy))
);
appendArrayInPlace(

View File

@@ -6,42 +6,42 @@ import { ALL, NORTH_AMERICA, EU, HK, TW, JP, KR } from '../Source/stream';
import { SHARED_DESCRIPTION } from './lib/constants';
import { RulesetOutput } from './lib/create-file';
export const createRulesetForStreamService = (span: Span, fileId: string, title: string, streamServices: Array<import('../Source/stream').StreamService>) => {
return span.traceChildAsync(fileId, async (childSpan) => Promise.all([
// Domains
new RulesetOutput(childSpan, fileId, 'non_ip')
.withTitle(`Sukka's Ruleset - Stream Services: ${title}`)
.withDescription([
...SHARED_DESCRIPTION,
'',
...streamServices.map((i) => `- ${i.name}`)
])
.addFromRuleset(streamServices.flatMap((i) => i.rules))
.write(),
// IP
new RulesetOutput(childSpan, fileId, 'ip')
.withTitle(`Sukka's Ruleset - Stream Services IPs: ${title}`)
.withDescription([
...SHARED_DESCRIPTION,
'',
...streamServices.map((i) => `- ${i.name}`)
])
.bulkAddCIDR4NoResolve(streamServices.flatMap(i => i.ip?.v4 ?? []))
.bulkAddCIDR6NoResolve(streamServices.flatMap(i => i.ip?.v6 ?? []))
.write()
]));
};
export const createRulesetForStreamService = (
span: Span,
fileId: string, title: string,
streamServices: Array<import('../Source/stream').StreamService>
) => span.traceChildAsync(fileId, async (childSpan) => Promise.all([
// Domains
new RulesetOutput(childSpan, fileId, 'non_ip')
.withTitle(`Sukka's Ruleset - Stream Services: ${title}`)
.withDescription([
...SHARED_DESCRIPTION,
'',
...streamServices.map((i) => `- ${i.name}`)
])
.addFromRuleset(streamServices.flatMap((i) => i.rules))
.write(),
// IP
new RulesetOutput(childSpan, fileId, 'ip')
.withTitle(`Sukka's Ruleset - Stream Services IPs: ${title}`)
.withDescription([
...SHARED_DESCRIPTION,
'',
...streamServices.map((i) => `- ${i.name}`)
])
.bulkAddCIDR4NoResolve(streamServices.flatMap(i => i.ip?.v4 ?? []))
.bulkAddCIDR6NoResolve(streamServices.flatMap(i => i.ip?.v6 ?? []))
.write()
]));
export const buildStreamService = task(require.main === module, __filename)(async (span) => {
return Promise.all([
createRulesetForStreamService(span, 'stream', 'All', ALL),
createRulesetForStreamService(span, 'stream_us', 'North America', NORTH_AMERICA),
createRulesetForStreamService(span, 'stream_eu', 'Europe', EU),
createRulesetForStreamService(span, 'stream_hk', 'Hong Kong', HK),
createRulesetForStreamService(span, 'stream_tw', 'Taiwan', TW),
createRulesetForStreamService(span, 'stream_jp', 'Japan', JP),
// createRulesetForStreamService('stream_au', 'Oceania', AU),
createRulesetForStreamService(span, 'stream_kr', 'Korean', KR)
// createRulesetForStreamService('stream_south_east_asia', 'South East Asia', SOUTH_EAST_ASIA)
]);
});
export const buildStreamService = task(require.main === module, __filename)(async (span) => Promise.all([
createRulesetForStreamService(span, 'stream', 'All', ALL),
createRulesetForStreamService(span, 'stream_us', 'North America', NORTH_AMERICA),
createRulesetForStreamService(span, 'stream_eu', 'Europe', EU),
createRulesetForStreamService(span, 'stream_hk', 'Hong Kong', HK),
createRulesetForStreamService(span, 'stream_tw', 'Taiwan', TW),
createRulesetForStreamService(span, 'stream_jp', 'Japan', JP),
// createRulesetForStreamService('stream_au', 'Oceania', AU),
createRulesetForStreamService(span, 'stream_kr', 'Korean', KR)
// createRulesetForStreamService('stream_south_east_asia', 'South East Asia', SOUTH_EAST_ASIA)
]));

View File

@@ -1,7 +1,5 @@
/** Packs two 16-bit integers into one 32-bit integer */
export const pack = (a: number, b: number): number => {
return (a << 16) | b;
};
export const pack = (a: number, b: number): number => (a << 16) | b;
/** Unpacks two 16-bit integers from one 32-bit integer */
export const unpack = (value: number, arr: [a: number, b: number] = Array.from(new Array(2).keys()) as any): [a: number, b: number] => {

View File

@@ -77,13 +77,11 @@ export const appendArrayFromSet = <T>(dest: T[], source: Set<T> | Array<Set<T>>,
return dest;
};
export const output = (id: string, type: 'non_ip' | 'ip' | 'domainset') => {
return [
path.join(OUTPUT_SURGE_DIR, type, id + '.conf'),
path.join(OUTPUT_CLASH_DIR, type, id + '.txt'),
path.join(OUTPUT_SINGBOX_DIR, type, id + '.json')
] as const;
};
export const output = (id: string, type: 'non_ip' | 'ip' | 'domainset') => [
path.join(OUTPUT_SURGE_DIR, type, id + '.conf'),
path.join(OUTPUT_CLASH_DIR, type, id + '.txt'),
path.join(OUTPUT_SINGBOX_DIR, type, id + '.json')
] as const;
export function withBannerArray(title: string, description: string[] | readonly string[], date: Date, content: string[]) {
return [

View File

@@ -223,9 +223,7 @@ export async function processFilterRules(
lineCb(line);
}
} else {
const filterRules = await span.traceChild('download adguard filter').traceAsyncFn(() => {
return fetchAssets(filterRulesUrl, fallbackUrls).then(text => text.split('\n'));
});
const filterRules = await span.traceChild('download adguard filter').traceAsyncFn(() => fetchAssets(filterRulesUrl, fallbackUrls).then(text => text.split('\n')));
span.traceChild('parse adguard filter').traceSyncFn(() => {
for (let i = 0, len = filterRules.length; i < len; i++) {

View File

@@ -36,14 +36,14 @@ export abstract class RuleOutput<TPreprocessed = unknown> {
protected pendingPromise = Promise.resolve();
static jsonToLines = (json: unknown): string[] => stringify(json).split('\n');
static readonly jsonToLines = (json: unknown): string[] => stringify(json).split('\n');
whitelistDomain = (domain: string) => {
this.domainTrie.whitelist(domain);
return this;
};
static domainWildCardToRegex = (domain: string) => {
static readonly domainWildCardToRegex = (domain: string) => {
let result = '^';
for (let i = 0, len = domain.length; i < len; i++) {
switch (domain[i]) {
@@ -67,8 +67,7 @@ export abstract class RuleOutput<TPreprocessed = unknown> {
constructor(
protected readonly span: Span,
protected readonly id: string
) {
}
) { }
protected title: string | null = null;
withTitle(title: string) {
@@ -199,7 +198,7 @@ export abstract class RuleOutput<TPreprocessed = unknown> {
return this;
}
static ipToCidr = (ip: string, version: 4 | 6 = 4) => {
static readonly ipToCidr = (ip: string, version: 4 | 6 = 4) => {
if (ip.includes('/')) return ip;
if (version === 4) {
return ip + '/32';
@@ -325,11 +324,7 @@ export const fileEqual = async (linesA: string[], source: AsyncIterable<string>)
index++;
if (index > linesA.length - 1) {
if (index === linesA.length && lineB === '') {
return true;
}
// The file becomes smaller
return false;
return (index === linesA.length && lineB === '');
}
const lineA = linesA[index];
@@ -353,12 +348,8 @@ export const fileEqual = async (linesA: string[], source: AsyncIterable<string>)
}
}
if (index < linesA.length - 1) {
// The file becomes larger
return false;
}
return true;
// The file becomes larger
return !(index < linesA.length - 1);
};
export async function compareAndWriteFile(span: Span, linesA: string[], filePath: string) {

View File

@@ -43,6 +43,4 @@ export const fnv1a52 = (str: string) => {
);
};
export const stringHash = (payload: string) => {
return fnv1a52(payload).toString(36) + payload.length.toString(36);
};
export const stringHash = (payload: string) => fnv1a52(payload).toString(36) + payload.length.toString(36);

View File

@@ -34,9 +34,7 @@ const deepTrieNodeToJSON = (
return obj;
};
const createNode = <Meta = any>(parent: TrieNode | null = null): TrieNode => {
return [false, parent, new Map<string, TrieNode>(), null] as TrieNode<Meta>;
};
const createNode = <Meta = any>(parent: TrieNode | null = null): TrieNode => [false, parent, new Map<string, TrieNode>(), null] as TrieNode<Meta>;
export const hostnameToTokens = (hostname: string): string[] => {
const tokens = hostname.split('.');
@@ -227,9 +225,7 @@ export const createTrie = <Meta = any>(from?: string[] | Set<string> | null, smo
return { node, parent };
};
const contains = (suffix: string): boolean => {
return walkIntoLeafWithSuffix(suffix) !== null;
};
const contains = (suffix: string): boolean => walkIntoLeafWithSuffix(suffix) !== null;
const walk = (
onMatches: (suffix: string[], meta: Meta) => void,

View File

@@ -20,11 +20,8 @@ import { SOURCE_DIR } from './constants/dir';
if (isDirectory) return true;
const extname = path.extname(filepath);
if (extname === '.js' || extname === '.ts') {
return false;
}
return true;
return !(extname === '.js' || extname === '.ts');
})
.crawl(SOURCE_DIR)
.withPromise();