brave/adblock-rust

NodeJS bindings issue

Closed this issue · 0 comments

Unclear why, but there's some bizarre issue in Node where awaiting a fetch changes the behavior of unrelated code later on. In this case it's causing intoContentBlocking to return an object with fields that are undefined.

package.json

{
  "type": "module",
  "dependencies": {
    "adblock-rs": "^0.7.17"
  }
}

index.js

import { FilterSet, FilterFormat, RuleTypes } from 'adblock-rs'
import { readFileSync } from 'fs'

// This line causes the returned fields from `intoContentBlocking` to be `undefined`.
// Everything works as expected when it's commented out.
await fetch("https://easylist-downloads.adblockplus.org/liste_ar.txt").then(r => r.text())

// ...even though we're using rules from disk (the fetch result is unused).
let rules2 = readFileSync('./Liste_AR.txt', 'utf8').trim().split('\n')

const filterSet = new FilterSet(true)
filterSet.addFilters(rules2, { format: FilterFormat.STANDARD, rule_types: RuleTypes.NETWORK_ONLY })
const { contentBlockingRules, filtersUsed } = filterSet.intoContentBlocking()

// logs `undefined undefined`
console.log(typeof contentBlockingRules, typeof filtersUsed)

There's some weird dependency between the rules used and the network request (I can't reproduce it using localhost or my personal website). Based on some poking around at this code, my guess is that the issue exists somewhere in neon-serde, although the fact that the fetch is able to affect the later operations smells like a node bug.