forked from AdguardTeam/AdguardBrowserExtension
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate-public-suffix-list.js
More file actions
70 lines (63 loc) · 2.13 KB
/
update-public-suffix-list.js
File metadata and controls
70 lines (63 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import gulp from 'gulp';
import request from 'request';
import fs from 'fs';
import path from 'path';
import punycode from 'punycode';
import {
PUBLIC_SUFFIXES_URL,
PUBLIC_SUFFIXES_FILE
} from './consts';
const convertListToObject = (list) => {
const rows = list.split('\n');
const suffixesWithoutCommentsAndSpaces = rows.filter((row) => {
return !(row.length === 0 || row.indexOf('//') !== -1);
});
const suffixesWithoutSpecialRules = suffixesWithoutCommentsAndSpaces.filter(suffix => {
return !(suffix.indexOf('*') !== -1 || suffix.indexOf('!') !== -1);
});
const suffixesInPunycode = suffixesWithoutSpecialRules.map(suffix => {
return punycode.toASCII(suffix);
});
const suffixesObject = {};
suffixesInPunycode
.sort((a, b) => {
if (a === b) {
return 0;
}
return a > b ? 1 : -1;
})
.forEach(suffix => {
suffixesObject[suffix] = 1;
});
return suffixesObject;
};
const writeFileSync = (data, path) => new Promise((resolve, reject) => {
fs.writeFile(path, data, (err) => {
if (err) {
reject(err);
}
resolve();
});
});
const readFileSync = (pathname) => new Promise((resolve, reject) => {
fs.readFile(pathname, 'utf8', (err, data) => {
if (err) {
reject(err);
}
resolve(data);
});
});
const updateSuffixes = () => {
return request(PUBLIC_SUFFIXES_URL, async (error, response, body) => {
const jsonString = JSON.stringify(convertListToObject(body), null, 2);
const data = await readFileSync(path.join(__dirname, PUBLIC_SUFFIXES_FILE));
const updatedData = data.replace(/(\/\/\%START_RESERVED_DOMAINS\%)[\s\S]*?(\/\/\%END_RESERVED_DOMAINS\%)/g, "$1\n" + jsonString + "\n $2");
writeFileSync(updatedData, path.join(__dirname, PUBLIC_SUFFIXES_FILE));
});
};
const startDownload = async (done) => {
await updateSuffixes();
return done();
}
const updatePublicSuffixes = (done) => startDownload(done);
export default gulp.series(updatePublicSuffixes);