-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.js
35 lines (28 loc) · 831 Bytes
/
lib.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
const es = require('event-stream');
function transformDirective(directive = '') {
return directive.replace(/"([\w-]+).*/i, '$1');
}
function domainFilter(uri = '') {
return uri.match(/(?!(w+)\.)\w*(?:\w+\.)+\w+/gm);
}
module.exports = {
transformDirective,
domainFilter,
transformCspChunk: es.mapSync((chunk = {}) => {
const match = domainFilter(chunk.blocked_uri);
if (!match || !chunk['violated-directive']) {
return;
}
return {
'directive': transformDirective(chunk['violated-directive']),
blocked_uri: match[0]
};
}),
getWildcard(domain = '') {
const splitted = domain.split('.');
if (splitted.length > 2) {
splitted[0] = '*';
}
return splitted.join('.');
}
};