build-reject-domainset-worker.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. const { workerData, move } = require('piscina');
  2. // pre check if fullset domain is starts with a "."
  3. // This avoid calling chatCodeAt repeatedly
  4. // workerData is an array of string. Sort it by length, short first:
  5. const fullsetDomainStartsWithADot = workerData.sort((a, b) => a.length - b.length).filter(domain => domain.charCodeAt(0) === 46);
  6. const totalLen = fullsetDomainStartsWithADot.length;
  7. module.exports = ({ chunk }) => {
  8. const chunkLength = chunk.length;
  9. const outputToBeRemoved = new Int8Array(chunkLength);
  10. for (let i = 0; i < chunkLength; i++) {
  11. const domainFromInput = chunk[i];
  12. for (let j = 0; j < totalLen; j++) {
  13. const domainFromFullSet = fullsetDomainStartsWithADot[j];
  14. // domainFromFullSet is now startsWith a "."
  15. if (domainFromFullSet === domainFromInput) continue;
  16. const domainFromInputLen = domainFromInput.length;
  17. const domainFromFullSetLen = domainFromFullSet.length;
  18. // !domainFromInput.starsWith('.') && `.${domainFromInput}` === domainFromFullSet
  19. if (domainFromInput.charCodeAt(0) !== 46) {
  20. if (domainFromInputLen + 1 === domainFromFullSetLen) {
  21. let shouldBeRemoved = true;
  22. for (let k = 0; k < domainFromInputLen; k++) {
  23. if (domainFromFullSet.charCodeAt(k + 1) !== domainFromInput.charCodeAt(k)) {
  24. shouldBeRemoved = false;
  25. break;
  26. }
  27. }
  28. if (shouldBeRemoved) {
  29. outputToBeRemoved[i] = 1;
  30. break;
  31. }
  32. }
  33. }
  34. // domainFromInput is now startsWith a "."
  35. if (domainFromInputLen >= domainFromFullSetLen) {
  36. if (domainFromInput.endsWith(domainFromFullSet)) {
  37. outputToBeRemoved[i] = 1;
  38. break;
  39. }
  40. }
  41. }
  42. }
  43. return move(outputToBeRemoved);
  44. };