mirror of
https://github.com/SukkaW/Surge.git
synced 2025-12-12 01:00:34 +08:00
Refactor: update library implementation
This commit is contained in:
parent
62ee184921
commit
f5436b7763
@ -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
|
||||
};
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
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());
|
||||
}
|
||||
|
||||
if (buf) {
|
||||
yield buf;
|
||||
}
|
||||
}
|
||||
// const decoder = new TextDecoder('utf-8');
|
||||
// async function *createTextLineAsyncGeneratorFromStreamSource(stream: ReadableStream<Uint8Array>): AsyncGenerator<string> {
|
||||
// let buf = '';
|
||||
|
||||
export function readFileByLine(file: string | URL | BunFile): AsyncGenerator<string> {
|
||||
// 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) {
|
||||
|
||||
@ -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)) {
|
||||
|
||||
@ -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({
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user