This repository was archived by the owner on May 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathfiltering.js
58 lines (52 loc) · 1.54 KB
/
filtering.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
57
58
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
const I = (x) => x
/**
* Same as filterPredicate but will log if there is a LOG_OUTPUT env variable
*/
const filterPredicateWithPossibleLogging = (rule, filterPredicate = I) => {
const result = filterPredicate(rule)
if (process.env['LOG_OUTPUT'] && !result) {
console.log('Filtering out rule: ', rule)
}
return result
}
/**
* Mapping rule which reformats rules
*/
const mapRule = (rule) => {
if (rule === '||imasdk.googleapis.com^$third-party') {
return '||imasdk.googleapis.com^$third-party,explicitcancel'
}
if (rule === '/ima3.js') {
return '/ima3.js$explicitcancel'
}
return rule
}
/**
* Given a list of inputs returns a filtered list of rules that should be used.
*
* @param input {string} - ABP filter syntax to filter
* @return A better filter list
*/
const sanitizeABPInput = (input, filterPredicate = I) =>
input.split('\n')
.filter((rule) =>
filterPredicateWithPossibleLogging(rule, filterPredicate))
.map(mapRule)
// TODO: This can be removed a few versions after 0.62.x.
// It was just added to be backwards compatible with 0.61.x.
.concat(extraConcatRules)
.join('\n')
/**
* Extra rules that should be appended when sanitizing ABP input
*/
const extraConcatRules = [
'||imasdk.googleapis.com/$third-party',
'^ima3.js'
]
module.exports = {
sanitizeABPInput,
extraConcatRules
}