├── .npmignore ├── _qunit_tests_ ├── resources │ ├── rules.txt │ ├── subdir │ │ ├── sub_rules.txt │ │ └── sub_rules_simple_include.txt │ ├── rules_simple_include.txt │ ├── rules_nested_subdir_includes.txt │ └── rules_nested_includes.txt ├── testrunner.ts └── filters-downloader.test.ts ├── __tests__ ├── fixtures │ ├── 02_includes │ │ ├── filter_2.txt │ │ ├── patches │ │ │ ├── v1.0.1-s-1702460925-5.patch │ │ │ ├── v1.0.0-472234-1.patch │ │ │ └── v1.0.0-s-1702460891-5.patch │ │ ├── filter_v1.0.0.txt │ │ ├── filter_v1.0.1.txt │ │ └── filter.txt │ ├── 03_no_patches │ │ ├── filter_2.txt │ │ ├── filter.txt │ │ └── filter_v1.0.0.txt │ ├── 01_simple │ │ ├── patches │ │ │ ├── v1.0.0-472234-1.patch │ │ │ └── v1.0.1-472235-1.patch │ │ ├── filter.txt │ │ ├── filter_v1.0.0.txt │ │ └── filter_v1.0.1.txt │ ├── 04_no_diff_path_for_included │ │ ├── filter.txt │ │ ├── filter_v1.0.0.txt │ │ └── filter_2.txt │ └── checksum │ │ ├── not_valid_1.txt │ │ └── valid_1.txt ├── checksum.test.ts ├── server │ └── index.ts └── filters-downloader.test.ts ├── .husky └── pre-commit ├── .gitignore ├── bamboo-specs ├── bamboo.yaml ├── permissions.yaml ├── deploy.yaml ├── increment.yaml ├── tests.yaml └── build.yaml ├── jest.config.ts ├── tsconfig.json ├── tsconfig.build.json ├── scripts └── build-txt.ts ├── src ├── index.ts ├── index.browser.ts ├── common │ └── content-type.ts ├── helpers │ └── logger.ts ├── node │ └── file-download-wrapper.ts ├── checksum.ts ├── browser │ └── file-download-wrapper.ts └── filters-downloader-creator.ts ├── rollup.config.ts ├── .github └── workflows │ ├── test.yaml │ └── release.yaml ├── .eslintrc.js ├── package.json ├── README.md ├── CHANGELOG.md └── LICENSE /.npmignore: -------------------------------------------------------------------------------- 1 | __tests__/ 2 | .idea 3 | -------------------------------------------------------------------------------- /_qunit_tests_/resources/rules.txt: -------------------------------------------------------------------------------- 1 | test 2 | -------------------------------------------------------------------------------- /_qunit_tests_/resources/subdir/sub_rules.txt: -------------------------------------------------------------------------------- 1 | sub_test 2 | -------------------------------------------------------------------------------- /__tests__/fixtures/02_includes/filter_2.txt: -------------------------------------------------------------------------------- 1 | ||included.com^ 2 | -------------------------------------------------------------------------------- /__tests__/fixtures/02_includes/patches/v1.0.1-s-1702460925-5.patch: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /__tests__/fixtures/03_no_patches/filter_2.txt: -------------------------------------------------------------------------------- 1 | ||included.com^ 2 | -------------------------------------------------------------------------------- /_qunit_tests_/resources/rules_simple_include.txt: -------------------------------------------------------------------------------- 1 | test_main 2 | !#include rules.txt 3 | -------------------------------------------------------------------------------- /.husky/pre-commit: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | . "$(dirname -- "$0")/_/husky.sh" 3 | 4 | pnpm lint-staged 5 | -------------------------------------------------------------------------------- /_qunit_tests_/resources/subdir/sub_rules_simple_include.txt: -------------------------------------------------------------------------------- 1 | sub_test_main 2 | !#include sub_rules.txt 3 | -------------------------------------------------------------------------------- /_qunit_tests_/resources/rules_nested_subdir_includes.txt: -------------------------------------------------------------------------------- 1 | test_parent 2 | !#include subdir/sub_rules_simple_include.txt 3 | -------------------------------------------------------------------------------- /_qunit_tests_/resources/rules_nested_includes.txt: -------------------------------------------------------------------------------- 1 | test_parent 2 | !#include rules_simple_include.txt 3 | !#include rules_simple_include.txt 4 | -------------------------------------------------------------------------------- /__tests__/fixtures/03_no_patches/filter.txt: -------------------------------------------------------------------------------- 1 | ! Title: Simple Example List 2 | ! Version: v1.0.2 3 | ||example.net^ 4 | !#include filter_2.txt 5 | -------------------------------------------------------------------------------- /__tests__/fixtures/03_no_patches/filter_v1.0.0.txt: -------------------------------------------------------------------------------- 1 | ! Title: Simple Example List 2 | ! Version: v1.0.0 3 | ||example.org^ 4 | !#include filter_2.txt 5 | -------------------------------------------------------------------------------- /__tests__/fixtures/01_simple/patches/v1.0.0-472234-1.patch: -------------------------------------------------------------------------------- 1 | d2 3 2 | a4 3 3 | ! Version: v1.0.1 4 | ! Diff-Path: patches/v1.0.1-472235-1.patch 5 | ||example.com^ -------------------------------------------------------------------------------- /__tests__/fixtures/04_no_diff_path_for_included/filter.txt: -------------------------------------------------------------------------------- 1 | ! Title: Simple Example List 2 | ! Version: v1.0.2 3 | ||example.net^ 4 | !#include filter_2.txt 5 | -------------------------------------------------------------------------------- /__tests__/fixtures/01_simple/patches/v1.0.1-472235-1.patch: -------------------------------------------------------------------------------- 1 | d2 3 2 | a4 3 3 | ! Version: v1.0.2 4 | ! Diff-Path: patches/v1.0.2-472236-1.patch 5 | ||example.net^ 6 | -------------------------------------------------------------------------------- /__tests__/fixtures/02_includes/patches/v1.0.0-472234-1.patch: -------------------------------------------------------------------------------- 1 | d2 3 2 | a4 3 3 | ! Version: v1.0.1 4 | ! Diff-Path: patches/v1.0.0-s-1702460891-5.patch 5 | ||example.com^ -------------------------------------------------------------------------------- /__tests__/fixtures/04_no_diff_path_for_included/filter_v1.0.0.txt: -------------------------------------------------------------------------------- 1 | ! Title: Simple Example List 2 | ! Version: v1.0.0 3 | ||example.org^ 4 | !#include filter_2.txt 5 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | .vscode/ 3 | node_modules/ 4 | *.iml 5 | build/ 6 | dist/ 7 | .DS_Store 8 | .pnpm-store/ 9 | .eslintcache 10 | 11 | .npmrc 12 | *.tgz 13 | -------------------------------------------------------------------------------- /__tests__/fixtures/01_simple/filter.txt: -------------------------------------------------------------------------------- 1 | ! Title: Diff Updates Simple Example List 2 | ! Version: v1.0.2 3 | ! Diff-Path: patches/v1.0.2-472236-1.patch 4 | ||example.net^ 5 | -------------------------------------------------------------------------------- /__tests__/fixtures/01_simple/filter_v1.0.0.txt: -------------------------------------------------------------------------------- 1 | ! Title: Diff Updates Simple Example List 2 | ! Version: v1.0.0 3 | ! Diff-Path: patches/v1.0.0-472234-1.patch 4 | ||example.org^ -------------------------------------------------------------------------------- /__tests__/fixtures/01_simple/filter_v1.0.1.txt: -------------------------------------------------------------------------------- 1 | ! Title: Diff Updates Simple Example List 2 | ! Version: v1.0.1 3 | ! Diff-Path: patches/v1.0.1-472235-1.patch 4 | ||example.com^ -------------------------------------------------------------------------------- /__tests__/fixtures/02_includes/filter_v1.0.0.txt: -------------------------------------------------------------------------------- 1 | ! Title: Diff Updates Simple Example List 2 | ! Version: v1.0.0 3 | ! Diff-Path: patches/v1.0.0-472234-1.patch 4 | ||example.org^ 5 | -------------------------------------------------------------------------------- /__tests__/fixtures/04_no_diff_path_for_included/filter_2.txt: -------------------------------------------------------------------------------- 1 | ! Diff-Path: patches/v1-s-1703001771-5.patch 2 | ! Title: Simple Filter 2 3 | ! Version: v1.0.0 4 | ||included2.com^ 5 | -------------------------------------------------------------------------------- /__tests__/fixtures/02_includes/filter_v1.0.1.txt: -------------------------------------------------------------------------------- 1 | ! Title: Diff Updates Simple Example List 2 | ! Version: v1.0.1 3 | ! Diff-Path: patches/v1.0.0-s-1702460891-5.patch 4 | ||example.com^ 5 | -------------------------------------------------------------------------------- /__tests__/fixtures/02_includes/patches/v1.0.0-s-1702460891-5.patch: -------------------------------------------------------------------------------- 1 | d2 3 2 | a4 4 3 | ! Version: v1.0.2 4 | ! Diff-Path: patches/v1.0.1-s-1702460925-5.patch 5 | ||example.net^ 6 | !#include ./filter_2.txt -------------------------------------------------------------------------------- /__tests__/fixtures/02_includes/filter.txt: -------------------------------------------------------------------------------- 1 | ! Title: Diff Updates Simple Example List 2 | ! Version: v1.0.2 3 | ! Diff-Path: patches/v1.0.1-s-1702460925-5.patch 4 | ||example.net^ 5 | !#include filter_2.txt 6 | -------------------------------------------------------------------------------- /bamboo-specs/bamboo.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | !include 'tests.yaml' 3 | 4 | --- 5 | !include 'increment.yaml' 6 | 7 | --- 8 | !include 'build.yaml' 9 | 10 | --- 11 | !include 'deploy.yaml' 12 | 13 | --- 14 | !include 'permissions.yaml' 15 | -------------------------------------------------------------------------------- /jest.config.ts: -------------------------------------------------------------------------------- 1 | export default { 2 | transform: { 3 | '^.+\\.(t|j)s$': '@swc/jest', 4 | }, 5 | testPathIgnorePatterns: [ 6 | '/node_modules/', 7 | '/_qunit_tests_/', 8 | '/__tests__/server/', 9 | ], 10 | }; 11 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.build.json", 3 | "include": [ 4 | "src", 5 | "__tests__", 6 | "_qunit_tests_", 7 | "scripts/build-txt.ts", 8 | "rollup.config.ts", 9 | ".eslintrc.js", 10 | "jest.config.ts" 11 | ] 12 | } 13 | -------------------------------------------------------------------------------- /bamboo-specs/permissions.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | version: 2 3 | deployment: 4 | name: filters-downloader - deploy 5 | deployment-permissions: 6 | - groups: 7 | - extensions-developers 8 | permissions: 9 | - view 10 | environment-permissions: 11 | - npmjs: 12 | - groups: 13 | - extensions-developers 14 | permissions: 15 | - view 16 | - deploy 17 | -------------------------------------------------------------------------------- /tsconfig.build.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "allowJs": true, 4 | "moduleResolution": "node", 5 | "target": "ESNext", 6 | "module":"ESNext", 7 | "lib": ["esnext", "dom"], 8 | "strict": true, 9 | "allowSyntheticDefaultImports": true, 10 | "esModuleInterop": true, 11 | "resolveJsonModule": true, 12 | }, 13 | "typeRoots": [ 14 | "node_modules/@types", 15 | ], 16 | "include": [ 17 | "src", 18 | ] 19 | } 20 | -------------------------------------------------------------------------------- /scripts/build-txt.ts: -------------------------------------------------------------------------------- 1 | const fs = require('fs'); 2 | const path = require('path'); 3 | const { version } = require('../package.json'); 4 | 5 | const PATH = '../build'; 6 | const FILENAME = 'build.txt'; 7 | 8 | const main = (): void => { 9 | const content = `version=${version}`; 10 | const dir = path.resolve(__dirname, PATH); 11 | 12 | if (!fs.existsSync(dir)) { 13 | fs.mkdirSync(dir); 14 | } 15 | 16 | fs.writeFileSync(path.resolve(__dirname, PATH, FILENAME), content); 17 | }; 18 | 19 | main(); 20 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | import { 2 | type IFiltersDownloader, 3 | DefinedExpressions, 4 | FiltersDownloaderCreator, 5 | type DownloadResult, 6 | } from './filters-downloader-creator'; 7 | import { 8 | getLocalFile, 9 | getExternalFile, 10 | } from './node/file-download-wrapper'; 11 | 12 | const FiltersDownloader = FiltersDownloaderCreator({ 13 | getLocalFile, 14 | getExternalFile, 15 | }); 16 | 17 | export { 18 | FiltersDownloader, 19 | type DownloadResult, 20 | type IFiltersDownloader, 21 | DefinedExpressions, 22 | }; 23 | -------------------------------------------------------------------------------- /src/index.browser.ts: -------------------------------------------------------------------------------- 1 | import { 2 | type IFiltersDownloader, 3 | DefinedExpressions, 4 | FiltersDownloaderCreator, 5 | type DownloadResult, 6 | } from './filters-downloader-creator'; 7 | import { 8 | getLocalFile, 9 | getExternalFile, 10 | } from './browser/file-download-wrapper'; 11 | 12 | const FiltersDownloader = FiltersDownloaderCreator({ 13 | getLocalFile, 14 | getExternalFile, 15 | }); 16 | 17 | export { 18 | FiltersDownloader, 19 | type DownloadResult, 20 | type IFiltersDownloader, 21 | DefinedExpressions, 22 | }; 23 | -------------------------------------------------------------------------------- /src/common/content-type.ts: -------------------------------------------------------------------------------- 1 | const PREFERRED_CONTENT_TYPE = 'text/plain'; 2 | 3 | /** 4 | * Supported content types. 5 | */ 6 | const SUPPORTED_CONTENT_TYPES = [ 7 | PREFERRED_CONTENT_TYPE, 8 | // https://github.com/AdguardTeam/AdguardBrowserExtension/issues/1723 9 | 'text/html', 10 | // https://github.com/AdguardTeam/AdguardBrowserExtension/issues/2908 11 | 'application/octet-stream', 12 | ]; 13 | 14 | /** 15 | * Returns content type error. 16 | * 17 | * @returns Error with description of supported content types. 18 | */ 19 | const getContentTypeError = (): Error => { 20 | return new Error(`Response content type should be one of: "${SUPPORTED_CONTENT_TYPES.join(', ')}"`); 21 | }; 22 | 23 | /** 24 | * Checks if the content type is supported. 25 | * 26 | * @param contentTypeHeader Content type header. 27 | * 28 | * @returns True if supported. 29 | */ 30 | const isContentTypeSupported = (contentTypeHeader: string | null): boolean => { 31 | if (!contentTypeHeader) { 32 | return false; 33 | } 34 | return SUPPORTED_CONTENT_TYPES.some((ct) => contentTypeHeader.includes(ct)); 35 | }; 36 | 37 | export { 38 | getContentTypeError, 39 | isContentTypeSupported, 40 | PREFERRED_CONTENT_TYPE, 41 | }; 42 | -------------------------------------------------------------------------------- /bamboo-specs/deploy.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | version: 2 3 | deployment: 4 | name: filters-downloader - deploy 5 | source-plan: AJL-FLTRDWNLBUILD 6 | release-naming: ${bamboo.inject.version} 7 | environments: 8 | - npmjs 9 | npmjs: 10 | docker: 11 | image: adguard/node-ssh:22.14--0 12 | volumes: 13 | ${system.PNPM_DIR}: "${bamboo.cachePnpm}" 14 | triggers: [ ] 15 | tasks: 16 | - checkout: 17 | force-clean-build: true 18 | - artifact-download: 19 | artifacts: 20 | - name: filters-downloader.tgz 21 | - script: 22 | interpreter: SHELL 23 | scripts: 24 | - |- 25 | set -e 26 | set -x 27 | 28 | # Fix mixed logs 29 | exec 2>&1 30 | 31 | ls -la 32 | 33 | export NPM_TOKEN=${bamboo.npmSecretToken} 34 | echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > .npmrc 35 | npm publish filters-downloader.tgz --access public 36 | requirements: 37 | - adg-docker: 'true' 38 | - extension: 'true' 39 | notifications: 40 | - events: 41 | - deployment-started-and-finished 42 | recipients: 43 | - webhook: 44 | name: Deploy webhook 45 | url: http://prod.jirahub.service.eu.consul/v1/webhook/bamboo 46 | -------------------------------------------------------------------------------- /bamboo-specs/increment.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | version: 2 3 | plan: 4 | project-key: AJL 5 | key: FLTRDWNLINC 6 | name: filters-downloader - increment version 7 | variables: 8 | dockerNode: adguard/node-ssh:22.14--0 9 | 10 | stages: 11 | - Increment: 12 | manual: true 13 | final: false 14 | jobs: 15 | - Increment 16 | Increment: 17 | key: INCR 18 | other: 19 | clean-working-dir: true 20 | docker: 21 | image: ${bamboo.dockerNode} 22 | volumes: 23 | ${system.PNPM_DIR}: "${bamboo.cachePnpm}" 24 | tasks: 25 | - checkout: 26 | force-clean-build: true 27 | - script: 28 | interpreter: SHELL 29 | scripts: 30 | - |- 31 | set -e 32 | set -x 33 | 34 | # Fix mixed logs 35 | exec 2>&1 36 | 37 | pnpm increment 38 | - any-task: 39 | plugin-key: com.atlassian.bamboo.plugins.vcs:task.vcs.commit 40 | configuration: 41 | commitMessage: 'skipci: Automatic increment build number' 42 | selectedRepository: defaultRepository 43 | requirements: 44 | - adg-docker: 'true' 45 | - extension: 'true' 46 | 47 | branches: 48 | create: manually 49 | delete: never 50 | link-to-jira: true 51 | notifications: [] 52 | labels: [] 53 | other: 54 | concurrent-build-plugin: system-default 55 | -------------------------------------------------------------------------------- /__tests__/checksum.test.ts: -------------------------------------------------------------------------------- 1 | import { describe, expect, it } from '@jest/globals'; 2 | import fs from 'fs/promises'; 3 | import path from 'path'; 4 | 5 | import { isValidChecksum } from '../src/checksum'; 6 | 7 | const getFilter = async (filterPath: string): Promise => { 8 | const filter = await fs.readFile(path.resolve(__dirname, filterPath), 'utf-8'); 9 | return filter; 10 | }; 11 | 12 | describe('isValidChecksum', () => { 13 | describe('when the filter does not have a checksum', () => { 14 | it('should return true, provided force equals false', async () => { 15 | const filterContent = '!some content'; 16 | expect(isValidChecksum(filterContent)).toBe(true); 17 | }); 18 | it('should return false, provided force equals true', async () => { 19 | const filterContent = '!some content'; 20 | expect(isValidChecksum(filterContent, true)).toBe(false); 21 | }); 22 | }); 23 | 24 | describe('when the filter has a checksum', () => { 25 | it('should return true if valid', async () => { 26 | const filter = await getFilter('./fixtures/checksum/valid_1.txt'); 27 | expect(isValidChecksum(filter, true)).toBe(true); 28 | }); 29 | it('should return false if invalid', async () => { 30 | const filter = await getFilter('./fixtures/checksum/not_valid_1.txt'); 31 | expect(isValidChecksum(filter, true)).toBe(false); 32 | }); 33 | }); 34 | }); 35 | -------------------------------------------------------------------------------- /__tests__/server/index.ts: -------------------------------------------------------------------------------- 1 | /* eslint-disable no-console */ 2 | import express from 'express'; 3 | import path from 'path'; 4 | import { Server } from 'http'; 5 | 6 | const app = express(); 7 | let serverInstance: Server; 8 | 9 | // Use Express.js static middleware to serve static files 10 | const root = path.resolve(__dirname, '../fixtures'); 11 | app.use('/fixtures', express.static(root)); 12 | 13 | /** 14 | * Start server. 15 | * @param port Port to listen. 16 | */ 17 | const start = (port = 3000): Promise => { 18 | return new Promise((resolve, reject) => { 19 | try { 20 | serverInstance = app.listen(port, () => { 21 | console.log(`Server is running at http://localhost:${port}`); 22 | resolve(); 23 | }); 24 | } catch (e) { 25 | reject(e); 26 | } 27 | }); 28 | }; 29 | 30 | /** 31 | * Stop server. 32 | */ 33 | const stop = (): Promise => { 34 | return new Promise((resolve, reject) => { 35 | if (!serverInstance) { 36 | reject(new Error('Server is not running')); 37 | } else { 38 | serverInstance.close((err) => { 39 | if (err) { 40 | reject(err); 41 | } else { 42 | console.log('Server has been stopped'); 43 | resolve(); 44 | } 45 | }); 46 | } 47 | }); 48 | }; 49 | 50 | const server = { start, stop }; 51 | export { server }; 52 | -------------------------------------------------------------------------------- /_qunit_tests_/testrunner.ts: -------------------------------------------------------------------------------- 1 | /* eslint-disable no-console */ 2 | ((): void => { 3 | // eslint-disable-next-line global-require 4 | const testRunner = require('node-qunit'); 5 | 6 | testRunner.setup({ 7 | log: { 8 | // log assertions overview 9 | assertions: false, 10 | // log expected and actual values for failed tests 11 | errors: true, 12 | // log tests overview 13 | tests: false, 14 | // log summary 15 | summary: true, 16 | // log global summary (all files) 17 | globalSummary: true, 18 | // log coverage 19 | coverage: true, 20 | // log global coverage (all files) 21 | globalCoverage: true, 22 | // log currently testing code file 23 | testing: false, 24 | }, 25 | // max amount of ms child can be blocked, after that we assume running an infinite loop 26 | maxBlockDuration: 50 * 1000, // 50 seconds 27 | }); 28 | 29 | const testCallback = (err: Error, report: any): void => { 30 | if (err) { 31 | console.error(err); 32 | process.exit(1); 33 | } 34 | if (report.failed > 0) { 35 | process.exit(1); 36 | } 37 | console.log('\n✅ Tests completed successfully!\n'); 38 | }; 39 | 40 | testRunner.run({ 41 | code: './dist/index.js', 42 | tests: './_qunit_tests_/filters-downloader.test.js', 43 | }, testCallback); 44 | })(); 45 | -------------------------------------------------------------------------------- /rollup.config.ts: -------------------------------------------------------------------------------- 1 | import { defineConfig } from 'rollup'; 2 | import resolve from '@rollup/plugin-node-resolve'; 3 | import commonjs from '@rollup/plugin-commonjs'; 4 | import typescript from '@rollup/plugin-typescript'; 5 | import json from '@rollup/plugin-json'; 6 | import cleanup from 'rollup-plugin-cleanup'; 7 | 8 | const commonPlugins = [ 9 | // Allow json resolution 10 | json(), 11 | 12 | // Compile TypeScript files 13 | typescript(), 14 | 15 | // Allow bundling cjs modules (unlike webpack, rollup doesn't understand cjs) 16 | commonjs({ sourceMap: false }), 17 | 18 | // Allow node_modules resolution, so you can use 'external' to control 19 | // which external modules to include in the bundle 20 | // https://github.com/rollup/rollup-plugin-node-resolve#usage 21 | resolve({ preferBuiltins: false }), 22 | 23 | cleanup({ comments: ['srcmaps'] }), 24 | ]; 25 | 26 | // Build for node 27 | const nodeConfig = defineConfig({ 28 | input: 'src/index.ts', 29 | output: [ 30 | { 31 | dir: 'dist', 32 | format: 'cjs', 33 | sourcemap: false, 34 | }, 35 | ], 36 | external: [ 37 | 'axios', 38 | 'path', 39 | 'fs', 40 | 'crypto', 41 | ], 42 | plugins: commonPlugins, 43 | }); 44 | 45 | // Build for browser 46 | const browserConfig = defineConfig({ 47 | input: 'src/index.browser.ts', 48 | output: [ 49 | { 50 | dir: 'dist', 51 | format: 'esm', 52 | sourcemap: false, 53 | }, 54 | ], 55 | external: [ 56 | // TODO: Maybe remove axios from diff-updater? 57 | 'axios', 58 | 'crypto', 59 | ], 60 | plugins: commonPlugins, 61 | }); 62 | 63 | export default [ 64 | nodeConfig, 65 | browserConfig, 66 | ]; 67 | -------------------------------------------------------------------------------- /__tests__/fixtures/checksum/not_valid_1.txt: -------------------------------------------------------------------------------- 1 | ! Checksum: /+5mDvEuIZh8NizEpyBZpw 2 | ! Diff-Path: ../patches/1/1-s-1706542562-3600.patch 3 | ! Title: AdGuard Russian filter 4 | ! Description: Filter that enables ad blocking on websites in Russian language. 5 | ! Version: 2.0.88.15 6 | ! TimeUpdated: 2024-01-29T15:32:16+00:00 7 | ! Expires: 5 days (update frequency) 8 | ! Homepage: https://github.com/AdguardTeam/AdGuardFilters 9 | ! License: https://github.com/AdguardTeam/AdguardFilters/blob/master/LICENSE 10 | ! 11 | !-------------------------------------------------------------------------------! 12 | !------------------ General JS API ---------------------------------------------! 13 | !-------------------------------------------------------------------------------! 14 | ! JS API START 15 | #%#var AG_onLoad=function(func){if(document.readyState==="complete"||document.readyState==="interactive")func();else if(document.addEventListener)document.addEventListener("DOMContentLoaded",func);else if(document.attachEvent)document.attachEvent("DOMContentLoaded",func)}; 16 | #%#var AG_removeElementById = function(id) { var element = document.getElementById(id); if (element && element.parentNode) { element.parentNode.removeChild(element); }}; 17 | #%#var AG_removeElementBySelector = function(selector) { if (!document.querySelectorAll) { return; } var nodes = document.querySelectorAll(selector); if (nodes) { for (var i = 0; i < nodes.length; i++) { if (nodes[i] && nodes[i].parentNode) { nodes[i].parentNode.removeChild(nodes[i]); } } } }; 18 | #%#var AG_each = function(selector, fn) { if (!document.querySelectorAll) return; var elements = document.querySelectorAll(selector); for (var i = 0; i < elements.length; i++) { fn(elements[i]); }; }; 19 | #%#var AG_removeParent = function(el, fn) { while (el && el.parentNode) { if (fn(el)) { el.parentNode.removeChild(el); return; } el = el.parentNode; } }; 20 | -------------------------------------------------------------------------------- /__tests__/fixtures/checksum/valid_1.txt: -------------------------------------------------------------------------------- 1 | ! Checksum: c+Z79vTMwWn+uMPYkdCgiA 2 | ! Diff-Path: ../patches/1/1-s-1706542562-3600.patch 3 | ! Title: AdGuard Russian filter 4 | ! Description: Filter that enables ad blocking on websites in Russian language. 5 | ! Version: 2.0.88.15 6 | ! TimeUpdated: 2024-01-29T15:32:16+00:00 7 | ! Expires: 5 days (update frequency) 8 | ! Homepage: https://github.com/AdguardTeam/AdGuardFilters 9 | ! License: https://github.com/AdguardTeam/AdguardFilters/blob/master/LICENSE 10 | ! 11 | !-------------------------------------------------------------------------------! 12 | !------------------ General JS API ---------------------------------------------! 13 | !-------------------------------------------------------------------------------! 14 | ! JS API START 15 | #%#var AG_onLoad=function(func){if(document.readyState==="complete"||document.readyState==="interactive")func();else if(document.addEventListener)document.addEventListener("DOMContentLoaded",func);else if(document.attachEvent)document.attachEvent("DOMContentLoaded",func)}; 16 | #%#var AG_removeElementById = function(id) { var element = document.getElementById(id); if (element && element.parentNode) { element.parentNode.removeChild(element); }}; 17 | #%#var AG_removeElementBySelector = function(selector) { if (!document.querySelectorAll) { return; } var nodes = document.querySelectorAll(selector); if (nodes) { for (var i = 0; i < nodes.length; i++) { if (nodes[i] && nodes[i].parentNode) { nodes[i].parentNode.removeChild(nodes[i]); } } } }; 18 | #%#var AG_each = function(selector, fn) { if (!document.querySelectorAll) return; var elements = document.querySelectorAll(selector); for (var i = 0; i < elements.length; i++) { fn(elements[i]); }; }; 19 | #%#var AG_removeParent = function(el, fn) { while (el && el.parentNode) { if (fn(el)) { el.parentNode.removeChild(el); return; } el = el.parentNode; } }; 20 | -------------------------------------------------------------------------------- /bamboo-specs/tests.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | version: 2 3 | plan: 4 | project-key: AJL 5 | key: FLTRDWNLTESTS 6 | name: filters-downloader - test 7 | variables: 8 | dockerNode: adguard/node-ssh:22.14--0 9 | 10 | stages: 11 | - Test: 12 | manual: false 13 | final: false 14 | jobs: 15 | - Test 16 | 17 | Test: 18 | key: TEST 19 | other: 20 | clean-working-dir: true 21 | docker: 22 | image: ${bamboo.dockerNode} 23 | volumes: 24 | ${system.PNPM_DIR}: "${bamboo.cachePnpm}" 25 | tasks: 26 | - checkout: 27 | force-clean-build: true 28 | - script: 29 | interpreter: SHELL 30 | scripts: 31 | - |- 32 | set -x 33 | set -e 34 | 35 | # Fix mixed logs 36 | exec 2>&1 37 | 38 | ls -laht 39 | 40 | # Set cache directory 41 | pnpm config set store-dir ${bamboo.cachePnpm} 42 | 43 | # Call build before test because test will operate with files from dist 44 | pnpm install ${bamboo_varsPnpm} 45 | 46 | pnpm build 47 | 48 | pnpm lint 49 | pnpm test 50 | final-tasks: 51 | - script: 52 | interpreter: SHELL 53 | scripts: 54 | - |- 55 | set -x 56 | set -e 57 | 58 | # Fix mixed logs 59 | exec 2>&1 60 | 61 | ls -laht 62 | 63 | echo "Size before cleanup:" && du -h | tail -n 1 64 | rm -rf node_modules 65 | echo "Size after cleanup:" && du -h | tail -n 1 66 | requirements: 67 | - adg-docker: 'true' 68 | - extension: 'true' 69 | 70 | branches: 71 | create: for-pull-request 72 | delete: 73 | after-deleted-days: '1' 74 | after-inactive-days: '5' 75 | link-to-jira: true 76 | 77 | notifications: [] 78 | labels: [] 79 | other: 80 | concurrent-build-plugin: system-default 81 | -------------------------------------------------------------------------------- /.github/workflows/test.yaml: -------------------------------------------------------------------------------- 1 | name: Test Filters Downloader 2 | 3 | env: 4 | # Minimum supported Node.js version is 18.13.0 which is specified in package.json 5 | # So we use it for testing to ensure compatibility 6 | NODE_VERSION: 18.13.0 7 | PNPM_VERSION: 10.7.1 8 | 9 | on: 10 | push 11 | 12 | jobs: 13 | build: 14 | 15 | runs-on: ubuntu-latest 16 | steps: 17 | - uses: actions/checkout@v4 18 | 19 | - name: Setup pnpm 20 | uses: pnpm/action-setup@v4 21 | with: 22 | version: ${{ env.PNPM_VERSION }} 23 | run_install: false 24 | 25 | - name: Use Node.jobs 26 | uses: actions/setup-node@v4 27 | with: 28 | node-version: ${{ env.NODE_VERSION }} 29 | 30 | - name: pnpm install 31 | run: pnpm install 32 | 33 | - name: pnpm build 34 | run: pnpm build 35 | 36 | - name: pnpm lint 37 | run: pnpm lint 38 | 39 | - name: pnpm test 40 | run: pnpm test 41 | 42 | notify: 43 | name: Send Slack notification on failure 44 | needs: build 45 | # Run this job only if the previous job failed and the event was triggered by the 'AdguardTeam/FiltersDownloader' repository 46 | # Note: 'always()' is needed to run the notify job even if the test job was failed 47 | if: 48 | ${{ 49 | always() && 50 | needs.check_code.result == 'failure' && 51 | github.repository == 'AdguardTeam/FiltersDownloader' && 52 | ( 53 | github.event_name == 'push' || 54 | github.event_name == 'workflow_dispatch' || 55 | github.event.pull_request.head.repo.full_name == github.repository 56 | ) 57 | }} 58 | runs-on: ubuntu-latest 59 | steps: 60 | - name: Send Slack notification 61 | uses: 8398a7/action-slack@v3 62 | with: 63 | status: failure 64 | fields: workflow, repo, message, commit, author, eventName, ref, job 65 | job_name: check_code 66 | env: 67 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 68 | SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} 69 | -------------------------------------------------------------------------------- /src/helpers/logger.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * The number of lines to include before the error line in the error message. 3 | */ 4 | const LINES_BEFORE_DIRECTIVE = 3; 5 | 6 | /** 7 | * Merge an error messages by joining the array strings with a newline character. 8 | * 9 | * @param {string[]} messages - The array of message strings to format. 10 | * @returns {string} The formatted error message. 11 | */ 12 | export const mergeErrorDetails = (messages: string[]): string => `${messages.join('\n')}\n`; 13 | 14 | /** 15 | * Creates and throws a detailed error message with context information. 16 | * 17 | * @param errorDescription The main error message. 18 | * @param errorRule The rule where the error occurred. 19 | * @param contextLines The context string (3 lines before) to include in the error message. 20 | * @param filterUrl The URL of the filter file. 21 | * @param originalError The original error to include in the error message. 22 | * @throws {Error} Throws an error with a detailed error message. 23 | */ 24 | export const throwError = ( 25 | errorDescription: string, 26 | errorRule: string, 27 | contextLines?: string, 28 | filterUrl?: string, 29 | originalError?: unknown, 30 | ): void => { 31 | const errorDetails = [`${errorDescription} '${errorRule}'`]; 32 | 33 | if (filterUrl) { 34 | errorDetails.push(`URL: '${filterUrl}'`); 35 | } 36 | 37 | if (contextLines) { 38 | errorDetails.push('Context:'); 39 | errorDetails.push(contextLines); 40 | errorDetails.push(`\t${errorRule}`); 41 | } 42 | 43 | if (originalError) { 44 | const originalErrorMessage = originalError instanceof Error ? originalError.message : String(originalError); 45 | errorDetails.push(`\t${originalErrorMessage}`); 46 | } 47 | 48 | const formattedErrorMessage = mergeErrorDetails(errorDetails); 49 | throw new Error(formattedErrorMessage); 50 | }; 51 | 52 | /** 53 | * Get (3) lines before the error rule for error messages. 54 | * @param rulesList List of rules to get context from. 55 | * @param errorRuleIndex Index of the error line. 56 | * @returns Lines before the error rule. 57 | */ 58 | export const getContext = (rulesList: string[], errorRuleIndex: number): string => { 59 | return rulesList 60 | .slice(Math.max(0, errorRuleIndex - LINES_BEFORE_DIRECTIVE), errorRuleIndex) 61 | .map((line) => `\t${line}`) 62 | .join('\n'); 63 | }; 64 | -------------------------------------------------------------------------------- /bamboo-specs/build.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | version: 2 3 | plan: 4 | project-key: AJL 5 | key: FLTRDWNLBUILD 6 | name: filters-downloader - build release 7 | variables: 8 | dockerNode: adguard/node-ssh:22.14--0 9 | 10 | stages: 11 | - Build: 12 | manual: false 13 | final: false 14 | jobs: 15 | - Build 16 | 17 | Build: 18 | key: BUILD 19 | other: 20 | clean-working-dir: true 21 | docker: 22 | image: ${bamboo.dockerNode} 23 | volumes: 24 | ${system.PNPM_DIR}: "${bamboo.cachePnpm}" 25 | tasks: 26 | - checkout: 27 | force-clean-build: true 28 | - script: 29 | interpreter: SHELL 30 | scripts: 31 | - |- 32 | set -x 33 | set -e 34 | 35 | # Fix mixed logs 36 | exec 2>&1 37 | 38 | ls -laht 39 | 40 | # Set cache directory 41 | pnpm config set store-dir ${bamboo.cachePnpm} 42 | 43 | # Call build before test because test will operate with files from dist 44 | pnpm install ${bamboo_varsPnpm} 45 | pnpm build 46 | 47 | pnpm pack --out filters-downloader.tgz 48 | - inject-variables: 49 | file: build/build.txt 50 | scope: RESULT 51 | namespace: inject 52 | - any-task: 53 | plugin-key: com.atlassian.bamboo.plugins.vcs:task.vcs.tagging 54 | configuration: 55 | selectedRepository: defaultRepository 56 | tagName: v${bamboo.inject.version} 57 | final-tasks: 58 | - script: 59 | interpreter: SHELL 60 | scripts: 61 | - |- 62 | set -x 63 | set -e 64 | 65 | # Fix mixed logs 66 | exec 2>&1 67 | 68 | ls -la 69 | 70 | echo "Size before cleanup:" && du -h | tail -n 1 71 | rm -rf node_modules 72 | echo "Size after cleanup:" && du -h | tail -n 1 73 | artifacts: 74 | - name: filters-downloader.tgz 75 | pattern: filters-downloader.tgz 76 | shared: true 77 | required: true 78 | requirements: 79 | - adg-docker: 'true' 80 | - extension: 'true' 81 | 82 | triggers: [] 83 | 84 | branches: 85 | create: manually 86 | delete: never 87 | link-to-jira: true 88 | 89 | notifications: 90 | - events: 91 | - plan-status-changed 92 | recipients: 93 | - webhook: 94 | name: Build webhook 95 | url: http://prod.jirahub.service.eu.consul/v1/webhook/bamboo 96 | labels: [] 97 | other: 98 | concurrent-build-plugin: system-default 99 | -------------------------------------------------------------------------------- /.github/workflows/release.yaml: -------------------------------------------------------------------------------- 1 | name: Github Release 2 | 3 | env: 4 | NODE_VERSION: 18.13.0 5 | PNPM_VERSION: 10.7.1 6 | # Forks do not have access to our vars 7 | VARS_PNPM: ${{ vars.VARS_PNPM || '--silent --ignore-scripts' }} 8 | 9 | # Workflow need write access to the repository to create a GitHub release 10 | permissions: 11 | contents: write 12 | 13 | on: 14 | push: 15 | tags: 16 | - v* 17 | # Make possible to run manually 18 | workflow_dispatch: 19 | inputs: 20 | # warn before running manually 21 | warning: 22 | description: 'Should be run only for tags like `v*`' 23 | required: false 24 | type: boolean 25 | 26 | # Make sure that only one release workflow runs at a time. 27 | concurrency: 28 | group: release 29 | 30 | jobs: 31 | release: 32 | name: Create GitHub release 33 | runs-on: ubuntu-latest 34 | # Only run this job for v* tags 35 | if: startsWith(github.ref, 'refs/tags/v') 36 | steps: 37 | - name: Check out the repository 38 | uses: actions/checkout@v4 39 | 40 | - name: Setup pnpm 41 | uses: pnpm/action-setup@v4 42 | with: 43 | version: ${{ env.PNPM_VERSION }} 44 | run_install: false 45 | 46 | - name: Set up Node.js 47 | uses: actions/setup-node@v4 48 | with: 49 | node-version: ${{ env.NODE_VERSION }} 50 | registry-url: https://registry.npmjs.org 51 | cache: pnpm 52 | 53 | - name: Install dependencies 54 | run: pnpm install $VARS_PNPM 55 | 56 | - name: Run build 57 | run: pnpm build 58 | 59 | - name: Run ESLint 60 | run: pnpm lint 61 | 62 | - name: Run tests 63 | run: pnpm test 64 | 65 | - name: Release on GitHub 66 | uses: softprops/action-gh-release@v1 67 | with: 68 | draft: false 69 | prerelease: false 70 | body: See [CHANGELOG.md](../master/CHANGELOG.md) for the list of changes. 71 | env: 72 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 73 | 74 | notify: 75 | name: Send Slack notification 76 | needs: 77 | - release 78 | 79 | if: 80 | ${{ always() && 81 | ( 82 | github.event_name == 'push' || 83 | github.event_name == 'workflow_dispatch' 84 | ) 85 | }} 86 | runs-on: ubuntu-latest 87 | steps: 88 | - name: Conclusion 89 | uses: technote-space/workflow-conclusion-action@v3 90 | 91 | - name: Send Slack notification 92 | uses: 8398a7/action-slack@v3 93 | with: 94 | status: ${{ env.WORKFLOW_CONCLUSION }} 95 | fields: workflow, repo, message, commit, author, eventName, ref 96 | env: 97 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 98 | SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} 99 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | const path = require('path'); 2 | 3 | module.exports = { 4 | root: true, 5 | env: { 6 | node: true, 7 | commonjs: true, 8 | es2021: true, 9 | }, 10 | parser: '@typescript-eslint/parser', 11 | parserOptions: { 12 | tsconfigRootDir: path.join(__dirname), 13 | project: 'tsconfig.json', 14 | }, 15 | plugins: [ 16 | 'import', 17 | '@typescript-eslint', 18 | ], 19 | extends: [ 20 | 'airbnb-base', 21 | 'airbnb-typescript/base', 22 | 'plugin:jsdoc/recommended', 23 | ], 24 | ignorePatterns: [ 25 | 'dist', 26 | ], 27 | rules: { 28 | indent: 'off', 29 | 'arrow-body-style': 'off', 30 | 'max-len': ['error', { code: 120, ignoreUrls: true }], 31 | 'no-new': 'off', 32 | 'no-continue': 'off', 33 | 'no-restricted-syntax': ['error', 'LabeledStatement', 'WithStatement'], 34 | 'no-constant-condition': ['error', { checkLoops: false }], 35 | 'no-param-reassign': 'off', 36 | 'no-unused-vars': ['error', { varsIgnorePattern: '^_' }], 37 | 'no-empty': ['error', { allowEmptyCatch: false }], 38 | 39 | 'import/prefer-default-export': 'off', 40 | 'import/no-extraneous-dependencies': ['error', { devDependencies: true }], 41 | 'import/no-cycle': 'off', 42 | 'import/export': 'off', 43 | 44 | '@typescript-eslint/indent': ['error', 4, { 45 | SwitchCase: 1, 46 | ignoredNodes: ['TSTypeParameterInstantiation'], 47 | }], 48 | '@typescript-eslint/no-non-null-assertion': 'off', 49 | '@typescript-eslint/interface-name-prefix': 'off', 50 | '@typescript-eslint/explicit-function-return-type': 'error', 51 | '@typescript-eslint/no-unused-vars': 'error', 52 | 53 | // types described in ts 54 | 'jsdoc/require-param-type': 'off', 55 | 'jsdoc/no-undefined-types': 'off', 56 | 'jsdoc/require-returns-type': 'off', 57 | 'jsdoc/tag-lines': 'off', 58 | 'jsdoc/require-throws': 'error', 59 | 'jsdoc/check-tag-names': ['error', { definedTags: ['jest-environment'] }], 60 | 'jsdoc/require-jsdoc': [ 61 | 'error', 62 | { 63 | contexts: [ 64 | 'ClassDeclaration', 65 | 'ClassProperty', 66 | 'FunctionDeclaration', 67 | 'MethodDefinition', 68 | ], 69 | }, 70 | ], 71 | 'jsdoc/require-description': [ 72 | 'error', 73 | { 74 | contexts: [ 75 | 'ClassDeclaration', 76 | 'ClassProperty', 77 | 'FunctionDeclaration', 78 | 'MethodDefinition', 79 | ], 80 | }, 81 | ], 82 | 'jsdoc/require-description-complete-sentence': ['error'], 83 | 'jsdoc/require-returns': ['error'], 84 | }, 85 | }; 86 | -------------------------------------------------------------------------------- /src/node/file-download-wrapper.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * This file is part of AdGuard Browser Extension (https://github.com/AdguardTeam/AdguardBrowserExtension). 3 | * 4 | * AdGuard Browser Extension is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * AdGuard Browser Extension is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with AdGuard Browser Extension. If not, see . 16 | */ 17 | 18 | import axios from 'axios'; 19 | import path from 'path'; 20 | import fs from 'fs'; 21 | 22 | import { isContentTypeSupported, getContentTypeError } from '../common/content-type'; 23 | 24 | /** 25 | * Executes async request. 26 | * 27 | * @param url Url. 28 | * 29 | * @returns Promise with request. 30 | */ 31 | const executeRequestAsync = (url: string): Promise => axios({ 32 | method: 'get', 33 | url, 34 | headers: { 35 | Pragma: 'no-cache', 36 | }, 37 | validateStatus: null, 38 | }); 39 | 40 | /** 41 | * Downloads filter rules from external url. 42 | * 43 | * @param url Filter file absolute URL or relative path. 44 | * @returns A promise that returns string with rules when if resolved and Error if rejected. 45 | */ 46 | const getExternalFile = (url: string): Promise => { 47 | return new Promise((resolve, reject) => { 48 | executeRequestAsync(url) 49 | .then((response) => { 50 | if (response.status !== 200 && response.status !== 0) { 51 | reject(new Error(`Response status for url ${url} is invalid: ${response.status}`)); 52 | } 53 | 54 | const responseContentType = response.headers['content-type']; 55 | 56 | if (!isContentTypeSupported(responseContentType)) { 57 | reject(getContentTypeError()); 58 | } 59 | 60 | const responseText = response.responseText ? response.responseText : response.data; 61 | 62 | resolve(responseText); 63 | }).catch((error) => { 64 | const updatedError = new Error(`Failed to request url '${url}'`, { cause: error }); 65 | reject(updatedError); 66 | }); 67 | }); 68 | }; 69 | 70 | /** 71 | * Get filter rules from the local path. 72 | * 73 | * @param url Local path. 74 | * @returns A promise that returns string with rules when if resolved and Error if rejected. 75 | */ 76 | const getLocalFile = (url: string): Promise => { 77 | return fs.promises.readFile(path.resolve(url), 'utf-8'); 78 | }; 79 | 80 | export { 81 | getLocalFile, 82 | getExternalFile, 83 | }; 84 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@adguard/filters-downloader", 3 | "version": "2.4.2", 4 | "description": "Compiles filters source files", 5 | "main": "dist/index.js", 6 | "browser": "dist/index.browser.js", 7 | "types": "dist/types/index.d.ts", 8 | "files": [ 9 | "dist" 10 | ], 11 | "exports": { 12 | ".": { 13 | "types": "./dist/types/index.d.ts", 14 | "default": "./dist/index.js" 15 | }, 16 | "./browser": { 17 | "types": "./dist/types/index.browser.d.ts", 18 | "default": "./dist/index.browser.js" 19 | } 20 | }, 21 | "scripts": { 22 | "prebuild": "pnpm rimraf dist && pnpm build:types", 23 | "build": "rollup -c rollup.config.ts --configPlugin typescript && pnpm build:version", 24 | "watch": "rollup -c rollup.config.ts --configPlugin typescript --watch", 25 | "build:version": "ts-node scripts/build-txt.ts", 26 | "build:types": "tsc --project tsconfig.build.json --declaration --emitDeclarationOnly --outdir dist/types", 27 | "test": "ts-node _qunit_tests_/testrunner.ts && jest", 28 | "lint": "eslint --cache . && tsc --noEmit", 29 | "lint-staged": "lint-staged", 30 | "prepare": "husky install", 31 | "increment": "pnpm version --patch --no-git-tag-version", 32 | "tgz": "pnpm pack --out filters-downloader.tgz" 33 | }, 34 | "lint-staged": { 35 | "{src,scripts,__tests__}/**/*.js": [ 36 | "eslint --cache . && tsc --noEmit" 37 | ] 38 | }, 39 | "engines": { 40 | "node": ">=18.13.0" 41 | }, 42 | "author": "AdGuard", 43 | "license": "GPL-3.0", 44 | "repository": { 45 | "type": "git", 46 | "url": "git+https://github.com/AdguardTeam/FiltersDownloader.git" 47 | }, 48 | "bugs": { 49 | "url": "https://github.com/AdguardTeam/FiltersDownloader/issues" 50 | }, 51 | "homepage": "https://github.com/AdguardTeam/FiltersDownloader#readme", 52 | "dependencies": { 53 | "@adguard/diff-builder": "1.1.2", 54 | "axios": "1.6.2", 55 | "crypto-js": "^4.2.0" 56 | }, 57 | "devDependencies": { 58 | "@jest/globals": "^29.7.0", 59 | "@rollup/plugin-commonjs": "^25.0.7", 60 | "@rollup/plugin-json": "^6.0.1", 61 | "@rollup/plugin-node-resolve": "^15.2.3", 62 | "@rollup/plugin-typescript": "^11.1.5", 63 | "@swc-node/register": "^1.8.0", 64 | "@swc/core": "^1.4.2", 65 | "@swc/helpers": "^0.5.6", 66 | "@swc/jest": "^0.2.29", 67 | "@types/crypto-js": "^4.2.2", 68 | "@types/express": "^4.17.21", 69 | "@types/mock-fs": "^4.13.4", 70 | "@types/node": "^20.9.2", 71 | "@types/qunit": "^2.19.9", 72 | "@typescript-eslint/eslint-plugin": "^6.11.0", 73 | "@typescript-eslint/parser": "^6.11.0", 74 | "eslint": "^8.50.0", 75 | "eslint-config-airbnb-base": "^15.0.0", 76 | "eslint-config-airbnb-typescript": "^17.1.0", 77 | "eslint-plugin-import": "^2.29.0", 78 | "eslint-plugin-import-newlines": "^1.3.4", 79 | "eslint-plugin-jsdoc": "^46.9.0", 80 | "express": "^4.18.2", 81 | "husky": "^8.0.3", 82 | "jest": "^29.7.0", 83 | "lint-staged": "^14.0.1", 84 | "mock-fs": "^5.5.0", 85 | "nock": "^13.4.0", 86 | "node-qunit": "^2.0.0", 87 | "regenerator-runtime": "^0.14.1", 88 | "rimraf": "^5.0.5", 89 | "rollup": "^4.9.1", 90 | "rollup-plugin-cleanup": "^3.2.1", 91 | "ts-node": "^10.9.1", 92 | "tslib": "^2.6.2", 93 | "typescript": "^5.3.3" 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /src/checksum.ts: -------------------------------------------------------------------------------- 1 | import MD5 from 'crypto-js/md5'; 2 | import Base64 from 'crypto-js/enc-base64'; 3 | 4 | const CHECKSUM_PATTERN = /^\s*!\s*checksum[\s-:]+([\w+/=]+).*[\r\n]+/i; 5 | 6 | /** 7 | * The maximum number of characters to search for the checksum pattern. 8 | */ 9 | const CHECKSUM_SEARCH_LIMIT = 200; 10 | 11 | /** 12 | * Removes the checksum line from the given content string. 13 | * @param content The content to remove the checksum from. 14 | * @returns The content with the checksum line removed. 15 | */ 16 | const removeChecksumLine = (content: string): string => { 17 | const partOfResponse = content.substring(0, CHECKSUM_SEARCH_LIMIT); 18 | const match = partOfResponse.match(CHECKSUM_PATTERN); 19 | if (match) { 20 | content = content.replace(match[0], ''); 21 | } 22 | return content; 23 | }; 24 | 25 | /** 26 | * Normalizes a message string by removing carriage return characters ('\r') and 27 | * replacing multiple newline characters ('\n') with a single newline character. 28 | * This function standardizes the format of newline characters in the message. 29 | * 30 | * @param content The string to normalize. 31 | * 32 | * @returns The normalized message with '\r' removed and consecutive '\n' 33 | * characters replaced with a single '\n'. 34 | */ 35 | const normalizeContent = (content: string): string => { 36 | content = removeChecksumLine(content); 37 | content = content.replace(/\r/g, ''); 38 | content = content.replace(/\n+/g, '\n'); 39 | return content; 40 | }; 41 | 42 | /** 43 | * Calculates the checksum of the given content using the MD5 hashing algorithm 44 | * and encodes it in Base64. It normalizes the content by removing carriage 45 | * returns and replacing multiple newlines with a single newline. 46 | * The checksum is then formatted with a trailing special comment identifier. 47 | * Trailing '=' characters in the Base64 encoded string are removed to match 48 | * the expected format. 49 | * 50 | * @see 51 | * {@link https://adblockplus.org/en/filters#special-comments Adblock Plus special comments} 52 | * {@link https://hg.adblockplus.org/adblockplus/file/tip/addChecksum.py Adblock Plus checksum script} 53 | * 54 | * @param content The content to hash. 55 | * 56 | * @returns The formatted checksum string. 57 | */ 58 | export const calculateChecksumMD5 = (content: string): string => { 59 | content = normalizeContent(content); 60 | const checksum = Base64.stringify(MD5(content)); 61 | 62 | return checksum.trim().replace(/=+$/g, ''); 63 | }; 64 | 65 | /** 66 | * Parses the checksum from the given content string. 67 | * 68 | * @param str The content string to parse. 69 | * @returns The checksum value or null if not found. 70 | */ 71 | const parseChecksum = (str: string): string | null => { 72 | const partOfResponse = str.substring(0, CHECKSUM_SEARCH_LIMIT); 73 | const checksumMatch = partOfResponse.match(CHECKSUM_PATTERN); 74 | if (!checksumMatch) { 75 | return null; 76 | } 77 | return checksumMatch[1] ?? null; 78 | }; 79 | 80 | /** 81 | * Checks if the given filter has a valid checksum. If the filter does not have 82 | * a checksum, it returns false unless the strict parameter is true. 83 | * 84 | * @param filter The filter to check. 85 | * @param strict If true, the function returns true if the filter does not have a 86 | * checksum. 87 | * @returns True if the filter has a valid checksum, false otherwise. 88 | */ 89 | export const isValidChecksum = (filter: string, strict = false): boolean => { 90 | const expectedChecksum = parseChecksum(filter); 91 | if (!expectedChecksum) { 92 | return !strict; 93 | } 94 | const actualChecksum = calculateChecksumMD5(filter); 95 | return actualChecksum === expectedChecksum; 96 | }; 97 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Filters Downloader 2 | 3 | ## Filters downloader package 4 | 5 | This utility tool resolves preprocessor directives in filter content. 6 | 7 | ### Directives syntax 8 | 9 | ```adblock 10 | !#if condition 11 | Anything goes here 12 | !#include URL_or_a_relative_path 13 | !#endif 14 | ``` 15 | 16 | or with an `else` branch: 17 | 18 | ```adblock 19 | !#if condition 20 | !#include URL_or_a_relative_path 21 | !#else 22 | !#include another_URL_or_a_relative_path 23 | !#endif 24 | ``` 25 | 26 | - `!#if`, `!#else`, `!#endif` — filters maintainers can use these conditions 27 | to supply different rules depending on the ad blocker type. 28 | - `condition` — just like in some popular programming languages, 29 | pre-processor conditions are based on constants declared by ad blockers. 30 | Ad blocker authors define on their own what exact constants do they declare. 31 | - `!#include` — this directive allows to include contents of a specified file into the filter. 32 | 33 | #### Logical conditions 34 | 35 | When an adblocker encounters an `!#if` directive and no `!#else` directive, 36 | it will compile the code between `!#if` and `!#endif` only if the specified condition is true. 37 | 38 | If there is an `!#else` directive, the code between `!#if` and `!#else` will be compiled if the condition is true, 39 | otherwise the code between `!#else` and `!#endif` will be compiled. 40 | 41 | Condition supports all the basic logical operators, i.e. `&&`, `||`, `!` and parentheses. 42 | 43 | Example: 44 | 45 | ```adblock 46 | !#if (adguard && !adguard_ext_safari) 47 | ||example.org^$third-party 48 | !#endif 49 | ``` 50 | 51 | #### Include 52 | 53 | The `!#include` directive supports only files from the same origin 54 | to make sure that the filter maintainer is in control of the specified file. 55 | The included file can also contain pre-processor directives (even other !#include directives). 56 | 57 | Ad blockers should consider the case of recursive !#include and implement a protection mechanism. 58 | 59 | Examples: 60 | 61 | Filter URL: `https://example.org/path/filter.txt` 62 | 63 | ```adblock 64 | ! 65 | ! Valid (same origin): 66 | !#include https://example.org/path/includedfile.txt 67 | ! 68 | ! Valid (relative path): 69 | !#include /includedfile.txt 70 | !#include ../path2/includedfile.txt 71 | ! 72 | ! Invalid (another origin): 73 | !#include https://example.com/path/includedfile.txt 74 | ``` 75 | 76 | ## Build 77 | 78 | To build one file for browser environment: 79 | 80 | ```bash 81 | pnpm build 82 | ``` 83 | 84 | See `./dist` directory for results. 85 | 86 | ## Usage 87 | 88 | ### Prerequisites 89 | 90 | 91 | 92 | - [Node.js] v18.13.0 or higher. 93 | 94 | ### Installation 95 | 96 | ```bash 97 | pnpm add @adguard/filters-downloader 98 | ``` 99 | 100 | ```js 101 | const FilterCompilerConditionsConstants = { 102 | adguard: true, 103 | // ... 104 | adguard_ext_android_cb: false 105 | }; 106 | 107 | // Option One 108 | let promise = FiltersDownloader.download("resources/rules.txt", FilterCompilerConditionsConstants); 109 | promise.then((compiled) => { 110 | // Success 111 | }, (exception) => { 112 | // Error 113 | }); 114 | 115 | // Option Two 116 | let promise = FiltersDownloader.compile(['rule'], 'http://example.com', FilterCompilerConditionsConstants); 117 | promise.then((compiled) => { 118 | // Success 119 | }, (exception) => { 120 | // Error 121 | }); 122 | 123 | // The downloadWithRaw() method downloads a filter, applies patches if possible and resolves conditionals; 124 | // if patch applying fails, isPatchUpdateFailed will be true. 125 | const { filter, rawFilter, isPatchUpdateFailed } = await FiltersDownloader.downloadWithRaw( 126 | url, 127 | { 128 | force: false, 129 | rawFilter: prevRawFilter, 130 | }, 131 | ); 132 | 133 | // Please note that, by default, empty files are not downloaded, and an error 'Response is empty' is thrown. 134 | // Only empty includes are downloaded. 135 | // If you want empty files to be downloaded, you can use the `allowEmptyResponse` option. 136 | let promise = FiltersDownloader.download("resources/empty.txt", FilterCompilerConditionsConstants, { allowEmptyResponse: true }); 137 | promise.then((compiled) => { 138 | // Success 139 | }, (exception) => { 140 | // Error 141 | }); 142 | ``` 143 | 144 | ## Tests 145 | 146 | ```bash 147 | pnpm test 148 | ``` 149 | 150 | ## Linter 151 | 152 | ```bash 153 | pnpm lint 154 | ``` 155 | -------------------------------------------------------------------------------- /src/browser/file-download-wrapper.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * This file is part of AdGuard Browser Extension (https://github.com/AdguardTeam/AdguardBrowserExtension). 3 | * 4 | * AdGuard Browser Extension is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * AdGuard Browser Extension is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with AdGuard Browser Extension. If not, see . 16 | */ 17 | import { 18 | isContentTypeSupported, 19 | getContentTypeError, 20 | PREFERRED_CONTENT_TYPE, 21 | } from '../common/content-type'; 22 | 23 | /** 24 | * Set of network protocols. Used to check if url is local. 25 | */ 26 | const networkProtocols = new Set(['http:', 'https:']); 27 | 28 | /** 29 | * If url protocol is not http or https return true, else false. 30 | * 31 | * @param url URL address to check. 32 | * 33 | * @returns Tue if provided address is local. 34 | */ 35 | const isLocal = (url: string): boolean => { 36 | const parsedUrl = new URL(url); 37 | return !networkProtocols.has(parsedUrl.protocol); 38 | }; 39 | 40 | /** 41 | * Executes async request via fetch. 42 | * Fetch doesn't allow downloading urls with file:// scheme. 43 | * 44 | * @param url URL. 45 | * 46 | * @returns Promise which will be resolved with string content of request 47 | * divided by '/r?/n'. 48 | */ 49 | const executeRequestAsyncFetch = async (url: string): Promise => { 50 | const response = await fetch(url, { 51 | cache: 'no-cache', 52 | headers: { 53 | Pragma: 'no-cache', 54 | 'Content-Type': PREFERRED_CONTENT_TYPE, 55 | }, 56 | }); 57 | 58 | if (response.status !== 200 && response.status !== 0) { 59 | throw new Error(`Response status for url ${url} is invalid: ${response.status}`); 60 | } 61 | 62 | // Don't check response headers if url is local, 63 | // because the edge extension doesn't provide headers for such url 64 | if (!isLocal(response.url)) { 65 | const responseContentType = response.headers.get('Content-Type'); 66 | if (!isContentTypeSupported(responseContentType)) { 67 | throw getContentTypeError(); 68 | } 69 | } 70 | 71 | return response.text(); 72 | }; 73 | 74 | /** 75 | * Executes an asynchronous XMLHttpRequest to retrieve the content of a file 76 | * from the given URL. 77 | * 78 | * @param url The URL of the file to retrieve. 79 | * @returns A Promise that resolves to a string representing data from the file. 80 | * @throws Throws an error if the response status is invalid, 81 | * the Content-Type is unsupported, or if there's an error during the request. 82 | */ 83 | const executeRequestAsyncXhr = (url: string): Promise => new Promise((resolve, reject) => { 84 | const onRequestLoad = (response: XMLHttpRequest): void => { 85 | if (response.status !== 200 && response.status !== 0) { 86 | reject(new Error(`Response status for url ${url} is invalid: ${response.status}`)); 87 | } 88 | 89 | const responseText = response.responseText 90 | ? response.responseText 91 | // @ts-ignore 92 | : response.data; 93 | 94 | // Don't check response headers if url is local, 95 | // because the edge extension doesn't provide headers for such url 96 | if (!isLocal(response.responseURL)) { 97 | const responseContentType = response.getResponseHeader('Content-Type'); 98 | if (!isContentTypeSupported(responseContentType)) { 99 | reject(getContentTypeError()); 100 | } 101 | } 102 | 103 | resolve(responseText); 104 | }; 105 | 106 | const request = new XMLHttpRequest(); 107 | 108 | try { 109 | request.open('GET', url); 110 | request.setRequestHeader('Pragma', 'no-cache'); 111 | request.overrideMimeType(PREFERRED_CONTENT_TYPE); 112 | // @ts-ignore 113 | request.mozBackgroundRequest = true; 114 | // eslint-disable-next-line func-names 115 | request.onload = (): void => { 116 | onRequestLoad(request); 117 | }; 118 | request.onerror = (): void => reject( 119 | new Error(`Request error happened: ${request.statusText || 'status text empty'}`), 120 | ); 121 | request.onabort = (): void => reject( 122 | new Error(`Request was aborted with status text: ${request.statusText}`), 123 | ); 124 | request.ontimeout = (): void => reject( 125 | new Error(`Request timed out with status text: ${request.statusText}`), 126 | ); 127 | 128 | request.send(null); 129 | } catch (ex) { 130 | reject(ex); 131 | } 132 | }); 133 | 134 | /** 135 | * Downloads filter rules from external url. 136 | * 137 | * @param url Filter file absolute URL or relative path. 138 | * @returns A promise that returns string of rules when resolved 139 | * and error if rejected. 140 | */ 141 | const getExternalFile = (url: string): Promise => executeRequestAsyncFetch(url); 142 | 143 | /** 144 | * Retrieves a local file content asynchronously using XMLHttpRequest or fetch API. 145 | * 146 | * @param url The URL of the local file to retrieve. 147 | * @returns A Promise that resolves to string representing the content of the file. 148 | * @throws Throws an error if neither XMLHttpRequest nor fetch is available or 149 | * if getting local files inside a service worker is not supported. 150 | */ 151 | const getLocalFile = (url: string): Promise => { 152 | if (typeof XMLHttpRequest !== 'undefined') { 153 | return executeRequestAsyncXhr(url); 154 | } 155 | if (typeof fetch !== 'undefined') { 156 | return executeRequestAsyncFetch(url); 157 | } 158 | throw new Error('XMLHttpRequest or fetch are undefined, getting local files inside service worker is not working'); 159 | }; 160 | 161 | export { 162 | getLocalFile, 163 | getExternalFile, 164 | }; 165 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Filters Downloader Changelog 2 | 3 | All notable changes to this project will be documented in this file. 4 | 5 | The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), 6 | and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). 7 | 8 | ## [2.4.2] - 2025-06-30 9 | 10 | ### Changed 11 | 12 | - Updated [@adguard/diff-builder] to 1.1.2. 13 | 14 | [2.4.2]: https://github.com/AdguardTeam/FiltersDownloader/compare/v2.4.1...v2.4.2 15 | 16 | ## [2.4.1] - 2025-06-09 17 | 18 | ### Changed 19 | 20 | - Updated [@adguard/diff-builder] to 1.1.1. 21 | 22 | [2.4.1]: https://github.com/AdguardTeam/FiltersDownloader/compare/v2.4.0...v2.4.1 23 | 24 | ## [2.4.0] - 2025-03-27 25 | 26 | ### Changed 27 | 28 | - Updated [@adguard/diff-builder] to 1.1.0. 29 | 30 | [2.4.0]: https://github.com/AdguardTeam/FiltersDownloader/compare/v2.3.1...v2.4.0 31 | 32 | ## [2.3.1] - 2025-03-11 33 | 34 | ### Added 35 | 36 | - `allowEmptyResponse` option for download empty files. [HostlistCompiler#85] 37 | 38 | [2.3.1]: https://github.com/AdguardTeam/FiltersDownloader/compare/v2.3.0...v2.3.1 39 | [HostlistCompiler#85]: https://github.com/AdguardTeam/HostlistCompiler/issues/85 40 | 41 | ## [2.3.0] - 2025-03-06 42 | 43 | ### Fixed 44 | 45 | - `filterOrigin` path duplication while download local file. [HostlistCompiler#82] 46 | 47 | [2.3.0]: https://github.com/AdguardTeam/FiltersDownloader/compare/v2.2.6...v2.3.0 48 | [HostlistCompiler#82]: https://github.com/AdguardTeam/HostlistCompiler/issues/82 49 | 50 | ## [2.2.6] - 2024-12-13 51 | 52 | ### Changed 53 | 54 | - Better description of exported types in package.json. 55 | 56 | [2.2.6]: https://github.com/AdguardTeam/FiltersDownloader/compare/v2.2.4...v2.2.6 57 | 58 | ## [2.2.4] - 2024-12-06 59 | 60 | ### Changed 61 | 62 | - Improved error message during `!#include` and condition directives resolving [FiltersCompiler#213]. 63 | 64 | [2.2.4]: https://github.com/AdguardTeam/FiltersDownloader/compare/v2.2.3...v2.2.4 65 | [FiltersCompiler#213]: https://github.com/AdguardTeam/FiltersCompiler/issues/213 66 | 67 | ## [2.2.3] - 2024-11-25 68 | 69 | ### Added 70 | 71 | - Ability to add filters from Google Drive [AdguardBrowserExtension#2908]. 72 | 73 | [2.2.3]: https://github.com/AdguardTeam/FiltersDownloader/compare/v2.2.2...v2.2.3 74 | [AdguardBrowserExtension#2908]: https://github.com/AdguardTeam/AdguardBrowserExtension/issues/2908 75 | 76 | ## [2.2.2] - 2024-07-11 77 | 78 | ### Added 79 | 80 | - `adguard_ext_chromium_mv3` optional property to the `DefinedExpressions` type. 81 | 82 | [2.2.2]: https://github.com/AdguardTeam/FiltersDownloader/compare/v2.2.1...v2.2.2 83 | 84 | ## [2.2.1] - 2024-05-22 85 | 86 | ### Changed 87 | 88 | - Improved error handling during include directives resolving. 89 | 90 | [2.2.1]: https://github.com/AdguardTeam/FiltersDownloader/compare/v2.2.0...v2.2.1 91 | 92 | ## [2.2.0] - 2024-03-26 93 | 94 | ### Changed 95 | 96 | - rawFilter is returned as a string, so that new lines can be preserved. 97 | 98 | [2.2.0]: https://github.com/AdguardTeam/FiltersDownloader/compare/v2.1.2...v2.2.0 99 | 100 | ## [2.1.2] - 2024-03-25 101 | 102 | ### Added 103 | 104 | - `isPatchUpdateFailed` flag in the result of `downloadWithRaw()` method to indicate 105 | that patch application failed [AdguardBrowserExtension#2717]. 106 | 107 | ### Changed 108 | 109 | - Updated [@adguard/diff-builder] to 1.0.17. 110 | 111 | [2.1.2]: https://github.com/AdguardTeam/FiltersDownloader/compare/v2.1.1...v2.1.2 112 | [AdguardBrowserExtension#2717]: https://github.com/AdguardTeam/AdguardBrowserExtension/issues/2717 113 | 114 | ## [2.1.1] - 2024-03-13 115 | 116 | ### Changed 117 | 118 | - Added validation of checksums for filters updates. 119 | 120 | [2.1.1]: https://github.com/AdguardTeam/FiltersDownloader/compare/v2.0.7...v2.1.1 121 | 122 | ## [2.0.7] - 2024-01-17 123 | 124 | ### Changed 125 | 126 | - Updated [@adguard/diff-builder] to 1.0.13. 127 | 128 | [2.0.7]: https://github.com/AdguardTeam/FiltersDownloader/compare/v2.0.6...v2.0.7 129 | 130 | ## [2.0.6] - 2024-01-17 131 | 132 | ### Changed 133 | 134 | - Updated [@adguard/diff-builder] to 1.0.12. 135 | 136 | [2.0.6]: https://github.com/AdguardTeam/FiltersDownloader/compare/v2.0.5...v2.0.6 137 | 138 | ## [2.0.5] - 2024-01-11 139 | 140 | ### Changed 141 | 142 | - Updated [@adguard/diff-builder] to 1.0.10. 143 | 144 | [2.0.5]: https://github.com/AdguardTeam/FiltersDownloader/compare/v2.0.4...v2.0.5 145 | 146 | ## [2.0.4] - 2024-01-11 147 | 148 | ### Fixed 149 | 150 | - Split lines considering all possible line endings. 151 | 152 | [2.0.4]: https://github.com/AdguardTeam/FiltersDownloader/compare/v2.0.3...v2.0.4 153 | 154 | ## [2.0.3] - 2023-12-29 155 | 156 | ### Fixed 157 | 158 | - Performance regression in resolveInclude 159 | 160 | [2.0.3]: https://github.com/AdguardTeam/FiltersDownloader/compare/v2.0.2...v2.0.3 161 | 162 | ## [2.0.2] - 2023-12-29 163 | 164 | ### Changed 165 | 166 | - Updated [@adguard/diff-builder] to 1.0.8. 167 | 168 | [2.0.2]: https://github.com/AdguardTeam/FiltersDownloader/compare/v2.0.1...v2.0.2 169 | 170 | ## [2.0.1] - 2023-12-29 171 | 172 | ### Changed 173 | 174 | - Updated [@adguard/diff-builder] to 1.0.7. 175 | 176 | [2.0.1]: https://github.com/AdguardTeam/FiltersDownloader/compare/v2.0.0...v2.0.1 177 | 178 | ## [2.0.0] - 2023-12-27 179 | 180 | ### Added 181 | 182 | - TypeScript support. 183 | - Using [@adguard/diff-builder] library to load filters updates by patches. 184 | 185 | [2.0.0]: https://github.com/AdguardTeam/FiltersDownloader/compare/v1.1.23...v2.0.0 186 | 187 | ## [1.1.23] - 2023-11-09 188 | 189 | ### Fixed 190 | 191 | - Cannot add a custom filter list with a .php URL [AdguardBrowserExtension#1723]. 192 | 193 | [1.1.23]: https://github.com/AdguardTeam/FiltersDownloader/compare/v1.1.21...v1.1.23 194 | [AdguardBrowserExtension#1723]: https://github.com/AdguardTeam/AdguardBrowserExtension/issues/1723 195 | 196 | ## [1.1.21] - 2023-10-20 197 | 198 | ### Fixed 199 | 200 | - Validation of included urls. 201 | 202 | [1.1.21]: https://github.com/AdguardTeam/FiltersDownloader/compare/v1.1.20...v1.1.21 203 | 204 | ## [1.1.20] - 2023-10-10 205 | 206 | ### Added 207 | 208 | - `!#else` pre-processor directive support [#20]. 209 | 210 | [#20]: https://github.com/AdguardTeam/FiltersDownloader/issues/20 211 | [1.1.20]: https://github.com/AdguardTeam/FiltersDownloader/compare/v1.1.15...v1.1.20 212 | 213 | ## [1.1.15] - 2022-11-25 214 | 215 | ### Fixed 216 | 217 | - Too large filter list including [AdGuardForSafari#720](https://github.com/AdguardTeam/AdGuardForSafari/issues/720). 218 | 219 | [1.1.15]: https://github.com/AdguardTeam/FiltersDownloader/compare/v1.1.14...v1.1.15 220 | 221 | [@adguard/diff-builder]: https://github.com/AdguardTeam/DiffBuilder/blob/master/CHANGELOG.md 222 | -------------------------------------------------------------------------------- /__tests__/filters-downloader.test.ts: -------------------------------------------------------------------------------- 1 | import { 2 | describe, 3 | it, 4 | expect, 5 | beforeAll, 6 | afterAll, 7 | afterEach, 8 | } from '@jest/globals'; 9 | import path from 'path'; 10 | import fs from 'fs/promises'; 11 | import nock from 'nock'; 12 | import mockFs from 'mock-fs'; 13 | 14 | import { FiltersDownloader } from '../src'; 15 | import { server } from './server'; 16 | import { mergeErrorDetails } from '../src/helpers/logger'; 17 | 18 | const FilterCompilerConditionsConstants = { 19 | adguard: true, 20 | adguard_ext_chromium: true, 21 | adguard_ext_firefox: false, 22 | adguard_ext_edge: false, 23 | adguard_ext_safari: false, 24 | adguard_ext_opera: false, 25 | adguard_ext_android_cb: false, 26 | }; 27 | 28 | describe('FiltersDownloader', () => { 29 | describe('error message during `!#include` and condition directives resolving', () => { 30 | it('unexpected condition else branch', async () => { 31 | const rules = [ 32 | 'always_included_rule', 33 | '!#else', 34 | 'if_adguard_included_rule', 35 | ]; 36 | const errorDetails = [ 37 | 'Found unexpected condition else branch: \'!#else\'', 38 | 'Context:', 39 | '\talways_included_rule', 40 | '\t!#else', 41 | ]; 42 | expect(() => FiltersDownloader.resolveConditions( 43 | rules, 44 | FilterCompilerConditionsConstants, 45 | )).toThrowError(new Error(mergeErrorDetails(errorDetails))); 46 | }); 47 | 48 | it('unexpected condition end', async () => { 49 | const rules = [ 50 | 'always_included_rule', 51 | 'if_adguard_included_rule', 52 | '!#endif', 53 | ]; 54 | const errorDetails = [ 55 | 'Found unexpected condition end: \'!#endif\'', 56 | 'Context:', 57 | '\talways_included_rule', 58 | '\tif_adguard_included_rule', 59 | '\t!#endif', 60 | ]; 61 | expect(() => FiltersDownloader.resolveConditions( 62 | rules, 63 | FilterCompilerConditionsConstants, 64 | )).toThrowError(new Error(mergeErrorDetails(errorDetails))); 65 | }); 66 | 67 | it('failed to resolve the include directive', async () => { 68 | const nonExistentFilePath = path.resolve(__dirname, './resources/not_found_file.txt'); 69 | const rules = [ 70 | 'always_included_rule', 71 | '||example.com', 72 | '||example.org', 73 | `!#include ${nonExistentFilePath}`, 74 | ]; 75 | const errorDetails = [ 76 | `Failed to resolve the include directive '!#include ${nonExistentFilePath}'`, 77 | 'Context:', 78 | '\talways_included_rule', 79 | '\t||example.com', 80 | '\t||example.org', 81 | `\t!#include ${nonExistentFilePath}`, 82 | `\tError: ENOENT: no such file or directory, open '${nonExistentFilePath}'`, 83 | ]; 84 | await expect(FiltersDownloader.resolveIncludes( 85 | rules, 86 | undefined, 87 | FilterCompilerConditionsConstants, 88 | )).rejects.toThrowError(new Error(mergeErrorDetails(errorDetails))); 89 | }); 90 | 91 | it('failed to resolve the include directive without file path', async () => { 92 | const rules = [ 93 | 'always_included_rule', 94 | 'included_rule', 95 | '||example.org^', 96 | '||example.com^', 97 | '!#include', 98 | ]; 99 | const errorDetails = [ 100 | 'Failed to resolve the include directive \'!#include\'', 101 | 'Context:', 102 | '\tincluded_rule', 103 | '\t||example.org^', 104 | '\t||example.com^', 105 | '\t!#include', 106 | '\tError: EISDIR: illegal operation on a directory, read', 107 | ]; 108 | await expect(FiltersDownloader.resolveIncludes( 109 | rules, 110 | undefined, 111 | FilterCompilerConditionsConstants, 112 | )).rejects.toThrowError(new Error(mergeErrorDetails(errorDetails))); 113 | }); 114 | 115 | it('failed to resolve the include directive with 404 path', async () => { 116 | const rules = [ 117 | 'always_included_rule', 118 | '||example.org^', 119 | '!#include https://raw.githubusercontent.com/AdguardTeam/FiltersDownloader/test-resources/__test__/resources/blabla.txt', 120 | 'if_adguard_included_rule', 121 | '||example.com^', 122 | ]; 123 | const errorDetails = [ 124 | 'Failed to resolve the include directive \'!#include https://raw.githubusercontent.com/AdguardTeam/FiltersDownloader/test-resources/__test__/resources/blabla.txt\'', 125 | 'Context:', 126 | '\talways_included_rule', 127 | '\t||example.org^', 128 | '\t!#include https://raw.githubusercontent.com/AdguardTeam/FiltersDownloader/test-resources/__test__/resources/blabla.txt', 129 | '\tResponse status for url https://raw.githubusercontent.com/AdguardTeam/FiltersDownloader/test-resources/__test__/resources/blabla.txt is invalid: 404', 130 | ]; 131 | await expect( 132 | FiltersDownloader.compile( 133 | rules, 134 | undefined, 135 | FilterCompilerConditionsConstants, 136 | ), 137 | ).rejects.toThrowError(new Error(mergeErrorDetails(errorDetails))); 138 | }); 139 | }); 140 | describe('downloadWithRaw', () => { 141 | describe('applies patches', () => { 142 | beforeAll(async () => { 143 | await server.start(); 144 | }); 145 | 146 | afterAll(async () => { 147 | await server.stop(); 148 | }); 149 | 150 | it('applies patches', async () => { 151 | const basePath = 'http://localhost:3000'; 152 | const url = new URL('./fixtures/01_simple/filter.txt', basePath).toString(); 153 | const prevFilter = await fs.readFile( 154 | path.resolve(__dirname, './fixtures/01_simple/filter_v1.0.0.txt'), 155 | 'utf-8', 156 | ); 157 | const curFilter = await fs.readFile( 158 | path.resolve(__dirname, './fixtures/01_simple/filter.txt'), 159 | 'utf-8', 160 | ); 161 | const { filter, rawFilter } = await FiltersDownloader.downloadWithRaw(url, { rawFilter: prevFilter }); 162 | expect(filter).toEqual(curFilter.trim().split(/[\r\n]+/)); 163 | // since there are no any includes, they are the same 164 | expect(rawFilter).toEqual(curFilter); 165 | }); 166 | 167 | it('applies conditions after patches', async () => { 168 | const basePath = 'http://localhost:3000'; 169 | const url = new URL('./fixtures/02_includes/filter.txt', basePath).toString(); 170 | const prevFilter = await fs.readFile( 171 | path.resolve(__dirname, './fixtures/02_includes/filter_v1.0.0.txt'), 172 | 'utf-8', 173 | ); 174 | const { filter, rawFilter } = await FiltersDownloader.downloadWithRaw(url, { rawFilter: prevFilter }); 175 | const expectedFilter = [ 176 | '! Title: Diff Updates Simple Example List', 177 | '! Version: v1.0.2', 178 | '! Diff-Path: patches/v1.0.1-s-1702460925-5.patch', 179 | '||example.net^', 180 | '||included.com^', 181 | ]; 182 | const expectedRawFilter = `! Title: Diff Updates Simple Example List 183 | ! Version: v1.0.2 184 | ! Diff-Path: patches/v1.0.1-s-1702460925-5.patch 185 | ||example.net^ 186 | !#include ./filter_2.txt`; 187 | expect(filter).toEqual(expectedFilter); 188 | expect(rawFilter).toEqual(expectedRawFilter); 189 | }); 190 | }); 191 | describe('force', () => { 192 | it('fetches only once when force is used', async () => { 193 | const basePath = 'http://localhost:3000'; 194 | const url = new URL('./fixtures/02_includes/filter.txt', basePath).toString(); 195 | 196 | // Setup Nock to intercept the request and count the number of times it's called 197 | const scope = nock(basePath) 198 | .get('/fixtures/02_includes/filter.txt') 199 | .once() // expecting it to be called only once 200 | .replyWithFile( 201 | 200, 202 | path.resolve(__dirname, './fixtures/02_includes/filter.txt'), 203 | { 204 | 'Content-Type': 'text/plain', 205 | }, 206 | ); 207 | 208 | scope 209 | .get('/fixtures/02_includes/filter_2.txt') 210 | .once() 211 | .replyWithFile( 212 | 200, 213 | path.resolve(__dirname, './fixtures/02_includes/filter_2.txt'), 214 | { 215 | 'Content-Type': 'text/plain', 216 | }, 217 | ); 218 | 219 | const { filter, rawFilter } = await FiltersDownloader.downloadWithRaw(url, { force: true }); 220 | const expectedFilter = [ 221 | '! Title: Diff Updates Simple Example List', 222 | '! Version: v1.0.2', 223 | '! Diff-Path: patches/v1.0.1-s-1702460925-5.patch', 224 | '||example.net^', 225 | '||included.com^', 226 | ]; 227 | const expectedRawFilter = `! Title: Diff Updates Simple Example List 228 | ! Version: v1.0.2 229 | ! Diff-Path: patches/v1.0.1-s-1702460925-5.patch 230 | ||example.net^ 231 | !#include filter_2.txt 232 | `; 233 | expect(filter).toEqual(expectedFilter); 234 | expect(rawFilter).toEqual(expectedRawFilter); 235 | // Assert that each URL was fetched only once 236 | expect(scope.isDone()).toBe(true); 237 | }); 238 | }); 239 | describe('if no Diff-Path in the filter', () => { 240 | it('fetches only once when force is used', async () => { 241 | const basePath = 'http://localhost:3000'; 242 | const url = new URL('./fixtures/03_no_patches/filter.txt', basePath).toString(); 243 | 244 | // Setup Nock to intercept the request and count the number of times it's called 245 | const scope = nock(basePath) 246 | .get('/fixtures/03_no_patches/filter.txt') 247 | .once() // expecting it to be called only once 248 | .replyWithFile( 249 | 200, 250 | path.resolve(__dirname, './fixtures/03_no_patches/filter.txt'), 251 | { 252 | 'Content-Type': 'text/plain', 253 | }, 254 | ); 255 | 256 | scope 257 | .get('/fixtures/03_no_patches/filter_2.txt') 258 | .once() // expecting it to be called only once 259 | .replyWithFile( 260 | 200, 261 | path.resolve(__dirname, './fixtures/03_no_patches/filter_2.txt'), 262 | { 263 | 'Content-Type': 'text/plain', 264 | }, 265 | ); 266 | 267 | const prevRawFilter = await fs.readFile( 268 | path.resolve(__dirname, './fixtures/03_no_patches/filter_v1.0.0.txt'), 269 | 'utf-8', 270 | ); 271 | 272 | const { filter, rawFilter } = await FiltersDownloader.downloadWithRaw( 273 | url, 274 | { 275 | force: false, 276 | rawFilter: prevRawFilter, 277 | }, 278 | ); 279 | const expectedFilter = [ 280 | '! Title: Simple Example List', 281 | '! Version: v1.0.2', 282 | '||example.net^', 283 | '||included.com^', 284 | ]; 285 | const expectedRawFilter = `! Title: Simple Example List 286 | ! Version: v1.0.2 287 | ||example.net^ 288 | !#include filter_2.txt 289 | `; 290 | expect(filter).toEqual(expectedFilter); 291 | expect(rawFilter).toEqual(expectedRawFilter); 292 | // Assert that each URL was fetched only once 293 | expect(scope.isDone()).toBe(true); 294 | }); 295 | }); 296 | describe('does not adds diff path from the included filter', () => { 297 | it('removes diff path from included filter', async () => { 298 | const basePath = 'http://localhost:3000'; 299 | const url = new URL('./fixtures/04_no_diff_path_for_included/filter.txt', basePath).toString(); 300 | 301 | // Setup Nock to intercept the request and count the number of times it's called 302 | const scope = nock(basePath) 303 | .get('/fixtures/04_no_diff_path_for_included/filter.txt') 304 | .once() // expecting it to be called only once 305 | .replyWithFile( 306 | 200, 307 | path.resolve(__dirname, './fixtures/04_no_diff_path_for_included/filter.txt'), 308 | { 309 | 'Content-Type': 'text/plain', 310 | }, 311 | ); 312 | 313 | scope 314 | .get('/fixtures/04_no_diff_path_for_included/filter_2.txt') 315 | .once() // expecting it to be called only once 316 | .replyWithFile( 317 | 200, 318 | path.resolve(__dirname, './fixtures/04_no_diff_path_for_included/filter_2.txt'), 319 | { 320 | 'Content-Type': 'text/plain', 321 | }, 322 | ); 323 | 324 | const prevRawFilter = await fs.readFile( 325 | path.resolve(__dirname, './fixtures/04_no_diff_path_for_included/filter_v1.0.0.txt'), 326 | 'utf-8', 327 | ); 328 | 329 | const { filter, rawFilter } = await FiltersDownloader.downloadWithRaw( 330 | url, 331 | { 332 | force: false, 333 | rawFilter: prevRawFilter, 334 | }, 335 | ); 336 | const expectedFilter = [ 337 | '! Title: Simple Example List', 338 | '! Version: v1.0.2', 339 | '||example.net^', 340 | '! Title: Simple Filter 2', 341 | '! Version: v1.0.0', 342 | '||included2.com^', 343 | ]; 344 | const expectedRawFilter = `! Title: Simple Example List 345 | ! Version: v1.0.2 346 | ||example.net^ 347 | !#include filter_2.txt 348 | `; 349 | expect(filter).toEqual(expectedFilter); 350 | expect(rawFilter).toEqual(expectedRawFilter); 351 | // Assert that each URL was fetched only once 352 | expect(scope.isDone()).toBe(true); 353 | }); 354 | }); 355 | }); 356 | 357 | describe('download local files', () => { 358 | afterEach(() => { 359 | // restore the file system 360 | mockFs.restore(); 361 | }); 362 | 363 | it('simple download from local source', async () => { 364 | // mock file system 365 | mockFs({ 366 | 'test/dir': { 367 | 'rules.txt': 'testrule', 368 | }, 369 | }); 370 | 371 | // compile the final list 372 | const list = await FiltersDownloader.download('test/dir/rules.txt'); 373 | 374 | const expectedRules = [ 375 | 'testrule', 376 | ]; 377 | 378 | expectedRules.forEach((rule) => { 379 | expect(list).toContain(rule); 380 | }); 381 | }); 382 | 383 | it('downloads and resolves includes from local source', async () => { 384 | // mock file system 385 | mockFs({ 386 | 'test/dir': { 387 | 'rules.txt': `example.com 388 | !#include include.txt`, 389 | 'include.txt': 'first.include.com', 390 | }, 391 | }); 392 | 393 | // compile the final list 394 | const list = await FiltersDownloader.download('test/dir/rules.txt'); 395 | 396 | const expectedRules = [ 397 | 'example.com', 398 | 'first.include.com', 399 | ]; 400 | 401 | expectedRules.forEach((rule) => { 402 | expect(list).toContain(rule); 403 | }); 404 | }); 405 | 406 | it('downloads and resolves nested includes from local source', async () => { 407 | // mock file system 408 | mockFs({ 409 | 'test/dir': { 410 | 'rules.txt': `example.com 411 | !#include include1.txt`, 412 | 'include1.txt': `first.include.com 413 | !#include include2.txt`, 414 | 'include2.txt': 'second.include.com', 415 | }, 416 | }); 417 | 418 | // compile the final list 419 | const list = await FiltersDownloader.download('test/dir/rules.txt'); 420 | 421 | const expectedRules = [ 422 | 'example.com', 423 | 'first.include.com', 424 | 'second.include.com', 425 | ]; 426 | 427 | expectedRules.forEach((rule) => { 428 | expect(list).toContain(rule); 429 | }); 430 | }); 431 | 432 | it('does not resolve include from different origin', async () => { 433 | // mock file system 434 | mockFs({ 435 | 'test/dir': { 436 | 'rules.txt': `example.com 437 | !#include https://example.org/inclusions.txt`, 438 | }, 439 | }); 440 | 441 | await expect(async () => { 442 | await FiltersDownloader.download('test/dir/rules.txt'); 443 | }).rejects.toThrow(Error); 444 | }); 445 | }); 446 | }); 447 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . -------------------------------------------------------------------------------- /src/filters-downloader-creator.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * This file is part of AdGuard Browser Extension (https://github.com/AdguardTeam/AdguardBrowserExtension). 3 | * 4 | * AdGuard Browser Extension is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU Lesser General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * AdGuard Browser Extension is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU Lesser General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with AdGuard Browser Extension. If not, see . 16 | */ 17 | 18 | import { DiffUpdater, UnacceptableResponseError } from '@adguard/diff-builder/diff-updater'; 19 | import { isValidChecksum } from './checksum'; 20 | import { throwError, getContext } from './helpers/logger'; 21 | 22 | /** 23 | * The utility tool resolves preprocessor directives in filter content. 24 | * 25 | * Directives syntax: 26 | * - `!#if`, `!#endif` — filters maintainers can use these conditions to supply different rules 27 | * depending on the ad blocker type. 28 | * - `condition` — just like in some popular programming languages, pre-processor conditions are based on constants 29 | * declared by ad blockers. Ad blocker authors define on their own what exact constants do they declare. 30 | * - `!#include` — this directive allows to include contents of a specified file into the filter. 31 | * 32 | * Condition constants should be declared in FilterCompilerConditionsConstants. 33 | * 34 | * More details: 35 | * https://github.com/AdguardTeam/AdguardBrowserExtension/issues/917. 36 | */ 37 | 38 | /** 39 | * An interface representing a file download wrapper with methods to retrieve 40 | * local and external files. 41 | * 42 | * @interface IFileDownloader 43 | */ 44 | interface IFileDownloader { 45 | getLocalFile: (url: string) => Promise, 46 | getExternalFile: (url: string) => Promise, 47 | } 48 | 49 | interface DownloadOptions { 50 | resolveDirectives: boolean, 51 | /** 52 | * These properties might be used in pre-processor directives (`#if`, etc.). They are used to resolve conditions. 53 | */ 54 | definedExpressions?: DefinedExpressions, 55 | filterOrigin?: string, 56 | validateChecksum?: boolean, 57 | validateChecksumStrict?: boolean 58 | allowEmptyResponse?: boolean 59 | } 60 | 61 | /** 62 | * These options were added separately to the download method to support backward compatibility. 63 | */ 64 | interface LegacyDownloadOptions { 65 | validateChecksum?: boolean, 66 | validateChecksumStrict?: boolean 67 | allowEmptyResponse?: boolean, 68 | } 69 | 70 | /** 71 | * Options interface for the downloadWithRaw method. 72 | */ 73 | interface DownloadWithRawOptions { 74 | /** 75 | * A boolean flag that, when set to true, indicates that the filter 76 | * should be downloaded entirely from the server without applying differential patches. By default, false. 77 | */ 78 | force?: boolean, 79 | /** 80 | * Filter before conditions and includes resolved, like it was downloaded from the server. 81 | * By default, undefined. 82 | */ 83 | rawFilter?: string, 84 | /** 85 | * Compiler options to be applied while downloading the filter. 86 | * By default, undefined. 87 | */ 88 | definedExpressions?: DefinedExpressions, 89 | 90 | /** 91 | * A boolean option for controlling the verbosity of logs. If true, it will log more detailed information during the 92 | * download process. 93 | * It is disabled (set to false) by default, but could be useful for troubleshooting and debugging. 94 | */ 95 | verbose?: boolean 96 | 97 | /** 98 | * Option to run validation of checksum of the downloaded filter. If checksum was not found in the file, it will 99 | * not throw error unless validateChecksumStrict is set to true. 100 | */ 101 | validateChecksum?: boolean 102 | 103 | /** 104 | * Whether checksum validation should throw error or no if checksum was not found in the file. 105 | * If it is true, it will throw error if checksum was not found in the file. 106 | */ 107 | validateChecksumStrict?: boolean 108 | 109 | /** 110 | * A flag to allow an empty response. 111 | * By default, it is set to false and will throw an error if the response is empty. 112 | * If set to true, it will allow download file with an empty response. 113 | */ 114 | allowEmptyResponse?: boolean 115 | } 116 | 117 | /** 118 | * This method downloads filter rules from a specified URL and applies the defined expressions to resolve conditions. 119 | */ 120 | interface DownloadInterface { 121 | ( 122 | /** 123 | * The absolute URL from which the filter is to be downloaded. 124 | */ 125 | url: string, 126 | /** 127 | * An object containing defined expressions for condition resolution. 128 | */ 129 | definedExpressions?: DefinedExpressions, 130 | /** 131 | * The options/configuration to be applied while downloading the filter. 132 | */ 133 | options?: LegacyDownloadOptions 134 | ): Promise 135 | } 136 | 137 | /** 138 | * Downloads filter rules from a specified URL, and returns both the processed filter and raw filter. 139 | * The raw filter, which is the filter before any conditions are applied or inclusions resolved, 140 | * is kept for future use, particularly for patch application. 141 | * If needed, the filter is also patched with any available patches. 142 | */ 143 | interface DownloadWithRawInterface { 144 | ( 145 | /** 146 | * The absolute URL from which the filter is to be downloaded. 147 | */ 148 | url: string, 149 | /** 150 | * The options/configuration to be applied while downloading the filter. 151 | * This is required. 152 | */ 153 | options: DownloadWithRawOptions, 154 | /** 155 | * A flag to allow an empty response. 156 | */ 157 | allowEmptyResponse?: boolean 158 | ): Promise; 159 | } 160 | 161 | /** 162 | * An interface representing defined expressions for condition resolution. 163 | * 164 | * @interface DefinedExpressions 165 | */ 166 | interface DefinedExpressions { 167 | adguard?: boolean, 168 | adguard_ext_chromium?: boolean, 169 | adguard_ext_chromium_mv3?: boolean, 170 | adguard_ext_firefox?: boolean, 171 | adguard_ext_edge?: boolean, 172 | adguard_ext_safari?: boolean, 173 | adguard_ext_opera?: boolean, 174 | adguard_ext_android_cb?: boolean 175 | } 176 | 177 | export interface DownloadResult { 178 | /** 179 | * Might be the same as raw filter if no conditions or includes were resolved. 180 | */ 181 | filter: string[]; 182 | 183 | /** 184 | * The raw filter, which is the filter before any conditions are applied or inclusions resolved. 185 | */ 186 | rawFilter: string; 187 | 188 | /** 189 | * Flag to indicate if the patch update failed. 190 | * 191 | * @see {@link https://github.com/AdguardTeam/AdguardBrowserExtension/issues/2717} 192 | */ 193 | isPatchUpdateFailed?: boolean; 194 | } 195 | 196 | /** 197 | * An interface representing a Filters Downloader, which provides methods to compile, download, 198 | * resolve conditions, resolve includes, and get the filter URL origin. 199 | * 200 | * @interface IFiltersDownloader 201 | */ 202 | interface IFiltersDownloader { 203 | compile: (rules: string[], filterOrigin?: string, expressions?: DefinedExpressions) => Promise; 204 | download: DownloadInterface; 205 | downloadWithRaw: DownloadWithRawInterface; 206 | resolveConditions: (rules: string[], expressions?: DefinedExpressions) => string[]; 207 | resolveIncludes: (rules: string[], filterOrigin?: string, expressions?: DefinedExpressions) => Promise; 208 | getFilterUrlOrigin: (url: string) => string; 209 | } 210 | 211 | const FiltersDownloaderCreator = (FileDownloadWrapper: IFileDownloader): IFiltersDownloader => { 212 | const CONDITION_IF_DIRECTIVE_START = '!#if'; 213 | const CONDITION_ELSE_DIRECTIVE_START = '!#else'; 214 | const CONDITION_DIRECTIVE_END = '!#endif'; 215 | 216 | const CONDITION_OPERATOR_NOT = '!'; 217 | const CONDITION_OPERATOR_AND = '&&'; 218 | const CONDITION_OPERATOR_OR = '||'; 219 | const CONDITION_BRACKET_OPEN_CHAR = '('; 220 | const CONDITION_BRACKET_CLOSE_CHAR = ')'; 221 | 222 | const INCLUDE_DIRECTIVE = '!#include'; 223 | 224 | const REGEXP_ABSOLUTE_URL = /^([a-z]+:\/\/|\/\/)/i; 225 | const REGEXP_EXTERNAL_ABSOLUTE_URL = /^((?!file)[a-z]+:\/\/|\/\/)/i; 226 | 227 | /** 228 | * Checks if the opening and closing brackets in a string are balanced. 229 | * 230 | * @param str The input string to check for bracket balance. 231 | * @returns Returns `true` if the brackets are balanced, `false` otherwise. 232 | */ 233 | const checkBracketsBalance = (str: string): boolean => { 234 | let depth = 0; 235 | 236 | for (let i = 0; i < str.length; i += 1) { 237 | if (str[i] === CONDITION_BRACKET_OPEN_CHAR) { 238 | // if the char is an opening parenthesis, then we increase the depth 239 | depth += 1; 240 | } else if (str[i] === CONDITION_BRACKET_CLOSE_CHAR) { 241 | // if the char is a closing parenthesis, then we decrease the depth 242 | depth -= 1; 243 | } 244 | // if the depth is negative we have a closing parenthesis 245 | // before any matching opening parenthesis 246 | if (depth < 0) { 247 | return false; 248 | } 249 | } 250 | 251 | // If the depth is not null then a closing parenthesis is missing 252 | if (depth > 0) { 253 | return false; 254 | } 255 | 256 | return true; 257 | }; 258 | 259 | /** 260 | * Parses url and returns its origin. 261 | * 262 | * @param url URL to parse. 263 | * 264 | * @returns URL origin if url can be parsed. 265 | * @throws If url cannot be parsed. 266 | */ 267 | const getUrlOrigin = (url: string): string => { 268 | try { 269 | const { origin } = new URL(url); 270 | 271 | return origin; 272 | } catch (e) { 273 | throw new Error(`Invalid url: '${url}'`); 274 | } 275 | }; 276 | 277 | /** 278 | * Get the `filterOrigin` from url for relative path resolve. 279 | * 280 | * @param url Filter file URL. 281 | * @returns Valid origin url. 282 | */ 283 | const getFilterUrlOrigin = (url: string): string => { 284 | return url.substring(0, url.lastIndexOf('/')); 285 | }; 286 | 287 | /** 288 | * Finds end of condition block started with startIndex. 289 | * 290 | * @param rules Array of all rules. 291 | * @param endDirective End directive for the condition block — `!#else` or `!#endif`. 292 | * @param startIndex Index of the start for presumed condition block. 293 | * @param endIndex Index of the end for presumed condition block, 294 | * needed for `!#else` directive if index of `!#endif` was found before to limit the search. 295 | * 296 | * @returns Index of the end of the condition block for the `endDirective`. 297 | */ 298 | const findConditionBlockEnd = ( 299 | rules: string[], 300 | endDirective: string, 301 | startIndex: number, 302 | endIndex: number, 303 | ): number => { 304 | const stack: string[] = []; 305 | 306 | for (let i = startIndex; i < endIndex; i += 1) { 307 | const rule = rules[i]; 308 | 309 | if (rule.startsWith(CONDITION_IF_DIRECTIVE_START)) { 310 | stack.push(CONDITION_IF_DIRECTIVE_START); 311 | } else if (rule.startsWith(endDirective)) { 312 | if (stack.length > 0) { 313 | stack.pop(); 314 | } else { 315 | return i; 316 | } 317 | } 318 | } 319 | 320 | return -1; 321 | }; 322 | 323 | /** 324 | * Resolves a conditional expression to a boolean value based 325 | * on defined properties. 326 | * 327 | * @param expression The conditional expression to resolve. 328 | * @param definedExpressions An object containing defined properties for evaluation. 329 | * 330 | * @throws Throws an error if the expression is empty. 331 | * 332 | * @returns Returns `true` if the expression evaluates to `true` or a defined 333 | * property exists; otherwise, returns `false`. 334 | */ 335 | const resolveConditionConstant = (expression: string, definedExpressions?: DefinedExpressions): boolean => { 336 | if (!expression) { 337 | throw new Error('Invalid directives: Empty condition'); 338 | } 339 | 340 | const trimmedExpression = expression.trim(); 341 | 342 | if (trimmedExpression === 'true') { 343 | return true; 344 | } 345 | 346 | const expressionAsKey = trimmedExpression as keyof DefinedExpressions; 347 | if (definedExpressions?.[expressionAsKey] === true) { 348 | return true; 349 | } 350 | 351 | return false; 352 | }; 353 | 354 | /** 355 | * Resolves a conditional expression and returns whether the expression 356 | * evaluates to `true` or `false` based on defined properties. 357 | * 358 | * @param rawExpression The raw conditional expression to resolve. 359 | * @param definedProperties An object containing defined expressions for 360 | * condition resolution. 361 | * 362 | * @returns Boolean `true` if the expression evaluates to `true`, 363 | * `false` otherwise. 364 | * 365 | * @throws Error If the expression is empty or contains incorrect brackets. 366 | */ 367 | const resolveExpression = (rawExpression: string, definedProperties?: DefinedExpressions): boolean => { 368 | if (!rawExpression) { 369 | throw new Error('Invalid directives: Empty condition'); 370 | } 371 | 372 | const expression = rawExpression.trim(); 373 | 374 | if (!checkBracketsBalance(expression)) { 375 | throw new Error(`Invalid directives: Incorrect brackets: ${expression}`); 376 | } 377 | 378 | // Replace bracketed expressions 379 | const openBracketIndex = expression.lastIndexOf(CONDITION_BRACKET_OPEN_CHAR); 380 | if (openBracketIndex !== -1) { 381 | const endBracketIndex = expression.indexOf(CONDITION_BRACKET_CLOSE_CHAR, openBracketIndex); 382 | const innerExpression = expression.substring(openBracketIndex + 1, endBracketIndex); 383 | const innerResult = resolveExpression(innerExpression, definedProperties); 384 | const resolvedInner = expression.substring(0, openBracketIndex) 385 | + innerResult + expression.substring(endBracketIndex + 1); 386 | 387 | return resolveExpression(resolvedInner, definedProperties); 388 | } 389 | 390 | let result; 391 | 392 | // Resolve logical operators 393 | const indexOfAndOperator = expression.indexOf(CONDITION_OPERATOR_AND); 394 | const indexOfOrOperator = expression.indexOf(CONDITION_OPERATOR_OR); 395 | const indexOfNotOperator = expression.indexOf(CONDITION_OPERATOR_NOT); 396 | 397 | if (indexOfOrOperator !== -1) { 398 | result = resolveExpression( 399 | expression.substring(0, indexOfOrOperator - 1), 400 | definedProperties, 401 | ) || resolveExpression( 402 | expression.substring(indexOfOrOperator + CONDITION_OPERATOR_OR.length, expression.length), 403 | definedProperties, 404 | ); 405 | } else if (indexOfAndOperator !== -1) { 406 | result = resolveExpression( 407 | expression.substring(0, indexOfAndOperator - 1), 408 | definedProperties, 409 | ) && resolveExpression( 410 | expression.substring(indexOfAndOperator + CONDITION_OPERATOR_AND.length, expression.length), 411 | definedProperties, 412 | ); 413 | } else if (indexOfNotOperator === 0) { 414 | result = !resolveExpression(expression.substring(CONDITION_OPERATOR_NOT.length), definedProperties); 415 | } else { 416 | result = resolveConditionConstant(expression, definedProperties); 417 | } 418 | 419 | return result; 420 | }; 421 | 422 | /** 423 | * Resolves a conditional directive and returns whether the condition is true based on defined properties. 424 | * 425 | * @param directive The conditional directive to resolve. 426 | * @param definedProperties An object containing defined expressions for condition resolution. 427 | * @returns `true` if the condition is true, `false` otherwise. 428 | */ 429 | const resolveCondition = (directive: string, definedProperties?: DefinedExpressions): boolean => { 430 | const expression = directive.substring(CONDITION_IF_DIRECTIVE_START.length).trim(); 431 | 432 | return resolveExpression(expression, definedProperties); 433 | }; 434 | 435 | /** 436 | * Resolves conditional directives in a list of filtering rules based on 437 | * defined properties. 438 | * 439 | * @param rules The list of filtering rules to resolve. 440 | * @param definedExpressions An object containing defined expressions for 441 | * condition resolution. 442 | * @param urlOrigin The origin of the URL from which the filter was downloaded. 443 | * 444 | * @returns The resolved filtering rules after processing conditional directives. 445 | * 446 | * @throws Throws an error if invalid conditional directives are encountered. 447 | */ 448 | const resolveConditions = ( 449 | rules: string[], 450 | definedExpressions?: DefinedExpressions, 451 | urlOrigin?: string, 452 | ): string[] => { 453 | if (!definedExpressions) { 454 | return rules; 455 | } 456 | 457 | let result: string[] = []; 458 | 459 | for (let i = 0; i < rules.length; i += 1) { 460 | const rule = rules[i]; 461 | 462 | if (rule.indexOf(CONDITION_IF_DIRECTIVE_START) === 0) { 463 | const endLineIndex = findConditionBlockEnd( 464 | rules, 465 | CONDITION_DIRECTIVE_END, 466 | i + 1, 467 | rules.length, 468 | ); 469 | if (endLineIndex === -1) { 470 | throwError( 471 | 'Invalid directives: Condition end not found', 472 | rule, 473 | getContext(rules, i), 474 | urlOrigin, 475 | ); 476 | } 477 | 478 | const elseLineIndex = findConditionBlockEnd( 479 | rules, 480 | CONDITION_ELSE_DIRECTIVE_START, 481 | i + 1, 482 | endLineIndex, 483 | ); 484 | const isConditionMatched = resolveCondition(rule, definedExpressions); 485 | 486 | // if there is no 'else' branch for the condition 487 | if (elseLineIndex === -1) { 488 | if (isConditionMatched) { 489 | const rulesUnderCondition = rules.slice(i + 1, endLineIndex); 490 | // Resolve inner conditions in recursion 491 | result = result 492 | .concat(resolveConditions(rulesUnderCondition, definedExpressions, urlOrigin)); 493 | } 494 | } else { 495 | // check if there is something after !#else 496 | if (rules[elseLineIndex].trim().length !== CONDITION_ELSE_DIRECTIVE_START.length) { 497 | throwError( 498 | 'Found invalid directive !#else', 499 | rule, 500 | getContext(rules, i), 501 | urlOrigin, 502 | ); 503 | } 504 | 505 | if (isConditionMatched) { 506 | const rulesForConditionTrue = rules.slice(i + 1, elseLineIndex); 507 | // Resolve inner conditions in recursion 508 | result = result 509 | .concat(resolveConditions(rulesForConditionTrue, definedExpressions, urlOrigin)); 510 | } else { 511 | const rulesForConditionFalse = rules.slice(elseLineIndex + 1, endLineIndex); 512 | // Resolve inner conditions in recursion 513 | result = result.concat(resolveConditions( 514 | rulesForConditionFalse, 515 | definedExpressions, 516 | urlOrigin, 517 | )); 518 | } 519 | } 520 | 521 | // Skip to the end of block 522 | i = endLineIndex; 523 | } else if (rule.indexOf(CONDITION_ELSE_DIRECTIVE_START) === 0) { 524 | // Found !#else without !#if 525 | throwError( 526 | 'Found unexpected condition else branch:', 527 | rule, 528 | getContext(rules, i), 529 | urlOrigin, 530 | ); 531 | } else if (rule.indexOf(CONDITION_DIRECTIVE_END) === 0) { 532 | // Found !#endif without !#if 533 | throwError( 534 | 'Found unexpected condition end:', 535 | rule, 536 | getContext(rules, i), 537 | urlOrigin, 538 | ); 539 | } else { 540 | result.push(rule); 541 | } 542 | } 543 | 544 | return result; 545 | }; 546 | 547 | /** 548 | * Validates a URL to ensure it matches the expected origin. 549 | * 550 | * @param url The URL to validate. 551 | * @param filterUrlOrigin The expected origin URL. If provided, the function 552 | * checks if the URL matches this origin. 553 | * @throws Throws an error if the URL is absolute and its origin doesn't 554 | * match the expected origin. 555 | */ 556 | const validateUrl = (url: string, filterUrlOrigin?: string): void => { 557 | if (filterUrlOrigin) { 558 | if (REGEXP_ABSOLUTE_URL.test(url)) { 559 | // Include url is absolute 560 | const urlOrigin = getUrlOrigin(url); 561 | const filterOrigin = getUrlOrigin(filterUrlOrigin); 562 | if (urlOrigin !== filterOrigin) { 563 | throw new Error(`Include url is rejected with origin: ${urlOrigin}`); 564 | } 565 | } 566 | } 567 | }; 568 | 569 | /** 570 | * Validates and resolves include directive. 571 | * 572 | * @param line Line with directive. 573 | * @param filterOrigin Filter file URL origin or undefined. 574 | * @param definedExpressions An object with the defined properties. 575 | * @param context Previous lines for better error messages. 576 | * These properties might be used in pre-processor directives (`#if`, etc.). 577 | * 578 | * @returns A promise that returns string with rules if resolved and Error if rejected. 579 | * @throws Error if failed to resolve the include directive. 580 | */ 581 | const resolveInclude = async ( 582 | line: string, 583 | filterOrigin?: string, 584 | definedExpressions?: DefinedExpressions, 585 | context?: string, 586 | ): Promise => { 587 | if (line.indexOf(INCLUDE_DIRECTIVE) !== 0) { 588 | return Promise.resolve(line); 589 | } 590 | const url = line.substring(INCLUDE_DIRECTIVE.length).trim(); 591 | validateUrl(url, filterOrigin); 592 | 593 | let filter: string[] = []; 594 | try { 595 | // eslint-disable-next-line @typescript-eslint/no-use-before-define 596 | const downloadResult = await downloadFilterRules(url, { 597 | filterOrigin, 598 | definedExpressions, 599 | resolveDirectives: true, 600 | }); 601 | filter = downloadResult.filter; 602 | } catch (error: unknown) { 603 | throwError( 604 | 'Failed to resolve the include directive', 605 | line, 606 | context, 607 | filterOrigin, 608 | error, 609 | ); 610 | } 611 | 612 | const MAX_LINES_TO_SCAN = 50; 613 | // Math.min inside for loop, because filter.length changes 614 | for (let i = 0; i < Math.min(MAX_LINES_TO_SCAN, filter.length); i += 1) { 615 | if (filter[i].trim().startsWith('! Diff-Path:')) { 616 | filter.splice(i, 1); 617 | } 618 | } 619 | 620 | return filter; 621 | }; 622 | 623 | /** 624 | * Resolves include directives. 625 | * 626 | * @param rules Array of rules. 627 | * @param filterOrigin Filter file URL origin or null. 628 | * @param definedExpressions An object with the defined expressions for conditions resolution. 629 | * These properties might be used in pre-processor directives (`#if`, etc.). 630 | * @returns A promise that returns {string} with rules when if resolved and {Error} if rejected. 631 | */ 632 | const resolveIncludes = async ( 633 | rules: string[], 634 | filterOrigin?: string, 635 | definedExpressions?: DefinedExpressions, 636 | ): Promise => { 637 | const promises = rules.map((rule, index) => { 638 | return resolveInclude(rule, filterOrigin, definedExpressions, getContext(rules, index)); 639 | }); 640 | 641 | let result: string[] = []; 642 | // We do not use here Promise.all because it freezes the Chromium browsers and electron built on it, if there 643 | // are more than 1_100_00 promises. Also, we consider that we can afford promises to be resolved sequentially. 644 | for (let i = 0; i < promises.length; i += 1) { 645 | // eslint-disable-next-line no-await-in-loop 646 | const resolved = await promises[i]; 647 | if (Array.isArray(resolved)) { 648 | result = result.concat(resolved); 649 | } else { 650 | result.push(resolved); 651 | } 652 | } 653 | 654 | return result; 655 | }; 656 | 657 | /** 658 | * Splits filter by lines. 659 | * @param filter Filter to split. 660 | * @returns Array of strings. 661 | */ 662 | const splitFilter = (filter: string): string[] => { 663 | return filter.trim().split(/[\r\n]+/); 664 | }; 665 | 666 | /** 667 | * Downloads filter rules from an external URL or a local path and resolves 668 | * pre-processor directives. 669 | * 670 | * @param url Filter file absolute URL or relative path. 671 | * @param downloadOptions Options to be applied while downloading the filter. 672 | * 673 | * @returns A promise that returns an array of strings with rules when 674 | * resolved or an Error if rejected. 675 | * @throws Error if validateChecksum flag is true and checksum is invalid. 676 | */ 677 | const externalDownload = async ( 678 | url: string, 679 | downloadOptions?: DownloadOptions, 680 | ): Promise => { 681 | const filterUrlOrigin = downloadOptions?.filterOrigin; 682 | 683 | const filterUrl = !REGEXP_ABSOLUTE_URL.test(url) && REGEXP_ABSOLUTE_URL.test(filterUrlOrigin || '') 684 | // getting absolute url for external file with relative url 685 | ? `${filterUrlOrigin}/${url}` 686 | : url; 687 | 688 | const rawFilter = await FileDownloadWrapper.getExternalFile(filterUrl); 689 | 690 | if (downloadOptions && downloadOptions.validateChecksum) { 691 | if (!isValidChecksum(rawFilter, downloadOptions.validateChecksumStrict)) { 692 | throw new Error('Invalid checksum'); 693 | } 694 | } 695 | 696 | const filter = splitFilter(rawFilter); 697 | 698 | if (!downloadOptions?.resolveDirectives) { 699 | return { 700 | filter, 701 | rawFilter, 702 | }; 703 | } 704 | 705 | const urlOrigin = getFilterUrlOrigin(filterUrl); 706 | const conditionsResult = resolveConditions(filter, downloadOptions.definedExpressions, urlOrigin); 707 | const includesResult = await resolveIncludes( 708 | conditionsResult, 709 | urlOrigin, 710 | downloadOptions.definedExpressions, 711 | ); 712 | 713 | return { 714 | filter: includesResult, 715 | rawFilter, 716 | }; 717 | }; 718 | 719 | /** 720 | * Compiles filter content. 721 | * 722 | * @param rules Array of strings. 723 | * @param filterOrigin Filter file URL origin or null. 724 | * @param definedProperties An object with the defined properties. 725 | * These properties might be used in pre-processor directives (`#if`, etc.). 726 | * @returns A promise that returns {string} with rules when if resolved and {Error} if rejected. 727 | */ 728 | const compile = ( 729 | rules: string[], 730 | filterOrigin?: string, 731 | definedProperties?: DefinedExpressions, 732 | ): Promise => { 733 | // Resolve 'if' conditions 734 | const resolvedConditionsResult = resolveConditions(rules, definedProperties, filterOrigin); 735 | 736 | // Resolve 'includes' directives 737 | return resolveIncludes(resolvedConditionsResult, filterOrigin, definedProperties); 738 | }; 739 | 740 | /** 741 | * Gets filter rules from a local path and resolves pre-processor directives. 742 | * 743 | * @param url Path to the local file. 744 | * @param downloadOptions Options to be applied while downloading the filter. 745 | * @returns A promise that returns an array of strings with rules when resolved or an Error if rejected. 746 | * @throws Error if validateChecksum flag is true and checksum is invalid. 747 | */ 748 | const getLocalFile = async ( 749 | url: string, 750 | downloadOptions: DownloadOptions, 751 | ): Promise => { 752 | const { filterOrigin } = downloadOptions; 753 | 754 | const urlToLoad = filterOrigin 755 | ? `${filterOrigin}/${url}` 756 | : url; 757 | 758 | const rawFilter = await FileDownloadWrapper.getLocalFile(urlToLoad); 759 | 760 | if (downloadOptions && downloadOptions.validateChecksum) { 761 | if (!isValidChecksum(rawFilter, downloadOptions.validateChecksumStrict)) { 762 | throw new Error('Invalid checksum'); 763 | } 764 | } 765 | 766 | const filterContent = splitFilter(rawFilter); 767 | 768 | if (!downloadOptions?.resolveDirectives) { 769 | return { 770 | filter: filterContent, 771 | rawFilter, 772 | }; 773 | } 774 | 775 | const urlOrigin = getFilterUrlOrigin(urlToLoad); 776 | // Resolve 'if' conditions and 'includes' directives 777 | const conditionsResult = resolveConditions(filterContent, downloadOptions.definedExpressions, urlOrigin); 778 | const includesResult = await resolveIncludes(conditionsResult, urlOrigin, downloadOptions.definedExpressions); 779 | return { 780 | filter: includesResult, 781 | rawFilter, 782 | }; 783 | }; 784 | 785 | /** 786 | * Downloads filter rules from a URL and resolves pre-processor directives. 787 | * 788 | * @param url Filter file URL. 789 | * @param downloadOptions Options to be applied while downloading the filter. 790 | * These properties might be used in pre-processor directives (`#if`, etc.). 791 | * 792 | * @returns A promise that returns an array of strings with rules when 793 | * resolved or an Error if rejected. 794 | */ 795 | const downloadFilterRules = ( 796 | url: string, 797 | downloadOptions: DownloadOptions, 798 | ): Promise => { 799 | if ( 800 | REGEXP_EXTERNAL_ABSOLUTE_URL.test(url) 801 | || REGEXP_EXTERNAL_ABSOLUTE_URL.test(downloadOptions.filterOrigin || '') 802 | ) { 803 | return externalDownload(url, downloadOptions); 804 | } 805 | return getLocalFile(url, downloadOptions); 806 | }; 807 | 808 | /** 809 | * Downloads a specified filter and resolves all the pre-processor directives from there. 810 | * 811 | * @param url The URL of the filter to download. 812 | * @param definedExpressions An object with the defined properties. 813 | * These properties might be used in pre-processor directives (`#if`, etc.). 814 | * @param options Options to be applied while downloading the filter. 815 | * 816 | * @returns A promise that resolves with a list of rules and rejects with an error if unable to download. 817 | * @throws Error if validateChecksum flag is true and checksum is invalid. 818 | */ 819 | const download = async ( 820 | url: string, 821 | definedExpressions?: DefinedExpressions, 822 | options?: LegacyDownloadOptions, 823 | ): Promise => { 824 | let filterUrlOrigin; 825 | if (url && REGEXP_EXTERNAL_ABSOLUTE_URL.test(url)) { 826 | filterUrlOrigin = getFilterUrlOrigin(url); 827 | } 828 | 829 | const result = await downloadFilterRules(url, { 830 | filterOrigin: filterUrlOrigin, 831 | definedExpressions, 832 | resolveDirectives: true, 833 | validateChecksum: options?.validateChecksum, 834 | validateChecksumStrict: options?.validateChecksumStrict, 835 | }); 836 | 837 | // if 'allowEmptyResponse' option is not set, an empty filter will not be loaded. 838 | // only included filters can be empty, in this case. 839 | // if 'allowEmptyResponse' option is set, the empty file will be downloaded. 840 | if ( 841 | result.filter 842 | && result.filter.join().trim() === '' 843 | && !options?.allowEmptyResponse 844 | ) { 845 | throw new Error('Response is empty'); 846 | } 847 | 848 | return result.filter; 849 | }; 850 | 851 | /** 852 | * Resolves conditions and includes based on the provided raw filter, options, and filter URL origin. 853 | * 854 | * @param rawFilter The raw filter to be resolved. 855 | * @param options The options used in the resolution process. 856 | * @param filterUrlOrigin The origin of the filter URL. 857 | * 858 | * @returns A Promise that resolves to the result of resolving the includes. 859 | */ 860 | async function resolveConditionsAndIncludes( 861 | rawFilter: string, 862 | options: DownloadWithRawOptions, 863 | filterUrlOrigin?: string, 864 | ): Promise { 865 | const filter = splitFilter(rawFilter); 866 | const resolvedConditionsResult = resolveConditions(filter, options.definedExpressions, filterUrlOrigin); 867 | return resolveIncludes( 868 | resolvedConditionsResult, 869 | filterUrlOrigin, 870 | options.definedExpressions, 871 | ); 872 | } 873 | 874 | /** 875 | * Downloads filter rules from a URL and resolves pre-processor directives. 876 | * 877 | * @param url Filter file URL. 878 | * @param options Options to be applied while downloading the filter. 879 | * 880 | * @returns A promise that returns an array of strings with rules. 881 | */ 882 | async function downloadAndProcess(url: string, options: DownloadWithRawOptions): Promise { 883 | let filterUrlOrigin; 884 | if (url && REGEXP_EXTERNAL_ABSOLUTE_URL.test(url)) { 885 | filterUrlOrigin = getFilterUrlOrigin(url); 886 | } 887 | 888 | const result = await downloadFilterRules(url, { 889 | filterOrigin: filterUrlOrigin, 890 | definedExpressions: options.definedExpressions, 891 | // `false` for not resolving directives 892 | resolveDirectives: false, 893 | validateChecksum: options.validateChecksum, 894 | validateChecksumStrict: options.validateChecksumStrict, 895 | }); 896 | 897 | // if 'allowEmptyResponse' option is not set, an empty filter will not be loaded. 898 | // only included filters can be empty, in this case. 899 | // if 'allowEmptyResponse' option is set, the empty file will be downloaded. 900 | if ( 901 | result.filter 902 | && result.filter.join().trim() === '' 903 | && !options?.allowEmptyResponse 904 | ) { 905 | throw new Error('Response is empty'); 906 | } 907 | 908 | const includesResult = await resolveConditionsAndIncludes(result.rawFilter, options, filterUrlOrigin); 909 | 910 | return { 911 | filter: includesResult, 912 | rawFilter: result.rawFilter, 913 | }; 914 | } 915 | 916 | /** 917 | * Downloads filter rules from a URL without resolving pre-processor directives. 918 | * 919 | * @param url Filter file URL. 920 | * @param options Options to be applied while downloading the filter. 921 | * 922 | * @returns A promise that returns an array of strings with rules when 923 | * resolved or an Error if rejected. 924 | * 925 | * @throws An error if 926 | * - validateChecksum flag is true and checksum is invalid; 927 | * - DiffUpdater.applyPatch() fails and the thrown error is not {@link UnacceptableResponseError}. 928 | */ 929 | const downloadWithRaw: DownloadWithRawInterface = async (url, options) => { 930 | options.verbose ??= false; 931 | options.force ??= false; 932 | 933 | let filterUrlOrigin; 934 | if (url && REGEXP_EXTERNAL_ABSOLUTE_URL.test(url)) { 935 | filterUrlOrigin = getFilterUrlOrigin(url); 936 | } 937 | 938 | // if options.force, then download the whole filter 939 | // if !options.rawFilter, then return result as is, since it is not possible to apply 940 | // patches without the raw filter 941 | if (options.force || !options.rawFilter) { 942 | return downloadAndProcess(url, options); 943 | } 944 | 945 | let rawFilter: string | null = ''; 946 | 947 | try { 948 | rawFilter = await DiffUpdater.applyPatch({ 949 | filterUrl: url, 950 | filterContent: options.rawFilter, 951 | verbose: options.verbose, 952 | }); 953 | } catch (e) { 954 | if (e instanceof UnacceptableResponseError) { 955 | return { 956 | filter: splitFilter(options.rawFilter), 957 | rawFilter: options.rawFilter, 958 | isPatchUpdateFailed: true, 959 | }; 960 | } 961 | 962 | // if the error is not UnacceptableResponseError, then rethrow it further 963 | throw e; 964 | } 965 | 966 | // applyPatch returns null if there is no Diff-Path in the filter metadata 967 | if (rawFilter === null) { 968 | const downloadResult = await downloadAndProcess(url, options); 969 | return downloadResult; 970 | } 971 | 972 | // if nothing changed, then return result as is 973 | if (rawFilter === options.rawFilter) { 974 | return { 975 | filter: splitFilter(options.rawFilter), 976 | rawFilter: options.rawFilter, 977 | }; 978 | } 979 | 980 | const resolveResult = await resolveConditionsAndIncludes( 981 | rawFilter, 982 | options, 983 | filterUrlOrigin, 984 | ); 985 | 986 | return { 987 | filter: resolveResult, 988 | rawFilter, 989 | }; 990 | }; 991 | 992 | return { 993 | compile, 994 | download, 995 | downloadWithRaw, 996 | resolveConditions, 997 | resolveIncludes, 998 | getFilterUrlOrigin, 999 | }; 1000 | }; 1001 | 1002 | export { 1003 | IFiltersDownloader, 1004 | DefinedExpressions, 1005 | FiltersDownloaderCreator, 1006 | }; 1007 | -------------------------------------------------------------------------------- /_qunit_tests_/filters-downloader.test.ts: -------------------------------------------------------------------------------- 1 | // TODO rewrite this tests to jest 2 | /* eslint-disable no-console */ 3 | /* eslint-disable global-require */ 4 | const mockFs = require('mock-fs'); 5 | 6 | const FilterCompilerConditionsConstants = { 7 | adguard: true, 8 | adguard_ext_chromium: true, 9 | adguard_ext_firefox: false, 10 | adguard_ext_edge: false, 11 | adguard_ext_safari: false, 12 | adguard_ext_opera: false, 13 | adguard_ext_android_cb: false, 14 | }; 15 | 16 | // eslint-disable-next-line max-len 17 | const TEST_FILTER_LIST_BASE = 'https://raw.githubusercontent.com/AdguardTeam/FiltersDownloader/test-resources/__tests__/resources'; 18 | 19 | // TODO remove test files and use local server for external downloading tests 20 | const URL0 = `${TEST_FILTER_LIST_BASE}/rules.txt`; 21 | const URL1 = `${TEST_FILTER_LIST_BASE}/rules_simple_include.txt`; 22 | const URL2 = `${TEST_FILTER_LIST_BASE}/rules_nested_subdir_includes.txt`; 23 | const URL3 = `${TEST_FILTER_LIST_BASE}/test-filter.txt`; 24 | const URL4 = `${TEST_FILTER_LIST_BASE}/rules_conditional_includes.txt`; 25 | const URL_EMPTY = `${TEST_FILTER_LIST_BASE}/empty-filter.txt`; 26 | const URL_INCLUDES_EMPTY = `${TEST_FILTER_LIST_BASE}/includes_empty_filter.txt`; 27 | 28 | // eslint-disable-next-line max-len 29 | const URL404 = 'https://raw.githubusercontent.com/AdguardTeam/FiltersDownloader/test-resources/__test__/resources/blabla.txt'; 30 | 31 | /** 32 | * Checks the result of `FiltersDownloader.resolveConditions()` for the `actual` rules 33 | * with the {@link FilterCompilerConditionsConstants} config 34 | * against the `expected` rules. 35 | * 36 | * @param {object} FiltersDownloader FiltersDownloader object with resolveConditions(). 37 | * @param {string[]} actual Input rules. 38 | * @param {string[]} expected Expected output rules. 39 | * @param assert Assert. 40 | */ 41 | const expectResolvedConditionsRules = ( 42 | FiltersDownloader: any, 43 | actual: string[], 44 | expected: string[], 45 | assert: any, 46 | ): void => { 47 | const compiled = FiltersDownloader.resolveConditions(actual, FilterCompilerConditionsConstants); 48 | assert.ok(compiled); 49 | assert.equal(compiled.length, expected.length); 50 | assert.deepEqual(compiled, expected); 51 | }; 52 | 53 | QUnit.test('Test filter downloader', async (assert) => { 54 | const { FiltersDownloader } = require('../dist'); 55 | assert.ok(FiltersDownloader); 56 | 57 | const compiled = await FiltersDownloader.download(URL0, FilterCompilerConditionsConstants); 58 | 59 | assert.ok(compiled); 60 | assert.equal(compiled.length, 1); 61 | assert.equal(compiled[0], 'test'); 62 | }); 63 | 64 | QUnit.test('Test filter download from external resource with relative url', async (assert) => { 65 | const { FiltersDownloader } = require('../dist'); 66 | assert.ok(FiltersDownloader); 67 | 68 | let compiled; 69 | 70 | try { 71 | compiled = await FiltersDownloader.download(URL1, FilterCompilerConditionsConstants); 72 | } catch (e) { 73 | console.log((e as Error).message); 74 | } 75 | 76 | assert.ok(compiled); 77 | assert.equal(compiled.length, 2); 78 | assert.equal(compiled[compiled.length - 1], 'test'); 79 | }); 80 | 81 | QUnit.test('Test filter download from external resource with relative url and subdir', async (assert) => { 82 | const { FiltersDownloader } = require('../dist'); 83 | assert.ok(FiltersDownloader); 84 | 85 | let compiled; 86 | 87 | try { 88 | compiled = await FiltersDownloader.download(URL2, FilterCompilerConditionsConstants); 89 | } catch (e) { 90 | console.log((e as Error).message); 91 | } 92 | 93 | assert.ok(compiled); 94 | assert.equal(compiled.length, 3); 95 | assert.equal(compiled[0], 'test_parent'); 96 | assert.equal(compiled[1], 'sub_test_main'); 97 | assert.equal(compiled[2], 'sub_test'); 98 | }); 99 | 100 | QUnit.test('Test that not found link returns error ', async (assert) => { 101 | const { FiltersDownloader } = require('../dist'); 102 | assert.ok(FiltersDownloader); 103 | let compiled; 104 | try { 105 | compiled = await FiltersDownloader.download(URL404, FilterCompilerConditionsConstants); 106 | } catch (e) { 107 | assert.equal((e as Error).message, `Response status for url ${URL404} is invalid: 404`); 108 | } 109 | 110 | assert.notOk(compiled); 111 | }); 112 | 113 | QUnit.test('Test filter downloader - simple "if" conditions', async (assert) => { 114 | let rules; 115 | let compiled; 116 | 117 | rules = [ 118 | 'always_included_rule', 119 | '!#if (adguard)', 120 | 'if_adguard_included_rule', 121 | '!#endif', 122 | ]; 123 | 124 | const { FiltersDownloader } = require('../dist'); 125 | assert.ok(FiltersDownloader); 126 | 127 | compiled = FiltersDownloader.resolveConditions(rules, FilterCompilerConditionsConstants); 128 | assert.ok(compiled); 129 | assert.equal(compiled.length, 2); 130 | assert.equal(compiled[0], 'always_included_rule'); 131 | assert.equal(compiled[1], 'if_adguard_included_rule'); 132 | 133 | rules = [ 134 | 'always_included_rule', 135 | '!#if !adguard', 136 | 'if_adguard_included_rule', 137 | '!#endif', 138 | ]; 139 | 140 | compiled = FiltersDownloader.resolveConditions(rules, FilterCompilerConditionsConstants); 141 | assert.ok(compiled); 142 | assert.equal(compiled.length, 1); 143 | assert.equal(compiled[0], 'always_included_rule'); 144 | 145 | rules = [ 146 | 'always_included_rule', 147 | '!#if !!adguard', 148 | 'if_adguard_included_rule', 149 | '!#endif', 150 | ]; 151 | 152 | compiled = FiltersDownloader.resolveConditions(rules, FilterCompilerConditionsConstants); 153 | assert.ok(compiled); 154 | assert.equal(compiled.length, 2); 155 | assert.equal(compiled[0], 'always_included_rule'); 156 | assert.equal(compiled[1], 'if_adguard_included_rule'); 157 | 158 | rules = [ 159 | 'always_included_rule', 160 | '!#if true', 161 | 'if_adguard_included_rule', 162 | '!#endif', 163 | ]; 164 | 165 | compiled = FiltersDownloader.resolveConditions(rules, FilterCompilerConditionsConstants); 166 | assert.ok(compiled); 167 | assert.equal(compiled.length, 2); 168 | assert.equal(compiled[0], 'always_included_rule'); 169 | assert.equal(compiled[1], 'if_adguard_included_rule'); 170 | 171 | rules = [ 172 | 'always_included_rule', 173 | '!#if adguard', 174 | 'if_adguard_included_rule', 175 | '!#endif', 176 | '!#if adguard_ext_chromium', 177 | 'if_adguard_ext_chromium_included_rule', 178 | '!#endif', 179 | ]; 180 | 181 | compiled = FiltersDownloader.resolveConditions(rules, FilterCompilerConditionsConstants); 182 | assert.ok(compiled); 183 | assert.equal(compiled.length, 3); 184 | assert.equal(compiled[0], 'always_included_rule'); 185 | assert.equal(compiled[1], 'if_adguard_included_rule'); 186 | assert.equal(compiled[2], 'if_adguard_ext_chromium_included_rule'); 187 | }); 188 | 189 | QUnit.test('Test filter downloader - unsupported conditions', async (assert) => { 190 | const rules = [ 191 | 'test', 192 | '!#if smth', 193 | 'if_adguard_included_rule', 194 | '!#endif', 195 | ]; 196 | 197 | const { FiltersDownloader } = require('../dist'); 198 | assert.ok(FiltersDownloader); 199 | 200 | const compiled = FiltersDownloader.resolveConditions(rules, FilterCompilerConditionsConstants); 201 | assert.ok(compiled); 202 | assert.equal(compiled.length, 1); 203 | assert.equal(compiled[0], 'test'); 204 | }); 205 | 206 | QUnit.test('Test filter downloader - logical "if" conditions', async (assert) => { 207 | let rules; 208 | let compiled; 209 | 210 | // true && true = true 211 | rules = [ 212 | 'always_included_rule', 213 | '!#if adguard && adguard_ext_chromium', 214 | 'if_adguard_included_rule', 215 | '!#endif', 216 | ]; 217 | 218 | const { FiltersDownloader } = require('../dist'); 219 | assert.ok(FiltersDownloader); 220 | 221 | compiled = FiltersDownloader.resolveConditions(rules, FilterCompilerConditionsConstants); 222 | assert.ok(compiled); 223 | assert.equal(compiled.length, 2); 224 | assert.equal(compiled[0], 'always_included_rule'); 225 | assert.equal(compiled[1], 'if_adguard_included_rule'); 226 | 227 | // true && false = false 228 | rules = [ 229 | 'always_included_rule', 230 | '!#if adguard && adguard_ext_opera', 231 | 'if_adguard_ext_opera_included_rule', 232 | '!#endif', 233 | ]; 234 | 235 | compiled = FiltersDownloader.resolveConditions(rules, FilterCompilerConditionsConstants); 236 | assert.ok(compiled); 237 | assert.equal(compiled.length, 1); 238 | assert.equal(compiled[0], 'always_included_rule'); 239 | 240 | // true || false = true 241 | rules = [ 242 | 'always_included_rule', 243 | '!#if adguard || adguard_ext_opera', 244 | 'if_adguard_included_rule', 245 | '!#endif', 246 | ]; 247 | 248 | compiled = FiltersDownloader.resolveConditions(rules, FilterCompilerConditionsConstants); 249 | assert.ok(compiled); 250 | assert.equal(compiled.length, 2); 251 | assert.equal(compiled[0], 'always_included_rule'); 252 | assert.equal(compiled[1], 'if_adguard_included_rule'); 253 | 254 | // true && false || true = true 255 | rules = [ 256 | 'always_included_rule', 257 | '!#if adguard && adguard_ext_opera || adguard_ext_chromium', 258 | 'if_adguard_included_rule', 259 | '!#endif', 260 | ]; 261 | 262 | compiled = FiltersDownloader.resolveConditions(rules, FilterCompilerConditionsConstants); 263 | assert.ok(compiled); 264 | assert.equal(compiled.length, 2); 265 | assert.equal(compiled[0], 'always_included_rule'); 266 | assert.equal(compiled[1], 'if_adguard_included_rule'); 267 | 268 | // true && false && true = false 269 | rules = [ 270 | 'always_included_rule', 271 | '!#if adguard && adguard_ext_opera && adguard_ext_chromium', 272 | 'if_adguard_included_rule', 273 | '!#endif', 274 | ]; 275 | 276 | compiled = FiltersDownloader.resolveConditions(rules, FilterCompilerConditionsConstants); 277 | assert.ok(compiled); 278 | assert.equal(compiled.length, 1); 279 | assert.equal(compiled[0], 'always_included_rule'); 280 | 281 | // false && true && true = false 282 | rules = [ 283 | 'always_included_rule', 284 | '!#if adguard_ext_opera && adguard && adguard_ext_chromium', 285 | 'if_adguard_included_rule', 286 | '!#endif', 287 | ]; 288 | 289 | compiled = FiltersDownloader.resolveConditions(rules, FilterCompilerConditionsConstants); 290 | assert.ok(compiled); 291 | assert.equal(compiled.length, 1); 292 | assert.equal(compiled[0], 'always_included_rule'); 293 | 294 | // false && true || true = true 295 | rules = [ 296 | 'always_included_rule', 297 | '!#if adguard_ext_opera && adguard || adguard_ext_chromium', 298 | 'if_adguard_included_rule', 299 | '!#endif', 300 | ]; 301 | 302 | compiled = FiltersDownloader.resolveConditions(rules, FilterCompilerConditionsConstants); 303 | assert.ok(compiled); 304 | assert.equal(compiled.length, 2); 305 | assert.equal(compiled[0], 'always_included_rule'); 306 | assert.equal(compiled[1], 'if_adguard_included_rule'); 307 | 308 | // false || true && true = true 309 | rules = [ 310 | 'always_included_rule', 311 | '!#if adguard_ext_opera || adguard && adguard_ext_chromium', 312 | 'if_adguard_included_rule', 313 | '!#endif', 314 | ]; 315 | 316 | compiled = FiltersDownloader.resolveConditions(rules, FilterCompilerConditionsConstants); 317 | assert.ok(compiled); 318 | assert.equal(compiled.length, 2); 319 | assert.equal(compiled[0], 'always_included_rule'); 320 | assert.equal(compiled[1], 'if_adguard_included_rule'); 321 | 322 | // false && false || true = true 323 | rules = [ 324 | 'always_included_rule', 325 | '!#if adguard_ext_opera && adguard_ext_firefox || adguard_ext_chromium', 326 | 'if_adguard_included_rule', 327 | '!#endif', 328 | ]; 329 | 330 | compiled = FiltersDownloader.resolveConditions(rules, FilterCompilerConditionsConstants); 331 | assert.ok(compiled); 332 | assert.equal(compiled.length, 2); 333 | assert.equal(compiled[0], 'always_included_rule'); 334 | assert.equal(compiled[1], 'if_adguard_included_rule'); 335 | 336 | // false && true && true = false 337 | rules = [ 338 | 'always_included_rule', 339 | '!#if adguard_ext_opera && adguard && adguard_ext_chromium', 340 | 'if_adguard_included_rule', 341 | '!#endif', 342 | ]; 343 | 344 | compiled = FiltersDownloader.resolveConditions(rules, FilterCompilerConditionsConstants); 345 | assert.ok(compiled); 346 | assert.equal(compiled.length, 1); 347 | assert.equal(compiled[0], 'always_included_rule'); 348 | }); 349 | 350 | QUnit.test('Test filter downloader - "if" conditions brackets', async (assert) => { 351 | let rules; 352 | let compiled; 353 | 354 | rules = [ 355 | 'always_included_rule', 356 | '!#if (((adguard)))', 357 | 'if_adguard_included_rule', 358 | '!#endif', 359 | ]; 360 | 361 | const { FiltersDownloader } = require('../dist'); 362 | assert.ok(FiltersDownloader); 363 | 364 | compiled = FiltersDownloader.resolveConditions(rules, FilterCompilerConditionsConstants); 365 | assert.ok(compiled); 366 | assert.equal(compiled.length, 2); 367 | assert.equal(compiled[0], 'always_included_rule'); 368 | assert.equal(compiled[1], 'if_adguard_included_rule'); 369 | 370 | // (true && true) = true 371 | rules = [ 372 | 'always_included_rule', 373 | '!#if (adguard && adguard_ext_chromium)', 374 | 'if_adguard_included_rule', 375 | '!#endif', 376 | ]; 377 | 378 | compiled = FiltersDownloader.resolveConditions(rules, FilterCompilerConditionsConstants); 379 | assert.ok(compiled); 380 | assert.equal(compiled.length, 2); 381 | assert.equal(compiled[0], 'always_included_rule'); 382 | assert.equal(compiled[1], 'if_adguard_included_rule'); 383 | 384 | // (true || false) && false = false 385 | rules = [ 386 | 'always_included_rule', 387 | '!#if (adguard || adguard_ext_opera) && adguard_ext_firefox', 388 | 'if_adguard_included_rule', 389 | '!#endif', 390 | ]; 391 | 392 | compiled = FiltersDownloader.resolveConditions(rules, FilterCompilerConditionsConstants); 393 | assert.ok(compiled); 394 | assert.equal(compiled.length, 1); 395 | assert.equal(compiled[0], 'always_included_rule'); 396 | 397 | // (false || false) && (false) = false 398 | rules = [ 399 | 'always_included_rule', 400 | '!#if ((adguard || adguard_ext_opera) && (adguard_ext_firefox))', 401 | 'if_adguard_included_rule', 402 | '!#endif', 403 | ]; 404 | 405 | compiled = FiltersDownloader.resolveConditions(rules, FilterCompilerConditionsConstants); 406 | assert.ok(compiled); 407 | assert.equal(compiled.length, 1); 408 | assert.equal(compiled[0], 'always_included_rule'); 409 | 410 | // false || true && (false) = false 411 | rules = [ 412 | 'always_included_rule', 413 | '!#if adguard_ext_opera || adguard && (adguard_ext_firefox)', 414 | 'if_adguard_included_rule', 415 | '!#endif', 416 | ]; 417 | 418 | compiled = FiltersDownloader.resolveConditions(rules, FilterCompilerConditionsConstants); 419 | assert.ok(compiled); 420 | assert.equal(compiled.length, 1); 421 | assert.equal(compiled[0], 'always_included_rule'); 422 | 423 | // !(false) = true 424 | rules = [ 425 | 'always_included_rule', 426 | '!#if !(adguard_ext_opera)', 427 | 'if_adguard_included_rule', 428 | '!#endif', 429 | ]; 430 | 431 | compiled = FiltersDownloader.resolveConditions(rules, FilterCompilerConditionsConstants); 432 | assert.ok(compiled); 433 | assert.equal(compiled.length, 2); 434 | assert.equal(compiled[0], 'always_included_rule'); 435 | assert.equal(compiled[1], 'if_adguard_included_rule'); 436 | 437 | // false && true || true = true 438 | rules = [ 439 | 'always_included_rule', 440 | '!#if adguard_ext_opera && adguard || adguard_ext_chromium', 441 | 'if_adguard_included_rule', 442 | '!#endif', 443 | ]; 444 | 445 | compiled = FiltersDownloader.resolveConditions(rules, FilterCompilerConditionsConstants); 446 | assert.ok(compiled); 447 | assert.equal(compiled.length, 2); 448 | assert.equal(compiled[0], 'always_included_rule'); 449 | assert.equal(compiled[1], 'if_adguard_included_rule'); 450 | 451 | // false && (true || true) = false 452 | rules = [ 453 | 'always_included_rule', 454 | '!#if adguard_ext_opera && (adguard || adguard_ext_chromium)', 455 | 'if_adguard_included_rule', 456 | '!#endif', 457 | ]; 458 | 459 | compiled = FiltersDownloader.resolveConditions(rules, FilterCompilerConditionsConstants); 460 | assert.ok(compiled); 461 | assert.equal(compiled.length, 1); 462 | assert.equal(compiled[0], 'always_included_rule'); 463 | }); 464 | 465 | QUnit.test('Test filter downloader - nested if conditions', async (assert) => { 466 | let rules; 467 | let compiled; 468 | 469 | rules = [ 470 | 'zero_level_rule', 471 | '!#if adguard', 472 | 'first_level_condition', 473 | '!#if adguard', 474 | 'second_level_condition', 475 | '!#endif', 476 | '!#endif', 477 | ]; 478 | 479 | const { FiltersDownloader } = require('../dist'); 480 | assert.ok(FiltersDownloader); 481 | 482 | compiled = FiltersDownloader.resolveConditions(rules, FilterCompilerConditionsConstants); 483 | assert.ok(compiled); 484 | assert.equal(compiled.length, 3); 485 | assert.equal(compiled[0], 'zero_level_rule'); 486 | assert.equal(compiled[1], 'first_level_condition'); 487 | assert.equal(compiled[2], 'second_level_condition'); 488 | 489 | rules = [ 490 | 'zero_level_rule', 491 | '!#if adguard', 492 | 'first_level_condition', 493 | '!#if adguard', 494 | 'second_level_condition', 495 | '!#if !adguard', 496 | 'third_level_condition', 497 | '!#endif', 498 | '!#endif', 499 | '!#endif', 500 | ]; 501 | 502 | compiled = FiltersDownloader.resolveConditions(rules, FilterCompilerConditionsConstants); 503 | assert.ok(compiled); 504 | assert.equal(compiled.length, 3); 505 | assert.equal(compiled[0], 'zero_level_rule'); 506 | assert.equal(compiled[1], 'first_level_condition'); 507 | assert.equal(compiled[2], 'second_level_condition'); 508 | 509 | rules = [ 510 | 'zero_level_rule', 511 | '!#if adguard', 512 | 'first_level_condition', 513 | '!#if adguard', 514 | 'second_level_1_condition', 515 | '!#endif', 516 | '!#if adguard', 517 | 'second_level_2_condition', 518 | '!#endif', 519 | '!#endif', 520 | ]; 521 | 522 | compiled = FiltersDownloader.resolveConditions(rules, FilterCompilerConditionsConstants); 523 | assert.ok(compiled); 524 | assert.equal(compiled.length, 4); 525 | assert.equal(compiled[0], 'zero_level_rule'); 526 | assert.equal(compiled[1], 'first_level_condition'); 527 | assert.equal(compiled[2], 'second_level_1_condition'); 528 | assert.equal(compiled[3], 'second_level_2_condition'); 529 | }); 530 | 531 | QUnit.test('Test filter downloader - invalid "if" conditions', async (assert) => { 532 | const { FiltersDownloader } = require('../dist'); 533 | assert.ok(FiltersDownloader); 534 | 535 | let rules: string[]; 536 | 537 | rules = [ 538 | 'always_included_rule', 539 | '!# if adguard', 540 | 'invalid_if_space', 541 | '!#endif', 542 | ]; 543 | assert.throws(() => { 544 | FiltersDownloader.resolveConditions(rules, FilterCompilerConditionsConstants); 545 | }); 546 | 547 | rules = [ 548 | 'always_included_rule', 549 | '!#if adguard', 550 | 'missing_endif', 551 | ]; 552 | assert.throws(() => { 553 | FiltersDownloader.resolveConditions(rules, FilterCompilerConditionsConstants); 554 | }); 555 | 556 | rules = [ 557 | 'always_included_rule', 558 | '!#if adguard', 559 | 'if_rule', 560 | '!#else', 561 | 'missing_endif', 562 | ]; 563 | assert.throws(() => { 564 | FiltersDownloader.resolveConditions(rules, FilterCompilerConditionsConstants); 565 | }); 566 | 567 | rules = [ 568 | 'always_included_rule', 569 | 'invalid_endif', 570 | '!#endif', 571 | ]; 572 | assert.throws(() => { 573 | FiltersDownloader.resolveConditions(rules, FilterCompilerConditionsConstants); 574 | }); 575 | 576 | rules = [ 577 | 'always_included_rule', 578 | '!#else', 579 | 'invalid_else', 580 | ]; 581 | assert.throws(() => { 582 | FiltersDownloader.resolveConditions(rules, FilterCompilerConditionsConstants); 583 | }); 584 | 585 | rules = [ 586 | 'always_included_rule', 587 | '!#if', 588 | 'invalid_condition', 589 | '!#endif', 590 | ]; 591 | assert.throws(() => { 592 | FiltersDownloader.resolveConditions(rules, FilterCompilerConditionsConstants); 593 | }); 594 | 595 | rules = [ 596 | 'always_included_rule', 597 | '!#if adguard_ext_chromium', 598 | 'if_chromium_rule', 599 | '!#elseif adguard_ext_firefox', 600 | 'invalid_else', 601 | '!#endif', 602 | ]; 603 | assert.throws(() => { 604 | FiltersDownloader.resolveConditions(rules, FilterCompilerConditionsConstants); 605 | }); 606 | 607 | rules = [ 608 | 'always_included_rule', 609 | '!#if (adguard', 610 | 'invalid_condition_brackets', 611 | '!#endif', 612 | ]; 613 | assert.throws(() => { 614 | FiltersDownloader.resolveConditions(rules, FilterCompilerConditionsConstants); 615 | }); 616 | }); 617 | 618 | QUnit.test('Test filter downloader - simple includes', async (assert) => { 619 | const { FiltersDownloader } = require('../dist'); 620 | assert.ok(FiltersDownloader); 621 | 622 | const rules = await FiltersDownloader.download( 623 | `${__dirname}/resources/rules_simple_include.txt`, 624 | FilterCompilerConditionsConstants, 625 | ); 626 | const resolve = await FiltersDownloader.resolveIncludes(rules, null, FilterCompilerConditionsConstants); 627 | assert.ok(resolve); 628 | assert.equal(resolve.length, 2); 629 | assert.equal(resolve[0], 'test_main'); 630 | assert.equal(resolve[1], 'test'); 631 | }); 632 | 633 | QUnit.test('Test filter downloader - nested includes', async (assert) => { 634 | const { FiltersDownloader } = require('../dist'); 635 | assert.ok(FiltersDownloader); 636 | 637 | const rules = await FiltersDownloader.download( 638 | `${__dirname}/resources/rules_nested_includes.txt`, 639 | FilterCompilerConditionsConstants, 640 | ); 641 | const resolve = await FiltersDownloader.resolveIncludes(rules, null, FilterCompilerConditionsConstants); 642 | assert.ok(resolve); 643 | assert.equal(resolve.length, 5); 644 | assert.equal(resolve[0], 'test_parent'); 645 | assert.equal(resolve[1], 'test_main'); 646 | assert.equal(resolve[2], 'test'); 647 | assert.equal(resolve[3], 'test_main'); 648 | assert.equal(resolve[4], 'test'); 649 | }); 650 | 651 | QUnit.test('Test filter downloader - nested subdir includes', async (assert) => { 652 | const { FiltersDownloader } = require('../dist'); 653 | assert.ok(FiltersDownloader); 654 | 655 | const rules = await FiltersDownloader.download( 656 | `${__dirname}/resources/rules_nested_subdir_includes.txt`, 657 | FilterCompilerConditionsConstants, 658 | ); 659 | const resolve = await FiltersDownloader.resolveIncludes(rules, null, FilterCompilerConditionsConstants); 660 | assert.ok(resolve); 661 | assert.equal(resolve.length, 3); 662 | assert.equal(resolve[0], 'test_parent'); 663 | assert.equal(resolve[1], 'sub_test_main'); 664 | assert.equal(resolve[2], 'sub_test'); 665 | }); 666 | 667 | QUnit.test('Test filter downloader - external download and includes with special characters', async (assert) => { 668 | const { FiltersDownloader } = require('../dist'); 669 | assert.ok(FiltersDownloader); 670 | 671 | const rules = await FiltersDownloader.download(URL3, FilterCompilerConditionsConstants); 672 | const resolve = await FiltersDownloader.resolveIncludes(rules, null, FilterCompilerConditionsConstants); 673 | assert.ok(resolve); 674 | assert.equal(resolve.length, 4); 675 | assert.equal(resolve[0], '||example1.org'); 676 | assert.equal(resolve[3], '||example4.org'); 677 | }); 678 | 679 | QUnit.test('Test filter downloader - invalid includes', async (assert) => { 680 | let rules; 681 | 682 | const { FiltersDownloader } = require('../dist'); 683 | assert.ok(FiltersDownloader); 684 | 685 | // non existing file 686 | rules = [ 687 | 'always_included_rule', 688 | '!#include resources/not_found_file.txt', 689 | ]; 690 | 691 | assert.rejects( 692 | FiltersDownloader.resolveIncludes(rules, null, FilterCompilerConditionsConstants), 693 | "Failed to resolve the include directive: '!#include resources/not_found_file.txt'", 694 | ); 695 | 696 | // different origin 697 | rules = [ 698 | 'always_included_rule', 699 | '!#include http://filters.adtidy.org/windows/filters/14.txt', 700 | ]; 701 | 702 | assert.rejects(FiltersDownloader.resolveIncludes(rules, 'http://google.com', FilterCompilerConditionsConstants)); 703 | }); 704 | 705 | QUnit.test('Test filter downloader - compile rules with conditional includes', async (assert) => { 706 | // case 1: positive condition and include existing url 707 | let rules = [ 708 | 'always_included_rule', 709 | '!#if adguard', 710 | `!#include ${URL0}`, 711 | 'if_adguard_included_rule', 712 | '!#endif', 713 | ]; 714 | 715 | const { FiltersDownloader } = require('../dist'); 716 | assert.ok(FiltersDownloader); 717 | 718 | let compiled = await FiltersDownloader.compile(rules, null, FilterCompilerConditionsConstants); 719 | assert.ok(compiled); 720 | assert.equal(compiled.length, 3); 721 | assert.equal(compiled[0], 'always_included_rule'); 722 | assert.equal(compiled[1], 'test'); 723 | assert.equal(compiled[2], 'if_adguard_included_rule'); 724 | 725 | // case 2: positive condition and include non-existing url 726 | rules = [ 727 | 'always_included_rule', 728 | '!#if adguard', 729 | `!#include ${URL404}`, 730 | 'if_adguard_included_rule', 731 | '!#endif', 732 | ]; 733 | 734 | assert.rejects( 735 | FiltersDownloader.compile(rules, null, FilterCompilerConditionsConstants), 736 | `Failed to resolve the include directive '!#include https://raw.githubusercontent.com/AdguardTeam/FiltersDownloader/test-resources/__test__/resources/blabla.txt' 737 | Context: 738 | always_included_rule 739 | !#include ${URL404} 740 | Response status for url ${URL404} is invalid: 404`, 741 | ); 742 | 743 | // case 3: negative condition and include non-existing url 744 | rules = [ 745 | 'always_included_rule', 746 | '!#if adguard_ext_firefox', 747 | `!#include ${URL404}`, 748 | 'if_adguard_included_rule', 749 | '!#endif', 750 | ]; 751 | 752 | compiled = await FiltersDownloader.compile(rules, null, FilterCompilerConditionsConstants); 753 | assert.ok(compiled); 754 | assert.equal(compiled.length, 1); 755 | assert.equal(compiled[0], 'always_included_rule'); 756 | 757 | rules = [ 758 | '!#if non_existing_variable', 759 | '!#include non_existing_file.txt', 760 | '!#endif', 761 | ]; 762 | 763 | compiled = await FiltersDownloader.compile(rules, null, FilterCompilerConditionsConstants); 764 | assert.ok(compiled); 765 | assert.equal(compiled.length, 0); 766 | }); 767 | 768 | QUnit.test('Test filter downloader - download filter with conditional includes', async (assert) => { 769 | const { FiltersDownloader } = require('../dist'); 770 | assert.ok(FiltersDownloader); 771 | 772 | const downloaded = await FiltersDownloader.download(URL4, FilterCompilerConditionsConstants); 773 | assert.ok(downloaded); 774 | assert.equal(downloaded.length, 3); 775 | assert.equal(downloaded[0], 'test_main'); 776 | assert.equal(downloaded[1], 'test'); 777 | assert.equal(downloaded[2], 'example'); 778 | }); 779 | 780 | QUnit.test('Test empty filter downloader without `allowEmptyResponse` option', async (assert) => { 781 | const { FiltersDownloader } = require('../dist'); 782 | assert.ok(FiltersDownloader); 783 | assert.rejects(FiltersDownloader.download(URL_EMPTY, FilterCompilerConditionsConstants), 'Response is empty'); 784 | }); 785 | 786 | QUnit.test('Test downloader with external filter and `allowEmptyResponse` option', async (assert) => { 787 | const { FiltersDownloader } = require('../dist'); 788 | 789 | const downloaded = await FiltersDownloader.download( 790 | URL_EMPTY, 791 | FilterCompilerConditionsConstants, 792 | { allowEmptyResponse: true }, 793 | ); 794 | 795 | assert.equal(downloaded.length, 1); 796 | assert.equal(downloaded[0], ''); 797 | }); 798 | 799 | QUnit.test('Test downloader with empty local filter and `allowEmptyResponse` option', async (assert) => { 800 | const { FiltersDownloader } = require('../dist'); 801 | 802 | mockFs({ 803 | 'test/empty.txt': '', 804 | }); 805 | 806 | const downloaded = await FiltersDownloader.download( 807 | 'test/empty.txt', 808 | FilterCompilerConditionsConstants, 809 | { allowEmptyResponse: true }, 810 | ); 811 | 812 | assert.equal(downloaded.length, 1); 813 | assert.equal(downloaded[0], ''); 814 | 815 | // Clean up the mock file 816 | mockFs.restore(); 817 | }); 818 | 819 | QUnit.test('Test filter downloader includes an empty filter', async (assert) => { 820 | const { FiltersDownloader } = require('../dist'); 821 | assert.ok(FiltersDownloader); 822 | 823 | const compiled = await FiltersDownloader.download(URL_INCLUDES_EMPTY, FilterCompilerConditionsConstants); 824 | 825 | assert.ok(compiled); 826 | assert.equal(compiled.length, 2); 827 | assert.equal(compiled[0], 'The test filter includes an empty filter'); 828 | assert.equal(compiled[1], ''); 829 | }); 830 | 831 | QUnit.test('Test filter downloader - simple "if" conditions with "else" branch', async (assert) => { 832 | const { FiltersDownloader } = require('../dist'); 833 | assert.ok(FiltersDownloader); 834 | 835 | let actual; 836 | let expected; 837 | 838 | // case 1 839 | actual = [ 840 | 'always_included_rule', 841 | '!#if (adguard)', 842 | 'adguard_included_rule', 843 | '!#else', 844 | 'non_adguard_included_rule', 845 | '!#endif', 846 | ]; 847 | expected = [ 848 | 'always_included_rule', 849 | 'adguard_included_rule', 850 | ]; 851 | expectResolvedConditionsRules(FiltersDownloader, actual, expected, assert); 852 | 853 | // case 2 854 | actual = [ 855 | 'always_included_rule', 856 | '!#if !adguard', 857 | 'non_adguard_included_rule', 858 | '!#else', 859 | 'adguard_included_rule_1', 860 | 'adguard_included_rule_2', 861 | '!#endif', 862 | ]; 863 | expected = [ 864 | 'always_included_rule', 865 | 'adguard_included_rule_1', 866 | 'adguard_included_rule_2', 867 | ]; 868 | expectResolvedConditionsRules(FiltersDownloader, actual, expected, assert); 869 | 870 | // case 3 871 | actual = [ 872 | 'always_included_rule', 873 | '!#if !!adguard', 874 | 'adguard_included_rule', 875 | '!#else', 876 | 'non_adguard_included_rule', 877 | '!#endif', 878 | ]; 879 | expected = [ 880 | 'always_included_rule', 881 | 'adguard_included_rule', 882 | ]; 883 | expectResolvedConditionsRules(FiltersDownloader, actual, expected, assert); 884 | 885 | // case 4 886 | actual = [ 887 | 'always_included_rule', 888 | '!#if adguard', 889 | 'adguard_included_rule', 890 | '!#else', 891 | 'non_adguard_included_rule_1', 892 | '!#endif', 893 | '!#if adguard_ext_chromium', 894 | 'adguard_ext_chromium_included_rule', 895 | '!#else', 896 | 'non_adguard_included_rule_2', 897 | '!#endif', 898 | ]; 899 | expected = [ 900 | 'always_included_rule', 901 | 'adguard_included_rule', 902 | 'adguard_ext_chromium_included_rule', 903 | ]; 904 | expectResolvedConditionsRules(FiltersDownloader, actual, expected, assert); 905 | 906 | // case 5 907 | actual = [ 908 | 'always_included_rule', 909 | '!#if !adguard', 910 | 'non_adguard_included_rule_1', 911 | '!#else', 912 | 'adguard_included_rule', 913 | '!#endif', 914 | '!#if !adguard_ext_chromium', 915 | 'non_adguard_included_rule_2', 916 | '!#else', 917 | 'adguard_ext_chromium_included_rule', 918 | '!#endif', 919 | ]; 920 | expected = [ 921 | 'always_included_rule', 922 | 'adguard_included_rule', 923 | 'adguard_ext_chromium_included_rule', 924 | ]; 925 | expectResolvedConditionsRules(FiltersDownloader, actual, expected, assert); 926 | }); 927 | 928 | QUnit.test('Test filter downloader - logical "if" conditions with "else" branch', async (assert) => { 929 | const { FiltersDownloader } = require('../dist'); 930 | assert.ok(FiltersDownloader); 931 | 932 | let actual; 933 | let expected; 934 | 935 | // true && true = true 936 | actual = [ 937 | 'always_included_rule', 938 | '!#if adguard && adguard_ext_chromium', 939 | 'adguard_included_rule', 940 | '!#else', 941 | 'non_adguard_included_rule', 942 | '!#endif', 943 | ]; 944 | expected = [ 945 | 'always_included_rule', 946 | 'adguard_included_rule', 947 | ]; 948 | expectResolvedConditionsRules(FiltersDownloader, actual, expected, assert); 949 | 950 | // true && false = false 951 | actual = [ 952 | 'always_included_rule', 953 | '!#if adguard && adguard_ext_opera', 954 | 'adguard_ext_opera_included_rule', 955 | '!#else', 956 | 'else_included_rule', 957 | '!#endif', 958 | ]; 959 | expected = [ 960 | 'always_included_rule', 961 | 'else_included_rule', 962 | ]; 963 | expectResolvedConditionsRules(FiltersDownloader, actual, expected, assert); 964 | 965 | // true || false = true 966 | actual = [ 967 | 'always_included_rule', 968 | '!#if adguard || adguard_ext_opera', 969 | 'if_included_rule', 970 | '!#else', 971 | 'else_included_rule', 972 | '!#endif', 973 | ]; 974 | expected = [ 975 | 'always_included_rule', 976 | 'if_included_rule', 977 | ]; 978 | expectResolvedConditionsRules(FiltersDownloader, actual, expected, assert); 979 | 980 | // true && false || true = true 981 | actual = [ 982 | 'always_included_rule', 983 | '!#if (adguard && adguard_ext_opera || adguard_ext_chromium)', 984 | 'if_included_rule', 985 | '!#else', 986 | 'else_included_rule', 987 | '!#endif', 988 | ]; 989 | expected = [ 990 | 'always_included_rule', 991 | 'if_included_rule', 992 | ]; 993 | expectResolvedConditionsRules(FiltersDownloader, actual, expected, assert); 994 | 995 | // true && false && true = false 996 | actual = [ 997 | 'always_included_rule', 998 | '!#if adguard && adguard_ext_opera && adguard_ext_chromium', 999 | 'if_included_rule', 1000 | '!#else', 1001 | 'else_included_rule', 1002 | '!#endif', 1003 | ]; 1004 | expected = [ 1005 | 'always_included_rule', 1006 | 'else_included_rule', 1007 | ]; 1008 | expectResolvedConditionsRules(FiltersDownloader, actual, expected, assert); 1009 | 1010 | // false && true || true = true 1011 | actual = [ 1012 | 'always_included_rule', 1013 | '!#if adguard_ext_opera && adguard || adguard_ext_chromium', 1014 | 'if_included_rule', 1015 | '!#else', 1016 | 'else_included_rule', 1017 | '!#endif', 1018 | ]; 1019 | expected = [ 1020 | 'always_included_rule', 1021 | 'if_included_rule', 1022 | ]; 1023 | expectResolvedConditionsRules(FiltersDownloader, actual, expected, assert); 1024 | 1025 | // false || true && true = true 1026 | actual = [ 1027 | 'always_included_rule', 1028 | '!#if adguard_ext_opera || adguard && adguard_ext_chromium', 1029 | 'if_adguard_included_rule', 1030 | '!#else', 1031 | 'else_included_rule', 1032 | '!#endif', 1033 | ]; 1034 | expected = [ 1035 | 'always_included_rule', 1036 | 'if_adguard_included_rule', 1037 | ]; 1038 | expectResolvedConditionsRules(FiltersDownloader, actual, expected, assert); 1039 | 1040 | // !true && !true = false 1041 | actual = [ 1042 | 'always_included_rule', 1043 | '!#if !adguard && !adguard_ext_chromium', 1044 | 'if_included_rule', 1045 | '!#else', 1046 | 'else_included_rule', 1047 | '!#endif', 1048 | ]; 1049 | expected = [ 1050 | 'always_included_rule', 1051 | 'else_included_rule', 1052 | ]; 1053 | expectResolvedConditionsRules(FiltersDownloader, actual, expected, assert); 1054 | 1055 | // !(true && true) = false 1056 | actual = [ 1057 | 'always_included_rule', 1058 | '!#if !(adguard && adguard_ext_chromium)', 1059 | 'if_included_rule', 1060 | '!#else', 1061 | 'else_included_rule', 1062 | '!#endif', 1063 | ]; 1064 | expected = [ 1065 | 'always_included_rule', 1066 | 'else_included_rule', 1067 | ]; 1068 | expectResolvedConditionsRules(FiltersDownloader, actual, expected, assert); 1069 | }); 1070 | 1071 | QUnit.test('Test filter downloader - nested if conditions with else branch', async (assert) => { 1072 | const { FiltersDownloader } = require('../dist'); 1073 | assert.ok(FiltersDownloader); 1074 | 1075 | let actual; 1076 | let expected; 1077 | 1078 | // case 1 -- two nested levels 1079 | actual = [ 1080 | 'zero_level_rule', 1081 | '!#if adguard', 1082 | 'if_first_level_rule', 1083 | '!#if adguard_ext_chromium', 1084 | 'if_second_level_rule', 1085 | '!#else', 1086 | 'else_second_level_rule', 1087 | '!#endif', 1088 | '!#else', 1089 | 'else_first_level_rule', 1090 | '!#endif', 1091 | ]; 1092 | expected = [ 1093 | 'zero_level_rule', 1094 | 'if_first_level_rule', 1095 | 'if_second_level_rule', 1096 | ]; 1097 | expectResolvedConditionsRules(FiltersDownloader, actual, expected, assert); 1098 | 1099 | // case 2 -- three nested levels 1100 | actual = [ 1101 | 'zero_level_rule', 1102 | '!#if adguard', 1103 | 'if_first_level_rule', 1104 | '!#if adguard_ext_chromium', 1105 | 'if_second_level_rule', 1106 | '!#if !adguard_ext_firefox', 1107 | 'if_third_level_rule', 1108 | '!#else', 1109 | 'else_third_level_rule', 1110 | '!#endif', 1111 | '!#else', 1112 | 'else_second_level_rule', 1113 | '!#endif', 1114 | '!#else', 1115 | 'else_first_level_rule', 1116 | '!#endif', 1117 | ]; 1118 | expected = [ 1119 | 'zero_level_rule', 1120 | 'if_first_level_rule', 1121 | 'if_second_level_rule', 1122 | 'if_third_level_rule', 1123 | ]; 1124 | expectResolvedConditionsRules(FiltersDownloader, actual, expected, assert); 1125 | 1126 | // case 3 -- two nested levels but with few inner if conditions 1127 | actual = [ 1128 | 'zero_level_rule', 1129 | '!#if adguard', 1130 | 'if_first_level_rule', 1131 | '!#if adguard_ext_chromium', 1132 | 'if_second_level_rule_1', 1133 | '!#else', 1134 | 'else_second_level_rule_1', 1135 | '!#endif', 1136 | '!#if !adguard_ext_firefox', 1137 | 'if_second_level_rule_2', 1138 | '!#else', 1139 | 'else_second_level_rule_2', 1140 | '!#endif', 1141 | '!#else', 1142 | 'else_first_level_rule', 1143 | '!#endif', 1144 | ]; 1145 | expected = [ 1146 | 'zero_level_rule', 1147 | 'if_first_level_rule', 1148 | 'if_second_level_rule_1', 1149 | 'if_second_level_rule_2', 1150 | ]; 1151 | expectResolvedConditionsRules(FiltersDownloader, actual, expected, assert); 1152 | 1153 | // case 4 -- two nested levels but else branch rules are included in the inner if condition 1154 | actual = [ 1155 | 'zero_level_rule', 1156 | '!#if adguard', 1157 | 'if_first_level_rule', 1158 | '!#if adguard_ext_firefox', 1159 | 'if_second_level_rule', 1160 | '!#else', 1161 | 'else_second_level_rule', 1162 | '!#endif', 1163 | '!#else', 1164 | 'else_first_level_rule', 1165 | '!#endif', 1166 | ]; 1167 | expected = [ 1168 | 'zero_level_rule', 1169 | 'if_first_level_rule', 1170 | 'else_second_level_rule', 1171 | ]; 1172 | expectResolvedConditionsRules(FiltersDownloader, actual, expected, assert); 1173 | 1174 | // case 5 -- two nested levels but inner if condition is skipped 1175 | actual = [ 1176 | 'zero_level_rule', 1177 | '!#if !adguard', 1178 | 'if_first_level_rule', 1179 | '!#if adguard_ext_firefox', 1180 | 'if_second_level_rule', 1181 | '!#else', 1182 | 'else_second_level_rule', 1183 | '!#endif', 1184 | '!#else', 1185 | 'else_first_level_rule', 1186 | '!#endif', 1187 | ]; 1188 | expected = [ 1189 | 'zero_level_rule', 1190 | 'else_first_level_rule', 1191 | ]; 1192 | expectResolvedConditionsRules(FiltersDownloader, actual, expected, assert); 1193 | }); 1194 | --------------------------------------------------------------------------------