build-reject-domainset-worker.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. const { workerData, move } = require('piscina');
  2. const len = workerData.length;
  3. // pre check if fullset domain is starts with a "."
  4. // This avoid calling chatCodeAt repeatedly
  5. const fullsetDomainStartsWithADot = workerData.map(domain => domain.charCodeAt(0) === 46);
  6. module.exports = ({ chunk }) => {
  7. const chunkLength = chunk.length;
  8. const outputToBeRemoved = new Int8Array(chunkLength);
  9. for (let i = 0; i < chunkLength; i++) {
  10. const domainFromInput = chunk[i];
  11. for (let j = 0; j < len; j++) {
  12. // Check if domainFromFullset starts with a "."
  13. if (!fullsetDomainStartsWithADot[j]) continue;
  14. // domainFromFullSet is now startsWith a "."
  15. const domainFromFullSet = workerData[j];
  16. if (domainFromFullSet === domainFromInput) continue;
  17. const domainFromInputLen = domainFromInput.length;
  18. if (domainFromInput.charCodeAt(0) !== 46) {
  19. let shouldBeRemoved = true;
  20. for (let k = 0; k < domainFromInputLen; k++) {
  21. if (domainFromFullSet.charCodeAt(k + 1) !== domainFromInput.charCodeAt(k)) {
  22. shouldBeRemoved = false;
  23. break;
  24. }
  25. }
  26. if (shouldBeRemoved) {
  27. outputToBeRemoved[i] = 1;
  28. break;
  29. }
  30. }
  31. // domainFromInput is now startsWith a "."
  32. if (domainFromInputLen >= domainFromFullSet.length) {
  33. if (domainFromInput.endsWith(domainFromFullSet)) {
  34. outputToBeRemoved[i] = 1;
  35. break;
  36. }
  37. }
  38. }
  39. }
  40. return move(outputToBeRemoved);
  41. };