Remove unused codes
Some checks are pending
Build / Build (push) Waiting to run
Build / Diff output (push) Blocked by required conditions
Build / Deploy to Cloudflare Pages (push) Blocked by required conditions
Build / Deploy to GitHub and GitLab (push) Blocked by required conditions

This commit is contained in:
SukkaW 2025-03-09 23:24:26 +08:00
parent 1c60b785c9
commit d40e191934
2 changed files with 18 additions and 93 deletions

View File

@ -6,7 +6,6 @@ import { createMemoizedPromise } from './lib/memo-promise';
import { CN_CIDR_MISSING_IN_CHNROUTE, NON_CN_CIDR_INCLUDED_IN_CHNROUTE } from './constants/cidr'; import { CN_CIDR_MISSING_IN_CHNROUTE, NON_CN_CIDR_INCLUDED_IN_CHNROUTE } from './constants/cidr';
import { appendArrayInPlace } from './lib/append-array-in-place'; import { appendArrayInPlace } from './lib/append-array-in-place';
import { IPListOutput } from './lib/rules/ip'; import { IPListOutput } from './lib/rules/ip';
import { cachedOnlyFail } from './lib/fs-memo';
import { createFileDescription } from './constants/description'; import { createFileDescription } from './constants/description';
const PROBE_CHN_CIDR_V4 = [ const PROBE_CHN_CIDR_V4 = [
@ -16,34 +15,28 @@ const PROBE_CHN_CIDR_V4 = [
'120.78.92.171' '120.78.92.171'
]; ];
export const getChnCidrPromise = createMemoizedPromise(cachedOnlyFail<[], [string[], string[]]>( export const getChnCidrPromise = createMemoizedPromise(async function getChnCidr() {
async function getChnCidr() { const [_cidr4, cidr6] = await Promise.all([
const [_cidr4, cidr6] = await Promise.all([ fetchRemoteTextByLine('https://raw.githubusercontent.com/misakaio/chnroutes2/master/chnroutes.txt', true).then(Array.fromAsync<string>),
fetchRemoteTextByLine('https://raw.githubusercontent.com/misakaio/chnroutes2/master/chnroutes.txt', true).then(Array.fromAsync<string>), fetchRemoteTextByLine('https://gaoyifan.github.io/china-operator-ip/china6.txt', true).then(Array.fromAsync<string>)
fetchRemoteTextByLine('https://gaoyifan.github.io/china-operator-ip/china6.txt', true).then(Array.fromAsync<string>) ]);
]);
const cidr4 = excludeCidr( const cidr4 = excludeCidr(
appendArrayInPlace(_cidr4, CN_CIDR_MISSING_IN_CHNROUTE), appendArrayInPlace(_cidr4, CN_CIDR_MISSING_IN_CHNROUTE),
NON_CN_CIDR_INCLUDED_IN_CHNROUTE, NON_CN_CIDR_INCLUDED_IN_CHNROUTE,
true true
); );
for (const probeIp of PROBE_CHN_CIDR_V4) { for (const probeIp of PROBE_CHN_CIDR_V4) {
if (!containsCidr(cidr4, PROBE_CHN_CIDR_V4)) { if (!containsCidr(cidr4, PROBE_CHN_CIDR_V4)) {
const err = new TypeError('chnroutes missing probe IP'); const err = new TypeError('chnroutes missing probe IP');
err.cause = probeIp; err.cause = probeIp;
throw err; throw err;
}
} }
return [cidr4, cidr6] as const;
},
{
serializer: JSON.stringify,
deserializer: JSON.parse
} }
));
return [cidr4, cidr6] as const;
});
export const buildChnCidr = task(require.main === module, __filename)(async (span) => { export const buildChnCidr = task(require.main === module, __filename)(async (span) => {
const [filteredCidr4, cidr6] = await span.traceChildAsync('download chnroutes2', getChnCidrPromise); const [filteredCidr4, cidr6] = await span.traceChildAsync('download chnroutes2', getChnCidrPromise);

View File

@ -1,68 +0,0 @@
import path from 'node:path';
import { isCI } from 'ci-info';
import picocolors from 'picocolors';
import { Cache } from './cache-filesystem';
import { createMemoize } from 'foxts/serialized-memo';
import type { MemoizeStorageProvider } from 'foxts/serialized-memo';
import { ROOT_DIR } from '../constants/dir';
const fsMemoCache = new Cache({ cachePath: path.join(ROOT_DIR, '.cache'), tableName: 'fs_memo_cache' });
const fsMemoCacheProvider: MemoizeStorageProvider = {
has(key) {
return fsMemoCache.get(key) !== null;
},
delete() {
// noop
},
get(key) {
return fsMemoCache.get(key) ?? undefined;
},
set(key, value, ttl) {
fsMemoCache.set(key, value, ttl);
},
updateTtl(key, ttl) {
fsMemoCache.updateTtl(key, ttl);
}
};
const TTL = isCI
// We run CI daily, so 1.5 days TTL is enough to persist the cache across runs
? 1.5 * 86400 * 1000
// We run locally less frequently, so we need to persist the cache for longer, 7 days
: 7 * 86400 * 1000;
export const cache = createMemoize(fsMemoCacheProvider, {
defaultTtl: TTL,
onCacheMiss(key, { humanReadableName, isUseCachedIfFail }) {
const cacheName = picocolors.gray(humanReadableName);
if (isUseCachedIfFail) {
console.log(picocolors.red('[fail] and no cache, throwing'), cacheName);
} else {
console.log(picocolors.yellow('[cache] miss'), cacheName);
}
},
onCacheUpdate(key, { humanReadableName, isUseCachedIfFail }) {
const cacheName = picocolors.gray(humanReadableName);
if (isUseCachedIfFail) {
console.log(picocolors.gray('[cache] update'), cacheName);
}
},
onCacheHit(key, { humanReadableName, isUseCachedIfFail }) {
const cacheName = picocolors.gray(humanReadableName);
if (isUseCachedIfFail) {
console.log(picocolors.yellow('[fail] try cache'), cacheName);
} else {
console.log(picocolors.green('[cache] hit'), cacheName);
}
}
});
export const cachedOnlyFail = createMemoize(fsMemoCacheProvider, {
defaultTtl: TTL,
onlyUseCachedIfFail: true
});
// export const cache = createCache(false);
// export const cachedOnlyFail = createCache(true);