mirror of
https://github.com/SukkaW/Surge.git
synced 2025-12-12 09:10:35 +08:00
Chore: improve types & performance
This commit is contained in:
parent
7750a0291d
commit
e68b064fe5
@ -22,7 +22,8 @@ export const buildPublic = task(import.meta.path, async (span) => {
|
|||||||
.traceChild('copy public files')
|
.traceChild('copy public files')
|
||||||
.traceAsyncFn(async () => {
|
.traceAsyncFn(async () => {
|
||||||
const filesToBeCopied = (await listDir(
|
const filesToBeCopied = (await listDir(
|
||||||
rootPath, {
|
rootPath,
|
||||||
|
{
|
||||||
ignoreHidden: true,
|
ignoreHidden: true,
|
||||||
ignorePattern: /node_modules|Build|public/
|
ignorePattern: /node_modules|Build|public/
|
||||||
}
|
}
|
||||||
|
|||||||
@ -64,9 +64,7 @@ console.log('Bun version:', Bun.version, Bun.revision);
|
|||||||
|
|
||||||
const buildMicrosoftCdnPromise = downloadPreviousBuildPromise.then(() => buildMicrosoftCdn(rootSpan));
|
const buildMicrosoftCdnPromise = downloadPreviousBuildPromise.then(() => buildMicrosoftCdn(rootSpan));
|
||||||
|
|
||||||
const buildSSPanelUIMAppProfilePromise = Promise.all([
|
const buildSSPanelUIMAppProfilePromise = downloadPreviousBuildPromise.then(() => buildSSPanelUIMAppProfile(rootSpan));
|
||||||
downloadPreviousBuildPromise
|
|
||||||
]).then(() => buildSSPanelUIMAppProfile(rootSpan));
|
|
||||||
|
|
||||||
const downloadMockAssetsPromise = downloadMockAssets(rootSpan);
|
const downloadMockAssetsPromise = downloadMockAssets(rootSpan);
|
||||||
|
|
||||||
|
|||||||
@ -7,15 +7,12 @@ import { processLine } from './process-line';
|
|||||||
|
|
||||||
const enableTextLineStream = !!process.env.ENABLE_TEXT_LINE_STREAM;
|
const enableTextLineStream = !!process.env.ENABLE_TEXT_LINE_STREAM;
|
||||||
|
|
||||||
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 *createTextLineAsyncIterableFromStreamSource(stream: ReadableStream<Uint8Array>): AsyncIterable<string> {
|
||||||
let buf = '';
|
let buf = '';
|
||||||
|
|
||||||
for await (const chunk of stream as any) {
|
// @ts-expect-error -- ReadableStream<Uint8Array> should be AsyncIterable<Uint8Array>
|
||||||
|
for await (const chunk of stream) {
|
||||||
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];
|
||||||
@ -33,45 +30,34 @@ async function *createTextLineAsyncGeneratorFromStreamSource(stream: ReadableStr
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const readFileByLine: ((file: string | URL | BunFile) => TextLineStreamLike) = enableTextLineStream
|
const getBunBlob = (file: string | URL | BunFile) => {
|
||||||
? (file: string | URL | BunFile) => {
|
if (typeof file === 'string') {
|
||||||
if (typeof file === 'string') {
|
return Bun.file(file);
|
||||||
file = Bun.file(file);
|
} if (!('writer' in file)) {
|
||||||
} else if (!('writer' in file)) {
|
return Bun.file(file);
|
||||||
file = Bun.file(file);
|
|
||||||
}
|
|
||||||
|
|
||||||
return file.stream().pipeThrough(new PolyfillTextDecoderStream()).pipeThrough(new TextLineStream());
|
|
||||||
}
|
}
|
||||||
: (file: string | URL | BunFile) => {
|
return file;
|
||||||
if (typeof file === 'string') {
|
};
|
||||||
file = Bun.file(file);
|
|
||||||
} else if (!('writer' in file)) {
|
|
||||||
file = Bun.file(file);
|
|
||||||
}
|
|
||||||
|
|
||||||
return createTextLineAsyncGeneratorFromStreamSource(file.stream()) as any;
|
// @ts-expect-error -- ReadableStream<string> should be AsyncIterable<string>
|
||||||
};
|
export const readFileByLine: ((file: string | URL | BunFile) => AsyncIterable<string>) = enableTextLineStream
|
||||||
|
? (file: string | URL | BunFile) => getBunBlob(file).stream().pipeThrough(new PolyfillTextDecoderStream()).pipeThrough(new TextLineStream())
|
||||||
|
: (file: string | URL | BunFile) => createTextLineAsyncIterableFromStreamSource(getBunBlob(file).stream());
|
||||||
|
|
||||||
export const createReadlineInterfaceFromResponse: ((resp: Response) => TextLineStreamLike) = enableTextLineStream
|
const ensureResponseBody = (resp: Response) => {
|
||||||
? (resp) => {
|
if (!resp.body) {
|
||||||
if (!resp.body) {
|
throw new Error('Failed to fetch remote text');
|
||||||
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());
|
|
||||||
}
|
}
|
||||||
: (resp) => {
|
if (resp.bodyUsed) {
|
||||||
if (!resp.body) {
|
throw new Error('Body has already been consumed.');
|
||||||
throw new Error('Failed to fetch remote text');
|
}
|
||||||
}
|
return resp.body;
|
||||||
if (resp.bodyUsed) {
|
};
|
||||||
throw new Error('Body has already been consumed.');
|
|
||||||
}
|
// @ts-expect-error -- ReadableStream<string> should be AsyncIterable<string>
|
||||||
return createTextLineAsyncGeneratorFromStreamSource(resp.body) as any;
|
export const createReadlineInterfaceFromResponse: ((resp: Response) => AsyncIterable<string>) = enableTextLineStream
|
||||||
};
|
? (resp) => ensureResponseBody(resp).pipeThrough(new PolyfillTextDecoderStream()).pipeThrough(new TextLineStream())
|
||||||
|
: (resp) => createTextLineAsyncIterableFromStreamSource(ensureResponseBody(resp));
|
||||||
|
|
||||||
export function fetchRemoteTextByLine(url: string | URL) {
|
export function fetchRemoteTextByLine(url: string | URL) {
|
||||||
return fetchWithRetry(url, defaultRequestInit).then(createReadlineInterfaceFromResponse);
|
return fetchWithRetry(url, defaultRequestInit).then(createReadlineInterfaceFromResponse);
|
||||||
|
|||||||
@ -23,7 +23,7 @@ export const processLine = (line: string): string | null => {
|
|||||||
return trimmed;
|
return trimmed;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const processLineFromReadline = async (rl: AsyncGenerator<string> | ReadableStream<string>): Promise<string[]> => {
|
export const processLineFromReadline = async (rl: AsyncIterable<string>): Promise<string[]> => {
|
||||||
const res: string[] = [];
|
const res: string[] = [];
|
||||||
for await (const line of rl) {
|
for await (const line of rl) {
|
||||||
const l: string | null = processLine(line);
|
const l: string | null = processLine(line);
|
||||||
|
|||||||
@ -21,7 +21,7 @@ export class PolyfillTextDecoderStream extends TransformStream<Uint8Array, strin
|
|||||||
readonly ignoreBOM: boolean;
|
readonly ignoreBOM: boolean;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
encoding: Encoding = 'utf-8',
|
encoding: Bun.Encoding = 'utf-8',
|
||||||
{ fatal = false, ignoreBOM = false }: ConstructorParameters<typeof TextDecoder>[1] = {}
|
{ fatal = false, ignoreBOM = false }: ConstructorParameters<typeof TextDecoder>[1] = {}
|
||||||
) {
|
) {
|
||||||
const decoder = new TextDecoder(encoding, { fatal, ignoreBOM });
|
const decoder = new TextDecoder(encoding, { fatal, ignoreBOM });
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user