Skip to content

Commit

Permalink
Upgrade to latest abp2dnr (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
seia-soto authored Jan 4, 2024
1 parent 6975f98 commit 9d5276e
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 11 deletions.
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
"dependencies": {
"@adguard/tsurlfilter": "^2.1.11",
"@ghostery/abp2dnr": "ghostery/abp2dnr#9e52b185e03d2d24170daaa5de0832e473d4b09f"
"@eyeo/webext-ad-filtering-solution": "^1.2.0"
},
"devDependencies": {
"@ghostery/trackerdb": "^1.0.36",
Expand Down
21 changes: 13 additions & 8 deletions src/converters/abp.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import { convertFilter } from "@ghostery/abp2dnr";
import { Filter } from "adblockpluscore/lib/filterClasses";
import { FilterParsingError, normalize } from "@eyeo/webext-ad-filtering-solution/adblockpluscore/lib/filters/index.js";
import { createConverter } from "@eyeo/webext-ad-filtering-solution/adblockpluscore/lib/dnr/index.js";
import { normalizeFilter, normalizeRule } from "./helpers";

export default async function convert(filters) {
const converter = createConverter({ isRegexSupported: () => true });
const rules = [];
const errors = [];
let nextId = 1;
for (const filter of filters) {
try {
const normalizedFilter = normalizeFilter(Filter.normalize(filter));
const abpFilter = Filter.fromText(normalizedFilter);
const dnrRules = await convertFilter(abpFilter, () => ({
isSupported: true,
}));
const normalizedFilter = normalizeFilter(normalize(filter));
const dnrRules = converter(normalizedFilter);
if (dnrRules instanceof FilterParsingError) {
throw dnrRules;
}
if (dnrRules.length > 0) {
rules.push(...dnrRules);
for (const rule of dnrRules) {
rule.id = nextId++;
rules.push(rule);
}
} else {
throw new Error("Unknown problem");
}
Expand Down
12 changes: 12 additions & 0 deletions src/converters/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,17 @@ export function normalizeRule(rule) {
) {
newRule.condition.regexFilter = `/${newRule.condition.regexFilter}/`;
}

if (newRule.condition && newRule.condition.excludedDomains) {
newRule.condition.excludedInitiatorDomains =
newRule.condition.excludedDomains;
delete newRule.condition.excludedDomains;
}

if (newRule.condition && newRule.condition.domains) {
newRule.condition.initiatorDomains = newRule.condition.domains;
delete newRule.condition.domains;
}

return newRule;
}
1 change: 1 addition & 0 deletions test/e2e/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ test.describe("converts rules with postMessage", () => {
errors: [],
rules: [
{
id: 1,
priority: 1000,
condition: {
isUrlFilterCaseSensitive: false,
Expand Down
3 changes: 2 additions & 1 deletion test/unit/converters/abp.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ describe('abp converter', () => {
isUrlFilterCaseSensitive: false,
urlFilter: "tinypass.com"
},
priority: 2000
priority: 2000,
id: 1
});
});

Expand Down
25 changes: 24 additions & 1 deletion test/unit/converters/helpers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ describe('normalizeRule', () => {
})
})


it('wraps regex rules in //', () => {
expect(normalizeRule({
condition: {
Expand All @@ -98,4 +97,28 @@ describe('normalizeRule', () => {
},
});
});

it('replaces domains with initiatorDomains', () => {
expect(normalizeRule({
condition: {
domains: ['test'],
},
})).toEqual({
condition: {
initiatorDomains: ['test'],
},
});
});

it('replaces excludedDomains with excludedInitiatorDomains', () => {
expect(normalizeRule({
condition: {
excludedDomains: ['test'],
},
})).toEqual({
condition: {
excludedInitiatorDomains: ['test'],
},
});
});
});

0 comments on commit 9d5276e

Please sign in to comment.