@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.
Files changed (114) hide show
  1. package/README.md +476 -0
  2. package/dist/cli.cjs +58912 -0
  3. package/dist/constants.js +23 -0
  4. package/dist/declarative-rule/declarative-rule.js +15 -0
  5. package/dist/declarative-rule/header-info.js +12 -0
  6. package/dist/declarative-rule/modify-header-info.js +31 -0
  7. package/dist/declarative-rule/query-key-value.js +12 -0
  8. package/dist/declarative-rule/query-transform.js +12 -0
  9. package/dist/declarative-rule/redirect.js +14 -0
  10. package/dist/declarative-rule/rule-action.js +44 -0
  11. package/dist/declarative-rule/rule-condition.js +77 -0
  12. package/dist/declarative-rule/url-transform.js +30 -0
  13. package/dist/errors/conversion-errors/empty-domains-error.js +24 -0
  14. package/dist/errors/conversion-errors/empty-resources-error.js +24 -0
  15. package/dist/errors/conversion-errors/index.js +17 -0
  16. package/dist/errors/conversion-errors/invalid-declarative-rule-error.js +22 -0
  17. package/dist/errors/conversion-errors/unsupported-modifier-error.js +20 -0
  18. package/dist/errors/conversion-errors/unsupported-regexp-error.js +27 -0
  19. package/dist/errors/converter-options-errors/empty-or-negative-number-of-rules-error.js +18 -0
  20. package/dist/errors/converter-options-errors/index.js +15 -0
  21. package/dist/errors/converter-options-errors/negative-number-of-rules-error.js +18 -0
  22. package/dist/errors/converter-options-errors/resources-path-error.js +18 -0
  23. package/dist/errors/limitation-errors/index.js +17 -0
  24. package/dist/errors/limitation-errors/max-scanned-rules-error.js +20 -0
  25. package/dist/errors/limitation-errors/too-many-error.js +28 -0
  26. package/dist/errors/limitation-errors/too-many-regexp-rules-error.js +25 -0
  27. package/dist/errors/limitation-errors/too-many-rules-error.js +25 -0
  28. package/dist/errors/limitation-errors/too-many-unsafe-rules-error.js +25 -0
  29. package/dist/errors/unavailable-sources-errors/unavailable-filter-source-error.js +21 -0
  30. package/dist/errors/unavailable-sources-errors/unavailable-ruleset-source-error.js +21 -0
  31. package/dist/filter/filter.js +92 -0
  32. package/dist/filter-converter/filter-converter.js +385 -0
  33. package/dist/grouped-rules-converters/url-transform-converter.js +340 -0
  34. package/dist/index.js +30 -0
  35. package/dist/package.json.js +3 -0
  36. package/dist/re2-regexp/re2-validator.js +37 -0
  37. package/dist/re2-regexp/regex-validator-extension.js +30 -0
  38. package/dist/re2.wasm +0 -0
  39. package/dist/rule/option-names.js +65 -0
  40. package/dist/rule/rule-badfilter.js +81 -0
  41. package/dist/rule/rule-priority.js +88 -0
  42. package/dist/rule/rule-validator.js +455 -0
  43. package/dist/rule/rule.js +853 -0
  44. package/dist/rule/string-utils.js +49 -0
  45. package/dist/rule-converters/csp-converter.js +105 -0
  46. package/dist/rule-converters/regular-rule-converter.js +789 -0
  47. package/dist/rule-converters/remove-header-converter.js +97 -0
  48. package/dist/rule-converters/rules-converter.js +365 -0
  49. package/dist/rule-converters/rules-grouper.js +75 -0
  50. package/dist/rules-scanner/rules-scanner.js +136 -0
  51. package/dist/ruleset/metadata-rule.js +35 -0
  52. package/dist/ruleset/metadata-ruleset.js +162 -0
  53. package/dist/ruleset/rules-hash-map.js +71 -0
  54. package/dist/ruleset/ruleset-with-source-map.js +406 -0
  55. package/dist/ruleset/ruleset.js +54 -0
  56. package/dist/ruleset/source-map.js +283 -0
  57. package/dist/types/cli/convert-filters.d.ts +32 -0
  58. package/dist/types/cli/index.d.ts +7 -0
  59. package/dist/types/cli/utils.d.ts +19 -0
  60. package/dist/types/src/declarative-rule/declarative-rule.d.ts +32 -0
  61. package/dist/types/src/declarative-rule/header-info.d.ts +31 -0
  62. package/dist/types/src/declarative-rule/modify-header-info.d.ts +46 -0
  63. package/dist/types/src/declarative-rule/query-key-value.d.ts +24 -0
  64. package/dist/types/src/declarative-rule/query-transform.d.ts +21 -0
  65. package/dist/types/src/declarative-rule/redirect.d.ts +32 -0
  66. package/dist/types/src/declarative-rule/rule-action.d.ts +68 -0
  67. package/dist/types/src/declarative-rule/rule-condition.d.ts +262 -0
  68. package/dist/types/src/declarative-rule/url-transform.d.ts +69 -0
  69. package/dist/types/src/errors/conversion-errors/empty-domains-error.d.ts +21 -0
  70. package/dist/types/src/errors/conversion-errors/empty-resources-error.d.ts +21 -0
  71. package/dist/types/src/errors/conversion-errors/index.d.ts +27 -0
  72. package/dist/types/src/errors/conversion-errors/invalid-declarative-rule-error.d.ts +30 -0
  73. package/dist/types/src/errors/conversion-errors/unsupported-modifier-error.d.ts +20 -0
  74. package/dist/types/src/errors/conversion-errors/unsupported-regexp-error.d.ts +23 -0
  75. package/dist/types/src/errors/converter-options-errors/empty-or-negative-number-of-rules-error.d.ts +13 -0
  76. package/dist/types/src/errors/converter-options-errors/index.d.ts +23 -0
  77. package/dist/types/src/errors/converter-options-errors/negative-number-of-rules-error.d.ts +13 -0
  78. package/dist/types/src/errors/converter-options-errors/resources-path-error.d.ts +13 -0
  79. package/dist/types/src/errors/limitation-errors/index.d.ts +27 -0
  80. package/dist/types/src/errors/limitation-errors/max-scanned-rules-error.d.ts +18 -0
  81. package/dist/types/src/errors/limitation-errors/too-many-error.d.ts +33 -0
  82. package/dist/types/src/errors/limitation-errors/too-many-regexp-rules-error.d.ts +21 -0
  83. package/dist/types/src/errors/limitation-errors/too-many-rules-error.d.ts +21 -0
  84. package/dist/types/src/errors/limitation-errors/too-many-unsafe-rules-error.d.ts +21 -0
  85. package/dist/types/src/errors/unavailable-sources-errors/unavailable-filter-source-error.d.ts +19 -0
  86. package/dist/types/src/errors/unavailable-sources-errors/unavailable-ruleset-source-error.d.ts +19 -0
  87. package/dist/types/src/filter/filter.d.ts +74 -0
  88. package/dist/types/src/filter/types.d.ts +45 -0
  89. package/dist/types/src/filter-converter/conversion-result.d.ts +35 -0
  90. package/dist/types/src/filter-converter/filter-converter-options.d.ts +81 -0
  91. package/dist/types/src/filter-converter/filter-converter.d.ts +197 -0
  92. package/dist/types/src/index.d.ts +37 -0
  93. package/dist/types/src/rule/rule-types.d.ts +33 -0
  94. package/dist/types/src/rule/rule-validator.d.ts +185 -0
  95. package/dist/types/src/rule/rule.d.ts +269 -0
  96. package/dist/types/src/ruleset/metadata-ruleset.d.ts +128 -0
  97. package/dist/types/src/ruleset/rules-hash-map.d.ts +70 -0
  98. package/dist/types/src/ruleset/ruleset-with-source-map.d.ts +318 -0
  99. package/dist/types/src/ruleset/ruleset.d.ts +86 -0
  100. package/dist/types/src/ruleset/source-map.d.ts +45 -0
  101. package/dist/types/src/ruleset/types.d.ts +68 -0
  102. package/dist/types/src/utils/is-safe-rule.d.ts +20 -0
  103. package/dist/types/src/utils/ruleset-utils.d.ts +45 -0
  104. package/dist/types/src/version.d.ts +6 -0
  105. package/dist/url-transform-converter.js +338 -0
  106. package/dist/utils/error.js +22 -0
  107. package/dist/utils/is-safe-rule.js +25 -0
  108. package/dist/utils/lazy-loader.js +116 -0
  109. package/dist/utils/lazy.js +50 -0
  110. package/dist/utils/ruleset-utils.js +76 -0
  111. package/dist/utils/string.js +180 -0
  112. package/dist/utils/valibot.js +74 -0
  113. package/dist/version.js +7 -0
  114. package/package.json +122 -0
@@ -0,0 +1,180 @@
1
+ import punycode from 'punycode/punycode.js';
2
+ import { SPACE_CHARACTER, MASK_REGEX_RULE } from '../constants.js';
3
+ import { getErrorMessage } from './error.js';
4
+
5
+ /**
6
+ * `djb2` hash algorithm.
7
+ *
8
+ * NOTE: This version uses some bit operands to exclude overflow MAX_SAFE_INTEGER (and moreover, exclude overflow 2^32).
9
+ *
10
+ * @see {@link https://gist.github.com/eplawless/52813b1d8ad9af510d85?permalink_comment_id=3367765#gistcomment-3367765}
11
+ *
12
+ * @param str String to get hash.
13
+ *
14
+ * @returns Hash.
15
+ */ function fastHash(str) {
16
+ if (str.length === 0) {
17
+ return 0;
18
+ }
19
+ // A magic constant that gives good distribution.
20
+ let hash = 5381;
21
+ for(let i = 0; i < str.length; i += 1){
22
+ hash = hash * 33 ^ str.charCodeAt(i);
23
+ }
24
+ return hash >>> 0;
25
+ }
26
+ /**
27
+ * `djb2` hash algorithm with sign bit masked off to fit 31 bits.
28
+ *
29
+ * @param str String to get hash.
30
+ *
31
+ * @returns Hash from 0 to 2^31-1.
32
+ */ function fastHash31(str) {
33
+ // Mask off sign bit to keep value in range [0, ..., 2^31‑1].
34
+ return fastHash(str) & 0x7fffffff;
35
+ }
36
+ /**
37
+ * Checks if the string contains only ASCII characters.
38
+ *
39
+ * @param str String to test.
40
+ *
41
+ * @returns True if string contains only ASCII characters.
42
+ */ function isASCII(str) {
43
+ // eslint-disable-next-line no-control-regex
44
+ return /^[\x00-\x7F]+$/.test(str);
45
+ }
46
+ /**
47
+ * Converts to ASCII characters only if `str` contains non-ASCII characters.
48
+ *
49
+ * @param str String to convert.
50
+ *
51
+ * @returns A transformed string containing only ASCII characters or
52
+ * the original string.
53
+ *
54
+ * @throws Error if conversion into ASCII fails.
55
+ */ function prepareASCII(str) {
56
+ let res = str;
57
+ try {
58
+ if (!isASCII(res)) {
59
+ // for cyrillic domains we need to convert them by isASCII()
60
+ res = punycode.toASCII(res);
61
+ }
62
+ // after toASCII() some characters can be still non-ASCII
63
+ // e.g. `abc“@` with non-ASCII `“`
64
+ if (!isASCII(res)) {
65
+ res = punycode.encode(res);
66
+ }
67
+ } catch (e) {
68
+ throw new Error(`Error converting to ASCII: "${str}" due to ${getErrorMessage(e)}`);
69
+ }
70
+ return res;
71
+ }
72
+ /**
73
+ * Converts a list of strings into strings containing only ASCII characters.
74
+ *
75
+ * @param strings List of strings.
76
+ *
77
+ * @returns List of string containing only ASCII characters.
78
+ */ function toASCII(strings) {
79
+ return strings.map(prepareASCII);
80
+ }
81
+ /**
82
+ * Removes slashes from the beginning and end of the string.
83
+ *
84
+ * @param str String to remove slashes.
85
+ *
86
+ * @returns String without slashes.
87
+ */ function removeSlashes(str) {
88
+ if (str.startsWith(MASK_REGEX_RULE) && str.endsWith(MASK_REGEX_RULE)) {
89
+ return str.substring(1, str.length - 1);
90
+ }
91
+ return str;
92
+ }
93
+ /**
94
+ * Check if pattern is Regex.
95
+ *
96
+ * @param str The string to check.
97
+ *
98
+ * @returns True if the string is a regex pattern, false otherwise.
99
+ */ function isRegexPattern(str) {
100
+ return str.startsWith(MASK_REGEX_RULE) && str.endsWith(MASK_REGEX_RULE);
101
+ }
102
+ /**
103
+ * Checks if string contains spaces.
104
+ *
105
+ * @param str String to check.
106
+ *
107
+ * @returns `true` if string contains spaces, `false` otherwise.
108
+ */ function hasSpaces(str) {
109
+ return str.includes(SPACE_CHARACTER);
110
+ }
111
+ /**
112
+ * Tab character used for pretty printing.
113
+ */ const TAB = '\t';
114
+ /**
115
+ * Serializes data to a JSON string.
116
+ *
117
+ * @param data Data to serialize.
118
+ * @param pretty Whether to pretty print the output.
119
+ *
120
+ * @returns JSON string.
121
+ */ function serializeJson(data, pretty) {
122
+ return JSON.stringify(data, null, pretty ? TAB : undefined);
123
+ }
124
+ /**
125
+ * Splits the string by the delimiter, ignoring escaped delimiters
126
+ * and without tokenizing.
127
+ *
128
+ * @param string String to split.
129
+ * @param delimiter Delimiter.
130
+ * @param escapeCharacter Escape character.
131
+ * @param preserveEmptyTokens If true, preserve empty parts.
132
+ * @param shouldUnescape If true, unescape characters.
133
+ *
134
+ * @returns Array of string parts.
135
+ */ function splitByDelimiterWithEscapeCharacter(string, delimiter, escapeCharacter, preserveEmptyTokens, shouldUnescape = true) {
136
+ if (!string) {
137
+ return [];
138
+ }
139
+ if (string.startsWith(delimiter)) {
140
+ // eslint-disable-next-line no-param-reassign
141
+ string = string.substring(1);
142
+ }
143
+ let words = [];
144
+ if (!string.includes(escapeCharacter)) {
145
+ words = string.split(delimiter);
146
+ return words;
147
+ }
148
+ let chars = [];
149
+ const makeWord = ()=>{
150
+ const word = chars.join('');
151
+ words.push(word);
152
+ chars = [];
153
+ };
154
+ for(let i = 0; i < string.length; i += 1){
155
+ const char = string.charAt(i);
156
+ const isLastChar = i === string.length - 1;
157
+ if (char === delimiter) {
158
+ const isEscapedChar = i > 0 && string[i - 1] === escapeCharacter;
159
+ if (isEscapedChar) {
160
+ if (shouldUnescape) {
161
+ chars.splice(chars.length - 1, 1);
162
+ }
163
+ chars.push(char);
164
+ } else {
165
+ makeWord();
166
+ }
167
+ if (isLastChar) {
168
+ makeWord();
169
+ }
170
+ } else if (isLastChar) {
171
+ chars.push(char);
172
+ makeWord();
173
+ } else {
174
+ chars.push(char);
175
+ }
176
+ }
177
+ return words;
178
+ }
179
+
180
+ export { fastHash, fastHash31, hasSpaces, isASCII, isRegexPattern, prepareASCII, removeSlashes, serializeJson, splitByDelimiterWithEscapeCharacter, toASCII };
@@ -0,0 +1,74 @@
1
+ import { strictObject, getDotPath } from 'valibot';
2
+
3
+ /**
4
+ * Creates a strict object schema by validating the entries against the expected type.
5
+ *
6
+ * @template TExpected The expected type for the object schema.
7
+ * @template TEntries The entries of the object schema.
8
+ *
9
+ * @see {@link strictObject}
10
+ *
11
+ * @param entries The entries schema.
12
+ *
13
+ * @returns A strict object schema.
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * // Valid case
18
+ * interface ValidExample {
19
+ * foo: string;
20
+ * }
21
+ *
22
+ * const ValidExampleValidator = strictObjectByType<ValidExample>({
23
+ * foo: v.string(),
24
+ * });
25
+ *
26
+ * // Invalid case
27
+ * interface InvalidExample {
28
+ * foo: string;
29
+ * bar: number;
30
+ * }
31
+ *
32
+ * const InvalidExampleValidator = strictObjectByType<InvalidExample>({
33
+ * foo: v.string(),
34
+ * baz: v.boolean(), // Error: Does not match the expected type
35
+ * // Error: Property 'bar' is missing
36
+ * });
37
+ * ```
38
+ */ function strictObjectByType(entries) {
39
+ return strictObject(entries);
40
+ }
41
+ /**
42
+ * Recursively extracts message from Valibot issue.
43
+ *
44
+ * @param issue Valibot's {@link BaseIssue}.
45
+ * @param nesting Nesting level prefix (e.g. `'1'`, `'1.1'`).
46
+ *
47
+ * @returns Message extracted from the issue and its sub-issues.
48
+ */ function extractMessageFromValiIssue(issue, nesting) {
49
+ const type = `Type: "${issue.type}"`;
50
+ const message = `Message: "${issue.message}"`;
51
+ const path = `Path: "${getDotPath(issue)}"`;
52
+ const messages = [
53
+ `${nesting}. ${type} | ${message} | ${path}`
54
+ ];
55
+ if (issue.issues && issue.issues.length > 0) {
56
+ const nestedMessages = issue.issues.map((subIssue, i)=>extractMessageFromValiIssue(subIssue, `${nesting}.${i + 1}`));
57
+ messages.push(...nestedMessages);
58
+ }
59
+ return messages.join('\n');
60
+ }
61
+ /**
62
+ * Extracts message from Valibot error.
63
+ *
64
+ * @param error Valibot's {@link ValiError}.
65
+ *
66
+ * @returns Message extracted from the error issues and its sub-issues.
67
+ */ function extractMessageFromValiError(error) {
68
+ if (error.issues.length === 0) {
69
+ return error.message;
70
+ }
71
+ return error.issues.map((issue, i)=>extractMessageFromValiIssue(issue, `${i + 1}`)).join('\n');
72
+ }
73
+
74
+ export { extractMessageFromValiError, strictObjectByType };
@@ -0,0 +1,7 @@
1
+ import { version } from './package.json.js';
2
+
3
+ /**
4
+ * Version of the `@adguard/dnr-converter` package.
5
+ */ const DNR_CONVERTER_VERSION = version;
6
+
7
+ export { DNR_CONVERTER_VERSION };
package/package.json ADDED
@@ -0,0 +1,122 @@
1
+ {
2
+ "name": "@adguard/dnr-converter",
3
+ "version": "1.0.0-alpha.2",
4
+ "description": "A converter that transforms adblock-style filtering rules into rules compatible with the Declarative Net Request (DNR) API.",
5
+ "main": "dist/index.js",
6
+ "module": "dist/index.js",
7
+ "types": "dist/types/src/index.d.ts",
8
+ "type": "module",
9
+ "sideEffects": false,
10
+ "files": [
11
+ "dist",
12
+ "!dist/build.txt"
13
+ ],
14
+ "exports": {
15
+ ".": {
16
+ "import": "./dist/index.js",
17
+ "types": "./dist/types/src/index.d.ts"
18
+ },
19
+ "./cli": {
20
+ "types": "./dist/types/cli/index.d.ts",
21
+ "default": "./dist/cli.cjs"
22
+ }
23
+ },
24
+ "bin": {
25
+ "dnr-converter": "dist/cli.cjs"
26
+ },
27
+ "typesVersions": {
28
+ "*": {
29
+ "*": [
30
+ "dist/types/src/index.d.ts"
31
+ ],
32
+ "cli": [
33
+ "dist/types/cli/index.d.ts"
34
+ ]
35
+ }
36
+ },
37
+ "author": "Adguard Software Ltd.",
38
+ "repository": {
39
+ "type": "git",
40
+ "url": "git+https://github.com/AdguardTeam/tsurlfilter.git",
41
+ "directory": "packages/dnr-converter"
42
+ },
43
+ "license": "GPL-3.0-only",
44
+ "engines": {
45
+ "node": ">=22"
46
+ },
47
+ "bugs": {
48
+ "url": "https://github.com/AdguardTeam/tsurlfilter/issues"
49
+ },
50
+ "homepage": "https://github.com/AdguardTeam/tsurlfilter/tree/master/packages/dnr-converter#readme",
51
+ "lint-staged": {
52
+ "{src,cli,test,tasks}/**/*.ts": [
53
+ "eslint"
54
+ ]
55
+ },
56
+ "peerDependencies": {
57
+ "@adguard/re2-wasm": "1.2.0"
58
+ },
59
+ "dependencies": {
60
+ "@adguard/scriptlets": "2.4.2",
61
+ "commander": "^12.1.0",
62
+ "punycode": "2.3.1",
63
+ "valibot": "^1.1.0",
64
+ "@adguard/agtree": "^4.1.1",
65
+ "@adguard/logger": "^2.0.0"
66
+ },
67
+ "devDependencies": {
68
+ "@rollup/plugin-commonjs": "^28.0.1",
69
+ "@rollup/plugin-json": "^6.1.0",
70
+ "@rollup/plugin-node-resolve": "^15.3.0",
71
+ "@rollup/plugin-swc": "^0.4.0",
72
+ "@rollup/plugin-typescript": "^12.1.1",
73
+ "@types/chrome": "^0.0.305",
74
+ "@types/node": "^22.14.0",
75
+ "@types/punycode": "2.1.4",
76
+ "@typescript-eslint/eslint-plugin": "^7.5.0",
77
+ "@typescript-eslint/parser": "^7.5.0",
78
+ "@vitest/coverage-v8": "^4.1.5",
79
+ "eslint": "8.57.1",
80
+ "eslint-config-airbnb-base": "15.0.0",
81
+ "eslint-config-airbnb-typescript": "^18.0.0",
82
+ "eslint-plugin-boundaries": "^5.0.1",
83
+ "eslint-plugin-import": "^2.29.0",
84
+ "eslint-plugin-import-newlines": "1.4.0",
85
+ "eslint-plugin-jsdoc": "^48.11.0",
86
+ "eslint-plugin-n": "^17.21.3",
87
+ "lint-staged": "^13.2.2",
88
+ "markdownlint": "^0.37.4",
89
+ "markdownlint-cli": "^0.44.0",
90
+ "rimraf": "^6.0.1",
91
+ "rollup": "^4.28.0",
92
+ "rollup-plugin-copy": "^3.5.0",
93
+ "rollup-plugin-dts": "^6.0.2",
94
+ "rollup-plugin-node-externals": "^8.0.0",
95
+ "tsd": "^0.32.0",
96
+ "tsx": "^4.19.3",
97
+ "typescript": "^5.7.3",
98
+ "vitest": "^4.1.5",
99
+ "@adguard/eslint-plugin-logger-context": "^1.0.1",
100
+ "@adguard/tsurlfilter": "^6.0.0"
101
+ },
102
+ "scripts": {
103
+ "lint": "pnpm lint:code && pnpm lint:types && pnpm lint:md",
104
+ "lint:code": "eslint --cache --ignore-path .gitignore .",
105
+ "lint:types": "tsc",
106
+ "lint:md": "markdownlint .",
107
+ "start": "rollup -c rollup.config.ts --configPlugin typescript -w",
108
+ "build": "rimraf dist && rollup -c rollup.config.ts --configPlugin typescript",
109
+ "postbuild": "tsx tasks/build-txt.ts",
110
+ "docs:examples": "tsx tasks/generate-examples.ts",
111
+ "test": "vitest run",
112
+ "test:light": "vitest run --exclude \"**/*benchmark*.test.ts\"",
113
+ "test:watch": "vitest run --watch",
114
+ "test:ci": "vitest run --no-cache --reporter default --reporter junit --outputFile=./tests-reports/dnr-converter.xml",
115
+ "test:coverage": "vitest run --coverage",
116
+ "test:smoke": "(cd test/smoke/esm && pnpm test) && (cd test/smoke/exports && pnpm test)",
117
+ "test:prod": "pnpm lint && pnpm test:smoke && pnpm test -- --no-cache",
118
+ "test:debug": "vitest run --inspect --no-file-parallelism --watch",
119
+ "precommit": "lint-staged",
120
+ "tgz": "pnpm pack --out dnr-converter.tgz"
121
+ }
122
+ }