From f374c057532084fbd395fae8fdd49564248113ed Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Mon, 19 Sep 2022 20:19:55 -0400 Subject: [PATCH] [mv3] Stick to int32 instead of 8-char hex strings for file hashes --- .github/workflows/mv3.yml | 3 +- .../mv3/extension/js/scripting-manager.js | 17 +++---- platform/mv3/make-rulesets.js | 51 +++++++++++-------- 3 files changed, 39 insertions(+), 32 deletions(-) diff --git a/.github/workflows/mv3.yml b/.github/workflows/mv3.yml index 415c7d6c1..38e4cb3b5 100644 --- a/.github/workflows/mv3.yml +++ b/.github/workflows/mv3.yml @@ -25,6 +25,7 @@ jobs: tools/make-mv3.sh full echo "PACKAGE=$(basename $(ls dist/build/uBOLite_*.mv3.zip))" >> $GITHUB_ENV echo "TAGNAME=$(basename $(ls dist/build/uBOLite_*.mv3.zip) .mv3.zip)" >> $GITHUB_ENV + echo "RELEASENAME=${PACKAGE/_/ }" >> $GITHUB_ENV cp dist/build/uBOLite.mv3/log.txt dist/chromium-mv3/ - name: Commit uBOLite MV3 build log file # https://github.com/marketplace/actions/github-action-for-committing-changes-to-a-repository @@ -39,7 +40,7 @@ jobs: GITHUB_TOKEN: ${{ github.token }} with: tag_name: ${{ env.TAGNAME }} - release_name: ${{ env.TAGNAME }} + release_name: ${{ env.RELEASENAME }} prerelease: true - name: Upload uBOLite MV3 package uses: actions/upload-release-asset@v1 diff --git a/platform/mv3/extension/js/scripting-manager.js b/platform/mv3/extension/js/scripting-manager.js index f7a0b6b55..acfdc6663 100644 --- a/platform/mv3/extension/js/scripting-manager.js +++ b/platform/mv3/extension/js/scripting-manager.js @@ -31,11 +31,6 @@ import { parsedURLromOrigin } from './utils.js'; /******************************************************************************/ -const CSS_TYPE = '0'; // jshint ignore:line -const JS_TYPE = '1'; - -/******************************************************************************/ - let scriptingDetailsPromise; function getScriptingDetails() { @@ -110,13 +105,14 @@ const toRegisterable = (fname, entry) => { } directive.js = [ `/rulesets/js/${fname.slice(0,1)}/${fname.slice(1)}.js` ]; directive.runAt = 'document_start'; - if ( fname.at(-1) === JS_TYPE ) { + if ( (parseInt(fname,16) & MAIN_WORLD_BIT) !== 0 ) { directive.world = 'MAIN'; } - return directive; }; +const MAIN_WORLD_BIT = 0b1; + /******************************************************************************/ const shouldUpdate = (registered, candidate) => { @@ -192,9 +188,10 @@ async function registerInjectable() { const toRegister = new Map(); const checkRealm = (details, prop, hn) => { - const fnames = details[prop]?.get(hn); - if ( fnames === undefined ) { return; } - for ( const fname of fnames ) { + const fids = details[prop]?.get(hn); + if ( fids === undefined ) { return; } + for ( const fid of fids ) { + const fname = fid.toString(16).padStart(8,'0'); const existing = toRegister.get(fname); if ( existing ) { existing[prop].push(hn); diff --git a/platform/mv3/make-rulesets.js b/platform/mv3/make-rulesets.js index 9bfb83d5c..76683334c 100644 --- a/platform/mv3/make-rulesets.js +++ b/platform/mv3/make-rulesets.js @@ -67,8 +67,10 @@ const jsonSetMapReplacer = (k, v) => { return v; }; -const uid = (s, l = 8) => - createHash('sha256').update(s).digest('hex').slice(0,l); +const uidint32 = (s) => { + const h = createHash('sha256').update(s).digest('hex').slice(0,8); + return parseInt(h,16) & 0x7FFFFFFF; +}; /******************************************************************************/ @@ -342,7 +344,7 @@ function loadAllSourceScriptlets() { const globalPatchedScriptletsSet = new Set(); -function addScriptingAPIResources(id, entry, prop, fname) { +function addScriptingAPIResources(id, entry, prop, fid) { if ( entry[prop] === undefined ) { return; } for ( const hn of entry[prop] ) { let details = scriptingDetails.get(id); @@ -353,15 +355,20 @@ function addScriptingAPIResources(id, entry, prop, fname) { }; scriptingDetails.set(id, details); } - let fnames = details[prop].get(hn); - if ( fnames === undefined ) { - fnames = new Set(); - details[prop].set(hn, fnames); + let fids = details[prop].get(hn); + if ( fids === undefined ) { + fids = new Set(); + details[prop].set(hn, fids); } - fnames.add(fname); + fids.add(fid); } } +const toCSSFileId = s => uidint32(s) & ~0b1; +const toJSFileId = s => uidint32(s) | 0b1; +const fileNameFromId = id => id.toString(16).padStart(8,'0'); + + /******************************************************************************/ async function processCosmeticFilters(assetDetails, mapin) { @@ -395,7 +402,7 @@ async function processCosmeticFilters(assetDetails, mapin) { const cssContentMap = new Map(); for ( const entry of optimized ) { // ends-with 0 = css resource - const id = parseInt(uid(entry.selector), 16); + const id = uidint32(entry.selector); let details = cssContentMap.get(id); if ( details === undefined ) { details = { a: entry.selector }; @@ -477,9 +484,10 @@ async function processCosmeticFilters(assetDetails, mapin) { `${JSON.stringify(hostnamesMap, jsonReplacer)}` ); // ends-with 0 = css resource - const fname = uid(patchedScriptlet) + '0'; - if ( globalPatchedScriptletsSet.has(fname) === false ) { - globalPatchedScriptletsSet.add(fname); + const fid = toCSSFileId(patchedScriptlet); + if ( globalPatchedScriptletsSet.has(fid) === false ) { + globalPatchedScriptletsSet.add(fid); + const fname = fileNameFromId(fid); writeFile(`${scriptletDir}/${fname.slice(0,1)}/${fname.slice(1)}.js`, patchedScriptlet, {}); generatedFiles.push(fname); } @@ -488,13 +496,13 @@ async function processCosmeticFilters(assetDetails, mapin) { assetDetails.id, { matches: entry[1].y }, 'matches', - fname + fid ); addScriptingAPIResources( assetDetails.id, { excludeMatches: entry[1].n }, 'excludeMatches', - fname + fid ); } } @@ -622,12 +630,12 @@ async function processScriptletFilters(assetDetails, mapin) { for ( const [ token, argsDetails ] of scriptletDetails ) { const argsMap = Array.from(argsDetails).map(entry => [ - parseInt(uid(entry[0]),16), + uidint32(entry[0]), { a: entry[1].a, n: entry[1].n } ]); const hostnamesMap = new Map(); for ( const [ argsHash, details ] of argsDetails ) { - toHostnamesMap(details.y, parseInt(uid(argsHash),16), hostnamesMap); + toHostnamesMap(details.y, uidint32(argsHash), hostnamesMap); } const patchedScriptlet = originalScriptletMap.get(token) .replace( @@ -638,9 +646,10 @@ async function processScriptletFilters(assetDetails, mapin) { `${JSON.stringify(hostnamesMap, jsonReplacer)}` ); // ends-with 1 = scriptlet resource - const fname = uid(patchedScriptlet) + '1'; - if ( globalPatchedScriptletsSet.has(fname) === false ) { - globalPatchedScriptletsSet.add(fname); + const fid = toJSFileId(patchedScriptlet); + if ( globalPatchedScriptletsSet.has(fid) === false ) { + globalPatchedScriptletsSet.add(fid); + const fname = fileNameFromId(fid); writeFile(`${scriptletDir}/${fname.slice(0,1)}/${fname.slice(1)}.js`, patchedScriptlet, {}); generatedFiles.push(fname); } @@ -649,13 +658,13 @@ async function processScriptletFilters(assetDetails, mapin) { assetDetails.id, { matches: details.y }, 'matches', - fname + fid ); addScriptingAPIResources( assetDetails.id, { excludeMatches: details.n }, 'excludeMatches', - fname + fid ); } }