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(); 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. // 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 // This is because some of the code (e.g. publicsuffixlist.js) sets
// module.exports. Once all included files are written like ES modules, using // module.exports. Once all included files are written like ES modules, using
@ -211,6 +256,7 @@ if ( typeof module !== 'undefined' && typeof exports !== 'undefined' ) {
export { export {
FilteringContext, FilteringContext,
StaticNetFilteringEngine,
enableWASM, enableWASM,
pslInit, pslInit,
createCompiler, createCompiler,

View File

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