build-reject-domainset-worker.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // @ts-check
  2. const Piscina = require('piscina');
  3. // const { isCI } = require('ci-info');
  4. /** @type {string[]} */
  5. const fullsetDomainStartsWithADot = Piscina.workerData
  6. const totalLen = fullsetDomainStartsWithADot.length;
  7. const DOT = '.';
  8. // const log = isCI ? () => { } : console.log.bind(console);
  9. /**
  10. * @param {{ chunk: string[] }} param0
  11. */
  12. module.exports = ({ chunk }) => {
  13. const chunkLength = chunk.length;
  14. const outputToBeRemoved = new Int8Array(chunkLength);
  15. for (let i = 0; i < chunkLength; i++) {
  16. const domainFromInputChunk = chunk[i];
  17. for (let j = 0; j < totalLen; j++) {
  18. const domainStartsWithADotAndFromFullSet = fullsetDomainStartsWithADot[j];
  19. // domainFromFullSet is always startsWith "."
  20. if (domainStartsWithADotAndFromFullSet === domainFromInputChunk) continue;
  21. const domainFromInputLen = domainFromInputChunk.length;
  22. const domainFromFullSetLen = domainStartsWithADotAndFromFullSet.length;
  23. if (domainFromInputLen < domainFromFullSetLen) {
  24. if (domainFromInputLen + 1 !== domainFromFullSetLen) {
  25. break;
  26. }
  27. // !domainFromInput.starsWith('.') && `.${domainFromInput}` === domainFromFullSet
  28. if (
  29. domainFromInputChunk[0] !== DOT
  30. && domainStartsWithADotAndFromFullSet.endsWith(domainFromInputChunk)
  31. ) {
  32. outputToBeRemoved[i] = 1;
  33. // log(domainFromInputChunk, domainStartsWithADotAndFromFullSet)
  34. break;
  35. }
  36. } else if (
  37. domainFromInputLen > domainFromFullSetLen
  38. && domainFromInputChunk.endsWith(domainStartsWithADotAndFromFullSet)
  39. ) {
  40. outputToBeRemoved[i] = 1;
  41. // log(domainFromInputChunk, domainStartsWithADotAndFromFullSet)
  42. break;
  43. }
  44. }
  45. }
  46. return Piscina.move(outputToBeRemoved);
  47. };