mirror of
https://github.com/SukkaW/Surge.git
synced 2025-12-12 01:00:34 +08:00
Refactor: drop a few Bun.file usage
This commit is contained in:
parent
e0e79c9fe5
commit
5664c38261
@ -1,5 +1,4 @@
|
|||||||
// @ts-check
|
// @ts-check
|
||||||
import { readFileByLine } from './fetch-text-by-line';
|
|
||||||
import { surgeDomainsetToClashDomainset, surgeRulesetToClashClassicalTextRuleset } from './clash';
|
import { surgeDomainsetToClashDomainset, surgeRulesetToClashClassicalTextRuleset } from './clash';
|
||||||
import picocolors from 'picocolors';
|
import picocolors from 'picocolors';
|
||||||
import type { Span } from '../trace';
|
import type { Span } from '../trace';
|
||||||
@ -8,10 +7,11 @@ import fs from 'fs';
|
|||||||
import fsp from 'fs/promises';
|
import fsp from 'fs/promises';
|
||||||
import { sort } from './timsort';
|
import { sort } from './timsort';
|
||||||
import { fastStringArrayJoin } from './misc';
|
import { fastStringArrayJoin } from './misc';
|
||||||
|
import { readFileByLine } from './fetch-text-by-line';
|
||||||
|
|
||||||
export async function compareAndWriteFile(span: Span, linesA: string[], filePath: string) {
|
export async function compareAndWriteFile(span: Span, linesA: string[], filePath: string) {
|
||||||
let isEqual = true;
|
let isEqual = true;
|
||||||
const file = Bun.file(filePath);
|
const fd = await fsp.open(filePath);
|
||||||
|
|
||||||
const linesALen = linesA.length;
|
const linesALen = linesA.length;
|
||||||
|
|
||||||
@ -28,7 +28,7 @@ export async function compareAndWriteFile(span: Span, linesA: string[], filePath
|
|||||||
isEqual = await span.traceChildAsync(`comparing ${filePath}`, async () => {
|
isEqual = await span.traceChildAsync(`comparing ${filePath}`, async () => {
|
||||||
let index = 0;
|
let index = 0;
|
||||||
|
|
||||||
for await (const lineB of readFileByLine(file)) {
|
for await (const lineB of readFileByLine(fd)) {
|
||||||
const lineA = linesA[index] as string | undefined;
|
const lineA = linesA[index] as string | undefined;
|
||||||
index++;
|
index++;
|
||||||
|
|
||||||
@ -83,6 +83,8 @@ export async function compareAndWriteFile(span: Span, linesA: string[], filePath
|
|||||||
|
|
||||||
// return writer.end();
|
// return writer.end();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
await fd.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
export const withBannerArray = (title: string, description: string[] | readonly string[], date: Date, content: string[]) => {
|
export const withBannerArray = (title: string, description: string[] | readonly string[], date: Date, content: string[]) => {
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
import type { BunFile } from 'bun';
|
import type { BunFile } from 'bun';
|
||||||
import { fetchWithRetry, defaultRequestInit } from './fetch-retry';
|
import { fetchWithRetry, defaultRequestInit } from './fetch-retry';
|
||||||
|
import type { FileHandle } from 'fs/promises';
|
||||||
|
|
||||||
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';
|
||||||
@ -35,18 +36,20 @@ async function *createTextLineAsyncIterableFromStreamSource(stream: ReadableStre
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const getBunBlob = (file: string | URL | BunFile) => {
|
const getReadableStream = (file: string | BunFile | FileHandle): ReadableStream => {
|
||||||
if (typeof file === 'string') {
|
if (typeof file === 'string') {
|
||||||
return Bun.file(file);
|
return Bun.file(file).stream();
|
||||||
} if (!('writer' in file)) {
|
|
||||||
return Bun.file(file);
|
|
||||||
}
|
}
|
||||||
return file;
|
if ('writer' in file) {
|
||||||
|
return file.stream();
|
||||||
|
}
|
||||||
|
return file.readableWebStream();
|
||||||
};
|
};
|
||||||
|
|
||||||
export const readFileByLine: ((file: string | URL | BunFile) => AsyncIterable<string>) = enableTextLineStream
|
// TODO: use FileHandle.readLine()
|
||||||
? (file: string | URL | BunFile) => getBunBlob(file).stream().pipeThrough(new PolyfillTextDecoderStream()).pipeThrough(new TextLineStream())
|
export const readFileByLine: ((file: string | BunFile | FileHandle) => AsyncIterable<string>) = enableTextLineStream
|
||||||
: (file: string | URL | BunFile) => createTextLineAsyncIterableFromStreamSource(getBunBlob(file).stream());
|
? (file: string | BunFile | FileHandle) => getReadableStream(file).pipeThrough(new PolyfillTextDecoderStream()).pipeThrough(new TextLineStream())
|
||||||
|
: (file: string | BunFile | FileHandle) => createTextLineAsyncIterableFromStreamSource(getReadableStream(file));
|
||||||
|
|
||||||
const ensureResponseBody = (resp: Response) => {
|
const ensureResponseBody = (resp: Response) => {
|
||||||
if (!resp.body) {
|
if (!resp.body) {
|
||||||
|
|||||||
@ -2,7 +2,9 @@ import { toASCII } from 'punycode';
|
|||||||
import { createMemoizedPromise } from './memo-promise';
|
import { createMemoizedPromise } from './memo-promise';
|
||||||
import { getPublicSuffixListTextPromise } from './download-publicsuffixlist';
|
import { getPublicSuffixListTextPromise } from './download-publicsuffixlist';
|
||||||
|
|
||||||
const customFetch = (url: string | URL): Promise<Blob> => Promise.resolve(Bun.file(url));
|
const customFetch = typeof Bun !== 'undefined'
|
||||||
|
? (url: string | URL) => Promise.resolve(Bun.file(url))
|
||||||
|
: (url: string | URL) => fetch(url).then(resp => resp.blob() as Promise<Blob>);
|
||||||
|
|
||||||
export const getGorhillPublicSuffixPromise = createMemoizedPromise(async () => {
|
export const getGorhillPublicSuffixPromise = createMemoizedPromise(async () => {
|
||||||
const [publicSuffixListDat, { default: gorhill }] = await Promise.all([
|
const [publicSuffixListDat, { default: gorhill }] = await Promise.all([
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user