Browse Source

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

SukkaW 1 month ago
parent
commit
a029b84b0c
4 changed files with 25 additions and 10 deletions
  1. 17 3
      Build/lib/fetch-retry.ts
  2. 2 1
      Build/lib/is-domain-alive.ts
  3. 1 1
      package.json
  4. 5 5
      pnpm-lock.yaml

+ 17 - 3
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<T extends UndiciResponseData | Response> 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<typeof undici.request>[1]) {
+  opt ??= {};
+  opt.dispatcher = agent;
+
   try {
     const res = await undici.request(url, opt);
     if (res.statusCode >= 400) {

+ 2 - 1
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,

+ 1 - 1
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",

+ 5 - 5
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