-
-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·89 lines (74 loc) · 2.07 KB
/
cli.js
File metadata and controls
executable file
·89 lines (74 loc) · 2.07 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/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));
const debug = process.env.DEBUG === 'true' ? console.log : () => {};
function showUsage() {
console.log(`
Usage: node cli.js [--source-url FIRST_PARTY_URL] URL
Options:
--source-url URL Overwrites the source URL (needed to test third-party requests).
--help Show help.
`);
}
function parseArguments() {
const args = process.argv.slice(2);
if (args.includes('--help')) {
showUsage();
process.exit(0);
}
let sourceUrl = null;
const sourceUrlIndex = args.indexOf('--source-url');
if (sourceUrlIndex !== -1 && sourceUrlIndex + 1 < args.length) {
sourceUrl = args[sourceUrlIndex + 1];
args.splice(sourceUrlIndex, 2);
}
const url = args[0];
if (!url) {
showUsage();
console.error('ERROR: Missing argument: URL is needed');
process.exit(1);
}
return { url, sourceUrl };
}
(async () => {
const startTime = Date.now();
const { url, sourceUrl } = parseArguments();
const loadingStart = Date.now();
const engine = FiltersEngine.deserialize(
readFileSync(path.join(__dirname, 'dist', 'trackerdb.engine')),
);
const loadingEnd = Date.now();
const requestDetails = { url: url, type: 'xhr' };
if (sourceUrl) {
requestDetails.sourceUrl = sourceUrl;
}
const matches = url.startsWith('http')
? engine.getPatternMetadata(Request.fromRawDetails(requestDetails), {
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,
),
);
}
})();