@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.
@@ -0,0 +1,112 @@
1
+ export declare const STATS_JSON = "stats.json";
2
+ export declare const FILTERS_DIR_NAME = "filters";
3
+ interface PercentEntry {
4
+ filterId: number;
5
+ }
6
+ export interface PercentJson {
7
+ config: PercentEntry[];
8
+ }
9
+ interface GroupConfig {
10
+ hits: number;
11
+ }
12
+ interface OptimizationGroup {
13
+ config: GroupConfig;
14
+ rules: Record<string, number>;
15
+ }
16
+ export interface OptimizationStats {
17
+ groups: OptimizationGroup[];
18
+ }
19
+ /**
20
+ * Thrown by `getOptimizationStatistics` when a filter's stats cannot be
21
+ * retrieved, from either a local file or the remote server.
22
+ *
23
+ * Carries `filterId` and `sourcePath` as structured fields so callers can
24
+ * build their own actionable message instead of matching on `error.message`.
25
+ */
26
+ export declare class OptimizationStatsError extends Error {
27
+ filterId: number;
28
+ sourcePath: string;
29
+ code: "OPTIMIZATION_STATS_UNAVAILABLE";
30
+ constructor(filterId: number, sourcePath: string, options?: ErrorOptions);
31
+ }
32
+ /**
33
+ * Validates that stats have non-empty groups.
34
+ *
35
+ * @param filterId - Numeric filter identifier.
36
+ * @param stats - Parsed optimization stats object.
37
+ * @throws {Error} if stats is not an object, or if stats.groups is missing or empty.
38
+ */
39
+ export declare function assertValidStats(filterId: number, stats: unknown): asserts stats is OptimizationStats;
40
+ /**
41
+ * Manages a local on-disk cache of optimization stats files.
42
+ *
43
+ * Typical usage for generating the cache:
44
+ * 1. `download(basePath, includedFilterIds, excludedFilterIds)` — save
45
+ * `stats.json` for filters listed in the remote `percent.json`.
46
+ *
47
+ * Typical usage for using the cache:
48
+ * 1. `use(basePath)` — tells `getOptimizationStatistics` to read stats
49
+ * from local files lazily during compilation instead of fetching remotely.
50
+ * 2. `reset(basePath)` — remove the cache directory and clear in-memory state.
51
+ */
52
+ export declare const localOptimizationStatistics: {
53
+ /**
54
+ * Downloads `stats.json` files for filters listed in the remote
55
+ * `percent.json` and saves them to disk.
56
+ * Existing `stats.json` files will be overwritten.
57
+ *
58
+ * `includedFilterIds` and `excludedFilterIds` cannot both be non-empty.
59
+ *
60
+ * @param basePath - Directory to save `filters/<filterId>/stats.json` into.
61
+ * @param includedFilterIds - Filter IDs to process; empty (default) processes all.
62
+ * @param excludedFilterIds - Filter IDs to exclude; empty (default) excludes none.
63
+ * @throws {Error} When both `includedFilterIds` and `excludedFilterIds` are non-empty.
64
+ */
65
+ download: (basePath: string, includedFilterIds?: number[], excludedFilterIds?: number[]) => Promise<void>;
66
+ /**
67
+ * Configures `getOptimizationStatistics` to read stats from local files under
68
+ * `basePath` instead of fetching from the remote server.
69
+ * Stats are loaded lazily on demand during compilation. `percent.json` is
70
+ * still fetched remotely to determine which filters are optimizable.
71
+ *
72
+ * @param basePath - Directory containing `filters/<filterId>/stats.json`.
73
+ */
74
+ use(basePath: string): void;
75
+ /**
76
+ * Removes the cache directory and clears in-memory state.
77
+ *
78
+ * @param basePath - Directory to remove.
79
+ */
80
+ reset(basePath: string): Promise<void>;
81
+ };
82
+ /**
83
+ * Returns the optimization stats for the given filter, or `null` when
84
+ * optimization is disabled or the filter is not listed in `percent.json`.
85
+ *
86
+ * When `localOptimizationStatistics.use(path)` has been called, stats
87
+ * are read lazily from local files. Otherwise stats are fetched from the
88
+ * remote server.
89
+ *
90
+ * @param filterId - Numeric filter identifier.
91
+ * @returns Parsed stats object, or `null` when the filter has no optimization stats.
92
+ * @throws {Error} When the stats are missing or malformed.
93
+ */
94
+ export declare const getOptimizationStatistics: (filterId: number) => Promise<OptimizationStats | null>;
95
+ /**
96
+ * Checks if rule should be skipped because optimization is enabled for this filter
97
+ * and the hit count for this rule is below the configured threshold.
98
+ *
99
+ * @param ruleText - Rule text to check.
100
+ * @param optimizationStats - Optimization config for this filter.
101
+ * @returns `true` if the rule should be skipped, `false` otherwise.
102
+ */
103
+ export declare const skipRuleWithOptimization: (ruleText: string, optimizationStats: OptimizationStats | null) => boolean;
104
+ /**
105
+ * Disables optimized filter builds
106
+ */
107
+ export declare const disableOptimization: () => void;
108
+ /**
109
+ * Enables optimized filter builds
110
+ */
111
+ export declare const enableOptimization: () => void;
112
+ export {};
@@ -0,0 +1,112 @@
1
+ export declare const STATS_JSON = "stats.json";
2
+ export declare const FILTERS_DIR_NAME = "filters";
3
+ interface PercentEntry {
4
+ filterId: number;
5
+ }
6
+ export interface PercentJson {
7
+ config: PercentEntry[];
8
+ }
9
+ interface GroupConfig {
10
+ hits: number;
11
+ }
12
+ interface OptimizationGroup {
13
+ config: GroupConfig;
14
+ rules: Record<string, number>;
15
+ }
16
+ export interface OptimizationStats {
17
+ groups: OptimizationGroup[];
18
+ }
19
+ /**
20
+ * Thrown by `getOptimizationStatistics` when a filter's stats cannot be
21
+ * retrieved, from either a local file or the remote server.
22
+ *
23
+ * Carries `filterId` and `sourcePath` as structured fields so callers can
24
+ * build their own actionable message instead of matching on `error.message`.
25
+ */
26
+ export declare class OptimizationStatsError extends Error {
27
+ filterId: number;
28
+ sourcePath: string;
29
+ code: "OPTIMIZATION_STATS_UNAVAILABLE";
30
+ constructor(filterId: number, sourcePath: string, options?: ErrorOptions);
31
+ }
32
+ /**
33
+ * Validates that stats have non-empty groups.
34
+ *
35
+ * @param filterId - Numeric filter identifier.
36
+ * @param stats - Parsed optimization stats object.
37
+ * @throws {Error} if stats is not an object, or if stats.groups is missing or empty.
38
+ */
39
+ export declare function assertValidStats(filterId: number, stats: unknown): asserts stats is OptimizationStats;
40
+ /**
41
+ * Manages a local on-disk cache of optimization stats files.
42
+ *
43
+ * Typical usage for generating the cache:
44
+ * 1. `download(basePath, includedFilterIds, excludedFilterIds)` — save
45
+ * `stats.json` for filters listed in the remote `percent.json`.
46
+ *
47
+ * Typical usage for using the cache:
48
+ * 1. `use(basePath)` — tells `getOptimizationStatistics` to read stats
49
+ * from local files lazily during compilation instead of fetching remotely.
50
+ * 2. `reset(basePath)` — remove the cache directory and clear in-memory state.
51
+ */
52
+ export declare const localOptimizationStatistics: {
53
+ /**
54
+ * Downloads `stats.json` files for filters listed in the remote
55
+ * `percent.json` and saves them to disk.
56
+ * Existing `stats.json` files will be overwritten.
57
+ *
58
+ * `includedFilterIds` and `excludedFilterIds` cannot both be non-empty.
59
+ *
60
+ * @param basePath - Directory to save `filters/<filterId>/stats.json` into.
61
+ * @param includedFilterIds - Filter IDs to process; empty (default) processes all.
62
+ * @param excludedFilterIds - Filter IDs to exclude; empty (default) excludes none.
63
+ * @throws {Error} When both `includedFilterIds` and `excludedFilterIds` are non-empty.
64
+ */
65
+ download: (basePath: string, includedFilterIds?: number[], excludedFilterIds?: number[]) => Promise<void>;
66
+ /**
67
+ * Configures `getOptimizationStatistics` to read stats from local files under
68
+ * `basePath` instead of fetching from the remote server.
69
+ * Stats are loaded lazily on demand during compilation. `percent.json` is
70
+ * still fetched remotely to determine which filters are optimizable.
71
+ *
72
+ * @param basePath - Directory containing `filters/<filterId>/stats.json`.
73
+ */
74
+ use(basePath: string): void;
75
+ /**
76
+ * Removes the cache directory and clears in-memory state.
77
+ *
78
+ * @param basePath - Directory to remove.
79
+ */
80
+ reset(basePath: string): Promise<void>;
81
+ };
82
+ /**
83
+ * Returns the optimization stats for the given filter, or `null` when
84
+ * optimization is disabled or the filter is not listed in `percent.json`.
85
+ *
86
+ * When `localOptimizationStatistics.use(path)` has been called, stats
87
+ * are read lazily from local files. Otherwise stats are fetched from the
88
+ * remote server.
89
+ *
90
+ * @param filterId - Numeric filter identifier.
91
+ * @returns Parsed stats object, or `null` when the filter has no optimization stats.
92
+ * @throws {Error} When the stats are missing or malformed.
93
+ */
94
+ export declare const getOptimizationStatistics: (filterId: number) => Promise<OptimizationStats | null>;
95
+ /**
96
+ * Checks if rule should be skipped because optimization is enabled for this filter
97
+ * and the hit count for this rule is below the configured threshold.
98
+ *
99
+ * @param ruleText - Rule text to check.
100
+ * @param optimizationStats - Optimization config for this filter.
101
+ * @returns `true` if the rule should be skipped, `false` otherwise.
102
+ */
103
+ export declare const skipRuleWithOptimization: (ruleText: string, optimizationStats: OptimizationStats | null) => boolean;
104
+ /**
105
+ * Disables optimized filter builds
106
+ */
107
+ export declare const disableOptimization: () => void;
108
+ /**
109
+ * Enables optimized filter builds
110
+ */
111
+ export declare const enableOptimization: () => void;
112
+ export {};
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Runs `fn` over `items` with at most `concurrency` calls in flight at once.
3
+ *
4
+ * @param items - Items to process.
5
+ * @param max - Max number of concurrent `fn` calls.
6
+ * @param fn - Async worker invoked for each item.
7
+ */
8
+ export declare const mapWithConcurrency: <T>(items: T[], max: number, fn: (item: T) => Promise<void>) => Promise<void>;
@@ -0,0 +1,31 @@
1
+ import { Logger } from '@adguard/logger';
2
+ /**
3
+ * Extend logger implementation
4
+ */
5
+ declare class CompilerLogger extends Logger {
6
+ #private;
7
+ /**
8
+ * Log file path, set after successful initialization.
9
+ */
10
+ logFile: string | undefined;
11
+ /** @inheritdoc */
12
+ info(message: unknown): void;
13
+ /** @inheritdoc */
14
+ error(message: unknown): void;
15
+ /** @inheritdoc */
16
+ warn(message: unknown): void;
17
+ /**
18
+ * Initializes logger
19
+ *
20
+ * @param logFilePath - log file path
21
+ *
22
+ * The log file is opened with 'w' (truncate/create). Subsequent writes are appended.
23
+ */
24
+ initialize(logFilePath: string | undefined): void;
25
+ /**
26
+ * Optional: call to close the file descriptor when done (e.g., on shutdown)
27
+ */
28
+ close(): void;
29
+ }
30
+ declare const logger: CompilerLogger;
31
+ export { logger };
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Number of times to retry downloading after the first failed attempt for `downloadFile` function.
3
+ */
4
+ export declare const RETRY_NUM = 5;
5
+ /**
6
+ * Downloads file from url with two attempts
7
+ *
8
+ * @param url
9
+ * @returns raw content of the file
10
+ */
11
+ export declare const downloadFile: (url: string) => Promise<string>;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -4,8 +4,10 @@
4
4
  "homepage": "http://adguard.com",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
7
8
  "files": [
8
- "dist"
9
+ "dist",
10
+ "types"
9
11
  ],
10
12
  "exports": {
11
13
  ".": {
@@ -45,6 +47,7 @@
45
47
  "eslint-config-airbnb-base": "15.0.0",
46
48
  "eslint-import-resolver-exports": "1.0.0-beta.5",
47
49
  "eslint-plugin-import": "2.31.0",
50
+ "eslint-plugin-jsdoc": "63.2.0",
48
51
  "husky": "8.0.2",
49
52
  "markdownlint": "0.40.0",
50
53
  "markdownlint-cli": "0.48.0",
@@ -55,7 +58,7 @@
55
58
  "typescript": "6.0.3",
56
59
  "vitest": "3.0.5"
57
60
  },
58
- "version": "3.2.11",
61
+ "version": "3.2.12",
59
62
  "scripts": {
60
63
  "prebuild": "rimraf dist",
61
64
  "build": "rollup --config rollup.config.js --silent",