[mv3] Code review of uBO filter converter

Related commit:
e8fb0e1cc9
This commit is contained in:
Raymond Hill 2025-06-22 17:06:35 -04:00
parent 56ecd04816
commit 8bcf533fc0
No known key found for this signature in database
GPG key ID: 25E1490B761470C2

View file

@ -93,7 +93,7 @@ function parseHostnameList(iter) {
/******************************************************************************/ /******************************************************************************/
function mergeIncludeExclude(rules) { export function mergeIncludeExclude(rules) {
const includeExcludes = [ const includeExcludes = [
{ includeName: 'requestDomains', excludeName: 'excludedRequestDomains' }, { includeName: 'requestDomains', excludeName: 'excludedRequestDomains' },
{ includeName: 'initiatorDomains', excludeName: 'excludedInitiatorDomains' }, { includeName: 'initiatorDomains', excludeName: 'excludedInitiatorDomains' },
@ -104,7 +104,7 @@ function mergeIncludeExclude(rules) {
const out = []; const out = [];
const distinctRules = new Map(); const distinctRules = new Map();
for ( const rule of rules ) { for ( const rule of rules ) {
const { condition } = rule; const { id, condition } = rule;
if ( Boolean(condition[includeName]?.length) === false ) { if ( Boolean(condition[includeName]?.length) === false ) {
if ( Boolean(condition[excludeName]?.length) === false ) { if ( Boolean(condition[excludeName]?.length) === false ) {
out.push(rule); out.push(rule);
@ -115,9 +115,10 @@ function mergeIncludeExclude(rules) {
condition[includeName] = undefined; condition[includeName] = undefined;
const excluded = condition[excludeName] || []; const excluded = condition[excludeName] || [];
condition[excludeName] = undefined; condition[excludeName] = undefined;
rule.id = undefined;
const hash = JSON.stringify(rule); const hash = JSON.stringify(rule);
const details = distinctRules.get(hash) || const details = distinctRules.get(hash) ||
{ included: new Set(), excluded: new Set() }; { id, included: new Set(), excluded: new Set() };
if ( details.included.size === 0 && details.excluded.size === 0 ) { if ( details.included.size === 0 && details.excluded.size === 0 ) {
distinctRules.set(hash, details); distinctRules.set(hash, details);
} }
@ -129,13 +130,16 @@ function mergeIncludeExclude(rules) {
details.excluded.add(hn); details.excluded.add(hn);
} }
} }
for ( const [ hash, details ] of distinctRules ) { for ( const [ hash, { id, included, excluded } ] of distinctRules ) {
const rule = JSON.parse(hash); const rule = JSON.parse(hash);
if ( details.included.size !== 0 ) { if ( id ) {
rule.condition[includeName] = Array.from(details.included); rule.id = id;
} }
if ( details.excluded.size !== 0 ) { if ( included.size !== 0 ) {
rule.condition[excludeName] = Array.from(details.excluded); rule.condition[includeName] = Array.from(included);
}
if ( excluded.size !== 0 ) {
rule.condition[excludeName] = Array.from(excluded);
} }
out.push(rule); out.push(rule);
} }
@ -161,7 +165,7 @@ function parseNetworkFilter(parser) {
let pattern = parser.getNetPattern(); let pattern = parser.getNetPattern();
if ( parser.isHostnamePattern() ) { if ( parser.isHostnamePattern() ) {
rule.condition.requestDomains = [ pattern ]; rule.condition.requestDomains = [ pattern ];
} else if ( parser.isGenericPattern() ) { } else if ( parser.isPlainPattern() || parser.isGenericPattern() ) {
if ( parser.isLeftHnAnchored() ) { if ( parser.isLeftHnAnchored() ) {
pattern = `||${pattern}`; pattern = `||${pattern}`;
} else if ( parser.isLeftAnchored() ) { } else if ( parser.isLeftAnchored() ) {
@ -203,7 +207,7 @@ function parseNetworkFilter(parser) {
for ( const type of parser.getNodeTypes() ) { for ( const type of parser.getNodeTypes() ) {
switch ( type ) { switch ( type ) {
case sfp.NODE_TYPE_NET_OPTION_NAME_1P: case sfp.NODE_TYPE_NET_OPTION_NAME_1P:
rule.domainType = parser.isNegatedOption(type) rule.condition.domainType = parser.isNegatedOption(type)
? 'thirdParty' ? 'thirdParty'
: 'firstParty'; : 'firstParty';
break; break;