|
| 1 | +import { BufferLineReader } from './reader/buffer-line-reader'; |
| 2 | +import { type IRuleList, LIST_ID_MAX_VALUE } from './rule-list'; |
| 3 | +import { RuleScanner } from './scanner/rule-scanner'; |
| 4 | +import { ScannerType } from './scanner/scanner-type'; |
| 5 | + |
| 6 | +/** |
| 7 | + * BufferRuleList represents a string-based rule list. It keeps the original |
| 8 | + * rule list as a byte array with UTF-8 encoded characters. This approach |
| 9 | + * allows saving on the memory used by tsurlfilter compared to StringRuleList. |
| 10 | + */ |
| 11 | +export class BufferRuleList implements IRuleList { |
| 12 | + /** |
| 13 | + * Rule list ID. |
| 14 | + */ |
| 15 | + private readonly id: number; |
| 16 | + |
| 17 | + /** |
| 18 | + * String with filtering rules (one per line) encoded as a |
| 19 | + * UTF-8 array. |
| 20 | + */ |
| 21 | + private readonly rulesBuffer: Uint8Array; |
| 22 | + |
| 23 | + /** |
| 24 | + * Whether to ignore cosmetic rules or not. |
| 25 | + */ |
| 26 | + private readonly ignoreCosmetic: boolean; |
| 27 | + |
| 28 | + /** |
| 29 | + * Whether to ignore javascript cosmetic rules or not. |
| 30 | + */ |
| 31 | + private readonly ignoreJS: boolean; |
| 32 | + |
| 33 | + /** |
| 34 | + * Whether to ignore unsafe rules or not. |
| 35 | + */ |
| 36 | + private readonly ignoreUnsafe: boolean; |
| 37 | + |
| 38 | + /** |
| 39 | + * Text decoder that is used to read strings from the internal buffer of |
| 40 | + * UTF-8 encoded characters. |
| 41 | + */ |
| 42 | + private static readonly decoder = new TextDecoder('utf-8'); |
| 43 | + |
| 44 | + /** |
| 45 | + * Constructor of BufferRuleList. |
| 46 | + * |
| 47 | + * @param listId - List identifier. |
| 48 | + * @param rulesText - String with filtering rules (one per line). |
| 49 | + * @param ignoreCosmetic - (Optional) True to ignore cosmetic rules. |
| 50 | + * @param ignoreJS - (Optional) True to ignore JS rules. |
| 51 | + * @param ignoreUnsafe - (Optional) True to ignore unsafe rules. |
| 52 | + */ |
| 53 | + constructor( |
| 54 | + listId: number, |
| 55 | + rulesText: string, |
| 56 | + ignoreCosmetic?: boolean, |
| 57 | + ignoreJS?: boolean, |
| 58 | + ignoreUnsafe?: boolean, |
| 59 | + ) { |
| 60 | + if (listId >= LIST_ID_MAX_VALUE) { |
| 61 | + throw new Error(`Invalid list identifier, it must be less than ${LIST_ID_MAX_VALUE}`); |
| 62 | + } |
| 63 | + |
| 64 | + this.id = listId; |
| 65 | + const encoder = new TextEncoder(); |
| 66 | + this.rulesBuffer = encoder.encode(rulesText); |
| 67 | + this.ignoreCosmetic = !!ignoreCosmetic; |
| 68 | + this.ignoreJS = !!ignoreJS; |
| 69 | + this.ignoreUnsafe = !!ignoreUnsafe; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Close does nothing as here's nothing to close in the BufferRuleList. |
| 74 | + */ |
| 75 | + // eslint-disable-next-line class-methods-use-this |
| 76 | + public close(): void { |
| 77 | + // Empty |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * @return - The rule list identifier |
| 82 | + */ |
| 83 | + getId(): number { |
| 84 | + return this.id; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Creates a new rules scanner that reads the list contents. |
| 89 | + * |
| 90 | + * @return - Scanner object. |
| 91 | + */ |
| 92 | + newScanner(scannerType: ScannerType): RuleScanner { |
| 93 | + const reader = new BufferLineReader(this.rulesBuffer); |
| 94 | + return new RuleScanner(reader, this.id, { |
| 95 | + scannerType, |
| 96 | + ignoreCosmetic: this.ignoreCosmetic, |
| 97 | + ignoreJS: this.ignoreJS, |
| 98 | + ignoreUnsafe: this.ignoreUnsafe, |
| 99 | + }); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Finds rule text by its index. |
| 104 | + * |
| 105 | + * If there's no rule by that index or the rule is invalid, it will return |
| 106 | + * null. |
| 107 | + * |
| 108 | + * @param ruleIdx - rule index. |
| 109 | + * @return - rule text or null. |
| 110 | + */ |
| 111 | + retrieveRuleText(ruleIdx: number): string | null { |
| 112 | + if (ruleIdx < 0 || ruleIdx >= this.rulesBuffer.length) { |
| 113 | + return null; |
| 114 | + } |
| 115 | + |
| 116 | + let endOfLine = this.rulesBuffer.indexOf(BufferLineReader.EOL, ruleIdx); |
| 117 | + if (endOfLine === -1) { |
| 118 | + endOfLine = this.rulesBuffer.length; |
| 119 | + } |
| 120 | + |
| 121 | + const lineBuffer = this.rulesBuffer.subarray(ruleIdx, endOfLine); |
| 122 | + const line = BufferRuleList.decoder.decode(lineBuffer).trim(); |
| 123 | + |
| 124 | + if (!line) { |
| 125 | + return null; |
| 126 | + } |
| 127 | + |
| 128 | + return line; |
| 129 | + } |
| 130 | +} |
0 commit comments