Chore: domain alive check use custom fetch for DoH [skip ci]

This commit is contained in:
SukkaW
2026-04-16 03:38:32 +08:00
parent 2fa532a366
commit a029b84b0c
4 changed files with 25 additions and 10 deletions

View File

@@ -8,7 +8,8 @@ import undici, {
import type { import type {
Dispatcher, Dispatcher,
Response, Response,
RequestInit RequestInit,
RequestInfo
} from 'undici'; } from 'undici';
import { BetterSqlite3CacheStore } from 'undici-cache-store-better-sqlite3'; import { BetterSqlite3CacheStore } from 'undici-cache-store-better-sqlite3';
@@ -139,10 +140,18 @@ export class ResponseError<T extends UndiciResponseData | Response> extends Erro
readonly code: number; readonly code: number;
readonly statusCode: number; readonly statusCode: number;
constructor(public readonly res: T, public readonly url: string, ...args: any[]) { readonly url: string;
constructor(public readonly res: T, public readonly info: RequestInfo, ...args: any[]) {
const statusCode = 'statusCode' in res ? res.statusCode : res.status; const statusCode = 'statusCode' in res ? res.statusCode : res.status;
super('HTTP ' + statusCode + ' ' + args.map(_ => inspect(_)).join(' ')); super('HTTP ' + statusCode + ' ' + args.map(_ => inspect(_)).join(' '));
this.url = typeof info === 'string'
? info
: ('url' in info
? info.url
: info.href);
// eslint-disable-next-line sukka/unicorn/custom-error-definition -- deliberatly use previous name // eslint-disable-next-line sukka/unicorn/custom-error-definition -- deliberatly use previous name
this.name = this.constructor.name; this.name = this.constructor.name;
this.res = res; this.res = res;
@@ -157,7 +166,9 @@ export const defaultRequestInit = {
} }
}; };
export async function $$fetch(url: string, init: RequestInit = defaultRequestInit) { export async function $$fetch(url: RequestInfo, init: RequestInit = defaultRequestInit) {
init.dispatcher = agent;
try { try {
const res = await undici.fetch(url, init); const res = await undici.fetch(url, init);
if (res.status >= 400) { if (res.status >= 400) {
@@ -184,6 +195,9 @@ export { $$fetch as '~fetch' };
/** @deprecated -- undici.requests doesn't support gzip/br/deflate, and has difficulty w/ undidi cache */ /** @deprecated -- undici.requests doesn't support gzip/br/deflate, and has difficulty w/ undidi cache */
export async function requestWithLog(url: string, opt?: Parameters<typeof undici.request>[1]) { export async function requestWithLog(url: string, opt?: Parameters<typeof undici.request>[1]) {
opt ??= {};
opt.dispatcher = agent;
try { try {
const res = await undici.request(url, opt); const res = await undici.request(url, opt);
if (res.statusCode >= 400) { if (res.statusCode >= 400) {

View File

@@ -72,7 +72,8 @@ export async function getMethods() {
const isDomainAlive = createDomainAliveChecker({ const isDomainAlive = createDomainAliveChecker({
dns: { dns: {
dnsServers, dnsServers,
maxAttempts: 6 maxAttempts: 6,
customFetchForDoH: $$fetch as typeof fetch
}, },
registerableDomainResultCache, registerableDomainResultCache,
resultCache, resultCache,

View File

@@ -25,7 +25,7 @@
"ci-info": "^4.4.0", "ci-info": "^4.4.0",
"cli-progress": "^3.12.0", "cli-progress": "^3.12.0",
"csv-parse": "^6.2.1", "csv-parse": "^6.2.1",
"domain-alive": "^0.1.18", "domain-alive": "^0.1.20",
"fast-cidr-tools": "^0.3.4", "fast-cidr-tools": "^0.3.4",
"fast-escape-regexp": "^1.0.1", "fast-escape-regexp": "^1.0.1",
"fast-uri": "^3.1.0", "fast-uri": "^3.1.0",

10
pnpm-lock.yaml generated
View File

@@ -33,8 +33,8 @@ importers:
specifier: ^6.2.1 specifier: ^6.2.1
version: 6.2.1 version: 6.2.1
domain-alive: domain-alive:
specifier: ^0.1.18 specifier: ^0.1.20
version: 0.1.18 version: 0.1.20
fast-cidr-tools: fast-cidr-tools:
specifier: ^0.3.4 specifier: ^0.3.4
version: 0.3.4 version: 0.3.4
@@ -1090,8 +1090,8 @@ packages:
dom-serializer@1.4.1: dom-serializer@1.4.1:
resolution: {integrity: sha512-VHwB3KfrcOOkelEG2ZOfxqLZdfkil8PtJi4P8N2MMXucZq2yLp75ClViUlOVwyoHEDjYU433Aq+5zWP61+RGag==} resolution: {integrity: sha512-VHwB3KfrcOOkelEG2ZOfxqLZdfkil8PtJi4P8N2MMXucZq2yLp75ClViUlOVwyoHEDjYU433Aq+5zWP61+RGag==}
domain-alive@0.1.18: domain-alive@0.1.20:
resolution: {integrity: sha512-m6cQhd2X3qBVfZ6qgKeMDJTAYwawom2bCScoUEgmBMsuNQuvl/s8DfnwQJA87meeLOmOzkLiWluGsm75yHwreg==} resolution: {integrity: sha512-2GdRUDUJVEesfdA39W6bSznACEhm6V0qYWyxRedGg/Sr6WSFh7x6nYrix0GQ/+ZFQwhIdjRNn5mszm4OBqZkiQ==}
domelementtype@2.3.0: domelementtype@2.3.0:
resolution: {integrity: sha512-OLETBj6w0OsagBwdXnPdN0cnMfF9opN69co+7ZrbfPGrdpPVNBUj02spi6B1N7wChLQiPn4CSH/zJvXw56gmHw==} resolution: {integrity: sha512-OLETBj6w0OsagBwdXnPdN0cnMfF9opN69co+7ZrbfPGrdpPVNBUj02spi6B1N7wChLQiPn4CSH/zJvXw56gmHw==}
@@ -2850,7 +2850,7 @@ snapshots:
domhandler: 4.3.1 domhandler: 4.3.1
entities: 2.2.0 entities: 2.2.0
domain-alive@0.1.18: domain-alive@0.1.20:
dependencies: dependencies:
debug: 4.4.1 debug: 4.4.1
foxts: 5.4.0 foxts: 5.4.0