explorbot 0.5.6 → 0.5.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ import { tag } from './logger.js';
2
+
3
+ export function parseSpecPairs(raw: string | undefined): SpecPair[] {
4
+ const pairs: SpecPair[] = [];
5
+ for (const pair of (raw || '').split(';')) {
6
+ const trimmed = pair.trim();
7
+ if (!trimmed) continue;
8
+ const sepMatch = trimmed.match(/^([^:=]+)\s*[:=]\s*(.*)$/);
9
+ if (!sepMatch) {
10
+ tag('warning').log(`Ignoring malformed spec pair: ${trimmed}`);
11
+ continue;
12
+ }
13
+ pairs.push({ key: sepMatch[1].trim().toLowerCase(), value: sepMatch[2].trim() });
14
+ }
15
+ return pairs;
16
+ }
17
+
18
+ export interface SpecPair {
19
+ key: string;
20
+ value: string;
21
+ }
@@ -0,0 +1,53 @@
1
+ import { tag } from './logger.js';
2
+ import { parseSpecPairs } from './spec.ts';
3
+ import { matchesUrl } from './url-matcher.ts';
4
+
5
+ export class UrlFilter {
6
+ readonly include: string[] = [];
7
+ readonly exclude: string[] = [];
8
+ sort?: 'priority' | 'lastmod';
9
+ limit = Number.POSITIVE_INFINITY;
10
+ private admitted = new Set<string>();
11
+
12
+ constructor(spec?: string) {
13
+ for (const { key, value } of parseSpecPairs(spec)) {
14
+ if (key === 'filter') {
15
+ this.include.push(value);
16
+ continue;
17
+ }
18
+ if (key === 'exclude') {
19
+ this.exclude.push(value);
20
+ continue;
21
+ }
22
+ if (key === 'sort' && (value === 'priority' || value === 'lastmod')) {
23
+ this.sort = value;
24
+ continue;
25
+ }
26
+ if (key === 'limit' && Number.parseInt(value, 10) > 0) {
27
+ this.limit = Number.parseInt(value, 10);
28
+ continue;
29
+ }
30
+ tag('warning').log(`Ignoring URL filter pair ${key}:${value} (use filter|exclude:<url pattern>, sort:priority|lastmod, limit:<n>)`);
31
+ }
32
+ }
33
+
34
+ allows(url: string): boolean {
35
+ const path = urlPath(url);
36
+ if (this.include.length > 0 && !this.include.some((pattern) => matchesUrl(pattern, path))) return false;
37
+ return !this.exclude.some((pattern) => matchesUrl(pattern, path));
38
+ }
39
+
40
+ admit(url: string): boolean {
41
+ if (!this.allows(url)) return false;
42
+ const path = urlPath(url);
43
+ if (this.admitted.has(path)) return true;
44
+ if (this.admitted.size >= this.limit) return false;
45
+ this.admitted.add(path);
46
+ return true;
47
+ }
48
+ }
49
+
50
+ function urlPath(url: string): string {
51
+ const parsed = new URL(url, 'http://localhost');
52
+ return `${parsed.pathname}${parsed.search}`;
53
+ }