-
Notifications
You must be signed in to change notification settings - Fork 25
/
cli.js
executable file
·56 lines (47 loc) · 1.36 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env node
import { readFileSync } from 'node:fs';
import path from 'node:path';
import * as url from 'url';
import { FiltersEngine, Request } from '@ghostery/adblocker';
const __dirname = url.fileURLToPath(new URL('.', import.meta.url));
// eslint-disable-next-line @typescript-eslint/no-empty-function
const debug = process.env.DEBUG === 'true' ? console.log : () => {};
(async () => {
const startTime = Date.now();
const [, , url] = process.argv;
if (!url) {
console.error('trackerdb takes URL as first argument');
process.exit(1);
}
const loadingStart = Date.now();
const engine = FiltersEngine.deserialize(
readFileSync(path.join(__dirname, 'dist', 'trackerdb.engine')),
);
const loadingEnd = Date.now();
const matches = url.startsWith('http')
? engine.getPatternMetadata(Request.fromRawDetails({ url, type: 'xhr' }), {
getDomainMetadata: true,
})
: engine.metadata.fromDomain(url);
const matchingEnd = Date.now();
debug('Timing:', {
init: loadingStart - startTime,
loading: loadingEnd - loadingStart,
matching: matchingEnd - loadingEnd,
total: matchingEnd - startTime,
});
if (matches.length === 0) {
console.log('No matches found');
} else {
console.log(
JSON.stringify(
{
url,
matches,
},
null,
2,
),
);
}
})();