Refactor: update library implementation

This commit is contained in:
SukkaW 2024-01-09 14:59:36 +08:00
parent 62ee184921
commit f5436b7763
4 changed files with 37 additions and 54 deletions

View File

@ -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
};

View File

@ -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<Uint8Array>).pipeThrough(new PolyfillTextDecoderStream()).pipeThrough(new TextLineStream());
// }
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;
}
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 readFileByLine(file: string | URL | BunFile): AsyncGenerator<string> {
// 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) {
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<string> {
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) {

View File

@ -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)) {

View File

@ -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<string, string> {
// private __buf = '';
constructor(options?: TextLineStreamOptions) {
const allowCR = options?.allowCR ?? false;
constructor({
allowCR = false
}: TextLineStreamOptions = {}) {
let __buf = '';
super({