Chore: throw on not handled filter line

This commit is contained in:
SukkaW 2024-10-21 23:48:06 +08:00
parent beb11fae79
commit 17918f722b
2 changed files with 15 additions and 11 deletions

View File

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

View File

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