mirror of
https://github.com/SukkaW/Surge.git
synced 2026-01-29 01:51:52 +08:00
Chore: process line stream
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import { TransformStream } from 'node:stream/web';
|
||||
|
||||
export function processLine(line: string): string | null {
|
||||
if (!line) {
|
||||
return null;
|
||||
@@ -11,8 +13,7 @@ export function processLine(line: string): string | null {
|
||||
const line_0: string = trimmed[0];
|
||||
|
||||
if (
|
||||
line_0 === '#'
|
||||
|| line_0 === ' '
|
||||
line_0 === ' '
|
||||
|| line_0 === '\r'
|
||||
|| line_0 === '\n'
|
||||
|| line_0 === '!'
|
||||
@@ -21,16 +22,48 @@ export function processLine(line: string): string | null {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (line_0 === '#') {
|
||||
if (trimmed[1] !== '#') {
|
||||
// # Comment
|
||||
return null;
|
||||
}
|
||||
if (trimmed[2] === '#' && trimmed[3] === '#') {
|
||||
// ################## EOF ##################
|
||||
return null;
|
||||
}
|
||||
/**
|
||||
* AdGuard Filter can be:
|
||||
*
|
||||
* ##.class
|
||||
* ##tag.class
|
||||
* ###id
|
||||
*/
|
||||
}
|
||||
|
||||
return trimmed;
|
||||
}
|
||||
|
||||
export async function processLineFromReadline(rl: AsyncIterable<string>): Promise<string[]> {
|
||||
const res: string[] = [];
|
||||
for await (const line of rl) {
|
||||
const l: string | null = processLine(line);
|
||||
if (l) {
|
||||
res.push(l);
|
||||
}
|
||||
export class ProcessLineStream extends TransformStream<string, string> {
|
||||
// private __buf = '';
|
||||
constructor() {
|
||||
super({
|
||||
transform(l, controller) {
|
||||
const line = processLine(l);
|
||||
if (line) {
|
||||
controller.enqueue(line);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
// export class ProcessLineNodeStream extends Transform {
|
||||
// _transform(chunk: string, encoding: BufferEncoding, callback: TransformCallback) {
|
||||
// // Convert chunk to string and then to uppercase
|
||||
// const upperCased = chunk.toUpperCase();
|
||||
// // Push transformed data to readable side
|
||||
// this.push(upperCased);
|
||||
// // Call callback when done
|
||||
// callback();
|
||||
// }
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user