浏览代码

Chore: throw on not handled filter line

SukkaW 1 年之前
父节点
当前提交
17918f722b
共有 2 个文件被更改,包括 15 次插入11 次删除
  1. 6 6
      Build/lib/fetch-text-by-line.ts
  2. 9 5
      Build/lib/parse-filter.ts

+ 6 - 6
Build/lib/fetch-text-by-line.ts

@@ -9,7 +9,7 @@ import { processLine } from './process-line';
 import { $fetch } from './make-fetch-happen';
 import type { NodeFetchResponse } from './make-fetch-happen';
 import type { UndiciResponseData } from './fetch-retry';
-import type { Response } from 'undici';
+import type { Response as UnidiciWebResponse } from 'undici';
 
 function getReadableStream(file: string | FileHandle): ReadableStream {
   if (typeof file === 'string') {
@@ -23,7 +23,7 @@ export const readFileByLine: ((file: string | FileHandle) => AsyncIterable<strin
   .pipeThrough(new TextDecoderStream())
   .pipeThrough(new TextLineStream());
 
-function ensureResponseBody<T extends NodeFetchResponse | UndiciResponseData | Response>(resp: T): NonNullable<T['body']> {
+function ensureResponseBody<T extends NodeFetchResponse | UndiciResponseData | UnidiciWebResponse>(resp: T): NonNullable<T['body']> {
   if (resp.body == null) {
     throw new Error('Failed to fetch remote text');
   }
@@ -33,15 +33,15 @@ function ensureResponseBody<T extends NodeFetchResponse | UndiciResponseData | R
   return resp.body;
 }
 
-export const createReadlineInterfaceFromResponse: ((resp: NodeFetchResponse | UndiciResponseData | Response) => AsyncIterable<string>) = (resp) => {
+export const createReadlineInterfaceFromResponse: ((resp: NodeFetchResponse | UndiciResponseData | UnidiciWebResponse) => AsyncIterable<string>) = (resp) => {
   const stream = ensureResponseBody(resp);
 
   const webStream: ReadableStream<Uint8Array> = 'getReader' in stream
     ? stream
     : (
-      'body' in stream
-        ? stream.body
-        : Readable.toWeb(new Readable().wrap(stream)) as any
+      'text' in stream
+        ? stream.body as any
+        : Readable.toWeb(new Readable().wrap(stream))
     );
 
   return webStream

+ 9 - 5
Build/lib/parse-filter.ts

@@ -129,7 +129,8 @@ const enum ParseType {
   BlackAbsolute = 1,
   BlackIncludeSubdomain = 2,
   ErrorMessage = 10,
-  Null = 1000
+  Null = 1000,
+  NotParsed = 2000
 }
 
 export { type ParseType };
@@ -151,7 +152,7 @@ export async function processFilterRules(
 
       const warningMessages: string[] = [];
 
-      const MUTABLE_PARSE_LINE_RESULT: [string, ParseType] = ['', 1000];
+      const MUTABLE_PARSE_LINE_RESULT: [string, ParseType] = ['', ParseType.NotParsed];
       /**
        * @param {string} line
        */
@@ -159,6 +160,9 @@ export async function processFilterRules(
         const result = parse(line, MUTABLE_PARSE_LINE_RESULT, allowThirdParty);
         const flag = result[1];
 
+        if (flag === ParseType.NotParsed) {
+          throw new Error(`Didn't parse line: ${line}`);
+        }
         if (flag === ParseType.Null) {
           return;
         }
@@ -187,9 +191,6 @@ export async function processFilterRules(
           case ParseType.WhiteAbsolute:
             whitelistDomainSets.add(hostname);
             break;
-          case ParseType.BlackAbsolute:
-            blacklistDomainSets.add(hostname);
-            break;
           case ParseType.BlackIncludeSubdomain:
             if (hostname[0] === '.') {
               blacklistDomainSets.add(hostname);
@@ -197,6 +198,9 @@ export async function processFilterRules(
               blacklistDomainSets.add(`.${hostname}`);
             }
             break;
+          case ParseType.BlackAbsolute:
+            blacklistDomainSets.add(hostname);
+            break;
           case ParseType.ErrorMessage:
             warningMessages.push(hostname);
             break;