@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,54 @@
|
|
|
1
|
+
import * as v from 'valibot';
|
|
2
|
+
import { isSafeRule } from '../utils/is-safe-rule.js';
|
|
3
|
+
import { DeclarativeRuleValidator } from '../declarative-rule/declarative-rule.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Rule set that holds converted declarative rules in memory without
|
|
7
|
+
* source maps, hash maps, or lazy loading. Implements {@link IRuleset}
|
|
8
|
+
* and is returned by the simple conversion flow ({@link FilterConverter}).
|
|
9
|
+
*/ class Ruleset {
|
|
10
|
+
/** @inheritdoc */ getId() {
|
|
11
|
+
return this.id;
|
|
12
|
+
}
|
|
13
|
+
/** @inheritdoc */ getSafeRulesCount() {
|
|
14
|
+
return this.safeRulesCount;
|
|
15
|
+
}
|
|
16
|
+
/** @inheritdoc */ getUnsafeRulesCount() {
|
|
17
|
+
return this.unsafeRulesCount;
|
|
18
|
+
}
|
|
19
|
+
/** @inheritdoc */ getRegexpRulesCount() {
|
|
20
|
+
return this.regexpRulesCount;
|
|
21
|
+
}
|
|
22
|
+
/** @inheritdoc */ getDeclarativeRules() {
|
|
23
|
+
return this.declarativeRules;
|
|
24
|
+
}
|
|
25
|
+
/** @inheritdoc */ serialize() {
|
|
26
|
+
return JSON.stringify(this.declarativeRules);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Reconstructs a {@link Ruleset} from a JSON string produced by
|
|
30
|
+
* {@link serialize}.
|
|
31
|
+
*
|
|
32
|
+
* @param id Rule set identifier.
|
|
33
|
+
* @param json JSON string produced by {@link serialize}.
|
|
34
|
+
*
|
|
35
|
+
* @returns Reconstructed {@link Ruleset}.
|
|
36
|
+
*/ static deserialize(id, json) {
|
|
37
|
+
const rules = v.parse(v.array(DeclarativeRuleValidator), JSON.parse(json));
|
|
38
|
+
return new Ruleset(id, rules);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Creates a new {@link Ruleset}.
|
|
42
|
+
*
|
|
43
|
+
* @param id Rule set identifier.
|
|
44
|
+
* @param declarativeRules All converted declarative rules.
|
|
45
|
+
*/ constructor(id, declarativeRules){
|
|
46
|
+
this.id = id;
|
|
47
|
+
this.declarativeRules = declarativeRules;
|
|
48
|
+
this.safeRulesCount = declarativeRules.filter((r)=>isSafeRule(r)).length;
|
|
49
|
+
this.unsafeRulesCount = declarativeRules.filter((r)=>!isSafeRule(r)).length;
|
|
50
|
+
this.regexpRulesCount = declarativeRules.filter(({ condition: { regexFilter } })=>regexFilter !== undefined).length;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export { Ruleset };
|
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
import * as v from 'valibot';
|
|
2
|
+
import { getErrorMessage } from '../utils/error.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Base64 alphabet used by the standard source-map VLQ encoding.
|
|
6
|
+
*/ const BASE64_CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
|
7
|
+
/**
|
|
8
|
+
* Precomputed reverse-lookup table: maps a base64 char code to its index in
|
|
9
|
+
* {@link BASE64_CHARS} (or `-1` for char codes outside the 0–127 ASCII range
|
|
10
|
+
* or not present in the alphabet).
|
|
11
|
+
*
|
|
12
|
+
* Indexed by char code so the runtime decode path avoids allocating a string
|
|
13
|
+
* (`String.fromCharCode`) and doing a linear `indexOf` scan over the 64-char
|
|
14
|
+
* alphabet for every digit.
|
|
15
|
+
*/ const BASE64_REVERSE = (()=>{
|
|
16
|
+
const table = new Int8Array(128).fill(-1);
|
|
17
|
+
for(let i = 0; i < BASE64_CHARS.length; i += 1){
|
|
18
|
+
table[BASE64_CHARS.charCodeAt(i)] = i;
|
|
19
|
+
}
|
|
20
|
+
return table;
|
|
21
|
+
})();
|
|
22
|
+
/**
|
|
23
|
+
* Number of data bits carried by a single VLQ base64 digit.
|
|
24
|
+
*/ const VLQ_BASE_SHIFT = 5;
|
|
25
|
+
/**
|
|
26
|
+
* Mask for the data bits of a single VLQ digit.
|
|
27
|
+
*/ const VLQ_BASE_MASK = (1 << VLQ_BASE_SHIFT) - 1;
|
|
28
|
+
/**
|
|
29
|
+
* Continuation bit: set on a VLQ digit when more digits follow.
|
|
30
|
+
*/ const VLQ_CONTINUATION_BIT = 1 << VLQ_BASE_SHIFT;
|
|
31
|
+
/**
|
|
32
|
+
* Encodes a single non-negative integer as a base64 VLQ string (standard
|
|
33
|
+
* source-map VLQ). A sign bit is used (bit 0 of the first decoded value), but
|
|
34
|
+
* all source-map values are non-negative, so the sign bit is always 0.
|
|
35
|
+
*
|
|
36
|
+
* Arithmetic (not bitwise) is used for the magnitude to support values up to
|
|
37
|
+
* 2^31-1 (declarative rule IDs are text hashes) without 32-bit overflow.
|
|
38
|
+
*
|
|
39
|
+
* Note: every value is non-negative, so an unsigned VLQ variant (without the
|
|
40
|
+
* sign bit) would save one bit per value. We keep the standard, well-understood
|
|
41
|
+
* source-map codec for interoperability and readability rather than shaving a
|
|
42
|
+
* marginal amount of size.
|
|
43
|
+
*
|
|
44
|
+
* @param value Non-negative integer to encode.
|
|
45
|
+
*
|
|
46
|
+
* @returns Base64 VLQ string for the value.
|
|
47
|
+
*/ const encodeVlq = (value)=>{
|
|
48
|
+
let result = '';
|
|
49
|
+
// source-map VLQ sign convention: bit 0 = sign, remaining bits = magnitude.
|
|
50
|
+
// Use arithmetic to avoid 32-bit overflow for values >= 2^30.
|
|
51
|
+
let vlq = value < 0 ? -value * 2 + 1 : value * 2;
|
|
52
|
+
do {
|
|
53
|
+
let digit = vlq % 32;
|
|
54
|
+
vlq = Math.floor(vlq / 32);
|
|
55
|
+
if (vlq > 0) {
|
|
56
|
+
digit |= VLQ_CONTINUATION_BIT;
|
|
57
|
+
}
|
|
58
|
+
result += BASE64_CHARS[digit];
|
|
59
|
+
}while (vlq > 0)
|
|
60
|
+
return result;
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* Decodes a base64 VLQ string starting at the given index, returning the
|
|
64
|
+
* decoded integer and the next index to read.
|
|
65
|
+
*
|
|
66
|
+
* Arithmetic (not bitwise) is used for the magnitude to support values up to
|
|
67
|
+
* 2^31-1 (declarative rule IDs are text hashes) without 32-bit overflow.
|
|
68
|
+
*
|
|
69
|
+
* @param str VLQ string.
|
|
70
|
+
* @param pos Index to start decoding at.
|
|
71
|
+
*
|
|
72
|
+
* @returns Tuple of the decoded integer and the next unread index.
|
|
73
|
+
*
|
|
74
|
+
* @throws Error When a character is not a valid base64 VLQ digit.
|
|
75
|
+
*/ const decodeVlq = (str, pos)=>{
|
|
76
|
+
let result = 0;
|
|
77
|
+
let shift = 1;
|
|
78
|
+
let next = pos;
|
|
79
|
+
let continuation = true;
|
|
80
|
+
do {
|
|
81
|
+
const ch = str.charCodeAt(next);
|
|
82
|
+
next += 1;
|
|
83
|
+
const base64Digit = ch < 128 ? BASE64_REVERSE[ch] : -1;
|
|
84
|
+
if (base64Digit === -1) {
|
|
85
|
+
throw new Error('Invalid base64 VLQ digit');
|
|
86
|
+
}
|
|
87
|
+
const digit = base64Digit & VLQ_BASE_MASK;
|
|
88
|
+
result += digit * shift;
|
|
89
|
+
shift *= 32;
|
|
90
|
+
continuation = (base64Digit & VLQ_CONTINUATION_BIT) !== 0;
|
|
91
|
+
}while (continuation)
|
|
92
|
+
const negate = result % 2 === 1;
|
|
93
|
+
const value = Math.floor(result / 2);
|
|
94
|
+
return [
|
|
95
|
+
negate ? -value : value,
|
|
96
|
+
next
|
|
97
|
+
];
|
|
98
|
+
};
|
|
99
|
+
/**
|
|
100
|
+
* Decodes a VLQ-encoded source-map string into an array of triples.
|
|
101
|
+
*
|
|
102
|
+
* @param str VLQ string (comma-separated segments).
|
|
103
|
+
*
|
|
104
|
+
* @returns Array of `[declarativeRuleId, sourceRuleIndex, filterId]` triples.
|
|
105
|
+
*
|
|
106
|
+
* @throws Error When the VLQ string is malformed or a segment does not contain
|
|
107
|
+
* exactly 3 values.
|
|
108
|
+
*/ const decodeSourceMapVlq = (str)=>{
|
|
109
|
+
if (str.length === 0) {
|
|
110
|
+
return [];
|
|
111
|
+
}
|
|
112
|
+
return str.split(',').map((segment)=>{
|
|
113
|
+
const triple = [];
|
|
114
|
+
let pos = 0;
|
|
115
|
+
for(let i = 0; i < 3; i += 1){
|
|
116
|
+
const [value, next] = decodeVlq(segment, pos);
|
|
117
|
+
triple.push(value);
|
|
118
|
+
pos = next;
|
|
119
|
+
}
|
|
120
|
+
// Reject trailing data after exactly 3 values so malformed/crafted
|
|
121
|
+
// segments cannot decode to silently-wrong data. This hardens the
|
|
122
|
+
// deserialization contract documented above.
|
|
123
|
+
if (pos !== segment.length) {
|
|
124
|
+
throw new Error('Invalid VLQ segment: trailing data after 3 values');
|
|
125
|
+
}
|
|
126
|
+
return triple;
|
|
127
|
+
});
|
|
128
|
+
};
|
|
129
|
+
/**
|
|
130
|
+
* Validator for a single serialized source-map entry.
|
|
131
|
+
*
|
|
132
|
+
* Each entry is a strict triple of non-negative integers in the order
|
|
133
|
+
* [declarativeRuleId, sourceRuleIndex, filterId].
|
|
134
|
+
*
|
|
135
|
+
* After the VLQ adoption, this validator is applied to each decoded triple to
|
|
136
|
+
* ensure values are non-negative integers (the VLQ decoder can produce negative
|
|
137
|
+
* values from a crafted malicious string).
|
|
138
|
+
*/ const SourceMapItemValidator = v.strictTuple([
|
|
139
|
+
v.pipe(v.number(), v.integer(), v.minValue(0)),
|
|
140
|
+
v.pipe(v.number(), v.integer(), v.minValue(0)),
|
|
141
|
+
v.pipe(v.number(), v.integer(), v.minValue(0))
|
|
142
|
+
]);
|
|
143
|
+
/**
|
|
144
|
+
* Validator for a serialized source map: an array of
|
|
145
|
+
* [declarativeRuleId, sourceRuleIndex, filterId] triples.
|
|
146
|
+
*/ const SourceMapValidator = v.array(SourceMapItemValidator);
|
|
147
|
+
/**
|
|
148
|
+
* Contains a list of records with source rule ID, converted rule ID
|
|
149
|
+
* and filter ID.
|
|
150
|
+
* Can return the source filter and rule for the provided conversion rule ID.
|
|
151
|
+
*/ class SourceMap {
|
|
152
|
+
/**
|
|
153
|
+
* Creates unique key for provided pair of source rule and filter id.
|
|
154
|
+
*
|
|
155
|
+
* @param source Pair of source rule and filter id.
|
|
156
|
+
*
|
|
157
|
+
* @returns Unique key for dictionary.
|
|
158
|
+
*/ static getKeyFromSource(source) {
|
|
159
|
+
return `${source.filterId}_${source.sourceRuleIndex}`;
|
|
160
|
+
}
|
|
161
|
+
/** @inheritdoc */ getByDeclarativeRuleId(ruleId) {
|
|
162
|
+
return this.ruleIdMap.get(ruleId) || [];
|
|
163
|
+
}
|
|
164
|
+
/** @inheritdoc */ getBySourceRuleIndex(source) {
|
|
165
|
+
const key = SourceMap.getKeyFromSource(source);
|
|
166
|
+
return this.declarativeIdMap.get(key) || [];
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Deserializes array of sources from a VLQ-encoded string.
|
|
170
|
+
*
|
|
171
|
+
* The serialized source map uses base64 VLQ encoding (standard source-map
|
|
172
|
+
* VLQ) to compactly represent `[declarativeRuleId, sourceRuleIndex,
|
|
173
|
+
* filterId]` integer triples. Each triple is one comma-separated segment;
|
|
174
|
+
* values within a segment are concatenated without a separator because VLQ
|
|
175
|
+
* is self-delimiting. Decoded triples are validated to ensure all values
|
|
176
|
+
* are non-negative integers.
|
|
177
|
+
*
|
|
178
|
+
* For backward compatibility, a string starting with `[` is parsed as the
|
|
179
|
+
* legacy JSON-array format persisted by previous `@adguard/tsurlfilter`
|
|
180
|
+
* versions (before the VLQ encoding). This lets an in-place extension
|
|
181
|
+
* upgrade read source maps persisted in the old format transparently,
|
|
182
|
+
* rather than failing to deserialize them.
|
|
183
|
+
*
|
|
184
|
+
* @param sourceString VLQ-encoded source-map string (produced by
|
|
185
|
+
* `serialize()`), or a legacy JSON-array string.
|
|
186
|
+
*
|
|
187
|
+
* @returns List of sources.
|
|
188
|
+
*
|
|
189
|
+
* @throws Error When `sourceString` is not valid VLQ/JSON or contains
|
|
190
|
+
* negative values. The thrown error carries a descriptive message.
|
|
191
|
+
*/ static deserializeSources(sourceString) {
|
|
192
|
+
// Backward-compatible with the legacy JSON-array format. VLQ strings
|
|
193
|
+
// never start with '[', so a leading bracket unambiguously identifies
|
|
194
|
+
// the old JSON-array format persisted by previous @adguard/tsurlfilter
|
|
195
|
+
// versions. An extension that upgrades in place may still hold
|
|
196
|
+
// old-format source maps in IndexedDB; recover them transparently
|
|
197
|
+
// rather than throwing a hard error (otherwise the filtering log would
|
|
198
|
+
// break on an update until the ruleset is re-synced).
|
|
199
|
+
try {
|
|
200
|
+
const triples = sourceString.startsWith('[') ? JSON.parse(sourceString) : decodeSourceMapVlq(sourceString);
|
|
201
|
+
const arr = v.parse(SourceMapValidator, triples);
|
|
202
|
+
return arr.map((item)=>({
|
|
203
|
+
declarativeRuleId: item[0],
|
|
204
|
+
sourceRuleIndex: item[1],
|
|
205
|
+
filterId: item[2]
|
|
206
|
+
}));
|
|
207
|
+
} catch (e) {
|
|
208
|
+
throw new Error(`Cannot deserialize source map, got error: ${getErrorMessage(e)}`);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Serializes source map to a base64 VLQ string.
|
|
213
|
+
*
|
|
214
|
+
* A base64 VLQ encoding was evaluated against the previous compact JSON
|
|
215
|
+
* format on a representative 100k+ line ruleset (EasyList + EasyPrivacy,
|
|
216
|
+
* 109,517 triples) and the full chromium-mv3 rule set (50 rulesets,
|
|
217
|
+
* 316,742 triples). VLQ reduced the source-map size by 38.51% (2,426,515 B
|
|
218
|
+
* → 1,492,126 B, saving 912 KB) on the single-filter sample and by 39.25%
|
|
219
|
+
* (7,171,443 B → 4,356,603 B, saving 2.68 MB) across the full set. The
|
|
220
|
+
* substantial absolute savings (multi-MB at scale) justify the custom
|
|
221
|
+
* codec and the shift away from human-readable JSON arrays. Persisted data
|
|
222
|
+
* can still be in the legacy JSON-array format across releases, so
|
|
223
|
+
* {@link SourceMap.deserializeSources} reads both formats. The measurement
|
|
224
|
+
* is recorded in `benchmarks/vlq-results.md` and is reproducible via
|
|
225
|
+
* `pnpm bench:vlq`.
|
|
226
|
+
*
|
|
227
|
+
* @returns VLQ-encoded string.
|
|
228
|
+
*/ serialize() {
|
|
229
|
+
// Build the VLQ string in a single pass over `this.sources`, appending
|
|
230
|
+
// each comma-separated segment directly to the result string. This
|
|
231
|
+
// avoids the two intermediate 100k+ element allocations that the
|
|
232
|
+
// previous `map` → `map` → `join` pipeline produced (one for the plain
|
|
233
|
+
// triples array and one for the VLQ-encoded segments).
|
|
234
|
+
let result = '';
|
|
235
|
+
for(let i = 0; i < this.sources.length; i += 1){
|
|
236
|
+
if (i > 0) {
|
|
237
|
+
result += ',';
|
|
238
|
+
}
|
|
239
|
+
const s = this.sources[i];
|
|
240
|
+
result += encodeVlq(s.declarativeRuleId) + encodeVlq(s.sourceRuleIndex) + encodeVlq(s.filterId);
|
|
241
|
+
}
|
|
242
|
+
return result;
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Creates new SourceMap from provided list of sources.
|
|
246
|
+
*
|
|
247
|
+
* @param sources List of sources.
|
|
248
|
+
*/ constructor(sources){
|
|
249
|
+
/**
|
|
250
|
+
* List of sources.
|
|
251
|
+
*/ this.sources = [];
|
|
252
|
+
/**
|
|
253
|
+
* Needs for fast search for source rule.
|
|
254
|
+
*/ this.ruleIdMap = new Map();
|
|
255
|
+
/**
|
|
256
|
+
* Needs for fast search for source rule.
|
|
257
|
+
*/ this.declarativeIdMap = new Map();
|
|
258
|
+
this.sources = sources;
|
|
259
|
+
// For fast search
|
|
260
|
+
this.sources.forEach((item)=>{
|
|
261
|
+
const { sourceRuleIndex, filterId, declarativeRuleId } = item;
|
|
262
|
+
// Fill source rules map.
|
|
263
|
+
const existingSourcePairs = this.ruleIdMap.get(declarativeRuleId);
|
|
264
|
+
const value = {
|
|
265
|
+
sourceRuleIndex,
|
|
266
|
+
filterId
|
|
267
|
+
};
|
|
268
|
+
const newSourceValue = existingSourcePairs ? existingSourcePairs.concat(value) : [
|
|
269
|
+
value
|
|
270
|
+
];
|
|
271
|
+
this.ruleIdMap.set(declarativeRuleId, newSourceValue);
|
|
272
|
+
// Fill declarative IDs map.
|
|
273
|
+
const key = SourceMap.getKeyFromSource(value);
|
|
274
|
+
const existingDeclarativeIdsPairs = this.declarativeIdMap.get(key);
|
|
275
|
+
const newDeclarativeIdsValue = existingDeclarativeIdsPairs ? existingDeclarativeIdsPairs.concat(declarativeRuleId) : [
|
|
276
|
+
declarativeRuleId
|
|
277
|
+
];
|
|
278
|
+
this.declarativeIdMap.set(key, newDeclarativeIdsValue);
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
export { SourceMap };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Options for the convert filters function.
|
|
3
|
+
*/
|
|
4
|
+
interface ConvertFiltersOptions {
|
|
5
|
+
/**
|
|
6
|
+
* If true, additional information is printed during conversion.
|
|
7
|
+
*/
|
|
8
|
+
debug?: boolean;
|
|
9
|
+
/**
|
|
10
|
+
* Defines whether to prettify the rulesets JSON or not.
|
|
11
|
+
*/
|
|
12
|
+
prettifyJson?: boolean;
|
|
13
|
+
/**
|
|
14
|
+
* Additional properties that can be passed to the converter to record
|
|
15
|
+
* inside metadata ruleset.
|
|
16
|
+
*/
|
|
17
|
+
additionalProperties?: Record<string, unknown>;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Converts filters with textual rules from the provided path to declarative
|
|
21
|
+
* rulesets and saves them with counters, source map and list of source filter
|
|
22
|
+
* identifiers on the specified path.
|
|
23
|
+
*
|
|
24
|
+
* @param filtersAndMetadataDir Path to source filters with metadata to convert.
|
|
25
|
+
* @param resourcesDir Path to web accessible resources.
|
|
26
|
+
* @param destRulesetsDir Destination path for declarative rulesets.
|
|
27
|
+
* @param options Options for convert filters.
|
|
28
|
+
*/
|
|
29
|
+
declare const convertFilters: (filtersAndMetadataDir: string, resourcesDir: string, destRulesetsDir: string, options?: ConvertFiltersOptions) => Promise<void>;
|
|
30
|
+
|
|
31
|
+
export { convertFilters };
|
|
32
|
+
export type { ConvertFiltersOptions };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { ConvertFiltersOptions, convertFilters } from './convert-filters.js';
|
|
2
|
+
export { generateMD5Hash, getIdFromFilterName } from './utils.js';
|
|
3
|
+
export { RULESET_FILE_EXT, extractRulesetId, getRulesetId, getRulesetPath } from '../src/utils/ruleset-utils.js';
|
|
4
|
+
|
|
5
|
+
declare const DEFAULT_DEST_RULESETS_DIR = "./build/rulesets";
|
|
6
|
+
|
|
7
|
+
export { DEFAULT_DEST_RULESETS_DIR };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generates an MD5 hash from a given input string.
|
|
3
|
+
*
|
|
4
|
+
* @param input The input string to be hashed.
|
|
5
|
+
*
|
|
6
|
+
* @returns The MD5 hash of the input string in hexadecimal format.
|
|
7
|
+
*/
|
|
8
|
+
declare function generateMD5Hash(input: string): string;
|
|
9
|
+
/**
|
|
10
|
+
* Extracts the filter ID from a given filter name.
|
|
11
|
+
*
|
|
12
|
+
* @param filterName The filter name in the format `filter_{filterId}.txt`.
|
|
13
|
+
*
|
|
14
|
+
* @returns The extracted filter ID, or null if the filter name does not match
|
|
15
|
+
* the expected format.
|
|
16
|
+
*/
|
|
17
|
+
declare const getIdFromFilterName: (filterName: string) => number | null;
|
|
18
|
+
|
|
19
|
+
export { generateMD5Hash, getIdFromFilterName };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { RuleAction } from './rule-action.js';
|
|
2
|
+
import { RuleCondition } from './rule-condition.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Interface that represents declarative rule that will apply {@link action} when {@link condition} is fulfilled.
|
|
6
|
+
*
|
|
7
|
+
* @see {@link https://developer.chrome.com/docs/extensions/reference/api/declarativeNetRequest#type-Rule}
|
|
8
|
+
*/
|
|
9
|
+
interface DeclarativeRule {
|
|
10
|
+
/**
|
|
11
|
+
* The action to take if this rule is matched.
|
|
12
|
+
*
|
|
13
|
+
* @see {@link RuleAction}
|
|
14
|
+
*/
|
|
15
|
+
action: RuleAction;
|
|
16
|
+
/**
|
|
17
|
+
* The condition under which this rule is triggered.
|
|
18
|
+
*
|
|
19
|
+
* @see {@link RuleCondition}
|
|
20
|
+
*/
|
|
21
|
+
condition: RuleCondition;
|
|
22
|
+
/**
|
|
23
|
+
* An id which uniquely identifies a rule. Mandatory and should be >= `1`.
|
|
24
|
+
*/
|
|
25
|
+
id: number;
|
|
26
|
+
/**
|
|
27
|
+
* Rule priority. Defaults to `1`. When specified, should be >= `1`.
|
|
28
|
+
*/
|
|
29
|
+
priority?: number;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export type { DeclarativeRule };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Interface that represents header info to match condition on response headers.
|
|
3
|
+
*
|
|
4
|
+
* @see {@link https://developer.chrome.com/docs/extensions/reference/api/declarativeNetRequest#type-HeaderInfo}
|
|
5
|
+
*
|
|
6
|
+
* @since Chrome 128
|
|
7
|
+
*/
|
|
8
|
+
interface HeaderInfo {
|
|
9
|
+
/**
|
|
10
|
+
* If specified, this condition matches if the header's value
|
|
11
|
+
* matches at least one pattern in this list. This supports
|
|
12
|
+
* case-insensitive header value matching plus the following constructs:
|
|
13
|
+
* - `'*'`: Matches any number of characters.
|
|
14
|
+
* - `'?'`: Matches zero or one character(s).
|
|
15
|
+
* - `'*'` and `'?'` can be escaped with a backslash, e.g. `'\*'` and `'\?'`.
|
|
16
|
+
*/
|
|
17
|
+
values?: string[];
|
|
18
|
+
/**
|
|
19
|
+
* If specified, this condition is not matched if the header
|
|
20
|
+
* exists but its value contains at least one element in this list.
|
|
21
|
+
* This uses the same match pattern syntax as {@link values}.
|
|
22
|
+
*/
|
|
23
|
+
excludedValues?: string[];
|
|
24
|
+
/**
|
|
25
|
+
* The name of the header. This condition matches on the name only
|
|
26
|
+
* if both {@link values} and {@link excludedValues} are not specified.
|
|
27
|
+
*/
|
|
28
|
+
header: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export type { HeaderInfo };
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Enum that represents the possible operations for a header modification.
|
|
3
|
+
*
|
|
4
|
+
* @see {@link https://developer.chrome.com/docs/extensions/reference/declarativeNetRequest/#type-HeaderOperation}
|
|
5
|
+
*
|
|
6
|
+
* @since Chrome 86
|
|
7
|
+
*/
|
|
8
|
+
declare enum HeaderOperation {
|
|
9
|
+
/**
|
|
10
|
+
* Adds a new entry for the specified header.
|
|
11
|
+
* This operation is not supported for request headers.
|
|
12
|
+
*/
|
|
13
|
+
Append = "append",
|
|
14
|
+
/**
|
|
15
|
+
* Sets a new value for the specified header, removing any existing headers with the same name.
|
|
16
|
+
*/
|
|
17
|
+
Set = "set",
|
|
18
|
+
/**
|
|
19
|
+
* Removes all entries for the specified header.
|
|
20
|
+
*/
|
|
21
|
+
Remove = "remove"
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Interface that represents header info modification operation.
|
|
25
|
+
*
|
|
26
|
+
* @see {@link https://developer.chrome.com/docs/extensions/reference/declarativeNetRequest/#type-ModifyHeaderInfo}
|
|
27
|
+
*
|
|
28
|
+
* @since Chrome 86
|
|
29
|
+
*/
|
|
30
|
+
interface ModifyHeaderInfo {
|
|
31
|
+
/**
|
|
32
|
+
* The name of the header to be modified.
|
|
33
|
+
*/
|
|
34
|
+
header: string;
|
|
35
|
+
/**
|
|
36
|
+
* The operation to be performed on a header.
|
|
37
|
+
*/
|
|
38
|
+
operation: HeaderOperation;
|
|
39
|
+
/**
|
|
40
|
+
* The new value for the header. Must be specified for `append` and `set` operations.
|
|
41
|
+
*/
|
|
42
|
+
value?: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export { HeaderOperation };
|
|
46
|
+
export type { ModifyHeaderInfo };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Interface that represents query key-value pair with optional {@link replaceOnly} flag.
|
|
3
|
+
*
|
|
4
|
+
* @see {@link https://developer.chrome.com/docs/extensions/reference/declarativeNetRequest/#type-QueryKeyValue}
|
|
5
|
+
*/
|
|
6
|
+
interface QueryKeyValue {
|
|
7
|
+
/**
|
|
8
|
+
* The name of the query parameter.
|
|
9
|
+
*/
|
|
10
|
+
key: string;
|
|
11
|
+
/**
|
|
12
|
+
* If `true`, the query key is replaced only if it's already present.
|
|
13
|
+
* Otherwise, the key is also added if it's missing. Defaults to `false`.
|
|
14
|
+
*
|
|
15
|
+
* @since Chrome 94
|
|
16
|
+
*/
|
|
17
|
+
replaceOnly?: boolean;
|
|
18
|
+
/**
|
|
19
|
+
* The value of the query parameter.
|
|
20
|
+
*/
|
|
21
|
+
value: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export type { QueryKeyValue };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { QueryKeyValue } from './query-key-value.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Interface that represents query transformation options.
|
|
5
|
+
*
|
|
6
|
+
* @see {@link https://developer.chrome.com/docs/extensions/reference/declarativeNetRequest/#type-QueryTransform}
|
|
7
|
+
*/
|
|
8
|
+
interface QueryTransform {
|
|
9
|
+
/**
|
|
10
|
+
* The list of query key-value pairs to be added or replaced.
|
|
11
|
+
*
|
|
12
|
+
* @see {@link QueryKeyValue}
|
|
13
|
+
*/
|
|
14
|
+
addOrReplaceParams?: QueryKeyValue[];
|
|
15
|
+
/**
|
|
16
|
+
* The list of query keys to be removed.
|
|
17
|
+
*/
|
|
18
|
+
removeParams?: string[];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export type { QueryTransform };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { URLTransform } from './url-transform.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Interface that represents a redirect action.
|
|
5
|
+
*
|
|
6
|
+
* @see {@link https://developer.chrome.com/docs/extensions/reference/declarativeNetRequest/#type-Redirect}
|
|
7
|
+
*/
|
|
8
|
+
interface Redirect {
|
|
9
|
+
/**
|
|
10
|
+
* Path relative to the extension directory. Should start with `'/'`.
|
|
11
|
+
*/
|
|
12
|
+
extensionPath?: string;
|
|
13
|
+
/**
|
|
14
|
+
* Substitution pattern for rules which specify a `regexFilter`.
|
|
15
|
+
* The first match of `regexFilter` within the URL will be replaced with this pattern.
|
|
16
|
+
* Within {@link regexSubstitution}, backslash-escaped digits (\1 to \9) can be used
|
|
17
|
+
* to insert the corresponding capture groups. \0 refers to the entire matching text.
|
|
18
|
+
*/
|
|
19
|
+
regexSubstitution?: string;
|
|
20
|
+
/**
|
|
21
|
+
* URL transformations to perform.
|
|
22
|
+
*
|
|
23
|
+
* @see {@link URLTransform}
|
|
24
|
+
*/
|
|
25
|
+
transform?: URLTransform;
|
|
26
|
+
/**
|
|
27
|
+
* The redirect URL. Redirects to JavaScript URLs are not allowed.
|
|
28
|
+
*/
|
|
29
|
+
url?: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export type { Redirect };
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { ModifyHeaderInfo } from './modify-header-info.js';
|
|
2
|
+
import { Redirect } from './redirect.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Enum that represents the kind of action to take if a given condition matches.
|
|
6
|
+
*
|
|
7
|
+
* @see {@link https://developer.chrome.com/docs/extensions/reference/declarativeNetRequest/#type-RuleActionType}
|
|
8
|
+
*/
|
|
9
|
+
declare enum RuleActionType {
|
|
10
|
+
/**
|
|
11
|
+
* Block the network request.
|
|
12
|
+
*/
|
|
13
|
+
Block = "block",
|
|
14
|
+
/**
|
|
15
|
+
* Redirect the network request.
|
|
16
|
+
*/
|
|
17
|
+
Redirect = "redirect",
|
|
18
|
+
/**
|
|
19
|
+
* Allow the network request.
|
|
20
|
+
*
|
|
21
|
+
* The request won't be intercepted if there is an allow rule which matches it.
|
|
22
|
+
*/
|
|
23
|
+
Allow = "allow",
|
|
24
|
+
/**
|
|
25
|
+
* Upgrade the network request url's scheme to `https` if the request is `http` or `ftp`.
|
|
26
|
+
*/
|
|
27
|
+
UpgradeScheme = "upgradeScheme",
|
|
28
|
+
/**
|
|
29
|
+
* Modify request/response headers from the network request.
|
|
30
|
+
*
|
|
31
|
+
* @since Chrome 86
|
|
32
|
+
*/
|
|
33
|
+
ModifyHeaders = "modifyHeaders",
|
|
34
|
+
/**
|
|
35
|
+
* Allow all requests within a frame hierarchy, including the frame request itself.
|
|
36
|
+
*/
|
|
37
|
+
AllowAllRequests = "allowAllRequests"
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Interface that represents a rule action to be performed when a rule condition is matched.
|
|
41
|
+
*
|
|
42
|
+
* @see {@link https://developer.chrome.com/docs/extensions/reference/declarativeNetRequest/#type-RuleAction}
|
|
43
|
+
*/
|
|
44
|
+
interface RuleAction {
|
|
45
|
+
/**
|
|
46
|
+
* Describes how the redirect should be performed. Only valid for redirect rules.
|
|
47
|
+
*/
|
|
48
|
+
redirect?: Redirect;
|
|
49
|
+
/**
|
|
50
|
+
* The request headers to modify for the request. Only valid for modify headers rules.
|
|
51
|
+
*
|
|
52
|
+
* @since Chrome 86
|
|
53
|
+
*/
|
|
54
|
+
requestHeaders?: ModifyHeaderInfo[];
|
|
55
|
+
/**
|
|
56
|
+
* The response headers to modify for the request. Only valid for modify headers rules.
|
|
57
|
+
*
|
|
58
|
+
* @since Chrome 86
|
|
59
|
+
*/
|
|
60
|
+
responseHeaders?: ModifyHeaderInfo[];
|
|
61
|
+
/**
|
|
62
|
+
* The type of action to perform.
|
|
63
|
+
*/
|
|
64
|
+
type: RuleActionType;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export { RuleActionType };
|
|
68
|
+
export type { RuleAction };
|