mirror of
https://github.com/SukkaW/Surge.git
synced 2026-01-29 01:51:52 +08:00
Chore: simplify build infra
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
import { readFileByLine } from './fetch-text-by-line';
|
||||
import { surgeDomainsetToClashDomainset, surgeRulesetToClashClassicalTextRuleset } from './clash';
|
||||
import { traceAsync } from './trace-runner';
|
||||
import picocolors from 'picocolors';
|
||||
|
||||
export async function compareAndWriteFile(linesA: string[], filePath: string) {
|
||||
let isEqual = true;
|
||||
@@ -45,11 +46,11 @@ export async function compareAndWriteFile(linesA: string[], filePath: string) {
|
||||
}
|
||||
|
||||
if (isEqual) {
|
||||
console.log(`Same Content, bail out writing: ${filePath}`);
|
||||
console.log(picocolors.gray(`Same Content, bail out writing: ${filePath}`));
|
||||
return;
|
||||
}
|
||||
|
||||
await traceAsync(`Writing ${filePath}`, async () => {
|
||||
await traceAsync(picocolors.gray(`Writing ${filePath}`), async () => {
|
||||
if (linesALen < 10000) {
|
||||
return Bun.write(file, `${linesA.join('\n')}\n`);
|
||||
}
|
||||
@@ -63,7 +64,7 @@ export async function compareAndWriteFile(linesA: string[], filePath: string) {
|
||||
|
||||
await writer.flush();
|
||||
return writer.end();
|
||||
});
|
||||
}, picocolors.gray);
|
||||
}
|
||||
|
||||
export const withBannerArray = (title: string, description: string[], date: Date, content: string[]) => {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import retry from 'async-retry';
|
||||
import picocolors from 'picocolors';
|
||||
|
||||
// retry settings
|
||||
const MIN_TIMEOUT = 10;
|
||||
@@ -86,7 +87,7 @@ function createFetchRetry($fetch: typeof fetch): typeof fetch {
|
||||
err.name === 'AbortError'
|
||||
|| ('digest' in err && err.digest === 'AbortError')
|
||||
) {
|
||||
console.log('[fetch abort]', url.toString());
|
||||
console.log(picocolors.gray('[fetch abort]'), picocolors.gray(url.toString()));
|
||||
return bail(err);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,3 +31,47 @@ export function isProbablyIpv4(hostname: string): boolean {
|
||||
&& /* '.' */ hostname.charCodeAt(hostname.length - 1) !== 46 /* '.' */
|
||||
);
|
||||
}
|
||||
|
||||
export function isProbablyIpv6(hostname: string): boolean {
|
||||
if (hostname.length < 3) {
|
||||
return false;
|
||||
}
|
||||
|
||||
let start = hostname[0] === '[' ? 1 : 0;
|
||||
let end = hostname.length;
|
||||
|
||||
if (hostname[end - 1] === ']') {
|
||||
end -= 1;
|
||||
}
|
||||
|
||||
// We only consider the maximum size of a normal IPV6. Note that this will
|
||||
// fail on so-called "IPv4 mapped IPv6 addresses" but this is a corner-case
|
||||
// and a proper validation library should be used for these.
|
||||
if (end - start > 39) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/* eslint-disable sukka/no-single-return -- here it goes */
|
||||
let hasColon = false;
|
||||
|
||||
for (; start < end; start += 1) {
|
||||
const code = hostname.charCodeAt(start);
|
||||
|
||||
if (code === 58 /* ':' */) {
|
||||
hasColon = true;
|
||||
} else if (
|
||||
!(
|
||||
(
|
||||
(code >= 48 && code <= 57) // 0-9
|
||||
|| (code >= 97 && code <= 102) // a-f
|
||||
|| (code >= 65 && code <= 90) // A-F
|
||||
)
|
||||
)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return hasColon;
|
||||
/* eslint-enable sukka/no-single-return -- here it goes */
|
||||
}
|
||||
|
||||
@@ -51,11 +51,7 @@ export async function processDomainLists(domainListsUrl: string, includeAllSubDo
|
||||
foundDebugDomain = true;
|
||||
}
|
||||
|
||||
if (includeAllSubDomain) {
|
||||
domainSets.add(`.${domainToAdd}`);
|
||||
} else {
|
||||
domainSets.add(domainToAdd);
|
||||
}
|
||||
domainSets.add(includeAllSubDomain ? `.${domainToAdd}` : domainToAdd);
|
||||
}
|
||||
|
||||
return domainSets;
|
||||
@@ -90,6 +86,8 @@ export async function processHosts(hostsUrl: string, includeAllSubDomain = false
|
||||
}
|
||||
}
|
||||
|
||||
console.log(picocolors.gray('[process hosts]'), picocolors.gray(hostsUrl), picocolors.gray(domainSets.size));
|
||||
|
||||
return domainSets;
|
||||
});
|
||||
}
|
||||
@@ -159,7 +157,7 @@ export async function processFilterRules(
|
||||
warningMessages.push(hostname);
|
||||
break;
|
||||
default:
|
||||
throw new Error(`Unknown flag: ${flag as any}`);
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -187,6 +185,13 @@ export async function processFilterRules(
|
||||
);
|
||||
});
|
||||
|
||||
console.log(
|
||||
picocolors.gray('[process filter]'),
|
||||
picocolors.gray(filterRulesUrl),
|
||||
picocolors.gray(`white: ${whitelistDomainSets.size}`),
|
||||
picocolors.gray(`black: ${blacklistDomainSets.size}`)
|
||||
);
|
||||
|
||||
return {
|
||||
white: whitelistDomainSets,
|
||||
black: blacklistDomainSets,
|
||||
@@ -569,25 +574,23 @@ class CustomAbortError extends Error {
|
||||
public readonly digest = 'AbortError';
|
||||
}
|
||||
|
||||
function sleepWithAbort(ms: number, signal: AbortSignal) {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
signal.throwIfAborted();
|
||||
signal.addEventListener('abort', stop);
|
||||
Bun.sleep(ms).then(done).catch(doReject);
|
||||
const sleepWithAbort = (ms: number, signal: AbortSignal) => new Promise<void>((resolve, reject) => {
|
||||
signal.throwIfAborted();
|
||||
signal.addEventListener('abort', stop);
|
||||
Bun.sleep(ms).then(done).catch(doReject);
|
||||
|
||||
function done() {
|
||||
signal.removeEventListener('abort', stop);
|
||||
resolve();
|
||||
}
|
||||
function stop(this: AbortSignal) {
|
||||
reject(this.reason);
|
||||
}
|
||||
function doReject(reason: unknown) {
|
||||
signal.removeEventListener('abort', stop);
|
||||
reject(reason);
|
||||
}
|
||||
});
|
||||
}
|
||||
function done() {
|
||||
signal.removeEventListener('abort', stop);
|
||||
resolve();
|
||||
}
|
||||
function stop(this: AbortSignal) {
|
||||
reject(this.reason);
|
||||
}
|
||||
function doReject(reason: unknown) {
|
||||
signal.removeEventListener('abort', stop);
|
||||
reject(reason);
|
||||
}
|
||||
});
|
||||
|
||||
async function fetchAssets(url: string, fallbackUrls: string[] | readonly string[]) {
|
||||
const controller = new AbortController();
|
||||
@@ -602,7 +605,7 @@ async function fetchAssets(url: string, fallbackUrls: string[] | readonly string
|
||||
const createFetchFallbackPromise = async (url: string, index: number) => {
|
||||
// Most assets can be downloaded within 250ms. To avoid wasting bandwidth, we will wait for 350ms before downloading from the fallback URL.
|
||||
try {
|
||||
await sleepWithAbort(200 + (index + 1) * 10, controller.signal);
|
||||
await sleepWithAbort(300 + (index + 1) * 20, controller.signal);
|
||||
} catch {
|
||||
console.log(picocolors.gray('[fetch cancelled early]'), picocolors.gray(url));
|
||||
throw new CustomAbortError();
|
||||
|
||||
Reference in New Issue
Block a user