mirror of
https://github.com/SukkaW/Surge.git
synced 2025-12-13 09:40:34 +08:00
Chore: try new way of reading files by line
This commit is contained in:
parent
83074c8d95
commit
47ef19e0f8
@ -1,9 +1,9 @@
|
|||||||
import { readFileByLine, readFileByLineLegacy } from './fetch-text-by-line';
|
import { readFileByLine, readFileByLineLegacy, readFileByLineNew } from './fetch-text-by-line';
|
||||||
import path from 'node:path';
|
import path from 'node:path';
|
||||||
import fsp from 'node:fs/promises';
|
import fsp from 'node:fs/promises';
|
||||||
import { SOURCE_DIR } from '../constants/dir';
|
import { OUTPUT_SURGE_DIR } from '../constants/dir';
|
||||||
|
|
||||||
const file = path.join(SOURCE_DIR, 'domainset/cdn.conf');
|
const file = path.join(OUTPUT_SURGE_DIR, 'domainset/reject_extra.conf');
|
||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
const { bench, group, run } = await import('mitata');
|
const { bench, group, run } = await import('mitata');
|
||||||
@ -11,6 +11,7 @@ const file = path.join(SOURCE_DIR, 'domainset/cdn.conf');
|
|||||||
group(() => {
|
group(() => {
|
||||||
bench('readFileByLine', () => Array.fromAsync(readFileByLine(file)));
|
bench('readFileByLine', () => Array.fromAsync(readFileByLine(file)));
|
||||||
bench('readFileByLineLegacy', () => Array.fromAsync(readFileByLineLegacy(file)));
|
bench('readFileByLineLegacy', () => Array.fromAsync(readFileByLineLegacy(file)));
|
||||||
|
bench('readFileByLineNew', async () => Array.fromAsync(await readFileByLineNew(file)));
|
||||||
bench('fsp.readFile', () => fsp.readFile(file, 'utf-8').then((content) => content.split('\n')));
|
bench('fsp.readFile', () => fsp.readFile(file, 'utf-8').then((content) => content.split('\n')));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
import fs from 'node:fs';
|
import fs from 'node:fs';
|
||||||
import { Readable } from 'node:stream';
|
import { Readable } from 'node:stream';
|
||||||
|
import fsp from 'node:fs/promises';
|
||||||
import type { FileHandle } from 'node:fs/promises';
|
import type { FileHandle } from 'node:fs/promises';
|
||||||
import readline from 'node:readline';
|
import readline from 'node:readline';
|
||||||
|
|
||||||
@ -25,10 +26,17 @@ export const readFileByLineLegacy: ((file: string /* | FileHandle */) => AsyncIt
|
|||||||
.pipeThrough(new TextDecoderStream())
|
.pipeThrough(new TextDecoderStream())
|
||||||
.pipeThrough(new TextLineStream());
|
.pipeThrough(new TextLineStream());
|
||||||
|
|
||||||
export const readFileByLine: ((file: string /* | FileHandle */) => AsyncIterable<string>) = (file: string) => readline.createInterface({
|
export function readFileByLine(file: string): AsyncIterable<string> {
|
||||||
input: fs.createReadStream(file/* , { encoding: 'utf-8' } */),
|
return readline.createInterface({
|
||||||
crlfDelay: Infinity
|
input: fs.createReadStream(file/* , { encoding: 'utf-8' } */),
|
||||||
});
|
crlfDelay: Infinity
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const fdReadLines = (fd: FileHandle) => fd.readLines();
|
||||||
|
export async function readFileByLineNew(file: string): Promise<AsyncIterable<string>> {
|
||||||
|
return fsp.open(file, 'r').then(fdReadLines);
|
||||||
|
}
|
||||||
|
|
||||||
function ensureResponseBody<T extends NodeFetchResponse | UndiciResponseData | UnidiciWebResponse>(resp: T): NonNullable<T['body']> {
|
function ensureResponseBody<T extends NodeFetchResponse | UndiciResponseData | UnidiciWebResponse>(resp: T): NonNullable<T['body']> {
|
||||||
if (resp.body == null) {
|
if (resp.body == null) {
|
||||||
|
|||||||
@ -1,7 +1,6 @@
|
|||||||
import path, { dirname } from 'node:path';
|
import { dirname } from 'node:path';
|
||||||
import fs from 'node:fs';
|
import fs from 'node:fs';
|
||||||
import fsp from 'node:fs/promises';
|
import fsp from 'node:fs/promises';
|
||||||
import { OUTPUT_CLASH_DIR, OUTPUT_SINGBOX_DIR, OUTPUT_SURGE_DIR } from '../constants/dir';
|
|
||||||
|
|
||||||
export function fastStringCompare(a: string, b: string) {
|
export function fastStringCompare(a: string, b: string) {
|
||||||
const lenA = a.length;
|
const lenA = a.length;
|
||||||
@ -68,14 +67,6 @@ export function domainWildCardToRegex(domain: string) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function output(id: string, type: 'non_ip' | 'ip' | 'domainset') {
|
|
||||||
return [
|
|
||||||
path.join(OUTPUT_SURGE_DIR, type, id + '.conf'),
|
|
||||||
path.join(OUTPUT_CLASH_DIR, type, id + '.txt'),
|
|
||||||
path.join(OUTPUT_SINGBOX_DIR, type, id + '.json')
|
|
||||||
] as const;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function withBannerArray(title: string, description: string[] | readonly string[], date: Date, content: string[]) {
|
export function withBannerArray(title: string, description: string[] | readonly string[], date: Date, content: string[]) {
|
||||||
return [
|
return [
|
||||||
'#########################################',
|
'#########################################',
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user