Fix the salvaging of rule ids

This commit is contained in:
Raymond Hill 2024-03-11 22:22:57 -04:00
parent 7c14b3963e
commit 2c9ab8ee04
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
2 changed files with 18 additions and 19 deletions

View File

@ -79,21 +79,21 @@ async function main() {
a.id = 0;
return [ JSON.stringify(a), id ];
}));
const usedIds = new Set();
const reusedIds = new Set();
for ( const afterRule of afterRules ) {
afterRule.id = 0;
const key = JSON.stringify(afterRule);
const beforeId = beforeMap.get(key);
if ( beforeId === undefined ) { continue; }
if ( usedIds.has(beforeId) ) { continue; }
if ( reusedIds.has(beforeId) ) { continue; }
afterRule.id = beforeId;
usedIds.add(beforeId);
reusedIds.add(beforeId);
}
// Assign new ids to unmatched rules
let ruleIdGenerator = 1;
for ( const afterRule of afterRules ) {
if ( afterRule.id !== 0 ) { continue; }
while ( usedIds.has(ruleIdGenerator) ) { ruleIdGenerator += 1; }
while ( reusedIds.has(ruleIdGenerator) ) { ruleIdGenerator += 1; }
afterRule.id = ruleIdGenerator++;
}
afterRules.sort((a, b) => a.id - b.id);
@ -102,11 +102,10 @@ async function main() {
for ( const afterRule of afterRules ) {
lines.push(JSON.stringify(afterRule, null, indent));
}
const path = `${afterDir}/rulesets/${folder}/${file}`;
console.log(` Salvaged ${reusedIds.size} ids in ${folder}/${file}`);
writePromises.push(
fs.writeFile(
`${afterDir}/rulesets/${folder}/${file}`,
`[\n${lines.join(',\n')}\n]\n`
)
fs.writeFile(path, `[\n${lines.join(',\n')}\n]\n`)
);
}
}

View File

@ -13,32 +13,30 @@ for i in "$@"; do
case $i in
quick)
QUICK="yes"
shift # past argument=value
;;
full)
FULL="yes"
shift # past argument=value
;;
firefox)
PLATFORM="firefox"
shift # past argument=value
;;
chromium)
PLATFORM="chromium"
shift # past argument=value
;;
uBOLite_+([0-9]).+([0-9]).+([0-9]).+([0-9]))
TAGNAME="$i"
FULL="yes"
shift # past argument=value
;;
before=+([print]))
before=+([[:print:]]))
BEFORE="${i:7}"
shift # past argument=value
;;
esac
done
echo "PLATFORM=$PLATFORM"
echo "TAGNAME=$TAGNAME"
echo "BEFORE=$BEFORE"
DES="dist/build/uBOLite.$PLATFORM"
if [ "$QUICK" != "yes" ]; then
@ -117,6 +115,12 @@ if [ "$QUICK" != "yes" ]; then
cp "$UBO_DIR"/src/web_accessible_resources/* "$TMPDIR"/web_accessible_resources/
cd "$TMPDIR"
node --no-warnings make-rulesets.js output="$DES" platform="$PLATFORM"
if [ -n "$BEFORE" ]; then
echo "*** uBOLite.mv3: salvaging rule ids to minimize diff size"
echo " before=$BEFORE/$PLATFORM"
echo " after=$DES"
node salvage-ruleids.mjs before="$BEFORE"/"$PLATFORM" after="$DES"
fi
cd - > /dev/null
rm -rf "$TMPDIR"
fi
@ -132,10 +136,6 @@ if [ -z "$TAGNAME" ] && [ "$PLATFORM" = "firefox" ]; then
fi
if [ "$FULL" = "yes" ]; then
if [ -n "$BEFORE" ]; then
echo "*** uBOLite.mv3: salvaging rule ids to minimize diff size"
node salvage-ruleids.mjs before="$BEFORE"/"$PLATFORM" after="$DES"
fi
EXTENSION="zip"
if [ "$PLATFORM" = "firefox" ]; then
EXTENSION="xpi"