Skip to content

Commit f374c05

Browse files
committed
[mv3] Stick to int32 instead of 8-char hex strings for file hashes
1 parent 90f666f commit f374c05

File tree

3 files changed

+39
-32
lines changed

3 files changed

+39
-32
lines changed

.github/workflows/mv3.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ jobs:
2525
tools/make-mv3.sh full
2626
echo "PACKAGE=$(basename $(ls dist/build/uBOLite_*.mv3.zip))" >> $GITHUB_ENV
2727
echo "TAGNAME=$(basename $(ls dist/build/uBOLite_*.mv3.zip) .mv3.zip)" >> $GITHUB_ENV
28+
echo "RELEASENAME=${PACKAGE/_/ }" >> $GITHUB_ENV
2829
cp dist/build/uBOLite.mv3/log.txt dist/chromium-mv3/
2930
- name: Commit uBOLite MV3 build log file
3031
# https://github.com/marketplace/actions/github-action-for-committing-changes-to-a-repository
@@ -39,7 +40,7 @@ jobs:
3940
GITHUB_TOKEN: ${{ github.token }}
4041
with:
4142
tag_name: ${{ env.TAGNAME }}
42-
release_name: ${{ env.TAGNAME }}
43+
release_name: ${{ env.RELEASENAME }}
4344
prerelease: true
4445
- name: Upload uBOLite MV3 package
4546
uses: actions/upload-release-asset@v1

platform/mv3/extension/js/scripting-manager.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,6 @@ import { parsedURLromOrigin } from './utils.js';
3131

3232
/******************************************************************************/
3333

34-
const CSS_TYPE = '0'; // jshint ignore:line
35-
const JS_TYPE = '1';
36-
37-
/******************************************************************************/
38-
3934
let scriptingDetailsPromise;
4035

4136
function getScriptingDetails() {
@@ -110,13 +105,14 @@ const toRegisterable = (fname, entry) => {
110105
}
111106
directive.js = [ `/rulesets/js/${fname.slice(0,1)}/${fname.slice(1)}.js` ];
112107
directive.runAt = 'document_start';
113-
if ( fname.at(-1) === JS_TYPE ) {
108+
if ( (parseInt(fname,16) & MAIN_WORLD_BIT) !== 0 ) {
114109
directive.world = 'MAIN';
115110
}
116-
117111
return directive;
118112
};
119113

114+
const MAIN_WORLD_BIT = 0b1;
115+
120116
/******************************************************************************/
121117

122118
const shouldUpdate = (registered, candidate) => {
@@ -192,9 +188,10 @@ async function registerInjectable() {
192188
const toRegister = new Map();
193189

194190
const checkRealm = (details, prop, hn) => {
195-
const fnames = details[prop]?.get(hn);
196-
if ( fnames === undefined ) { return; }
197-
for ( const fname of fnames ) {
191+
const fids = details[prop]?.get(hn);
192+
if ( fids === undefined ) { return; }
193+
for ( const fid of fids ) {
194+
const fname = fid.toString(16).padStart(8,'0');
198195
const existing = toRegister.get(fname);
199196
if ( existing ) {
200197
existing[prop].push(hn);

platform/mv3/make-rulesets.js

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,10 @@ const jsonSetMapReplacer = (k, v) => {
6767
return v;
6868
};
6969

70-
const uid = (s, l = 8) =>
71-
createHash('sha256').update(s).digest('hex').slice(0,l);
70+
const uidint32 = (s) => {
71+
const h = createHash('sha256').update(s).digest('hex').slice(0,8);
72+
return parseInt(h,16) & 0x7FFFFFFF;
73+
};
7274

7375
/******************************************************************************/
7476

@@ -342,7 +344,7 @@ function loadAllSourceScriptlets() {
342344

343345
const globalPatchedScriptletsSet = new Set();
344346

345-
function addScriptingAPIResources(id, entry, prop, fname) {
347+
function addScriptingAPIResources(id, entry, prop, fid) {
346348
if ( entry[prop] === undefined ) { return; }
347349
for ( const hn of entry[prop] ) {
348350
let details = scriptingDetails.get(id);
@@ -353,15 +355,20 @@ function addScriptingAPIResources(id, entry, prop, fname) {
353355
};
354356
scriptingDetails.set(id, details);
355357
}
356-
let fnames = details[prop].get(hn);
357-
if ( fnames === undefined ) {
358-
fnames = new Set();
359-
details[prop].set(hn, fnames);
358+
let fids = details[prop].get(hn);
359+
if ( fids === undefined ) {
360+
fids = new Set();
361+
details[prop].set(hn, fids);
360362
}
361-
fnames.add(fname);
363+
fids.add(fid);
362364
}
363365
}
364366

367+
const toCSSFileId = s => uidint32(s) & ~0b1;
368+
const toJSFileId = s => uidint32(s) | 0b1;
369+
const fileNameFromId = id => id.toString(16).padStart(8,'0');
370+
371+
365372
/******************************************************************************/
366373

367374
async function processCosmeticFilters(assetDetails, mapin) {
@@ -395,7 +402,7 @@ async function processCosmeticFilters(assetDetails, mapin) {
395402
const cssContentMap = new Map();
396403
for ( const entry of optimized ) {
397404
// ends-with 0 = css resource
398-
const id = parseInt(uid(entry.selector), 16);
405+
const id = uidint32(entry.selector);
399406
let details = cssContentMap.get(id);
400407
if ( details === undefined ) {
401408
details = { a: entry.selector };
@@ -477,9 +484,10 @@ async function processCosmeticFilters(assetDetails, mapin) {
477484
`${JSON.stringify(hostnamesMap, jsonReplacer)}`
478485
);
479486
// ends-with 0 = css resource
480-
const fname = uid(patchedScriptlet) + '0';
481-
if ( globalPatchedScriptletsSet.has(fname) === false ) {
482-
globalPatchedScriptletsSet.add(fname);
487+
const fid = toCSSFileId(patchedScriptlet);
488+
if ( globalPatchedScriptletsSet.has(fid) === false ) {
489+
globalPatchedScriptletsSet.add(fid);
490+
const fname = fileNameFromId(fid);
483491
writeFile(`${scriptletDir}/${fname.slice(0,1)}/${fname.slice(1)}.js`, patchedScriptlet, {});
484492
generatedFiles.push(fname);
485493
}
@@ -488,13 +496,13 @@ async function processCosmeticFilters(assetDetails, mapin) {
488496
assetDetails.id,
489497
{ matches: entry[1].y },
490498
'matches',
491-
fname
499+
fid
492500
);
493501
addScriptingAPIResources(
494502
assetDetails.id,
495503
{ excludeMatches: entry[1].n },
496504
'excludeMatches',
497-
fname
505+
fid
498506
);
499507
}
500508
}
@@ -622,12 +630,12 @@ async function processScriptletFilters(assetDetails, mapin) {
622630

623631
for ( const [ token, argsDetails ] of scriptletDetails ) {
624632
const argsMap = Array.from(argsDetails).map(entry => [
625-
parseInt(uid(entry[0]),16),
633+
uidint32(entry[0]),
626634
{ a: entry[1].a, n: entry[1].n }
627635
]);
628636
const hostnamesMap = new Map();
629637
for ( const [ argsHash, details ] of argsDetails ) {
630-
toHostnamesMap(details.y, parseInt(uid(argsHash),16), hostnamesMap);
638+
toHostnamesMap(details.y, uidint32(argsHash), hostnamesMap);
631639
}
632640
const patchedScriptlet = originalScriptletMap.get(token)
633641
.replace(
@@ -638,9 +646,10 @@ async function processScriptletFilters(assetDetails, mapin) {
638646
`${JSON.stringify(hostnamesMap, jsonReplacer)}`
639647
);
640648
// ends-with 1 = scriptlet resource
641-
const fname = uid(patchedScriptlet) + '1';
642-
if ( globalPatchedScriptletsSet.has(fname) === false ) {
643-
globalPatchedScriptletsSet.add(fname);
649+
const fid = toJSFileId(patchedScriptlet);
650+
if ( globalPatchedScriptletsSet.has(fid) === false ) {
651+
globalPatchedScriptletsSet.add(fid);
652+
const fname = fileNameFromId(fid);
644653
writeFile(`${scriptletDir}/${fname.slice(0,1)}/${fname.slice(1)}.js`, patchedScriptlet, {});
645654
generatedFiles.push(fname);
646655
}
@@ -649,13 +658,13 @@ async function processScriptletFilters(assetDetails, mapin) {
649658
assetDetails.id,
650659
{ matches: details.y },
651660
'matches',
652-
fname
661+
fid
653662
);
654663
addScriptingAPIResources(
655664
assetDetails.id,
656665
{ excludeMatches: details.n },
657666
'excludeMatches',
658-
fname
667+
fid
659668
);
660669
}
661670
}

0 commit comments

Comments
 (0)