rule-matching.ts 657 B

12345678910111213141516171819202122
  1. export function wildcardToRegExp(pattern: string): RegExp {
  2. const escaped = pattern
  3. .split("*")
  4. .map((part) => part.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"))
  5. .join(".*");
  6. return new RegExp(`^${escaped}$`);
  7. }
  8. export function domainRuleSubject(url: string): string | undefined {
  9. try {
  10. const parsed = new URL(url.trim());
  11. const hostname = parsed.hostname.toLowerCase().replace(/\.$/, "");
  12. return hostname.length > 0 ? `domain:${hostname}` : undefined;
  13. } catch {
  14. return undefined;
  15. }
  16. }
  17. export function webFetchRuleSubjects(url: string): string[] {
  18. const domain = domainRuleSubject(url);
  19. return domain ? [domain] : [];
  20. }