(url: string)
| 18 | } |
| 19 | |
| 20 | public urlMatch(url: string): T[] { |
| 21 | const cacheMap = this.cacheMap; |
| 22 | if (cacheMap.has(url)) { |
| 23 | const cached = cacheMap.get(url) as T[]; |
| 24 | cacheMap.delete(url); |
| 25 | cacheMap.set(url, cached); |
| 26 | return cached; |
| 27 | } |
| 28 | const res: T[] = []; |
| 29 | for (const [uuid, rules] of this.rulesMap) { |
| 30 | try { |
| 31 | if (isUrlIncluded(url, rules)) { |
| 32 | res.push(uuid); |
| 33 | } |
| 34 | } catch (e) { |
| 35 | console.warn("Unexpected match error", e); |
| 36 | } |
| 37 | } |
| 38 | const sorter = this.sorter; |
| 39 | if (sorter !== null && typeof sorter === "object" && typeof res[0] === "string") { |
| 40 | (res as string[]).sort((a, b) => { |
| 41 | const p = sorter[a]; |
| 42 | const q = sorter[b]; |
| 43 | if (p! > -1 && q! > -1) { |
| 44 | return p! - q!; |
| 45 | } |
| 46 | return a.localeCompare(b); |
| 47 | }); |
| 48 | } |
| 49 | cacheMap.set(url, res); |
| 50 | if (cacheMap.size > this.maxCacheEntries) { |
| 51 | const oldest = cacheMap.keys().next().value; |
| 52 | if (oldest !== undefined) cacheMap.delete(oldest); |
| 53 | } |
| 54 | return res; |
| 55 | } |
| 56 | |
| 57 | public clearRules(uuid: T) { |
| 58 | this.cacheMap.clear(); |
no test coverage detected