import {parse, getFingerprint} from './abp-filter-parser.js';
import fs from 'fs';

function discoverMatchingPrefix(bloomFilter, str, prefixLen = 8) {
  for (var i = 0; i < str.length - prefixLen + 1; i++) {
    let sub = str.substring(i, i + prefixLen);
    let cleaned = sub.replace(/^https?:\/\//, '');
    if (bloomFilter.exists(cleaned)) {
      console.log('bad-fingerprint:', sub, 'for url:', str);
    }
  }
}

let sitesToCheck = [
  'http://static.digg.com/static/fe/229ff0/images/reader/top-setting@2x.png'
];

fs.readFile('./test/data/easylist.txt', 'utf8', function (err,data) {
  if (err) {
    return console.log(err);
  }

  let parserData = {};
  parse(data, parserData);

  // Write out the bloom filter data files
  fs.writeFileSync('bloomFilterData', new Buffer(new Uint8Array(parserData.bloomFilter.toJSON())));
  fs.writeFileSync('hostBloomFilterData', new Buffer(new Uint8Array(parserData.hostBloomFilter.toJSON())));
  fs.writeFileSync('exceptionBloomFilterData', new Buffer(new Uint8Array(parserData.exceptionBloomFilter.toJSON())));

  //console.log('Number of filters processed: ', parserData.filterCount);

  console.log('-------');
  sitesToCheck.forEach(s =>
    discoverMatchingPrefix(parserData.bloomFilter, s));

  // WRite out the POD cached filter data JSM
  delete parserData.bloomFilter;
  delete parserData.hostBloomFilter;
  delete parserData.exceptionBloomFilter;
  let cachedFilterDataJSM = 'dump("######Loaded cached-rules.jsm\\n");\nthis.EXPORTED_SYMBOLS = ["parserData"];\nthis.parserData = ' + JSON.stringify(parserData) + ';\n';
  fs.writeFileSync('cachedFilterData.jsm', cachedFilterDataJSM);
});

@@ -75,16 +75,61 @@
 om 'fs';
+%0Aimport %7BBloomFilter%7D from 'bloom-filter-js';
 %0A%0Afuncti
@@ -189,16 +189,128 @@
  = 8) %7B%0A
+  if (!bloomFilter.substringExists(str, prefixLen)) %7B%0A    console.log('no substring exists for url:', str);%0A  %7D%0A
   for (v
@@ -604,74 +604,105 @@
 p://
-static.digg.com/static/fe/229ff0/images/reader/top-setting@2x.png'
+c.s-microsoft.com/en-ca/CMSImages/store_symbol.png?version=e2eecca5-4550-10c6-57b1-5114804a4c01',
 %0A%5D;%0A
@@ -1236,24 +1236,215 @@
 JSON())));%0A%0A
+  let readData = fs.readFileSync('./bloomFilterData');%0A  let bloomData = new BloomFilter(new Uint8Array(readData));%0A  console.log(bloomData);%0A  let bloomFilter = new BloomFilter(bloomData);%0A%0A
   //console.
@@ -1506,16 +1506,17 @@
 ount);%0A%0A
+%0A
   consol
@@ -1588,16 +1588,29 @@
 gPrefix(
+bloomFilter/*
 parserDa
@@ -1623,16 +1623,18 @@
 omFilter
+*/
 , s));%0A%0A

Add substring not exists check and add serialize example