@adguard/dnr-converter 1.0.0-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +476 -0
- package/dist/cli.cjs +58912 -0
- package/dist/constants.js +23 -0
- package/dist/declarative-rule/declarative-rule.js +15 -0
- package/dist/declarative-rule/header-info.js +12 -0
- package/dist/declarative-rule/modify-header-info.js +31 -0
- package/dist/declarative-rule/query-key-value.js +12 -0
- package/dist/declarative-rule/query-transform.js +12 -0
- package/dist/declarative-rule/redirect.js +14 -0
- package/dist/declarative-rule/rule-action.js +44 -0
- package/dist/declarative-rule/rule-condition.js +77 -0
- package/dist/declarative-rule/url-transform.js +30 -0
- package/dist/errors/conversion-errors/empty-domains-error.js +24 -0
- package/dist/errors/conversion-errors/empty-resources-error.js +24 -0
- package/dist/errors/conversion-errors/index.js +17 -0
- package/dist/errors/conversion-errors/invalid-declarative-rule-error.js +22 -0
- package/dist/errors/conversion-errors/unsupported-modifier-error.js +20 -0
- package/dist/errors/conversion-errors/unsupported-regexp-error.js +27 -0
- package/dist/errors/converter-options-errors/empty-or-negative-number-of-rules-error.js +18 -0
- package/dist/errors/converter-options-errors/index.js +15 -0
- package/dist/errors/converter-options-errors/negative-number-of-rules-error.js +18 -0
- package/dist/errors/converter-options-errors/resources-path-error.js +18 -0
- package/dist/errors/limitation-errors/index.js +17 -0
- package/dist/errors/limitation-errors/max-scanned-rules-error.js +20 -0
- package/dist/errors/limitation-errors/too-many-error.js +28 -0
- package/dist/errors/limitation-errors/too-many-regexp-rules-error.js +25 -0
- package/dist/errors/limitation-errors/too-many-rules-error.js +25 -0
- package/dist/errors/limitation-errors/too-many-unsafe-rules-error.js +25 -0
- package/dist/errors/unavailable-sources-errors/unavailable-filter-source-error.js +21 -0
- package/dist/errors/unavailable-sources-errors/unavailable-ruleset-source-error.js +21 -0
- package/dist/filter/filter.js +92 -0
- package/dist/filter-converter/filter-converter.js +385 -0
- package/dist/grouped-rules-converters/url-transform-converter.js +340 -0
- package/dist/index.js +30 -0
- package/dist/package.json.js +3 -0
- package/dist/re2-regexp/re2-validator.js +37 -0
- package/dist/re2-regexp/regex-validator-extension.js +30 -0
- package/dist/re2.wasm +0 -0
- package/dist/rule/option-names.js +65 -0
- package/dist/rule/rule-badfilter.js +81 -0
- package/dist/rule/rule-priority.js +88 -0
- package/dist/rule/rule-validator.js +455 -0
- package/dist/rule/rule.js +853 -0
- package/dist/rule/string-utils.js +49 -0
- package/dist/rule-converters/csp-converter.js +105 -0
- package/dist/rule-converters/regular-rule-converter.js +789 -0
- package/dist/rule-converters/remove-header-converter.js +97 -0
- package/dist/rule-converters/rules-converter.js +365 -0
- package/dist/rule-converters/rules-grouper.js +75 -0
- package/dist/rules-scanner/rules-scanner.js +136 -0
- package/dist/ruleset/metadata-rule.js +35 -0
- package/dist/ruleset/metadata-ruleset.js +162 -0
- package/dist/ruleset/rules-hash-map.js +71 -0
- package/dist/ruleset/ruleset-with-source-map.js +406 -0
- package/dist/ruleset/ruleset.js +54 -0
- package/dist/ruleset/source-map.js +283 -0
- package/dist/types/cli/convert-filters.d.ts +32 -0
- package/dist/types/cli/index.d.ts +7 -0
- package/dist/types/cli/utils.d.ts +19 -0
- package/dist/types/src/declarative-rule/declarative-rule.d.ts +32 -0
- package/dist/types/src/declarative-rule/header-info.d.ts +31 -0
- package/dist/types/src/declarative-rule/modify-header-info.d.ts +46 -0
- package/dist/types/src/declarative-rule/query-key-value.d.ts +24 -0
- package/dist/types/src/declarative-rule/query-transform.d.ts +21 -0
- package/dist/types/src/declarative-rule/redirect.d.ts +32 -0
- package/dist/types/src/declarative-rule/rule-action.d.ts +68 -0
- package/dist/types/src/declarative-rule/rule-condition.d.ts +262 -0
- package/dist/types/src/declarative-rule/url-transform.d.ts +69 -0
- package/dist/types/src/errors/conversion-errors/empty-domains-error.d.ts +21 -0
- package/dist/types/src/errors/conversion-errors/empty-resources-error.d.ts +21 -0
- package/dist/types/src/errors/conversion-errors/index.d.ts +27 -0
- package/dist/types/src/errors/conversion-errors/invalid-declarative-rule-error.d.ts +30 -0
- package/dist/types/src/errors/conversion-errors/unsupported-modifier-error.d.ts +20 -0
- package/dist/types/src/errors/conversion-errors/unsupported-regexp-error.d.ts +23 -0
- package/dist/types/src/errors/converter-options-errors/empty-or-negative-number-of-rules-error.d.ts +13 -0
- package/dist/types/src/errors/converter-options-errors/index.d.ts +23 -0
- package/dist/types/src/errors/converter-options-errors/negative-number-of-rules-error.d.ts +13 -0
- package/dist/types/src/errors/converter-options-errors/resources-path-error.d.ts +13 -0
- package/dist/types/src/errors/limitation-errors/index.d.ts +27 -0
- package/dist/types/src/errors/limitation-errors/max-scanned-rules-error.d.ts +18 -0
- package/dist/types/src/errors/limitation-errors/too-many-error.d.ts +33 -0
- package/dist/types/src/errors/limitation-errors/too-many-regexp-rules-error.d.ts +21 -0
- package/dist/types/src/errors/limitation-errors/too-many-rules-error.d.ts +21 -0
- package/dist/types/src/errors/limitation-errors/too-many-unsafe-rules-error.d.ts +21 -0
- package/dist/types/src/errors/unavailable-sources-errors/unavailable-filter-source-error.d.ts +19 -0
- package/dist/types/src/errors/unavailable-sources-errors/unavailable-ruleset-source-error.d.ts +19 -0
- package/dist/types/src/filter/filter.d.ts +74 -0
- package/dist/types/src/filter/types.d.ts +45 -0
- package/dist/types/src/filter-converter/conversion-result.d.ts +35 -0
- package/dist/types/src/filter-converter/filter-converter-options.d.ts +81 -0
- package/dist/types/src/filter-converter/filter-converter.d.ts +197 -0
- package/dist/types/src/index.d.ts +37 -0
- package/dist/types/src/rule/rule-types.d.ts +33 -0
- package/dist/types/src/rule/rule-validator.d.ts +185 -0
- package/dist/types/src/rule/rule.d.ts +269 -0
- package/dist/types/src/ruleset/metadata-ruleset.d.ts +128 -0
- package/dist/types/src/ruleset/rules-hash-map.d.ts +70 -0
- package/dist/types/src/ruleset/ruleset-with-source-map.d.ts +318 -0
- package/dist/types/src/ruleset/ruleset.d.ts +86 -0
- package/dist/types/src/ruleset/source-map.d.ts +45 -0
- package/dist/types/src/ruleset/types.d.ts +68 -0
- package/dist/types/src/utils/is-safe-rule.d.ts +20 -0
- package/dist/types/src/utils/ruleset-utils.d.ts +45 -0
- package/dist/types/src/version.d.ts +6 -0
- package/dist/url-transform-converter.js +338 -0
- package/dist/utils/error.js +22 -0
- package/dist/utils/is-safe-rule.js +25 -0
- package/dist/utils/lazy-loader.js +116 -0
- package/dist/utils/lazy.js +50 -0
- package/dist/utils/ruleset-utils.js +76 -0
- package/dist/utils/string.js +180 -0
- package/dist/utils/valibot.js +74 -0
- package/dist/version.js +7 -0
- package/package.json +122 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { TooManyError } from './too-many-error.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @typedef {import('../../ruleset/source-map').Source} Source
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Describes an error when the maximum number of unsafe rules is reached.
|
|
8
|
+
*/
|
|
9
|
+
declare class TooManyUnsafeRulesError extends TooManyError {
|
|
10
|
+
/**
|
|
11
|
+
* Describes an error when the maximum number of unsafe rules is reached.
|
|
12
|
+
*
|
|
13
|
+
* @param message Message of error.
|
|
14
|
+
* @param excludedRulesIds List of excluded source (important!) {@link Source} rules ids.
|
|
15
|
+
* @param numberOfMaximumRules Number of maximum rules.
|
|
16
|
+
* @param numberOfExcludedDeclarativeRules Number of excluded declarative rules.
|
|
17
|
+
*/
|
|
18
|
+
constructor(message: string, excludedRulesIds: number[], numberOfMaximumRules: number, numberOfExcludedDeclarativeRules: number);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export { TooManyUnsafeRulesError };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Describes an error when filter source is not available.
|
|
3
|
+
*/
|
|
4
|
+
declare class UnavailableFilterSourceError extends Error {
|
|
5
|
+
/**
|
|
6
|
+
* Filter id, the source of which is not available.
|
|
7
|
+
*/
|
|
8
|
+
filterId: number;
|
|
9
|
+
/**
|
|
10
|
+
* Describes an error when filter source is not available.
|
|
11
|
+
*
|
|
12
|
+
* @param message Message of error.
|
|
13
|
+
* @param filterId Filter id, the source of which is not available.
|
|
14
|
+
* @param cause Basic error, describes why the source is unavailable.
|
|
15
|
+
*/
|
|
16
|
+
constructor(message: string, filterId: number, cause?: Error);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export { UnavailableFilterSourceError };
|
package/dist/types/src/errors/unavailable-sources-errors/unavailable-ruleset-source-error.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Describes an error when rule set source is not available.
|
|
3
|
+
*/
|
|
4
|
+
declare class UnavailableRulesetSourceError extends Error {
|
|
5
|
+
/**
|
|
6
|
+
* Rule set id, the source of which is not available.
|
|
7
|
+
*/
|
|
8
|
+
rulesetId: string;
|
|
9
|
+
/**
|
|
10
|
+
* Describes an error when rule set source is not available.
|
|
11
|
+
*
|
|
12
|
+
* @param message Message of error.
|
|
13
|
+
* @param rulesetId Rule set id, the source of which is not available.
|
|
14
|
+
* @param cause Basic error, describes why the source is unavailable.
|
|
15
|
+
*/
|
|
16
|
+
constructor(message: string, rulesetId: string, cause?: Error);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export { UnavailableRulesetSourceError };
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { IFilter } from './types.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @file Concrete Filter implementation for both simple and advanced DNR
|
|
5
|
+
* conversion flows.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Concrete filter implementation for the DNR converter.
|
|
10
|
+
*
|
|
11
|
+
* Supports two construction modes:
|
|
12
|
+
* - **Pre-loaded**: `new Filter(id, content)` — content is a plain string,
|
|
13
|
+
* `getContent()` resolves immediately, `unloadContent()` is a no-op.
|
|
14
|
+
* - **Lazy-loaded**: `new Filter(id, source)` — content is fetched on the first
|
|
15
|
+
* call to `getContent()`, with promise deduplication and unload/reload support.
|
|
16
|
+
*
|
|
17
|
+
* Use with {@link FilterConverter}.
|
|
18
|
+
*/
|
|
19
|
+
declare class Filter implements IFilter {
|
|
20
|
+
/**
|
|
21
|
+
* Regex that matches both \n and \r\n line endings when splitting
|
|
22
|
+
* filter content into individual rules.
|
|
23
|
+
*/
|
|
24
|
+
private static readonly LINE_SPLITTER;
|
|
25
|
+
/**
|
|
26
|
+
* ID of filter.
|
|
27
|
+
*/
|
|
28
|
+
private readonly id;
|
|
29
|
+
/**
|
|
30
|
+
* Pre-loaded content, set only in pre-loaded mode. When non-null,
|
|
31
|
+
* `getContent()` returns it directly and `unloadContent()` is a no-op
|
|
32
|
+
* (pre-loaded filters have no source to reload from).
|
|
33
|
+
*/
|
|
34
|
+
private readonly preloadedContent;
|
|
35
|
+
/**
|
|
36
|
+
* Lazy content loader, set only in lazy-loaded mode.
|
|
37
|
+
* {@link LazyLoader} Handles caching, concurrent-call coalescing, and
|
|
38
|
+
* safe unload-during-load.
|
|
39
|
+
*/
|
|
40
|
+
private readonly contentLoader;
|
|
41
|
+
/**
|
|
42
|
+
* Lazily-built map from rule-start character offset to rule text, used
|
|
43
|
+
* by {@link getRuleByIndex} for O(1) lookups. Rebuilt on first access
|
|
44
|
+
* after content is (re)loaded; cleared on {@link unloadContent}.
|
|
45
|
+
*/
|
|
46
|
+
private ruleByOffset;
|
|
47
|
+
/**
|
|
48
|
+
* Creates a pre-loaded Filter that wraps a plain string.
|
|
49
|
+
* GetContent() resolves immediately; unloadContent() is a no-op..
|
|
50
|
+
*
|
|
51
|
+
* @param id Numeric filter identifier.
|
|
52
|
+
* @param content Pre-loaded filter content (one rule per line).
|
|
53
|
+
*/
|
|
54
|
+
constructor(id: number, content: string);
|
|
55
|
+
/**
|
|
56
|
+
* Creates a lazy-loaded Filter.
|
|
57
|
+
* The getSource callback is invoked at most once per load cycle; concurrent
|
|
58
|
+
* getContent() calls coalesce onto a single in-flight Promise.
|
|
59
|
+
*
|
|
60
|
+
* @param id Numeric filter identifier.
|
|
61
|
+
* @param getSource Zero-argument async callback that resolves to filter content.
|
|
62
|
+
*/
|
|
63
|
+
constructor(id: number, getSource: () => Promise<string>);
|
|
64
|
+
/** @inheritdoc */
|
|
65
|
+
getId(): number;
|
|
66
|
+
/** @inheritdoc */
|
|
67
|
+
getContent(): Promise<string>;
|
|
68
|
+
/** @inheritdoc */
|
|
69
|
+
getRuleByIndex(index: number): Promise<string>;
|
|
70
|
+
/** @inheritdoc */
|
|
71
|
+
unloadContent(): void;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export { Filter };
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Defines the IFilter interface for the DNR converter.
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Interface for a filter list used in both the simple and advanced conversion
|
|
6
|
+
* flows. Implementations provide filter identity, asynchronous content loading,
|
|
7
|
+
* and optional source-map helpers.
|
|
8
|
+
*
|
|
9
|
+
* Use {@link Filter} as the concrete implementation for both pre-loaded and
|
|
10
|
+
* lazy-loaded content.
|
|
11
|
+
*/
|
|
12
|
+
interface IFilter {
|
|
13
|
+
/**
|
|
14
|
+
* Returns filter id.
|
|
15
|
+
*
|
|
16
|
+
* @returns Filter id.
|
|
17
|
+
*/
|
|
18
|
+
getId(): number;
|
|
19
|
+
/**
|
|
20
|
+
* Returns the filter content.
|
|
21
|
+
*
|
|
22
|
+
* @returns Promise resolving to filter content.
|
|
23
|
+
*/
|
|
24
|
+
getContent(): Promise<string>;
|
|
25
|
+
/**
|
|
26
|
+
* Returns original rule text by character offset.
|
|
27
|
+
*
|
|
28
|
+
* Optional: only invoked by the converter in the source-map flow
|
|
29
|
+
* (`withSourceMap: true`). Consumers of the simple flow may omit it.
|
|
30
|
+
*
|
|
31
|
+
* @param index Character offset of the rule's start in the original filter
|
|
32
|
+
* text (i.e. `node.start` from the AGTree AST), **not** a line number or
|
|
33
|
+
* rule-list index.
|
|
34
|
+
*
|
|
35
|
+
* @returns Original filtering rule at the given character offset.
|
|
36
|
+
*/
|
|
37
|
+
getRuleByIndex?(index: number): Promise<string>;
|
|
38
|
+
/**
|
|
39
|
+
* Unload filter content.
|
|
40
|
+
* This method can be used to free memory until the content is needed again.
|
|
41
|
+
*/
|
|
42
|
+
unloadContent(): void;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export type { IFilter };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { ConversionError } from '../errors/conversion-errors/index.js';
|
|
2
|
+
import { LimitationError } from '../errors/limitation-errors/index.js';
|
|
3
|
+
import { IRuleset } from '../ruleset/ruleset.js';
|
|
4
|
+
import { UpdateStaticRulesOptions } from '../ruleset/types.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The result of the conversion from filter with string rules to ruleset
|
|
8
|
+
* with declarative rules.
|
|
9
|
+
*
|
|
10
|
+
* @template T The type of the rule set. Defaults to {@link IRuleset}.
|
|
11
|
+
*/
|
|
12
|
+
interface ConversionResult<T = IRuleset> {
|
|
13
|
+
/**
|
|
14
|
+
* Rule set with all the information about the declarative rules.
|
|
15
|
+
*/
|
|
16
|
+
ruleset: T;
|
|
17
|
+
/**
|
|
18
|
+
* Errors that may have occurred during the conversion.
|
|
19
|
+
*/
|
|
20
|
+
errors: (ConversionError | Error)[];
|
|
21
|
+
/**
|
|
22
|
+
* If the resulting declarative rules have been truncated,
|
|
23
|
+
* information about it will be found in the limitations section.
|
|
24
|
+
*/
|
|
25
|
+
limitations: LimitationError[];
|
|
26
|
+
/**
|
|
27
|
+
* If there were $badfilter rules in the input raw dynamic rules and a list
|
|
28
|
+
* of already converted declarative rules from static filters, the result of
|
|
29
|
+
* the conversion can contain a list of declarative rules that
|
|
30
|
+
* should be canceled.
|
|
31
|
+
*/
|
|
32
|
+
declarativeRulesToCancel?: UpdateStaticRulesOptions[];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export type { ConversionResult };
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { Rule } from '../rule/rule.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Options common to both conversion modes (simple and source-map).
|
|
5
|
+
*/
|
|
6
|
+
interface BaseFilterConverterOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Path to web accessible resources, relative to the extension root dir.
|
|
9
|
+
* Should start with leading slash and should not end with trailing slash (`'/'`).
|
|
10
|
+
*/
|
|
11
|
+
resourcesPath?: string;
|
|
12
|
+
/**
|
|
13
|
+
* Maximum number of converted rules.
|
|
14
|
+
*/
|
|
15
|
+
maxNumberOfRules?: number;
|
|
16
|
+
/**
|
|
17
|
+
* Maximum number of converted unsafe dynamic rules. AG-33779.
|
|
18
|
+
*/
|
|
19
|
+
maxNumberOfUnsafeRules?: number;
|
|
20
|
+
/**
|
|
21
|
+
* Maximum number of converted rules with regexps.
|
|
22
|
+
*/
|
|
23
|
+
maxNumberOfRegexpRules?: number;
|
|
24
|
+
/**
|
|
25
|
+
* When `true`, all filters passed to `convert()` are merged into a single
|
|
26
|
+
* combined {@link Ruleset} instead of one ruleset per filter.
|
|
27
|
+
*
|
|
28
|
+
* @default false
|
|
29
|
+
*/
|
|
30
|
+
combine?: boolean;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Options for the simple conversion flow.
|
|
34
|
+
* Produces {@link IRuleset} rulesets with synchronous access to declarative
|
|
35
|
+
* rules and no source-map support.
|
|
36
|
+
*/
|
|
37
|
+
interface SimpleConverterOptions extends BaseFilterConverterOptions {
|
|
38
|
+
/**
|
|
39
|
+
* Selects simple mode. When omitted or `false`, `convert()` returns
|
|
40
|
+
* {@link IRuleset} rulesets. Source-map-only options such as
|
|
41
|
+
* {@link SourceMapConverterOptions.badFilterRules} are not accepted here.
|
|
42
|
+
*
|
|
43
|
+
* @default false
|
|
44
|
+
*/
|
|
45
|
+
withSourceMap?: false;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Options for the source-map conversion flow.
|
|
49
|
+
* Produces {@link IRulesetWithSourceMap} rulesets that include source maps,
|
|
50
|
+
* hash maps, lazy loading, and full `$badfilter` cross-filter support.
|
|
51
|
+
*/
|
|
52
|
+
interface SourceMapConverterOptions extends BaseFilterConverterOptions {
|
|
53
|
+
/**
|
|
54
|
+
* Selects source-map mode. Must be the literal `true` to discriminate
|
|
55
|
+
* from {@link SimpleConverterOptions}.
|
|
56
|
+
*/
|
|
57
|
+
withSourceMap: true;
|
|
58
|
+
/**
|
|
59
|
+
* Flat array of `$badfilter` network rules collected from pre-built static
|
|
60
|
+
* rulesets (via {@link IRulesetWithSourceMap.getBadFilterRules}).
|
|
61
|
+
*
|
|
62
|
+
* When provided, `convert()` builds an internal hash map from these rules
|
|
63
|
+
* and skips any dynamic rules that are negated by them **before** conversion
|
|
64
|
+
* (scan-time skip). This avoids the N-to-1 grouping problem that would
|
|
65
|
+
* arise if the skip were applied post-conversion.
|
|
66
|
+
*
|
|
67
|
+
* Only valid in source-map mode (enforced by the discriminated union).
|
|
68
|
+
*/
|
|
69
|
+
badFilterRules?: Rule[];
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Interface that represents options for the converter.
|
|
73
|
+
*
|
|
74
|
+
* Discriminated union on {@link BaseFilterConverterOptions} plus the
|
|
75
|
+
* `withSourceMap` flag — this lets TypeScript enforce at compile time that
|
|
76
|
+
* source-map-only options (e.g. `badFilterRules`) are passed only when
|
|
77
|
+
* `withSourceMap: true`.
|
|
78
|
+
*/
|
|
79
|
+
type FilterConverterOptions = SimpleConverterOptions | SourceMapConverterOptions;
|
|
80
|
+
|
|
81
|
+
export type { BaseFilterConverterOptions, FilterConverterOptions, SimpleConverterOptions, SourceMapConverterOptions };
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { IFilter } from '../filter/types.js';
|
|
2
|
+
import { Rule } from '../rule/rule.js';
|
|
3
|
+
import { IRuleset } from '../ruleset/ruleset.js';
|
|
4
|
+
import { IRulesetWithSourceMap } from '../ruleset/ruleset-with-source-map.js';
|
|
5
|
+
import { UpdateStaticRulesOptions } from '../ruleset/types.js';
|
|
6
|
+
import { ConversionResult } from './conversion-result.js';
|
|
7
|
+
import { SourceMapConverterOptions, SimpleConverterOptions, BaseFilterConverterOptions } from './filter-converter-options.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @typedef {import('../declarative-rule').DeclarativeRule} DeclarativeRule
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Unified filter-to-rules converter. Converts adblock filter lists into
|
|
15
|
+
* declarative rule sets.
|
|
16
|
+
*
|
|
17
|
+
* Supports two conversion modes controlled by the
|
|
18
|
+
* {@link FilterConverterOptions.withSourceMap} flag:
|
|
19
|
+
*
|
|
20
|
+
* - **Simple mode** (`withSourceMap` omitted or `false`): produces
|
|
21
|
+
* {@link IRuleset} with synchronous access to declarative rules.
|
|
22
|
+
*
|
|
23
|
+
* - **Source-map mode** (`withSourceMap: true`): produces
|
|
24
|
+
* {@link IRulesetWithSourceMap} with source maps, hash maps, lazy loading,
|
|
25
|
+
* and full `$badfilter` cross-filter support.
|
|
26
|
+
*/
|
|
27
|
+
declare class FilterConverter {
|
|
28
|
+
/**
|
|
29
|
+
* Same as chrome.declarativeNetRequest.DYNAMIC_RULESET_ID.
|
|
30
|
+
* Used as the combined ruleset ID when `combine: true`.
|
|
31
|
+
*/
|
|
32
|
+
static readonly COMBINED_RULESET_ID = "_dynamic";
|
|
33
|
+
/**
|
|
34
|
+
* Returns the rule set ID for a given filter ID.
|
|
35
|
+
*
|
|
36
|
+
* @param filterId Filter ID.
|
|
37
|
+
*
|
|
38
|
+
* @returns Rule set ID string.
|
|
39
|
+
*/
|
|
40
|
+
static getRulesetId(filterId: number): string;
|
|
41
|
+
/**
|
|
42
|
+
* Number of scanned rules can be limited via converter options. In this
|
|
43
|
+
* case we increase the limit by 10% to scan more rules in case of some
|
|
44
|
+
* network rules will be combined into one declarative rule. It is safe,
|
|
45
|
+
* because we have double check for maxNumberOfRules on the converted DNR
|
|
46
|
+
* rules.
|
|
47
|
+
*/
|
|
48
|
+
private static readonly SCANNED_RULES_MULTIPLICATOR;
|
|
49
|
+
/**
|
|
50
|
+
* Converts the provided list of filters into declarative rule sets with
|
|
51
|
+
* source maps, hash maps, and lazy loading.
|
|
52
|
+
*
|
|
53
|
+
* @param filters List of {@link IFilter} to convert.
|
|
54
|
+
* @param options Options with `withSourceMap: true`.
|
|
55
|
+
*
|
|
56
|
+
* @returns Array of {@link ConversionResult} items with source maps.
|
|
57
|
+
*/
|
|
58
|
+
convert(filters: IFilter[], options: SourceMapConverterOptions): Promise<ConversionResult<IRulesetWithSourceMap>[]>;
|
|
59
|
+
/**
|
|
60
|
+
* Converts the provided list of filters into simple declarative rule sets.
|
|
61
|
+
*
|
|
62
|
+
* @param filters List of {@link IFilter} to convert.
|
|
63
|
+
* @param options Converter options (optional).
|
|
64
|
+
*
|
|
65
|
+
* @returns Array of {@link ConversionResult} items.
|
|
66
|
+
*/
|
|
67
|
+
convert(filters: IFilter[], options?: SimpleConverterOptions): Promise<ConversionResult<IRuleset>[]>;
|
|
68
|
+
/**
|
|
69
|
+
* Fallback overload for callers that compute `withSourceMap` at runtime
|
|
70
|
+
* (e.g. from a config flag typed as `boolean`). The return type is the
|
|
71
|
+
* union of both ruleset types, forcing the caller to narrow on
|
|
72
|
+
* `ruleset`'s methods. Use a literal `true`/`false` to select a specific
|
|
73
|
+
* overload and get a precise return type.
|
|
74
|
+
*
|
|
75
|
+
* @param filters List of {@link IFilter} to convert.
|
|
76
|
+
* @param options Converter options with a computed `withSourceMap` flag.
|
|
77
|
+
*
|
|
78
|
+
* @returns Array of {@link ConversionResult} items (union of both modes).
|
|
79
|
+
*/
|
|
80
|
+
convert(filters: IFilter[], options: BaseFilterConverterOptions & {
|
|
81
|
+
withSourceMap: boolean;
|
|
82
|
+
badFilterRules?: Rule[];
|
|
83
|
+
}): Promise<ConversionResult<IRuleset | IRulesetWithSourceMap>[]>;
|
|
84
|
+
/**
|
|
85
|
+
* Applies `$badfilter` rules from dynamic rulesets to static rulesets and
|
|
86
|
+
* returns the list of declarative rule IDs that should be disabled.
|
|
87
|
+
*
|
|
88
|
+
* This handles the **dynamic → static** direction of cross-ruleset
|
|
89
|
+
* `$badfilter` application. The reverse direction (static → dynamic) is
|
|
90
|
+
* handled via `options.badFilterRules` during `convert()`.
|
|
91
|
+
*
|
|
92
|
+
* @param dynamicRulesets Converted dynamic rulesets containing potential
|
|
93
|
+
* `$badfilter` rules.
|
|
94
|
+
* @param staticRulesets Already-built static rulesets to match against.
|
|
95
|
+
*
|
|
96
|
+
* @returns Array of {@link UpdateStaticRulesOptions} describing which
|
|
97
|
+
* static ruleset IDs should have which declarative rule IDs disabled.
|
|
98
|
+
*/
|
|
99
|
+
computeRulesToDisable(dynamicRulesets: IRulesetWithSourceMap[], staticRulesets: IRulesetWithSourceMap[]): Promise<UpdateStaticRulesOptions[]>;
|
|
100
|
+
/**
|
|
101
|
+
* Runs the simple conversion flow: scans, converts, wraps in {@link Ruleset}.
|
|
102
|
+
*
|
|
103
|
+
* @param filters Filters to convert.
|
|
104
|
+
* @param options Converter options.
|
|
105
|
+
*
|
|
106
|
+
* @returns Array of simple conversion results.
|
|
107
|
+
*/
|
|
108
|
+
private static convertSimple;
|
|
109
|
+
/**
|
|
110
|
+
* Scans and converts the given filters into a single simple
|
|
111
|
+
* {@link ConversionResult} identified by `rulesetId`.
|
|
112
|
+
*
|
|
113
|
+
* @param filters Filters to scan and convert.
|
|
114
|
+
* @param rulesetId ID to assign to the produced {@link Ruleset}.
|
|
115
|
+
* @param options Converter options.
|
|
116
|
+
* @param scannedLimit Optional limit on the number of scanned rules.
|
|
117
|
+
*
|
|
118
|
+
* @returns Conversion result for the given filters.
|
|
119
|
+
*/
|
|
120
|
+
private static convertFiltersSimple;
|
|
121
|
+
/**
|
|
122
|
+
* Runs the advanced conversion flow: scans with optional `$badfilter`
|
|
123
|
+
* pre-filtering, converts, wraps in {@link RulesetWithSourceMap}.
|
|
124
|
+
*
|
|
125
|
+
* @param filters Filters to convert.
|
|
126
|
+
* @param options Converter options.
|
|
127
|
+
*
|
|
128
|
+
* @returns Array of source-map conversion results.
|
|
129
|
+
*/
|
|
130
|
+
private static convertWithSourceMap;
|
|
131
|
+
/**
|
|
132
|
+
* Collects {@link ConversionResult} from provided list of raw filters,
|
|
133
|
+
* scanned filters, converted rules and bad filter rules.
|
|
134
|
+
* Creates new {@link RulesetWithSourceMap} and wraps all data for
|
|
135
|
+
* {@link RulesetContentProvider}.
|
|
136
|
+
*
|
|
137
|
+
* @param rulesetId Rule set id.
|
|
138
|
+
* @param filterList List of raw filters.
|
|
139
|
+
* @param scannedFilters Already scanned filters.
|
|
140
|
+
* @param convertedRules Converted rules.
|
|
141
|
+
* @param badFilterRules List of rules with $badfilter modifier.
|
|
142
|
+
*
|
|
143
|
+
* @returns Item of {@link ConversionResult}.
|
|
144
|
+
*/
|
|
145
|
+
private static collectConvertedResult;
|
|
146
|
+
/**
|
|
147
|
+
* Creates dictionary where key is hash of network rule and value is array
|
|
148
|
+
* of rules with this hash.
|
|
149
|
+
*
|
|
150
|
+
* @param badFilterRules A flat list of `$badfilter` network rules.
|
|
151
|
+
*
|
|
152
|
+
* @returns Dictionary with all $badfilter rules indexed by hash.
|
|
153
|
+
*/
|
|
154
|
+
private static buildRulesBadFilterHashMap;
|
|
155
|
+
/**
|
|
156
|
+
* Checks if some rules (fastMatchedRulesByHash) from the staticRuleset,
|
|
157
|
+
* which have been fast matched by hash, can be negated with the provided
|
|
158
|
+
* badFilterRule via the `$badfilter` option.
|
|
159
|
+
*
|
|
160
|
+
* @param badFilterRule Network rule with `$badfilter` option.
|
|
161
|
+
* @param staticRuleset Static rule set which contains fast matched rules.
|
|
162
|
+
* @param fastMatchedRulesByHash Rules that have been fast matched by hash
|
|
163
|
+
* for potential negation.
|
|
164
|
+
*
|
|
165
|
+
* @returns List of declarative rule IDs that have been canceled by
|
|
166
|
+
* the provided badFilterRule.
|
|
167
|
+
*/
|
|
168
|
+
private static checkFastMatchedRulesCanBeCancelled;
|
|
169
|
+
/**
|
|
170
|
+
* Applies rules with $badfilter modifier from dynamic rulesets to all rules
|
|
171
|
+
* from static rulesets and returns list of ids of declarative rules to
|
|
172
|
+
* disable them.
|
|
173
|
+
*
|
|
174
|
+
* @param staticRulesets List of converted static rulesets.
|
|
175
|
+
* @param dynamicBadFilterRules List of rules with $badfilter.
|
|
176
|
+
*
|
|
177
|
+
* @returns List of ids of declarative rules to disable them.
|
|
178
|
+
*/
|
|
179
|
+
private static collectDeclarativeRulesToCancel;
|
|
180
|
+
/**
|
|
181
|
+
* Checks that provided converter options are correct.
|
|
182
|
+
*
|
|
183
|
+
* @param options Contains path to web accessible resources,
|
|
184
|
+
* maximum number of converter rules and regexp rules. @see
|
|
185
|
+
* {@link FilterConverterOptions} for details.
|
|
186
|
+
*
|
|
187
|
+
* @throws An {@link ResourcesPathError} if the resources path does not
|
|
188
|
+
* start with a slash or it ends with a slash
|
|
189
|
+
* OR an {@link EmptyOrNegativeNumberOfRulesError} if maximum number of
|
|
190
|
+
* rules is equal or less than 0.
|
|
191
|
+
* OR an {@link NegativeNumberOfRulesError} if maximum number of
|
|
192
|
+
* regexp rules is less than 0.
|
|
193
|
+
*/
|
|
194
|
+
private static checkConverterOptions;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export { FilterConverter };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export { DNR_CONVERTER_VERSION } from './version.js';
|
|
2
|
+
export { FilterConverterOptions as ConverterOptions } from './filter-converter/filter-converter-options.js';
|
|
3
|
+
export { FilterConverter } from './filter-converter/filter-converter.js';
|
|
4
|
+
export { isSafeRule } from './utils/is-safe-rule.js';
|
|
5
|
+
export { IBaseRuleset, SourceRuleAndFilterId, UpdateStaticRulesOptions } from './ruleset/types.js';
|
|
6
|
+
export { IRuleset, Ruleset } from './ruleset/ruleset.js';
|
|
7
|
+
export { DeserializedRuleset, IRulesetWithSourceMap, RulesetContentProvider, RulesetWithSourceMap, SerializedRuleset, SerializedRulesetData, SerializedRulesetLazyData } from './ruleset/ruleset-with-source-map.js';
|
|
8
|
+
export { ConversionResult } from './filter-converter/conversion-result.js';
|
|
9
|
+
export { Filter } from './filter/filter.js';
|
|
10
|
+
export { IFilter } from './filter/types.js';
|
|
11
|
+
export { HashWithSource, IRulesHashMap, RulesHashMap } from './ruleset/rules-hash-map.js';
|
|
12
|
+
export { ISourceMap } from './ruleset/source-map.js';
|
|
13
|
+
export { UnavailableFilterSourceError } from './errors/unavailable-sources-errors/unavailable-filter-source-error.js';
|
|
14
|
+
export { UnavailableRulesetSourceError } from './errors/unavailable-sources-errors/unavailable-ruleset-source-error.js';
|
|
15
|
+
export { METADATA_RULESET_ID, MetadataRuleset } from './ruleset/metadata-ruleset.js';
|
|
16
|
+
export { Rule } from './rule/rule.js';
|
|
17
|
+
export { RuleDeclarativeValidator } from './rule/rule-validator.js';
|
|
18
|
+
export { DeclarativeRule } from './declarative-rule/declarative-rule.js';
|
|
19
|
+
export { RuleActionType } from './declarative-rule/rule-action.js';
|
|
20
|
+
export { ResourceType } from './declarative-rule/rule-condition.js';
|
|
21
|
+
export { ConversionError, isConversionError } from './errors/conversion-errors/index.js';
|
|
22
|
+
export { ConverterOptionsError, isConverterOptionsError } from './errors/converter-options-errors/index.js';
|
|
23
|
+
export { LimitationError, isLimitationError } from './errors/limitation-errors/index.js';
|
|
24
|
+
export { RULESET_NAME_PREFIX, extractRulesetId, getRulesetId, getRulesetPath } from './utils/ruleset-utils.js';
|
|
25
|
+
export { HttpHeaderMatcher } from './rule/rule-types.js';
|
|
26
|
+
export { EmptyDomainsError } from './errors/conversion-errors/empty-domains-error.js';
|
|
27
|
+
export { EmptyResourcesError } from './errors/conversion-errors/empty-resources-error.js';
|
|
28
|
+
export { UnsupportedModifierError } from './errors/conversion-errors/unsupported-modifier-error.js';
|
|
29
|
+
export { UnsupportedRegexpError } from './errors/conversion-errors/unsupported-regexp-error.js';
|
|
30
|
+
export { InvalidDeclarativeRuleError } from './errors/conversion-errors/invalid-declarative-rule-error.js';
|
|
31
|
+
export { EmptyOrNegativeNumberOfRulesError } from './errors/converter-options-errors/empty-or-negative-number-of-rules-error.js';
|
|
32
|
+
export { NegativeNumberOfRulesError } from './errors/converter-options-errors/negative-number-of-rules-error.js';
|
|
33
|
+
export { ResourcesPathError } from './errors/converter-options-errors/resources-path-error.js';
|
|
34
|
+
export { MaxScannedRulesError } from './errors/limitation-errors/max-scanned-rules-error.js';
|
|
35
|
+
export { TooManyRegexpRulesError } from './errors/limitation-errors/too-many-regexp-rules-error.js';
|
|
36
|
+
export { TooManyUnsafeRulesError } from './errors/limitation-errors/too-many-unsafe-rules-error.js';
|
|
37
|
+
export { TooManyRulesError } from './errors/limitation-errors/too-many-rules-error.js';
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Type definitions for the parsed network rule representation.
|
|
3
|
+
*
|
|
4
|
+
* Modifier tracking uses string sets and priority uses counter-based
|
|
5
|
+
* calculation.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* HTTP header matcher for the `$header` modifier.
|
|
9
|
+
* Specifies the header name and an optional value/pattern to match.
|
|
10
|
+
*/
|
|
11
|
+
type HttpHeaderMatcher = {
|
|
12
|
+
/**
|
|
13
|
+
* Name of the HTTP header to match.
|
|
14
|
+
*/
|
|
15
|
+
header: string;
|
|
16
|
+
/**
|
|
17
|
+
* HTTP header value matcher.
|
|
18
|
+
*
|
|
19
|
+
* - `null` — match by name only (any value passes).
|
|
20
|
+
* - `string` — exact string or regex-pattern to match against.
|
|
21
|
+
* When {@link isRegExp} is `true`, the string is a regex source
|
|
22
|
+
* (without surrounding slashes) and the consumer is responsible
|
|
23
|
+
* for compiling it into a `RegExp`.
|
|
24
|
+
*/
|
|
25
|
+
value: string | null;
|
|
26
|
+
/**
|
|
27
|
+
* Whether {@link value} should be interpreted as a regular expression
|
|
28
|
+
* source string.
|
|
29
|
+
*/
|
|
30
|
+
isRegExp: boolean;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export type { HttpHeaderMatcher };
|