process-line.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { TransformStream } from 'node:stream/web';
  2. export function processLine(line: string): string | null {
  3. const trimmed: string = line.trim();
  4. if (trimmed.length === 0) {
  5. return null;
  6. }
  7. const line_0 = trimmed.charCodeAt(0);
  8. if (
  9. // line_0 === 32 /** [space] */
  10. // || line_0 === 13 /** \r */
  11. // || line_0 === 10 /** \n */
  12. line_0 === 33 /** ! */
  13. || (line_0 === 47 /** / */ && trimmed.charCodeAt(1) === 47 /** / */)
  14. ) {
  15. return null;
  16. }
  17. if (line_0 === 35 /** # */) {
  18. if (trimmed.charCodeAt(1) !== 35 /** # */) {
  19. // # Comment
  20. // AdGuard Rule like #@.not_ad
  21. return null;
  22. }
  23. if (trimmed.charCodeAt(2) === 35 /** # */ && trimmed.charCodeAt(3) === 35 /** # */) {
  24. // ################## EOF ##################
  25. return null;
  26. }
  27. /**
  28. * AdGuard Filter can be:
  29. *
  30. * ##.class
  31. * ##tag.class
  32. * ###id
  33. */
  34. }
  35. return trimmed;
  36. }
  37. export class ProcessLineStream extends TransformStream<string, string> {
  38. // private __buf = '';
  39. constructor() {
  40. super({
  41. transform(l, controller) {
  42. const line = processLine(l);
  43. if (line) {
  44. controller.enqueue(line);
  45. }
  46. }
  47. });
  48. }
  49. }
  50. // export class ProcessLineNodeStream extends Transform {
  51. // _transform(chunk: string, encoding: BufferEncoding, callback: TransformCallback) {
  52. // // Convert chunk to string and then to uppercase
  53. // const upperCased = chunk.toUpperCase();
  54. // // Push transformed data to readable side
  55. // this.push(upperCased);
  56. // // Call callback when done
  57. // callback();
  58. // }
  59. // }