process-line.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. return null;
  21. }
  22. if (trimmed.charCodeAt(2) === 35 /** # */ && trimmed.charCodeAt(3) === 35) {
  23. // ################## EOF ##################
  24. return null;
  25. }
  26. /**
  27. * AdGuard Filter can be:
  28. *
  29. * ##.class
  30. * ##tag.class
  31. * ###id
  32. */
  33. }
  34. return trimmed;
  35. }
  36. export class ProcessLineStream extends TransformStream<string, string> {
  37. // private __buf = '';
  38. constructor() {
  39. super({
  40. transform(l, controller) {
  41. const line = processLine(l);
  42. if (line) {
  43. controller.enqueue(line);
  44. }
  45. }
  46. });
  47. }
  48. }
  49. // export class ProcessLineNodeStream extends Transform {
  50. // _transform(chunk: string, encoding: BufferEncoding, callback: TransformCallback) {
  51. // // Convert chunk to string and then to uppercase
  52. // const upperCased = chunk.toUpperCase();
  53. // // Push transformed data to readable side
  54. // this.push(upperCased);
  55. // // Call callback when done
  56. // callback();
  57. // }
  58. // }