From a029b84b0ce8955723c7a81ed6239444783f1f44 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Thu, 16 Apr 2026 03:38:32 +0800 Subject: [PATCH] Chore: domain alive check use custom fetch for DoH [skip ci] --- Build/lib/fetch-retry.ts | 20 +++++++++++++++++--- Build/lib/is-domain-alive.ts | 3 ++- package.json | 2 +- pnpm-lock.yaml | 10 +++++----- 4 files changed, 25 insertions(+), 10 deletions(-) diff --git a/Build/lib/fetch-retry.ts b/Build/lib/fetch-retry.ts index 7585fb1b..f56d915e 100644 --- a/Build/lib/fetch-retry.ts +++ b/Build/lib/fetch-retry.ts @@ -8,7 +8,8 @@ import undici, { import type { Dispatcher, Response, - RequestInit + RequestInit, + RequestInfo } from 'undici'; import { BetterSqlite3CacheStore } from 'undici-cache-store-better-sqlite3'; @@ -139,10 +140,18 @@ export class ResponseError extends Erro readonly code: 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; 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 this.name = this.constructor.name; 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 { const res = await undici.fetch(url, init); 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 */ export async function requestWithLog(url: string, opt?: Parameters[1]) { + opt ??= {}; + opt.dispatcher = agent; + try { const res = await undici.request(url, opt); if (res.statusCode >= 400) { diff --git a/Build/lib/is-domain-alive.ts b/Build/lib/is-domain-alive.ts index cab86e76..8f53ffbf 100644 --- a/Build/lib/is-domain-alive.ts +++ b/Build/lib/is-domain-alive.ts @@ -72,7 +72,8 @@ export async function getMethods() { const isDomainAlive = createDomainAliveChecker({ dns: { dnsServers, - maxAttempts: 6 + maxAttempts: 6, + customFetchForDoH: $$fetch as typeof fetch }, registerableDomainResultCache, resultCache, diff --git a/package.json b/package.json index 78495112..7f21e05e 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,7 @@ "ci-info": "^4.4.0", "cli-progress": "^3.12.0", "csv-parse": "^6.2.1", - "domain-alive": "^0.1.18", + "domain-alive": "^0.1.20", "fast-cidr-tools": "^0.3.4", "fast-escape-regexp": "^1.0.1", "fast-uri": "^3.1.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a78f7ec9..bc27aeee 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -33,8 +33,8 @@ importers: specifier: ^6.2.1 version: 6.2.1 domain-alive: - specifier: ^0.1.18 - version: 0.1.18 + specifier: ^0.1.20 + version: 0.1.20 fast-cidr-tools: specifier: ^0.3.4 version: 0.3.4 @@ -1090,8 +1090,8 @@ packages: dom-serializer@1.4.1: resolution: {integrity: sha512-VHwB3KfrcOOkelEG2ZOfxqLZdfkil8PtJi4P8N2MMXucZq2yLp75ClViUlOVwyoHEDjYU433Aq+5zWP61+RGag==} - domain-alive@0.1.18: - resolution: {integrity: sha512-m6cQhd2X3qBVfZ6qgKeMDJTAYwawom2bCScoUEgmBMsuNQuvl/s8DfnwQJA87meeLOmOzkLiWluGsm75yHwreg==} + domain-alive@0.1.20: + resolution: {integrity: sha512-2GdRUDUJVEesfdA39W6bSznACEhm6V0qYWyxRedGg/Sr6WSFh7x6nYrix0GQ/+ZFQwhIdjRNn5mszm4OBqZkiQ==} domelementtype@2.3.0: resolution: {integrity: sha512-OLETBj6w0OsagBwdXnPdN0cnMfF9opN69co+7ZrbfPGrdpPVNBUj02spi6B1N7wChLQiPn4CSH/zJvXw56gmHw==} @@ -2850,7 +2850,7 @@ snapshots: domhandler: 4.3.1 entities: 2.2.0 - domain-alive@0.1.18: + domain-alive@0.1.20: dependencies: debug: 4.4.1 foxts: 5.4.0