File indexing completed on 2024-11-24 04:54:31

0001 /* This Source Code Form is subject to the terms of the Mozilla Public
0002  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
0003  * You can obtain one at http://mozilla.org/MPL/2.0/. */
0004 
0005 const I = (x) => x
0006 
0007 /**
0008  * Same as filterPredicate but will log if there is a LOG_OUTPUT env variable
0009  */
0010 const filterPredicateWithPossibleLogging = (rule, filterPredicate = I) => {
0011   const result = filterPredicate(rule)
0012   if (process.env['LOG_OUTPUT'] && !result) {
0013     console.log('Filtering out rule: ', rule)
0014   }
0015   return result
0016 }
0017 
0018 /**
0019  * Mapping rule which reformats rules
0020  */
0021 const mapRule = (rule) => {
0022   if (rule === '||imasdk.googleapis.com^$third-party') {
0023     return '||imasdk.googleapis.com^$third-party,explicitcancel'
0024   }
0025   if (rule === '/ima3.js') {
0026     return '/ima3.js$explicitcancel'
0027   }
0028   return rule
0029 }
0030 
0031 /**
0032  * Given a list of inputs returns a filtered list of rules that should be used.
0033  *
0034  * @param input {string} - ABP filter syntax to filter
0035  * @return A better filter list
0036  */
0037 const sanitizeABPInput = (input, filterPredicate = I) =>
0038   input.split('\n')
0039     .filter((rule) =>
0040       filterPredicateWithPossibleLogging(rule, filterPredicate))
0041     .map(mapRule)
0042     // TODO: This can be removed a few versions after 0.62.x.
0043     // It was just added to be backwards compatible with 0.61.x.
0044     .concat(extraConcatRules)
0045     .join('\n')
0046 
0047 /**
0048  * Extra rules that should be appended when sanitizing ABP input
0049  */
0050 const extraConcatRules = [
0051   '||imasdk.googleapis.com/$third-party',
0052   '^ima3.js'
0053 ]
0054 
0055 module.exports = {
0056   sanitizeABPInput,
0057   extraConcatRules
0058 }