From 65f0909ba08c938473ab45f7b54aa31cc9d49a3a Mon Sep 17 00:00:00 2001 From: Manish Jethani Date: Sun, 8 Aug 2021 16:52:02 +0530 Subject: [PATCH] Implement class StaticNetFilteringEngine (#3805) --- platform/nodejs/index.js | 46 +++++++++++++++++++++++++++++++++++++ platform/nodejs/test.js | 49 ++++++++++++++++++++++------------------ 2 files changed, 73 insertions(+), 22 deletions(-) diff --git a/platform/nodejs/index.js b/platform/nodejs/index.js index 4cb0011c2..ac87b1653 100644 --- a/platform/nodejs/index.js +++ b/platform/nodejs/index.js @@ -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, diff --git a/platform/nodejs/test.js b/platform/nodejs/test.js index 0c8466102..f4691edac 100644 --- a/platform/nodejs/test.js +++ b/platform/nodejs/test.js @@ -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();