Skip to content

Commit

Permalink
Normalise resource names (#16)
Browse files Browse the repository at this point in the history
Co-authored-by: chrmod <[email protected]>
Co-authored-by: Philipp Claßen <[email protected]>
  • Loading branch information
3 people authored Aug 7, 2024
1 parent d1a430f commit 4067287
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
node_modules
dist

test-results
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
bun 1.0.1
bun 1.1.17
Binary file modified bun.lockb
Binary file not shown.
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
"test.e2e": "playwright test test/e2e/index.spec.js"
},
"dependencies": {
"@adguard/tsurlfilter": "2.2.15",
"@adguard/scriptlets": "^1.11.16",
"@adguard/tsurlfilter": "2.2.23",
"@eyeo/webext-ad-filtering-solution": "1.5.0"
},
"devDependencies": {
"@ghostery/trackerdb": "^1.0.36",
"@playwright/test": "^1.38.0"
"@ghostery/trackerdb": "^1.0.208",
"@playwright/test": "^1.45.3"
}
}
66 changes: 65 additions & 1 deletion src/converters/helpers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,57 @@
import * as path from 'node:path'
import redirects from '@adguard/scriptlets/dist/redirects.json' with { type: 'json' }

export function generateResourcesMapping() {
const resourcesMapping = new Map();
const allowedResourceExtensions = [
'html',
'js',
'css',
'mp4',
'mp3',
'xml',
'txt',
'json',
'empty',
];

function getPreferredResource(aliases) {
// ignore non-supported files and manually created uBO aliases by AdGuard
return aliases.find(alias => {
const extension = alias.split('.').pop();
return allowedResourceExtensions.includes(extension) && !alias.startsWith('ubo-');
});
}

for (const redirect of redirects) {
// Skip, in case of AdGuard-only resource
if (redirect.aliases === undefined) {
continue;
}

const preferredResourceName = getPreferredResource(redirect.aliases);

// Skip, in case of safe redirect resource name that's safe to use wasn't found
if (preferredResourceName === undefined) {
continue;
}

// Register to mapping
resourcesMapping.set(redirect.title, preferredResourceName);
for (const alias of redirect.aliases) {
if (alias !== preferredResourceName) {
resourcesMapping.set(alias, preferredResourceName);
}
}
}

return resourcesMapping;
}

export const DEFAULT_PARAM_MAPPING = {
'3p': 'third-party',
'xhr': 'xmlhttprequest',
'frame': 'subdocument'
};

export function normalizeFilter(filter, { mapping = DEFAULT_PARAM_MAPPING } = {}) {
Expand Down Expand Up @@ -30,7 +82,9 @@ export function normalizeFilter(filter, { mapping = DEFAULT_PARAM_MAPPING } = {}
return `${front}$${params.join(',')}`;
}

export function normalizeRule(rule) {
export const DEFAULT_RESOURCE_MAPPING = generateResourcesMapping();

export function normalizeRule(rule, { resourcesMapping = DEFAULT_RESOURCE_MAPPING } = {}) {
if (!rule) {
return;
}
Expand Down Expand Up @@ -67,5 +121,15 @@ export function normalizeRule(rule) {
delete newRule.condition.domains;
}

if (newRule.action && newRule.action.type === 'redirect') {
const filename = path.basename(newRule.action.redirect.extensionPath);
const preferredFilename = resourcesMapping.get(filename);

if (preferredFilename !== undefined) {
newRule.action.redirect.extensionPath =
path.dirname(newRule.action.redirect.extensionPath) + '/' + preferredFilename;
}
}

return newRule;
}
37 changes: 36 additions & 1 deletion test/unit/converters/helpers.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, it, expect } from "bun:test";

import { normalizeFilter, normalizeRule } from "../../../src/converters/helpers.js";
import { generateResourcesMapping, normalizeFilter, normalizeRule } from "../../../src/converters/helpers.js";

describe("normalizeFilter", () => {
it("format params", () => {
Expand Down Expand Up @@ -121,4 +121,39 @@ describe('normalizeRule', () => {
},
});
});

it('replaces extensionPath respecting existing dirname', () => {
expect(normalizeRule({
action: {
type: 'redirect',
redirect: {
extensionPath: '/rule_resources/redirects/alias',
},
},
}, {
resourcesMapping: new Map([
[
'alias',
'test.js',
],
]),
})).toEqual({
action: {
type: 'redirect',
redirect: {
extensionPath: '/rule_resources/redirects/test.js',
},
},
});
});
});

describe('generateResourcesMapping', () => {
it('filters resources without file extension', () => {
const mapping = generateResourcesMapping();

for (const destination of mapping.values()) {
expect(destination.match(/\w+\.\w+|empty/)).not.toBe(null);
}
});
});
3 changes: 3 additions & 0 deletions test/unit/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ function normalize(rule) {
if (!rule) {
return undefined;
}
if (rule.condition?.resourceTypes !== undefined) {
rule.condition.resourceTypes = rule.condition.resourceTypes.sort();
}
delete rule.priority;
delete rule.id;
return rule;
Expand Down
8 changes: 8 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
// https://bun.sh/docs/runtime/modules#path-re-mapping
"paths": {
"@adguard/scriptlets/dist/redirects.json": ["./node_modules/@adguard/scriptlets/dist/redirects.json"]
}
}
}

0 comments on commit 4067287

Please sign in to comment.