From 17918f722b8a00be466e0c67d400d415fe0febd4 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Mon, 21 Oct 2024 23:48:06 +0800 Subject: [PATCH] Chore: throw on not handled filter line --- Build/lib/fetch-text-by-line.ts | 12 ++++++------ Build/lib/parse-filter.ts | 14 +++++++++----- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/Build/lib/fetch-text-by-line.ts b/Build/lib/fetch-text-by-line.ts index a32c3779..52db0d2b 100644 --- a/Build/lib/fetch-text-by-line.ts +++ b/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(resp: T): NonNullable { +function ensureResponseBody(resp: T): NonNullable { if (resp.body == null) { throw new Error('Failed to fetch remote text'); } @@ -33,15 +33,15 @@ function ensureResponseBody AsyncIterable) = (resp) => { +export const createReadlineInterfaceFromResponse: ((resp: NodeFetchResponse | UndiciResponseData | UnidiciWebResponse) => AsyncIterable) = (resp) => { const stream = ensureResponseBody(resp); const webStream: ReadableStream = '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 diff --git a/Build/lib/parse-filter.ts b/Build/lib/parse-filter.ts index 02b2e05a..f7351115 100644 --- a/Build/lib/parse-filter.ts +++ b/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;