@adguard/dnr-converter 1.1.2 → 2.0.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/LICENSE +674 -0
- package/README.md +75 -5
- package/dist/cli.cjs +694 -179
- package/dist/errors/metadata-errors/invalid-metadata-chunks-error.js +23 -0
- package/dist/index.js +2 -0
- package/dist/package.json.js +144 -2
- package/dist/ruleset/compact-ruleset.js +32 -0
- package/dist/ruleset/metadata-chunks.js +178 -0
- package/dist/ruleset/metadata-rule.js +100 -28
- package/dist/ruleset/metadata-ruleset.js +23 -31
- package/dist/ruleset/ruleset-with-source-map.js +135 -46
- package/dist/types/src/errors/metadata-errors/invalid-metadata-chunks-error.d.ts +21 -0
- package/dist/types/src/index.d.ts +2 -0
- package/dist/types/src/ruleset/compact-ruleset.d.ts +52 -0
- package/dist/types/src/ruleset/metadata-ruleset.d.ts +10 -6
- package/dist/types/src/ruleset/ruleset-with-source-map.d.ts +120 -3
- package/dist/types/src/version.d.ts +6 -0
- package/dist/utils/string.js +1 -1
- package/dist/version.js +11 -2
- package/package.json +9 -11
|
@@ -41,6 +41,16 @@ interface IRulesetWithSourceMap extends IBaseRuleset {
|
|
|
41
41
|
* @returns Dictionary with hashes of all ruleset's source rules.
|
|
42
42
|
*/
|
|
43
43
|
getRulesHashMap(): IRulesHashMap;
|
|
44
|
+
/**
|
|
45
|
+
* Returns the number of metadata rules at the beginning of the ruleset
|
|
46
|
+
* file the rule set was read from. They are static DNR rules too and take
|
|
47
|
+
* static rules quota, but are not counted by
|
|
48
|
+
* {@link IBaseRuleset.getSafeRulesCount}. 0 for a rule set that was not
|
|
49
|
+
* read from a file, e.g. right after conversion.
|
|
50
|
+
*
|
|
51
|
+
* @returns Number of metadata rules.
|
|
52
|
+
*/
|
|
53
|
+
getMetadataRulesCount(): number;
|
|
44
54
|
/**
|
|
45
55
|
* For provided source returns list of ids of converted declarative rule.
|
|
46
56
|
*
|
|
@@ -68,7 +78,11 @@ interface IRulesetWithSourceMap extends IBaseRuleset {
|
|
|
68
78
|
*/
|
|
69
79
|
unloadContent(): void;
|
|
70
80
|
/**
|
|
71
|
-
* Serializes rule set to a single file
|
|
81
|
+
* Serializes rule set to a single file: a JSON array that starts with
|
|
82
|
+
* metadata rules carrying the serialized {@link CompactRulesetEnvelope}
|
|
83
|
+
* in `metadata.chunk` fragments (each `metadata` value at most 64 KiB),
|
|
84
|
+
* followed by the ordinary declarative rules. Metadata rules take the
|
|
85
|
+
* smallest ids not used by ordinary rules or by `unsafeRules`.
|
|
72
86
|
*
|
|
73
87
|
* @param unsafeRules List of unsafe rules to add to the serialized output.
|
|
74
88
|
* Number of unsafe rules will be excluded from the counter of declarative
|
|
@@ -110,6 +124,25 @@ type SerializedRulesetData = {
|
|
|
110
124
|
badFilterRulesRaw: string[];
|
|
111
125
|
unsafeRules: DeclarativeRule[];
|
|
112
126
|
};
|
|
127
|
+
/**
|
|
128
|
+
* Payload of a filter ruleset file written by
|
|
129
|
+
* {@link RulesetWithSourceMap.serializeCompact}: it is serialized to JSON,
|
|
130
|
+
* split into fragments and stored in the leading metadata rules of the file.
|
|
131
|
+
*/
|
|
132
|
+
type CompactRulesetEnvelope = {
|
|
133
|
+
/**
|
|
134
|
+
* Data needed to create the ruleset instantly.
|
|
135
|
+
*/
|
|
136
|
+
metadata: SerializedRulesetData;
|
|
137
|
+
/**
|
|
138
|
+
* Data needed only to find and show source rules.
|
|
139
|
+
*/
|
|
140
|
+
lazyMetadata: SerializedRulesetLazyData;
|
|
141
|
+
/**
|
|
142
|
+
* Original filter text.
|
|
143
|
+
*/
|
|
144
|
+
filterContent: string;
|
|
145
|
+
};
|
|
113
146
|
/**
|
|
114
147
|
* A serialized rule set with primitive values separated into two parts: one is
|
|
115
148
|
* needed for instant creating ruleset, while the other is needed only when
|
|
@@ -180,6 +213,10 @@ declare class RulesetWithSourceMap implements IRulesetWithSourceMap {
|
|
|
180
213
|
* Converted declarative regexp rules.
|
|
181
214
|
*/
|
|
182
215
|
private readonly regexpRulesCount;
|
|
216
|
+
/**
|
|
217
|
+
* Number of metadata rules in the ruleset file the rule set was read from.
|
|
218
|
+
*/
|
|
219
|
+
private readonly metadataRulesCount;
|
|
183
220
|
/**
|
|
184
221
|
* Source map for declarative rules.
|
|
185
222
|
*/
|
|
@@ -219,8 +256,10 @@ declare class RulesetWithSourceMap implements IRulesetWithSourceMap {
|
|
|
219
256
|
* @param badFilterRules List of rules with $badfilter modifier.
|
|
220
257
|
* @param rulesHashMap Dictionary with hashes for all source rules.
|
|
221
258
|
* @param unsafeRules List of unsafe DNR rules.
|
|
259
|
+
* @param metadataRulesCount Number of metadata rules in the ruleset file
|
|
260
|
+
* the rule set was read from, 0 if it was not read from a file.
|
|
222
261
|
*/
|
|
223
|
-
constructor(id: string, safeRulesCount: number, unsafeRulesCount: number, regexpRulesCount: number, rulesetContentProvider: RulesetContentProvider, badFilterRules: Rule[], rulesHashMap: IRulesHashMap, unsafeRules: DeclarativeRule[]);
|
|
262
|
+
constructor(id: string, safeRulesCount: number, unsafeRulesCount: number, regexpRulesCount: number, rulesetContentProvider: RulesetContentProvider, badFilterRules: Rule[], rulesHashMap: IRulesHashMap, unsafeRules: DeclarativeRule[], metadataRulesCount?: number);
|
|
224
263
|
/** @inheritdoc */
|
|
225
264
|
getUnsafeRules(): Promise<DeclarativeRule[]>;
|
|
226
265
|
/** @inheritdoc */
|
|
@@ -230,6 +269,8 @@ declare class RulesetWithSourceMap implements IRulesetWithSourceMap {
|
|
|
230
269
|
/** @inheritdoc */
|
|
231
270
|
getRegexpRulesCount(): number;
|
|
232
271
|
/** @inheritdoc */
|
|
272
|
+
getMetadataRulesCount(): number;
|
|
273
|
+
/** @inheritdoc */
|
|
233
274
|
getId(): string;
|
|
234
275
|
/**
|
|
235
276
|
* Returns a list of pairs of source text rules and their filter identifiers
|
|
@@ -296,6 +337,82 @@ declare class RulesetWithSourceMap implements IRulesetWithSourceMap {
|
|
|
296
337
|
* is not available.
|
|
297
338
|
*/
|
|
298
339
|
static deserialize(id: string, rawData: string, loadLazyData: () => Promise<string>, loadDeclarativeRules: () => Promise<string>, filterList: IFilter[]): Promise<DeserializedRuleset>;
|
|
340
|
+
/**
|
|
341
|
+
* Creates a rule set from the content of a compact ruleset file read by
|
|
342
|
+
* {@link parseCompactRuleset}, without serializing its parts back to
|
|
343
|
+
* strings. The metadata is validated at once with the same schema as in
|
|
344
|
+
* {@link RulesetWithSourceMap.deserialize}; the lazy metadata and the
|
|
345
|
+
* declarative rules are validated when the content is first loaded.
|
|
346
|
+
*
|
|
347
|
+
* The rule set keeps references to `content.lazyMetadata` and
|
|
348
|
+
* `content.declarativeRules` for its whole lifetime, so the parsed file
|
|
349
|
+
* stays in memory. Calling {@link RulesetWithSourceMap.unloadContent}
|
|
350
|
+
* releases only the validated copies made on load, not this content. When
|
|
351
|
+
* the content must be released, for example in a long-running extension,
|
|
352
|
+
* keep it in storage and use {@link RulesetWithSourceMap.deserialize} with
|
|
353
|
+
* loaders instead.
|
|
354
|
+
*
|
|
355
|
+
* @param rulesetId Id of rule set.
|
|
356
|
+
* @param content Parsed content of the ruleset file: `metadata`,
|
|
357
|
+
* `lazyMetadata`, ordinary `declarativeRules` and `metadataRulesCount`, as
|
|
358
|
+
* returned by {@link parseCompactRuleset}.
|
|
359
|
+
* @param filterList List of {@link IFilter} the rule set was converted from.
|
|
360
|
+
*
|
|
361
|
+
* @returns Rule set ready to use.
|
|
362
|
+
*
|
|
363
|
+
* @throws Error {@link UnavailableRulesetSourceError} if the metadata does
|
|
364
|
+
* not match {@link SerializedRulesetData}. Errors of
|
|
365
|
+
* {@link RulesetWithSourceMap.fromDeserialized} come unwrapped.
|
|
366
|
+
*/
|
|
367
|
+
static fromCompact(rulesetId: string, content: Pick<CompactRulesetEnvelope, 'metadata' | 'lazyMetadata'> & {
|
|
368
|
+
declarativeRules: DeclarativeRule[];
|
|
369
|
+
metadataRulesCount: number;
|
|
370
|
+
}, filterList: IFilter[]): RulesetWithSourceMap;
|
|
371
|
+
/**
|
|
372
|
+
* Creates a rule set from the result of
|
|
373
|
+
* {@link RulesetWithSourceMap.deserialize}: builds the `$badfilter` rules
|
|
374
|
+
* and the rules hash map from the serialized data and calls the
|
|
375
|
+
* constructor.
|
|
376
|
+
*
|
|
377
|
+
* @param deserialized Deserialized rule set data and content provider.
|
|
378
|
+
* @param metadataRulesCount Number of metadata rules in the ruleset file
|
|
379
|
+
* the data was read from, `metadataRulesCount` of
|
|
380
|
+
* {@link parseCompactRuleset}.
|
|
381
|
+
*
|
|
382
|
+
* @returns Rule set ready to use.
|
|
383
|
+
*
|
|
384
|
+
* @throws Error if a `$badfilter` rule text cannot be parsed
|
|
385
|
+
* ({@link Rule.createFromText}), and `SyntaxError` if the serialized rules
|
|
386
|
+
* hash map is not valid JSON ({@link RulesHashMap.deserializeSources}).
|
|
387
|
+
* Both come unwrapped: the data is produced by this converter, so such an
|
|
388
|
+
* error is a producer bug, not an unavailable source.
|
|
389
|
+
*/
|
|
390
|
+
static fromDeserialized(deserialized: DeserializedRuleset, metadataRulesCount: number): RulesetWithSourceMap;
|
|
391
|
+
/**
|
|
392
|
+
* Validates serialized rule set data.
|
|
393
|
+
*
|
|
394
|
+
* @param id Id of rule set.
|
|
395
|
+
* @param getData Returns the data to validate; parsing errors thrown by it
|
|
396
|
+
* are wrapped as well.
|
|
397
|
+
*
|
|
398
|
+
* @returns Validated rule set data.
|
|
399
|
+
*
|
|
400
|
+
* @throws Error {@link UnavailableRulesetSourceError} if the data cannot be
|
|
401
|
+
* obtained or does not match {@link SerializedRulesetData}.
|
|
402
|
+
*/
|
|
403
|
+
private static parseData;
|
|
404
|
+
/**
|
|
405
|
+
* Creates a content provider that loads and validates the lazy metadata
|
|
406
|
+
* once and validates declarative rules on each load.
|
|
407
|
+
*
|
|
408
|
+
* @param id Id of rule set.
|
|
409
|
+
* @param loadLazyData Loads the parsed {@link SerializedRulesetLazyData}.
|
|
410
|
+
* @param loadDeclarativeRules Loads the parsed list of declarative rules.
|
|
411
|
+
* @param filterList List of {@link IFilter}.
|
|
412
|
+
*
|
|
413
|
+
* @returns Rule set content provider.
|
|
414
|
+
*/
|
|
415
|
+
private static createContentProvider;
|
|
299
416
|
/**
|
|
300
417
|
* Helper method to get serialized rule set data.
|
|
301
418
|
*
|
|
@@ -315,4 +432,4 @@ declare class RulesetWithSourceMap implements IRulesetWithSourceMap {
|
|
|
315
432
|
}
|
|
316
433
|
|
|
317
434
|
export { RulesetWithSourceMap };
|
|
318
|
-
export type { DeserializedRuleset, IRulesetWithSourceMap, RulesetContentProvider, SerializedRuleset, SerializedRulesetData, SerializedRulesetLazyData };
|
|
435
|
+
export type { CompactRulesetEnvelope, DeserializedRuleset, IRulesetWithSourceMap, RulesetContentProvider, SerializedRuleset, SerializedRulesetData, SerializedRulesetLazyData };
|
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Version of the `@adguard/dnr-converter` package.
|
|
3
|
+
*
|
|
4
|
+
* Annotated as `string` (not inferred from `packageJson.version`) because the
|
|
5
|
+
* committed manifest is versionless: before injection TypeScript narrows
|
|
6
|
+
* `packageJson.version` to `never`, which would leak an unstable `never` type
|
|
7
|
+
* into the public API (`never` is assignable to `string`, and the runtime guard
|
|
8
|
+
* above still enforces non-emptyness in both states).
|
|
3
9
|
*/
|
|
4
10
|
declare const DNR_CONVERTER_VERSION: string;
|
|
5
11
|
|
package/dist/utils/string.js
CHANGED
|
@@ -177,4 +177,4 @@ import { getErrorMessage } from './error.js';
|
|
|
177
177
|
return words;
|
|
178
178
|
}
|
|
179
179
|
|
|
180
|
-
export { fastHash, fastHash31, hasSpaces, isASCII, isRegexPattern, prepareASCII, removeSlashes, serializeJson, splitByDelimiterWithEscapeCharacter, toASCII };
|
|
180
|
+
export { TAB, fastHash, fastHash31, hasSpaces, isASCII, isRegexPattern, prepareASCII, removeSlashes, serializeJson, splitByDelimiterWithEscapeCharacter, toASCII };
|
package/dist/version.js
CHANGED
|
@@ -1,7 +1,16 @@
|
|
|
1
|
-
import
|
|
1
|
+
import packageJson from './package.json.js';
|
|
2
2
|
|
|
3
|
+
if (!('version' in packageJson) || "string" !== 'string' || packageJson.version.length === 0) {
|
|
4
|
+
throw new Error('Package version is missing. Run scripts/inject-package-versions.mjs before building.');
|
|
5
|
+
}
|
|
3
6
|
/**
|
|
4
7
|
* Version of the `@adguard/dnr-converter` package.
|
|
5
|
-
|
|
8
|
+
*
|
|
9
|
+
* Annotated as `string` (not inferred from `packageJson.version`) because the
|
|
10
|
+
* committed manifest is versionless: before injection TypeScript narrows
|
|
11
|
+
* `packageJson.version` to `never`, which would leak an unstable `never` type
|
|
12
|
+
* into the public API (`never` is assignable to `string`, and the runtime guard
|
|
13
|
+
* above still enforces non-emptyness in both states).
|
|
14
|
+
*/ const DNR_CONVERTER_VERSION = packageJson.version;
|
|
6
15
|
|
|
7
16
|
export { DNR_CONVERTER_VERSION };
|
package/package.json
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adguard/dnr-converter",
|
|
3
|
-
"version": "1.1.2",
|
|
4
3
|
"description": "A converter that transforms adblock-style filtering rules into rules compatible with the Declarative Net Request (DNR) API.",
|
|
5
4
|
"main": "dist/index.js",
|
|
6
5
|
"module": "dist/index.js",
|
|
@@ -8,8 +7,7 @@
|
|
|
8
7
|
"type": "module",
|
|
9
8
|
"sideEffects": false,
|
|
10
9
|
"files": [
|
|
11
|
-
"dist"
|
|
12
|
-
"!dist/build.txt"
|
|
10
|
+
"dist"
|
|
13
11
|
],
|
|
14
12
|
"exports": {
|
|
15
13
|
".": {
|
|
@@ -37,7 +35,7 @@
|
|
|
37
35
|
"author": "Adguard Software Ltd.",
|
|
38
36
|
"repository": {
|
|
39
37
|
"type": "git",
|
|
40
|
-
"url": "git+https://github.com/
|
|
38
|
+
"url": "git+https://github.com/AdGuardSoftwareLimited/ext-tsurlfilter.git",
|
|
41
39
|
"directory": "packages/dnr-converter"
|
|
42
40
|
},
|
|
43
41
|
"license": "GPL-3.0-only",
|
|
@@ -61,8 +59,8 @@
|
|
|
61
59
|
"commander": "^12.1.0",
|
|
62
60
|
"punycode": "2.3.1",
|
|
63
61
|
"valibot": "^1.1.0",
|
|
64
|
-
"@adguard/
|
|
65
|
-
"@adguard/
|
|
62
|
+
"@adguard/agtree": "^4.2.1",
|
|
63
|
+
"@adguard/logger": "^2.0.1"
|
|
66
64
|
},
|
|
67
65
|
"devDependencies": {
|
|
68
66
|
"@rollup/plugin-commonjs": "^28.0.1",
|
|
@@ -75,7 +73,7 @@
|
|
|
75
73
|
"@types/punycode": "2.1.4",
|
|
76
74
|
"@typescript-eslint/eslint-plugin": "^7.5.0",
|
|
77
75
|
"@typescript-eslint/parser": "^7.5.0",
|
|
78
|
-
"@vitest/coverage-v8": "^
|
|
76
|
+
"@vitest/coverage-v8": "^5.0.0",
|
|
79
77
|
"eslint": "8.57.1",
|
|
80
78
|
"eslint-config-airbnb-base": "15.0.0",
|
|
81
79
|
"eslint-config-airbnb-typescript": "^18.0.0",
|
|
@@ -95,10 +93,11 @@
|
|
|
95
93
|
"tsd": "^0.32.0",
|
|
96
94
|
"tsx": "^4.19.3",
|
|
97
95
|
"typescript": "^5.7.3",
|
|
98
|
-
"vitest": "^
|
|
99
|
-
"@adguard/
|
|
100
|
-
"@adguard/
|
|
96
|
+
"vitest": "^5.0.0",
|
|
97
|
+
"@adguard/tsurlfilter": "^6.0.3",
|
|
98
|
+
"@adguard/eslint-plugin-logger-context": "^1.0.1"
|
|
101
99
|
},
|
|
100
|
+
"version": "2.0.0",
|
|
102
101
|
"scripts": {
|
|
103
102
|
"lint": "pnpm lint:code && pnpm lint:types && pnpm lint:md",
|
|
104
103
|
"lint:code": "eslint --cache --ignore-path .gitignore .",
|
|
@@ -106,7 +105,6 @@
|
|
|
106
105
|
"lint:md": "markdownlint .",
|
|
107
106
|
"start": "rollup -c rollup.config.ts --configPlugin typescript -w",
|
|
108
107
|
"build": "rimraf dist && rollup -c rollup.config.ts --configPlugin typescript",
|
|
109
|
-
"postbuild": "tsx tasks/build-txt.ts",
|
|
110
108
|
"docs:examples": "tsx tasks/generate-examples.ts",
|
|
111
109
|
"test": "vitest run",
|
|
112
110
|
"test:light": "vitest run --exclude \"**/*benchmark*.test.ts\"",
|