mirror of
https://github.com/SukkaW/Surge.git
synced 2026-02-03 04:21:53 +08:00
Chore: more refactor to the bun
This commit is contained in:
62
Build/lib/fetch-remote-text-by-line.ts
Normal file
62
Build/lib/fetch-remote-text-by-line.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import type { BunFile } from 'bun';
|
||||
import { fetchWithRetry } from './fetch-retry';
|
||||
|
||||
const decoder = new TextDecoder('utf-8');
|
||||
|
||||
export async function* readFileByLine(file: string | BunFile): AsyncGenerator<string> {
|
||||
if (typeof file === 'string') {
|
||||
file = Bun.file(file);
|
||||
}
|
||||
|
||||
let buf = '';
|
||||
|
||||
for await (const chunk of file.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 async function* createReadlineInterfaceFromResponse(resp: Response): AsyncGenerator<string> {
|
||||
if (!resp.body) {
|
||||
throw new Error('Failed to fetch remote text');
|
||||
}
|
||||
if (resp.bodyUsed) {
|
||||
throw new Error('Body has already been consumed.');
|
||||
}
|
||||
|
||||
let buf = '';
|
||||
|
||||
for await (const chunk of resp.body) {
|
||||
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 async function fetchRemoteTextAndCreateReadlineInterface(url: string | URL, opt?: RequestInit): Promise<AsyncGenerator<string>> {
|
||||
const resp = await fetchWithRetry(url, opt);
|
||||
return createReadlineInterfaceFromResponse(resp);
|
||||
}
|
||||
Reference in New Issue
Block a user