Browse Source

Reafactor: rewrite domain wildcard to regex

SukkaW 1 year ago
parent
commit
991b192fa7
3 changed files with 20 additions and 16 deletions
  1. 18 2
      Build/lib/misc.ts
  2. 0 2
      package.json
  3. 2 12
      pnpm-lock.yaml

+ 18 - 2
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 = <T>(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');

+ 0 - 2
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",

+ 2 - 12
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: {}