@adguard/hostlist-compiler 1.0.34 → 1.0.35
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/CHANGELOG.md +9 -0
- package/README.md +23 -0
- package/package.json +1 -1
- package/src/rule.js +36 -0
- package/src/schemas/configuration.schema.json +4 -2
- package/src/transformations/covert-to-ascii.js +30 -0
- package/src/transformations/transform.js +5 -0
- package/src/transformations/validate.js +33 -26
- package/test/compile-source.test.js +39 -3
- package/test/configuration.test.js +2 -0
- package/test/transformations/covert-to-ascii.test.js +30 -0
- package/test/transformations/transform.test.js +31 -0
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,15 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [1.0.35] - 2025-02-07
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
- `ConvertToAscii` transformation option [#62]
|
|
13
|
+
|
|
14
|
+
[1.0.35]: https://github.com/AdguardTeam/HostlistCompiler/compare/v1.0.34...v1.0.35
|
|
15
|
+
[#62]: https://github.com/AdguardTeam/HostlistCompiler/issues/62
|
|
16
|
+
|
|
8
17
|
## [1.0.34] - 2025-01-10
|
|
9
18
|
|
|
10
19
|
### Removed
|
package/README.md
CHANGED
|
@@ -19,6 +19,7 @@ This is a simple tool that makes it easier to compile a [hosts blocklist](https:
|
|
|
19
19
|
- [RemoveEmptyLines](#removeemptylines)
|
|
20
20
|
- [TrimLines](#trimlines)
|
|
21
21
|
- [InsertFinalNewLine](#insertfinalnewline)
|
|
22
|
+
- [ConvertToAscii](#convert-to-ascii)
|
|
22
23
|
- [How to build](#how-to-build)
|
|
23
24
|
|
|
24
25
|
## <a name="usage"></a> Usage
|
|
@@ -453,6 +454,28 @@ rule3
|
|
|
453
454
|
|
|
454
455
|
`RemoveEmptyLines` doesn't delete this empty row due to the execution order.
|
|
455
456
|
|
|
457
|
+
### <a name="convert-to-ascii"></a> ConvertToAscii
|
|
458
|
+
|
|
459
|
+
This transformation converts all non-ASCII characters to their ASCII equivalents. It is always performed first.
|
|
460
|
+
|
|
461
|
+
**Example:**
|
|
462
|
+
|
|
463
|
+
Original list:
|
|
464
|
+
|
|
465
|
+
```
|
|
466
|
+
||*.рус^
|
|
467
|
+
||*.कॉम^
|
|
468
|
+
||*.セール^
|
|
469
|
+
```
|
|
470
|
+
|
|
471
|
+
Here's what we will have after applying this transformation:
|
|
472
|
+
|
|
473
|
+
```
|
|
474
|
+
||*.xn--p1acf^
|
|
475
|
+
||*.xn--11b4c3d^
|
|
476
|
+
||*.xn--1qqw23a^
|
|
477
|
+
```
|
|
478
|
+
|
|
456
479
|
## <a name="how-to-build"></a> How to build
|
|
457
480
|
|
|
458
481
|
- `yarn install` - installs dependencies
|
package/package.json
CHANGED
package/src/rule.js
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
const _ = require('lodash');
|
|
2
|
+
const { domainToASCII } = require('url');
|
|
2
3
|
const utils = require('./utils');
|
|
3
4
|
|
|
4
5
|
// eslint-disable-next-line max-len
|
|
5
6
|
const domainRegex = /^(?=.{1,255}$)[0-9A-Za-z](?:(?:[0-9A-Za-z]|-){0,61}[0-9A-Za-z])?(?:\.[0-9A-Za-z](?:(?:[0-9A-Za-z]|-){0,61}[0-9A-Za-z])?)*\.?$/;
|
|
6
7
|
const etcHostsRegex = /^([a-f0-9.:\][]+)(%[a-z0-9]+)?\s+([^#]+)(#.*)?$/;
|
|
8
|
+
const domainPatternRegex = /(\*\.|)([^\s^$|=]+(?:\.[^\s^$|=]+)+)/g;
|
|
9
|
+
// eslint-disable-next-line no-control-regex
|
|
10
|
+
const nonAsciiRegexp = /[^\x00-\x7F]/;
|
|
7
11
|
|
|
8
12
|
/**
|
|
9
13
|
* Helper utils for working with filtering rules
|
|
@@ -45,6 +49,36 @@ function isEtcHostsRule(ruleText) {
|
|
|
45
49
|
return etcHostsRegex.test(ruleText);
|
|
46
50
|
}
|
|
47
51
|
|
|
52
|
+
/**
|
|
53
|
+
* Check if the rule contains non-ASCII characters
|
|
54
|
+
* @param {string} ruleText - rule to check
|
|
55
|
+
* @returns {boolean} true if the rule contains non-latin characters
|
|
56
|
+
*/
|
|
57
|
+
function containsNonAsciiCharacters(ruleText) {
|
|
58
|
+
return nonAsciiRegexp.test(ruleText);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Function to convert all parts of the string that look like domain names with a possible wildcard (*.)
|
|
63
|
+
* and contain non-Latin characters to Punycode.
|
|
64
|
+
*
|
|
65
|
+
* @param {string} line The original string (adblock rule, regular domain, etc.)
|
|
66
|
+
* @returns {string} String with non-Latin domains converted to Punycode
|
|
67
|
+
*/
|
|
68
|
+
function convertNonAsciiToPunycode(line) {
|
|
69
|
+
return line.replace(domainPatternRegex, (match, wildcard, domain) => {
|
|
70
|
+
// Check if the domain contains non-ASCII characters
|
|
71
|
+
if (containsNonAsciiCharacters(domain)) {
|
|
72
|
+
// Convert only the domain (without '*.')
|
|
73
|
+
const punycodeDomain = domainToASCII(domain);
|
|
74
|
+
// Return with possible wildcard prefix '*.'
|
|
75
|
+
return wildcard + punycodeDomain;
|
|
76
|
+
}
|
|
77
|
+
// If there are no non-Latin characters, leave the original part
|
|
78
|
+
return match;
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
48
82
|
/**
|
|
49
83
|
* Represents a /etc/hosts rule
|
|
50
84
|
* @typedef {Object} AdblockRuleTokens
|
|
@@ -276,6 +310,8 @@ module.exports = {
|
|
|
276
310
|
isEtcHostsRule,
|
|
277
311
|
loadEtcHostsRuleProperties,
|
|
278
312
|
loadAdblockRuleProperties,
|
|
313
|
+
containsNonAsciiCharacters,
|
|
314
|
+
convertNonAsciiToPunycode,
|
|
279
315
|
findModifier,
|
|
280
316
|
removeModifier,
|
|
281
317
|
adblockRuleToString,
|
|
@@ -49,7 +49,8 @@
|
|
|
49
49
|
"InvertAllow",
|
|
50
50
|
"RemoveEmptyLines",
|
|
51
51
|
"TrimLines",
|
|
52
|
-
"InsertFinalNewLine"
|
|
52
|
+
"InsertFinalNewLine",
|
|
53
|
+
"ConvertToAscii"
|
|
53
54
|
]
|
|
54
55
|
}
|
|
55
56
|
},
|
|
@@ -125,7 +126,8 @@
|
|
|
125
126
|
"InvertAllow",
|
|
126
127
|
"RemoveEmptyLines",
|
|
127
128
|
"TrimLines",
|
|
128
|
-
"InsertFinalNewLine"
|
|
129
|
+
"InsertFinalNewLine",
|
|
130
|
+
"ConvertToAscii"
|
|
129
131
|
]
|
|
130
132
|
}
|
|
131
133
|
},
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
const consola = require('consola');
|
|
2
|
+
const ruleUtils = require('../rule');
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Converts non-ASCII characters in the given rules to their ASCII punycode equivalents
|
|
6
|
+
*
|
|
7
|
+
* @param {Array<string>} rules - An array of rules to be converted.
|
|
8
|
+
* @returns {Array<string>} - The array of rules with non-ASCII characters converted to punycode.
|
|
9
|
+
*/
|
|
10
|
+
function convertToAscii(rules) {
|
|
11
|
+
return rules.map((rule) => {
|
|
12
|
+
// 1. Check if rule is comment or empty
|
|
13
|
+
if (ruleUtils.isComment(rule) || rule.length === 0) {
|
|
14
|
+
return rule;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// 2. Check if rule contains non ascii characters
|
|
18
|
+
if (!ruleUtils.containsNonAsciiCharacters(rule)) {
|
|
19
|
+
return rule;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// 3. Replace the rule with its punycode version
|
|
23
|
+
const punycodeRule = ruleUtils.convertNonAsciiToPunycode(rule);
|
|
24
|
+
consola.debug(`Converting non-ASCII line ${rule} to punycode ${punycodeRule}`);
|
|
25
|
+
|
|
26
|
+
return punycodeRule;
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
module.exports = convertToAscii;
|
|
@@ -10,6 +10,7 @@ const invertAllow = require('./invertallow');
|
|
|
10
10
|
const removeEmptyLines = require('./remove-empty-lines');
|
|
11
11
|
const trimLines = require('./trim-lines');
|
|
12
12
|
const insertFinalNewLine = require('./insert-final-newline');
|
|
13
|
+
const convertToAscii = require('./covert-to-ascii');
|
|
13
14
|
|
|
14
15
|
/**
|
|
15
16
|
* Enum with all available transformations
|
|
@@ -25,6 +26,7 @@ const TRANSFORMATIONS = Object.freeze({
|
|
|
25
26
|
RemoveEmptyLines: 'RemoveEmptyLines',
|
|
26
27
|
TrimLines: 'TrimLines',
|
|
27
28
|
InsertFinalNewLine: 'InsertFinalNewLine',
|
|
29
|
+
ConvertToAscii: 'ConvertToAscii',
|
|
28
30
|
});
|
|
29
31
|
|
|
30
32
|
/**
|
|
@@ -55,6 +57,9 @@ async function transform(rules, configuration, transformations) {
|
|
|
55
57
|
configuration.inclusions_sources,
|
|
56
58
|
);
|
|
57
59
|
|
|
60
|
+
if (transformations.indexOf(TRANSFORMATIONS.ConvertToAscii) !== -1) {
|
|
61
|
+
transformed = convertToAscii(transformed);
|
|
62
|
+
}
|
|
58
63
|
if (transformations.indexOf(TRANSFORMATIONS.TrimLines) !== -1) {
|
|
59
64
|
transformed = trimLines(transformed);
|
|
60
65
|
}
|
|
@@ -248,51 +248,58 @@ function valid(ruleText, allowedIP) {
|
|
|
248
248
|
* Class representing a validator for a list of rules.
|
|
249
249
|
*/
|
|
250
250
|
class Validator {
|
|
251
|
+
/**
|
|
252
|
+
* Creates a new rule validator.
|
|
253
|
+
* @param {boolean} allowedIP - Flag indicating whether IP addresses should be considered valid.
|
|
254
|
+
*/
|
|
251
255
|
constructor(allowedIP) {
|
|
252
256
|
/**
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
+
* Indicates that a rule was previously removed (the iteration processed an invalid rule).
|
|
258
|
+
* Used to remove preceding comments or empty lines.
|
|
259
|
+
* @type {boolean}
|
|
260
|
+
*/
|
|
261
|
+
this.previousRuleRemoved = false;
|
|
262
|
+
|
|
257
263
|
/**
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
264
|
+
* Flag to allow or disallow IP addresses during validation.
|
|
265
|
+
* @type {boolean}
|
|
266
|
+
*/
|
|
261
267
|
this.allowedIP = allowedIP;
|
|
262
268
|
}
|
|
263
269
|
|
|
264
270
|
/**
|
|
265
|
-
* Validates
|
|
271
|
+
* Validates a list of rules and removes invalid rules.
|
|
266
272
|
* If a rule is invalid, any preceding comments or empty lines are also removed.
|
|
267
273
|
*
|
|
268
|
-
* @
|
|
269
|
-
* @
|
|
274
|
+
* @param {string[]} rules - An array of strings (rules) to validate.
|
|
275
|
+
* @returns {string[]} A filtered list of valid rules.
|
|
270
276
|
*/
|
|
271
277
|
validate(rules) {
|
|
272
|
-
/**
|
|
273
|
-
* A filtered list of rules after validation.
|
|
274
|
-
* @type {Array<string>}
|
|
275
|
-
*/
|
|
276
278
|
const filtered = [...rules];
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
//
|
|
285
|
-
|
|
286
|
-
filtered.splice(
|
|
279
|
+
// Iterate from the end to the beginning so we can remove
|
|
280
|
+
// preceding comments/empty lines if needed
|
|
281
|
+
for (let i = filtered.length - 1; i >= 0; i -= 1) {
|
|
282
|
+
const isValidRule = valid(filtered[i], this.allowedIP);
|
|
283
|
+
const isCommentOrEmptyLine = ruleUtils.isComment(filtered[i]) || _.isEmpty(filtered[i]);
|
|
284
|
+
|
|
285
|
+
if (!isValidRule) {
|
|
286
|
+
// If the rule is invalid, remove it and set the flag to remove preceding lines
|
|
287
|
+
this.previousRuleRemoved = true;
|
|
288
|
+
filtered.splice(i, 1);
|
|
289
|
+
} else if (this.previousRuleRemoved && isCommentOrEmptyLine) {
|
|
290
|
+
// If a previous invalid rule was removed, remove this comment or empty line as well
|
|
291
|
+
consola.debug(`Removing a comment or empty line preceding an invalid rule: ${filtered[i]}`);
|
|
292
|
+
filtered.splice(i, 1);
|
|
287
293
|
} else {
|
|
288
|
-
//
|
|
289
|
-
this.
|
|
294
|
+
// If it's valid and doesn't match the "remove preceding" scenario, reset the removal flag
|
|
295
|
+
this.previousRuleRemoved = false;
|
|
290
296
|
}
|
|
291
297
|
}
|
|
292
298
|
|
|
293
299
|
return filtered;
|
|
294
300
|
}
|
|
295
301
|
}
|
|
302
|
+
|
|
296
303
|
/**
|
|
297
304
|
* Validates a list of rules.
|
|
298
305
|
*
|
|
@@ -88,12 +88,12 @@ describe('Source compiler', () => {
|
|
|
88
88
|
|
|
89
89
|
it('compile the source with transformations', async () => {
|
|
90
90
|
const testList = `
|
|
91
|
-
|
|
91
|
+
|
|
92
92
|
example.org
|
|
93
93
|
|
|
94
|
-
test1.com
|
|
94
|
+
test1.com
|
|
95
95
|
|
|
96
|
-
! comment
|
|
96
|
+
! comment
|
|
97
97
|
test1.com
|
|
98
98
|
|
|
99
99
|
test2.com`;
|
|
@@ -121,4 +121,40 @@ describe('Source compiler', () => {
|
|
|
121
121
|
]);
|
|
122
122
|
scope.done();
|
|
123
123
|
});
|
|
124
|
+
|
|
125
|
+
it('compile the source with converToAscii transformation', async () => {
|
|
126
|
+
const testList = `
|
|
127
|
+
! comment
|
|
128
|
+
||*.ком^
|
|
129
|
+
||*.ком^
|
|
130
|
+
||*.укр^
|
|
131
|
+
||*.мон^
|
|
132
|
+
||*.ευ^
|
|
133
|
+
||*.ελ^
|
|
134
|
+
||*.հայ^`;
|
|
135
|
+
const scope = nock('https://example.org')
|
|
136
|
+
.get('/test-filter.txt')
|
|
137
|
+
.reply(200, testList);
|
|
138
|
+
|
|
139
|
+
const source = {
|
|
140
|
+
name: 'test source',
|
|
141
|
+
source: 'https://example.org/test-filter.txt',
|
|
142
|
+
transformations: [
|
|
143
|
+
TRANSFORMATIONS.ConvertToAscii,
|
|
144
|
+
TRANSFORMATIONS.Deduplicate,
|
|
145
|
+
TRANSFORMATIONS.RemoveComments,
|
|
146
|
+
TRANSFORMATIONS.TrimLines,
|
|
147
|
+
],
|
|
148
|
+
};
|
|
149
|
+
const compiled = await compileSource(source);
|
|
150
|
+
expect(compiled).toEqual([
|
|
151
|
+
'||*.xn--j1aef^',
|
|
152
|
+
'||*.xn--j1amh^',
|
|
153
|
+
'||*.xn--l1acc^',
|
|
154
|
+
'||*.xn--qxa6a^',
|
|
155
|
+
'||*.xn--qxam^',
|
|
156
|
+
'||*.xn--y9a3aq^',
|
|
157
|
+
]);
|
|
158
|
+
scope.done();
|
|
159
|
+
});
|
|
124
160
|
});
|
|
@@ -35,6 +35,7 @@ describe('Configuration', () => {
|
|
|
35
35
|
'Validate',
|
|
36
36
|
'Deduplicate',
|
|
37
37
|
'InvertAllow',
|
|
38
|
+
'ConvertToAscii',
|
|
38
39
|
],
|
|
39
40
|
},
|
|
40
41
|
],
|
|
@@ -45,6 +46,7 @@ describe('Configuration', () => {
|
|
|
45
46
|
'Validate',
|
|
46
47
|
'Deduplicate',
|
|
47
48
|
'InvertAllow',
|
|
49
|
+
'ConvertToAscii',
|
|
48
50
|
],
|
|
49
51
|
});
|
|
50
52
|
expect(ret.valid).toBe(true);
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
const convertToAscii = require('../../src/transformations/covert-to-ascii');
|
|
2
|
+
|
|
3
|
+
describe('Exclusions', () => {
|
|
4
|
+
it('convert non latin symbols in full domain to punycode', () => {
|
|
5
|
+
const rules = `||*.рус^$denyallow=example.com
|
|
6
|
+
||*.कॉम^$denyallow=example.com
|
|
7
|
+
||*.セール^$denyallow=example.com`.split(/\r?\n/);
|
|
8
|
+
const filtered = convertToAscii(rules);
|
|
9
|
+
|
|
10
|
+
expect(filtered).toEqual([
|
|
11
|
+
'||*.xn--p1acf^$denyallow=example.com',
|
|
12
|
+
'||*.xn--11b4c3d^$denyallow=example.com',
|
|
13
|
+
'||*.xn--1ck2e1b^$denyallow=example.com',
|
|
14
|
+
]);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('convert non latin symbols in modifiers to punycode', () => {
|
|
18
|
+
const rules = `||example.com^$denyallow=*.рф
|
|
19
|
+
||example.com^$denyallow=пример.рф
|
|
20
|
+
||example.com^$client=пример.рф`.split(/\r?\n/);
|
|
21
|
+
|
|
22
|
+
const filtered = convertToAscii(rules);
|
|
23
|
+
|
|
24
|
+
expect(filtered).toEqual([
|
|
25
|
+
'||example.com^$denyallow=*.xn--p1ai',
|
|
26
|
+
'||example.com^$denyallow=xn--e1afmkfd.xn--p1ai',
|
|
27
|
+
'||example.com^$client=xn--e1afmkfd.xn--p1ai',
|
|
28
|
+
]);
|
|
29
|
+
});
|
|
30
|
+
});
|
|
@@ -73,4 +73,35 @@ rule2
|
|
|
73
73
|
expect(filtered).toHaveLength(4);
|
|
74
74
|
expect(filtered).toEqual(['! test comment', 'rule1', '||185.149.120.173^', '||example.com^']);
|
|
75
75
|
});
|
|
76
|
+
|
|
77
|
+
it('simple transformations with ConvertToAscii', async () => {
|
|
78
|
+
const rules = `! test comment
|
|
79
|
+
||*.рус^
|
|
80
|
+
||*.कॉम^
|
|
81
|
+
||*.セール^
|
|
82
|
+
||*.佛山^
|
|
83
|
+
||*.ಭಾರತ^
|
|
84
|
+
||*.慈善^
|
|
85
|
+
||*.集团^
|
|
86
|
+
||*.在线^`.split(/\r?\n/);
|
|
87
|
+
const exclusions = [
|
|
88
|
+
'', // empty exclusions are ignored
|
|
89
|
+
'||*.セール^',
|
|
90
|
+
];
|
|
91
|
+
const configuration = {
|
|
92
|
+
exclusions,
|
|
93
|
+
};
|
|
94
|
+
const filtered = await transform(rules, configuration, ['ConvertToAscii']);
|
|
95
|
+
expect(filtered).toHaveLength(8);
|
|
96
|
+
expect(filtered).toEqual([
|
|
97
|
+
'! test comment',
|
|
98
|
+
'||*.xn--p1acf^',
|
|
99
|
+
'||*.xn--11b4c3d^',
|
|
100
|
+
'||*.xn--1qqw23a^',
|
|
101
|
+
'||*.xn--2scrj9c^',
|
|
102
|
+
'||*.xn--30rr7y^',
|
|
103
|
+
'||*.xn--3bst00m^',
|
|
104
|
+
'||*.xn--3ds443g^',
|
|
105
|
+
]);
|
|
106
|
+
});
|
|
76
107
|
});
|