@adguard/filters-compiler 3.2.11 → 3.2.12

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 CHANGED
@@ -89,13 +89,14 @@ timestamped log to `logPath`.
89
89
 
90
90
  ## API Overview
91
91
 
92
- The library exports three functions:
92
+ The library exports four functions:
93
93
 
94
- | Function | Purpose |
95
- | ------------------------- | ---------------------------------------------------- |
96
- | `compile(...)` | Compiles filter lists into platform-specific output |
97
- | `validateJSONSchema(...)` | Validates built platform output against JSON schemas |
98
- | `validateLocales(...)` | Validates locale translation files for completeness |
94
+ | Function | Purpose |
95
+ | ----------------------------- | ---------------------------------------------------- |
96
+ | `compile(...)` | Compiles filter lists into platform-specific output |
97
+ | `validateJSONSchema(...)` | Validates built platform output against JSON schemas |
98
+ | `validateLocales(...)` | Validates locale translation files for completeness |
99
+ | `localOptimizationStatistics` | Caches optimization statistics for local compilation |
99
100
 
100
101
  ### `compile(...)`
101
102
 
@@ -104,9 +105,9 @@ function compile(
104
105
  path: string,
105
106
  logPath: string | undefined,
106
107
  reportFile: string | undefined,
107
- platformsPath: string,
108
- whitelist: number[],
109
- blacklist: number[],
108
+ platformsPath: string | null,
109
+ includedFilterIds?: number[] | null,
110
+ excludedFilterIds?: number[] | null,
110
111
  customPlatformsConfig?: CustomPlatformsConfig,
111
112
  ): Promise<void>;
112
113
  ```
@@ -114,9 +115,9 @@ function compile(
114
115
  Compiles the filter lists in `path` for all configured platforms and
115
116
  writes the results to `platformsPath`.
116
117
 
117
- - `whitelist` — compile only the filter IDs listed here (empty array
118
+ - `includedFilterIds` — compile only the filter IDs listed here (empty array
118
119
  compiles all filters).
119
- - `blacklist` — exclude the filter IDs listed here.
120
+ - `excludedFilterIds` — exclude the filter IDs listed here.
120
121
  - `customPlatformsConfig` — overrides or extends the built-in platform
121
122
  definitions (see [Custom platforms](#custom-platforms)).
122
123
 
@@ -162,6 +163,21 @@ found, `data` and `log` contain the per-locale details and `ok` is
162
163
  `false` only if at least one warning is critical. Throws when the
163
164
  locales directory is missing or empty.
164
165
 
166
+ ### `localOptimizationStatistics`
167
+
168
+ A namespace for caching optimization statistics locally so that
169
+ `compile()` reads each filter's `stats.json` from disk instead of
170
+ fetching it from the remote server. `percent.json` (which determines
171
+ which filters are optimizable) is always fetched remotely, even when
172
+ using the local cache.
173
+
174
+ It exposes `download`, `use`, and `reset` methods and throws
175
+ `OptimizationStatsError` (an `Error` subclass carrying `filterId` and
176
+ `sourcePath` fields) when stats for a filter cannot be retrieved.
177
+
178
+ See [Local Optimization Statistics](#local-optimization-statistics)
179
+ for a complete workflow.
180
+
165
181
  ## Usage Examples
166
182
 
167
183
  ### Compiling filter lists
@@ -176,8 +192,8 @@ await compile(
176
192
  './log.txt', // log file (omit to disable logging)
177
193
  './report.txt', // compilation report
178
194
  './platforms', // platform output directory
179
- [], // whitelist (empty = compile all)
180
- [], // blacklist (empty = exclude none)
195
+ [], // includedFilterIds (empty = compile all)
196
+ [], // excludedFilterIds (empty = exclude none)
181
197
  );
182
198
  ```
183
199
 
@@ -386,7 +402,7 @@ const customPlatformsConfig = {
386
402
 
387
403
  `replacements[].from` is treated as a regular-expression pattern (it is
388
404
  passed to `new RegExp(from, 'g')`), not as literal text. Escape regex
389
- metacharacters in `from` when a literal match is intended.
405
+ meta characters in `from` when a literal match is intended.
390
406
 
391
407
  ### Logging
392
408
 
@@ -430,6 +446,53 @@ Each writes its output to a subdirectory under `platformsPath`.
430
446
  | `EXTENSION_ANDROID_CONTENT_BLOCKER` | `extension/android-content-blocker` | AdGuard content blocker (Android) |
431
447
  | `EXTENSION_UBLOCK` | `extension/ublock` | uBlock Origin-compatible output |
432
448
 
449
+ ## Local Optimization Statistics
450
+
451
+ `localOptimizationStatistics` provides a workflow for caching optimization statistics
452
+ locally so that `compile()` reads each filter's `stats.json` from disk instead of fetching it
453
+ from the remote server. `percent.json` (which determines which filters are optimizable) is
454
+ always fetched remotely, even when using the local cache.
455
+
456
+ ### Directory layout
457
+
458
+ ```text
459
+ <basePath>/
460
+ filters/
461
+ <filterId>/
462
+ stats.json # per-filter hit counts (STATS_JSON)
463
+ ```
464
+
465
+ ### Workflow
466
+
467
+ 1. Call `download(basePath, includedFilterIds, excludedFilterIds)` to save
468
+ `stats.json` for filters listed in the remote `percent.json`.
469
+ `includedFilterIds` limits processing to those IDs (default `[]` = all).
470
+ `excludedFilterIds` skips those IDs (default `[]` = none). The two cannot use both.
471
+ 2. Call `use(basePath)` before `compile()` to tell
472
+ `getOptimizationStatistics` to read stats from local files under
473
+ `basePath` instead of fetching from the remote server.
474
+ 3. Call `reset(basePath)` when done to remove the cache directory and clear
475
+ in-memory state.
476
+
477
+ If stats for a filter can't be retrieved (missing local file, or a failed remote
478
+ fetch), `getOptimizationStatistics` throws `OptimizationStatsError` — an `Error`
479
+ subclass carrying `filterId` and `sourcePath` fields, so callers can build their
480
+ own actionable message instead of matching on `error.message`.
481
+
482
+ ```js
483
+ import { localOptimizationStatistics } from '@adguard/filters-compiler';
484
+
485
+ // 1. Download stats for all optimizable filters to ./stats-cache
486
+ await localOptimizationStatistics.download('./stats-cache');
487
+
488
+ // 2. Tell compile() to read stats from the local cache
489
+ localOptimizationStatistics.use('./stats-cache');
490
+ await compile('./filters', undefined, undefined, './platforms', [], []);
491
+
492
+ // 3. Clean up when done
493
+ await localOptimizationStatistics.reset('./stats-cache');
494
+ ```
495
+
433
496
  ## Documentation
434
497
 
435
498
  - [Development](DEVELOPMENT.md) — setup, build, and contributing workflow