mirror of
https://github.com/SukkaW/Surge.git
synced 2025-12-12 17:20:35 +08:00
Perf: make microsoft cdn in the dedicated thread
This commit is contained in:
parent
c5b88362ef
commit
7d0e22583d
@ -1,44 +1,57 @@
|
|||||||
import { task } from './trace';
|
import { task } from './trace';
|
||||||
import { fetchRemoteTextByLine } from './lib/fetch-text-by-line';
|
|
||||||
import { HostnameSmolTrie } from './lib/trie';
|
|
||||||
import { SHARED_DESCRIPTION } from './constants/description';
|
import { SHARED_DESCRIPTION } from './constants/description';
|
||||||
import { createMemoizedPromise } from './lib/memo-promise';
|
import { createMemoizedPromise } from './lib/memo-promise';
|
||||||
import { extractDomainsFromFelixDnsmasq } from './lib/parse-dnsmasq';
|
|
||||||
import { RulesetOutput } from './lib/rules/ruleset';
|
import { RulesetOutput } from './lib/rules/ruleset';
|
||||||
import { appendArrayInPlace } from './lib/append-array-in-place';
|
import Worktank from 'worktank';
|
||||||
|
|
||||||
const PROBE_DOMAINS = ['.microsoft.com', '.windows.net', '.windows.com', '.windowsupdate.com', '.windowssearch.com', '.office.net'];
|
const pool = new Worktank({
|
||||||
|
name: 'build-internal-reverse-chn-cidr',
|
||||||
|
size: 1,
|
||||||
|
timeout: 10000, // The maximum number of milliseconds to wait for the result from the worker, if exceeded the worker is terminated and the execution promise rejects
|
||||||
|
warmup: true,
|
||||||
|
autoterminate: 30000, // The interval of milliseconds at which to check if the pool can be automatically terminated, to free up resources, workers will be spawned up again if needed
|
||||||
|
env: {},
|
||||||
|
methods: {
|
||||||
|
// eslint-disable-next-line object-shorthand -- workertank
|
||||||
|
getMicrosoftCdnRuleset: async function (importMetaUrl: string): Promise<[domains: string[], domainSuffixes: string[]]> {
|
||||||
|
// TODO: createRequire is a temporary workaround for https://github.com/nodejs/node/issues/51956
|
||||||
|
const { default: module } = await import('node:module');
|
||||||
|
const __require = module.createRequire(importMetaUrl);
|
||||||
|
|
||||||
const DOMAINS = [
|
const { HostnameSmolTrie } = __require('./lib/trie');
|
||||||
'res.cdn.office.net',
|
const { PROBE_DOMAINS, DOMAINS, DOMAIN_SUFFIXES, BLACKLIST } = __require('./constants/microsoft-cdn') as typeof import('./constants/microsoft-cdn');
|
||||||
'res-1.cdn.office.net',
|
const { fetchRemoteTextByLine } = __require('./lib/fetch-text-by-line') as typeof import('./lib/fetch-text-by-line');
|
||||||
'statics.teams.cdn.office.net'
|
const { appendArrayInPlace } = __require('./lib/append-array-in-place') as typeof import('./lib/append-array-in-place');
|
||||||
];
|
const { extractDomainsFromFelixDnsmasq } = __require('./lib/parse-dnsmasq') as typeof import('./lib/parse-dnsmasq');
|
||||||
const DOMAIN_SUFFIXES = ['download.prss.microsoft.com'];
|
|
||||||
|
|
||||||
const BLACKLIST = [
|
const trie = new HostnameSmolTrie();
|
||||||
'www.microsoft.com',
|
|
||||||
'windowsupdate.com'
|
|
||||||
];
|
|
||||||
|
|
||||||
export const getMicrosoftCdnRulesetPromise = createMemoizedPromise<[domains: string[], domainSuffixes: string[]]>(async () => {
|
for await (const line of await fetchRemoteTextByLine('https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/accelerated-domains.china.conf')) {
|
||||||
// First trie is to find the microsoft domains that matches probe domains
|
const domain = extractDomainsFromFelixDnsmasq(line);
|
||||||
const trie = new HostnameSmolTrie();
|
if (domain) {
|
||||||
|
trie.add(domain);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for await (const line of await fetchRemoteTextByLine('https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/accelerated-domains.china.conf')) {
|
// remove blacklist domain from trie, to prevent them from being included in the later dump
|
||||||
const domain = extractDomainsFromFelixDnsmasq(line);
|
BLACKLIST.forEach(black => trie.whitelist(black));
|
||||||
if (domain) {
|
|
||||||
trie.add(domain);
|
const domains: string[] = DOMAINS;
|
||||||
|
const domainSuffixes = appendArrayInPlace(PROBE_DOMAINS.flatMap(domain => trie.find(domain)), DOMAIN_SUFFIXES);
|
||||||
|
|
||||||
|
return [domains, domainSuffixes] as const;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// remove blacklist domain from trie, to prevent them from being included in the later dump
|
export const getMicrosoftCdnRulesetPromise = createMemoizedPromise<[domains: string[], domainSuffixes: string[]]>(async () => {
|
||||||
BLACKLIST.forEach(black => trie.whitelist(black));
|
const res = await pool.exec(
|
||||||
|
'getMicrosoftCdnRuleset',
|
||||||
|
[import.meta.url]
|
||||||
|
);
|
||||||
|
pool.terminate();
|
||||||
|
|
||||||
const domains: string[] = DOMAINS;
|
return res;
|
||||||
const domainSuffixes = appendArrayInPlace(PROBE_DOMAINS.flatMap(domain => trie.find(domain)), DOMAIN_SUFFIXES);
|
|
||||||
|
|
||||||
return [domains, domainSuffixes] as const;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
export const buildMicrosoftCdn = task(require.main === module, __filename)(async (span) => {
|
export const buildMicrosoftCdn = task(require.main === module, __filename)(async (span) => {
|
||||||
|
|||||||
14
Build/constants/microsoft-cdn.ts
Normal file
14
Build/constants/microsoft-cdn.ts
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
export const PROBE_DOMAINS = ['.microsoft.com', '.windows.net', '.windows.com', '.windowsupdate.com', '.windowssearch.com', '.office.net'];
|
||||||
|
|
||||||
|
export const DOMAINS = [
|
||||||
|
'res.cdn.office.net',
|
||||||
|
'res-1.cdn.office.net',
|
||||||
|
'statics.teams.cdn.office.net'
|
||||||
|
];
|
||||||
|
|
||||||
|
export const DOMAIN_SUFFIXES = ['download.prss.microsoft.com'];
|
||||||
|
|
||||||
|
export const BLACKLIST = [
|
||||||
|
'www.microsoft.com',
|
||||||
|
'windowsupdate.com'
|
||||||
|
];
|
||||||
Loading…
x
Reference in New Issue
Block a user