@adguard/dnr-converter 1.1.2 → 2.0.0-beta.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 +8 -10
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as v from 'valibot';
|
|
2
2
|
import { getRulesetId } from '../utils/ruleset-utils.js';
|
|
3
3
|
import { serializeJson } from '../utils/string.js';
|
|
4
|
-
import {
|
|
4
|
+
import { MetadataRules } from './metadata-rule.js';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Metadata ruleset ID.
|
|
@@ -10,7 +10,8 @@ import { createMetadataRule } from './metadata-rule.js';
|
|
|
10
10
|
* Checksum map validator.
|
|
11
11
|
*/ const checksumMapValidator = v.record(v.string(), v.string());
|
|
12
12
|
/**
|
|
13
|
-
* Metadata validator.
|
|
13
|
+
* Metadata validator. Uses `v.strictObject` so unknown payload keys are
|
|
14
|
+
* rejected at deserialization time.
|
|
14
15
|
*/ const metadataValidator = v.strictObject({
|
|
15
16
|
/**
|
|
16
17
|
* Checksums for all rulesets.
|
|
@@ -22,16 +23,6 @@ import { createMetadataRule } from './metadata-rule.js';
|
|
|
22
23
|
* Validation should be performed by users.
|
|
23
24
|
*/ additionalProperties: v.record(v.string(), v.unknown())
|
|
24
25
|
});
|
|
25
|
-
/**
|
|
26
|
-
* Metadata rule validator.
|
|
27
|
-
*
|
|
28
|
-
* Uses `v.looseObject` to allow additional Chrome DNR fields (`id`,
|
|
29
|
-
* `priority`, `action`, `condition` etc.) that are outside our control.
|
|
30
|
-
* The nested `metadata` field uses `v.strictObject()` so unknown metadata
|
|
31
|
-
* keys are rejected at deserialization time.
|
|
32
|
-
*/ const metadataRuleValidator = v.looseObject({
|
|
33
|
-
metadata: metadataValidator
|
|
34
|
-
});
|
|
35
26
|
/**
|
|
36
27
|
* Represents a specialized metadata ruleset for managing and validating
|
|
37
28
|
* metadata associated with various rulesets.
|
|
@@ -54,7 +45,7 @@ import { createMetadataRule } from './metadata-rule.js';
|
|
|
54
45
|
* @param rulesetId Rule set id.
|
|
55
46
|
* @param checksum Checksum.
|
|
56
47
|
*/ setChecksum(rulesetId, checksum) {
|
|
57
|
-
this.
|
|
48
|
+
this.metadata.checksums[rulesetId] = checksum;
|
|
58
49
|
}
|
|
59
50
|
/**
|
|
60
51
|
* Returns checksum for the specified rule set.
|
|
@@ -63,14 +54,14 @@ import { createMetadataRule } from './metadata-rule.js';
|
|
|
63
54
|
*
|
|
64
55
|
* @returns Checksum or undefined if not found.
|
|
65
56
|
*/ getChecksum(rulesetId) {
|
|
66
|
-
return this.
|
|
57
|
+
return this.metadata.checksums[rulesetId];
|
|
67
58
|
}
|
|
68
59
|
/**
|
|
69
60
|
* Returns all rule set ids in the metadata.
|
|
70
61
|
*
|
|
71
62
|
* @returns Rule set ids.
|
|
72
63
|
*/ getRulesetIds() {
|
|
73
|
-
return Object.keys(this.
|
|
64
|
+
return Object.keys(this.metadata.checksums);
|
|
74
65
|
}
|
|
75
66
|
/**
|
|
76
67
|
* Gets additional property.
|
|
@@ -79,7 +70,7 @@ import { createMetadataRule } from './metadata-rule.js';
|
|
|
79
70
|
*
|
|
80
71
|
* @returns Property value or undefined if not found.
|
|
81
72
|
*/ getAdditionalProperty(key) {
|
|
82
|
-
return this.
|
|
73
|
+
return this.metadata.additionalProperties[key];
|
|
83
74
|
}
|
|
84
75
|
/**
|
|
85
76
|
* Sets additional property.
|
|
@@ -88,7 +79,7 @@ import { createMetadataRule } from './metadata-rule.js';
|
|
|
88
79
|
* @param value Property value. The class does not validate that the value is
|
|
89
80
|
* JSON-serializable; callers are responsible for ensuring serializability.
|
|
90
81
|
*/ setAdditionalProperty(key, value) {
|
|
91
|
-
this.
|
|
82
|
+
this.metadata.additionalProperties[key] = value;
|
|
92
83
|
}
|
|
93
84
|
/**
|
|
94
85
|
* Checks whether additional property exists.
|
|
@@ -97,25 +88,26 @@ import { createMetadataRule } from './metadata-rule.js';
|
|
|
97
88
|
*
|
|
98
89
|
* @returns Whether the property exists.
|
|
99
90
|
*/ hasAdditionalProperty(key) {
|
|
100
|
-
return Object.hasOwn(this.
|
|
91
|
+
return Object.hasOwn(this.metadata.additionalProperties, key);
|
|
101
92
|
}
|
|
102
93
|
/**
|
|
103
94
|
* Removes additional property.
|
|
104
95
|
*
|
|
105
96
|
* @param key Property key.
|
|
106
97
|
*/ removeAdditionalProperty(key) {
|
|
107
|
-
delete this.
|
|
98
|
+
delete this.metadata.additionalProperties[key];
|
|
108
99
|
}
|
|
109
100
|
/**
|
|
110
|
-
* Serializes the ruleset to a string
|
|
101
|
+
* Serializes the ruleset to a string: a JSON array of metadata rules
|
|
102
|
+
* carrying the payload in `metadata.chunk` fragments.
|
|
111
103
|
*
|
|
112
104
|
* @param pretty Whether to prettify the output.
|
|
113
105
|
*
|
|
114
106
|
* @returns Serialized ruleset.
|
|
115
107
|
*/ serialize(pretty = false) {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
108
|
+
// The metadata ruleset has no ordinary rules, so no ids are taken.
|
|
109
|
+
const rules = MetadataRules.create(this.metadata, new Set(), pretty);
|
|
110
|
+
return serializeJson(rules, pretty);
|
|
119
111
|
}
|
|
120
112
|
/**
|
|
121
113
|
* Deserializes the ruleset from a string.
|
|
@@ -124,14 +116,14 @@ import { createMetadataRule } from './metadata-rule.js';
|
|
|
124
116
|
*
|
|
125
117
|
* @returns Deserialized ruleset.
|
|
126
118
|
*
|
|
127
|
-
* @throws {ValiError} If the metadata fails schema validation.
|
|
128
119
|
* @throws {SyntaxError} If `rawJson` is not valid JSON.
|
|
120
|
+
* @throws {InvalidMetadataChunksError} If the metadata rules are missing
|
|
121
|
+
* or malformed.
|
|
122
|
+
* @throws {ValiError} If the payload fails schema validation.
|
|
129
123
|
*/ static deserialize(rawJson) {
|
|
130
|
-
const
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
}
|
|
134
|
-
const { metadata: { checksums, additionalProperties } } = v.parse(metadataRuleValidator, parsed[0]);
|
|
124
|
+
const rulesetId = getRulesetId(METADATA_RULESET_ID);
|
|
125
|
+
const { payload } = MetadataRules.read(rulesetId, JSON.parse(rawJson));
|
|
126
|
+
const { checksums, additionalProperties } = v.parse(metadataValidator, payload);
|
|
135
127
|
return new MetadataRuleset(checksums, additionalProperties);
|
|
136
128
|
}
|
|
137
129
|
/**
|
|
@@ -148,14 +140,14 @@ import { createMetadataRule } from './metadata-rule.js';
|
|
|
148
140
|
* @note
|
|
149
141
|
* Inputs are shallow-cloned so the instance owns its internal state.
|
|
150
142
|
*/ constructor(checksums = {}, additionalProperties = {}){
|
|
151
|
-
this.
|
|
143
|
+
this.metadata = {
|
|
152
144
|
checksums: {
|
|
153
145
|
...checksums
|
|
154
146
|
},
|
|
155
147
|
additionalProperties: {
|
|
156
148
|
...additionalProperties
|
|
157
149
|
}
|
|
158
|
-
}
|
|
150
|
+
};
|
|
159
151
|
}
|
|
160
152
|
}
|
|
161
153
|
|
|
@@ -4,7 +4,8 @@ import { getErrorMessage } from '../utils/error.js';
|
|
|
4
4
|
import { LazyLoader } from '../utils/lazy-loader.js';
|
|
5
5
|
import { serializeJson } from '../utils/string.js';
|
|
6
6
|
import { strictObjectByType } from '../utils/valibot.js';
|
|
7
|
-
import {
|
|
7
|
+
import { MetadataRules } from './metadata-rule.js';
|
|
8
|
+
import { RulesHashMap } from './rules-hash-map.js';
|
|
8
9
|
import { SourceMap } from './source-map.js';
|
|
9
10
|
import { UnavailableFilterSourceError } from '../errors/unavailable-sources-errors/unavailable-filter-source-error.js';
|
|
10
11
|
import { UnavailableRulesetSourceError } from '../errors/unavailable-sources-errors/unavailable-ruleset-source-error.js';
|
|
@@ -41,6 +42,9 @@ import { DeclarativeRuleValidator } from '../declarative-rule/declarative-rule.j
|
|
|
41
42
|
/** @inheritdoc */ getRegexpRulesCount() {
|
|
42
43
|
return this.regexpRulesCount;
|
|
43
44
|
}
|
|
45
|
+
/** @inheritdoc */ getMetadataRulesCount() {
|
|
46
|
+
return this.metadataRulesCount;
|
|
47
|
+
}
|
|
44
48
|
/** @inheritdoc */ getId() {
|
|
45
49
|
return this.id;
|
|
46
50
|
}
|
|
@@ -206,15 +210,106 @@ import { DeclarativeRuleValidator } from '../declarative-rule/declarative-rule.j
|
|
|
206
210
|
* @throws Error {@link UnavailableRulesetSourceError} if rule set source
|
|
207
211
|
* is not available.
|
|
208
212
|
*/ static async deserialize(id, rawData, loadLazyData, loadDeclarativeRules, filterList) {
|
|
209
|
-
|
|
213
|
+
const data = RulesetWithSourceMap.parseData(id, ()=>JSON.parse(rawData));
|
|
214
|
+
const rulesetContentProvider = RulesetWithSourceMap.createContentProvider(id, async ()=>JSON.parse(await loadLazyData()), async ()=>JSON.parse(await loadDeclarativeRules()), filterList);
|
|
215
|
+
return {
|
|
216
|
+
id,
|
|
217
|
+
data,
|
|
218
|
+
rulesetContentProvider
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Creates a rule set from the content of a compact ruleset file read by
|
|
223
|
+
* {@link parseCompactRuleset}, without serializing its parts back to
|
|
224
|
+
* strings. The metadata is validated at once with the same schema as in
|
|
225
|
+
* {@link RulesetWithSourceMap.deserialize}; the lazy metadata and the
|
|
226
|
+
* declarative rules are validated when the content is first loaded.
|
|
227
|
+
*
|
|
228
|
+
* The rule set keeps references to `content.lazyMetadata` and
|
|
229
|
+
* `content.declarativeRules` for its whole lifetime, so the parsed file
|
|
230
|
+
* stays in memory. Calling {@link RulesetWithSourceMap.unloadContent}
|
|
231
|
+
* releases only the validated copies made on load, not this content. When
|
|
232
|
+
* the content must be released, for example in a long-running extension,
|
|
233
|
+
* keep it in storage and use {@link RulesetWithSourceMap.deserialize} with
|
|
234
|
+
* loaders instead.
|
|
235
|
+
*
|
|
236
|
+
* @param rulesetId Id of rule set.
|
|
237
|
+
* @param content Parsed content of the ruleset file: `metadata`,
|
|
238
|
+
* `lazyMetadata`, ordinary `declarativeRules` and `metadataRulesCount`, as
|
|
239
|
+
* returned by {@link parseCompactRuleset}.
|
|
240
|
+
* @param filterList List of {@link IFilter} the rule set was converted from.
|
|
241
|
+
*
|
|
242
|
+
* @returns Rule set ready to use.
|
|
243
|
+
*
|
|
244
|
+
* @throws Error {@link UnavailableRulesetSourceError} if the metadata does
|
|
245
|
+
* not match {@link SerializedRulesetData}. Errors of
|
|
246
|
+
* {@link RulesetWithSourceMap.fromDeserialized} come unwrapped.
|
|
247
|
+
*/ static fromCompact(rulesetId, content, filterList) {
|
|
248
|
+
const data = RulesetWithSourceMap.parseData(rulesetId, ()=>content.metadata);
|
|
249
|
+
const rulesetContentProvider = RulesetWithSourceMap.createContentProvider(rulesetId, async ()=>content.lazyMetadata, async ()=>content.declarativeRules, filterList);
|
|
250
|
+
return RulesetWithSourceMap.fromDeserialized({
|
|
251
|
+
id: rulesetId,
|
|
252
|
+
data,
|
|
253
|
+
rulesetContentProvider
|
|
254
|
+
}, content.metadataRulesCount);
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Creates a rule set from the result of
|
|
258
|
+
* {@link RulesetWithSourceMap.deserialize}: builds the `$badfilter` rules
|
|
259
|
+
* and the rules hash map from the serialized data and calls the
|
|
260
|
+
* constructor.
|
|
261
|
+
*
|
|
262
|
+
* @param deserialized Deserialized rule set data and content provider.
|
|
263
|
+
* @param metadataRulesCount Number of metadata rules in the ruleset file
|
|
264
|
+
* the data was read from, `metadataRulesCount` of
|
|
265
|
+
* {@link parseCompactRuleset}.
|
|
266
|
+
*
|
|
267
|
+
* @returns Rule set ready to use.
|
|
268
|
+
*
|
|
269
|
+
* @throws Error if a `$badfilter` rule text cannot be parsed
|
|
270
|
+
* ({@link Rule.createFromText}), and `SyntaxError` if the serialized rules
|
|
271
|
+
* hash map is not valid JSON ({@link RulesHashMap.deserializeSources}).
|
|
272
|
+
* Both come unwrapped: the data is produced by this converter, so such an
|
|
273
|
+
* error is a producer bug, not an unavailable source.
|
|
274
|
+
*/ static fromDeserialized(deserialized, metadataRulesCount) {
|
|
275
|
+
const { id, data, rulesetContentProvider } = deserialized;
|
|
276
|
+
// Rules with `$badfilter` are used only for matching, not for source
|
|
277
|
+
// attribution, so filter id and rule index are not needed.
|
|
278
|
+
const badFilterRules = data.badFilterRulesRaw.flatMap((rawRule)=>Rule.createFromText(0, 0, rawRule));
|
|
279
|
+
const rulesHashMap = new RulesHashMap(RulesHashMap.deserializeSources(data.rulesetHashMapRaw));
|
|
280
|
+
return new RulesetWithSourceMap(id, data.safeRulesCount, data.unsafeRulesCount, data.regexpRulesCount, rulesetContentProvider, badFilterRules, rulesHashMap, data.unsafeRules, metadataRulesCount);
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Validates serialized rule set data.
|
|
284
|
+
*
|
|
285
|
+
* @param id Id of rule set.
|
|
286
|
+
* @param getData Returns the data to validate; parsing errors thrown by it
|
|
287
|
+
* are wrapped as well.
|
|
288
|
+
*
|
|
289
|
+
* @returns Validated rule set data.
|
|
290
|
+
*
|
|
291
|
+
* @throws Error {@link UnavailableRulesetSourceError} if the data cannot be
|
|
292
|
+
* obtained or does not match {@link SerializedRulesetData}.
|
|
293
|
+
*/ static parseData(id, getData) {
|
|
210
294
|
try {
|
|
211
|
-
|
|
212
|
-
data = v.parse(serializedRulesetDataValidator, objectFromString);
|
|
295
|
+
return v.parse(serializedRulesetDataValidator, getData());
|
|
213
296
|
} catch (e) {
|
|
214
297
|
// eslint-disable-next-line max-len
|
|
215
298
|
const msg = `Cannot parse serialized ruleset's data with id "${id}", got error: ${getErrorMessage(e)}`;
|
|
216
299
|
throw new UnavailableRulesetSourceError(msg, id, e);
|
|
217
300
|
}
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* Creates a content provider that loads and validates the lazy metadata
|
|
304
|
+
* once and validates declarative rules on each load.
|
|
305
|
+
*
|
|
306
|
+
* @param id Id of rule set.
|
|
307
|
+
* @param loadLazyData Loads the parsed {@link SerializedRulesetLazyData}.
|
|
308
|
+
* @param loadDeclarativeRules Loads the parsed list of declarative rules.
|
|
309
|
+
* @param filterList List of {@link IFilter}.
|
|
310
|
+
*
|
|
311
|
+
* @returns Rule set content provider.
|
|
312
|
+
*/ static createContentProvider(id, loadLazyData, loadDeclarativeRules, filterList) {
|
|
218
313
|
/**
|
|
219
314
|
* This variable is used as a singleton for all three functions
|
|
220
315
|
* (`loadSourceMap`, `loadFilterList`, `loadDeclarativeRules`) to load
|
|
@@ -225,9 +320,7 @@ import { DeclarativeRuleValidator } from '../declarative-rule/declarative-rule.j
|
|
|
225
320
|
return deserializedLazyData;
|
|
226
321
|
}
|
|
227
322
|
try {
|
|
228
|
-
const
|
|
229
|
-
const objectFromString = JSON.parse(lazyData);
|
|
230
|
-
const parsed = v.parse(serializedRulesetLazyDataValidator, objectFromString);
|
|
323
|
+
const parsed = v.parse(serializedRulesetLazyDataValidator, await loadLazyData());
|
|
231
324
|
deserializedLazyData = parsed;
|
|
232
325
|
return parsed;
|
|
233
326
|
} catch (e) {
|
|
@@ -236,28 +329,20 @@ import { DeclarativeRuleValidator } from '../declarative-rule/declarative-rule.j
|
|
|
236
329
|
throw new UnavailableRulesetSourceError(msg, id, e);
|
|
237
330
|
}
|
|
238
331
|
};
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
},
|
|
252
|
-
loadDeclarativeRules: async ()=>{
|
|
253
|
-
const rawFileContent = await loadDeclarativeRules();
|
|
254
|
-
const objectFromString = JSON.parse(rawFileContent);
|
|
255
|
-
const declarativeRules = v.parse(v.array(DeclarativeRuleValidator), objectFromString);
|
|
256
|
-
return declarativeRules;
|
|
257
|
-
}
|
|
332
|
+
return {
|
|
333
|
+
loadSourceMap: async ()=>{
|
|
334
|
+
const { sourceMapRaw } = await getLazyData();
|
|
335
|
+
const sources = SourceMap.deserializeSources(sourceMapRaw);
|
|
336
|
+
return new SourceMap(sources);
|
|
337
|
+
},
|
|
338
|
+
loadFilterList: async ()=>{
|
|
339
|
+
const { filterIds } = await getLazyData();
|
|
340
|
+
return filterList.filter((filter)=>filterIds.includes(filter.getId()));
|
|
341
|
+
},
|
|
342
|
+
loadDeclarativeRules: async ()=>{
|
|
343
|
+
return v.parse(v.array(DeclarativeRuleValidator), await loadDeclarativeRules());
|
|
258
344
|
}
|
|
259
345
|
};
|
|
260
|
-
return deserialized;
|
|
261
346
|
}
|
|
262
347
|
/**
|
|
263
348
|
* Helper method to get serialized rule set data.
|
|
@@ -328,27 +413,28 @@ import { DeclarativeRuleValidator } from '../declarative-rule/declarative-rule.j
|
|
|
328
413
|
const msg = `Unsafe rules count is not equal to the length of provided unsafe rules array in rule set '${id}'`;
|
|
329
414
|
throw new Error(msg);
|
|
330
415
|
}
|
|
331
|
-
const
|
|
416
|
+
const envelope = {
|
|
332
417
|
metadata: this.getSerializedRulesetData(unsafeRules),
|
|
333
418
|
lazyMetadata: this.getSerializedRulesetLazyData(),
|
|
334
419
|
filterContent: content
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
//
|
|
338
|
-
//
|
|
339
|
-
|
|
340
|
-
declarativeRules =
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
//
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
declarativeRules
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
420
|
+
};
|
|
421
|
+
const unsafeIds = new Set(unsafeRules.map((rule)=>rule.id));
|
|
422
|
+
// Exclude unsafe rules from declarative rules. `filter()` returns a
|
|
423
|
+
// new array, so the internal state of the ruleset is never mutated
|
|
424
|
+
// and repeated calls stay independent.
|
|
425
|
+
const declarativeRules = (await this.getDeclarativeRules()).filter((rule)=>!unsafeIds.has(rule.id));
|
|
426
|
+
// Metadata rules take the smallest ids not used by any rule of the
|
|
427
|
+
// file, including the unsafe rules stored inside the metadata, so
|
|
428
|
+
// no id appears twice anywhere in the ruleset.
|
|
429
|
+
const usedIds = new Set([
|
|
430
|
+
...unsafeIds,
|
|
431
|
+
...declarativeRules.map((rule)=>rule.id)
|
|
432
|
+
]);
|
|
433
|
+
const metadataRules = MetadataRules.create(envelope, usedIds, prettyPrint);
|
|
434
|
+
return serializeJson([
|
|
435
|
+
...metadataRules,
|
|
436
|
+
...declarativeRules
|
|
437
|
+
], prettyPrint);
|
|
352
438
|
}
|
|
353
439
|
/**
|
|
354
440
|
* Constructor of RulesetWithSourceMap.
|
|
@@ -362,7 +448,9 @@ import { DeclarativeRuleValidator } from '../declarative-rule/declarative-rule.j
|
|
|
362
448
|
* @param badFilterRules List of rules with $badfilter modifier.
|
|
363
449
|
* @param rulesHashMap Dictionary with hashes for all source rules.
|
|
364
450
|
* @param unsafeRules List of unsafe DNR rules.
|
|
365
|
-
|
|
451
|
+
* @param metadataRulesCount Number of metadata rules in the ruleset file
|
|
452
|
+
* the rule set was read from, 0 if it was not read from a file.
|
|
453
|
+
*/ constructor(id, safeRulesCount, unsafeRulesCount, regexpRulesCount, rulesetContentProvider, badFilterRules, rulesHashMap, unsafeRules, metadataRulesCount = 0){
|
|
366
454
|
/**
|
|
367
455
|
* Array of converted declarative rules.
|
|
368
456
|
*/ this.declarativeRules = [];
|
|
@@ -390,6 +478,7 @@ import { DeclarativeRuleValidator } from '../declarative-rule/declarative-rule.j
|
|
|
390
478
|
this.badFilterRules = badFilterRules;
|
|
391
479
|
this.rulesHashMap = rulesHashMap;
|
|
392
480
|
this.unsafeRules = unsafeRules;
|
|
481
|
+
this.metadataRulesCount = metadataRulesCount;
|
|
393
482
|
this.contentLoader = new LazyLoader(async ()=>{
|
|
394
483
|
const { loadSourceMap, loadFilterList, loadDeclarativeRules } = this.rulesetContentProvider;
|
|
395
484
|
this.sourceMap = await loadSourceMap();
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Describes an error when the metadata rules of a serialized ruleset are
|
|
3
|
+
* missing, malformed or do not form valid JSON.
|
|
4
|
+
*/
|
|
5
|
+
declare class InvalidMetadataChunksError extends Error {
|
|
6
|
+
/**
|
|
7
|
+
* Rule set id whose metadata could not be read.
|
|
8
|
+
*/
|
|
9
|
+
rulesetId: string;
|
|
10
|
+
/**
|
|
11
|
+
* Describes an error when the metadata rules of a serialized ruleset
|
|
12
|
+
* cannot be read. The message names the rule set and the reason.
|
|
13
|
+
*
|
|
14
|
+
* @param rulesetId Rule set id whose metadata could not be read.
|
|
15
|
+
* @param reason Why the metadata cannot be read.
|
|
16
|
+
* @param cause Basic error, describes why the metadata is unreadable.
|
|
17
|
+
*/
|
|
18
|
+
constructor(rulesetId: string, reason: string, cause?: Error);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export { InvalidMetadataChunksError };
|
|
@@ -12,6 +12,8 @@ export { HashWithSource, IRulesHashMap, RulesHashMap } from './ruleset/rules-has
|
|
|
12
12
|
export { ISourceMap } from './ruleset/source-map.js';
|
|
13
13
|
export { UnavailableFilterSourceError } from './errors/unavailable-sources-errors/unavailable-filter-source-error.js';
|
|
14
14
|
export { UnavailableRulesetSourceError } from './errors/unavailable-sources-errors/unavailable-ruleset-source-error.js';
|
|
15
|
+
export { InvalidMetadataChunksError } from './errors/metadata-errors/invalid-metadata-chunks-error.js';
|
|
16
|
+
export { CompactRulesetContent, parseCompactRuleset } from './ruleset/compact-ruleset.js';
|
|
15
17
|
export { METADATA_RULESET_ID, MetadataRuleset } from './ruleset/metadata-ruleset.js';
|
|
16
18
|
export { Rule } from './rule/rule.js';
|
|
17
19
|
export { RuleDeclarativeValidator } from './rule/rule-validator.js';
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { DeclarativeRule } from '../declarative-rule/declarative-rule.js';
|
|
2
|
+
import { CompactRulesetEnvelope } from './ruleset-with-source-map.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @file Reader for compact ruleset files produced by
|
|
6
|
+
* {@link RulesetWithSourceMap.serializeCompact}.
|
|
7
|
+
*
|
|
8
|
+
* The files are build artifacts of this converter, so the joined metadata
|
|
9
|
+
* payload is trusted to be a {@link CompactRulesetEnvelope}. Only the chunk
|
|
10
|
+
* layer is checked here; the nested `metadata` and `lazyMetadata` objects are
|
|
11
|
+
* validated by {@link RulesetWithSourceMap.fromCompact} or
|
|
12
|
+
* {@link RulesetWithSourceMap.deserialize}.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Content of a compact ruleset file: the envelope written by
|
|
17
|
+
* {@link RulesetWithSourceMap.serializeCompact}, the ordinary declarative
|
|
18
|
+
* rules and the number of metadata rules that precede them.
|
|
19
|
+
*/
|
|
20
|
+
type CompactRulesetContent = CompactRulesetEnvelope & {
|
|
21
|
+
/**
|
|
22
|
+
* Ordinary declarative rules in file order, without the metadata rules.
|
|
23
|
+
*/
|
|
24
|
+
declarativeRules: DeclarativeRule[];
|
|
25
|
+
/**
|
|
26
|
+
* Number of metadata rules at the beginning of the file. They are static
|
|
27
|
+
* DNR rules and count towards the static rule quota.
|
|
28
|
+
*/
|
|
29
|
+
metadataRulesCount: number;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* Reads a parsed filter ruleset file: joins the leading metadata rules,
|
|
33
|
+
* parses the metadata once and returns it with the ordinary rules and the
|
|
34
|
+
* number of metadata rules. The payload is taken as the envelope written by
|
|
35
|
+
* {@link RulesetWithSourceMap.serializeCompact} without further checks, and
|
|
36
|
+
* ordinary rules are returned as parsed; both are validated later by
|
|
37
|
+
* {@link RulesetWithSourceMap.fromCompact} or
|
|
38
|
+
* {@link RulesetWithSourceMap.deserialize} and the ruleset content provider.
|
|
39
|
+
*
|
|
40
|
+
* @param rulesetId Rule set id, used in error messages.
|
|
41
|
+
* @param parsedRuleset Parsed JSON content of a `ruleset_N.json` file.
|
|
42
|
+
*
|
|
43
|
+
* @returns Envelope fields, ordinary rules and metadata rule count.
|
|
44
|
+
*
|
|
45
|
+
* @throws {InvalidMetadataChunksError} If `parsedRuleset` is not an array,
|
|
46
|
+
* has no leading metadata rule, has a metadata rule without a string `chunk`,
|
|
47
|
+
* or if the joined fragments are not valid JSON.
|
|
48
|
+
*/
|
|
49
|
+
declare function parseCompactRuleset(rulesetId: string, parsedRuleset: unknown): CompactRulesetContent;
|
|
50
|
+
|
|
51
|
+
export { parseCompactRuleset };
|
|
52
|
+
export type { CompactRulesetContent };
|
|
@@ -4,8 +4,9 @@ import * as v from 'valibot';
|
|
|
4
4
|
* @file MetadataRuleset class for managing and serializing metadata rulesets.
|
|
5
5
|
*
|
|
6
6
|
* A metadata ruleset stores checksums and additional properties for a
|
|
7
|
-
* collection of DNR rulesets. It serializes to a JSON array
|
|
8
|
-
*
|
|
7
|
+
* collection of DNR rulesets. It serializes to a JSON array of metadata
|
|
8
|
+
* rules whose `metadata.chunk` fragments, joined in order, form the payload
|
|
9
|
+
* `{ checksums, additionalProperties }`.
|
|
9
10
|
*/
|
|
10
11
|
|
|
11
12
|
/**
|
|
@@ -29,9 +30,9 @@ type ChecksumMap = v.InferOutput<typeof checksumMapValidator>;
|
|
|
29
30
|
*/
|
|
30
31
|
declare class MetadataRuleset {
|
|
31
32
|
/**
|
|
32
|
-
*
|
|
33
|
+
* Checksums and additional properties.
|
|
33
34
|
*/
|
|
34
|
-
private
|
|
35
|
+
private metadata;
|
|
35
36
|
/**
|
|
36
37
|
* Creates an instance of the MetadataRuleset class.
|
|
37
38
|
*
|
|
@@ -105,7 +106,8 @@ declare class MetadataRuleset {
|
|
|
105
106
|
*/
|
|
106
107
|
removeAdditionalProperty(key: string): void;
|
|
107
108
|
/**
|
|
108
|
-
* Serializes the ruleset to a string
|
|
109
|
+
* Serializes the ruleset to a string: a JSON array of metadata rules
|
|
110
|
+
* carrying the payload in `metadata.chunk` fragments.
|
|
109
111
|
*
|
|
110
112
|
* @param pretty Whether to prettify the output.
|
|
111
113
|
*
|
|
@@ -119,8 +121,10 @@ declare class MetadataRuleset {
|
|
|
119
121
|
*
|
|
120
122
|
* @returns Deserialized ruleset.
|
|
121
123
|
*
|
|
122
|
-
* @throws {ValiError} If the metadata fails schema validation.
|
|
123
124
|
* @throws {SyntaxError} If `rawJson` is not valid JSON.
|
|
125
|
+
* @throws {InvalidMetadataChunksError} If the metadata rules are missing
|
|
126
|
+
* or malformed.
|
|
127
|
+
* @throws {ValiError} If the payload fails schema validation.
|
|
124
128
|
*/
|
|
125
129
|
static deserialize(rawJson: string): MetadataRuleset;
|
|
126
130
|
}
|