diff --git a/Build/lib/create-file.ts b/Build/lib/create-file.ts index 0262b641..d9a76b07 100644 --- a/Build/lib/create-file.ts +++ b/Build/lib/create-file.ts @@ -1,5 +1,4 @@ // @ts-check -import { readFileByLine } from './fetch-text-by-line'; import { surgeDomainsetToClashDomainset, surgeRulesetToClashClassicalTextRuleset } from './clash'; import picocolors from 'picocolors'; import type { Span } from '../trace'; @@ -8,10 +7,11 @@ import fs from 'fs'; import fsp from 'fs/promises'; import { sort } from './timsort'; import { fastStringArrayJoin } from './misc'; +import { readFileByLine } from './fetch-text-by-line'; export async function compareAndWriteFile(span: Span, linesA: string[], filePath: string) { let isEqual = true; - const file = Bun.file(filePath); + const fd = await fsp.open(filePath); const linesALen = linesA.length; @@ -28,7 +28,7 @@ export async function compareAndWriteFile(span: Span, linesA: string[], filePath isEqual = await span.traceChildAsync(`comparing ${filePath}`, async () => { let index = 0; - for await (const lineB of readFileByLine(file)) { + for await (const lineB of readFileByLine(fd)) { const lineA = linesA[index] as string | undefined; index++; @@ -83,6 +83,8 @@ export async function compareAndWriteFile(span: Span, linesA: string[], filePath // return writer.end(); }); + + await fd.close(); } export const withBannerArray = (title: string, description: string[] | readonly string[], date: Date, content: string[]) => { diff --git a/Build/lib/fetch-text-by-line.ts b/Build/lib/fetch-text-by-line.ts index cfa0e965..1ed8cf82 100644 --- a/Build/lib/fetch-text-by-line.ts +++ b/Build/lib/fetch-text-by-line.ts @@ -1,5 +1,6 @@ import type { BunFile } from 'bun'; import { fetchWithRetry, defaultRequestInit } from './fetch-retry'; +import type { FileHandle } from 'fs/promises'; import { TextLineStream } from './text-line-transform-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') { - return Bun.file(file); - } if (!('writer' in file)) { - return Bun.file(file); + return Bun.file(file).stream(); } - return file; + if ('writer' in file) { + return file.stream(); + } + return file.readableWebStream(); }; -export const readFileByLine: ((file: string | URL | BunFile) => AsyncIterable) = enableTextLineStream - ? (file: string | URL | BunFile) => getBunBlob(file).stream().pipeThrough(new PolyfillTextDecoderStream()).pipeThrough(new TextLineStream()) - : (file: string | URL | BunFile) => createTextLineAsyncIterableFromStreamSource(getBunBlob(file).stream()); +// TODO: use FileHandle.readLine() +export const readFileByLine: ((file: string | BunFile | FileHandle) => AsyncIterable) = enableTextLineStream + ? (file: string | BunFile | FileHandle) => getReadableStream(file).pipeThrough(new PolyfillTextDecoderStream()).pipeThrough(new TextLineStream()) + : (file: string | BunFile | FileHandle) => createTextLineAsyncIterableFromStreamSource(getReadableStream(file)); const ensureResponseBody = (resp: Response) => { if (!resp.body) { diff --git a/Build/lib/get-gorhill-publicsuffix.ts b/Build/lib/get-gorhill-publicsuffix.ts index 5a6aa1b1..78766707 100644 --- a/Build/lib/get-gorhill-publicsuffix.ts +++ b/Build/lib/get-gorhill-publicsuffix.ts @@ -2,7 +2,9 @@ import { toASCII } from 'punycode'; import { createMemoizedPromise } from './memo-promise'; import { getPublicSuffixListTextPromise } from './download-publicsuffixlist'; -const customFetch = (url: string | URL): Promise => 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); export const getGorhillPublicSuffixPromise = createMemoizedPromise(async () => { const [publicSuffixListDat, { default: gorhill }] = await Promise.all([