Browse Source

Refactor: update library implementation

SukkaW 2 years ago
parent
commit
f5436b7763

+ 1 - 1
Build/lib/cache-filesystem.ts

@@ -49,7 +49,7 @@ export const TTL = {
   TWLVE_HOURS: () => randomInt(8, 12) * 60 * 60 * 1000,
   TWLVE_HOURS: () => randomInt(8, 12) * 60 * 60 * 1000,
   ONE_DAY: () => randomInt(23, 25) * 60 * 60 * 1000,
   ONE_DAY: () => randomInt(23, 25) * 60 * 60 * 1000,
   THREE_DAYS: () => randomInt(1, 3) * 24 * 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,
   TWO_WEEKS: () => randomInt(10, 14) * 24 * 60 * 60 * 1000,
   TEN_DAYS: () => randomInt(7, 10) * 24 * 60 * 60 * 1000
   TEN_DAYS: () => randomInt(7, 10) * 24 * 60 * 60 * 1000
 };
 };

+ 29 - 42
Build/lib/fetch-text-by-line.ts

@@ -1,60 +1,47 @@
 import type { BunFile } from 'bun';
 import type { BunFile } from 'bun';
 import { fetchWithRetry, defaultRequestInit } from './fetch-retry';
 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());
-// }
+import { TextLineStream } from './text-line-transform-stream';
+import { PolyfillTextDecoderStream } from './text-decoder-stream';
+function createTextLineStreamFromStreamSource(stream: ReadableStream<Uint8Array>) {
+  return 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.');
+// const decoder = new TextDecoder('utf-8');
+// async function *createTextLineAsyncGeneratorFromStreamSource(stream: ReadableStream<Uint8Array>): AsyncGenerator<string> {
+//   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;
+//       }
+//     }
 //   }
 //   }
 
 
-//   return (resp.body as ReadableStream<Uint8Array>).pipeThrough(new PolyfillTextDecoderStream()).pipeThrough(new TextLineStream());
+//   if (buf) {
+//     yield buf;
+//   }
 // }
 // }
 
 
-const decoder = new TextDecoder('utf-8');
-
-async function *createTextLineAsyncGeneratorFromStreamSource(stream: ReadableStream<Uint8Array>): AsyncGenerator<string> {
-  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): AsyncGenerator<string> {
+export function readFileByLine(file: string | URL | BunFile) {
   if (typeof file === 'string') {
   if (typeof file === 'string') {
     file = Bun.file(file);
     file = Bun.file(file);
   } else if (!('writer' in file)) {
   } else if (!('writer' in file)) {
     file = Bun.file(file);
     file = Bun.file(file);
   }
   }
 
 
-  return createTextLineAsyncGeneratorFromStreamSource(file.stream());
+  return createTextLineStreamFromStreamSource(file.stream());
 }
 }
 
 
-export function createReadlineInterfaceFromResponse(resp: Response): AsyncGenerator<string> {
+export function createReadlineInterfaceFromResponse(resp: Response) {
   if (!resp.body) {
   if (!resp.body) {
     throw new Error('Failed to fetch remote text');
     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.');
     throw new Error('Body has already been consumed.');
   }
   }
 
 
-  return createTextLineAsyncGeneratorFromStreamSource(resp.body);
+  return createTextLineStreamFromStreamSource(resp.body);
 }
 }
 
 
 export function fetchRemoteTextByLine(url: string | URL) {
 export function fetchRemoteTextByLine(url: string | URL) {

+ 0 - 3
Build/lib/parse-filter.ts

@@ -127,9 +127,6 @@ export async function processFilterRules(
 
 
         const flag = result[1];
         const flag = result[1];
         const hostname = result[0];
         const hostname = result[0];
-        // if (hostname.endsWith('.')) {
-        //   hostname = hostname.slice(0, -1);
-        // }
 
 
         if (DEBUG_DOMAIN_TO_FIND) {
         if (DEBUG_DOMAIN_TO_FIND) {
           if (hostname.includes(DEBUG_DOMAIN_TO_FIND)) {
           if (hostname.includes(DEBUG_DOMAIN_TO_FIND)) {

+ 4 - 5
Build/lib/text-line-transform-stream.ts

@@ -4,14 +4,13 @@
 
 
 interface TextLineStreamOptions {
 interface TextLineStreamOptions {
   /** Allow splitting by solo \r */
   /** Allow splitting by solo \r */
-  allowCR: boolean
+  allowCR?: boolean
 }
 }
 
 
 /** Transform a stream into a stream where each chunk is divided by a newline,
 /** 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.
  * be it `\n` or `\r\n`. `\r` can be enabled via the `allowCR` option.
  *
  *
  * ```ts
  * ```ts
- * import { TextLineStream } from 'https://deno.land/std@$STD_VERSION/streams/text_line_stream.ts';
  * const res = await fetch('https://example.com');
  * const res = await fetch('https://example.com');
  * const lines = res.body!
  * const lines = res.body!
  *   .pipeThrough(new TextDecoderStream())
  *   .pipeThrough(new TextDecoderStream())
@@ -20,9 +19,9 @@ interface TextLineStreamOptions {
  */
  */
 export class TextLineStream extends TransformStream<string, string> {
 export class TextLineStream extends TransformStream<string, string> {
   // private __buf = '';
   // private __buf = '';
-  constructor(options?: TextLineStreamOptions) {
-    const allowCR = options?.allowCR ?? false;
-
+  constructor({
+    allowCR = false
+  }: TextLineStreamOptions = {}) {
     let __buf = '';
     let __buf = '';
 
 
     super({
     super({