Implement class StaticNetFilteringEngine (#3805)

This commit is contained in:
Manish Jethani 2021-08-08 16:52:02 +05:30 committed by GitHub
parent 500c895f6b
commit 65f0909ba0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 73 additions and 22 deletions

View File

@ -201,6 +201,51 @@ function reset() {
snfe.reset();
}
/******************************************************************************/
let pslInitialized = false;
let staticNetFilteringEngineInstance = null;
class StaticNetFilteringEngine {
constructor() {
if ( staticNetFilteringEngineInstance !== null ) {
throw new Error('Only a single instance is supported.');
}
staticNetFilteringEngineInstance = this;
this._context = new FilteringContext();
}
async useLists(lists) {
await useRawLists(lists);
}
matchRequest({ url, originURL, type }) {
this._context.setDocOriginFromURL(originURL);
this._context.setURL(url);
this._context.setType(type);
return snfe.matchRequest(this._context);
}
toLogData() {
return snfe.toLogData();
}
}
StaticNetFilteringEngine.initialize = async function initialize() {
if ( !pslInitialized ) {
if ( !pslInit() ) {
throw new Error('Failed to initialize public suffix list.');
}
pslInitialized = true;
}
};
/******************************************************************************/
// rollup.js needs module.exports to be set back to the local exports object.
// This is because some of the code (e.g. publicsuffixlist.js) sets
// module.exports. Once all included files are written like ES modules, using
@ -211,6 +256,7 @@ if ( typeof module !== 'undefined' && typeof exports !== 'undefined' ) {
export {
FilteringContext,
StaticNetFilteringEngine,
enableWASM,
pslInit,
createCompiler,

View File

@ -30,9 +30,7 @@ import { createRequire } from 'module';
import {
enableWASM,
FilteringContext,
pslInit,
useRawLists,
StaticNetFilteringEngine,
} from './index.js';
/******************************************************************************/
@ -54,39 +52,46 @@ async function main() {
console.log(ex);
}
await pslInit();
await StaticNetFilteringEngine.initialize();
const snfe = await useRawLists([
const engine = new StaticNetFilteringEngine();
await engine.useLists([
fetch('easylist').then(raw => ({ name: 'easylist', raw })),
fetch('easyprivacy').then(raw => ({ name: 'easyprivacy', raw })),
]);
// Reuse filtering context: it's what uBO does
const fctxt = new FilteringContext();
let result = null;
// Tests
// Not blocked
fctxt.setDocOriginFromURL('https://www.bloomberg.com/');
fctxt.setURL('https://www.bloomberg.com/tophat/assets/v2.6.1/that.css');
fctxt.setType('stylesheet');
if ( snfe.matchRequest(fctxt) !== 0 ) {
console.log(snfe.toLogData());
result = engine.matchRequest({
originURL: 'https://www.bloomberg.com/',
url: 'https://www.bloomberg.com/tophat/assets/v2.6.1/that.css',
type: 'stylesheet'
});
if ( result !== 0 ) {
console.log(engine.toLogData());
}
// Blocked
fctxt.setDocOriginFromURL('https://www.bloomberg.com/');
fctxt.setURL('https://securepubads.g.doubleclick.net/tag/js/gpt.js');
fctxt.setType('script');
if ( snfe.matchRequest(fctxt) !== 0 ) {
console.log(snfe.toLogData());
result = engine.matchRequest({
originURL: 'https://www.bloomberg.com/',
url: 'https://securepubads.g.doubleclick.net/tag/js/gpt.js',
type: 'script'
});
if ( result !== 0 ) {
console.log(engine.toLogData());
}
// Unblocked
fctxt.setDocOriginFromURL('https://www.bloomberg.com/');
fctxt.setURL('https://sourcepointcmp.bloomberg.com/ccpa.js');
fctxt.setType('script');
if ( snfe.matchRequest(fctxt) !== 0 ) {
console.log(snfe.toLogData());
result = engine.matchRequest({
originURL: 'https://www.bloomberg.com/',
url: 'https://sourcepointcmp.bloomberg.com/ccpa.js',
type: 'script'
});
if ( result !== 0 ) {
console.log(engine.toLogData());
}
process.exit();