Skip to content

Commit

Permalink
Chore: throw on not handled filter line
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Oct 21, 2024
1 parent beb11fa commit 17918f7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
12 changes: 6 additions & 6 deletions Build/lib/fetch-text-by-line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand All @@ -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');
}
Expand All @@ -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
Expand Down
14 changes: 9 additions & 5 deletions Build/lib/parse-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ const enum ParseType {
BlackAbsolute = 1,
BlackIncludeSubdomain = 2,
ErrorMessage = 10,
Null = 1000
Null = 1000,
NotParsed = 2000
}

export { type ParseType };
Expand All @@ -151,14 +152,17 @@ 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
*/
const lineCb = (line: string) => {
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;
}
Expand Down Expand Up @@ -187,16 +191,16 @@ 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);
} else {
blacklistDomainSets.add(`.${hostname}`);
}
break;
case ParseType.BlackAbsolute:
blacklistDomainSets.add(hostname);
break;
case ParseType.ErrorMessage:
warningMessages.push(hostname);
break;
Expand Down

0 comments on commit 17918f7

Please sign in to comment.