process-line.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. const otherPoundSign = trimmed.indexOf('#');
  36. if (otherPoundSign > 0) {
  37. return trimmed.slice(0, otherPoundSign).trimEnd();
  38. }
  39. return trimmed;
  40. }
  41. export class ProcessLineStream extends TransformStream<string, string> {
  42. // private __buf = '';
  43. constructor() {
  44. super({
  45. transform(l, controller) {
  46. const line = processLine(l);
  47. if (line) {
  48. controller.enqueue(line);
  49. }
  50. }
  51. });
  52. }
  53. }
  54. // export class ProcessLineNodeStream extends Transform {
  55. // _transform(chunk: string, encoding: BufferEncoding, callback: TransformCallback) {
  56. // // Convert chunk to string and then to uppercase
  57. // const upperCased = chunk.toUpperCase();
  58. // // Push transformed data to readable side
  59. // this.push(upperCased);
  60. // // Call callback when done
  61. // callback();
  62. // }
  63. // }