diff --git a/Build/lib/cache-filesystem.ts b/Build/lib/cache-filesystem.ts index 1a812891..1065b127 100644 --- a/Build/lib/cache-filesystem.ts +++ b/Build/lib/cache-filesystem.ts @@ -49,7 +49,7 @@ export const TTL = { TWLVE_HOURS: () => randomInt(8, 12) * 60 * 60 * 1000, ONE_DAY: () => randomInt(23, 25) * 60 * 60 * 1000, THREE_DAYS: () => randomInt(1, 3) * 24 * 60 * 60 * 1000, - ONE_WEEK: () => randomInt(5, 7) * 24 * 60 * 60 * 1000, + ONE_WEEK: () => randomInt(4, 7) * 24 * 60 * 60 * 1000, TWO_WEEKS: () => randomInt(10, 14) * 24 * 60 * 60 * 1000, TEN_DAYS: () => randomInt(7, 10) * 24 * 60 * 60 * 1000 }; diff --git a/Build/lib/fetch-text-by-line.ts b/Build/lib/fetch-text-by-line.ts index 53a87e71..48dae086 100644 --- a/Build/lib/fetch-text-by-line.ts +++ b/Build/lib/fetch-text-by-line.ts @@ -1,60 +1,47 @@ import type { BunFile } from 'bun'; import { fetchWithRetry, defaultRequestInit } from './fetch-retry'; -// import { TextLineStream } from './text-line-transform-stream'; -// import { PolyfillTextDecoderStream } from './text-decoder-stream'; -// export function readFileByLine(file: string | BunFile) { -// if (typeof file === 'string') { -// file = Bun.file(file); -// } -// return file.stream().pipeThrough(new PolyfillTextDecoderStream()).pipeThrough(new TextLineStream()); -// } - -// export function createReadlineInterfaceFromResponse(resp: Response) { -// if (!resp.body) { -// throw new Error('Failed to fetch remote text'); -// } -// if (resp.bodyUsed) { -// throw new Error('Body has already been consumed.'); -// } - -// return (resp.body as ReadableStream).pipeThrough(new PolyfillTextDecoderStream()).pipeThrough(new TextLineStream()); -// } - -const decoder = new TextDecoder('utf-8'); - -async function *createTextLineAsyncGeneratorFromStreamSource(stream: ReadableStream): AsyncGenerator { - let buf = ''; - - for await (const chunk of stream) { - const chunkStr = decoder.decode(chunk).replaceAll('\r\n', '\n'); - for (let i = 0, len = chunkStr.length; i < len; i++) { - const char = chunkStr[i]; - if (char === '\n') { - yield buf; - buf = ''; - } else { - buf += char; - } - } - } - - if (buf) { - yield buf; - } +import { TextLineStream } from './text-line-transform-stream'; +import { PolyfillTextDecoderStream } from './text-decoder-stream'; +function createTextLineStreamFromStreamSource(stream: ReadableStream) { + return stream + .pipeThrough(new PolyfillTextDecoderStream()) + .pipeThrough(new TextLineStream()); } -export function readFileByLine(file: string | URL | BunFile): AsyncGenerator { +// const decoder = new TextDecoder('utf-8'); +// async function *createTextLineAsyncGeneratorFromStreamSource(stream: ReadableStream): AsyncGenerator { +// let buf = ''; + +// for await (const chunk of stream) { +// const chunkStr = decoder.decode(chunk).replaceAll('\r\n', '\n'); +// for (let i = 0, len = chunkStr.length; i < len; i++) { +// const char = chunkStr[i]; +// if (char === '\n') { +// yield buf; +// buf = ''; +// } else { +// buf += char; +// } +// } +// } + +// if (buf) { +// yield buf; +// } +// } + +export function readFileByLine(file: string | URL | BunFile) { if (typeof file === 'string') { file = Bun.file(file); } else if (!('writer' in file)) { file = Bun.file(file); } - return createTextLineAsyncGeneratorFromStreamSource(file.stream()); + return createTextLineStreamFromStreamSource(file.stream()); } -export function createReadlineInterfaceFromResponse(resp: Response): AsyncGenerator { +export function createReadlineInterfaceFromResponse(resp: Response) { if (!resp.body) { throw new Error('Failed to fetch remote text'); } @@ -62,7 +49,7 @@ export function createReadlineInterfaceFromResponse(resp: Response): AsyncGenera throw new Error('Body has already been consumed.'); } - return createTextLineAsyncGeneratorFromStreamSource(resp.body); + return createTextLineStreamFromStreamSource(resp.body); } export function fetchRemoteTextByLine(url: string | URL) { diff --git a/Build/lib/parse-filter.ts b/Build/lib/parse-filter.ts index 74e4bc63..b92057d3 100644 --- a/Build/lib/parse-filter.ts +++ b/Build/lib/parse-filter.ts @@ -127,9 +127,6 @@ export async function processFilterRules( const flag = result[1]; const hostname = result[0]; - // if (hostname.endsWith('.')) { - // hostname = hostname.slice(0, -1); - // } if (DEBUG_DOMAIN_TO_FIND) { if (hostname.includes(DEBUG_DOMAIN_TO_FIND)) { diff --git a/Build/lib/text-line-transform-stream.ts b/Build/lib/text-line-transform-stream.ts index 9bc2f175..65ee5966 100644 --- a/Build/lib/text-line-transform-stream.ts +++ b/Build/lib/text-line-transform-stream.ts @@ -4,14 +4,13 @@ interface TextLineStreamOptions { /** Allow splitting by solo \r */ - allowCR: boolean + allowCR?: boolean } /** Transform a stream into a stream where each chunk is divided by a newline, * be it `\n` or `\r\n`. `\r` can be enabled via the `allowCR` option. * * ```ts - * import { TextLineStream } from 'https://deno.land/std@$STD_VERSION/streams/text_line_stream.ts'; * const res = await fetch('https://example.com'); * const lines = res.body! * .pipeThrough(new TextDecoderStream()) @@ -20,9 +19,9 @@ interface TextLineStreamOptions { */ export class TextLineStream extends TransformStream { // private __buf = ''; - constructor(options?: TextLineStreamOptions) { - const allowCR = options?.allowCR ?? false; - + constructor({ + allowCR = false + }: TextLineStreamOptions = {}) { let __buf = ''; super({