From 991b192fa7cb2119bd78a726e176b3a5b13dfbc6 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Wed, 14 Aug 2024 03:40:01 +0800 Subject: [PATCH] Reafactor: rewrite domain wildcard to regex --- Build/lib/misc.ts | 20 ++++++++++++++++++-- package.json | 2 -- pnpm-lock.yaml | 14 ++------------ 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/Build/lib/misc.ts b/Build/lib/misc.ts index 08736f82..da4f7fd4 100644 --- a/Build/lib/misc.ts +++ b/Build/lib/misc.ts @@ -1,7 +1,6 @@ import path, { dirname } from 'path'; import fs from 'fs'; import fsp from 'fs/promises'; -import { makeRe } from 'picomatch'; export const isTruthy = (i: T | 0 | '' | false | null | undefined): i is T => !!i; @@ -31,7 +30,24 @@ export const writeFile: Write = async (destination: string, input, dir = dirname }; export const domainWildCardToRegex = (domain: string) => { - return makeRe(domain, { contains: false, strictSlashes: true }).source; + let result = '^'; + for (let i = 0, len = domain.length; i < len; i++) { + switch (domain[i]) { + case '.': + result += String.raw`\.`; + break; + case '*': + result += '[a-zA-Z0-9-.]*?'; + break; + case '?': + result += '[a-zA-Z0-9-.]'; + break; + default: + result += domain[i]; + } + } + result += '$'; + return result; }; const OUTPUT_SURGE_DIR = path.resolve(__dirname, '../../List'); diff --git a/package.json b/package.json index fea1f990..fccfcce9 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,6 @@ "mnemonist": "^0.39.8", "path-scurry": "^1.11.1", "picocolors": "^1.0.1", - "picomatch": "^4.0.2", "punycode": "^2.3.1", "table": "^6.8.2", "tar-stream": "^3.1.7", @@ -50,7 +49,6 @@ "@types/better-sqlite3": "^7.6.11", "@types/chai": "^4.3.17", "@types/mocha": "^10.0.7", - "@types/picomatch": "^3.0.1", "@types/punycode": "^2.1.4", "@types/tar-stream": "^3.1.3", "chai": "4", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 347e353a..aeaa809b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -53,9 +53,6 @@ importers: picocolors: specifier: ^1.0.1 version: 1.0.1 - picomatch: - specifier: ^4.0.2 - version: 4.0.2 punycode: specifier: ^2.3.1 version: 2.3.1 @@ -96,9 +93,6 @@ importers: '@types/mocha': specifier: ^10.0.7 version: 10.0.7 - '@types/picomatch': - specifier: ^3.0.1 - version: 3.0.1 '@types/punycode': specifier: ^2.1.4 version: 2.1.4 @@ -446,9 +440,6 @@ packages: '@types/node@20.14.11': resolution: {integrity: sha512-kprQpL8MMeszbz6ojB5/tU8PLN4kesnN8Gjzw349rDlNgsSzg90lAVj3llK99Dh7JON+t9AuscPPFW6mPbTnSA==} - '@types/picomatch@3.0.1': - resolution: {integrity: sha512-1MRgzpzY0hOp9pW/kLRxeQhUWwil6gnrUYd3oEpeYBqp/FexhaCPv3F8LsYr47gtUU45fO2cm1dbwkSrHEo8Uw==} - '@types/punycode@2.1.4': resolution: {integrity: sha512-trzh6NzBnq8yw5e35f8xe8VTYjqM3NE7bohBtvDVf/dtUer3zYTLK1Ka3DG3p7bdtoaOHZucma6FfVKlQ134pQ==} @@ -1905,8 +1896,6 @@ snapshots: dependencies: undici-types: 5.26.5 - '@types/picomatch@3.0.1': {} - '@types/punycode@2.1.4': {} '@types/retry@0.12.5': {} @@ -2821,7 +2810,8 @@ snapshots: picomatch@2.3.1: {} - picomatch@4.0.2: {} + picomatch@4.0.2: + optional: true pirates@4.0.6: {}