mirror of
https://github.com/SukkaW/Surge.git
synced 2025-12-12 09:10: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 { fetchRemoteTextByLine } from './lib/fetch-text-by-line';
|
||||
import { HostnameSmolTrie } from './lib/trie';
|
||||
import { SHARED_DESCRIPTION } from './constants/description';
|
||||
import { createMemoizedPromise } from './lib/memo-promise';
|
||||
import { extractDomainsFromFelixDnsmasq } from './lib/parse-dnsmasq';
|
||||
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 = [
|
||||
'res.cdn.office.net',
|
||||
'res-1.cdn.office.net',
|
||||
'statics.teams.cdn.office.net'
|
||||
];
|
||||
const DOMAIN_SUFFIXES = ['download.prss.microsoft.com'];
|
||||
const { HostnameSmolTrie } = __require('./lib/trie');
|
||||
const { PROBE_DOMAINS, DOMAINS, DOMAIN_SUFFIXES, BLACKLIST } = __require('./constants/microsoft-cdn') as typeof import('./constants/microsoft-cdn');
|
||||
const { fetchRemoteTextByLine } = __require('./lib/fetch-text-by-line') as typeof import('./lib/fetch-text-by-line');
|
||||
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 BLACKLIST = [
|
||||
'www.microsoft.com',
|
||||
'windowsupdate.com'
|
||||
];
|
||||
const trie = new HostnameSmolTrie();
|
||||
|
||||
export const getMicrosoftCdnRulesetPromise = createMemoizedPromise<[domains: string[], domainSuffixes: string[]]>(async () => {
|
||||
// First trie is to find the microsoft domains that matches probe domains
|
||||
const trie = new HostnameSmolTrie();
|
||||
for await (const line of await fetchRemoteTextByLine('https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/accelerated-domains.china.conf')) {
|
||||
const domain = extractDomainsFromFelixDnsmasq(line);
|
||||
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')) {
|
||||
const domain = extractDomainsFromFelixDnsmasq(line);
|
||||
if (domain) {
|
||||
trie.add(domain);
|
||||
// remove blacklist domain from trie, to prevent them from being included in the later dump
|
||||
BLACKLIST.forEach(black => trie.whitelist(black));
|
||||
|
||||
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
|
||||
BLACKLIST.forEach(black => trie.whitelist(black));
|
||||
export const getMicrosoftCdnRulesetPromise = createMemoizedPromise<[domains: string[], domainSuffixes: string[]]>(async () => {
|
||||
const res = await pool.exec(
|
||||
'getMicrosoftCdnRuleset',
|
||||
[import.meta.url]
|
||||
);
|
||||
pool.terminate();
|
||||
|
||||
const domains: string[] = DOMAINS;
|
||||
const domainSuffixes = appendArrayInPlace(PROBE_DOMAINS.flatMap(domain => trie.find(domain)), DOMAIN_SUFFIXES);
|
||||
|
||||
return [domains, domainSuffixes] as const;
|
||||
return res;
|
||||
});
|
||||
|
||||
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