Browse Source

Chore: minor changes

SukkaW 4 months ago
parent
commit
9bc40a80b5
2 changed files with 7 additions and 21 deletions
  1. 2 7
      Build/lib/fetch-assets.ts
  2. 5 14
      Build/lib/fetch-retry.ts

+ 2 - 7
Build/lib/fetch-assets.ts

@@ -8,14 +8,9 @@ import { AdGuardFilterIgnoreUnsupportedLinesStream } from './parse-filter/filter
 import { appendArrayInPlace } from 'foxts/append-array-in-place';
 
 import { newQueue } from '@henrygd/queue';
+import { AbortError } from 'foxts/abort-error';
 
-// eslint-disable-next-line sukka/unicorn/custom-error-definition -- typescript is better
-class CustomAbortError extends Error {
-  public readonly name = 'AbortError';
-  public readonly digest = 'AbortError';
-}
-
-const reusedCustomAbortError = new CustomAbortError();
+const reusedCustomAbortError = new AbortError();
 
 const queue = newQueue(16);
 

+ 5 - 14
Build/lib/fetch-retry.ts

@@ -18,6 +18,7 @@ import { inspect } from 'node:util';
 import path from 'node:path';
 import fs from 'node:fs';
 import { CACHE_DIR } from '../constants/dir';
+import { isAbortErrorLike } from 'foxts/abort-error';
 
 if (!fs.existsSync(CACHE_DIR)) {
   fs.mkdirSync(CACHE_DIR, { recursive: true });
@@ -166,13 +167,8 @@ export async function $$fetch(url: string, init: RequestInit = defaultRequestIni
 
     return res;
   } catch (err: unknown) {
-    if (typeof err === 'object' && err !== null && 'name' in err) {
-      if ((
-        err.name === 'AbortError'
-        || ('digest' in err && err.digest === 'AbortError')
-      )) {
-        console.log(picocolors.gray('[fetch abort]'), url);
-      }
+    if (isAbortErrorLike(err)) {
+      console.log(picocolors.gray('[fetch abort]'), url);
     } else {
       console.log(picocolors.gray('[fetch fail]'), url, { name: (err as any).name }, err);
     }
@@ -195,13 +191,8 @@ export async function requestWithLog(url: string, opt?: Parameters<typeof undici
 
     return res;
   } catch (err: unknown) {
-    if (typeof err === 'object' && err !== null && 'name' in err) {
-      if ((
-        err.name === 'AbortError'
-        || ('digest' in err && err.digest === 'AbortError')
-      )) {
-        console.log(picocolors.gray('[fetch abort]'), url);
-      }
+    if (isAbortErrorLike(err)) {
+      console.log(picocolors.gray('[fetch abort]'), url);
     } else {
       console.log(picocolors.gray('[fetch fail]'), url, { name: (err as any).name }, err);
     }