@adguard/dnr-converter 2.0.0 → 2.1.0
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 +62 -3
- package/dist/cli.cjs +51902 -47539
- package/dist/constants.js +4 -1
- package/dist/errors/conversion-errors/index.js +3 -2
- package/dist/errors/conversion-errors/unexpected-conversion-error.js +23 -0
- package/dist/errors/scan-errors.js +39 -0
- package/dist/errors/unavailable-sources-errors/unavailable-filter-source-error.js +10 -1
- package/dist/filter-converter/filter-converter.js +151 -6
- package/dist/index.js +5 -1
- package/dist/package.json.js +1 -1
- package/dist/rule/rule-badfilter.js +144 -55
- package/dist/rule/rule-validator.js +56 -6
- package/dist/rule/rule.js +95 -9
- package/dist/rule-converters/action-kind.js +121 -0
- package/dist/rule-converters/csp-condition-coverage.js +268 -0
- package/dist/rule-converters/csp-converter.js +132 -11
- package/dist/rule-converters/csp-rules-resolver.js +417 -0
- package/dist/rule-converters/regular-rule-converter.js +479 -129
- package/dist/rule-converters/rules-converter.js +75 -7
- package/dist/rule-converters/rules-grouper.js +5 -3
- package/dist/rules-scanner/rules-scanner.js +124 -56
- package/dist/ruleset/ruleset-with-source-map.js +5 -1
- package/dist/types/src/errors/conversion-errors/index.d.ts +7 -2
- package/dist/types/src/errors/conversion-errors/unexpected-conversion-error.d.ts +21 -0
- package/dist/types/src/errors/scan-errors.d.ts +38 -0
- package/dist/types/src/errors/unavailable-sources-errors/unavailable-filter-source-error.d.ts +9 -1
- package/dist/types/src/filter-converter/filter-converter-options.d.ts +18 -1
- package/dist/types/src/filter-converter/filter-converter.d.ts +83 -6
- package/dist/types/src/index.d.ts +7 -3
- package/dist/types/src/rule/rule-validator.d.ts +21 -0
- package/dist/types/src/rule/rule.d.ts +52 -2
- package/dist/types/src/rule-converters/csp-converter.d.ts +12 -0
- package/dist/types/src/ruleset/ruleset-with-source-map.d.ts +14 -0
- package/dist/utils/string.js +27 -1
- package/package.json +6 -6
- package/dist/rule/string-utils.js +0 -49
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import { IFilter } from '../filter/types.js';
|
|
2
|
-
import { Rule } from '../rule/rule.js';
|
|
3
2
|
import { IRuleset } from '../ruleset/ruleset.js';
|
|
4
3
|
import { IRulesetWithSourceMap } from '../ruleset/ruleset-with-source-map.js';
|
|
5
4
|
import { UpdateStaticRulesOptions } from '../ruleset/types.js';
|
|
6
5
|
import { ConversionResult } from './conversion-result.js';
|
|
7
|
-
import { SourceMapConverterOptions, SimpleConverterOptions,
|
|
6
|
+
import { SourceMapConverterOptions, SimpleConverterOptions, ComputedWithSourceMapOptions } from './filter-converter-options.js';
|
|
8
7
|
|
|
9
8
|
/**
|
|
10
9
|
* @typedef {import('../declarative-rule').DeclarativeRule} DeclarativeRule
|
|
@@ -30,6 +29,26 @@ declare class FilterConverter {
|
|
|
30
29
|
* Used as the combined ruleset ID when `combine: true`.
|
|
31
30
|
*/
|
|
32
31
|
static readonly COMBINED_RULESET_ID = "_dynamic";
|
|
32
|
+
/**
|
|
33
|
+
* Runtime ruleset ID used for CSP rules.
|
|
34
|
+
*/
|
|
35
|
+
static readonly CSP_RULESET_ID = "_csp";
|
|
36
|
+
/**
|
|
37
|
+
* Raw modifier names which may affect CSP conversion.
|
|
38
|
+
*
|
|
39
|
+
* Aliases of `$csp` are taken from the AGTree conversion map, so they
|
|
40
|
+
* cannot drift from it.
|
|
41
|
+
*/
|
|
42
|
+
private static readonly CSP_RAW_MODIFIER_NAMES;
|
|
43
|
+
/**
|
|
44
|
+
* Matches a CSP-related modifier anywhere in a modifier list.
|
|
45
|
+
*
|
|
46
|
+
* A modifier name is looked up right after `$` or `,`, spaces are allowed
|
|
47
|
+
* after the separator, and the name must be complete: `$csp` and `,csp=`
|
|
48
|
+
* match, while `$cspreport` does not. The first modifier of a list follows
|
|
49
|
+
* `$`, so a rule where `csp` is not the first modifier is found as well.
|
|
50
|
+
*/
|
|
51
|
+
private static readonly CSP_RELATED_MODIFIER_REGEXP;
|
|
33
52
|
/**
|
|
34
53
|
* Returns the rule set ID for a given filter ID.
|
|
35
54
|
*
|
|
@@ -72,15 +91,56 @@ declare class FilterConverter {
|
|
|
72
91
|
* `ruleset`'s methods. Use a literal `true`/`false` to select a specific
|
|
73
92
|
* overload and get a precise return type.
|
|
74
93
|
*
|
|
94
|
+
* @see {@link ComputedWithSourceMapOptions} for details.
|
|
95
|
+
*
|
|
75
96
|
* @param filters List of {@link IFilter} to convert.
|
|
76
97
|
* @param options Converter options with a computed `withSourceMap` flag.
|
|
77
98
|
*
|
|
78
99
|
* @returns Array of {@link ConversionResult} items (union of both modes).
|
|
79
100
|
*/
|
|
80
|
-
convert(filters: IFilter[], options:
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
101
|
+
convert(filters: IFilter[], options: ComputedWithSourceMapOptions): Promise<ConversionResult<IRuleset | IRulesetWithSourceMap>[]>;
|
|
102
|
+
/**
|
|
103
|
+
* Converts CSP rules from all provided filters into one ruleset.
|
|
104
|
+
*
|
|
105
|
+
* Only candidate lines are parsed: a line must contain `csp` or a `csp`
|
|
106
|
+
* alias as a modifier. `$badfilter` lines are not candidates: the regular
|
|
107
|
+
* pass owns them. The whole text is still split into lines, so the cost
|
|
108
|
+
* grows with the list size, while parsing stays proportional to the number
|
|
109
|
+
* of candidate lines.
|
|
110
|
+
*
|
|
111
|
+
* Allowlist `$csp` rules are resolved here only: the regular `convert()`
|
|
112
|
+
* rejects them, because an exception can only be applied when all filters
|
|
113
|
+
* are seen at once.
|
|
114
|
+
*
|
|
115
|
+
* @param filters Source filters.
|
|
116
|
+
* @param resourcesPath Path to web-accessible resources.
|
|
117
|
+
*
|
|
118
|
+
* @returns Combined CSP conversion result with source-map support.
|
|
119
|
+
*
|
|
120
|
+
* @throws An {@link ResourcesPathError} if the resources path does not
|
|
121
|
+
* start with a slash or ends with a slash.
|
|
122
|
+
*/
|
|
123
|
+
convertCspRules(filters: IFilter[], resourcesPath?: string): Promise<ConversionResult<IRulesetWithSourceMap>>;
|
|
124
|
+
/**
|
|
125
|
+
* Filters out filters whose content cannot contain CSP rules.
|
|
126
|
+
*
|
|
127
|
+
* A filter whose content cannot be read is skipped and reported, so one
|
|
128
|
+
* broken source does not fail the whole configuration.
|
|
129
|
+
*
|
|
130
|
+
* @param filters Source filters.
|
|
131
|
+
*
|
|
132
|
+
* @returns Filters whose content mentions a CSP-related modifier and
|
|
133
|
+
* errors of unreadable filters.
|
|
134
|
+
*/
|
|
135
|
+
private static filterCspFilters;
|
|
136
|
+
/**
|
|
137
|
+
* Checks whether raw filter text may contain a CSP-related rule.
|
|
138
|
+
*
|
|
139
|
+
* @param text Filter content or a single line of it.
|
|
140
|
+
*
|
|
141
|
+
* @returns `true` if the text should be parsed by the CSP pass.
|
|
142
|
+
*/
|
|
143
|
+
private static isCspRelatedText;
|
|
84
144
|
/**
|
|
85
145
|
* Applies `$badfilter` rules from dynamic rulesets to static rulesets and
|
|
86
146
|
* returns the list of declarative rule IDs that should be disabled.
|
|
@@ -118,6 +178,20 @@ declare class FilterConverter {
|
|
|
118
178
|
* @returns Conversion result for the given filters.
|
|
119
179
|
*/
|
|
120
180
|
private static convertFiltersSimple;
|
|
181
|
+
/**
|
|
182
|
+
* Creates a predicate to filter out rules which must not be converted.
|
|
183
|
+
*
|
|
184
|
+
* When CSP rules are rebuilt globally, the regular conversion must not
|
|
185
|
+
* convert them again, while `$badfilter` rules are kept because they can
|
|
186
|
+
* still disable other rules.
|
|
187
|
+
*
|
|
188
|
+
* @param skipNegatedRulesFn Predicate to skip rules negated by `$badfilter`,
|
|
189
|
+
* if any.
|
|
190
|
+
* @param excludeCspRules Whether CSP rules are rebuilt separately.
|
|
191
|
+
*
|
|
192
|
+
* @returns Predicate to filter out rules.
|
|
193
|
+
*/
|
|
194
|
+
private static createRuleFilterFn;
|
|
121
195
|
/**
|
|
122
196
|
* Runs the advanced conversion flow: scans with optional `$badfilter`
|
|
123
197
|
* pre-filtering, converts, wraps in {@link RulesetWithSourceMap}.
|
|
@@ -134,6 +208,9 @@ declare class FilterConverter {
|
|
|
134
208
|
* Creates new {@link RulesetWithSourceMap} and wraps all data for
|
|
135
209
|
* {@link RulesetContentProvider}.
|
|
136
210
|
*
|
|
211
|
+
* Freshly converted rulesets have no metadata-stored unsafe rules yet:
|
|
212
|
+
* unsafe rules are excluded later by the `serializeCompact()` caller.
|
|
213
|
+
*
|
|
137
214
|
* @param rulesetId Rule set id.
|
|
138
215
|
* @param filterList List of raw filters.
|
|
139
216
|
* @param scannedFilters Already scanned filters.
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { DNR_CONVERTER_VERSION } from './version.js';
|
|
2
|
-
export {
|
|
2
|
+
export { isCspDeclarativeRule } from './rule-converters/csp-converter.js';
|
|
3
|
+
export { ComputedWithSourceMapOptions, FilterConverterOptions as ConverterOptions } from './filter-converter/filter-converter-options.js';
|
|
3
4
|
export { FilterConverter } from './filter-converter/filter-converter.js';
|
|
4
5
|
export { isSafeRule } from './utils/is-safe-rule.js';
|
|
5
6
|
export { IBaseRuleset, SourceRuleAndFilterId, UpdateStaticRulesOptions } from './ruleset/types.js';
|
|
@@ -9,10 +10,11 @@ export { ConversionResult } from './filter-converter/conversion-result.js';
|
|
|
9
10
|
export { Filter } from './filter/filter.js';
|
|
10
11
|
export { IFilter } from './filter/types.js';
|
|
11
12
|
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';
|
|
13
|
+
export { ISourceMap, SourceRuleIdxAndFilterId } from './ruleset/source-map.js';
|
|
14
|
+
export { UnavailableFilterSourceError, isUnavailableFilterSourceError } from './errors/unavailable-sources-errors/unavailable-filter-source-error.js';
|
|
14
15
|
export { UnavailableRulesetSourceError } from './errors/unavailable-sources-errors/unavailable-ruleset-source-error.js';
|
|
15
16
|
export { InvalidMetadataChunksError } from './errors/metadata-errors/invalid-metadata-chunks-error.js';
|
|
17
|
+
export { RuleScanError, isRuleScanError } from './errors/scan-errors.js';
|
|
16
18
|
export { CompactRulesetContent, parseCompactRuleset } from './ruleset/compact-ruleset.js';
|
|
17
19
|
export { METADATA_RULESET_ID, MetadataRuleset } from './ruleset/metadata-ruleset.js';
|
|
18
20
|
export { Rule } from './rule/rule.js';
|
|
@@ -30,10 +32,12 @@ export { EmptyResourcesError } from './errors/conversion-errors/empty-resources-
|
|
|
30
32
|
export { UnsupportedModifierError } from './errors/conversion-errors/unsupported-modifier-error.js';
|
|
31
33
|
export { UnsupportedRegexpError } from './errors/conversion-errors/unsupported-regexp-error.js';
|
|
32
34
|
export { InvalidDeclarativeRuleError } from './errors/conversion-errors/invalid-declarative-rule-error.js';
|
|
35
|
+
export { UnexpectedConversionError } from './errors/conversion-errors/unexpected-conversion-error.js';
|
|
33
36
|
export { EmptyOrNegativeNumberOfRulesError } from './errors/converter-options-errors/empty-or-negative-number-of-rules-error.js';
|
|
34
37
|
export { NegativeNumberOfRulesError } from './errors/converter-options-errors/negative-number-of-rules-error.js';
|
|
35
38
|
export { ResourcesPathError } from './errors/converter-options-errors/resources-path-error.js';
|
|
36
39
|
export { MaxScannedRulesError } from './errors/limitation-errors/max-scanned-rules-error.js';
|
|
40
|
+
export { TooManyError } from './errors/limitation-errors/too-many-error.js';
|
|
37
41
|
export { TooManyRegexpRulesError } from './errors/limitation-errors/too-many-regexp-rules-error.js';
|
|
38
42
|
export { TooManyUnsafeRulesError } from './errors/limitation-errors/too-many-unsafe-rules-error.js';
|
|
39
43
|
export { TooManyRulesError } from './errors/limitation-errors/too-many-rules-error.js';
|
|
@@ -173,13 +173,34 @@ declare class RuleDeclarativeValidator {
|
|
|
173
173
|
* $permissions.
|
|
174
174
|
*
|
|
175
175
|
* @param rule Network rule.
|
|
176
|
+
* @param allowCspException CSP exception validation flag.
|
|
176
177
|
*
|
|
177
178
|
* @returns Boolean flag - `false` if the rule does not require conversion
|
|
178
179
|
* and `true` if the rule is convertible.
|
|
179
180
|
*
|
|
180
181
|
* @throws Error with type {@link UnsupportedModifierError} if the rule is not convertible.
|
|
181
182
|
*/
|
|
183
|
+
private static validateRule;
|
|
184
|
+
/**
|
|
185
|
+
* Checks if a network rule can be converted to a declarative format or not.
|
|
186
|
+
*
|
|
187
|
+
* @param rule Network rule.
|
|
188
|
+
*
|
|
189
|
+
* @returns Boolean flag indicating whether the rule should be converted.
|
|
190
|
+
*
|
|
191
|
+
* @throws Error with type {@link UnsupportedModifierError} if the rule is not convertible.
|
|
192
|
+
*/
|
|
182
193
|
static shouldConvertRule(rule: Rule): boolean;
|
|
194
|
+
/**
|
|
195
|
+
* Checks whether a CSP exception can enter CSP resolution.
|
|
196
|
+
*
|
|
197
|
+
* @param rule CSP exception rule.
|
|
198
|
+
*
|
|
199
|
+
* @returns True if the rule can enter CSP resolution.
|
|
200
|
+
*
|
|
201
|
+
* @throws Error with type {@link UnsupportedModifierError} if another modifier is not supported.
|
|
202
|
+
*/
|
|
203
|
+
static shouldProcessCspException(rule: Rule): boolean;
|
|
183
204
|
}
|
|
184
205
|
|
|
185
206
|
export { RuleDeclarativeValidator };
|
|
@@ -104,8 +104,35 @@ declare class Rule {
|
|
|
104
104
|
* `$removeparam`: a value-less `$removeparam` is stored as `''`, because
|
|
105
105
|
* it carries a meaning of its own ("remove all query parameters") that
|
|
106
106
|
* must be distinguishable from "no value".
|
|
107
|
+
*
|
|
108
|
+
* This is the parsed value used by the converters. For `$badfilter`
|
|
109
|
+
* identity see {@link originalText}.
|
|
107
110
|
*/
|
|
108
111
|
readonly advancedModifierValue: string | null;
|
|
112
|
+
/**
|
|
113
|
+
* CSP directive of a `$csp` rule, `null` when the directive is omitted.
|
|
114
|
+
*
|
|
115
|
+
* A dedicated field is required because {@link advancedModifierValue} is
|
|
116
|
+
* shared by every value-bearing advanced modifier and keeps only the last
|
|
117
|
+
* one: `$csp=script-src 'none',urltransform=/a/b/` would otherwise carry
|
|
118
|
+
* the url-transform value as the directive.
|
|
119
|
+
*/
|
|
120
|
+
readonly cspDirective: string | null;
|
|
121
|
+
/**
|
|
122
|
+
* Rule text exactly as written in the filter list, before conversion to AG
|
|
123
|
+
* syntax.
|
|
124
|
+
*
|
|
125
|
+
* A `$badfilter` rule disables a rule only when the rule text matches, so
|
|
126
|
+
* the text as written is compared instead of the parsed (normalized) state —
|
|
127
|
+
* see {@link RuleBadfilter.negates}. Conversion rewrites names and values
|
|
128
|
+
* (for example `$redirect=noop.js` becomes `$redirect=noopjs`), so the text
|
|
129
|
+
* has to be captured before it.
|
|
130
|
+
*
|
|
131
|
+
* Serialization must keep it: rebuilding a rule from its converted text
|
|
132
|
+
* would silently change the written spelling of modifiers, and with it the
|
|
133
|
+
* `$badfilter` identity.
|
|
134
|
+
*/
|
|
135
|
+
readonly originalText: string;
|
|
109
136
|
/**
|
|
110
137
|
* Parsed data for the `$header` modifier.
|
|
111
138
|
*/
|
|
@@ -130,12 +157,14 @@ declare class Rule {
|
|
|
130
157
|
*/
|
|
131
158
|
readonly priority: number;
|
|
132
159
|
/**
|
|
133
|
-
* Creates a new {@link Rule} from filter list metadata
|
|
134
|
-
* network rule AST node.
|
|
160
|
+
* Creates a new {@link Rule} from filter list metadata, an already-converted
|
|
161
|
+
* network rule AST node and the rule text as written in the filter list.
|
|
135
162
|
*
|
|
136
163
|
* @param filterListId Filter list ID.
|
|
137
164
|
* @param index Rule index within the filter list.
|
|
138
165
|
* @param node Network rule AST node (must already be in AG syntax).
|
|
166
|
+
* @param originalText Rule text as written in the filter list, captured
|
|
167
|
+
* before conversion to AG syntax.
|
|
139
168
|
*
|
|
140
169
|
* @throws `SyntaxError` when the pattern contains spaces, the rule is too
|
|
141
170
|
* general, or any modifier is invalid.
|
|
@@ -278,6 +307,27 @@ declare class Rule {
|
|
|
278
307
|
* general.
|
|
279
308
|
*/
|
|
280
309
|
static parseFromNode(filterListId: number, index: number, node: AnyRule): Rule[];
|
|
310
|
+
/**
|
|
311
|
+
* Checks whether the given node is a network rule node.
|
|
312
|
+
*
|
|
313
|
+
* @param node Rule AST node.
|
|
314
|
+
*
|
|
315
|
+
* @returns `true` if the node is a network rule node.
|
|
316
|
+
*/
|
|
317
|
+
private static isNetworkRuleNode;
|
|
318
|
+
/**
|
|
319
|
+
* Returns the text of a rule node as written in the filter list.
|
|
320
|
+
*
|
|
321
|
+
* The parser records the raw text in `node.raws.text` when the `includeRaws`
|
|
322
|
+
* option is enabled, which the scanner always does. Nodes built by hand may
|
|
323
|
+
* lack it, so the text is regenerated as a fallback.
|
|
324
|
+
*
|
|
325
|
+
* @param node Rule AST node, must not be converted to AG syntax yet.
|
|
326
|
+
*
|
|
327
|
+
* @returns Rule text as written, or an empty string when it cannot be
|
|
328
|
+
* reconstructed.
|
|
329
|
+
*/
|
|
330
|
+
private static getWrittenText;
|
|
281
331
|
}
|
|
282
332
|
|
|
283
333
|
export { HttpHeaderMatcher, Rule };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { DeclarativeRule } from '../declarative-rule/declarative-rule.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Checks whether a declarative rule appends a CSP response header.
|
|
5
|
+
*
|
|
6
|
+
* @param rule Declarative rule to check.
|
|
7
|
+
*
|
|
8
|
+
* @returns `true` for CSP declarative rules.
|
|
9
|
+
*/
|
|
10
|
+
declare function isCspDeclarativeRule(rule: DeclarativeRule): boolean;
|
|
11
|
+
|
|
12
|
+
export { isCspDeclarativeRule };
|
|
@@ -29,6 +29,18 @@ interface IRulesetWithSourceMap extends IBaseRuleset {
|
|
|
29
29
|
* is not available.
|
|
30
30
|
*/
|
|
31
31
|
getRulesById(declarativeRuleId: number): Promise<SourceRuleAndFilterId[]>;
|
|
32
|
+
/**
|
|
33
|
+
* Returns source rule indexes and their filter identifiers for a given
|
|
34
|
+
* declarative rule identifier.
|
|
35
|
+
*
|
|
36
|
+
* Unlike {@link getRulesById}, it does not read source rule texts, so it
|
|
37
|
+
* works even when the filter list content is not available.
|
|
38
|
+
*
|
|
39
|
+
* @param declarativeRuleId {@link DeclarativeRule|declarative rule} Id.
|
|
40
|
+
*
|
|
41
|
+
* @returns Promise with list of filter ids and source rule indexes.
|
|
42
|
+
*/
|
|
43
|
+
getSourceRuleIndexes(declarativeRuleId: number): Promise<SourceRuleIdxAndFilterId[]>;
|
|
32
44
|
/**
|
|
33
45
|
* Returns list of network rules with `$badfilter` option.
|
|
34
46
|
*
|
|
@@ -296,6 +308,8 @@ declare class RulesetWithSourceMap implements IRulesetWithSourceMap {
|
|
|
296
308
|
/** @inheritdoc */
|
|
297
309
|
getRulesById(declarativeRuleId: number): Promise<SourceRuleAndFilterId[]>;
|
|
298
310
|
/** @inheritdoc */
|
|
311
|
+
getSourceRuleIndexes(declarativeRuleId: number): Promise<SourceRuleIdxAndFilterId[]>;
|
|
312
|
+
/** @inheritdoc */
|
|
299
313
|
getBadFilterRules(): Rule[];
|
|
300
314
|
/** @inheritdoc */
|
|
301
315
|
getRulesHashMap(): IRulesHashMap;
|
package/dist/utils/string.js
CHANGED
|
@@ -78,6 +78,32 @@ import { getErrorMessage } from './error.js';
|
|
|
78
78
|
*/ function toASCII(strings) {
|
|
79
79
|
return strings.map(prepareASCII);
|
|
80
80
|
}
|
|
81
|
+
/**
|
|
82
|
+
* Creates a copy of a string which does not share storage with the original.
|
|
83
|
+
*
|
|
84
|
+
* A slice of a large string keeps the whole string alive in V8, so a value
|
|
85
|
+
* built from a sliced filter line would retain the whole filter content.
|
|
86
|
+
*
|
|
87
|
+
* @param str String to copy.
|
|
88
|
+
*
|
|
89
|
+
* @returns A new string with the same characters.
|
|
90
|
+
*/ function copyString(str) {
|
|
91
|
+
return Array.from(str).join('');
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Percent-encodes non-ASCII characters, leaving ASCII characters as is.
|
|
95
|
+
*
|
|
96
|
+
* Unlike {@link prepareASCII}, which converts a domain to punycode, this is
|
|
97
|
+
* meant for the rest of a URL filter: DNR matches it against a URL where
|
|
98
|
+
* non-ASCII characters are percent-encoded in UTF-8.
|
|
99
|
+
*
|
|
100
|
+
* @param str String to convert.
|
|
101
|
+
*
|
|
102
|
+
* @returns A string containing only ASCII characters.
|
|
103
|
+
*/ function encodeNonAscii(str) {
|
|
104
|
+
// eslint-disable-next-line no-control-regex
|
|
105
|
+
return str.replace(/[^\x00-\x7F]/gu, (char)=>encodeURIComponent(char));
|
|
106
|
+
}
|
|
81
107
|
/**
|
|
82
108
|
* Removes slashes from the beginning and end of the string.
|
|
83
109
|
*
|
|
@@ -177,4 +203,4 @@ import { getErrorMessage } from './error.js';
|
|
|
177
203
|
return words;
|
|
178
204
|
}
|
|
179
205
|
|
|
180
|
-
export { TAB, fastHash, fastHash31, hasSpaces, isASCII, isRegexPattern, prepareASCII, removeSlashes, serializeJson, splitByDelimiterWithEscapeCharacter, toASCII };
|
|
206
|
+
export { TAB, copyString, encodeNonAscii, fastHash, fastHash31, hasSpaces, isASCII, isRegexPattern, prepareASCII, removeSlashes, serializeJson, splitByDelimiterWithEscapeCharacter, toASCII };
|
package/package.json
CHANGED
|
@@ -55,12 +55,12 @@
|
|
|
55
55
|
"@adguard/re2-wasm": "1.2.0"
|
|
56
56
|
},
|
|
57
57
|
"dependencies": {
|
|
58
|
-
"@adguard/scriptlets": "2.
|
|
58
|
+
"@adguard/scriptlets": "2.6.0",
|
|
59
59
|
"commander": "^12.1.0",
|
|
60
60
|
"punycode": "2.3.1",
|
|
61
61
|
"valibot": "^1.1.0",
|
|
62
|
-
"@adguard/
|
|
63
|
-
"@adguard/
|
|
62
|
+
"@adguard/logger": "^2.0.1",
|
|
63
|
+
"@adguard/agtree": "^4.4.0"
|
|
64
64
|
},
|
|
65
65
|
"devDependencies": {
|
|
66
66
|
"@rollup/plugin-commonjs": "^28.0.1",
|
|
@@ -94,10 +94,10 @@
|
|
|
94
94
|
"tsx": "^4.19.3",
|
|
95
95
|
"typescript": "^5.7.3",
|
|
96
96
|
"vitest": "^5.0.0",
|
|
97
|
-
"@adguard/
|
|
98
|
-
"@adguard/
|
|
97
|
+
"@adguard/eslint-plugin-logger-context": "^1.0.1",
|
|
98
|
+
"@adguard/tsurlfilter": "^6.1.0"
|
|
99
99
|
},
|
|
100
|
-
"version": "2.
|
|
100
|
+
"version": "2.1.0",
|
|
101
101
|
"scripts": {
|
|
102
102
|
"lint": "pnpm lint:code && pnpm lint:types && pnpm lint:md",
|
|
103
103
|
"lint:code": "eslint --cache --ignore-path .gitignore .",
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file Utility functions for comparing string arrays.
|
|
3
|
-
*/ /**
|
|
4
|
-
* Checks whether two nullable string arrays are equal (same elements in same order).
|
|
5
|
-
* Both `null` is considered equal.
|
|
6
|
-
*
|
|
7
|
-
* @param left First array or `null`.
|
|
8
|
-
* @param right Second array or `null`.
|
|
9
|
-
*
|
|
10
|
-
* @returns `true` when both are `null` or both arrays have the same elements in the same order.
|
|
11
|
-
*/ function stringArraysEqual(left, right) {
|
|
12
|
-
if (left === null && right === null) {
|
|
13
|
-
return true;
|
|
14
|
-
}
|
|
15
|
-
if (left === null || right === null) {
|
|
16
|
-
return false;
|
|
17
|
-
}
|
|
18
|
-
if (left.length !== right.length) {
|
|
19
|
-
return false;
|
|
20
|
-
}
|
|
21
|
-
for(let i = 0; i < left.length; i += 1){
|
|
22
|
-
if (left[i] !== right[i]) {
|
|
23
|
-
return false;
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
return true;
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* Checks whether two nullable string arrays share at least one element.
|
|
30
|
-
* `null` on either side is treated as a wildcard (no filter applied), so the
|
|
31
|
-
* function returns `true` whenever at least one side is `null`.
|
|
32
|
-
*
|
|
33
|
-
* This matches the tsurlfilter semantics used in `negatesBadfilter()`: a
|
|
34
|
-
* `$badfilter` rule without a `$domain` modifier (`null`) should cancel a
|
|
35
|
-
* target rule that has `$domain=x.com`, because the badfilter's domain list
|
|
36
|
-
* is unrestricted.
|
|
37
|
-
*
|
|
38
|
-
* @param left First array or `null`.
|
|
39
|
-
* @param right Second array or `null`.
|
|
40
|
-
*
|
|
41
|
-
* @returns `true` when either side is `null`, or at least one common element exists.
|
|
42
|
-
*/ function stringArraysHaveIntersection(left, right) {
|
|
43
|
-
if (left === null || right === null) {
|
|
44
|
-
return true;
|
|
45
|
-
}
|
|
46
|
-
return new Set(left).intersection(new Set(right)).size > 0;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export { stringArraysEqual, stringArraysHaveIntersection };
|