tools-dedupe-src.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { fdir as Fdir } from 'fdir';
  2. import path from 'node:path';
  3. import fsp from 'node:fs/promises';
  4. import { SOURCE_DIR } from './constants/dir';
  5. import { readFileByLine } from './lib/fetch-text-by-line';
  6. import { processLine } from './lib/process-line';
  7. import { HostnameSmolTrie, HostnameTrie } from './lib/trie';
  8. import { task } from './trace';
  9. const ENFORCED_WHITELIST = [
  10. 'hola.sk',
  11. 'hola.org',
  12. 'hola-shopping.com',
  13. 'mynextphone.io',
  14. 'iadmatapk.nosdn.127.net',
  15. 'httpdns.bilivideo.com',
  16. 'httpdns-v6.gslb.yy.com',
  17. 'twemoji.maxcdn.com',
  18. 'samsungcloudsolution.com',
  19. 'samsungcloudsolution.net',
  20. 'samsungqbe.com',
  21. 'ntp.api.bz',
  22. 'cdn.tuk.dev',
  23. 'vocadb-analytics.fly.dev'
  24. ];
  25. const WHITELIST: string[] = ['ntp.api.bz', 'httpdns.bilivideo.com', 'httpdns-v6.gslb.yy.com', 'smd-cms.nasa.gov', 'ddcdn.comtucdncom.com', 'cpan.noris.de', 'mopnativeadv.037201.com', 'vocadb-analytics.fly.dev', 'iadmatapk.nosdn.127.net', 'ad-stat.ksosoft.com', 'adapi.lenovogame.com', 'img5.gelbooru.com'];
  26. task(require.main === module, __filename)(async (span) => {
  27. const files = await span.traceChildAsync('crawl thru all files', () => new Fdir()
  28. .withFullPaths()
  29. .filter((filepath, isDirectory) => {
  30. if (isDirectory) return true;
  31. const extname = path.extname(filepath);
  32. return extname !== '.js' && extname !== '.ts';
  33. })
  34. .crawl(SOURCE_DIR)
  35. .withPromise());
  36. const whiteTrie = span.traceChildSync('build whitelist trie', () => {
  37. const trie = new HostnameSmolTrie(WHITELIST);
  38. ENFORCED_WHITELIST.forEach((item) => trie.whitelist(item));
  39. return trie;
  40. });
  41. await Promise.all(files.map(file => span.traceChildAsync('dedupe ' + file, () => dedupeFile(file, whiteTrie))));
  42. });
  43. async function dedupeFile(file: string, whitelist: HostnameSmolTrie) {
  44. const result: string[] = [];
  45. const trie = new HostnameTrie();
  46. let line: string | null = '';
  47. for await (const l of readFileByLine(file)) {
  48. line = processLine(l);
  49. if (!line) {
  50. if (l.startsWith('# $ skip_dedupe_src')) {
  51. return;
  52. }
  53. result.push(l); // keep all comments and blank lines
  54. continue;
  55. }
  56. if (trie.has(line)) {
  57. continue; // drop duplicate
  58. }
  59. if (whitelist.has(line)) {
  60. continue; // drop whitelisted items
  61. }
  62. trie.add(line);
  63. result.push(line);
  64. }
  65. return fsp.writeFile(file, result.join('\n') + '\n');
  66. }
  67. // function isDomainSuffix(whiteItem: string, incomingItem: string) {
  68. // const whiteIncludeDomain = whiteItem[0] === '.';
  69. // whiteItem = whiteItem[0] === '.' ? whiteItem.slice(1) : whiteItem;
  70. // if (whiteItem === incomingItem) {
  71. // return true; // as long as exact match, we don't care if subdomain is included or not
  72. // }
  73. // if (whiteIncludeDomain) {
  74. // return incomingItem.endsWith('.' + whiteItem);
  75. // }
  76. // return false;
  77. // }