mirror of
https://github.com/SukkaW/Surge.git
synced 2025-12-12 01:00:34 +08:00
Chore: enable TextLineStream with env
This commit is contained in:
parent
c276ac3330
commit
0b0dda6cc2
@ -18,11 +18,6 @@ export const buildDeprecateFiles = task(import.meta.path, (span) => span.traceCh
|
|||||||
const surgeFile = path.resolve(outputSurgeDir, `${filePath}.conf`);
|
const surgeFile = path.resolve(outputSurgeDir, `${filePath}.conf`);
|
||||||
const clashFile = path.resolve(outputClashDir, `${filePath}.txt`);
|
const clashFile = path.resolve(outputClashDir, `${filePath}.txt`);
|
||||||
|
|
||||||
console.log({
|
|
||||||
surgeFile,
|
|
||||||
clashFile
|
|
||||||
});
|
|
||||||
|
|
||||||
const content = [
|
const content = [
|
||||||
'#########################################',
|
'#########################################',
|
||||||
'# Sukka\'s Ruleset - Deprecated',
|
'# Sukka\'s Ruleset - Deprecated',
|
||||||
|
|||||||
@ -1,20 +1,21 @@
|
|||||||
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 { TextLineStream } from './text-line-transform-stream';
|
||||||
// import { PolyfillTextDecoderStream } from './text-decoder-stream';
|
import { PolyfillTextDecoderStream } from './text-decoder-stream';
|
||||||
import { processLine } from './process-line';
|
import { processLine } from './process-line';
|
||||||
// function createTextLineStreamFromStreamSource(stream: ReadableStream<Uint8Array>) {
|
|
||||||
// return stream
|
const enableTextLineStream = !!process.env.ENABLE_TEXT_LINE_STREAM;
|
||||||
// .pipeThrough(new PolyfillTextDecoderStream())
|
|
||||||
// .pipeThrough(new TextLineStream());
|
interface TextLineStreamLike {
|
||||||
// }
|
[Symbol.asyncIterator](): AsyncIterableIterator<string>
|
||||||
|
}
|
||||||
|
|
||||||
const decoder = new TextDecoder('utf-8');
|
const decoder = new TextDecoder('utf-8');
|
||||||
async function *createTextLineAsyncGeneratorFromStreamSource(stream: ReadableStream<Uint8Array>): AsyncGenerator<string> {
|
async function *createTextLineAsyncGeneratorFromStreamSource(stream: ReadableStream<Uint8Array>): AsyncGenerator<string> {
|
||||||
let buf = '';
|
let buf = '';
|
||||||
|
|
||||||
for await (const chunk of stream) {
|
for await (const chunk of stream as any) {
|
||||||
const chunkStr = decoder.decode(chunk).replaceAll('\r\n', '\n');
|
const chunkStr = decoder.decode(chunk).replaceAll('\r\n', '\n');
|
||||||
for (let i = 0, len = chunkStr.length; i < len; i++) {
|
for (let i = 0, len = chunkStr.length; i < len; i++) {
|
||||||
const char = chunkStr[i];
|
const char = chunkStr[i];
|
||||||
@ -32,25 +33,45 @@ async function *createTextLineAsyncGeneratorFromStreamSource(stream: ReadableStr
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function readFileByLine(file: string | URL | BunFile) {
|
export const readFileByLine: ((file: string | URL | BunFile) => TextLineStreamLike) = enableTextLineStream
|
||||||
if (typeof file === 'string') {
|
? (file: string | URL | BunFile) => {
|
||||||
file = Bun.file(file);
|
if (typeof file === 'string') {
|
||||||
} else if (!('writer' in file)) {
|
file = Bun.file(file);
|
||||||
file = Bun.file(file);
|
} else if (!('writer' in file)) {
|
||||||
}
|
file = Bun.file(file);
|
||||||
|
}
|
||||||
|
|
||||||
return createTextLineAsyncGeneratorFromStreamSource(file.stream());
|
return file.stream().pipeThrough(new PolyfillTextDecoderStream()).pipeThrough(new TextLineStream());
|
||||||
}
|
}
|
||||||
|
: (file: string | URL | BunFile) => {
|
||||||
|
if (typeof file === 'string') {
|
||||||
|
file = Bun.file(file);
|
||||||
|
} else if (!('writer' in file)) {
|
||||||
|
file = Bun.file(file);
|
||||||
|
}
|
||||||
|
|
||||||
export function createReadlineInterfaceFromResponse(this: void, resp: Response) {
|
return createTextLineAsyncGeneratorFromStreamSource(file.stream()) as any;
|
||||||
if (!resp.body) {
|
};
|
||||||
throw new Error('Failed to fetch remote text');
|
|
||||||
|
export const createReadlineInterfaceFromResponse: ((resp: Response) => TextLineStreamLike) = enableTextLineStream
|
||||||
|
? (resp) => {
|
||||||
|
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.pipeThrough(new PolyfillTextDecoderStream()).pipeThrough(new TextLineStream());
|
||||||
}
|
}
|
||||||
if (resp.bodyUsed) {
|
: (resp) => {
|
||||||
throw new Error('Body has already been consumed.');
|
if (!resp.body) {
|
||||||
}
|
throw new Error('Failed to fetch remote text');
|
||||||
return createTextLineAsyncGeneratorFromStreamSource(resp.body);
|
}
|
||||||
}
|
if (resp.bodyUsed) {
|
||||||
|
throw new Error('Body has already been consumed.');
|
||||||
|
}
|
||||||
|
return createTextLineAsyncGeneratorFromStreamSource(resp.body) as any;
|
||||||
|
};
|
||||||
|
|
||||||
export function fetchRemoteTextByLine(url: string | URL) {
|
export function fetchRemoteTextByLine(url: string | URL) {
|
||||||
return fetchWithRetry(url, defaultRequestInit).then(createReadlineInterfaceFromResponse);
|
return fetchWithRetry(url, defaultRequestInit).then(createReadlineInterfaceFromResponse);
|
||||||
|
|||||||
@ -9,6 +9,7 @@
|
|||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "bun ./Build/index.ts",
|
"build": "bun ./Build/index.ts",
|
||||||
|
"build-stream": "ENABLE_TEXT_LINE_STREAM=true bun ./Build/index.ts",
|
||||||
"lint": "eslint --format=sukka ."
|
"lint": "eslint --format=sukka ."
|
||||||
},
|
},
|
||||||
"author": "",
|
"author": "",
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user