Chore: build infra changes

This commit is contained in:
SukkaW
2023-11-23 22:09:01 +08:00
parent 265df07893
commit 101913e4f1
26 changed files with 187 additions and 191 deletions

5
Build/lib/constants.ts Normal file
View File

@@ -0,0 +1,5 @@
export const SHARED_DESCRIPTION = [
'License: AGPL 3.0',
'Homepage: https://ruleset.skk.moe',
'GitHub: https://github.com/SukkaW/Surge',
] as const;

View File

@@ -10,7 +10,7 @@ export function readFileByLine(file: string | BunFile) {
return file.stream().pipeThrough(new PolyfillTextDecoderStream()).pipeThrough(new TextLineStream());
}
export async function createReadlineInterfaceFromResponse(resp: Response) {
export function createReadlineInterfaceFromResponse(resp: Response) {
if (!resp.body) {
throw new Error('Failed to fetch remote text');
}

View File

@@ -4,7 +4,7 @@ import { traceAsync } from './trace-runner';
import { defaultRequestInit, fetchWithRetry } from './fetch-retry';
import type { PublicSuffixList } from 'gorhill-publicsuffixlist';
const publicSuffixPath = path.resolve(__dirname, '../../node_modules/.cache/public_suffix_list_dat.txt');
const publicSuffixPath = path.resolve(import.meta.dir, '../../node_modules/.cache/public_suffix_list_dat.txt');
const getGorhillPublicSuffix = () => traceAsync('create gorhill public suffix instance', async () => {
const customFetch = async (url: string | URL) => Bun.file(url);

View File

@@ -23,7 +23,7 @@ export const processLine = (line: string): string | null => {
return trimmed;
};
export const processLineFromReadline = async (rl: AsyncGenerator<string>): Promise<string[]> => {
export const processLineFromReadline = async (rl: AsyncGenerator<string> | ReadableStream<string>): Promise<string[]> => {
const res: string[] = [];
for await (const line of rl) {
const l: string | null = processLine(line);

View File

@@ -13,7 +13,6 @@
// limitations under the License.
// Polyfill for TextEncoderStream and TextDecoderStream
// Modified by Sukka (https://skk.moe) to increase compatibility and performance with Bun.
export class PolyfillTextDecoderStream extends TransformStream<Uint8Array, string> {
@@ -23,10 +22,7 @@ export class PolyfillTextDecoderStream extends TransformStream<Uint8Array, strin
constructor(
encoding: 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 });
super({

View File

@@ -19,57 +19,55 @@ interface TextLineStreamOptions {
* ```
*/
export class TextLineStream extends TransformStream<string, string> {
private __allowCR: boolean;
private __buf = '';
constructor(options?: TextLineStreamOptions) {
const allowCR = options?.allowCR ?? false;
super({
transform: (chunk, controller) => this.handle(chunk, controller),
transform: (chunk, controller) => {
chunk = this.__buf + chunk;
for (; ;) {
const lfIndex = chunk.indexOf('\n');
if (allowCR) {
const crIndex = chunk.indexOf('\r');
if (
crIndex !== -1 && crIndex !== (chunk.length - 1) &&
(lfIndex === -1 || (lfIndex - 1) > crIndex)
) {
controller.enqueue(chunk.slice(0, crIndex));
chunk = chunk.slice(crIndex + 1);
continue;
}
}
if (lfIndex !== -1) {
let crOrLfIndex = lfIndex;
if (chunk[lfIndex - 1] === '\r') {
crOrLfIndex--;
}
controller.enqueue(chunk.slice(0, crOrLfIndex));
chunk = chunk.slice(lfIndex + 1);
continue;
}
break;
}
this.__buf = chunk;
},
flush: (controller) => {
if (this.__buf.length > 0) {
if (
this.__allowCR &&
this.__buf[this.__buf.length - 1] === '\r'
) controller.enqueue(this.__buf.slice(0, -1));
else controller.enqueue(this.__buf);
if (allowCR && this.__buf[this.__buf.length - 1] === '\r') {
controller.enqueue(this.__buf.slice(0, -1));
} else {
controller.enqueue(this.__buf);
};
}
},
});
this.__allowCR = options?.allowCR ?? false;
}
private handle(chunk: string, controller: TransformStreamDefaultController<string>) {
chunk = this.__buf + chunk;
for (;;) {
const lfIndex = chunk.indexOf('\n');
if (this.__allowCR) {
const crIndex = chunk.indexOf('\r');
if (
crIndex !== -1 && crIndex !== (chunk.length - 1) &&
(lfIndex === -1 || (lfIndex - 1) > crIndex)
) {
controller.enqueue(chunk.slice(0, crIndex));
chunk = chunk.slice(crIndex + 1);
continue;
}
}
if (lfIndex !== -1) {
let crOrLfIndex = lfIndex;
if (chunk[lfIndex - 1] === '\r') {
crOrLfIndex--;
}
controller.enqueue(chunk.slice(0, crOrLfIndex));
chunk = chunk.slice(lfIndex + 1);
continue;
}
break;
}
this.__buf = chunk;
}
}

View File

@@ -24,8 +24,8 @@ export interface TaskResult {
readonly taskName: string;
}
const task = <T>(__filename: string, fn: () => Promise<T>, customname: string | null = null) => {
const taskName = customname ?? path.basename(__filename, path.extname(__filename));
const task = <T>(importMetaPath: string, fn: () => Promise<T>, customname: string | null = null) => {
const taskName = customname ?? path.basename(importMetaPath, path.extname(importMetaPath));
return async () => {
console.log(`🏃 [${taskName}] Start executing`);
const start = performance.now();