diff --git a/platform/nodejs/index.js b/platform/nodejs/index.js index 832843e7c..0a7e360bc 100644 --- a/platform/nodejs/index.js +++ b/platform/nodejs/index.js @@ -182,6 +182,25 @@ async function useLists(lists, options = {}) { /******************************************************************************/ +class MockStorage { + constructor(serialized) { + this.map = new Map(serialized); + } + + async put(assetKey, content) { + this.map.set(assetKey, content); + return ({ assetKey, content }); + } + + async get(assetKey) { + return ({ assetKey, content: this.map.get(assetKey) }); + } + + *[Symbol.iterator]() { + yield* this.map; + } +} + const fctx = new FilteringContext(); let snfeInstance = null; @@ -194,7 +213,7 @@ class StaticNetFilteringEngine { } async useLists(lists) { - return useLists(lists); + await useLists(lists); } matchRequest(details) { @@ -213,7 +232,18 @@ class StaticNetFilteringEngine { return compileList(...args); } - static async create({ noPSL } = {}) { + async serialize() { + const storage = new MockStorage(); + await snfe.toSelfie(storage, 'path'); + return JSON.stringify([...storage]); + } + + async deserialize(serialized) { + const storage = new MockStorage(JSON.parse(serialized)); + await snfe.fromSelfie(storage, 'path'); + } + + static async create({ noPSL = false } = {}) { const instance = new StaticNetFilteringEngine(); if ( noPSL !== true && !pslInit() ) { diff --git a/platform/nodejs/test.js b/platform/nodejs/test.js index 58d732407..d93b6939b 100644 --- a/platform/nodejs/test.js +++ b/platform/nodejs/test.js @@ -42,23 +42,7 @@ function fetch(listName) { }); } -async function main() { - try { - const result = await enableWASM(); - if ( result !== true ) { - console.log('Failed to enable all WASM code paths'); - } - } catch(ex) { - console.log(ex); - } - - const engine = await StaticNetFilteringEngine.create(); - - await engine.useLists([ - fetch('easylist').then(raw => ({ name: 'easylist', raw })), - fetch('easyprivacy').then(raw => ({ name: 'easyprivacy', raw })), - ]); - +function runTests(engine) { let result = 0; // Tests @@ -91,6 +75,35 @@ async function main() { if ( result !== 0 ) { console.log(engine.toLogData()); } +} + +async function main() { + try { + const result = await enableWASM(); + if ( result !== true ) { + console.log('Failed to enable all WASM code paths'); + } + } catch(ex) { + console.log(ex); + } + + const engine = await StaticNetFilteringEngine.create(); + + await engine.useLists([ + fetch('easylist').then(raw => ({ name: 'easylist', raw })), + fetch('easyprivacy').then(raw => ({ name: 'easyprivacy', raw })), + ]); + + runTests(engine); + + const serialized = await engine.serialize(); + engine.useLists([]); + + runTests(engine); + + await engine.deserialize(serialized); + + runTests(engine); process.exit(); }