@adguard/hostlist-compiler 1.0.33 → 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 +17 -0
- package/README.md +23 -0
- package/package.json +1 -1
- package/src/index.js +3 -7
- 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/src/utils.js +0 -33
- package/test/compile-source.test.js +39 -3
- package/test/configuration.test.js +2 -0
- package/test/index.test.js +0 -37
- package/test/transformations/covert-to-ascii.test.js +30 -0
- package/test/transformations/transform.test.js +31 -0
- package/test/utils.test.js +0 -54
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,23 @@ 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
|
+
|
|
17
|
+
## [1.0.34] - 2025-01-10
|
|
18
|
+
|
|
19
|
+
### Removed
|
|
20
|
+
|
|
21
|
+
- Calculation of checksum for filters. [#76]
|
|
22
|
+
|
|
23
|
+
[1.0.34]: https://github.com/AdguardTeam/HostlistCompiler/compare/v1.0.33...v1.0.34
|
|
24
|
+
|
|
8
25
|
## [1.0.33] - 2024-12-27
|
|
9
26
|
|
|
10
27
|
### Fixed
|
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/index.js
CHANGED
|
@@ -4,7 +4,6 @@ const config = require('./configuration');
|
|
|
4
4
|
const compileSource = require('./compile-source');
|
|
5
5
|
const { transform } = require('./transformations/transform');
|
|
6
6
|
const packageJson = require('../package.json');
|
|
7
|
-
const { calculateChecksum } = require('./utils');
|
|
8
7
|
|
|
9
8
|
/**
|
|
10
9
|
* Prepares list header
|
|
@@ -15,6 +14,7 @@ const { calculateChecksum } = require('./utils');
|
|
|
15
14
|
*/
|
|
16
15
|
function prepareHeader(configuration) {
|
|
17
16
|
const lines = [
|
|
17
|
+
'!',
|
|
18
18
|
`! Title: ${configuration.name}`,
|
|
19
19
|
];
|
|
20
20
|
|
|
@@ -101,12 +101,8 @@ async function compile(configuration) {
|
|
|
101
101
|
|
|
102
102
|
// Now prepend the list header and we're good to go
|
|
103
103
|
const header = prepareHeader(configuration);
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
// Concat everything together
|
|
107
|
-
const data = [checksum, ...header, ...finalList];
|
|
108
|
-
consola.info(`Final length of the list is ${data.length}`);
|
|
109
|
-
return data;
|
|
104
|
+
consola.info(`Final length of the list is ${header.length + finalList.length}`);
|
|
105
|
+
return header.concat(finalList);
|
|
110
106
|
}
|
|
111
107
|
|
|
112
108
|
module.exports = compile;
|
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
|
*
|
package/src/utils.js
CHANGED
|
@@ -1,39 +1,7 @@
|
|
|
1
1
|
const _ = require('lodash');
|
|
2
2
|
const fs = require('fs').promises;
|
|
3
|
-
const crypto = require('crypto');
|
|
4
3
|
const axios = require('axios');
|
|
5
4
|
|
|
6
|
-
/**
|
|
7
|
-
* Strips specified character from the end of a string.
|
|
8
|
-
*
|
|
9
|
-
* @param {string} string - The string to strip.
|
|
10
|
-
* @param {string} char - The character to strip.
|
|
11
|
-
* @returns {string} - The stripped string.
|
|
12
|
-
*/
|
|
13
|
-
const stripEnd = (str, char) => {
|
|
14
|
-
let result = str;
|
|
15
|
-
while (result.endsWith(char)) {
|
|
16
|
-
result = result.slice(0, -1);
|
|
17
|
-
}
|
|
18
|
-
return result;
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Calculates checksum for the given header and rules.
|
|
23
|
-
* See:
|
|
24
|
-
* https://adblockplus.org/en/filters#special-comments
|
|
25
|
-
* https://hg.adblockplus.org/adblockplus/file/tip/addChecksum.py
|
|
26
|
-
*
|
|
27
|
-
* @param {Array<string>} header - The header lines.
|
|
28
|
-
* @param {Array<string>} rules - The rules lines.
|
|
29
|
-
* @returns {string} - The calculated checksum.
|
|
30
|
-
*/
|
|
31
|
-
const calculateChecksum = (header, rules) => {
|
|
32
|
-
const content = header.concat(rules).join('\n');
|
|
33
|
-
const checksum = crypto.createHash('md5').update(content).digest('base64');
|
|
34
|
-
return `! Checksum: ${stripEnd(checksum.trim(), '=')}`;
|
|
35
|
-
};
|
|
36
|
-
|
|
37
5
|
function isURL(str) {
|
|
38
6
|
try {
|
|
39
7
|
// eslint-disable-next-line no-unused-vars
|
|
@@ -201,7 +169,6 @@ class Wildcard {
|
|
|
201
169
|
}
|
|
202
170
|
|
|
203
171
|
module.exports = {
|
|
204
|
-
calculateChecksum,
|
|
205
172
|
download,
|
|
206
173
|
Wildcard,
|
|
207
174
|
splitByDelimiterWithEscapeCharacter,
|
|
@@ -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);
|
package/test/index.test.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
const nock = require('nock');
|
|
2
2
|
const consola = require('consola');
|
|
3
3
|
const compile = require('../src/index');
|
|
4
|
-
const { calculateChecksum } = require('../src/utils');
|
|
5
4
|
|
|
6
5
|
describe('Hostlist compiler', () => {
|
|
7
6
|
it('compile from multiple sources', async () => {
|
|
@@ -33,7 +32,6 @@ describe('Hostlist compiler', () => {
|
|
|
33
32
|
const list = await compile(configuration);
|
|
34
33
|
|
|
35
34
|
// assert
|
|
36
|
-
expect(list[0].startsWith('! Checksum:')).toBe(true);
|
|
37
35
|
expect(list).toContain('||example.org');
|
|
38
36
|
expect(list).toContain('||example.com');
|
|
39
37
|
expect(list).toContain('! Version: 1.0.0.9');
|
|
@@ -43,39 +41,4 @@ describe('Hostlist compiler', () => {
|
|
|
43
41
|
|
|
44
42
|
scope.done();
|
|
45
43
|
});
|
|
46
|
-
|
|
47
|
-
it('calculates checksum correctly', async () => {
|
|
48
|
-
// Prepare source
|
|
49
|
-
const scope = nock('https://example.org')
|
|
50
|
-
.get('/source1.txt')
|
|
51
|
-
.reply(200, '||example.org');
|
|
52
|
-
|
|
53
|
-
// compiler configuration
|
|
54
|
-
const configuration = {
|
|
55
|
-
name: 'Test filter',
|
|
56
|
-
description: 'Checksum test filter',
|
|
57
|
-
version: '1.0.0.1',
|
|
58
|
-
sources: [
|
|
59
|
-
{
|
|
60
|
-
name: 'source 1',
|
|
61
|
-
source: 'https://example.org/source1.txt',
|
|
62
|
-
},
|
|
63
|
-
],
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
// compile the final list
|
|
67
|
-
const list = await compile(configuration);
|
|
68
|
-
|
|
69
|
-
// assert
|
|
70
|
-
const checksumLine = list[0];
|
|
71
|
-
expect(checksumLine.startsWith('! Checksum:')).toBe(true);
|
|
72
|
-
|
|
73
|
-
const header = list.slice(1, list.indexOf('||example.org'));
|
|
74
|
-
const finalList = list.slice(list.indexOf('||example.org'));
|
|
75
|
-
|
|
76
|
-
const expectedChecksum = calculateChecksum(header, finalList);
|
|
77
|
-
expect(checksumLine).toBe(expectedChecksum);
|
|
78
|
-
|
|
79
|
-
scope.done();
|
|
80
|
-
});
|
|
81
44
|
});
|
|
@@ -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
|
});
|
package/test/utils.test.js
CHANGED
|
@@ -34,57 +34,3 @@ describe('substringBetween', () => {
|
|
|
34
34
|
expect(substr).toBe(null);
|
|
35
35
|
});
|
|
36
36
|
});
|
|
37
|
-
|
|
38
|
-
describe('calculateChecksum', () => {
|
|
39
|
-
it('calculates checksum with multiple headers and rules', () => {
|
|
40
|
-
const header = ['! Title: AdGuard DNS filter', '! Title: Example filter list'];
|
|
41
|
-
const rules = ['||example.com^', '||test.com^'];
|
|
42
|
-
const expectedChecksum = '! Checksum: VSbHtv4WyU405Fks327CsA';
|
|
43
|
-
|
|
44
|
-
const checksum = utils.calculateChecksum(header, rules);
|
|
45
|
-
expect(checksum).toBe(expectedChecksum);
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
it('calculates checksum with empty rules', () => {
|
|
49
|
-
const header = ['[Adblock Plus 2.0]'];
|
|
50
|
-
const rules = [];
|
|
51
|
-
const expectedChecksum = '! Checksum: 87GLA4VsMnV9PiusIkPkEg';
|
|
52
|
-
|
|
53
|
-
const checksum = utils.calculateChecksum(header, rules);
|
|
54
|
-
expect(checksum).toBe(expectedChecksum);
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
it('calculates checksum with empty header and rules', () => {
|
|
58
|
-
const header = [];
|
|
59
|
-
const rules = [];
|
|
60
|
-
const expectedChecksum = '! Checksum: 1B2M2Y8AsgTpgAmY7PhCfg';
|
|
61
|
-
|
|
62
|
-
const checksum = utils.calculateChecksum(header, rules);
|
|
63
|
-
expect(checksum).toBe(expectedChecksum);
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
it('calculates checksum without stripping empty lines', () => {
|
|
67
|
-
const header = [
|
|
68
|
-
'! Title: 1Hosts (mini)',
|
|
69
|
-
// eslint-disable-next-line max-len
|
|
70
|
-
'! Description: List for blocking pesky ads, trackers, and malware. Lenient version: unblocks a number of ads & trackers for in-app rewards, anti-AdBlock, etc.',
|
|
71
|
-
'! Version: 1.0.14.48',
|
|
72
|
-
'! Homepage: https://badmojr.github.io/1Hosts/',
|
|
73
|
-
'! Last modified: 2024-12-27T09:45:56.615Z',
|
|
74
|
-
'!',
|
|
75
|
-
'! Compiled by @adguard/hostlist-compiler v1.0.32',
|
|
76
|
-
'!',
|
|
77
|
-
];
|
|
78
|
-
const rules = [
|
|
79
|
-
'',
|
|
80
|
-
'||logs.netflix.com^',
|
|
81
|
-
'||teams.events.data.microsoft.com^',
|
|
82
|
-
'||measure.office.net^',
|
|
83
|
-
'||app-analytics-services.com^',
|
|
84
|
-
];
|
|
85
|
-
const expectedChecksum = '! Checksum: 4v+tPGfpA0fy+xfhlr8P6Q';
|
|
86
|
-
|
|
87
|
-
const checksum = utils.calculateChecksum(header, rules);
|
|
88
|
-
expect(checksum).toBe(expectedChecksum);
|
|
89
|
-
});
|
|
90
|
-
});
|