Housekeeping & Make ESLint Happy

This commit is contained in:
SukkaW
2024-08-26 08:14:03 +08:00
parent a00d85fb03
commit 8a0690fe9e
37 changed files with 194 additions and 133 deletions

View File

@@ -1,3 +1,5 @@
import process from 'node:process';
export const createCache = (namespace?: string, printStats = false) => {
const cache = new Map();

View File

@@ -1,17 +1,16 @@
import createDb from 'better-sqlite3';
import type { Database } from 'better-sqlite3';
import os from 'os';
import path from 'path';
import { mkdirSync } from 'fs';
import os from 'node:os';
import path from 'node:path';
import { mkdirSync } from 'node:fs';
import picocolors from 'picocolors';
import { fastStringArrayJoin } from './misc';
import { performance } from 'perf_hooks';
import fs from 'fs';
import { performance } from 'node:perf_hooks';
import fs from 'node:fs';
import { stringHash } from './string-hash';
const identity = (x: any) => x;
// eslint-disable-next-line sukka-ts/no-const-enum -- bun is smart, right?
const enum CacheStatus {
Hit = 'hit',
Stale = 'stale',
@@ -156,7 +155,7 @@ export class Cache<S = string> {
const now = Date.now();
const rv = this.db.prepare<string, { ttl: number }>(`SELECT ttl FROM ${this.tableName} WHERE key = ?`).get(key);
return !rv ? CacheStatus.Miss : (rv.ttl > now ? CacheStatus.Hit : CacheStatus.Stale);
return rv ? (rv.ttl > now ? CacheStatus.Hit : CacheStatus.Stale) : CacheStatus.Miss;
}
del(key: string): void {

View File

@@ -1,9 +1,11 @@
import path from 'path';
import fs from 'fs';
import fsp from 'fs/promises';
import { Readable } from 'stream';
import { pipeline } from 'stream/promises';
import zlib from 'zlib';
import path from 'node:path';
import fs from 'node:fs';
import fsp from 'node:fs/promises';
import { Readable } from 'node:stream';
import { pipeline } from 'node:stream/promises';
import zlib from 'node:zlib';
import process from 'node:process';
import { async as ezspawn } from '@jsdevtools/ez-spawn';
const mihomoBinaryDir = path.join(__dirname, '../../node_modules/.cache/mihomo');

View File

@@ -2,8 +2,8 @@
import { surgeDomainsetToClashDomainset, surgeRulesetToClashClassicalTextRuleset } from './clash';
import picocolors from 'picocolors';
import type { Span } from '../trace';
import path from 'path';
import fs from 'fs';
import path from 'node:path';
import fs from 'node:fs';
import { fastStringArrayJoin, writeFile } from './misc';
import { readFileByLine } from './fetch-text-by-line';
import stringify from 'json-stringify-pretty-compact';

View File

@@ -1,8 +1,13 @@
import picocolors from 'picocolors';
import { defaultRequestInit, fetchWithRetry } from './fetch-retry';
import { setTimeout } from 'timers/promises';
import { setTimeout } from 'node:timers/promises';
class CustomAbortError extends Error {
constructor() {
super();
this.name = 'CustomAbortError';
}
public readonly name = 'AbortError';
public readonly digest = 'AbortError';
}

View File

@@ -1,6 +1,6 @@
import retry from 'async-retry';
import picocolors from 'picocolors';
import { setTimeout } from 'timers/promises';
import { setTimeout } from 'node:timers/promises';
function isClientError(err: unknown): err is NodeJS.ErrnoException {
if (!err || typeof err !== 'object') return false;
@@ -24,6 +24,7 @@ export class ResponseError extends Error {
Error.captureStackTrace(this, ResponseError);
}
// eslint-disable-next-line sukka/unicorn/custom-error-definition -- deliberatly use previous name
this.name = this.constructor.name;
this.res = res;
this.code = res.status;
@@ -90,16 +91,12 @@ function createFetchRetry($fetch: typeof fetch): FetchWithRetry {
return res;
}
} catch (err: unknown) {
if (err instanceof Error) {
if (
err.name === 'AbortError'
|| ('digest' in err && err.digest === 'AbortError')
) {
if (!retryOpts.retryOnAborted) {
console.log(picocolors.gray('[fetch abort]'), url);
return bail(err) as never;
}
}
if (err instanceof Error && (
err.name === 'AbortError'
|| ('digest' in err && err.digest === 'AbortError')
) && !retryOpts.retryOnAborted) {
console.log(picocolors.gray('[fetch abort]'), url);
return bail(err) as never;
}
if (isClientError(err)) {
return bail(err) as never;

View File

@@ -1,8 +1,8 @@
import { bench, group, run } from 'mitata';
import { processLine, processLineFromReadline } from './process-line';
import { readFileByLine } from './fetch-text-by-line';
import path from 'path';
import fsp from 'fs/promises';
import path from 'node:path';
import fsp from 'node:fs/promises';
const file = path.resolve(__dirname, '../../Source/domainset/cdn.conf');

View File

@@ -1,11 +1,11 @@
import fs from 'fs';
import { Readable } from 'stream';
import fs from 'node:fs';
import { Readable } from 'node:stream';
import { fetchWithRetry, defaultRequestInit } from './fetch-retry';
import type { FileHandle } from 'fs/promises';
import type { FileHandle } from 'node:fs/promises';
import { TextLineStream } from './text-line-transform-stream';
import type { ReadableStream } from 'stream/web';
import { TextDecoderStream } from 'stream/web';
import type { ReadableStream } from 'node:stream/web';
import { TextDecoderStream } from 'node:stream/web';
import { processLine } from './process-line';
const getReadableStream = (file: string | FileHandle): ReadableStream => {

View File

@@ -1,6 +1,6 @@
import path, { dirname } from 'path';
import fs from 'fs';
import fsp from 'fs/promises';
import path, { dirname } from 'node:path';
import fs from 'node:fs';
import fsp from 'node:fs/promises';
export const isTruthy = <T>(i: T | 0 | '' | false | null | undefined): i is T => !!i;

View File

@@ -132,7 +132,6 @@ export function processHosts(span: Span, hostsUrl: string, mirrors: string[] | n
));
}
// eslint-disable-next-line sukka-ts/no-const-enum -- bun bundler is smart, maybe?
const enum ParseType {
WhiteIncludeSubdomain = 0,
WhiteAbsolute = -1,
@@ -174,25 +173,23 @@ export async function processFilterRules(
const hostname = result[0];
if (DEBUG_DOMAIN_TO_FIND) {
if (hostname.includes(DEBUG_DOMAIN_TO_FIND)) {
console.warn(
picocolors.red(filterRulesUrl),
flag === ParseType.WhiteIncludeSubdomain || flag === ParseType.WhiteAbsolute
? '(white)'
: '(black)',
hostname.replaceAll(DEBUG_DOMAIN_TO_FIND, picocolors.bold(DEBUG_DOMAIN_TO_FIND))
);
foundDebugDomain = true;
}
if (DEBUG_DOMAIN_TO_FIND && hostname.includes(DEBUG_DOMAIN_TO_FIND)) {
console.warn(
picocolors.red(filterRulesUrl),
flag === ParseType.WhiteIncludeSubdomain || flag === ParseType.WhiteAbsolute
? '(white)'
: '(black)',
hostname.replaceAll(DEBUG_DOMAIN_TO_FIND, picocolors.bold(DEBUG_DOMAIN_TO_FIND))
);
foundDebugDomain = true;
}
switch (flag) {
case ParseType.WhiteIncludeSubdomain:
if (hostname[0] !== '.') {
whitelistDomainSets.add(`.${hostname}`);
} else {
if (hostname[0] === '.') {
whitelistDomainSets.add(hostname);
} else {
whitelistDomainSets.add(`.${hostname}`);
}
break;
case ParseType.WhiteAbsolute:
@@ -202,10 +199,10 @@ export async function processFilterRules(
blacklistDomainSets.add(hostname);
break;
case ParseType.BlackIncludeSubdomain:
if (hostname[0] !== '.') {
blacklistDomainSets.add(`.${hostname}`);
} else {
if (hostname[0] === '.') {
blacklistDomainSets.add(hostname);
} else {
blacklistDomainSets.add(`.${hostname}`);
}
break;
case ParseType.ErrorMessage:

View File

@@ -2,7 +2,7 @@
// This module is browser compatible.
// Modified by Sukka (https://skk.moe) to increase compatibility and performance with Bun.
import { TransformStream } from 'stream/web';
import { TransformStream } from 'node:stream/web';
interface TextLineStreamOptions {
/** Allow splitting by solo \r */

View File

@@ -1,5 +1,5 @@
import fsp from 'fs/promises';
import { sep } from 'path';
import fsp from 'node:fs/promises';
import { sep } from 'node:path';
interface TreeFileType {
type: 'file',

View File

@@ -3,7 +3,7 @@
*/
import { fastStringArrayJoin } from './misc';
import { inspect } from 'util';
import { inspect } from 'node:util';
const noop = () => { /** noop */ };